Skip to content

Truly independent web browser

LibWeb: Read parents' language and direction when publishing arrivals

Andreas Kling7a4d6b44903e
diff --git a/Libraries/LibWeb/CSS/StyleEngineInput.cpp b/Libraries/LibWeb/CSS/StyleEngineInput.cpp
index 317e7569179..066b95cdd4c 100644
--- a/Libraries/LibWeb/CSS/StyleEngineInput.cpp
+++ b/Libraries/LibWeb/CSS/StyleEngineInput.cpp
@@ -8,6 +8,7 @@
#include <AK/QuickSort.h>
#include <AK/SetUnion.h>
#include <AK/TemporaryChange.h>
+#include <LibGC/ConservativeVector.h>
#include <LibGC/RootHashTable.h>
#include <LibWeb/Animations/KeyframeEffect.h>
#include <LibWeb/CSS/CSSAnimation.h>
@@ -65,7 +66,32 @@
namespace Web::CSS {
static void record_element_heading_level(DOM::Element&);
-static void record_element_initial_features(DOM::Element&);
+
+// The directionalities of the elements on the way from the root to the one published last, as far as they were published
+// in this batch. A batch comes mostly in tree order, so most elements find their parent's here and don't walk to the root.
+class PublishedDirectionalities {
+public:
+ DOM::Element::Directionality publish(DOM::Element const& element)
+ {
+ auto const* parent = element.parent_or_shadow_host_element();
+ while (!m_elements_to_parent.is_empty() && m_elements_to_parent.last().element.ptr() != parent)
+ m_elements_to_parent.take_last();
+ auto directionality = element.directionality_unless_from_parent().value_or_lazy_evaluated([&] {
+ return m_elements_to_parent.is_empty() ? element.directionality() : m_elements_to_parent.last().directionality;
+ });
+ m_elements_to_parent.append({ element, directionality });
+ return directionality;
+ }
+
+private:
+ struct Row {
+ GC::Ptr<DOM::Element const> element;
+ DOM::Element::Directionality directionality;
+ };
+ GC::ConservativeVector<Row, 32> m_elements_to_parent;
+};
+
+static void record_element_initial_features(DOM::Element&, PublishedDirectionalities&);
static void record_element_inline_style_properties(DOM::Element&);
static void record_heading_levels_in_subtree(DOM::Element&);
static void republish_assigned_slots_of(ReadonlySpan<GC::Ref<DOM::Node>>);
@@ -326,6 +352,7 @@ void publish_pending_element_features(StyleEngine& style_engine, StyleComputer&
// rule it queued since its last job leaves it not knowing.
if (!nodes.is_empty())
StyleEngineFFI::document_host_learn_selector_requirements(style_engine.host(), &read);
+ PublishedDirectionalities directionalities;
for (auto node : nodes) {
auto element = style_computer.element_for_style_node(node);
// NB: An element that left the tree gave up its mark. Its identity may have gone to an element since, which is
@@ -341,7 +368,13 @@ void publish_pending_element_features(StyleEngine& style_engine, StyleComputer&
continue;
}
element->set_initial_style_features_pending(false);
- record_element_initial_features(*element);
+ record_element_initial_features(*element, directionalities);
+ }
+ // NB: A tag read now may not be final, as an XML parser sets attributes after insertion, so none is left cached. They
+ // stay for the length of this loop, as the batch itself changes no tag and each element reads its parent's.
+ for (auto node : nodes) {
+ if (auto element = style_computer.element_for_style_node(node))
+ element->invalidate_lang_value();
}
}
@@ -1011,7 +1044,7 @@ static u8 associated_pseudo_kind_plus_one(DOM::Element const& element)
// An element arrives with facts already true of it, and the engine has heard none of them. They are
// published as ordinary deltas from absent, so the projection learns them by exactly the same path
// later mutations take rather than by a second one.
-static void publish_element_selector_features(StyleEngine& style_engine, DOM::Element& element)
+static void publish_element_selector_features(StyleEngine& style_engine, DOM::Element& element, PublishedDirectionalities& directionalities)
{
auto node = element.style_node_id();
// Everything the element's selectors test arrives as one row, which the engine writes in one go.
@@ -1065,15 +1098,13 @@ static void publish_element_selector_features(StyleEngine& style_engine, DOM::El
state_bits |= static_cast<u64>(1) << to_underlying(*fact);
});
- // The tag is computed afresh and not left cached. One cached where the element used to be says nothing about where
- // it arrives, as a subtree that moves while detached is never reached by the walk a `lang` change runs, and one
- // read now may not be final, as an XML parser sets attributes after insertion.
+ // The tag is computed afresh. One cached where the element used to be says nothing about where it arrives, as a
+ // subtree that moves while detached is never reached by the walk a `lang` change runs.
element.invalidate_lang_value();
auto const language = element.lang_view();
auto language_atom = language.has_value() ? style_engine.intern_language_atom(*language) : StyleAtomID {};
- auto const directionality = element.directionality() == DOM::Element::Directionality::Rtl ? "rtl"_utf16_fly_string : "ltr"_utf16_fly_string;
+ auto const directionality = directionalities.publish(element) == DOM::Element::Directionality::Rtl ? "rtl"_utf16_fly_string : "ltr"_utf16_fly_string;
auto directionality_atom = style_engine.intern_atom(directionality);
- element.invalidate_lang_value();
GC::Ptr<HTML::HTMLHeadingElement const> heading = as_if<HTML::HTMLHeadingElement>(element);
auto heading_level = static_cast<u8>(min(heading ? heading->heading_level() : 0, 255u));
@@ -1632,13 +1663,13 @@ static StyleAtomID intern_id_or_class_atom(StyleEngine& style_engine, DOM::Eleme
return style_engine.intern_atom(name);
}
-static void record_element_initial_features(DOM::Element& element)
+static void record_element_initial_features(DOM::Element& element, PublishedDirectionalities& directionalities)
{
auto* style_engine = style_engine_for_identified(element);
if (!style_engine)
return;
- publish_element_selector_features(*style_engine, element);
+ publish_element_selector_features(*style_engine, element, directionalities);
if (auto const& id = element.id(); id.has_value())
StyleEngineFFI::style_engine_set_element_id_name(style_engine->host(), element.style_node_id(), style_engine->intern_atom(*id));
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index cac00d4bae9..169ce3da267 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -6128,6 +6128,18 @@ bool Element::has_auto_directionality() const
// https://html.spec.whatwg.org/multipage/dom.html#the-directionality
Element::Directionality Element::directionality() const
+{
+ if (auto directionality = directionality_unless_from_parent(); directionality.has_value())
+ return *directionality;
+
+ // Otherwise:
+ // Return the parent directionality of element.
+ return parent_directionality();
+}
+
+// NB: Every case of the directionality of an element but the last, which reads the parent's. A caller that has the
+// parent's at hand spares the walk up.
+Optional<Element::Directionality> Element::directionality_unless_from_parent() const
{
// The directionality of an element (any element, not just an HTML element) is either 'ltr' or 'rtl'.
// To compute the directionality given an element element, switch on element's dir attribute state:
@@ -6178,9 +6190,7 @@ Element::Directionality Element::directionality() const
return Directionality::Ltr;
}
- // Otherwise:
- // Return the parent directionality of element.
- return parent_directionality();
+ return {};
}
// https://html.spec.whatwg.org/multipage/dom.html#auto-directionality-form-associated-elements
diff --git a/Libraries/LibWeb/DOM/Element.h b/Libraries/LibWeb/DOM/Element.h
index 9971b320bf2..5824432c40b 100644
--- a/Libraries/LibWeb/DOM/Element.h
+++ b/Libraries/LibWeb/DOM/Element.h
@@ -832,6 +832,7 @@ public:
Rtl,
};
Directionality directionality() const;
+ Optional<Directionality> directionality_unless_from_parent() const;
bool is_auto_directionality_form_associated_element() const;
Optional<Utf16FlyString> const& id() const { return m_id; }
diff --git a/Tests/LibWeb/Text/expected/css/style-engine/arrival-batch-language-direction.txt b/Tests/LibWeb/Text/expected/css/style-engine/arrival-batch-language-direction.txt
new file mode 100644
index 00000000000..32a1400cbf0
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/style-engine/arrival-batch-language-direction.txt
@@ -0,0 +1,16 @@
+root: rtl he
+deep: rtl he
+sibling after a deep subtree: rtl he
+own attributes: ltr en
+below own attributes: ltr en
+auto: rtl he
+below auto: rtl he
+telephone input: ltr he
+bdi: ltr he
+in a shadow tree: rtl he
+below that: rtl he
+moved under a later parent: rtl de
+below that: rtl de
+moved out again: ltr none
+below that: ltr none
+deep after the root changed: ltr de
diff --git a/Tests/LibWeb/Text/input/css/style-engine/arrival-batch-language-direction.html b/Tests/LibWeb/Text/input/css/style-engine/arrival-batch-language-direction.html
new file mode 100644
index 00000000000..76abd5b61fb
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/style-engine/arrival-batch-language-direction.html
@@ -0,0 +1,78 @@
+<!doctype html>
+<script src="../../include.js"></script>
+<body>
+<script>
+ // Elements arriving together read the language and direction of their parents, at any depth and after the
+ // publication of a sibling's deep subtree, whichever order they took their identities in.
+ function element(tag, attributes = {}, ...children) {
+ const result = document.createElement(tag);
+ for (const [name, value] of Object.entries(attributes))
+ result.setAttribute(name, value);
+ result.append(...children);
+ return result;
+ }
+
+ function describe(name, node) {
+ const direction = node.matches(":dir(rtl)") ? "rtl" : node.matches(":dir(ltr)") ? "ltr" : "none";
+ const language = ["he", "de", "en"].find(tag => node.matches(`:lang(${tag})`)) ?? "none";
+ println(`${name}: ${direction} ${language}`);
+ }
+
+ asyncTest(async done => {
+ const deep = element("div");
+ let chain = deep;
+ for (let i = 0; i < 20; ++i)
+ chain = element("div", {}, chain);
+ const after_deep = element("span");
+ const own_direction = element("div", { dir: "ltr", lang: "en" }, element("span", { id: "below-own" }));
+ const auto_direction = element("div", { dir: "auto" }, "א", element("span"));
+ const telephone = element("input", { type: "tel" });
+ const bdi = element("bdi", {}, "abc");
+ const root = element("div", { dir: "rtl", lang: "he" }, chain, after_deep, own_direction, auto_direction, telephone, bdi);
+ document.body.append(root);
+ await animationFrame();
+
+ describe("root", root);
+ describe("deep", deep);
+ describe("sibling after a deep subtree", after_deep);
+ describe("own attributes", own_direction);
+ describe("below own attributes", own_direction.firstElementChild);
+ describe("auto", auto_direction);
+ describe("below auto", auto_direction.lastElementChild);
+ describe("telephone input", telephone);
+ describe("bdi", bdi);
+
+ const host = element("div", { dir: "rtl", lang: "he" });
+ const in_shadow = element("span", {}, element("b"));
+ host.attachShadow({ mode: "open" }).append(in_shadow);
+ document.body.append(host);
+ await animationFrame();
+ describe("in a shadow tree", in_shadow);
+ describe("below that", in_shadow.firstElementChild);
+
+ // An element that took its identity first and then moves under a parent that takes one later.
+ const first = element("div");
+ document.body.append(first);
+ const moved = element("div", {}, element("i"));
+ first.append(moved);
+ const later_parent = element("div", { dir: "rtl", lang: "de" });
+ document.body.append(later_parent);
+ later_parent.append(moved);
+ await animationFrame();
+ describe("moved under a later parent", moved);
+ describe("below that", moved.firstElementChild);
+
+ // Elements the update published and later moved read where they went, and later changes reach them.
+ document.body.append(moved);
+ await animationFrame();
+ describe("moved out again", moved);
+ describe("below that", moved.firstElementChild);
+ root.setAttribute("dir", "ltr");
+ root.setAttribute("lang", "de");
+ await animationFrame();
+ describe("deep after the root changed", deep);
+
+ document.body.innerHTML = "";
+ done();
+ });
+</script>