1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
|
/*
* (C) 1999-2003 Lars Knoll (knoll@kde.org)
* Copyright (C) 2004, 2005, 2006, 2007, 2008, 2012 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "core/css/CSSPrimitiveValue.h"
#include "core/css/CSSBasicShapes.h"
#include "core/css/CSSCalculationValue.h"
#include "core/css/CSSHelper.h"
#include "core/css/CSSMarkup.h"
#include "core/css/CSSToLengthConversionData.h"
#include "core/css/Counter.h"
#include "core/css/Pair.h"
#include "core/css/Rect.h"
#include "core/css/StyleSheetContents.h"
#include "core/dom/Node.h"
#include "core/rendering/style/RenderStyle.h"
#include "platform/Decimal.h"
#include "platform/LayoutUnit.h"
#include "platform/fonts/FontMetrics.h"
#include "wtf/StdLibExtras.h"
#include "wtf/text/StringBuffer.h"
#include "wtf/text/StringBuilder.h"
using namespace WTF;
namespace blink {
// Max/min values for CSS, needs to slightly smaller/larger than the true max/min values to allow for rounding without overflowing.
// Subtract two (rather than one) to allow for values to be converted to float and back without exceeding the LayoutUnit::max.
const int maxValueForCssLength = INT_MAX / kFixedPointDenominator - 2;
const int minValueForCssLength = INT_MIN / kFixedPointDenominator + 2;
static inline bool isValidCSSUnitTypeForDoubleConversion(CSSPrimitiveValue::UnitType unitType)
{
switch (unitType) {
case CSSPrimitiveValue::CSS_CALC:
case CSSPrimitiveValue::CSS_CALC_PERCENTAGE_WITH_NUMBER:
case CSSPrimitiveValue::CSS_CALC_PERCENTAGE_WITH_LENGTH:
case CSSPrimitiveValue::CSS_CM:
case CSSPrimitiveValue::CSS_DEG:
case CSSPrimitiveValue::CSS_DIMENSION:
case CSSPrimitiveValue::CSS_DPPX:
case CSSPrimitiveValue::CSS_DPI:
case CSSPrimitiveValue::CSS_DPCM:
case CSSPrimitiveValue::CSS_EMS:
case CSSPrimitiveValue::CSS_EXS:
case CSSPrimitiveValue::CSS_GRAD:
case CSSPrimitiveValue::CSS_HZ:
case CSSPrimitiveValue::CSS_IN:
case CSSPrimitiveValue::CSS_KHZ:
case CSSPrimitiveValue::CSS_MM:
case CSSPrimitiveValue::CSS_MS:
case CSSPrimitiveValue::CSS_NUMBER:
case CSSPrimitiveValue::CSS_PERCENTAGE:
case CSSPrimitiveValue::CSS_PC:
case CSSPrimitiveValue::CSS_PT:
case CSSPrimitiveValue::CSS_PX:
case CSSPrimitiveValue::CSS_RAD:
case CSSPrimitiveValue::CSS_REMS:
case CSSPrimitiveValue::CSS_CHS:
case CSSPrimitiveValue::CSS_S:
case CSSPrimitiveValue::CSS_TURN:
case CSSPrimitiveValue::CSS_VW:
case CSSPrimitiveValue::CSS_VH:
case CSSPrimitiveValue::CSS_VMIN:
case CSSPrimitiveValue::CSS_VMAX:
case CSSPrimitiveValue::CSS_FR:
return true;
case CSSPrimitiveValue::CSS_ATTR:
case CSSPrimitiveValue::CSS_COUNTER:
case CSSPrimitiveValue::CSS_COUNTER_NAME:
case CSSPrimitiveValue::CSS_IDENT:
case CSSPrimitiveValue::CSS_PROPERTY_ID:
case CSSPrimitiveValue::CSS_VALUE_ID:
case CSSPrimitiveValue::CSS_PAIR:
case CSSPrimitiveValue::CSS_PARSER_HEXCOLOR:
case CSSPrimitiveValue::CSS_RECT:
case CSSPrimitiveValue::CSS_QUAD:
case CSSPrimitiveValue::CSS_RGBCOLOR:
case CSSPrimitiveValue::CSS_SHAPE:
case CSSPrimitiveValue::CSS_STRING:
case CSSPrimitiveValue::CSS_UNICODE_RANGE:
case CSSPrimitiveValue::CSS_UNKNOWN:
case CSSPrimitiveValue::CSS_URI:
return false;
}
ASSERT_NOT_REACHED();
return false;
}
typedef HashMap<String, CSSPrimitiveValue::UnitType> StringToUnitTable;
StringToUnitTable createStringToUnitTable()
{
StringToUnitTable table;
table.set(String("em"), CSSPrimitiveValue::CSS_EMS);
table.set(String("ex"), CSSPrimitiveValue::CSS_EXS);
table.set(String("px"), CSSPrimitiveValue::CSS_PX);
table.set(String("cm"), CSSPrimitiveValue::CSS_CM);
table.set(String("mm"), CSSPrimitiveValue::CSS_MM);
table.set(String("in"), CSSPrimitiveValue::CSS_IN);
table.set(String("pt"), CSSPrimitiveValue::CSS_PT);
table.set(String("pc"), CSSPrimitiveValue::CSS_PC);
table.set(String("deg"), CSSPrimitiveValue::CSS_DEG);
table.set(String("rad"), CSSPrimitiveValue::CSS_RAD);
table.set(String("grad"), CSSPrimitiveValue::CSS_GRAD);
table.set(String("ms"), CSSPrimitiveValue::CSS_MS);
table.set(String("s"), CSSPrimitiveValue::CSS_S);
table.set(String("hz"), CSSPrimitiveValue::CSS_HZ);
table.set(String("khz"), CSSPrimitiveValue::CSS_KHZ);
table.set(String("dpi"), CSSPrimitiveValue::CSS_DPI);
table.set(String("dpcm"), CSSPrimitiveValue::CSS_DPCM);
table.set(String("dppx"), CSSPrimitiveValue::CSS_DPPX);
table.set(String("vw"), CSSPrimitiveValue::CSS_VW);
table.set(String("vh"), CSSPrimitiveValue::CSS_VH);
table.set(String("vmin"), CSSPrimitiveValue::CSS_VMIN);
table.set(String("vmax"), CSSPrimitiveValue::CSS_VMAX);
table.set(String("rem"), CSSPrimitiveValue::CSS_REMS);
table.set(String("fr"), CSSPrimitiveValue::CSS_FR);
table.set(String("turn"), CSSPrimitiveValue::CSS_TURN);
table.set(String("ch"), CSSPrimitiveValue::CSS_CHS);
return table;
}
CSSPrimitiveValue::UnitType CSSPrimitiveValue::fromName(const String& unit)
{
DEFINE_STATIC_LOCAL(StringToUnitTable, unitTable, (createStringToUnitTable()));
return unitTable.get(unit.lower());
}
CSSPrimitiveValue::UnitCategory CSSPrimitiveValue::unitCategory(UnitType type)
{
// Here we violate the spec (http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSPrimitiveValue) and allow conversions
// between CSS_PX and relative lengths (see cssPixelsPerInch comment in core/css/CSSHelper.h for the topic treatment).
switch (type) {
case CSS_NUMBER:
return CSSPrimitiveValue::UNumber;
case CSS_PERCENTAGE:
return CSSPrimitiveValue::UPercent;
case CSS_PX:
case CSS_CM:
case CSS_MM:
case CSS_IN:
case CSS_PT:
case CSS_PC:
return CSSPrimitiveValue::ULength;
case CSS_MS:
case CSS_S:
return CSSPrimitiveValue::UTime;
case CSS_DEG:
case CSS_RAD:
case CSS_GRAD:
case CSS_TURN:
return CSSPrimitiveValue::UAngle;
case CSS_HZ:
case CSS_KHZ:
return CSSPrimitiveValue::UFrequency;
case CSS_DPPX:
case CSS_DPI:
case CSS_DPCM:
return CSSPrimitiveValue::UResolution;
default:
return CSSPrimitiveValue::UOther;
}
}
bool CSSPrimitiveValue::colorIsDerivedFromElement() const
{
int valueID = getValueID();
switch (valueID) {
case CSSValueWebkitText:
case CSSValueWebkitLink:
case CSSValueWebkitActivelink:
case CSSValueCurrentcolor:
return true;
default:
return false;
}
}
typedef HashMap<const CSSPrimitiveValue*, String> CSSTextCache;
static CSSTextCache& cssTextCache()
{
DEFINE_STATIC_LOCAL(CSSTextCache, cache, ());
return cache;
}
CSSPrimitiveValue::UnitType CSSPrimitiveValue::primitiveType() const
{
if (m_primitiveUnitType == CSS_PROPERTY_ID || m_primitiveUnitType == CSS_VALUE_ID)
return CSS_IDENT;
if (m_primitiveUnitType != CSS_CALC)
return static_cast<UnitType>(m_primitiveUnitType);
switch (m_value.calc->category()) {
case CalcAngle:
return CSS_DEG;
case CalcFrequency:
return CSS_HZ;
case CalcNumber:
return CSS_NUMBER;
case CalcPercent:
return CSS_PERCENTAGE;
case CalcLength:
return CSS_PX;
case CalcPercentNumber:
return CSS_CALC_PERCENTAGE_WITH_NUMBER;
case CalcPercentLength:
return CSS_CALC_PERCENTAGE_WITH_LENGTH;
case CalcTime:
return CSS_MS;
case CalcOther:
return CSS_UNKNOWN;
}
return CSS_UNKNOWN;
}
static const AtomicString& propertyName(CSSPropertyID propertyID)
{
ASSERT_ARG(propertyID, propertyID >= 0);
ASSERT_ARG(propertyID, (propertyID >= firstCSSProperty && propertyID < firstCSSProperty + numCSSProperties));
if (propertyID < 0)
return nullAtom;
return getPropertyNameAtomicString(propertyID);
}
static const AtomicString& valueName(CSSValueID valueID)
{
ASSERT_ARG(valueID, valueID >= 0);
ASSERT_ARG(valueID, valueID < numCSSValueKeywords);
if (valueID < 0)
return nullAtom;
static AtomicString* keywordStrings = new AtomicString[numCSSValueKeywords]; // Leaked intentionally.
AtomicString& keywordString = keywordStrings[valueID];
if (keywordString.isNull())
keywordString = getValueName(valueID);
return keywordString;
}
CSSPrimitiveValue::CSSPrimitiveValue(CSSValueID valueID)
: CSSValue(PrimitiveClass)
{
m_primitiveUnitType = CSS_VALUE_ID;
m_value.valueID = valueID;
}
CSSPrimitiveValue::CSSPrimitiveValue(CSSPropertyID propertyID)
: CSSValue(PrimitiveClass)
{
m_primitiveUnitType = CSS_PROPERTY_ID;
m_value.propertyID = propertyID;
}
CSSPrimitiveValue::CSSPrimitiveValue(double num, UnitType type)
: CSSValue(PrimitiveClass)
{
m_primitiveUnitType = type;
ASSERT(std::isfinite(num));
m_value.num = num;
}
CSSPrimitiveValue::CSSPrimitiveValue(const String& str, UnitType type)
: CSSValue(PrimitiveClass)
{
m_primitiveUnitType = type;
m_value.string = str.impl();
if (m_value.string)
m_value.string->ref();
}
CSSPrimitiveValue::CSSPrimitiveValue(const LengthSize& lengthSize, const RenderStyle& style)
: CSSValue(PrimitiveClass)
{
init(lengthSize, style);
}
CSSPrimitiveValue::CSSPrimitiveValue(RGBA32 color, UnitType type)
: CSSValue(PrimitiveClass)
{
ASSERT(type == CSS_RGBCOLOR);
m_primitiveUnitType = CSS_RGBCOLOR;
m_value.rgbcolor = color;
}
CSSPrimitiveValue::CSSPrimitiveValue(const Length& length, float zoom)
: CSSValue(PrimitiveClass)
{
switch (length.type()) {
case Auto:
m_primitiveUnitType = CSS_VALUE_ID;
m_value.valueID = CSSValueAuto;
break;
case Intrinsic:
m_primitiveUnitType = CSS_VALUE_ID;
m_value.valueID = CSSValueIntrinsic;
break;
case MinIntrinsic:
m_primitiveUnitType = CSS_VALUE_ID;
m_value.valueID = CSSValueMinIntrinsic;
break;
case MinContent:
m_primitiveUnitType = CSS_VALUE_ID;
m_value.valueID = CSSValueMinContent;
break;
case MaxContent:
m_primitiveUnitType = CSS_VALUE_ID;
m_value.valueID = CSSValueMaxContent;
break;
case FillAvailable:
m_primitiveUnitType = CSS_VALUE_ID;
m_value.valueID = CSSValueWebkitFillAvailable;
break;
case FitContent:
m_primitiveUnitType = CSS_VALUE_ID;
m_value.valueID = CSSValueWebkitFitContent;
break;
case ExtendToZoom:
m_primitiveUnitType = CSS_VALUE_ID;
m_value.valueID = CSSValueInternalExtendToZoom;
break;
case Percent:
m_primitiveUnitType = CSS_PERCENTAGE;
ASSERT(std::isfinite(length.percent()));
m_value.num = length.percent();
break;
case Fixed:
m_primitiveUnitType = CSS_PX;
m_value.num = length.value() / zoom;
break;
case Calculated: {
const CalculationValue& calc = length.calculationValue();
if (calc.pixels() && calc.percent()) {
init(CSSCalcValue::create(
CSSCalcValue::createExpressionNode(calc.pixels() / zoom, calc.percent()),
calc.isNonNegative() ? ValueRangeNonNegative : ValueRangeAll));
break;
}
if (calc.percent()) {
m_primitiveUnitType = CSS_PERCENTAGE;
m_value.num = calc.percent();
} else {
m_primitiveUnitType = CSS_PX;
m_value.num = calc.pixels() / zoom;
}
if (m_value.num < 0 && calc.isNonNegative())
m_value.num = 0;
break;
}
case DeviceWidth:
case DeviceHeight:
case MaxSizeNone:
ASSERT_NOT_REACHED();
break;
}
}
void CSSPrimitiveValue::init(const LengthSize& lengthSize, const RenderStyle& style)
{
m_primitiveUnitType = CSS_PAIR;
m_hasCachedCSSText = false;
m_value.pair = Pair::create(create(lengthSize.width(), style.effectiveZoom()), create(lengthSize.height(), style.effectiveZoom()), Pair::KeepIdenticalValues).leakRef();
}
void CSSPrimitiveValue::init(PassRefPtrWillBeRawPtr<Counter> c)
{
m_primitiveUnitType = CSS_COUNTER;
m_hasCachedCSSText = false;
m_value.counter = c.leakRef();
}
void CSSPrimitiveValue::init(PassRefPtrWillBeRawPtr<Rect> r)
{
m_primitiveUnitType = CSS_RECT;
m_hasCachedCSSText = false;
m_value.rect = r.leakRef();
}
void CSSPrimitiveValue::init(PassRefPtrWillBeRawPtr<Quad> quad)
{
m_primitiveUnitType = CSS_QUAD;
m_hasCachedCSSText = false;
m_value.quad = quad.leakRef();
}
void CSSPrimitiveValue::init(PassRefPtrWillBeRawPtr<Pair> p)
{
m_primitiveUnitType = CSS_PAIR;
m_hasCachedCSSText = false;
m_value.pair = p.leakRef();
}
void CSSPrimitiveValue::init(PassRefPtrWillBeRawPtr<CSSCalcValue> c)
{
m_primitiveUnitType = CSS_CALC;
m_hasCachedCSSText = false;
m_value.calc = c.leakRef();
}
void CSSPrimitiveValue::init(PassRefPtrWillBeRawPtr<CSSBasicShape> shape)
{
m_primitiveUnitType = CSS_SHAPE;
m_hasCachedCSSText = false;
m_value.shape = shape.leakRef();
}
CSSPrimitiveValue::~CSSPrimitiveValue()
{
cleanup();
}
void CSSPrimitiveValue::cleanup()
{
switch (static_cast<UnitType>(m_primitiveUnitType)) {
case CSS_STRING:
case CSS_URI:
case CSS_ATTR:
case CSS_COUNTER_NAME:
case CSS_PARSER_HEXCOLOR:
if (m_value.string)
m_value.string->deref();
break;
case CSS_COUNTER:
// We must not call deref() when oilpan is enabled because m_value.counter is traced.
#if !ENABLE(OILPAN)
m_value.counter->deref();
#endif
break;
case CSS_RECT:
// We must not call deref() when oilpan is enabled because m_value.rect is traced.
#if !ENABLE(OILPAN)
m_value.rect->deref();
#endif
break;
case CSS_QUAD:
// We must not call deref() when oilpan is enabled because m_value.quad is traced.
#if !ENABLE(OILPAN)
m_value.quad->deref();
#endif
break;
case CSS_PAIR:
// We must not call deref() when oilpan is enabled because m_value.pair is traced.
#if !ENABLE(OILPAN)
m_value.pair->deref();
#endif
break;
case CSS_CALC:
// We must not call deref() when oilpan is enabled because m_value.calc is traced.
#if !ENABLE(OILPAN)
m_value.calc->deref();
#endif
break;
case CSS_CALC_PERCENTAGE_WITH_NUMBER:
case CSS_CALC_PERCENTAGE_WITH_LENGTH:
ASSERT_NOT_REACHED();
break;
case CSS_SHAPE:
// We must not call deref() when oilpan is enabled because m_value.shape is traced.
#if !ENABLE(OILPAN)
m_value.shape->deref();
#endif
break;
case CSS_NUMBER:
case CSS_PERCENTAGE:
case CSS_EMS:
case CSS_EXS:
case CSS_REMS:
case CSS_CHS:
case CSS_PX:
case CSS_CM:
case CSS_MM:
case CSS_IN:
case CSS_PT:
case CSS_PC:
case CSS_DEG:
case CSS_RAD:
case CSS_GRAD:
case CSS_MS:
case CSS_S:
case CSS_HZ:
case CSS_KHZ:
case CSS_TURN:
case CSS_VW:
case CSS_VH:
case CSS_VMIN:
case CSS_VMAX:
case CSS_DPPX:
case CSS_DPI:
case CSS_DPCM:
case CSS_FR:
case CSS_IDENT:
case CSS_RGBCOLOR:
case CSS_DIMENSION:
case CSS_UNKNOWN:
case CSS_UNICODE_RANGE:
case CSS_PROPERTY_ID:
case CSS_VALUE_ID:
break;
}
m_primitiveUnitType = 0;
if (m_hasCachedCSSText) {
cssTextCache().remove(this);
m_hasCachedCSSText = false;
}
}
double CSSPrimitiveValue::computeSeconds()
{
ASSERT(isTime() || (isCalculated() && cssCalcValue()->category() == CalcTime));
UnitType currentType = isCalculated() ? cssCalcValue()->expressionNode()->primitiveType() : static_cast<UnitType>(m_primitiveUnitType);
if (currentType == CSS_S)
return getDoubleValue();
if (currentType == CSS_MS)
return getDoubleValue() / 1000;
ASSERT_NOT_REACHED();
return 0;
}
double CSSPrimitiveValue::computeDegrees() const
{
ASSERT(isAngle() || (isCalculated() && cssCalcValue()->category() == CalcAngle));
UnitType currentType = isCalculated() ? cssCalcValue()->expressionNode()->primitiveType() : static_cast<UnitType>(m_primitiveUnitType);
switch (currentType) {
case CSS_DEG:
return getDoubleValue();
case CSS_RAD:
return rad2deg(getDoubleValue());
case CSS_GRAD:
return grad2deg(getDoubleValue());
case CSS_TURN:
return turn2deg(getDoubleValue());
default:
ASSERT_NOT_REACHED();
return 0;
}
}
template<> int CSSPrimitiveValue::computeLength(const CSSToLengthConversionData& conversionData)
{
return roundForImpreciseConversion<int>(computeLengthDouble(conversionData));
}
template<> unsigned CSSPrimitiveValue::computeLength(const CSSToLengthConversionData& conversionData)
{
return roundForImpreciseConversion<unsigned>(computeLengthDouble(conversionData));
}
template<> Length CSSPrimitiveValue::computeLength(const CSSToLengthConversionData& conversionData)
{
return Length(clampTo<float>(computeLengthDouble(conversionData), minValueForCssLength, maxValueForCssLength), Fixed);
}
template<> short CSSPrimitiveValue::computeLength(const CSSToLengthConversionData& conversionData)
{
return roundForImpreciseConversion<short>(computeLengthDouble(conversionData));
}
template<> unsigned short CSSPrimitiveValue::computeLength(const CSSToLengthConversionData& conversionData)
{
return roundForImpreciseConversion<unsigned short>(computeLengthDouble(conversionData));
}
template<> float CSSPrimitiveValue::computeLength(const CSSToLengthConversionData& conversionData)
{
return static_cast<float>(computeLengthDouble(conversionData));
}
template<> double CSSPrimitiveValue::computeLength(const CSSToLengthConversionData& conversionData)
{
return computeLengthDouble(conversionData);
}
double CSSPrimitiveValue::computeLengthDouble(const CSSToLengthConversionData& conversionData)
{
// The logic in this function is duplicated in MediaValues::computeLength
// because MediaValues::computeLength needs nearly identical logic, but we haven't found a way to make
// CSSPrimitiveValue::computeLengthDouble more generic (to solve both cases) without hurting performance.
if (m_primitiveUnitType == CSS_CALC)
return m_value.calc->computeLengthPx(conversionData);
double factor;
switch (primitiveType()) {
case CSS_EMS:
factor = conversionData.emFontSize();
break;
case CSS_EXS:
factor = conversionData.exFontSize();
break;
case CSS_REMS:
factor = conversionData.remFontSize();
break;
case CSS_CHS:
factor = conversionData.chFontSize();
break;
case CSS_PX:
factor = 1.0;
break;
case CSS_CM:
factor = cssPixelsPerCentimeter;
break;
case CSS_MM:
factor = cssPixelsPerMillimeter;
break;
case CSS_IN:
factor = cssPixelsPerInch;
break;
case CSS_PT:
factor = cssPixelsPerPoint;
break;
case CSS_PC:
factor = cssPixelsPerPica;
break;
case CSS_VW:
factor = conversionData.viewportWidthPercent();
break;
case CSS_VH:
factor = conversionData.viewportHeightPercent();
break;
case CSS_VMIN:
factor = conversionData.viewportMinPercent();
break;
case CSS_VMAX:
factor = conversionData.viewportMaxPercent();
break;
case CSS_CALC_PERCENTAGE_WITH_LENGTH:
case CSS_CALC_PERCENTAGE_WITH_NUMBER:
ASSERT_NOT_REACHED();
return -1.0;
default:
ASSERT_NOT_REACHED();
return -1.0;
}
// We do not apply the zoom factor when we are computing the value of the font-size property. The zooming
// for font sizes is much more complicated, since we have to worry about enforcing the minimum font size preference
// as well as enforcing the implicit "smart minimum."
double result = getDoubleValue() * factor;
if (isFontRelativeLength())
return result;
return result * conversionData.zoom();
}
void CSSPrimitiveValue::accumulateLengthArray(CSSLengthArray& lengthArray, CSSLengthTypeArray& lengthTypeArray, double multiplier) const
{
ASSERT(lengthArray.size() == LengthUnitTypeCount);
if (m_primitiveUnitType == CSS_CALC) {
cssCalcValue()->accumulateLengthArray(lengthArray, lengthTypeArray, multiplier);
return;
}
LengthUnitType lengthType;
if (unitTypeToLengthUnitType(static_cast<UnitType>(m_primitiveUnitType), lengthType)) {
lengthArray.at(lengthType) += m_value.num * conversionToCanonicalUnitsScaleFactor(static_cast<UnitType>(m_primitiveUnitType)) * multiplier;
lengthTypeArray.set(lengthType);
}
}
void CSSPrimitiveValue::accumulateLengthArray(CSSLengthArray& lengthArray, double multiplier) const
{
CSSLengthTypeArray lengthTypeArray;
lengthTypeArray.resize(CSSPrimitiveValue::LengthUnitTypeCount);
for (size_t i = 0; i < CSSPrimitiveValue::LengthUnitTypeCount; ++i)
lengthTypeArray.clear(i);
return CSSPrimitiveValue::accumulateLengthArray(lengthArray, lengthTypeArray, multiplier);
}
double CSSPrimitiveValue::conversionToCanonicalUnitsScaleFactor(UnitType unitType)
{
double factor = 1.0;
// FIXME: the switch can be replaced by an array of scale factors.
switch (unitType) {
// These are "canonical" units in their respective categories.
case CSS_PX:
case CSS_DEG:
case CSS_MS:
case CSS_HZ:
break;
case CSS_CM:
factor = cssPixelsPerCentimeter;
break;
case CSS_DPCM:
factor = 1 / cssPixelsPerCentimeter;
break;
case CSS_MM:
factor = cssPixelsPerMillimeter;
break;
case CSS_IN:
factor = cssPixelsPerInch;
break;
case CSS_DPI:
factor = 1 / cssPixelsPerInch;
break;
case CSS_PT:
factor = cssPixelsPerPoint;
break;
case CSS_PC:
factor = cssPixelsPerPica;
break;
case CSS_RAD:
factor = 180 / piDouble;
break;
case CSS_GRAD:
factor = 0.9;
break;
case CSS_TURN:
factor = 360;
break;
case CSS_S:
case CSS_KHZ:
factor = 1000;
break;
default:
break;
}
return factor;
}
double CSSPrimitiveValue::getDoubleValue(UnitType unitType) const
{
double result = 0;
getDoubleValueInternal(unitType, &result);
return result;
}
double CSSPrimitiveValue::getDoubleValue() const
{
return m_primitiveUnitType != CSS_CALC ? m_value.num : m_value.calc->doubleValue();
}
CSSPrimitiveValue::UnitType CSSPrimitiveValue::canonicalUnitTypeForCategory(UnitCategory category)
{
// The canonical unit type is chosen according to the way BisonCSSParser::validUnit() chooses the default unit
// in each category (based on unitflags).
switch (category) {
case UNumber:
return CSS_NUMBER;
case ULength:
return CSS_PX;
case UPercent:
return CSS_UNKNOWN; // Cannot convert between numbers and percent.
case UTime:
return CSS_MS;
case UAngle:
return CSS_DEG;
case UFrequency:
return CSS_HZ;
case UResolution:
return CSS_DPPX;
default:
return CSS_UNKNOWN;
}
}
bool CSSPrimitiveValue::getDoubleValueInternal(UnitType requestedUnitType, double* result) const
{
if (!isValidCSSUnitTypeForDoubleConversion(static_cast<UnitType>(m_primitiveUnitType)) || !isValidCSSUnitTypeForDoubleConversion(requestedUnitType))
return false;
UnitType sourceUnitType = primitiveType();
if (requestedUnitType == sourceUnitType || requestedUnitType == CSS_DIMENSION) {
*result = getDoubleValue();
return true;
}
UnitCategory sourceCategory = unitCategory(sourceUnitType);
ASSERT(sourceCategory != UOther);
UnitType targetUnitType = requestedUnitType;
UnitCategory targetCategory = unitCategory(targetUnitType);
ASSERT(targetCategory != UOther);
// Cannot convert between unrelated unit categories if one of them is not UNumber.
if (sourceCategory != targetCategory && sourceCategory != UNumber && targetCategory != UNumber)
return false;
if (targetCategory == UNumber) {
// We interpret conversion to CSS_NUMBER as conversion to a canonical unit in this value's category.
targetUnitType = canonicalUnitTypeForCategory(sourceCategory);
if (targetUnitType == CSS_UNKNOWN)
return false;
}
if (sourceUnitType == CSS_NUMBER) {
// We interpret conversion from CSS_NUMBER in the same way as BisonCSSParser::validUnit() while using non-strict mode.
sourceUnitType = canonicalUnitTypeForCategory(targetCategory);
if (sourceUnitType == CSS_UNKNOWN)
return false;
}
double convertedValue = getDoubleValue();
// First convert the value from m_primitiveUnitType to canonical type.
double factor = conversionToCanonicalUnitsScaleFactor(sourceUnitType);
convertedValue *= factor;
// Now convert from canonical type to the target unitType.
factor = conversionToCanonicalUnitsScaleFactor(targetUnitType);
convertedValue /= factor;
*result = convertedValue;
return true;
}
bool CSSPrimitiveValue::unitTypeToLengthUnitType(UnitType unitType, LengthUnitType& lengthType)
{
switch (unitType) {
case CSSPrimitiveValue::CSS_PX:
case CSSPrimitiveValue::CSS_CM:
case CSSPrimitiveValue::CSS_MM:
case CSSPrimitiveValue::CSS_IN:
case CSSPrimitiveValue::CSS_PT:
case CSSPrimitiveValue::CSS_PC:
lengthType = UnitTypePixels;
return true;
case CSSPrimitiveValue::CSS_EMS:
lengthType = UnitTypeFontSize;
return true;
case CSSPrimitiveValue::CSS_EXS:
lengthType = UnitTypeFontXSize;
return true;
case CSSPrimitiveValue::CSS_REMS:
lengthType = UnitTypeRootFontSize;
return true;
case CSSPrimitiveValue::CSS_CHS:
lengthType = UnitTypeZeroCharacterWidth;
return true;
case CSSPrimitiveValue::CSS_PERCENTAGE:
lengthType = UnitTypePercentage;
return true;
case CSSPrimitiveValue::CSS_VW:
lengthType = UnitTypeViewportWidth;
return true;
case CSSPrimitiveValue::CSS_VH:
lengthType = UnitTypeViewportHeight;
return true;
case CSSPrimitiveValue::CSS_VMIN:
lengthType = UnitTypeViewportMin;
return true;
case CSSPrimitiveValue::CSS_VMAX:
lengthType = UnitTypeViewportMax;
return true;
default:
return false;
}
}
CSSPrimitiveValue::UnitType CSSPrimitiveValue::lengthUnitTypeToUnitType(LengthUnitType type)
{
switch (type) {
case UnitTypePixels:
return CSSPrimitiveValue::CSS_PX;
case UnitTypeFontSize:
return CSSPrimitiveValue::CSS_EMS;
case UnitTypeFontXSize:
return CSSPrimitiveValue::CSS_EXS;
case UnitTypeRootFontSize:
return CSSPrimitiveValue::CSS_REMS;
case UnitTypeZeroCharacterWidth:
return CSSPrimitiveValue::CSS_CHS;
case UnitTypePercentage:
return CSSPrimitiveValue::CSS_PERCENTAGE;
case UnitTypeViewportWidth:
return CSSPrimitiveValue::CSS_VW;
case UnitTypeViewportHeight:
return CSSPrimitiveValue::CSS_VH;
case UnitTypeViewportMin:
return CSSPrimitiveValue::CSS_VMIN;
case UnitTypeViewportMax:
return CSSPrimitiveValue::CSS_VMAX;
case LengthUnitTypeCount:
break;
}
ASSERT_NOT_REACHED();
return CSSPrimitiveValue::CSS_UNKNOWN;
}
String CSSPrimitiveValue::getStringValue() const
{
switch (m_primitiveUnitType) {
case CSS_STRING:
case CSS_ATTR:
case CSS_URI:
return m_value.string;
case CSS_VALUE_ID:
return valueName(m_value.valueID);
case CSS_PROPERTY_ID:
return propertyName(m_value.propertyID);
default:
break;
}
return String();
}
static String formatNumber(double number, const char* suffix, unsigned suffixLength)
{
Decimal decimal = Decimal::fromDouble(number);
String result = decimal.toString();
result.append(suffix, suffixLength);
return result;
}
template <unsigned characterCount>
ALWAYS_INLINE static String formatNumber(double number, const char (&characters)[characterCount])
{
return formatNumber(number, characters, characterCount - 1);
}
static String formatNumber(double number, const char* characters)
{
return formatNumber(number, characters, strlen(characters));
}
const char* CSSPrimitiveValue::unitTypeToString(UnitType type)
{
switch (type) {
case CSS_NUMBER:
return "";
case CSS_PERCENTAGE:
return "%";
case CSS_EMS:
return "em";
case CSS_EXS:
return "ex";
case CSS_REMS:
return "rem";
case CSS_CHS:
return "ch";
case CSS_PX:
return "px";
case CSS_CM:
return "cm";
case CSS_DPPX:
return "dppx";
case CSS_DPI:
return "dpi";
case CSS_DPCM:
return "dpcm";
case CSS_MM:
return "mm";
case CSS_IN:
return "in";
case CSS_PT:
return "pt";
case CSS_PC:
return "pc";
case CSS_DEG:
return "deg";
case CSS_RAD:
return "rad";
case CSS_GRAD:
return "grad";
case CSS_MS:
return "ms";
case CSS_S:
return "s";
case CSS_HZ:
return "hz";
case CSS_KHZ:
return "khz";
case CSS_TURN:
return "turn";
case CSS_FR:
return "fr";
case CSS_VW:
return "vw";
case CSS_VH:
return "vh";
case CSS_VMIN:
return "vmin";
case CSS_VMAX:
return "vmax";
case CSS_UNKNOWN:
case CSS_DIMENSION:
case CSS_STRING:
case CSS_URI:
case CSS_VALUE_ID:
case CSS_PROPERTY_ID:
case CSS_ATTR:
case CSS_COUNTER_NAME:
case CSS_COUNTER:
case CSS_RECT:
case CSS_QUAD:
case CSS_RGBCOLOR:
case CSS_PARSER_HEXCOLOR:
case CSS_PAIR:
case CSS_CALC:
case CSS_SHAPE:
case CSS_IDENT:
case CSS_UNICODE_RANGE:
case CSS_CALC_PERCENTAGE_WITH_NUMBER:
case CSS_CALC_PERCENTAGE_WITH_LENGTH:
break;
};
ASSERT_NOT_REACHED();
return "";
}
String CSSPrimitiveValue::customCSSText(CSSTextFormattingFlags formattingFlag) const
{
if (m_hasCachedCSSText) {
ASSERT(cssTextCache().contains(this));
return cssTextCache().get(this);
}
String text;
switch (m_primitiveUnitType) {
case CSS_UNKNOWN:
// FIXME
break;
case CSS_NUMBER:
case CSS_PERCENTAGE:
case CSS_EMS:
case CSS_EXS:
case CSS_REMS:
case CSS_CHS:
case CSS_PX:
case CSS_CM:
case CSS_DPPX:
case CSS_DPI:
case CSS_DPCM:
case CSS_MM:
case CSS_IN:
case CSS_PT:
case CSS_PC:
case CSS_DEG:
case CSS_RAD:
case CSS_GRAD:
case CSS_MS:
case CSS_S:
case CSS_HZ:
case CSS_KHZ:
case CSS_TURN:
case CSS_FR:
case CSS_VW:
case CSS_VH:
case CSS_VMIN:
case CSS_VMAX:
text = formatNumber(m_value.num, unitTypeToString((UnitType)m_primitiveUnitType));
case CSS_DIMENSION:
// FIXME: We currently don't handle CSS_DIMENSION properly as we don't store
// the actual dimension, just the numeric value as a string.
break;
case CSS_STRING:
text = formattingFlag == AlwaysQuoteCSSString ? quoteCSSString(m_value.string) : quoteCSSStringIfNeeded(m_value.string);
break;
case CSS_URI:
text = "url(" + quoteCSSURLIfNeeded(m_value.string) + ")";
break;
case CSS_VALUE_ID:
text = valueName(m_value.valueID);
break;
case CSS_PROPERTY_ID:
text = propertyName(m_value.propertyID);
break;
case CSS_ATTR: {
StringBuilder result;
result.reserveCapacity(6 + m_value.string->length());
result.appendLiteral("attr(");
result.append(m_value.string);
result.append(')');
text = result.toString();
break;
}
case CSS_COUNTER_NAME:
text = "counter(" + String(m_value.string) + ')';
break;
case CSS_COUNTER: {
StringBuilder result;
String separator = m_value.counter->separator();
if (separator.isEmpty())
result.appendLiteral("counter(");
else
result.appendLiteral("counters(");
result.append(m_value.counter->identifier());
if (!separator.isEmpty()) {
result.appendLiteral(", ");
result.append(quoteCSSStringIfNeeded(separator));
}
String listStyle = m_value.counter->listStyle();
if (!listStyle.isEmpty()) {
result.appendLiteral(", ");
result.append(listStyle);
}
result.append(')');
text = result.toString();
break;
}
case CSS_RECT:
text = getRectValue()->cssText();
break;
case CSS_QUAD:
text = getQuadValue()->cssText();
break;
case CSS_RGBCOLOR:
case CSS_PARSER_HEXCOLOR: {
RGBA32 rgbColor = m_value.rgbcolor;
if (m_primitiveUnitType == CSS_PARSER_HEXCOLOR)
Color::parseHexColor(m_value.string, rgbColor);
Color color(rgbColor);
text = color.serializedAsCSSComponentValue();
break;
}
case CSS_PAIR:
text = getPairValue()->cssText();
break;
case CSS_CALC:
text = m_value.calc->cssText();
break;
case CSS_SHAPE:
text = m_value.shape->cssText();
break;
}
ASSERT(!cssTextCache().contains(this));
cssTextCache().set(this, text);
m_hasCachedCSSText = true;
return text;
}
bool CSSPrimitiveValue::equals(const CSSPrimitiveValue& other) const
{
if (m_primitiveUnitType != other.m_primitiveUnitType)
return false;
switch (m_primitiveUnitType) {
case CSS_UNKNOWN:
return false;
case CSS_NUMBER:
case CSS_PERCENTAGE:
case CSS_EMS:
case CSS_EXS:
case CSS_REMS:
case CSS_PX:
case CSS_CM:
case CSS_DPPX:
case CSS_DPI:
case CSS_DPCM:
case CSS_MM:
case CSS_IN:
case CSS_PT:
case CSS_PC:
case CSS_DEG:
case CSS_RAD:
case CSS_GRAD:
case CSS_MS:
case CSS_S:
case CSS_HZ:
case CSS_KHZ:
case CSS_TURN:
case CSS_VW:
case CSS_VH:
case CSS_VMIN:
case CSS_VMAX:
case CSS_DIMENSION:
case CSS_FR:
return m_value.num == other.m_value.num;
case CSS_PROPERTY_ID:
return propertyName(m_value.propertyID) == propertyName(other.m_value.propertyID);
case CSS_VALUE_ID:
return valueName(m_value.valueID) == valueName(other.m_value.valueID);
case CSS_STRING:
case CSS_URI:
case CSS_ATTR:
case CSS_COUNTER_NAME:
case CSS_PARSER_HEXCOLOR:
return equal(m_value.string, other.m_value.string);
case CSS_COUNTER:
return m_value.counter && other.m_value.counter && m_value.counter->equals(*other.m_value.counter);
case CSS_RECT:
return m_value.rect && other.m_value.rect && m_value.rect->equals(*other.m_value.rect);
case CSS_QUAD:
return m_value.quad && other.m_value.quad && m_value.quad->equals(*other.m_value.quad);
case CSS_RGBCOLOR:
return m_value.rgbcolor == other.m_value.rgbcolor;
case CSS_PAIR:
return m_value.pair && other.m_value.pair && m_value.pair->equals(*other.m_value.pair);
case CSS_CALC:
return m_value.calc && other.m_value.calc && m_value.calc->equals(*other.m_value.calc);
case CSS_SHAPE:
return m_value.shape && other.m_value.shape && m_value.shape->equals(*other.m_value.shape);
}
return false;
}
void CSSPrimitiveValue::traceAfterDispatch(Visitor* visitor)
{
#if ENABLE(OILPAN)
switch (m_primitiveUnitType) {
case CSS_COUNTER:
visitor->trace(m_value.counter);
break;
case CSS_RECT:
visitor->trace(m_value.rect);
break;
case CSS_QUAD:
visitor->trace(m_value.quad);
break;
case CSS_PAIR:
visitor->trace(m_value.pair);
break;
case CSS_CALC:
visitor->trace(m_value.calc);
break;
case CSS_SHAPE:
visitor->trace(m_value.shape);
break;
default:
break;
}
#endif
CSSValue::traceAfterDispatch(visitor);
}
} // namespace blink
|