OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 #include "config.h" |
| 32 #include "core/css/parser/CSSToken.h" |
| 33 |
| 34 #include "wtf/HashMap.h" |
| 35 #include "wtf/text/StringHash.h" |
| 36 |
| 37 namespace WebCore { |
| 38 |
| 39 typedef HashMap<String, CSSPrimitiveValue::UnitTypes> UnitTable; |
| 40 |
| 41 UnitTable createUnitTable() |
| 42 { |
| 43 UnitTable table; |
| 44 table.set(String("em"), CSSPrimitiveValue::CSS_EMS); |
| 45 table.set(String("ex"), CSSPrimitiveValue::CSS_EXS); |
| 46 table.set(String("px"), CSSPrimitiveValue::CSS_PX); |
| 47 table.set(String("cm"), CSSPrimitiveValue::CSS_CM); |
| 48 table.set(String("mm"), CSSPrimitiveValue::CSS_MM); |
| 49 table.set(String("in"), CSSPrimitiveValue::CSS_IN); |
| 50 table.set(String("pt"), CSSPrimitiveValue::CSS_PT); |
| 51 table.set(String("pc"), CSSPrimitiveValue::CSS_PC); |
| 52 table.set(String("deg"), CSSPrimitiveValue::CSS_DEG); |
| 53 table.set(String("rad"), CSSPrimitiveValue::CSS_RAD); |
| 54 table.set(String("grad"), CSSPrimitiveValue::CSS_GRAD); |
| 55 table.set(String("ms"), CSSPrimitiveValue::CSS_MS); |
| 56 table.set(String("s"), CSSPrimitiveValue::CSS_S); |
| 57 table.set(String("hz"), CSSPrimitiveValue::CSS_HZ); |
| 58 table.set(String("khz"), CSSPrimitiveValue::CSS_KHZ); |
| 59 return table; |
| 60 } |
| 61 |
| 62 static UnitTable* getUnitTable() |
| 63 { |
| 64 // Allocating to avoid exit time desctructor |
| 65 static UnitTable* unitTable = new UnitTable(createUnitTable()); |
| 66 return unitTable; |
| 67 } |
| 68 |
| 69 CSSToken::CSSToken(CSSTokenType type) |
| 70 : m_type(type) |
| 71 , m_delimiter(0) |
| 72 , m_unit(CSSPrimitiveValue::CSS_UNKNOWN) |
| 73 { |
| 74 } |
| 75 |
| 76 // Just a helper used for Delim tokens. |
| 77 CSSToken::CSSToken(CSSTokenType type, UChar c) |
| 78 : m_type(type) |
| 79 , m_delimiter(c) |
| 80 , m_unit(CSSPrimitiveValue::CSS_UNKNOWN) |
| 81 { |
| 82 ASSERT(m_type == DelimToken); |
| 83 } |
| 84 |
| 85 CSSToken::CSSToken(CSSTokenType type, String value) |
| 86 : m_type(type) |
| 87 , m_value(value) |
| 88 , m_delimiter(0) |
| 89 , m_unit(CSSPrimitiveValue::CSS_UNKNOWN) |
| 90 { |
| 91 } |
| 92 |
| 93 CSSToken::CSSToken(CSSTokenType type, String name, HashTokenType hashType) |
| 94 : m_type(type) |
| 95 , m_value(name) |
| 96 , m_hashTokenType(hashType) |
| 97 , m_delimiter(0) |
| 98 , m_unit(CSSPrimitiveValue::CSS_UNKNOWN) |
| 99 { |
| 100 ASSERT(m_type == HashToken); |
| 101 } |
| 102 |
| 103 CSSToken::CSSToken(CSSTokenType type, String value, double numericValue, Numeric
ValueType numericValueType) |
| 104 : m_type(type) |
| 105 , m_value(value) |
| 106 , m_delimiter(0) |
| 107 , m_numericValueType(numericValueType) |
| 108 , m_numericValue(numericValue) |
| 109 , m_unit(CSSPrimitiveValue::CSS_NUMBER) |
| 110 { |
| 111 ASSERT(type == NumberToken); |
| 112 } |
| 113 |
| 114 void CSSToken::convertToDimensionWithUnit(String unit) |
| 115 { |
| 116 ASSERT(m_type == NumberToken); |
| 117 m_type = DimensionToken; |
| 118 StringImpl* lowerUnit = unit.lower().impl(); |
| 119 if (getUnitTable()->contains(lowerUnit)) |
| 120 m_unit = getUnitTable()->get(lowerUnit); |
| 121 else |
| 122 m_unit = CSSPrimitiveValue::CSS_UNKNOWN; |
| 123 } |
| 124 |
| 125 void CSSToken::convertToPercentage() |
| 126 { |
| 127 ASSERT(m_type == NumberToken); |
| 128 m_type = PercentageToken; |
| 129 m_unit = CSSPrimitiveValue::CSS_PERCENTAGE; |
| 130 } |
| 131 |
| 132 } // namespace WebCore |
OLD | NEW |