From 2e0b78ffc1a00c825b772d02e3871684dc38572e Mon Sep 17 00:00:00 2001 From: Adam Vandolder Date: Thu, 19 Jan 2023 16:00:55 +0000 Subject: [PATCH] Bug 1418449 - Add translate attribute to HTMLElement. r=smaug Differential Revision: https://phabricator.services.mozilla.com/D167081 --- dom/base/Element.cpp | 7 +++ dom/base/Element.h | 2 + dom/html/nsGenericHTMLElement.cpp | 15 +++++- dom/html/nsGenericHTMLElement.h | 5 ++ dom/webidl/HTMLElement.webidl | 3 +- .../reactions/HTMLElement.html.ini | 8 ---- .../the-translate-attribute-007.html.ini | 5 -- .../the-translate-attribute-008.html.ini | 5 -- .../the-translate-attribute-009.html.ini | 5 -- .../the-translate-attribute-010.html.ini | 5 -- .../the-translate-attribute-011.html.ini | 5 -- .../the-translate-attribute-012.html.ini | 5 -- ...enumerated-ascii-case-insensitive.html.ini | 5 -- ...anslate-inherit-no-parent-element.html.ini | 17 ------- .../meta/html/dom/idlharness.https.html.ini | 6 --- .../translate-non-html-translation-mode.html | 46 +++++++++++++++++++ 16 files changed, 76 insertions(+), 68 deletions(-) delete mode 100644 testing/web-platform/meta/custom-elements/reactions/HTMLElement.html.ini delete mode 100644 testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-007.html.ini delete mode 100644 testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-008.html.ini delete mode 100644 testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-009.html.ini delete mode 100644 testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-010.html.ini delete mode 100644 testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-011.html.ini delete mode 100644 testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-012.html.ini delete mode 100644 testing/web-platform/meta/html/dom/elements/global-attributes/translate-enumerated-ascii-case-insensitive.html.ini delete mode 100644 testing/web-platform/meta/html/dom/elements/global-attributes/translate-inherit-no-parent-element.html.ini create mode 100644 testing/web-platform/tests/html/dom/elements/global-attributes/translate-non-html-translation-mode.html diff --git a/dom/base/Element.cpp b/dom/base/Element.cpp index 4622a8180b76..83a2d4621c15 100644 --- a/dom/base/Element.cpp +++ b/dom/base/Element.cpp @@ -4842,4 +4842,11 @@ void Element::SetHTML(const nsAString& aInnerHTML, } } +bool Element::Translate() const { + if (const auto* parent = Element::FromNodeOrNull(mParent)) { + return parent->Translate(); + } + return true; +} + } // namespace mozilla::dom diff --git a/dom/base/Element.h b/dom/base/Element.h index f146aa674f93..873d5d1b36f1 100644 --- a/dom/base/Element.h +++ b/dom/base/Element.h @@ -2042,6 +2042,8 @@ class Element : public FragmentOrElement { */ virtual void GetLinkTarget(nsAString& aTarget); + virtual bool Translate() const; + protected: enum class ReparseAttributes { No, Yes }; /** diff --git a/dom/html/nsGenericHTMLElement.cpp b/dom/html/nsGenericHTMLElement.cpp index bc2a79685e68..c5896506c9c1 100644 --- a/dom/html/nsGenericHTMLElement.cpp +++ b/dom/html/nsGenericHTMLElement.cpp @@ -919,7 +919,8 @@ bool nsGenericHTMLElement::ParseAttribute(int32_t aNamespaceID, return true; } - if (aAttribute == nsGkAtoms::contenteditable) { + if (aAttribute == nsGkAtoms::contenteditable || + aAttribute == nsGkAtoms::translate) { aResult.ParseAtom(aValue); return true; } @@ -3077,3 +3078,15 @@ void nsGenericHTMLElement::GetAutocapitalize(nsAString& aValue) const { GetEnumAttr(nsGkAtoms::autocapitalize, nullptr, kDefaultAutocapitalize->tag, aValue); } + +bool nsGenericHTMLElement::Translate() const { + if (const nsAttrValue* attr = mAttrs.GetAttr(nsGkAtoms::translate)) { + if (attr->IsEmptyString() || attr->Equals(nsGkAtoms::yes, eIgnoreCase)) { + return true; + } + if (attr->Equals(nsGkAtoms::no, eIgnoreCase)) { + return false; + } + } + return nsGenericHTMLElementBase::Translate(); +} diff --git a/dom/html/nsGenericHTMLElement.h b/dom/html/nsGenericHTMLElement.h index 871cc0a0c1dd..80c806aaa0e9 100644 --- a/dom/html/nsGenericHTMLElement.h +++ b/dom/html/nsGenericHTMLElement.h @@ -75,6 +75,11 @@ class nsGenericHTMLElement : public nsGenericHTMLElementBase { GetHTMLAttr(nsGkAtoms::lang, aLang); } void SetLang(const nsAString& aLang) { SetHTMLAttr(nsGkAtoms::lang, aLang); } + bool Translate() const override; + void SetTranslate(bool aTranslate, mozilla::ErrorResult& aError) { + SetHTMLAttr(nsGkAtoms::translate, aTranslate ? u"yes"_ns : u"no"_ns, + aError); + } void GetDir(nsAString& aDir) { GetHTMLEnumAttr(nsGkAtoms::dir, aDir); } void SetDir(const nsAString& aDir, mozilla::ErrorResult& aError) { SetHTMLAttr(nsGkAtoms::dir, aDir, aError); diff --git a/dom/webidl/HTMLElement.webidl b/dom/webidl/HTMLElement.webidl index 8af5030e3996..14b9d0afb658 100644 --- a/dom/webidl/HTMLElement.webidl +++ b/dom/webidl/HTMLElement.webidl @@ -21,7 +21,8 @@ interface HTMLElement : Element { attribute DOMString title; [CEReactions] attribute DOMString lang; - // attribute boolean translate; + [CEReactions, SetterThrows, Pure] + attribute boolean translate; [CEReactions, SetterThrows, Pure] attribute DOMString dir; diff --git a/testing/web-platform/meta/custom-elements/reactions/HTMLElement.html.ini b/testing/web-platform/meta/custom-elements/reactions/HTMLElement.html.ini deleted file mode 100644 index 78111541267f..000000000000 --- a/testing/web-platform/meta/custom-elements/reactions/HTMLElement.html.ini +++ /dev/null @@ -1,8 +0,0 @@ -[HTMLElement.html] - expected: - if (os == "android") and fission: [OK, TIMEOUT] - [translate on HTMLElement must enqueue an attributeChanged reaction when adding translate content attribute] - expected: FAIL - - [translate on HTMLElement must enqueue an attributeChanged reaction when replacing an existing attribute] - expected: FAIL diff --git a/testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-007.html.ini b/testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-007.html.ini deleted file mode 100644 index 06901a9d9eab..000000000000 --- a/testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-007.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[the-translate-attribute-007.html] - expected: - if (os == "android") and fission: [OK, TIMEOUT] - [In the default case, ie. with no translate attribute in the page, javascript will detect the translation mode of text as translate-enabled.] - expected: FAIL diff --git a/testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-008.html.ini b/testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-008.html.ini deleted file mode 100644 index b6bc2ce06fe4..000000000000 --- a/testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-008.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[the-translate-attribute-008.html] - expected: - if (os == "android") and fission: [OK, TIMEOUT] - [If the translate attribute is set to yes, javascript will detect the translation mode of text as translate-enabled.] - expected: FAIL diff --git a/testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-009.html.ini b/testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-009.html.ini deleted file mode 100644 index bbe67e8be2fb..000000000000 --- a/testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-009.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[the-translate-attribute-009.html] - expected: - if (os == "android") and fission: [OK, TIMEOUT] - [If the translate attribute is set to no, javascript will detect the translation mode of text as no-translate.] - expected: FAIL diff --git a/testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-010.html.ini b/testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-010.html.ini deleted file mode 100644 index 15a377e0d7bc..000000000000 --- a/testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-010.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[the-translate-attribute-010.html] - expected: - if (os == "android") and fission: [OK, TIMEOUT] - [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.] - expected: FAIL diff --git a/testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-011.html.ini b/testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-011.html.ini deleted file mode 100644 index 2fa9ad18db5b..000000000000 --- a/testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-011.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[the-translate-attribute-011.html] - expected: - if (os == "android") and fission: [OK, TIMEOUT] - [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.] - expected: FAIL diff --git a/testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-012.html.ini b/testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-012.html.ini deleted file mode 100644 index 523070049476..000000000000 --- a/testing/web-platform/meta/html/dom/elements/global-attributes/the-translate-attribute-012.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[the-translate-attribute-012.html] - expected: - if (os == "android") and fission: [TIMEOUT, OK] - [If the translate attribute is set to a null string, javascript will detect the translation mode of text as translate-enabled.] - expected: FAIL diff --git a/testing/web-platform/meta/html/dom/elements/global-attributes/translate-enumerated-ascii-case-insensitive.html.ini b/testing/web-platform/meta/html/dom/elements/global-attributes/translate-enumerated-ascii-case-insensitive.html.ini deleted file mode 100644 index bda62a49bb40..000000000000 --- a/testing/web-platform/meta/html/dom/elements/global-attributes/translate-enumerated-ascii-case-insensitive.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[translate-enumerated-ascii-case-insensitive.html] - expected: - if (os == "android") and fission: [OK, TIMEOUT] - [keyword yes] - expected: FAIL diff --git a/testing/web-platform/meta/html/dom/elements/global-attributes/translate-inherit-no-parent-element.html.ini b/testing/web-platform/meta/html/dom/elements/global-attributes/translate-inherit-no-parent-element.html.ini deleted file mode 100644 index 814723ed7535..000000000000 --- a/testing/web-platform/meta/html/dom/elements/global-attributes/translate-inherit-no-parent-element.html.ini +++ /dev/null @@ -1,17 +0,0 @@ -[translate-inherit-no-parent-element.html] - expected: - if (os == "android") and fission: [OK, TIMEOUT] - [No parent node] - expected: FAIL - - [DocumentFragment parent node] - expected: FAIL - - [ShadowRoot parent node whose shadow host has translate=yes] - expected: FAIL - - [ShadowRoot parent node whose shadow host has translate=no] - expected: FAIL - - [Document parent node] - expected: FAIL diff --git a/testing/web-platform/meta/html/dom/idlharness.https.html.ini b/testing/web-platform/meta/html/dom/idlharness.https.html.ini index 417f4dbf75e4..3aaf737454b7 100644 --- a/testing/web-platform/meta/html/dom/idlharness.https.html.ini +++ b/testing/web-platform/meta/html/dom/idlharness.https.html.ini @@ -562,9 +562,6 @@ prefs: [dom.security.featurePolicy.experimental.enabled:true, dom.security.featu [HTMLInputElement interface: createInput("time") must inherit property "dirName" with the proper type] expected: FAIL - [HTMLElement interface: document.createElement("noscript") must inherit property "translate" with the proper type] - expected: FAIL - [HTMLInputElement interface: createInput("number") must inherit property "dirName" with the proper type] expected: FAIL @@ -628,9 +625,6 @@ prefs: [dom.security.featurePolicy.experimental.enabled:true, dom.security.featu [HTMLMediaElement interface: attribute videoTracks] expected: FAIL - [HTMLElement interface: attribute translate] - expected: FAIL - [HTMLInputElement interface: createInput("image") must inherit property "dirName" with the proper type] expected: FAIL diff --git a/testing/web-platform/tests/html/dom/elements/global-attributes/translate-non-html-translation-mode.html b/testing/web-platform/tests/html/dom/elements/global-attributes/translate-non-html-translation-mode.html new file mode 100644 index 000000000000..3bd7f6ace9cb --- /dev/null +++ b/testing/web-platform/tests/html/dom/elements/global-attributes/translate-non-html-translation-mode.html @@ -0,0 +1,46 @@ + + +Non-HTML elements have a translation mode + + +