On Thursday 2002-12-26 15:17 +0100, Maurice PHILIPPE wrote:
> I'm trying to catch background and foreground color of a frame in mozilla.
> when the color is defined in the HTML document via a TAG, I can get it via
> the contentNode but
> when it is defined via a CSS link, I can't.
> I believe the right way is to use the nsStyleContext and inheritance
> information but I don't know how
> to interpret an d use the mBits value. Perhaps can I use the nsRuleNode
> class ?

If you want to use a stable API, the best way is probably something
like this, although it will give you a string rather than a color value:

  nsCOMPtr<nsIDOMElement> domElt = do_QueryInterface(aNode);
  NS_ENSURE_TRUE(domElt, NS_ERROR_UNEXPECTED);
 
  nsCOMPtr<nsIDOMDocument> domDoc;
  aNode->GetOwnerDocument(getter_AddRefs(domDoc));
  nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
  NS_ENSURE_TRUE(doc, NS_ERROR_UNEXPECTED);
     
  nsCOMPtr<nsIScriptGlobalObject> global;
  doc->GetScriptGlobalObject(getter_AddRefs(global));
  nsCOMPtr<nsIDOMViewCSS> viewCSS = do_QueryInterface(global);
              
  nsCOMPtr<nsIDOMCSSStyleDeclaration> cssDecl;
  viewCSS->GetComputedStyle(domElt, NS_LITERAL_STRING(""),
                            getter_AddRefs(cssDecl));

  nsAutoString bgColor;
  cssDecl->GetPropertyValue(NS_LITERAL_STRING("background-color"), bgColor);

If you want to use the style context (although the API may change, it
won't be binary-compatible, and it will be unlikely to continue to work
from outside layout), you could do:

  const nsStyleBackground *bg;
  ::GetStyleData(aFrame, &bg);

and then look at the background color value in the nsStyleBackground
object.

-David

-- 
L. David Baron        <URL: http://www.people.fas.harvard.edu/~dbaron/ >

Reply via email to