Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Side by Side Diff: third_party/WebKit/Source/core/css/CSSPrimitiveValue.cpp

Issue 1938343002: Generate a series of nested switch statements to parse CSSPrimitiveValue units. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * (C) 1999-2003 Lars Knoll (knoll@kde.org) 2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2012 Apple Inc. All rights reserv ed. 3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2012 Apple Inc. All rights reserv ed.
4 * 4 *
5 * This library is free software; you can redistribute it and/or 5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public 6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either 7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version. 8 * version 2 of the License, or (at your option) any later version.
9 * 9 *
10 * This library is distributed in the hope that it will be useful, 10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details. 13 * Library General Public License for more details.
14 * 14 *
15 * You should have received a copy of the GNU Library General Public License 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 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, 17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA. 18 * Boston, MA 02110-1301, USA.
19 */ 19 */
20 20
21 #include "core/css/CSSPrimitiveValue.h" 21 #include "core/css/CSSPrimitiveValue.h"
22 22
23 #include "core/css/CSSCalculationValue.h" 23 #include "core/css/CSSCalculationValue.h"
24 #include "core/css/CSSHelper.h" 24 #include "core/css/CSSHelper.h"
25 #include "core/css/CSSMarkup.h" 25 #include "core/css/CSSMarkup.h"
26 #include "core/css/CSSPrimitiveValueUnitTrie.h"
26 #include "core/css/CSSToLengthConversionData.h" 27 #include "core/css/CSSToLengthConversionData.h"
27 #include "core/css/StyleSheetContents.h" 28 #include "core/css/StyleSheetContents.h"
28 #include "core/dom/Node.h" 29 #include "core/dom/Node.h"
29 #include "core/style/ComputedStyle.h" 30 #include "core/style/ComputedStyle.h"
30 #include "platform/LayoutUnit.h" 31 #include "platform/LayoutUnit.h"
31 #include "platform/fonts/FontMetrics.h" 32 #include "platform/fonts/FontMetrics.h"
32 #include "wtf/StdLibExtras.h" 33 #include "wtf/StdLibExtras.h"
33 #include "wtf/text/StringBuffer.h" 34 #include "wtf/text/StringBuffer.h"
34 #include "wtf/text/StringBuilder.h" 35 #include "wtf/text/StringBuilder.h"
35 36
36 using namespace WTF; 37 using namespace WTF;
37 38
38 namespace blink { 39 namespace blink {
39 40
40 namespace { 41 namespace {
41 42
42 // Max/min values for CSS, needs to slightly smaller/larger than the true max/mi n values to allow for rounding without overflowing. 43 // Max/min values for CSS, needs to slightly smaller/larger than the true max/mi n values to allow for rounding without overflowing.
43 // Subtract two (rather than one) to allow for values to be converted to float a nd back without exceeding the LayoutUnit::max. 44 // Subtract two (rather than one) to allow for values to be converted to float a nd back without exceeding the LayoutUnit::max.
44 const int maxValueForCssLength = INT_MAX / kFixedPointDenominator - 2; 45 const int maxValueForCssLength = INT_MAX / kFixedPointDenominator - 2;
45 const int minValueForCssLength = INT_MIN / kFixedPointDenominator + 2; 46 const int minValueForCssLength = INT_MIN / kFixedPointDenominator + 2;
46 47
47 using StringToUnitTable = HashMap<String, CSSPrimitiveValue::UnitType>;
48
49 StringToUnitTable createStringToUnitTable()
50 {
51 StringToUnitTable table;
52 table.set(String("em"), CSSPrimitiveValue::UnitType::Ems);
53 table.set(String("ex"), CSSPrimitiveValue::UnitType::Exs);
54 table.set(String("px"), CSSPrimitiveValue::UnitType::Pixels);
55 table.set(String("cm"), CSSPrimitiveValue::UnitType::Centimeters);
56 table.set(String("mm"), CSSPrimitiveValue::UnitType::Millimeters);
57 table.set(String("in"), CSSPrimitiveValue::UnitType::Inches);
58 table.set(String("pt"), CSSPrimitiveValue::UnitType::Points);
59 table.set(String("pc"), CSSPrimitiveValue::UnitType::Picas);
60 table.set(String(""), CSSPrimitiveValue::UnitType::UserUnits);
61 table.set(String("deg"), CSSPrimitiveValue::UnitType::Degrees);
62 table.set(String("rad"), CSSPrimitiveValue::UnitType::Radians);
63 table.set(String("grad"), CSSPrimitiveValue::UnitType::Gradians);
64 table.set(String("ms"), CSSPrimitiveValue::UnitType::Milliseconds);
65 table.set(String("s"), CSSPrimitiveValue::UnitType::Seconds);
66 table.set(String("hz"), CSSPrimitiveValue::UnitType::Hertz);
67 table.set(String("khz"), CSSPrimitiveValue::UnitType::Kilohertz);
68 table.set(String("dpi"), CSSPrimitiveValue::UnitType::DotsPerInch);
69 table.set(String("dpcm"), CSSPrimitiveValue::UnitType::DotsPerCentimeter);
70 table.set(String("dppx"), CSSPrimitiveValue::UnitType::DotsPerPixel);
71 table.set(String("vw"), CSSPrimitiveValue::UnitType::ViewportWidth);
72 table.set(String("vh"), CSSPrimitiveValue::UnitType::ViewportHeight);
73 table.set(String("vmin"), CSSPrimitiveValue::UnitType::ViewportMin);
74 table.set(String("vmax"), CSSPrimitiveValue::UnitType::ViewportMax);
75 table.set(String("rem"), CSSPrimitiveValue::UnitType::Rems);
76 table.set(String("fr"), CSSPrimitiveValue::UnitType::Fraction);
77 table.set(String("turn"), CSSPrimitiveValue::UnitType::Turns);
78 table.set(String("ch"), CSSPrimitiveValue::UnitType::Chs);
79 table.set(String("__qem"), CSSPrimitiveValue::UnitType::QuirkyEms);
80 return table;
81 }
82
83 StringToUnitTable& unitTable()
84 {
85 DEFINE_STATIC_LOCAL(StringToUnitTable, unitTable, (createStringToUnitTable() ));
86 return unitTable;
87 }
88
89 } // namespace 48 } // namespace
90 49
91 float CSSPrimitiveValue::clampToCSSLengthRange(double value) 50 float CSSPrimitiveValue::clampToCSSLengthRange(double value)
92 { 51 {
93 return clampTo<float>(value, minValueForCssLength, maxValueForCssLength); 52 return clampTo<float>(value, minValueForCssLength, maxValueForCssLength);
94 } 53 }
95 54
96 void CSSPrimitiveValue::initUnitTable()
97 {
98 // Make sure we initialize this during blink initialization
99 // to avoid racy static local initialization.
100 unitTable();
101 }
102
103 CSSPrimitiveValue::UnitType CSSPrimitiveValue::fromName(const String& unit)
104 {
105 return unitTable().get(unit.lower());
106 }
107
108 CSSPrimitiveValue::UnitCategory CSSPrimitiveValue::unitTypeToUnitCategory(UnitTy pe type) 55 CSSPrimitiveValue::UnitCategory CSSPrimitiveValue::unitTypeToUnitCategory(UnitTy pe type)
109 { 56 {
110 switch (type) { 57 switch (type) {
111 case UnitType::Number: 58 case UnitType::Number:
112 return CSSPrimitiveValue::UNumber; 59 return CSSPrimitiveValue::UNumber;
113 case UnitType::Percentage: 60 case UnitType::Percentage:
114 return CSSPrimitiveValue::UPercent; 61 return CSSPrimitiveValue::UPercent;
115 case UnitType::Pixels: 62 case UnitType::Pixels:
116 case UnitType::Centimeters: 63 case UnitType::Centimeters:
117 case UnitType::Millimeters: 64 case UnitType::Millimeters:
(...skipping 657 matching lines...) Expand 10 before | Expand all | Expand 10 after
775 case UnitType::Calc: 722 case UnitType::Calc:
776 visitor->trace(m_value.calc); 723 visitor->trace(m_value.calc);
777 break; 724 break;
778 default: 725 default:
779 break; 726 break;
780 } 727 }
781 CSSValue::traceAfterDispatch(visitor); 728 CSSValue::traceAfterDispatch(visitor);
782 } 729 }
783 730
784 } // namespace blink 731 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698