1/*
2 * Copyright (C) 2012 Intel Corporation. All rights reserved.
3 * Copyright (C) 2012 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above
10 *    copyright notice, this list of conditions and the following
11 *    disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 *    copyright notice, this list of conditions and the following
14 *    disclaimer in the documentation and/or other materials
15 *    provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER âAS ISâ AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
26 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "core/css/CSSViewportRule.h"
33
34#include "core/css/PropertySetCSSStyleDeclaration.h"
35#include "core/css/StylePropertySet.h"
36#include "core/css/StyleRule.h"
37#include "wtf/text/StringBuilder.h"
38
39namespace WebCore {
40
41CSSViewportRule::CSSViewportRule(StyleRuleViewport* viewportRule, CSSStyleSheet* sheet)
42    : CSSRule(sheet)
43    , m_viewportRule(viewportRule)
44{
45}
46
47CSSViewportRule::~CSSViewportRule()
48{
49    if (m_propertiesCSSOMWrapper)
50        m_propertiesCSSOMWrapper->clearParentRule();
51}
52
53CSSStyleDeclaration* CSSViewportRule::style() const
54{
55    if (!m_propertiesCSSOMWrapper)
56        m_propertiesCSSOMWrapper = StyleRuleCSSStyleDeclaration::create(m_viewportRule->mutableProperties(), const_cast<CSSViewportRule*>(this));
57
58    return m_propertiesCSSOMWrapper.get();
59}
60
61String CSSViewportRule::cssText() const
62{
63    StringBuilder result;
64    result.appendLiteral("@viewport { ");
65
66    String decls = m_viewportRule->properties()->asText();
67    result.append(decls);
68    if (!decls.isEmpty())
69        result.append(' ');
70
71    result.append('}');
72
73    return result.toString();
74}
75
76void CSSViewportRule::reattach(StyleRuleBase* rule)
77{
78    ASSERT(rule);
79    ASSERT_WITH_SECURITY_IMPLICATION(rule->isViewportRule());
80    m_viewportRule = static_cast<StyleRuleViewport*>(rule);
81
82    if (m_propertiesCSSOMWrapper)
83        m_propertiesCSSOMWrapper->reattach(m_viewportRule->mutableProperties());
84}
85
86} // namespace WebCore
87