Skip to content

Commit

Permalink
LibWeb: Apply style rules in order of specificity (kinda)
Browse files Browse the repository at this point in the history
We now sort the matched rules by the specificity of the first selector
in them. This is not perfect, since a rule can have multiple selectors,
but it is a nice chin-related progression on ACID2. :^)
  • Loading branch information
awesomekling committed Jun 10, 2020
1 parent 4e1939c commit 7fe2f5f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Libraries/LibWeb/CSS/StyleResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <AK/QuickSort.h>
#include <LibWeb/CSS/SelectorEngine.h>
#include <LibWeb/CSS/StyleResolver.h>
#include <LibWeb/CSS/StyleSheet.h>
Expand Down Expand Up @@ -439,6 +440,13 @@ NonnullRefPtr<StyleProperties> StyleResolver::resolve_style(const Element& eleme
element.apply_presentational_hints(*style);

auto matching_rules = collect_matching_rules(element);

// FIXME: We need to look at the specificity of the matching *selector*, not just the matched *rule*!
// FIXME: It's really awkward that NonnullRefPtrVector cannot be quick_sort()'ed
quick_sort(reinterpret_cast<Vector<NonnullRefPtr<StyleRule>>&>(matching_rules), [&](auto& a, auto& b) {
return a->selectors().first().specificity() < b->selectors().first().specificity();
});

for (auto& rule : matching_rules) {
for (auto& property : rule.declaration().properties()) {
set_property_expanding_shorthands(style, property.property_id, property.value);
Expand Down
3 changes: 3 additions & 0 deletions Libraries/LibWeb/CSS/StyleRule.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
namespace Web {

class StyleRule : public RefCounted<StyleRule> {
AK_MAKE_NONCOPYABLE(StyleRule);
AK_MAKE_NONMOVABLE(StyleRule);

public:
static NonnullRefPtr<StyleRule> create(Vector<Selector>&& selectors, NonnullRefPtr<StyleDeclaration>&& declaration)
{
Expand Down

0 comments on commit 7fe2f5f

Please sign in to comment.