diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-10-05 09:01:12 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-10-05 09:02:29 +0200 |
commit | ee64d99a96a03f88365a566539aa29421f4c68d8 (patch) | |
tree | 071f0730822ecb0df3c8e0cb58747ee623421bec /Libraries/LibHTML/CSS/StyleResolver.cpp | |
parent | 77218b1c2a226359569970486032f8fbff97686f (diff) | |
download | serenity-ee64d99a96a03f88365a566539aa29421f4c68d8.zip |
LibHTML: Make StyleResolver responsible for loading the default style
Instead of HtmlView clients having to worry about parsing and loading
the default CSS, just take care of it inside StyleResolver.
The default style is automatically inserted into the stylesheet list,
at the very start, so everyone else gets a chance to override it.
Diffstat (limited to 'Libraries/LibHTML/CSS/StyleResolver.cpp')
-rw-r--r-- | Libraries/LibHTML/CSS/StyleResolver.cpp | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/Libraries/LibHTML/CSS/StyleResolver.cpp b/Libraries/LibHTML/CSS/StyleResolver.cpp index c592574dc0..081675a945 100644 --- a/Libraries/LibHTML/CSS/StyleResolver.cpp +++ b/Libraries/LibHTML/CSS/StyleResolver.cpp @@ -33,10 +33,31 @@ static bool matches(const Selector& selector, const Element& element) } } +static StyleSheet& default_stylesheet() +{ + static StyleSheet* sheet; + if (!sheet) { + extern const char default_stylesheet_source[]; + String css = default_stylesheet_source; + sheet = &parse_css(css).leak_ref(); + } + return *sheet; +} + +template<typename Callback> +void StyleResolver::for_each_stylesheet(Callback callback) const +{ + callback(default_stylesheet()); + for (auto& sheet : document().stylesheets()) { + callback(sheet); + } +} + NonnullRefPtrVector<StyleRule> StyleResolver::collect_matching_rules(const Element& element) const { NonnullRefPtrVector<StyleRule> matching_rules; - for (auto& sheet : document().stylesheets()) { + + for_each_stylesheet([&](auto& sheet) { for (auto& rule : sheet.rules()) { for (auto& selector : rule.selectors()) { if (matches(selector, element)) { @@ -45,7 +66,7 @@ NonnullRefPtrVector<StyleRule> StyleResolver::collect_matching_rules(const Eleme } } } - } + }); #ifdef HTML_DEBUG dbgprintf("Rules matching Element{%p}\n", &element); |