summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/CSS/Selector.cpp
blob: a532df7dd9cb1077a7ec4d5bc81f613e69bfd316 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "Selector.h"
#include <AK/GenericLexer.h>
#include <AK/StringUtils.h>
#include <ctype.h>

namespace Web::CSS {

Selector::Selector(Vector<CompoundSelector>&& compound_selectors)
    : m_compound_selectors(move(compound_selectors))
{
}

Selector::~Selector()
{
}

u32 Selector::specificity() const
{
    unsigned ids = 0;
    unsigned tag_names = 0;
    unsigned classes = 0;

    for (auto& list : m_compound_selectors) {
        for (auto& simple_selector : list.simple_selectors) {
            switch (simple_selector.type) {
            case SimpleSelector::Type::Id:
                ++ids;
                break;
            case SimpleSelector::Type::Class:
                ++classes;
                break;
            case SimpleSelector::Type::TagName:
                ++tag_names;
                break;
            default:
                break;
            }
        }
    }

    return ids * 0x10000 + classes * 0x100 + tag_names;
}

Selector::SimpleSelector::ANPlusBPattern Selector::SimpleSelector::ANPlusBPattern::parse(StringView const& args)
{
    // FIXME: Remove this when the DeprecatedCSSParser is gone.
    // The new Parser::parse_nth_child_pattern() does the same as this, using Tokens.
    CSS::Selector::SimpleSelector::ANPlusBPattern pattern;
    if (args.equals_ignoring_case("odd")) {
        pattern.step_size = 2;
        pattern.offset = 1;
    } else if (args.equals_ignoring_case("even")) {
        pattern.step_size = 2;
    } else {
        auto const consume_int = [](GenericLexer& lexer) -> Optional<int> {
            return AK::StringUtils::convert_to_int(lexer.consume_while([](char c) -> bool {
                return isdigit(c) || c == '+' || c == '-';
            }));
        };

        // Try to match any of following patterns:
        // 1. An+B
        // 2. An
        // 3. B
        // ...where "A" is "step_size", "B" is "offset" and rest are literals.
        // "A" can be omitted, in that case "A" = 1.
        // "A" may have "+" or "-" sign, "B" always must be predated by sign for pattern (1).

        int step_size_or_offset = 0;
        GenericLexer lexer { args };

        // "When a=1, or a=-1, the 1 may be omitted from the rule."
        if (lexer.consume_specific("n") || lexer.consume_specific("+n")) {
            step_size_or_offset = +1;
            lexer.retreat();
        } else if (lexer.consume_specific("-n")) {
            step_size_or_offset = -1;
            lexer.retreat();
        } else {
            auto const value = consume_int(lexer);
            if (!value.has_value())
                return {};
            step_size_or_offset = value.value();
        }

        if (lexer.consume_specific("n")) {
            lexer.ignore_while(isspace);
            if (lexer.next_is('+') || lexer.next_is('-')) {
                auto const sign = lexer.next_is('+') ? 1 : -1;
                lexer.ignore();
                lexer.ignore_while(isspace);

                // "An+B" pattern
                auto const offset = consume_int(lexer);
                if (!offset.has_value())
                    return {};
                pattern.step_size = step_size_or_offset;
                pattern.offset = sign * offset.value();
            } else {
                // "An" pattern
                pattern.step_size = step_size_or_offset;
            }
        } else {
            // "B" pattern
            pattern.offset = step_size_or_offset;
        }

        if (lexer.remaining().length() > 0)
            return {};
    }

    return pattern;
}

}