diff --git a/Tests/LibWeb/Text/expected/css/CSSStyleSheet-constructor.txt b/Tests/LibWeb/Text/expected/css/CSSStyleSheet-constructor.txt index a12a6c0d90e..4dda0264098 100644 --- a/Tests/LibWeb/Text/expected/css/CSSStyleSheet-constructor.txt +++ b/Tests/LibWeb/Text/expected/css/CSSStyleSheet-constructor.txt @@ -1,6 +1,6 @@ Empty sheet ownerNode: null Empty sheet ownerRule: null -Empty sheet title is empty string: true +Empty sheet title: null Empty sheet cssRules is empty: true Empty sheet is disabled by default: false cssRules length after insertRule(): 1 diff --git a/Tests/LibWeb/Text/input/css/CSSStyleSheet-constructor.html b/Tests/LibWeb/Text/input/css/CSSStyleSheet-constructor.html index 70f5c59647b..d08b2fc05bd 100644 --- a/Tests/LibWeb/Text/input/css/CSSStyleSheet-constructor.html +++ b/Tests/LibWeb/Text/input/css/CSSStyleSheet-constructor.html @@ -6,7 +6,7 @@ const sheet = new CSSStyleSheet(); println(`Empty sheet ownerNode: ${sheet.ownerNode}`); 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 is disabled by default: ${sheet.disabled}`); diff --git a/Userland/Libraries/LibWeb/CSS/StyleSheet.cpp b/Userland/Libraries/LibWeb/CSS/StyleSheet.cpp index 194cb561ea8..a5c95b51748 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleSheet.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleSheet.cpp @@ -36,4 +36,14 @@ void StyleSheet::set_parent_css_style_sheet(CSSStyleSheet* parent) m_parent_style_sheet = parent; } +// https://drafts.csswg.org/cssom/#dom-stylesheet-title +Optional 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; +} + } diff --git a/Userland/Libraries/LibWeb/CSS/StyleSheet.h b/Userland/Libraries/LibWeb/CSS/StyleSheet.h index 7449f5d0655..8d59417e0a3 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleSheet.h +++ b/Userland/Libraries/LibWeb/CSS/StyleSheet.h @@ -29,8 +29,9 @@ public: Optional location() const { return m_location; } void set_location(Optional location) { m_location = move(location); } - Optional title() const { return m_title; } - void set_title(Optional title) { m_title = move(title); } + String title() const { return m_title; } + Optional title_for_bindings() const; + void set_title(String title) { m_title = move(title); } void set_type(String type) { m_type_string = move(type); } @@ -66,7 +67,7 @@ private: JS::GCPtr m_parent_style_sheet; Optional m_location; - Optional m_title; + String m_title; String m_type_string; bool m_disabled { false }; diff --git a/Userland/Libraries/LibWeb/CSS/StyleSheet.idl b/Userland/Libraries/LibWeb/CSS/StyleSheet.idl index 8ef9cdb8d20..c4fb1e3dc88 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleSheet.idl +++ b/Userland/Libraries/LibWeb/CSS/StyleSheet.idl @@ -13,7 +13,7 @@ interface StyleSheet { readonly attribute Element? ownerNode; readonly attribute CSSStyleSheet? parentStyleSheet; - readonly attribute DOMString? title; + [ImplementedAs=title_for_bindings] readonly attribute DOMString? title; [SameObject, PutForwards=mediaText] readonly attribute MediaList media; attribute boolean disabled; diff --git a/Userland/Libraries/LibWeb/CSS/StyleSheetList.cpp b/Userland/Libraries/LibWeb/CSS/StyleSheetList.cpp index 27192abe4c3..92190f93b52 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleSheetList.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleSheetList.cpp @@ -36,11 +36,9 @@ void StyleSheetList::add_a_css_style_sheet(CSS::CSSStyleSheet& sheet) if (sheet.disabled()) 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. - 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: @@ -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 // 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 - 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); return; }