Skip to content

Style properties set on detached elements cannot be read #380

@niloc132

Description

@niloc132

Tested in latest stable, HtmlUnit 2.52.0.

Given an element, either newly created, or detached from its document, if it has a style set programmatically (not on the html attribute style), it will not be available to be read. Additionally, reading the attribute style will result in null, rather than either an empty string, or the correct string that includes the set style properties.

Example HTML to demonstrate this in a few ways:

<html>
<body>
<div id="style-already-set" style="vertical-align: bottom"></div>
<div id="style-unset"></div>
</body>
<script>
var newlyCreatedWithProperty = document.createElement('div');
newlyCreatedWithProperty.style.verticalAlign = 'bottom';
logStyleProperty('newlyCreatedWithProperty', newlyCreatedWithProperty);

var newlyCreatedWithAttribute = document.createElement('div');
newlyCreatedWithAttribute.setAttribute('style', 'vertical-align:bottom');
logStyleProperty('newlyCreatedWithProperty', newlyCreatedWithAttribute);


var styleAlreadySet = document.getElementById('style-already-set');
document.body.removeChild(styleAlreadySet);
logStyleProperty("styleAlreadySet", styleAlreadySet);

var styleUnset = document.getElementById('style-unset');
styleUnset.style.verticalAlign = 'bottom';
document.body.removeChild(styleUnset);
logStyleProperty("styleUnset", styleUnset);


function logStyleProperty(prefix, element) {
    var style = element.style;
    // check the style prop in two different ways, just in case there is a variation
    console.log(prefix + ".style.getPropertyValue('vertical-align')", style.getPropertyValue('vertical-align'));
    console.log(prefix + ".style['verticalAlign']", style['verticalAlign']);
    console.log(prefix + ".getAttribute('style')", element.getAttribute('style'));
}
</script>
</html>

Simple Java main to reproduce, assuming the above file is served from localhost:8000/style-get.html

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

import java.io.IOException;
import java.net.MalformedURLException;

public class HtmlUnitMain {

    public static void main(String[] args) {
        try (WebClient webClient = new WebClient()) {
            HtmlPage page = webClient.getPage("http://localhost:8000/style-get.html");

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }
}

Expected log, as seen in firefox (and chrome), note that every line contains "bottom" in some form:

newlyCreatedWithProperty.style.getPropertyValue('vertical-align') bottom
newlyCreatedWithProperty.style['verticalAlign'] bottom
newlyCreatedWithProperty.getAttribute('style') vertical-align: bottom;
newlyCreatedWithProperty.style.getPropertyValue('vertical-align') bottom
newlyCreatedWithProperty.style['verticalAlign'] bottom
newlyCreatedWithProperty.getAttribute('style') vertical-align:bottom
styleAlreadySet.style.getPropertyValue('vertical-align') bottom 
styleAlreadySet.style['verticalAlign'] bottom
styleAlreadySet.getAttribute('style') vertical-align: bottom
styleUnset.style.getPropertyValue('vertical-align') bottom
styleUnset.style['verticalAlign'] bottom
styleUnset.getAttribute('style') vertical-align: bottom;

Actual HtmlUnit log output - rows without "bottom" are exhibiting the bug:

INFO: newlyCreatedWithProperty.style.getPropertyValue('vertical-align') 
INFO: newlyCreatedWithProperty.style['verticalAlign'] 
INFO: newlyCreatedWithProperty.getAttribute('style') null
INFO: newlyCreatedWithProperty.style.getPropertyValue('vertical-align') bottom
INFO: newlyCreatedWithProperty.style['verticalAlign'] bottom
INFO: newlyCreatedWithProperty.getAttribute('style') vertical-align:bottom
INFO: styleAlreadySet.style.getPropertyValue('vertical-align') bottom
INFO: styleAlreadySet.style['verticalAlign'] bottom
INFO: styleAlreadySet.getAttribute('style') vertical-align: bottom
INFO: styleUnset.style.getPropertyValue('vertical-align') 
INFO: styleUnset.style['verticalAlign'] 
INFO: styleUnset.getAttribute('style') null

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions