瀏覽代碼

LibWeb: Align StyleSheet title getter with the specification

The CSSOM specification says that StyleSheet.title should return null
if the title field is empty.
Tim Ledbetter 1 年之前
父節點
當前提交
84193f2746

+ 1 - 1
Tests/LibWeb/Text/expected/css/CSSStyleSheet-constructor.txt

@@ -1,6 +1,6 @@
 Empty sheet ownerNode: null
 Empty sheet ownerNode: null
 Empty sheet ownerRule: null
 Empty sheet ownerRule: null
-Empty sheet title is empty string: true
+Empty sheet title: null
 Empty sheet cssRules is empty: true
 Empty sheet cssRules is empty: true
 Empty sheet is disabled by default: false
 Empty sheet is disabled by default: false
 cssRules length after insertRule(): 1
 cssRules length after insertRule(): 1

+ 1 - 1
Tests/LibWeb/Text/input/css/CSSStyleSheet-constructor.html

@@ -6,7 +6,7 @@
         const sheet = new CSSStyleSheet();
         const sheet = new CSSStyleSheet();
         println(`Empty sheet ownerNode: ${sheet.ownerNode}`);
         println(`Empty sheet ownerNode: ${sheet.ownerNode}`);
         println(`Empty sheet ownerRule: ${sheet.ownerRule}`);
         println(`Empty sheet ownerRule: ${sheet.ownerRule}`);
-        println(`Empty sheet title is empty string: ${sheet.title === ""}`);
+        println(`Empty sheet title: ${sheet.title}`);
         println(`Empty sheet cssRules is empty: ${sheet.cssRules.length === 0}`);
         println(`Empty sheet cssRules is empty: ${sheet.cssRules.length === 0}`);
         println(`Empty sheet is disabled by default: ${sheet.disabled}`);
         println(`Empty sheet is disabled by default: ${sheet.disabled}`);
 
 

+ 10 - 0
Userland/Libraries/LibWeb/CSS/StyleSheet.cpp

@@ -36,4 +36,14 @@ void StyleSheet::set_parent_css_style_sheet(CSSStyleSheet* parent)
     m_parent_style_sheet = parent;
     m_parent_style_sheet = parent;
 }
 }
 
 
+// https://drafts.csswg.org/cssom/#dom-stylesheet-title
+Optional<String> StyleSheet::title_for_bindings() const
+{
+    // The title attribute must return the title or null if title is the empty string.
+    if (m_title.is_empty())
+        return {};
+
+    return m_title;
+}
+
 }
 }

+ 4 - 3
Userland/Libraries/LibWeb/CSS/StyleSheet.h

@@ -29,8 +29,9 @@ public:
     Optional<String> location() const { return m_location; }
     Optional<String> location() const { return m_location; }
     void set_location(Optional<String> location) { m_location = move(location); }
     void set_location(Optional<String> location) { m_location = move(location); }
 
 
-    Optional<String> title() const { return m_title; }
-    void set_title(Optional<String> title) { m_title = move(title); }
+    String title() const { return m_title; }
+    Optional<String> title_for_bindings() const;
+    void set_title(String title) { m_title = move(title); }
 
 
     void set_type(String type) { m_type_string = move(type); }
     void set_type(String type) { m_type_string = move(type); }
 
 
@@ -66,7 +67,7 @@ private:
     JS::GCPtr<CSSStyleSheet> m_parent_style_sheet;
     JS::GCPtr<CSSStyleSheet> m_parent_style_sheet;
 
 
     Optional<String> m_location;
     Optional<String> m_location;
-    Optional<String> m_title;
+    String m_title;
     String m_type_string;
     String m_type_string;
 
 
     bool m_disabled { false };
     bool m_disabled { false };

+ 1 - 1
Userland/Libraries/LibWeb/CSS/StyleSheet.idl

@@ -13,7 +13,7 @@ interface StyleSheet {
     readonly attribute Element? ownerNode;
     readonly attribute Element? ownerNode;
 
 
     readonly attribute CSSStyleSheet? parentStyleSheet;
     readonly attribute CSSStyleSheet? parentStyleSheet;
-    readonly attribute DOMString? title;
+    [ImplementedAs=title_for_bindings] readonly attribute DOMString? title;
     [SameObject, PutForwards=mediaText] readonly attribute MediaList media;
     [SameObject, PutForwards=mediaText] readonly attribute MediaList media;
     attribute boolean disabled;
     attribute boolean disabled;
 
 

+ 5 - 7
Userland/Libraries/LibWeb/CSS/StyleSheetList.cpp

@@ -36,11 +36,9 @@ void StyleSheetList::add_a_css_style_sheet(CSS::CSSStyleSheet& sheet)
     if (sheet.disabled())
     if (sheet.disabled())
         return;
         return;
 
 
-    VERIFY(sheet.title().has_value());
-
     // 3. If the title is not the empty string, the alternate flag is unset, and preferred CSS style sheet set name is the empty string change the preferred CSS style sheet set name to the title.
     // 3. If the title is not the empty string, the alternate flag is unset, and preferred CSS style sheet set name is the empty string change the preferred CSS style sheet set name to the title.
-    if (!sheet.title()->is_empty() && !sheet.is_alternate() && m_preferred_css_style_sheet_set_name.is_empty()) {
-        m_preferred_css_style_sheet_set_name = sheet.title().value();
+    if (!sheet.title().is_empty() && !sheet.is_alternate() && m_preferred_css_style_sheet_set_name.is_empty()) {
+        m_preferred_css_style_sheet_set_name = sheet.title();
     }
     }
 
 
     // 4. If any of the following is true, then unset the disabled flag and return:
     // 4. If any of the following is true, then unset the disabled flag and return:
@@ -50,9 +48,9 @@ void StyleSheetList::add_a_css_style_sheet(CSS::CSSStyleSheet& sheet)
     // NOTE: We don't enable alternate sheets with an empty title.  This isn't directly mentioned in the algorithm steps, but the
     // NOTE: We don't enable alternate sheets with an empty title.  This isn't directly mentioned in the algorithm steps, but the
     // HTML specification says that the title element must be specified with a non-empty value for alternative style sheets.
     // HTML specification says that the title element must be specified with a non-empty value for alternative style sheets.
     // See: https://html.spec.whatwg.org/multipage/links.html#the-link-is-an-alternative-stylesheet
     // See: https://html.spec.whatwg.org/multipage/links.html#the-link-is-an-alternative-stylesheet
-    if ((sheet.title()->is_empty() && !sheet.is_alternate())
-        || (!m_last_css_style_sheet_set_name.has_value() && sheet.title().value().equals_ignoring_case(m_preferred_css_style_sheet_set_name))
-        || (m_last_css_style_sheet_set_name.has_value() && sheet.title().value().equals_ignoring_case(m_last_css_style_sheet_set_name.value()))) {
+    if ((sheet.title().is_empty() && !sheet.is_alternate())
+        || (!m_last_css_style_sheet_set_name.has_value() && sheet.title().equals_ignoring_case(m_preferred_css_style_sheet_set_name))
+        || (m_last_css_style_sheet_set_name.has_value() && sheet.title().equals_ignoring_case(m_last_css_style_sheet_set_name.value()))) {
         sheet.set_disabled(false);
         sheet.set_disabled(false);
         return;
         return;
     }
     }