Skip to content

OffsetHeight not calculated properly #124

@dvinihan

Description

@dvinihan

Running version 2.36.0.

When I build an HTMLUnit page using the following HTML, the offsetHeight is not properly calculated. It resolves to the height of the inner "title-sizer" element, rather than the full height rendered by the long text in the "title" element.

This code works correctly in the Chrome browser when opened from an .html file.

<!DOCTYPE html>
<html>
  <head>
    <title>ESS Template Generator</title>
    <meta charset="UTF-8" content="" />
    <style>
      .title-box {
        position: absolute;
        top: 430px;
        left: 75px;
        width: 960px;
        margin-top: 6px;
        font-size: 60px;
      }
      .title-sizer {
        height: 300px;
      }
    </style>
  </head>
  <body>
    <div class="title-box">
      <span class="title-sizer"
        ><span class="title"
          >8oz steak from Good and Gather. 8oz steak from Good and Gather. 8oz
          steak from Good and Gather. 8oz steak from Good and Gather. 8oz steak
          from Good and Gather. 8oz steak from Good and Gather. 8oz steak from
          Good and Gather. 8oz steak from Good and Gather.</span
        ></span
      >
    </div>
  </body>
  <script>
    function getAttributeValue(element, attribute) {
      if (element) {
        return window.getComputedStyle(element)[attribute].split("px")[0];
      }
      return 0;
    }

    var titleBox = document.querySelector(".title-sizer");
    var title = document.querySelector(".title");

    if (titleBox) {
      var titleHeight = titleBox.offsetHeight;
      alert(titleHeight);
      var titleFontSize = getAttributeValue(titleBox, "fontSize");
      var titleHeightGoal = getAttributeValue(titleBox, "height");

      while (titleHeight > titleHeightGoal) {
        titleFontSize -= 1;

        if (title) {
          title.style.fontSize = titleFontSize + "px";
        }

        titleHeight = titleBox.offsetHeight;
      }
    }
  </script>
</html>

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