diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp index 4ee14b534b3..e3352434fee 100644 --- a/Libraries/LibWeb/DOM/Element.cpp +++ b/Libraries/LibWeb/DOM/Element.cpp @@ -3327,6 +3327,36 @@ IntersectionObserver::IntersectionObserverRegistration& Element::get_intersectio return *registration_iterator; } +// https://html.spec.whatwg.org/multipage/dom.html#translation-mode +Element::TranslationMode Element::translation_mode() const +{ + // Each element (even non-HTML elements) has a translation mode, which is in either the translate-enabled state or + // the no-translate state. + + // If an HTML element's translate attribute is in the Yes state, then the element's translation mode is in the + // translate-enabled state; + // NOTE: The attribute is in the Yes state if the attribute is present and its value is the empty string or is a + // ASCII-case-insensitive match for "yes". + auto maybe_translate_attribute = attribute(HTML::AttributeNames::translate); + if (maybe_translate_attribute.has_value() && (maybe_translate_attribute.value().is_empty() || maybe_translate_attribute.value().equals_ignoring_ascii_case("yes"sv))) + return TranslationMode::TranslateEnabled; + + // otherwise, if the element's translate attribute is in the No state, then the element's translation mode is in + // the no-translate state. + if (maybe_translate_attribute.has_value() && maybe_translate_attribute.value().equals_ignoring_ascii_case("no"sv)) { + return TranslationMode::NoTranslate; + } + + // Otherwise, either the element's translate attribute is in the Inherit state, or the element is not an HTML + // element and thus does not have a translate attribute; in either case, the element's translation mode is in the + // same state as its parent element's, if any. + if (auto parent = parent_element()) + return parent->translation_mode(); + + // or in the translate-enabled state, if the element's parent element is null + return TranslationMode::TranslateEnabled; +} + // https://html.spec.whatwg.org/multipage/dom.html#the-directionality Element::Directionality Element::directionality() const { diff --git a/Libraries/LibWeb/DOM/Element.h b/Libraries/LibWeb/DOM/Element.h index 53e31f0b5e7..104919874d9 100644 --- a/Libraries/LibWeb/DOM/Element.h +++ b/Libraries/LibWeb/DOM/Element.h @@ -378,6 +378,12 @@ public: CSSPixelPoint scroll_offset(ScrollOffsetFor type) const { return m_scroll_offset[to_underlying(type)]; } void set_scroll_offset(ScrollOffsetFor type, CSSPixelPoint offset) { m_scroll_offset[to_underlying(type)] = offset; } + enum class TranslationMode { + TranslateEnabled, + NoTranslate + }; + TranslationMode translation_mode() const; + enum class Dir { Ltr, Rtl, diff --git a/Libraries/LibWeb/HTML/AttributeNames.h b/Libraries/LibWeb/HTML/AttributeNames.h index f4f18060541..c6606be6ae2 100644 --- a/Libraries/LibWeb/HTML/AttributeNames.h +++ b/Libraries/LibWeb/HTML/AttributeNames.h @@ -299,6 +299,7 @@ namespace AttributeNames { __ENUMERATE_HTML_ATTRIBUTE(text, "text") \ __ENUMERATE_HTML_ATTRIBUTE(title, "title") \ __ENUMERATE_HTML_ATTRIBUTE(topmargin, "topmargin") \ + __ENUMERATE_HTML_ATTRIBUTE(translate, "translate") \ __ENUMERATE_HTML_ATTRIBUTE(truespeed, "truespeed") \ __ENUMERATE_HTML_ATTRIBUTE(type, "type") \ __ENUMERATE_HTML_ATTRIBUTE(usemap, "usemap") \ diff --git a/Libraries/LibWeb/HTML/HTMLElement.cpp b/Libraries/LibWeb/HTML/HTMLElement.cpp index 40ebe3058ad..a73bd4d5aa3 100644 --- a/Libraries/LibWeb/HTML/HTMLElement.cpp +++ b/Libraries/LibWeb/HTML/HTMLElement.cpp @@ -104,6 +104,25 @@ bool HTMLElement::is_potentially_render_blocking() return is_implicitly_potentially_render_blocking(); } +// https://html.spec.whatwg.org/multipage/dom.html#dom-translate +bool HTMLElement::translate() const +{ + // The translate IDL attribute must, on getting, return true if the element's translation mode is + // translate-enabled, and false otherwise + return translation_mode() == TranslationMode::TranslateEnabled; +} + +// https://html.spec.whatwg.org/multipage/dom.html#dom-translate +void HTMLElement::set_translate(bool new_value) +{ + // On setting, it must set the content attribute's value to "yes" if the new value is true, and set the content + // attribute's value to "no" otherwise. + if (new_value) + MUST(set_attribute(HTML::AttributeNames::translate, "yes"_string)); + else + MUST(set_attribute(HTML::AttributeNames::translate, "no"_string)); +} + // https://html.spec.whatwg.org/multipage/dom.html#dom-dir StringView HTMLElement::dir() const { diff --git a/Libraries/LibWeb/HTML/HTMLElement.h b/Libraries/LibWeb/HTML/HTMLElement.h index 15a00c558fe..609ba0f3300 100644 --- a/Libraries/LibWeb/HTML/HTMLElement.h +++ b/Libraries/LibWeb/HTML/HTMLElement.h @@ -82,6 +82,9 @@ public: Optional title() const { return attribute(HTML::AttributeNames::title); } + bool translate() const; + void set_translate(bool); + StringView dir() const; void set_dir(String const&); diff --git a/Libraries/LibWeb/HTML/HTMLElement.idl b/Libraries/LibWeb/HTML/HTMLElement.idl index 7b4892f09ce..9df48602a0a 100644 --- a/Libraries/LibWeb/HTML/HTMLElement.idl +++ b/Libraries/LibWeb/HTML/HTMLElement.idl @@ -14,7 +14,7 @@ interface HTMLElement : Element { // metadata attributes [Reflect, CEReactions] attribute DOMString title; [Reflect, CEReactions] attribute DOMString lang; - [FIXME, CEReactions] attribute boolean translate; + [CEReactions] attribute boolean translate; [CEReactions] attribute DOMString dir; // user interaction diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-007.txt b/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-007.txt new file mode 100644 index 00000000000..079b02da945 --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-007.txt @@ -0,0 +1,6 @@ +Harness status: OK + +Found 1 tests + +1 Pass +Pass In the default case, ie. with no translate attribute in the page, javascript will detect the translation mode of text as translate-enabled. \ No newline at end of file diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-008.txt b/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-008.txt new file mode 100644 index 00000000000..985c0721d90 --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-008.txt @@ -0,0 +1,6 @@ +Harness status: OK + +Found 1 tests + +1 Pass +Pass If the translate attribute is set to yes, javascript will detect the translation mode of text as translate-enabled. \ No newline at end of file diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-009.txt b/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-009.txt new file mode 100644 index 00000000000..2b2a98a11c5 --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-009.txt @@ -0,0 +1,6 @@ +Harness status: OK + +Found 1 tests + +1 Pass +Pass If the translate attribute is set to no, javascript will detect the translation mode of text as no-translate. \ No newline at end of file diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-010.txt b/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-010.txt new file mode 100644 index 00000000000..a3222599eff --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-010.txt @@ -0,0 +1,6 @@ +Harness status: OK + +Found 1 tests + +1 Pass +Pass If the translate attribute is set to no, javascript will detect the translation mode of elements inside that element with no translate flag as no-translate. \ No newline at end of file diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-011.txt b/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-011.txt new file mode 100644 index 00000000000..87dd834c9b3 --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-011.txt @@ -0,0 +1,6 @@ +Harness status: OK + +Found 1 tests + +1 Pass +Pass If the translate attribute is set to yes on an element inside an element with the translate attribute set to no, javascript will detect the translation mode of text in the inner element as translate-enabled. \ No newline at end of file diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-012.txt b/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-012.txt new file mode 100644 index 00000000000..f5899cac3ef --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-012.txt @@ -0,0 +1,6 @@ +Harness status: OK + +Found 1 tests + +1 Pass +Pass If the translate attribute is set to a null string, javascript will detect the translation mode of text as translate-enabled. \ No newline at end of file diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/translate-enumerated-ascii-case-insensitive.txt b/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/translate-enumerated-ascii-case-insensitive.txt new file mode 100644 index 00000000000..b4a47305ef5 --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/translate-enumerated-ascii-case-insensitive.txt @@ -0,0 +1,6 @@ +Harness status: OK + +Found 1 tests + +1 Pass +Pass keyword yes \ No newline at end of file diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/translate-inherit-no-parent-element.txt b/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/translate-inherit-no-parent-element.txt new file mode 100644 index 00000000000..cadd32cb0b6 --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/translate-inherit-no-parent-element.txt @@ -0,0 +1,10 @@ +Harness status: OK + +Found 5 tests + +5 Pass +Pass No parent node +Pass DocumentFragment parent node +Pass ShadowRoot parent node whose shadow host has translate=yes +Pass ShadowRoot parent node whose shadow host has translate=no +Pass Document parent node \ No newline at end of file diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/translate-non-html-translation-mode.txt b/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/translate-non-html-translation-mode.txt new file mode 100644 index 00000000000..99a38807dc0 --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/html/dom/elements/global-attributes/translate-non-html-translation-mode.txt @@ -0,0 +1,8 @@ +Harness status: OK + +Found 3 tests + +3 Pass +Pass Non-HTML elements default to translate-enabled +Pass Non-HTML elements inherit their parent's translation-enabled state +Pass Non-HTML elements inherit their parent's no-translation state \ No newline at end of file diff --git a/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-007.html b/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-007.html new file mode 100644 index 00000000000..9e2f63778a0 --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-007.html @@ -0,0 +1,29 @@ + + + + +no translate attribute + + + + + + + + + + +
 
