From 89ba7246dd07f3ec1812736be43606c08d45daef Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 10 May 2023 15:33:10 +0200 Subject: [PATCH] LibWeb: Don't resolve CSS property values for unconnected elements While it's possible to getComputedStyle() on an unconnected element, the resulting object is not supposed to have any values, since we can't resolve style without a document root anyway. This fixes a crash on https://bandcamp.com --- .../getComputedStyle-on-unconnected-element.txt | 7 +++++++ .../getComputedStyle-on-unconnected-element.html | 11 +++++++++++ .../LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp | 5 +++++ 3 files changed, 23 insertions(+) create mode 100644 Tests/LibWeb/Layout/expected/getComputedStyle-on-unconnected-element.txt create mode 100644 Tests/LibWeb/Layout/input/getComputedStyle-on-unconnected-element.html diff --git a/Tests/LibWeb/Layout/expected/getComputedStyle-on-unconnected-element.txt b/Tests/LibWeb/Layout/expected/getComputedStyle-on-unconnected-element.txt new file mode 100644 index 0000000000..08865062d8 --- /dev/null +++ b/Tests/LibWeb/Layout/expected/getComputedStyle-on-unconnected-element.txt @@ -0,0 +1,7 @@ +Viewport <#document> at (0,0) content-size 800x600 children: not-inline + BlockContainer at (0,0) content-size 800x37.835937 [BFC] children: not-inline + BlockContainer at (8,8) content-size 784x21.835937 children: inline + line 0 width: 362.617187, height: 21.835937, bottom: 21.835937, baseline: 16.914062 + frag 0 from TextNode start: 0, length: 35, rect: [8,8 362.617187x21.835937] + "This test passes if we don't crash." + TextNode <#text> diff --git a/Tests/LibWeb/Layout/input/getComputedStyle-on-unconnected-element.html b/Tests/LibWeb/Layout/input/getComputedStyle-on-unconnected-element.html new file mode 100644 index 0000000000..34c51bfef6 --- /dev/null +++ b/Tests/LibWeb/Layout/input/getComputedStyle-on-unconnected-element.html @@ -0,0 +1,11 @@ + +This test passes if we don't crash. \ No newline at end of file diff --git a/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp b/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp index c1cc9fc703..99a2987d8d 100644 --- a/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp +++ b/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp @@ -666,6 +666,11 @@ ErrorOr> ResolvedCSSStyleDeclaration::style_value_for_p Optional ResolvedCSSStyleDeclaration::property(PropertyID property_id) const { + // https://www.w3.org/TR/cssom-1/#dom-window-getcomputedstyle + // NOTE: This is a partial enforcement of step 5 ("If elt is connected, ...") + if (!m_element->is_connected()) + return {}; + if (CSS::property_affects_layout(property_id)) { const_cast(m_element->document()).update_layout(); } else {