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.
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.
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.
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.
30
#include "PageRuleCollector.h"
32
#include "StyleProperties.h"
33
#include "StyleRule.h"
34
#include "UserAgentStyle.h"
39
static inline bool comparePageRules(const StyleRulePage* r1, const StyleRulePage* r2)
41
return r1->selector()->specificityForPage() < r2->selector()->specificityForPage();
44
bool PageRuleCollector::isLeftPage(int pageIndex) const
46
bool isFirstPageLeft = false;
47
if (!m_state.rootElementStyle()->isLeftToRightDirection())
48
isFirstPageLeft = true;
50
return (pageIndex + (isFirstPageLeft ? 1 : 0)) % 2;
53
bool PageRuleCollector::isFirstPage(int pageIndex) const
55
// FIXME: In case of forced left/right page, page at index 1 (not 0) can be the first page.
59
String PageRuleCollector::pageName(int /* pageIndex */) const
61
// FIXME: Implement page index to page name mapping.
65
void PageRuleCollector::matchAllPageRules(int pageIndex)
67
const bool isLeft = isLeftPage(pageIndex);
68
const bool isFirst = isFirstPage(pageIndex);
69
const String page = pageName(pageIndex);
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);
78
void PageRuleCollector::matchPageRules(RuleSet* rules, bool isLeftPage, bool isFirstPage, const String& pageName)
83
Vector<StyleRulePage*> matchedPageRules;
84
matchPageRulesForList(matchedPageRules, rules->pageRules(), isLeftPage, isFirstPage, pageName);
85
if (matchedPageRules.isEmpty())
88
std::stable_sort(matchedPageRules.begin(), matchedPageRules.end(), comparePageRules);
90
for (unsigned i = 0; i < matchedPageRules.size(); i++)
91
m_result.authorDeclarations.append({ &matchedPageRules[i]->properties() });
94
static bool checkPageSelectorComponents(const CSSSelector* selector, bool isLeftPage, bool isFirstPage, const String& pageName)
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)
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))
114
void PageRuleCollector::matchPageRulesForList(Vector<StyleRulePage*>& matchedRules, const Vector<StyleRulePage*>& rules, bool isLeftPage, bool isFirstPage, const String& pageName)
116
for (unsigned i = 0; i < rules.size(); ++i) {
117
StyleRulePage* rule = rules[i];
119
if (!checkPageSelectorComponents(rule->selector(), isLeftPage, isFirstPage, pageName))
122
// If the rule has no properties to apply, then ignore it.
123
const StyleProperties& properties = rule->properties();
124
if (properties.isEmpty())
127
// Add this rule to our list of matched rules.
128
matchedRules.append(rule);
133
} // namespace WebCore