Browse Source

LibWeb/HTML: Implement text attribute in HTMLTitleElement

Kemal Zebari 1 year ago
parent
commit
24b9d05ea8

+ 13 - 9
Tests/LibWeb/Text/expected/title.txt

@@ -3,12 +3,16 @@
 2b: 1
 2c: "This is a title!"
 2d: "This is a title!"
-3: ""
-4a: 3
-4b: ""
-4c: ""
-4d: ""
-4e: 3
-4f: "This is another title!"
-4g: ""
-4h: ""
+3a: "This is a title!"
+3b: "This is a title!"
+3c: "Another title!"
+3d: "Another title!"
+4: ""
+5a: 3
+5b: ""
+5c: ""
+5d: ""
+5e: 3
+5f: "This is another title!"
+5g: ""
+5h: ""

+ 17 - 9
Tests/LibWeb/Text/input/title.html

@@ -15,9 +15,17 @@
         println(`2c: "${titleElements[0].innerText}"`);
         println(`2d: "${document.title}"`);
 
+        // Getting and setting the title's text attribute.
+        println(`3a: "${titleElements[0].text}"`);
+        println(`3b: "${document.title}"`);
+
+        titleElements[0].text = "Another title!";
+        println(`3c: "${titleElements[0].text}"`);
+        println(`3d: "${document.title}"`);
+
         // Removing the title element sets the title back to default.
         titleElements[0].remove();
-        println(`3: "${document.title}"`);
+        println(`4: "${document.title}"`);
 
         // After adding several title elements to the body, setting the title updates the text
         // content of only the first title element.
@@ -26,17 +34,17 @@
         document.body.appendChild(document.createElement('title'));
 
         titleElements = document.getElementsByTagName('title');
-        println(`4a: ${titleElements.length}`)
-        println(`4b: "${titleElements[0].innerText}"`);
-        println(`4c: "${titleElements[1].innerText}"`);
-        println(`4d: "${titleElements[2].innerText}"`);
+        println(`5a: ${titleElements.length}`)
+        println(`5b: "${titleElements[0].innerText}"`);
+        println(`5c: "${titleElements[1].innerText}"`);
+        println(`5d: "${titleElements[2].innerText}"`);
 
         document.title = 'This is another title!';
 
         titleElements = document.getElementsByTagName('title');
-        println(`4e: ${titleElements.length}`)
-        println(`4f: "${titleElements[0].innerText}"`);
-        println(`4g: "${titleElements[1].innerText}"`);
-        println(`4h: "${titleElements[2].innerText}"`);
+        println(`5e: ${titleElements.length}`)
+        println(`5f: "${titleElements[0].innerText}"`);
+        println(`5g: "${titleElements[1].innerText}"`);
+        println(`5h: "${titleElements[2].innerText}"`);
     });
 </script>

+ 14 - 0
Userland/Libraries/LibWeb/HTML/HTMLTitleElement.cpp

@@ -34,4 +34,18 @@ void HTMLTitleElement::children_changed()
     }
 }
 
+// https://html.spec.whatwg.org/multipage/semantics.html#dom-title-text
+DeprecatedString HTMLTitleElement::text()
+{
+    // The text attribute's getter must return this title element's child text content.
+    return child_text_content();
+}
+
+// https://html.spec.whatwg.org/multipage/semantics.html#dom-title-text
+void HTMLTitleElement::set_text(String const& value)
+{
+    // The text attribute's setter must string replace all with the given value within this title element.
+    string_replace_all(value.to_deprecated_string());
+}
+
 }

+ 3 - 0
Userland/Libraries/LibWeb/HTML/HTMLTitleElement.h

@@ -17,6 +17,9 @@ class HTMLTitleElement final : public HTMLElement {
 public:
     virtual ~HTMLTitleElement() override;
 
+    DeprecatedString text();
+    void set_text(String const& value);
+
 private:
     HTMLTitleElement(DOM::Document&, DOM::QualifiedName);
 

+ 1 - 1
Userland/Libraries/LibWeb/HTML/HTMLTitleElement.idl

@@ -6,6 +6,6 @@ interface HTMLTitleElement : HTMLElement {
 
     [HTMLConstructor] constructor();
 
-    // FIXME: [CEReactions] attribute DOMString text;
+    [CEReactions] attribute DOMString text;
 
 };