Skip to content

CSSStyleDeclaration not able to calculate float values into pixel #302

Description

@Deadc00l

If you have a font-size of e.g. 0.6em, the current result of CSSStyleDeclaration.pixelValuel(final string value) is 0, since this method converts the float value into an integer.
With a simple regex adjustment like: "(\d+(?:\.\d+)?).*" it would be possible to cover also this case.

Activity

rbri commented on Feb 3, 2021

@rbri
Member

Sounds good - do you like to create a PR (including a unit test)?

Deadc00l commented on Feb 12, 2021

@Deadc00l
ContributorAuthor

Deadc00l commented on Feb 18, 2021

@Deadc00l
ContributorAuthor

Is there any news about the pullrequest?

rbri commented on Feb 18, 2021

@rbri
Member

Any chance to add a test case to show the problem?

Deadc00l commented on Feb 18, 2021

@Deadc00l
ContributorAuthor

I am unable to check in the code right now. However, the test should look like this

/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/ComputedCSSStyleDeclarationTest.java

    /**
     * @throws Exception if an error occurs
     */
    @Test
    @Alerts("9.6px")
    public void fontSize2() throws Exception {
        final String html = "<html><body>\n"
            + "<div id='d0' style='font-size: 0.6em;'>\n"
            + "<div id='d1'>inside</div>\n"
            + "</div>\n"
            + "<script>\n"
            + "function getStyle(x) {\n"
            + "  var d = document.getElementById(x);\n"
            + "  var cs = window.getComputedStyle(d, null);\n"
            + "  return cs;\n"
            + "}\n"
            + "var cs1 = getStyle('d1');\n"
            + "alert(cs1.fontSize);\n"
            + "</script>\n"
            + "</body></html>";
        loadPageWithAlerts2(html);
    }

The expected value is 9.6px. Since changing the return type to float for the method com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleDeclaration.pixelValue (String) is not so easy from my point of view, I rounded the calculation. The result is 10px (instead of 9.6px). Currently 0.6em is parsed as int (= 0em) and the result is accordingly also 0px, which is wrong.

rbri commented on Feb 22, 2021

@rbri
Member

Many thanks for your contribution - will make a new snapshot build available soon - check twitter

rbri commented on Feb 24, 2021

@rbri
Member

Snapshot is out - again thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    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