How to access CSS properties for a DOM node using the Cobra/Lobo HTML renderer

487 views Asked by At

I am trying figure out how to access CSS properties for a DOM node (in this example <img> nodes) using the Cobra/Lobo toolkit. What I have at the moment is:

    UserAgentContext uacontext = new SimpleUserAgentContext();
    DocumentBuilderImpl builder = new DocumentBuilderImpl(uacontext);

    URL url = new URL(TEST_URI);
    InputStream in = url.openConnection().getInputStream();

    Reader reader = new InputStreamReader(in, "ISO-8859-1");
    InputSourceImpl inputSource = new InputSourceImpl(reader, TEST_URI);
    HTMLDocumentImpl d = (HTMLDocumentImpl) builder.parse(inputSource);
    HTMLCollection images = d.getImages();

    for (int i = 0; i < images.getLength(); i++) {

        HTMLElementImpl n = (HTMLElementImpl) images.item(i);
        AbstractCSS2Properties curr = n.getCurrentStyle();

        System.out.println("Image " + i + ": " + curr.getPropertyValue("background-color"));
    }

Now this only seems to give me directly set styles - not inherited or computed styles. How can I get these as well?

Thanks

1

There are 1 answers

0
ChrisW On

I expect you need to use the getComputedStyle method of Class HTMLElementImpl.