2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "CSSVariablesDeclaration.h"
29 #include "CSSParser.h"
31 #include "CSSValueList.h"
33 #include "ExceptionCode.h"
37 CSSVariablesDeclaration::CSSVariablesDeclaration(StyleBase* parent, const Vector<String>& names, const Vector<RefPtr<CSSValue> >& values)
40 m_variableNames = names;
41 ASSERT(names.size() == values.size());
42 unsigned s = names.size();
43 for (unsigned i = 0; i < s; ++i)
44 addParsedVariable(names[i], values[i], false);
47 CSSVariablesDeclaration::~CSSVariablesDeclaration()
51 String CSSVariablesDeclaration::getVariableValue(const String& variableName)
53 CSSValue* val = m_variablesMap.get(variableName).get();
55 return val->cssText();
59 String CSSVariablesDeclaration::removeVariable(const String& variableName, ExceptionCode&)
61 // FIXME: The spec has this method taking an exception code but no exceptions are
62 // specified as being thrown.
63 RefPtr<CSSValue> val = m_variablesMap.take(variableName);
64 String result = val ? val->cssText() : "";
66 int s = m_variableNames.size();
67 for (int i = 0; i < s; ++i) {
68 if (m_variableNames[i] == variableName) {
69 m_variableNames.remove(i);
75 setNeedsStyleRecalc();
78 // FIXME: Communicate this change so that the document will update.
82 void CSSVariablesDeclaration::setVariable(const String& variableName, const String& variableValue, ExceptionCode& excCode)
84 // Try to parse the variable value into a Value*. If it fails we throw an exception.
85 CSSParser parser(useStrictParsing());
86 if (!parser.parseVariable(this, variableName, variableValue)) // If the parse succeeds, it will call addParsedVariable (our internal method for doing the add) with the parsed Value*.
89 setNeedsStyleRecalc();
92 void CSSVariablesDeclaration::addParsedVariable(const String& variableName, PassRefPtr<CSSValue> variableValue, bool updateNamesList)
94 // FIXME: Disabling declarations as variable values for now since they no longer have a common base class with CSSValues.
96 variableValue->setParent(this); // Needed to connect variables that are CSSMutableStyleDeclarations, since the parent couldn't be set until now.
99 // Don't leak duplicates. For multiple variables with the same name, the last one
100 // declared will win.
101 CSSValue* current = m_variablesMap.take(variableName).get();
102 if (!current && updateNamesList)
103 m_variableNames.append(variableName);
104 m_variablesMap.set(variableName, variableValue);
106 // FIXME: Communicate this change so the document will update.
109 CSSValueList* CSSVariablesDeclaration::getParsedVariable(const String& variableName)
111 CSSValue* result = m_variablesMap.get(variableName).get();
112 if (result->isValueList())
113 return static_cast<CSSValueList*>(result);
117 CSSMutableStyleDeclaration* CSSVariablesDeclaration::getParsedVariableDeclarationBlock(const String&)
119 // FIXME: Disabling declarations as variable values for now since they no longer have a common base class with CSSValues.
121 StyleBase* result = m_variablesMap.get(variableName).get();
123 if (result->isMutableStyleDeclaration())
124 return static_cast<CSSMutableStyleDeclaration*>(result);
129 unsigned CSSVariablesDeclaration::length() const
131 return m_variableNames.size();
134 String CSSVariablesDeclaration::item(unsigned index)
136 if (index >= m_variableNames.size())
138 return m_variableNames[index];
141 CSSRule* CSSVariablesDeclaration::parentRule()
143 return (parent() && parent()->isRule()) ? static_cast<CSSRule*>(parent()) : 0;
146 String CSSVariablesDeclaration::cssText() const
148 String result = "{ ";
149 unsigned s = m_variableNames.size();
150 for (unsigned i = 0; i < s; ++i) {
151 result += m_variableNames[i] + ": ";
152 result += m_variablesMap.get(m_variableNames[i])->cssText();
160 void CSSVariablesDeclaration::setCssText(const String&)
162 // FIXME: It's not clear if this is actually settable.
165 void CSSVariablesDeclaration::setNeedsStyleRecalc()
167 // FIXME: Make this much better (it has the same problem CSSMutableStyleDeclaration does).
168 StyleBase* root = this;
169 while (StyleBase* parent = root->parent())
171 if (root->isCSSStyleSheet())
172 static_cast<CSSStyleSheet*>(root)->doc()->updateStyleSelector();