Browse Source

LibWeb/DOM: Implement the Document object's partial attributes

PGHales 1 year ago
parent
commit
75626c6cd2

+ 20 - 0
Tests/LibWeb/Text/expected/DOM/Document-set-partial-attributes.txt

@@ -0,0 +1,20 @@
+before document.fgColor=
+before body attribute text=null
+after document.fgColor=red
+after body attribute text=red
+before document.linkColor=
+before body attribute link=null
+after document.linkColor=red
+after body attribute link=red
+before document.alinkColor=
+before body attribute alink=null
+after document.alinkColor=red
+after body attribute alink=red
+before document.vlinkColor=
+before body attribute vlink=null
+after document.vlinkColor=red
+after body attribute vlink=red
+before document.bgColor=
+before body attribute bgcolor=null
+after document.bgColor=red
+after body attribute bgcolor=red

+ 18 - 0
Tests/LibWeb/Text/input/DOM/Document-set-partial-attributes.html

@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+    test(() => {
+        function testPartialDocumentAttribute(documentAttr, bodyAttr) {
+            println(`before document.${documentAttr}=${document[documentAttr]}`);
+            println(`before body attribute ${bodyAttr}=${document.body.getAttribute(bodyAttr)}`);
+            document[documentAttr] = "red";
+            println(`after document.${documentAttr}=${document[documentAttr]}`);
+            println(`after body attribute ${bodyAttr}=${document.body.getAttribute(bodyAttr)}`);
+        }
+        testPartialDocumentAttribute("fgColor", "text");
+        testPartialDocumentAttribute("linkColor", "link");
+        testPartialDocumentAttribute("alinkColor", "alink");
+        testPartialDocumentAttribute("vlinkColor", "vlink");
+        testPartialDocumentAttribute("bgColor", "bgcolor");
+    });
+</script>

+ 65 - 0
Userland/Libraries/LibWeb/DOM/Document.cpp

@@ -2285,6 +2285,71 @@ void Document::set_cookie(StringView cookie_string, Cookie::Source source)
     page().client().page_did_set_cookie(m_url, cookie.value(), source);
 }
 
+String Document::fg_color() const
+{
+    if (auto* body_element = body(); body_element && !is<HTML::HTMLFrameSetElement>(*body_element))
+        return body_element->get_attribute_value(HTML::AttributeNames::text);
+    return ""_string;
+}
+
+void Document::set_fg_color(String const& value)
+{
+    if (auto* body_element = body(); body_element && !is<HTML::HTMLFrameSetElement>(*body_element))
+        MUST(body_element->set_attribute(HTML::AttributeNames::text, value));
+}
+
+String Document::link_color() const
+{
+    if (auto* body_element = body(); body_element && !is<HTML::HTMLFrameSetElement>(*body_element))
+        return body_element->get_attribute_value(HTML::AttributeNames::link);
+    return ""_string;
+}
+
+void Document::set_link_color(String const& value)
+{
+    if (auto* body_element = body(); body_element && !is<HTML::HTMLFrameSetElement>(*body_element))
+        MUST(body_element->set_attribute(HTML::AttributeNames::link, value));
+}
+
+String Document::vlink_color() const
+{
+    if (auto* body_element = body(); body_element && !is<HTML::HTMLFrameSetElement>(*body_element))
+        return body_element->get_attribute_value(HTML::AttributeNames::vlink);
+    return ""_string;
+}
+
+void Document::set_vlink_color(String const& value)
+{
+    if (auto* body_element = body(); body_element && !is<HTML::HTMLFrameSetElement>(*body_element))
+        MUST(body_element->set_attribute(HTML::AttributeNames::vlink, value));
+}
+
+String Document::alink_color() const
+{
+    if (auto* body_element = body(); body_element && !is<HTML::HTMLFrameSetElement>(*body_element))
+        return body_element->get_attribute_value(HTML::AttributeNames::alink);
+    return ""_string;
+}
+
+void Document::set_alink_color(String const& value)
+{
+    if (auto* body_element = body(); body_element && !is<HTML::HTMLFrameSetElement>(*body_element))
+        MUST(body_element->set_attribute(HTML::AttributeNames::alink, value));
+}
+
+String Document::bg_color() const
+{
+    if (auto* body_element = body(); body_element && !is<HTML::HTMLFrameSetElement>(*body_element))
+        return body_element->get_attribute_value(HTML::AttributeNames::bgcolor);
+    return ""_string;
+}
+
+void Document::set_bg_color(String const& value)
+{
+    if (auto* body_element = body(); body_element && !is<HTML::HTMLFrameSetElement>(*body_element))
+        MUST(body_element->set_attribute(HTML::AttributeNames::bgcolor, value));
+}
+
 String Document::dump_dom_tree_as_json() const
 {
     StringBuilder builder;

+ 15 - 0
Userland/Libraries/LibWeb/DOM/Document.h

@@ -110,6 +110,21 @@ public:
     String cookie(Cookie::Source = Cookie::Source::NonHttp);
     void set_cookie(StringView, Cookie::Source = Cookie::Source::NonHttp);
 
+    String fg_color() const;
+    void set_fg_color(String const&);
+
+    String link_color() const;
+    void set_link_color(String const&);
+
+    String vlink_color() const;
+    void set_vlink_color(String const&);
+
+    String alink_color() const;
+    void set_alink_color(String const&);
+
+    String bg_color() const;
+    void set_bg_color(String const&);
+
     String referrer() const;
     void set_referrer(String);
 

+ 7 - 0
Userland/Libraries/LibWeb/DOM/Document.idl

@@ -57,6 +57,13 @@ interface Document : Node {
 
     attribute DOMString cookie;
 
+    // https://html.spec.whatwg.org/#Document-partial
+    [CEReactions, LegacyNullToEmptyString] attribute DOMString fgColor;
+    [CEReactions, LegacyNullToEmptyString] attribute DOMString linkColor;
+    [CEReactions, LegacyNullToEmptyString] attribute DOMString vlinkColor;
+    [CEReactions, LegacyNullToEmptyString] attribute DOMString alinkColor;
+    [CEReactions, LegacyNullToEmptyString] attribute DOMString bgColor;
+
     readonly attribute USVString referrer;
 
     readonly attribute Element? activeElement;