2 * Copyright (C) 2011, 2012 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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "core/css/CSSValuePool.h"
29 #include "core/css/parser/BisonCSSParser.h"
30 #include "core/css/CSSValueList.h"
31 #include "core/rendering/style/RenderStyle.h"
35 CSSValuePool& cssValuePool()
37 DEFINE_STATIC_LOCAL(CSSValuePool, pool, ());
41 CSSValuePool::CSSValuePool()
42 : m_inheritedValue(CSSInheritedValue::create())
43 , m_implicitInitialValue(CSSInitialValue::createImplicit())
44 , m_explicitInitialValue(CSSInitialValue::createExplicit())
45 , m_colorTransparent(CSSPrimitiveValue::createColor(Color::transparent))
46 , m_colorWhite(CSSPrimitiveValue::createColor(Color::white))
47 , m_colorBlack(CSSPrimitiveValue::createColor(Color::black))
51 PassRefPtr<CSSPrimitiveValue> CSSValuePool::createIdentifierValue(CSSValueID ident)
54 return CSSPrimitiveValue::createIdentifier(ident);
56 if (!m_identifierValueCache[ident])
57 m_identifierValueCache[ident] = CSSPrimitiveValue::createIdentifier(ident);
58 return m_identifierValueCache[ident];
61 PassRefPtr<CSSPrimitiveValue> CSSValuePool::createIdentifierValue(CSSPropertyID ident)
63 return CSSPrimitiveValue::createIdentifier(ident);
66 PassRefPtr<CSSPrimitiveValue> CSSValuePool::createColorValue(unsigned rgbValue)
68 // These are the empty and deleted values of the hash table.
69 if (rgbValue == Color::transparent)
70 return m_colorTransparent;
71 if (rgbValue == Color::white)
73 // Just because it is common.
74 if (rgbValue == Color::black)
77 // Just wipe out the cache and start rebuilding if it gets too big.
78 const unsigned maximumColorCacheSize = 512;
79 if (m_colorValueCache.size() > maximumColorCacheSize)
80 m_colorValueCache.clear();
82 RefPtr<CSSPrimitiveValue> dummyValue;
83 ColorValueCache::AddResult entry = m_colorValueCache.add(rgbValue, dummyValue);
85 entry.iterator->value = CSSPrimitiveValue::createColor(rgbValue);
86 return entry.iterator->value;
89 PassRefPtr<CSSPrimitiveValue> CSSValuePool::createValue(double value, CSSPrimitiveValue::UnitTypes type)
91 if (value < 0 || value > maximumCacheableIntegerValue)
92 return CSSPrimitiveValue::create(value, type);
94 int intValue = static_cast<int>(value);
95 if (value != intValue)
96 return CSSPrimitiveValue::create(value, type);
98 RefPtr<CSSPrimitiveValue>* cache;
100 case CSSPrimitiveValue::CSS_PX:
101 cache = m_pixelValueCache;
103 case CSSPrimitiveValue::CSS_PERCENTAGE:
104 cache = m_percentValueCache;
106 case CSSPrimitiveValue::CSS_NUMBER:
107 cache = m_numberValueCache;
110 return CSSPrimitiveValue::create(value, type);
113 if (!cache[intValue])
114 cache[intValue] = CSSPrimitiveValue::create(value, type);
115 return cache[intValue];
118 PassRefPtr<CSSPrimitiveValue> CSSValuePool::createValue(const Length& value, const RenderStyle& style)
120 return CSSPrimitiveValue::create(value, style.effectiveZoom());
123 PassRefPtr<CSSPrimitiveValue> CSSValuePool::createFontFamilyValue(const String& familyName)
125 RefPtr<CSSPrimitiveValue>& value = m_fontFamilyValueCache.add(familyName, 0).iterator->value;
127 value = CSSPrimitiveValue::create(familyName, CSSPrimitiveValue::CSS_STRING);
131 PassRefPtr<CSSValueList> CSSValuePool::createFontFaceValue(const AtomicString& string)
133 // Just wipe out the cache and start rebuilding if it gets too big.
134 const unsigned maximumFontFaceCacheSize = 128;
135 if (m_fontFaceValueCache.size() > maximumFontFaceCacheSize)
136 m_fontFaceValueCache.clear();
138 RefPtr<CSSValueList>& value = m_fontFaceValueCache.add(string, 0).iterator->value;
140 value = BisonCSSParser::parseFontFaceValue(string);