From ee64d99a96a03f88365a566539aa29421f4c68d8 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 5 Oct 2019 09:01:12 +0200 Subject: [PATCH] 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. --- Applications/Help/main.cpp | 5 ----- Libraries/LibHTML/CSS/StyleResolver.cpp | 25 +++++++++++++++++++++++-- Libraries/LibHTML/CSS/StyleResolver.h | 3 +++ Userland/html.cpp | 5 ----- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/Applications/Help/main.cpp b/Applications/Help/main.cpp index 8030b5fdd39..d76cbc93c26 100644 --- a/Applications/Help/main.cpp +++ b/Applications/Help/main.cpp @@ -46,10 +46,6 @@ int main(int argc, char* argv[]) auto html_view = HtmlView::construct(splitter); - extern const char default_stylesheet_source[]; - String css = default_stylesheet_source; - auto sheet = parse_css(css); - History history; RefPtr go_back_action; @@ -86,7 +82,6 @@ int main(int argc, char* argv[]) String html = md_document.render_to_html(); auto html_document = parse_html(html); html_document->normalize(); - html_document->add_sheet(sheet); html_view->set_document(html_document); String page_and_section = model->page_and_section(tree_view->selection().first()); diff --git a/Libraries/LibHTML/CSS/StyleResolver.cpp b/Libraries/LibHTML/CSS/StyleResolver.cpp index c592574dc04..081675a9459 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 +void StyleResolver::for_each_stylesheet(Callback callback) const +{ + callback(default_stylesheet()); + for (auto& sheet : document().stylesheets()) { + callback(sheet); + } +} + NonnullRefPtrVector StyleResolver::collect_matching_rules(const Element& element) const { NonnullRefPtrVector 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 StyleResolver::collect_matching_rules(const Eleme } } } - } + }); #ifdef HTML_DEBUG dbgprintf("Rules matching Element{%p}\n", &element); diff --git a/Libraries/LibHTML/CSS/StyleResolver.h b/Libraries/LibHTML/CSS/StyleResolver.h index 077e0545e28..9056e130bac 100644 --- a/Libraries/LibHTML/CSS/StyleResolver.h +++ b/Libraries/LibHTML/CSS/StyleResolver.h @@ -23,5 +23,8 @@ public: NonnullRefPtrVector collect_matching_rules(const Element&) const; private: + template + void for_each_stylesheet(Callback) const; + Document& m_document; }; diff --git a/Userland/html.cpp b/Userland/html.cpp index cff3afb1b09..b2bf2ae691a 100644 --- a/Userland/html.cpp +++ b/Userland/html.cpp @@ -33,14 +33,9 @@ int main(int argc, char** argv) return 1; } - extern const char default_stylesheet_source[]; - String css = default_stylesheet_source; - auto sheet = parse_css(css); - String html = String::copy(f->read_all()); auto document = parse_html(html); document->normalize(); - document->add_sheet(*sheet); auto window = GWindow::construct(); auto widget = HtmlView::construct();