Title: [116540] trunk/Source/WebCore
Revision
116540
Author
[email protected]
Date
2012-05-09 10:06:19 -0700 (Wed, 09 May 2012)

Log Message

Simplify CSSParser::parseSimpleLengthValue()
https://bugs.webkit.org/show_bug.cgi?id=85910

Reviewed by Alexis Menard.

Various small improvements to this function, mainly:
- Move the check if the property ID accepts a simple length as early as possible;
- Remove the check for the characters{8,16} pointers since they'll be valid (we ASSERT that);
- Use a template to avoid duplicate code for 8 and 16 bit characters.

* css/CSSParser.cpp:
(WebCore):
(WebCore::parseSimpleLength):
(WebCore::parseSimpleLengthValue):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (116539 => 116540)


--- trunk/Source/WebCore/ChangeLog	2012-05-09 17:02:50 UTC (rev 116539)
+++ trunk/Source/WebCore/ChangeLog	2012-05-09 17:06:19 UTC (rev 116540)
@@ -1,3 +1,20 @@
+2012-05-09  Caio Marcelo de Oliveira Filho  <[email protected]>
+
+        Simplify CSSParser::parseSimpleLengthValue()
+        https://bugs.webkit.org/show_bug.cgi?id=85910
+
+        Reviewed by Alexis Menard.
+
+        Various small improvements to this function, mainly:
+        - Move the check if the property ID accepts a simple length as early as possible;
+        - Remove the check for the characters{8,16} pointers since they'll be valid (we ASSERT that);
+        - Use a template to avoid duplicate code for 8 and 16 bit characters.
+
+        * css/CSSParser.cpp:
+        (WebCore):
+        (WebCore::parseSimpleLength):
+        (WebCore::parseSimpleLengthValue):
+
 2012-05-09  Ami Fischman  <[email protected]>
 
         [chromium] Support multiple buffered time ranges

Modified: trunk/Source/WebCore/css/CSSParser.cpp (116539 => 116540)


--- trunk/Source/WebCore/css/CSSParser.cpp	2012-05-09 17:02:50 UTC (rev 116539)
+++ trunk/Source/WebCore/css/CSSParser.cpp	2012-05-09 17:06:19 UTC (rev 116540)
@@ -439,63 +439,46 @@
     }
 }
 
+template <typename CharType>
+static inline bool parseSimpleLength(const CharType* characters, unsigned& length, CSSPrimitiveValue::UnitTypes& unit, double& number)
+{
+    if (length > 2 && (characters[length - 2] | 0x20) == 'p' && (characters[length - 1] | 0x20) == 'x') {
+        length -= 2;
+        unit = CSSPrimitiveValue::CSS_PX;
+    } else if (length > 1 && characters[length - 1] == '%') {
+        length -= 1;
+        unit = CSSPrimitiveValue::CSS_PERCENTAGE;
+    }
+
+    // We rely on charactersToDouble for validation as well. The function
+    // will set "ok" to "false" if the entire passed-in character range does
+    // not represent a double.
+    bool ok;
+    number = charactersToDouble(characters, length, &ok);
+    return ok;
+}
+
 static bool parseSimpleLengthValue(StylePropertySet* declaration, CSSPropertyID propertyId, const String& string, bool important, CSSParserMode cssParserMode)
 {
     ASSERT(!string.isEmpty());
     bool acceptsNegativeNumbers;
-    bool strict = isStrictParserMode(cssParserMode);
-    unsigned length = string.length();
+    if (!isSimpleLengthPropertyID(propertyId, acceptsNegativeNumbers))
+        return false;
 
+    unsigned length = string.length();
     double number;
-    bool ok;
     CSSPrimitiveValue::UnitTypes unit = CSSPrimitiveValue::CSS_NUMBER;
 
     if (string.is8Bit()) {
-        const LChar* characters8 = string.characters8();
-        if (!characters8)
+        if (!parseSimpleLength(string.characters8(), length, unit, number))
             return false;
-
-        if (!isSimpleLengthPropertyID(propertyId, acceptsNegativeNumbers))
-            return false;
-
-        if (length > 2 && (characters8[length - 2] | 0x20) == 'p' && (characters8[length - 1] | 0x20) == 'x') {
-            length -= 2;
-            unit = CSSPrimitiveValue::CSS_PX;
-        } else if (length > 1 && characters8[length - 1] == '%') {
-            length -= 1;
-            unit = CSSPrimitiveValue::CSS_PERCENTAGE;
-        }
-
-        // We rely on charactersToDouble for validation as well. The function
-        // will set "ok" to "false" if the entire passed-in character range does
-        // not represent a double.
-        number = charactersToDouble(characters8, length, &ok);
     } else {
-        const UChar* characters16 = string.characters16();
-        if (!characters16)
+        if (!parseSimpleLength(string.characters16(), length, unit, number))
             return false;
-
-        if (!isSimpleLengthPropertyID(propertyId, acceptsNegativeNumbers))
-            return false;
-
-        if (length > 2 && (characters16[length - 2] | 0x20) == 'p' && (characters16[length - 1] | 0x20) == 'x') {
-            length -= 2;
-            unit = CSSPrimitiveValue::CSS_PX;
-        } else if (length > 1 && characters16[length - 1] == '%') {
-            length -= 1;
-            unit = CSSPrimitiveValue::CSS_PERCENTAGE;
-        }
-
-        // We rely on charactersToDouble for validation as well. The function
-        // will set "ok" to "false" if the entire passed-in character range does
-        // not represent a double.
-        number = charactersToDouble(characters16, length, &ok);
     }
 
-    if (!ok)
-        return false;
     if (unit == CSSPrimitiveValue::CSS_NUMBER) {
-        if (number && strict)
+        if (number && isStrictParserMode(cssParserMode))
             return false;
         unit = CSSPrimitiveValue::CSS_PX;
     }
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to