From d6297ec074b3fea5617df3bad5dc03f9fd12aaf3 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Tue, 28 May 2024 06:42:08 +0100 Subject: [PATCH] LibWeb: Cast to the correct type in `Element::auto_directionality()` Previously, we always cast to a HTMLInputElement when getting the value of an auto directionality form associated element. This caused undefined behavior when determining the directionality of an element that wasn't a HTMLInputElement. --- .../dir-pseudo-on-form-associated-element.txt | 30 +++++++++++ .../css/dir-pseudo-on-input-element.txt | 14 ----- ...dir-pseudo-on-form-associated-element.html | 53 +++++++++++++++++++ .../css/dir-pseudo-on-input-element.html | 44 --------------- Userland/Libraries/LibWeb/DOM/Element.cpp | 4 +- 5 files changed, 86 insertions(+), 59 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/css/dir-pseudo-on-form-associated-element.txt delete mode 100644 Tests/LibWeb/Text/expected/css/dir-pseudo-on-input-element.txt create mode 100644 Tests/LibWeb/Text/input/css/dir-pseudo-on-form-associated-element.html delete mode 100644 Tests/LibWeb/Text/input/css/dir-pseudo-on-input-element.html diff --git a/Tests/LibWeb/Text/expected/css/dir-pseudo-on-form-associated-element.txt b/Tests/LibWeb/Text/expected/css/dir-pseudo-on-form-associated-element.txt new file mode 100644 index 00000000000..94405c7239d --- /dev/null +++ b/Tests/LibWeb/Text/expected/css/dir-pseudo-on-form-associated-element.txt @@ -0,0 +1,30 @@ +Testing input element with type=text +Input matches :dir(ltr): true +Input matches :dir(rtl): false +Input matches :dir(ltr): true +Input matches :dir(rtl): false +Input matches :dir(ltr): false +Input matches :dir(rtl): true +Input matches :dir(ltr): true +Input matches :dir(rtl): false +Input matches :dir(ltr): false +Input matches :dir(rtl): true +Input matches :dir(ltr): true +Input matches :dir(rtl): false +Input matches :dir(ltr): true +Input matches :dir(rtl): false +Testing textarea element +Input matches :dir(ltr): true +Input matches :dir(rtl): false +Input matches :dir(ltr): true +Input matches :dir(rtl): false +Input matches :dir(ltr): false +Input matches :dir(rtl): true +Input matches :dir(ltr): true +Input matches :dir(rtl): false +Input matches :dir(ltr): false +Input matches :dir(rtl): true +Input matches :dir(ltr): true +Input matches :dir(rtl): false +Input matches :dir(ltr): true +Input matches :dir(rtl): false diff --git a/Tests/LibWeb/Text/expected/css/dir-pseudo-on-input-element.txt b/Tests/LibWeb/Text/expected/css/dir-pseudo-on-input-element.txt deleted file mode 100644 index b7b6c5025a5..00000000000 --- a/Tests/LibWeb/Text/expected/css/dir-pseudo-on-input-element.txt +++ /dev/null @@ -1,14 +0,0 @@ -Input matches :dir(ltr): true -Input matches :dir(rtl): false -Input matches :dir(ltr): true -Input matches :dir(rtl): false -Input matches :dir(ltr): false -Input matches :dir(rtl): true -Input matches :dir(ltr): true -Input matches :dir(rtl): false -Input matches :dir(ltr): false -Input matches :dir(rtl): true -Input matches :dir(ltr): true -Input matches :dir(rtl): false -Input matches :dir(ltr): true -Input matches :dir(rtl): false diff --git a/Tests/LibWeb/Text/input/css/dir-pseudo-on-form-associated-element.html b/Tests/LibWeb/Text/input/css/dir-pseudo-on-form-associated-element.html new file mode 100644 index 00000000000..c0b5eb43006 --- /dev/null +++ b/Tests/LibWeb/Text/input/css/dir-pseudo-on-form-associated-element.html @@ -0,0 +1,53 @@ + + + + diff --git a/Tests/LibWeb/Text/input/css/dir-pseudo-on-input-element.html b/Tests/LibWeb/Text/input/css/dir-pseudo-on-input-element.html deleted file mode 100644 index 1c628038a5b..00000000000 --- a/Tests/LibWeb/Text/input/css/dir-pseudo-on-input-element.html +++ /dev/null @@ -1,44 +0,0 @@ - - - - diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index 6174f6d57eb..becd9a47658 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -2373,7 +2373,9 @@ Optional Element::auto_directionality() const // 1. If element is an auto-directionality form-associated element: if (is_auto_directionality_form_associated_element()) { - auto const& value = static_cast(*this).value(); + auto const* form_associated_element = dynamic_cast(this); + VERIFY(form_associated_element); + auto const& value = form_associated_element->value(); // 1. If element's value contains a character of bidirectional character type AL or R, // and there is no character of bidirectional character type L anywhere before it in the element's value, then return 'rtl'.