diff options
author | Andreas Kling <kling@serenityos.org> | 2020-06-25 16:43:49 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-06-25 16:43:49 +0200 |
commit | 49dd4b7e8ab1f3c3f07104d056294b46097398f6 (patch) | |
tree | 8b0d7d485d56ee271cf3ecf7492c81ad80ce8f6c /Libraries | |
parent | 8be74ea65c4a1126e3e711fbccbaedcaf798ca1b (diff) | |
download | serenity-49dd4b7e8ab1f3c3f07104d056294b46097398f6.zip |
LibWeb: Compress specificity into a 32-bit unsigned int
Instead of storing the three-part specificy for every selector,
just mash them together into a 32-bit value instead.
This saves both space and time, and matches the behavior of other
browser engines.
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibWeb/CSS/Selector.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibWeb/CSS/Selector.h | 2 | ||||
-rw-r--r-- | Libraries/LibWeb/CSS/Specificity.h | 64 | ||||
-rw-r--r-- | Libraries/LibWeb/CSS/StyleResolver.cpp | 4 |
4 files changed, 6 insertions, 68 deletions
diff --git a/Libraries/LibWeb/CSS/Selector.cpp b/Libraries/LibWeb/CSS/Selector.cpp index 8bc2aafb5e..e5ca4e8962 100644 --- a/Libraries/LibWeb/CSS/Selector.cpp +++ b/Libraries/LibWeb/CSS/Selector.cpp @@ -37,7 +37,7 @@ Selector::~Selector() { } -Specificity Selector::specificity() const +u32 Selector::specificity() const { unsigned ids = 0; unsigned tag_names = 0; @@ -61,7 +61,7 @@ Specificity Selector::specificity() const } } - return { ids, classes, tag_names }; + return ids * 0x10000 + classes * 0x100 + tag_names; } } diff --git a/Libraries/LibWeb/CSS/Selector.h b/Libraries/LibWeb/CSS/Selector.h index b61285847a..d4be26188a 100644 --- a/Libraries/LibWeb/CSS/Selector.h +++ b/Libraries/LibWeb/CSS/Selector.h @@ -91,7 +91,7 @@ public: const Vector<ComplexSelector>& complex_selectors() const { return m_complex_selectors; } - Specificity specificity() const; + u32 specificity() const; private: Vector<ComplexSelector> m_complex_selectors; diff --git a/Libraries/LibWeb/CSS/Specificity.h b/Libraries/LibWeb/CSS/Specificity.h deleted file mode 100644 index 30e7b3c7d5..0000000000 --- a/Libraries/LibWeb/CSS/Specificity.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#pragma once - -namespace Web { - -class Specificity { -public: - Specificity(unsigned ids, unsigned classes, unsigned tag_names) - : m_ids(ids) - , m_classes(classes) - , m_tag_names(tag_names) - { - } - - unsigned ids() const { return m_ids; } - unsigned classes() const { return m_classes; } - unsigned tag_names() const { return m_tag_names; } - - bool operator<(const Specificity& other) const - { - return m_ids < other.m_ids - || m_classes < other.m_classes - || m_tag_names < other.m_tag_names; - } - - bool operator==(const Specificity& other) const - { - return m_ids == other.m_ids - || m_classes == other.m_classes - || m_tag_names == other.m_tag_names; - } - -private: - unsigned m_ids { 0 }; - unsigned m_classes { 0 }; - unsigned m_tag_names { 0 }; -}; - -} diff --git a/Libraries/LibWeb/CSS/StyleResolver.cpp b/Libraries/LibWeb/CSS/StyleResolver.cpp index 142a100342..2d74c34ddd 100644 --- a/Libraries/LibWeb/CSS/StyleResolver.cpp +++ b/Libraries/LibWeb/CSS/StyleResolver.cpp @@ -497,12 +497,14 @@ NonnullRefPtr<StyleProperties> StyleResolver::resolve_style(const Element& eleme quick_sort(matching_rules, [&](MatchingRule& a, MatchingRule& b) { auto& a_selector = a.rule->selectors()[a.selector_index]; auto& b_selector = b.rule->selectors()[b.selector_index]; + auto a_specificity = a_selector.specificity(); + auto b_specificity = b_selector.specificity(); if (a_selector.specificity() == b_selector.specificity()) { if (a.style_sheet_index == b.style_sheet_index) return a.rule_index < b.rule_index; return a.style_sheet_index < b.style_sheet_index; } - return a_selector.specificity() < b_selector.specificity(); + return a_specificity < b_specificity; }); for (auto& match : matching_rules) { |