+ + + + +
+ + + diff --git a/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-008.html b/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-008.html new file mode 100644 index 00000000000..3f75fe15afe --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-008.html @@ -0,0 +1,29 @@ + + + + +translate=yes + + + + + + + + + + +
 
+ + + + +
+ + + diff --git a/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-009.html b/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-009.html new file mode 100644 index 00000000000..a4b2b415485 --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-009.html @@ -0,0 +1,29 @@ + + + + +translate=no + + + + + + + + + + +
 
+ + + + +
+ + + diff --git a/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-010.html b/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-010.html new file mode 100644 index 00000000000..ea7f8738d41 --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-010.html @@ -0,0 +1,29 @@ + + + + +translate inherits no + + + + + + + + + + +
   
+ + + + +
+ + + diff --git a/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-011.html b/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-011.html new file mode 100644 index 00000000000..6a34be94a32 --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-011.html @@ -0,0 +1,29 @@ + + + + +translate=yes inside translate=no + + + + + + + + + + +
   
+ + + + +
+ + + diff --git a/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-012.html b/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-012.html new file mode 100644 index 00000000000..6186bff89d7 --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/the-translate-attribute-012.html @@ -0,0 +1,29 @@ + + + + +translate="" + + + + + + + + + + +
 
+ + + + +
+ + + diff --git a/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/translate-enumerated-ascii-case-insensitive.html b/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/translate-enumerated-ascii-case-insensitive.html new file mode 100644 index 00000000000..401f9506755 --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/translate-enumerated-ascii-case-insensitive.html @@ -0,0 +1,26 @@ + + + + + + + + +
+
+
+ diff --git a/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/translate-inherit-no-parent-element.html b/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/translate-inherit-no-parent-element.html new file mode 100644 index 00000000000..504ca4dd6c0 --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/translate-inherit-no-parent-element.html @@ -0,0 +1,33 @@ + + +The translate attribute inherit state when there's no parent element + + + diff --git a/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/translate-non-html-translation-mode.html b/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/translate-non-html-translation-mode.html new file mode 100644 index 00000000000..e194e6aec24 --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/html/dom/elements/global-attributes/translate-non-html-translation-mode.html @@ -0,0 +1,46 @@ + + +Non-HTML elements have a translation mode + + +