~mmach/netext73/webkit2gtk

« back to all changes in this revision

Viewing changes to Source/WebCore/style/PageRuleCollector.cpp

  • Committer: mmach
  • Date: 2023-06-16 17:21:37 UTC
  • Revision ID: netbit73@gmail.com-20230616172137-2rqx6yr96ga9g3kp
1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
 
3
 *           (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com)
 
4
 * Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com)
 
5
 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Apple Inc. All rights reserved.
 
6
 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
 
7
 * Copyright (C) 2007, 2008 Eric Seidel <eric@webkit.org>
 
8
 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
 
9
 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
 
10
 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
 
11
 * Copyright (C) 2012 Google Inc. All rights reserved.
 
12
 *
 
13
 * This library is free software; you can redistribute it and/or
 
14
 * modify it under the terms of the GNU Library General Public
 
15
 * License as published by the Free Software Foundation; either
 
16
 * version 2 of the License, or (at your option) any later version.
 
17
 *
 
18
 * This library is distributed in the hope that it will be useful,
 
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
21
 * Library General Public License for more details.
 
22
 *
 
23
 * You should have received a copy of the GNU Library General Public License
 
24
 * along with this library; see the file COPYING.LIB.  If not, write to
 
25
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
26
 * Boston, MA 02110-1301, USA.
 
27
 */
 
28
 
 
29
#include "config.h"
 
30
#include "PageRuleCollector.h"
 
31
 
 
32
#include "StyleProperties.h"
 
33
#include "StyleRule.h"
 
34
#include "UserAgentStyle.h"
 
35
 
 
36
namespace WebCore {
 
37
namespace Style {
 
38
 
 
39
static inline bool comparePageRules(const StyleRulePage* r1, const StyleRulePage* r2)
 
40
{
 
41
    return r1->selector()->specificityForPage() < r2->selector()->specificityForPage();
 
42
}
 
43
 
 
44
bool PageRuleCollector::isLeftPage(int pageIndex) const
 
45
{
 
46
    bool isFirstPageLeft = false;
 
47
    if (!m_state.rootElementStyle()->isLeftToRightDirection())
 
48
        isFirstPageLeft = true;
 
49
 
 
50
    return (pageIndex + (isFirstPageLeft ? 1 : 0)) % 2;
 
51
}
 
52
 
 
53
bool PageRuleCollector::isFirstPage(int pageIndex) const
 
54
{
 
55
    // FIXME: In case of forced left/right page, page at index 1 (not 0) can be the first page.
 
56
    return (!pageIndex);
 
57
}
 
58
 
 
59
String PageRuleCollector::pageName(int /* pageIndex */) const
 
60
{
 
61
    // FIXME: Implement page index to page name mapping.
 
62
    return emptyString();
 
63
}
 
64
 
 
65
void PageRuleCollector::matchAllPageRules(int pageIndex)
 
66
{
 
67
    const bool isLeft = isLeftPage(pageIndex);
 
68
    const bool isFirst = isFirstPage(pageIndex);
 
69
    const String page = pageName(pageIndex);
 
70
    
 
71
    matchPageRules(UserAgentStyle::defaultPrintStyle, isLeft, isFirst, page);
 
72
    matchPageRules(m_ruleSets.userStyle(), isLeft, isFirst, page);
 
73
    // Only consider the global author RuleSet for @page rules, as per the HTML5 spec.
 
74
    if (m_ruleSets.isAuthorStyleDefined())
 
75
        matchPageRules(&m_ruleSets.authorStyle(), isLeft, isFirst, page);
 
76
}
 
77
 
 
78
void PageRuleCollector::matchPageRules(RuleSet* rules, bool isLeftPage, bool isFirstPage, const String& pageName)
 
79
{
 
80
    if (!rules)
 
81
        return;
 
82
 
 
83
    Vector<StyleRulePage*> matchedPageRules;
 
84
    matchPageRulesForList(matchedPageRules, rules->pageRules(), isLeftPage, isFirstPage, pageName);
 
85
    if (matchedPageRules.isEmpty())
 
86
        return;
 
87
 
 
88
    std::stable_sort(matchedPageRules.begin(), matchedPageRules.end(), comparePageRules);
 
89
 
 
90
    for (unsigned i = 0; i < matchedPageRules.size(); i++)
 
91
        m_result.authorDeclarations.append({ &matchedPageRules[i]->properties() });
 
92
}
 
93
 
 
94
static bool checkPageSelectorComponents(const CSSSelector* selector, bool isLeftPage, bool isFirstPage, const String& pageName)
 
95
{
 
96
    for (const CSSSelector* component = selector; component; component = component->tagHistory()) {
 
97
        if (component->match() == CSSSelector::Tag) {
 
98
            const AtomString& localName = component->tagQName().localName();
 
99
            if (localName != starAtom() && localName != pageName)
 
100
                return false;
 
101
        } else if (component->match() == CSSSelector::PagePseudoClass) {
 
102
            CSSSelector::PagePseudoClassType pseudoType = component->pagePseudoClassType();
 
103
            if ((pseudoType == CSSSelector::PagePseudoClassLeft && !isLeftPage)
 
104
                || (pseudoType == CSSSelector::PagePseudoClassRight && isLeftPage)
 
105
                || (pseudoType == CSSSelector::PagePseudoClassFirst && !isFirstPage))
 
106
            {
 
107
                return false;
 
108
            }
 
109
        }
 
110
    }
 
111
    return true;
 
112
}
 
113
 
 
114
void PageRuleCollector::matchPageRulesForList(Vector<StyleRulePage*>& matchedRules, const Vector<StyleRulePage*>& rules, bool isLeftPage, bool isFirstPage, const String& pageName)
 
115
{
 
116
    for (unsigned i = 0; i < rules.size(); ++i) {
 
117
        StyleRulePage* rule = rules[i];
 
118
 
 
119
        if (!checkPageSelectorComponents(rule->selector(), isLeftPage, isFirstPage, pageName))
 
120
            continue;
 
121
 
 
122
        // If the rule has no properties to apply, then ignore it.
 
123
        const StyleProperties& properties = rule->properties();
 
124
        if (properties.isEmpty())
 
125
            continue;
 
126
 
 
127
        // Add this rule to our list of matched rules.
 
128
        matchedRules.append(rule);
 
129
    }
 
130
}
 
131
 
 
132
} // namespace Style
 
133
} // namespace WebCore