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

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: Make all values appear in the .in file 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,
(...skipping 26 matching lines...) Expand all
37 37
38 namespace blink { 38 namespace blink {
39 39
40 namespace { 40 namespace {
41 41
42 // Max/min values for CSS, needs to slightly smaller/larger than the true max/mi n values to allow for rounding without overflowing. 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 // Subtract two (rather than one) to allow for values to be converted to float a nd back without exceeding the LayoutUnit::max. 43 // 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; 44 const int maxValueForCssLength = INT_MAX / kFixedPointDenominator - 2;
45 const int minValueForCssLength = INT_MIN / kFixedPointDenominator + 2; 45 const int minValueForCssLength = INT_MIN / kFixedPointDenominator + 2;
46 46
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("deg"), CSSPrimitiveValue::UnitType::Degrees);
61 table.set(String("rad"), CSSPrimitiveValue::UnitType::Radians);
62 table.set(String("grad"), CSSPrimitiveValue::UnitType::Gradians);
63 table.set(String("ms"), CSSPrimitiveValue::UnitType::Milliseconds);
64 table.set(String("s"), CSSPrimitiveValue::UnitType::Seconds);
65 table.set(String("hz"), CSSPrimitiveValue::UnitType::Hertz);
66 table.set(String("khz"), CSSPrimitiveValue::UnitType::Kilohertz);
67 table.set(String("dpi"), CSSPrimitiveValue::UnitType::DotsPerInch);
68 table.set(String("dpcm"), CSSPrimitiveValue::UnitType::DotsPerCentimeter);
69 table.set(String("dppx"), CSSPrimitiveValue::UnitType::DotsPerPixel);
70 table.set(String("vw"), CSSPrimitiveValue::UnitType::ViewportWidth);
71 table.set(String("vh"), CSSPrimitiveValue::UnitType::ViewportHeight);
72 table.set(String("vmin"), CSSPrimitiveValue::UnitType::ViewportMin);
73 table.set(String("vmax"), CSSPrimitiveValue::UnitType::ViewportMax);
74 table.set(String("rem"), CSSPrimitiveValue::UnitType::Rems);
75 table.set(String("fr"), CSSPrimitiveValue::UnitType::Fraction);
76 table.set(String("turn"), CSSPrimitiveValue::UnitType::Turns);
77 table.set(String("ch"), CSSPrimitiveValue::UnitType::Chs);
78 table.set(String("__qem"), CSSPrimitiveValue::UnitType::QuirkyEms);
79 return table;
80 }
81
82 StringToUnitTable& unitTable()
83 {
84 DEFINE_STATIC_LOCAL(StringToUnitTable, unitTable, (createStringToUnitTable() ));
85 return unitTable;
86 }
87
88 } // namespace 47 } // namespace
89 48
90 float CSSPrimitiveValue::clampToCSSLengthRange(double value) 49 float CSSPrimitiveValue::clampToCSSLengthRange(double value)
91 { 50 {
92 return clampTo<float>(value, minValueForCssLength, maxValueForCssLength); 51 return clampTo<float>(value, minValueForCssLength, maxValueForCssLength);
93 } 52 }
94 53
95 void CSSPrimitiveValue::initUnitTable()
96 {
97 // Make sure we initialize this during blink initialization
98 // to avoid racy static local initialization.
99 unitTable();
100 }
101
102 CSSPrimitiveValue::UnitType CSSPrimitiveValue::fromName(const String& unit)
103 {
104 return unitTable().get(unit.lower());
105 }
106
107 CSSPrimitiveValue::UnitCategory CSSPrimitiveValue::unitTypeToUnitCategory(UnitTy pe type) 54 CSSPrimitiveValue::UnitCategory CSSPrimitiveValue::unitTypeToUnitCategory(UnitTy pe type)
108 { 55 {
109 switch (type) { 56 switch (type) {
110 case UnitType::Number: 57 case UnitType::Number:
111 return CSSPrimitiveValue::UNumber; 58 return CSSPrimitiveValue::UNumber;
112 case UnitType::Percentage: 59 case UnitType::Percentage:
113 return CSSPrimitiveValue::UPercent; 60 return CSSPrimitiveValue::UPercent;
114 case UnitType::Pixels: 61 case UnitType::Pixels:
115 case UnitType::Centimeters: 62 case UnitType::Centimeters:
116 case UnitType::Millimeters: 63 case UnitType::Millimeters:
(...skipping 657 matching lines...) Expand 10 before | Expand all | Expand 10 after
774 case UnitType::Calc: 721 case UnitType::Calc:
775 visitor->trace(m_value.calc); 722 visitor->trace(m_value.calc);
776 break; 723 break;
777 default: 724 default:
778 break; 725 break;
779 } 726 }
780 CSSValue::traceAfterDispatch(visitor); 727 CSSValue::traceAfterDispatch(visitor);
781 } 728 }
782 729
783 } // namespace blink 730 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698