CSSStyleDeclaration.cpp revision 39ef870949ce97df4abfe4febd0b38b312c85007
1/* 2 * (C) 1999-2003 Lars Knoll (knoll@kde.org) 3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Library General Public 7 * License as published by the Free Software Foundation; either 8 * version 2 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Library General Public License for more details. 14 * 15 * You should have received a copy of the GNU Library General Public License 16 * along with this library; see the file COPYING.LIB. If not, write to 17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 * Boston, MA 02110-1301, USA. 19 */ 20 21#include "config.h" 22#include "CSSStyleDeclaration.h" 23 24#include "CSSMutableStyleDeclaration.h" 25#include "CSSParser.h" 26#include "CSSProperty.h" 27#include "CSSPropertyNames.h" 28#include "CSSRule.h" 29#include <wtf/ASCIICType.h> 30#include <wtf/text/CString.h> 31 32using namespace WTF; 33 34namespace WebCore { 35 36CSSStyleDeclaration::CSSStyleDeclaration(CSSRule* parent) 37 : StyleBase(parent) 38{ 39} 40 41PassRefPtr<CSSValue> CSSStyleDeclaration::getPropertyCSSValue(const String& propertyName) 42{ 43 int propID = cssPropertyID(propertyName); 44 if (!propID) 45 return 0; 46 return getPropertyCSSValue(propID); 47} 48 49String CSSStyleDeclaration::getPropertyValue(const String &propertyName) 50{ 51 int propID = cssPropertyID(propertyName); 52 if (!propID) 53 return String(); 54 return getPropertyValue(propID); 55} 56 57String CSSStyleDeclaration::getPropertyPriority(const String& propertyName) 58{ 59 int propID = cssPropertyID(propertyName); 60 if (!propID) 61 return String(); 62 return getPropertyPriority(propID) ? "important" : ""; 63} 64 65String CSSStyleDeclaration::getPropertyShorthand(const String& propertyName) 66{ 67 int propID = cssPropertyID(propertyName); 68 if (!propID) 69 return String(); 70 int shorthandID = getPropertyShorthand(propID); 71 if (!shorthandID) 72 return String(); 73 return getPropertyName(static_cast<CSSPropertyID>(shorthandID)); 74} 75 76bool CSSStyleDeclaration::isPropertyImplicit(const String& propertyName) 77{ 78 int propID = cssPropertyID(propertyName); 79 if (!propID) 80 return false; 81 return isPropertyImplicit(propID); 82} 83 84void CSSStyleDeclaration::setProperty(const String& propertyName, const String& value, ExceptionCode& ec) 85{ 86 size_t important = value.find("!important", 0, false); 87 int propertyID = cssPropertyID(propertyName); 88 if (!propertyID) 89 return; 90 if (important == notFound) 91 setProperty(propertyID, value, false, ec); 92 else 93 setProperty(propertyID, value.left(important - 1), true, ec); 94} 95 96void CSSStyleDeclaration::setProperty(const String& propertyName, const String& value, const String& priority, ExceptionCode& ec) 97{ 98 int propID = cssPropertyID(propertyName); 99 if (!propID) { 100 // FIXME: Should we raise an exception here? 101 return; 102 } 103 bool important = priority.find("important", 0, false) != notFound; 104 setProperty(propID, value, important, ec); 105} 106 107String CSSStyleDeclaration::removeProperty(const String& propertyName, ExceptionCode& ec) 108{ 109 int propID = cssPropertyID(propertyName); 110 if (!propID) 111 return String(); 112 return removeProperty(propID, ec); 113} 114 115bool CSSStyleDeclaration::isPropertyName(const String& propertyName) 116{ 117 return cssPropertyID(propertyName); 118} 119 120CSSRule* CSSStyleDeclaration::parentRule() const 121{ 122 return (parent() && parent()->isRule()) ? static_cast<CSSRule*>(parent()) : 0; 123} 124 125bool CSSStyleDeclaration::cssPropertyMatches(const CSSProperty* property) const 126{ 127 RefPtr<CSSValue> value = getPropertyCSSValue(property->id()); 128 return value && value->cssText() == property->value()->cssText(); 129} 130 131void CSSStyleDeclaration::diff(CSSMutableStyleDeclaration* style) const 132{ 133 if (!style) 134 return; 135 136 Vector<int> propertiesToRemove; 137 { 138 CSSMutableStyleDeclaration::const_iterator end = style->end(); 139 for (CSSMutableStyleDeclaration::const_iterator it = style->begin(); it != end; ++it) { 140 const CSSProperty& property = *it; 141 if (cssPropertyMatches(&property)) 142 propertiesToRemove.append(property.id()); 143 } 144 } 145 146 // FIXME: This should use mass removal. 147 for (unsigned i = 0; i < propertiesToRemove.size(); i++) 148 style->removeProperty(propertiesToRemove[i]); 149} 150 151PassRefPtr<CSSMutableStyleDeclaration> CSSStyleDeclaration::copyPropertiesInSet(const int* set, unsigned length) const 152{ 153 Vector<CSSProperty> list; 154 list.reserveInitialCapacity(length); 155 for (unsigned i = 0; i < length; i++) { 156 RefPtr<CSSValue> value = getPropertyCSSValue(set[i]); 157 if (value) 158 list.append(CSSProperty(set[i], value.release(), false)); 159 } 160 return CSSMutableStyleDeclaration::create(list); 161} 162 163} // namespace WebCore 164