1/*
2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * (C) 2002-2003 Dirk Mueller (mueller@kde.org)
4 * Copyright (C) 2002, 2005, 2006, 2008, 2012 Apple Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB.  If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22#include "config.h"
23#include "CSSPageRule.h"
24
25#include "CSSParser.h"
26#include "CSSSelector.h"
27#include "CSSStyleSheet.h"
28#include "Document.h"
29#include "PropertySetCSSStyleDeclaration.h"
30#include "StylePropertySet.h"
31#include "StyleRule.h"
32#include <wtf/Vector.h>
33#include <wtf/text/StringBuilder.h>
34
35namespace WebCore {
36
37CSSPageRule::CSSPageRule(StyleRulePage* pageRule, CSSStyleSheet* parent)
38    : CSSRule(parent)
39    , m_pageRule(pageRule)
40{
41}
42
43CSSPageRule::~CSSPageRule()
44{
45    if (m_propertiesCSSOMWrapper)
46        m_propertiesCSSOMWrapper->clearParentRule();
47}
48
49CSSStyleDeclaration* CSSPageRule::style()
50{
51    if (!m_propertiesCSSOMWrapper)
52        m_propertiesCSSOMWrapper = StyleRuleCSSStyleDeclaration::create(m_pageRule->mutableProperties(), this);
53    return m_propertiesCSSOMWrapper.get();
54}
55
56String CSSPageRule::selectorText() const
57{
58    StringBuilder text;
59    text.appendLiteral("@page");
60    const CSSSelector* selector = m_pageRule->selector();
61    if (selector) {
62        String pageSpecification = selector->selectorText();
63        if (!pageSpecification.isEmpty() && pageSpecification != starAtom) {
64            text.append(' ');
65            text.append(pageSpecification);
66        }
67    }
68    return text.toString();
69}
70
71void CSSPageRule::setSelectorText(const String& selectorText)
72{
73    CSSParser parser(parserContext());
74    CSSSelectorList selectorList;
75    parser.parseSelector(selectorText, selectorList);
76    if (!selectorList.isValid())
77        return;
78
79    CSSStyleSheet::RuleMutationScope mutationScope(this);
80
81    m_pageRule->wrapperAdoptSelectorList(selectorList);
82}
83
84String CSSPageRule::cssText() const
85{
86    StringBuilder result;
87    result.append(selectorText());
88    result.appendLiteral(" { ");
89    String decls = m_pageRule->properties()->asText();
90    result.append(decls);
91    if (!decls.isEmpty())
92        result.append(' ');
93    result.append('}');
94    return result.toString();
95}
96
97void CSSPageRule::reattach(StyleRuleBase* rule)
98{
99    ASSERT(rule);
100    ASSERT_WITH_SECURITY_IMPLICATION(rule->isPageRule());
101    m_pageRule = static_cast<StyleRulePage*>(rule);
102    if (m_propertiesCSSOMWrapper)
103        m_propertiesCSSOMWrapper->reattach(m_pageRule->mutableProperties());
104}
105
106} // namespace WebCore
107