Ver Fonte

LibWeb/CSS: Store the style sheet's source text on the CSSStyleSheet

This is to enable the inspector to show this source.

There's a fairly hefty FIXME here because duplicating the source text is
a significant waste of memory. But I don't want to get too sidetracked.
Sam Atkins há 10 meses atrás
pai
commit
b00137df38

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

@@ -371,4 +371,14 @@ void CSSStyleSheet::recalculate_namespaces()
     }
     }
 }
 }
 
 
+void CSSStyleSheet::set_source_text(String source)
+{
+    m_source_text = move(source);
+}
+
+Optional<String> CSSStyleSheet::source_text(Badge<DOM::Document>) const
+{
+    return m_source_text;
+}
+
 }
 }

+ 5 - 0
Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h

@@ -78,6 +78,9 @@ public:
 
 
     bool disallow_modification() const { return m_disallow_modification; }
     bool disallow_modification() const { return m_disallow_modification; }
 
 
+    void set_source_text(String);
+    Optional<String> source_text(Badge<DOM::Document>) const;
+
 private:
 private:
     CSSStyleSheet(JS::Realm&, CSSRuleList&, MediaList&, Optional<URL::URL> location);
     CSSStyleSheet(JS::Realm&, CSSRuleList&, MediaList&, Optional<URL::URL> location);
 
 
@@ -89,6 +92,8 @@ private:
     void set_constructed(bool constructed) { m_constructed = constructed; }
     void set_constructed(bool constructed) { m_constructed = constructed; }
     void set_disallow_modification(bool disallow_modification) { m_disallow_modification = disallow_modification; }
     void set_disallow_modification(bool disallow_modification) { m_disallow_modification = disallow_modification; }
 
 
+    Optional<String> m_source_text;
+
     JS::GCPtr<CSSRuleList> m_rules;
     JS::GCPtr<CSSRuleList> m_rules;
     JS::GCPtr<CSSNamespaceRule> m_default_namespace_rule;
     JS::GCPtr<CSSNamespaceRule> m_default_namespace_rule;
     HashMap<FlyString, JS::GCPtr<CSSNamespaceRule>> m_namespace_rules;
     HashMap<FlyString, JS::GCPtr<CSSNamespaceRule>> m_namespace_rules;

+ 7 - 2
Userland/Libraries/LibWeb/CSS/Parser/Helpers.cpp

@@ -20,9 +20,14 @@ CSS::CSSStyleSheet* parse_css_stylesheet(CSS::Parser::ParsingContext const& cont
     if (css.is_empty()) {
     if (css.is_empty()) {
         auto rule_list = CSS::CSSRuleList::create_empty(context.realm());
         auto rule_list = CSS::CSSRuleList::create_empty(context.realm());
         auto media_list = CSS::MediaList::create(context.realm(), {});
         auto media_list = CSS::MediaList::create(context.realm(), {});
-        return CSS::CSSStyleSheet::create(context.realm(), rule_list, media_list, location);
+        auto style_sheet = CSS::CSSStyleSheet::create(context.realm(), rule_list, media_list, location);
+        style_sheet->set_source_text({});
+        return style_sheet;
     }
     }
-    return CSS::Parser::Parser::create(context, css).parse_as_css_stylesheet(location);
+    auto* style_sheet = CSS::Parser::Parser::create(context, css).parse_as_css_stylesheet(location);
+    // FIXME: Avoid this copy
+    style_sheet->set_source_text(MUST(String::from_utf8(css)));
+    return style_sheet;
 }
 }
 
 
 CSS::ElementInlineCSSStyleDeclaration* parse_css_style_attribute(CSS::Parser::ParsingContext const& context, StringView css, DOM::Element& element)
 CSS::ElementInlineCSSStyleDeclaration* parse_css_style_attribute(CSS::Parser::ParsingContext const& context, StringView css, DOM::Element& element)