Properly determine if css custom property values are computationally independent
https://bugs.webkit.org/show_bug.cgi?id=190303

Patch by Justin Michaud <justin_michaud@apple.com> on 2018-10-06
Reviewed by Antti Koivisto.

LayoutTests/imported/w3c:

* web-platform-tests/css/css-properties-values-api/register-property-syntax-parsing-expected.txt:
* web-platform-tests/css/css-properties-values-api/registered-property-computation-expected.txt:

Source/WebCore:

Add getDirectComputationalDependencies method to determine if a value is computationally
dependent. Use this method in CSS.registerProperty to replace existing substring checks.
No new tests are needed because the existing tests cover this behaviour.

* css/CSSCalculationValue.cpp:
(WebCore::determineCategory):
* css/CSSCalculationValue.h:
(WebCore::CSSCalcValue::getDirectComputationalDependencies const):
(WebCore::CSSCalcValue::getDirectRootComputationalDependencies const):
* css/CSSCustomPropertyValue.cpp:
(WebCore::CSSCustomPropertyValue::customCSSText const):
(WebCore::CSSCustomPropertyValue::tokens const):
(WebCore::CSSCustomPropertyValue::setResolvedTypedValue):
* css/CSSCustomPropertyValue.h:
* css/CSSPrimitiveValue.cpp:
(WebCore::CSSPrimitiveValue::getDirectComputationalDependencies const):
(WebCore::CSSPrimitiveValue::getDirectRootComputationalDependencies const):
* css/CSSPrimitiveValue.h:
* css/CSSValue.cpp:
(WebCore::CSSValue::getDirectComputationalDependencies const):
(WebCore::CSSValue::getDirectRootComputationalDependencies const):
* css/CSSValue.h:
* css/CSSVariableData.cpp:
(WebCore::CSSVariableData::CSSVariableData):
(WebCore::CSSVariableData::resolveVariableReference const):
* css/DOMCSSRegisterCustomProperty.cpp:
(WebCore::DOMCSSRegisterCustomProperty::registerProperty):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@236895 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/css/CSSCustomPropertyValue.cpp b/Source/WebCore/css/CSSCustomPropertyValue.cpp
index 3551763..7c16e84 100644
--- a/Source/WebCore/css/CSSCustomPropertyValue.cpp
+++ b/Source/WebCore/css/CSSCustomPropertyValue.cpp
@@ -25,10 +25,56 @@
 
 #include "config.h"
 #include "CSSCustomPropertyValue.h"
+#include "CSSTokenizer.h"
 
 
 namespace WebCore {
 
+String CSSCustomPropertyValue::customCSSText() const
+{
+    if (!m_serialized) {
+        m_serialized = true;
+        if (m_resolvedTypedValue) // FIXME: Unit should be based on syntax.
+            m_stringValue = CSSPrimitiveValue::create(m_resolvedTypedValue->value(), CSSPrimitiveValue::CSS_PX)->cssText();
+        else if (m_value)
+            m_stringValue = m_value->tokenRange().serialize();
+        else if (m_valueId != CSSValueInvalid)
+            m_stringValue = getValueName(m_valueId);
+        else
+            m_stringValue = emptyString();
+    }
+    return m_stringValue;
+}
+
+Vector<CSSParserToken> CSSCustomPropertyValue::tokens(const CSSRegisteredCustomPropertySet& registeredProperties, const RenderStyle& style) const
+{
+    if (m_resolvedTypedValue) {
+        Vector<CSSParserToken> result;
+        CSSTokenizer tokenizer(cssText());
+
+        auto tokenizerRange = tokenizer.tokenRange();
+        while (!tokenizerRange.atEnd())
+            result.append(tokenizerRange.consume());
+
+        return result;
+    }
+
+    if (!m_value)
+        return { };
+
+    if (m_containsVariables) {
+        Vector<CSSParserToken> result;
+        // FIXME: Avoid doing this work more than once.
+        RefPtr<CSSVariableData> resolvedData = m_value->resolveVariableReferences(registeredProperties, style);
+        if (resolvedData)
+            result.appendVector(resolvedData->tokens());
+
+        return result;
+    }
+
+    return m_value->tokens();
+}
+
 bool CSSCustomPropertyValue::checkVariablesForCycles(const AtomicString& name, const RenderStyle& style, HashSet<AtomicString>& seenProperties, HashSet<AtomicString>& invalidProperties) const
 {
     ASSERT(containsVariables());
@@ -51,4 +97,10 @@
         resolvedValues.append(CSSCustomPropertyValue::createWithID(m_name, CSSValueInvalid));
 }
 
+void CSSCustomPropertyValue::setResolvedTypedValue(Length length)
+{
+    ASSERT(length.isSpecified());
+    m_resolvedTypedValue = WTFMove(length);
+}
+
 }