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

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

Issue 717933002: Clean up after getPropertyCSSValue (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: rebase / fix debug compile? Created 6 years, 1 month 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
« no previous file with comments | « Source/core/css/CSSPrimitiveValue.h ('k') | Source/core/css/CSSPrimitiveValue.idl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "config.h" 21 #include "config.h"
22 #include "core/css/CSSPrimitiveValue.h" 22 #include "core/css/CSSPrimitiveValue.h"
23 23
24 #include "bindings/core/v8/ExceptionState.h"
25 #include "core/css/CSSBasicShapes.h" 24 #include "core/css/CSSBasicShapes.h"
26 #include "core/css/CSSCalculationValue.h" 25 #include "core/css/CSSCalculationValue.h"
27 #include "core/css/CSSHelper.h" 26 #include "core/css/CSSHelper.h"
28 #include "core/css/CSSMarkup.h" 27 #include "core/css/CSSMarkup.h"
29 #include "core/css/CSSToLengthConversionData.h" 28 #include "core/css/CSSToLengthConversionData.h"
30 #include "core/css/Counter.h" 29 #include "core/css/Counter.h"
31 #include "core/css/Pair.h" 30 #include "core/css/Pair.h"
32 #include "core/css/RGBColor.h"
33 #include "core/css/Rect.h" 31 #include "core/css/Rect.h"
34 #include "core/css/StyleSheetContents.h" 32 #include "core/css/StyleSheetContents.h"
35 #include "core/dom/ExceptionCode.h"
36 #include "core/dom/Node.h" 33 #include "core/dom/Node.h"
37 #include "core/rendering/style/RenderStyle.h" 34 #include "core/rendering/style/RenderStyle.h"
38 #include "platform/Decimal.h" 35 #include "platform/Decimal.h"
39 #include "platform/LayoutUnit.h" 36 #include "platform/LayoutUnit.h"
40 #include "platform/fonts/FontMetrics.h" 37 #include "platform/fonts/FontMetrics.h"
41 #include "wtf/StdLibExtras.h" 38 #include "wtf/StdLibExtras.h"
42 #include "wtf/text/StringBuffer.h" 39 #include "wtf/text/StringBuffer.h"
43 #include "wtf/text/StringBuilder.h" 40 #include "wtf/text/StringBuilder.h"
44 41
45 using namespace WTF; 42 using namespace WTF;
(...skipping 629 matching lines...) Expand 10 before | Expand all | Expand 10 after
675 if (m_primitiveUnitType == CSS_CALC) { 672 if (m_primitiveUnitType == CSS_CALC) {
676 cssCalcValue()->accumulateLengthArray(lengthArray, multiplier); 673 cssCalcValue()->accumulateLengthArray(lengthArray, multiplier);
677 return; 674 return;
678 } 675 }
679 676
680 LengthUnitType lengthType; 677 LengthUnitType lengthType;
681 if (unitTypeToLengthUnitType(static_cast<UnitType>(m_primitiveUnitType), len gthType)) 678 if (unitTypeToLengthUnitType(static_cast<UnitType>(m_primitiveUnitType), len gthType))
682 lengthArray.at(lengthType) += m_value.num * conversionToCanonicalUnitsSc aleFactor(static_cast<UnitType>(m_primitiveUnitType)) * multiplier; 679 lengthArray.at(lengthType) += m_value.num * conversionToCanonicalUnitsSc aleFactor(static_cast<UnitType>(m_primitiveUnitType)) * multiplier;
683 } 680 }
684 681
685 void CSSPrimitiveValue::setFloatValue(unsigned short, double, ExceptionState& ex ceptionState)
686 {
687 // Keeping values immutable makes optimizations easier and allows sharing of the primitive value objects.
688 // No other engine supports mutating style through this API. Computed style is always read-only anyway.
689 // Supporting setter would require making primitive value copy-on-write and taking care of style invalidation.
690 exceptionState.throwDOMException(NoModificationAllowedError, "CSSPrimitiveVa lue objects are read-only.");
691 }
692
693 double CSSPrimitiveValue::conversionToCanonicalUnitsScaleFactor(UnitType unitTyp e) 682 double CSSPrimitiveValue::conversionToCanonicalUnitsScaleFactor(UnitType unitTyp e)
694 { 683 {
695 double factor = 1.0; 684 double factor = 1.0;
696 // FIXME: the switch can be replaced by an array of scale factors. 685 // FIXME: the switch can be replaced by an array of scale factors.
697 switch (unitType) { 686 switch (unitType) {
698 // These are "canonical" units in their respective categories. 687 // These are "canonical" units in their respective categories.
699 case CSS_PX: 688 case CSS_PX:
700 case CSS_DEG: 689 case CSS_DEG:
701 case CSS_MS: 690 case CSS_MS:
702 case CSS_HZ: 691 case CSS_HZ:
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
735 case CSS_KHZ: 724 case CSS_KHZ:
736 factor = 1000; 725 factor = 1000;
737 break; 726 break;
738 default: 727 default:
739 break; 728 break;
740 } 729 }
741 730
742 return factor; 731 return factor;
743 } 732 }
744 733
745 double CSSPrimitiveValue::getDoubleValue(UnitType unitType, ExceptionState& exce ptionState) const
746 {
747 double result = 0;
748 bool success = getDoubleValueInternal(unitType, &result);
749 if (!success) {
750 exceptionState.throwDOMException(InvalidAccessError, "Failed to obtain a double value.");
751 return 0.0;
752 }
753
754 return result;
755 }
756
757 double CSSPrimitiveValue::getDoubleValue(UnitType unitType) const 734 double CSSPrimitiveValue::getDoubleValue(UnitType unitType) const
758 { 735 {
759 double result = 0; 736 double result = 0;
760 getDoubleValueInternal(unitType, &result); 737 getDoubleValueInternal(unitType, &result);
761 return result; 738 return result;
762 } 739 }
763 740
764 double CSSPrimitiveValue::getDoubleValue() const 741 double CSSPrimitiveValue::getDoubleValue() const
765 { 742 {
766 return m_primitiveUnitType != CSS_CALC ? m_value.num : m_value.calc->doubleV alue(); 743 return m_primitiveUnitType != CSS_CALC ? m_value.num : m_value.calc->doubleV alue();
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
906 return CSSPrimitiveValue::CSS_VMIN; 883 return CSSPrimitiveValue::CSS_VMIN;
907 case UnitTypeViewportMax: 884 case UnitTypeViewportMax:
908 return CSSPrimitiveValue::CSS_VMAX; 885 return CSSPrimitiveValue::CSS_VMAX;
909 case LengthUnitTypeCount: 886 case LengthUnitTypeCount:
910 break; 887 break;
911 } 888 }
912 ASSERT_NOT_REACHED(); 889 ASSERT_NOT_REACHED();
913 return CSSPrimitiveValue::CSS_UNKNOWN; 890 return CSSPrimitiveValue::CSS_UNKNOWN;
914 } 891 }
915 892
916 void CSSPrimitiveValue::setStringValue(unsigned short, const String&, ExceptionS tate& exceptionState)
917 {
918 // Keeping values immutable makes optimizations easier and allows sharing of the primitive value objects.
919 // No other engine supports mutating style through this API. Computed style is always read-only anyway.
920 // Supporting setter would require making primitive value copy-on-write and taking care of style invalidation.
921 exceptionState.throwDOMException(NoModificationAllowedError, "CSSPrimitiveVa lue objects are read-only.");
922 }
923
924 String CSSPrimitiveValue::getStringValue(ExceptionState& exceptionState) const
925 {
926 switch (m_primitiveUnitType) {
927 case CSS_STRING:
928 case CSS_ATTR:
929 case CSS_URI:
930 return m_value.string;
931 case CSS_VALUE_ID:
932 return valueName(m_value.valueID);
933 case CSS_PROPERTY_ID:
934 return propertyName(m_value.propertyID);
935 default:
936 exceptionState.throwDOMException(InvalidAccessError, "This object's value cannot be represented as a string.");
937 break;
938 }
939
940 return String();
941 }
942
943 String CSSPrimitiveValue::getStringValue() const 893 String CSSPrimitiveValue::getStringValue() const
944 { 894 {
945 switch (m_primitiveUnitType) { 895 switch (m_primitiveUnitType) {
946 case CSS_STRING: 896 case CSS_STRING:
947 case CSS_ATTR: 897 case CSS_ATTR:
948 case CSS_URI: 898 case CSS_URI:
949 return m_value.string; 899 return m_value.string;
950 case CSS_VALUE_ID: 900 case CSS_VALUE_ID:
951 return valueName(m_value.valueID); 901 return valueName(m_value.valueID);
952 case CSS_PROPERTY_ID: 902 case CSS_PROPERTY_ID:
953 return propertyName(m_value.propertyID); 903 return propertyName(m_value.propertyID);
954 default: 904 default:
955 break; 905 break;
956 } 906 }
957 907
958 return String(); 908 return String();
959 } 909 }
960 910
961 Counter* CSSPrimitiveValue::getCounterValue(ExceptionState& exceptionState) cons t
962 {
963 if (m_primitiveUnitType != CSS_COUNTER) {
964 exceptionState.throwDOMException(InvalidAccessError, "This object is not a counter value.");
965 return 0;
966 }
967
968 return m_value.counter;
969 }
970
971 Rect* CSSPrimitiveValue::getRectValue(ExceptionState& exceptionState) const
972 {
973 if (m_primitiveUnitType != CSS_RECT) {
974 exceptionState.throwDOMException(InvalidAccessError, "This object is not a rect value.");
975 return 0;
976 }
977
978 return m_value.rect;
979 }
980
981 Quad* CSSPrimitiveValue::getQuadValue(ExceptionState& exceptionState) const
982 {
983 if (m_primitiveUnitType != CSS_QUAD) {
984 exceptionState.throwDOMException(InvalidAccessError, "This object is not a quad value.");
985 return 0;
986 }
987
988 return m_value.quad;
989 }
990
991 PassRefPtrWillBeRawPtr<RGBColor> CSSPrimitiveValue::getRGBColorValue(ExceptionSt ate& exceptionState) const
992 {
993 if (m_primitiveUnitType != CSS_RGBCOLOR) {
994 exceptionState.throwDOMException(InvalidAccessError, "This object is not an RGB color value.");
995 return nullptr;
996 }
997
998 // FIMXE: This should not return a new object for each invocation.
999 return RGBColor::create(m_value.rgbcolor);
1000 }
1001
1002 Pair* CSSPrimitiveValue::getPairValue(ExceptionState& exceptionState) const
1003 {
1004 if (m_primitiveUnitType != CSS_PAIR) {
1005 exceptionState.throwDOMException(InvalidAccessError, "This object is not a pair value.");
1006 return 0;
1007 }
1008
1009 return m_value.pair;
1010 }
1011
1012 static String formatNumber(double number, const char* suffix, unsigned suffixLen gth) 911 static String formatNumber(double number, const char* suffix, unsigned suffixLen gth)
1013 { 912 {
1014 Decimal decimal = Decimal::fromDouble(number); 913 Decimal decimal = Decimal::fromDouble(number);
1015 String result = decimal.toString(); 914 String result = decimal.toString();
1016 result.append(suffix, suffixLength); 915 result.append(suffix, suffixLength);
1017 return result; 916 return result;
1018 } 917 }
1019 918
1020 template <unsigned characterCount> 919 template <unsigned characterCount>
1021 ALWAYS_INLINE static String formatNumber(double number, const char (&characters) [characterCount]) 920 ALWAYS_INLINE static String formatNumber(double number, const char (&characters) [characterCount])
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
1234 text = m_value.shape->cssText(); 1133 text = m_value.shape->cssText();
1235 break; 1134 break;
1236 } 1135 }
1237 1136
1238 ASSERT(!cssTextCache().contains(this)); 1137 ASSERT(!cssTextCache().contains(this));
1239 cssTextCache().set(this, text); 1138 cssTextCache().set(this, text);
1240 m_hasCachedCSSText = true; 1139 m_hasCachedCSSText = true;
1241 return text; 1140 return text;
1242 } 1141 }
1243 1142
1244 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSPrimitiveValue::cloneForCSSOM() con st
1245 {
1246 RefPtrWillBeRawPtr<CSSPrimitiveValue> result = nullptr;
1247
1248 switch (m_primitiveUnitType) {
1249 case CSS_STRING:
1250 case CSS_URI:
1251 case CSS_ATTR:
1252 case CSS_COUNTER_NAME:
1253 result = CSSPrimitiveValue::create(m_value.string, static_cast<UnitType> (m_primitiveUnitType));
1254 break;
1255 case CSS_COUNTER:
1256 result = CSSPrimitiveValue::create(m_value.counter->cloneForCSSOM());
1257 break;
1258 case CSS_RECT:
1259 result = CSSPrimitiveValue::create(m_value.rect->cloneForCSSOM());
1260 break;
1261 case CSS_QUAD:
1262 result = CSSPrimitiveValue::create(m_value.quad->cloneForCSSOM());
1263 break;
1264 case CSS_PAIR:
1265 // Pair is not exposed to the CSSOM, no need for a deep clone.
1266 result = CSSPrimitiveValue::create(m_value.pair);
1267 break;
1268 case CSS_CALC:
1269 // CSSCalcValue is not exposed to the CSSOM, no need for a deep clone.
1270 result = CSSPrimitiveValue::create(m_value.calc);
1271 break;
1272 case CSS_SHAPE:
1273 // CSSShapeValue is not exposed to the CSSOM, no need for a deep clone.
1274 result = CSSPrimitiveValue::create(m_value.shape);
1275 break;
1276 case CSS_NUMBER:
1277 case CSS_PERCENTAGE:
1278 case CSS_EMS:
1279 case CSS_EXS:
1280 case CSS_REMS:
1281 case CSS_CHS:
1282 case CSS_PX:
1283 case CSS_CM:
1284 case CSS_MM:
1285 case CSS_IN:
1286 case CSS_PT:
1287 case CSS_PC:
1288 case CSS_DEG:
1289 case CSS_RAD:
1290 case CSS_GRAD:
1291 case CSS_MS:
1292 case CSS_S:
1293 case CSS_HZ:
1294 case CSS_KHZ:
1295 case CSS_TURN:
1296 case CSS_VW:
1297 case CSS_VH:
1298 case CSS_VMIN:
1299 case CSS_VMAX:
1300 case CSS_DPPX:
1301 case CSS_DPI:
1302 case CSS_DPCM:
1303 case CSS_FR:
1304 result = CSSPrimitiveValue::create(m_value.num, static_cast<UnitType>(m_ primitiveUnitType));
1305 break;
1306 case CSS_PROPERTY_ID:
1307 result = CSSPrimitiveValue::createIdentifier(m_value.propertyID);
1308 break;
1309 case CSS_VALUE_ID:
1310 result = CSSPrimitiveValue::createIdentifier(m_value.valueID);
1311 break;
1312 case CSS_RGBCOLOR:
1313 result = CSSPrimitiveValue::createColor(m_value.rgbcolor);
1314 break;
1315 case CSS_DIMENSION:
1316 case CSS_UNKNOWN:
1317 case CSS_PARSER_HEXCOLOR:
1318 ASSERT_NOT_REACHED();
1319 break;
1320 }
1321 if (result)
1322 result->setCSSOMSafe();
1323
1324 return result;
1325 }
1326
1327 bool CSSPrimitiveValue::equals(const CSSPrimitiveValue& other) const 1143 bool CSSPrimitiveValue::equals(const CSSPrimitiveValue& other) const
1328 { 1144 {
1329 if (m_primitiveUnitType != other.m_primitiveUnitType) 1145 if (m_primitiveUnitType != other.m_primitiveUnitType)
1330 return false; 1146 return false;
1331 1147
1332 switch (m_primitiveUnitType) { 1148 switch (m_primitiveUnitType) {
1333 case CSS_UNKNOWN: 1149 case CSS_UNKNOWN:
1334 return false; 1150 return false;
1335 case CSS_NUMBER: 1151 case CSS_NUMBER:
1336 case CSS_PERCENTAGE: 1152 case CSS_PERCENTAGE:
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1412 visitor->trace(m_value.shape); 1228 visitor->trace(m_value.shape);
1413 break; 1229 break;
1414 default: 1230 default:
1415 break; 1231 break;
1416 } 1232 }
1417 #endif 1233 #endif
1418 CSSValue::traceAfterDispatch(visitor); 1234 CSSValue::traceAfterDispatch(visitor);
1419 } 1235 }
1420 1236
1421 } // namespace blink 1237 } // namespace blink
OLDNEW
« no previous file with comments | « Source/core/css/CSSPrimitiveValue.h ('k') | Source/core/css/CSSPrimitiveValue.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698