Changeset 236379 in webkit


Ignore:
Timestamp:
Sep 21, 2018 6:03:48 PM (6 years ago)
Author:
commit-queue@webkit.org
Message:

Implement initialValue support for CSS Custom Properties and Values API
https://bugs.webkit.org/show_bug.cgi?id=189819

Patch by Justin Michaud <Justin Michaud> on 2018-09-21
Reviewed by Simon Fraser.

Source/WebCore:

  • css/CSSComputedStyleDeclaration.cpp:

(WebCore::ComputedStyleExtractor::customPropertyValue):

  • css/CSSCustomPropertyValue.cpp:

(WebCore::CSSCustomPropertyValue::resolveVariableReferences const):

  • css/CSSCustomPropertyValue.h:
  • css/CSSRegisteredCustomProperty.h:
  • css/CSSVariableData.cpp:

(WebCore::CSSVariableData::resolveVariableFallback const):
(WebCore::CSSVariableData::resolveVariableReference const):
(WebCore::CSSVariableData::resolveVariableReferences const):
(WebCore::CSSVariableData::resolveTokenRange const):

  • css/CSSVariableData.h:
  • css/DOMCSSRegisterCustomProperty.cpp:

(WebCore::DOMCSSRegisterCustomProperty::registerProperty):

  • css/DOMCSSRegisterCustomProperty.h:
  • css/DOMCSSRegisterCustomProperty.idl:
  • css/StyleResolver.cpp:

(WebCore::StyleResolver::resolvedVariableValue):
(WebCore::StyleResolver::applyCascadedProperties):

  • css/parser/CSSParser.cpp:

(WebCore::CSSParser::parseValueWithVariableReferences):

  • css/parser/CSSParser.h:
  • dom/Document.h:

(WebCore::Document::getCSSRegisteredCustomPropertySet const):

  • rendering/style/RenderStyle.cpp:

(WebCore::RenderStyle::checkVariablesInCustomProperties):

  • rendering/style/RenderStyle.h:

LayoutTests:

  • css-custom-properties-api/initialValue-expected.html: Added.
  • css-custom-properties-api/initialValue.html: Added.
  • css-custom-properties-api/initialValueJS-expected.txt: Added.
  • css-custom-properties-api/initialValueJS.html: Added.
  • css-custom-properties-api/registerProperty-expected.txt:
  • css-custom-properties-api/registerProperty.html:
  • platform/win/TestExpectations:
Location:
trunk
Files:
4 added
20 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r236377 r236379  
     12018-09-21  Justin Michaud  <justin_michaud@apple.com>
     2
     3        Implement initialValue support for CSS Custom Properties and Values API
     4        https://bugs.webkit.org/show_bug.cgi?id=189819
     5
     6        Reviewed by Simon Fraser.
     7
     8        * css-custom-properties-api/initialValue-expected.html: Added.
     9        * css-custom-properties-api/initialValue.html: Added.
     10        * css-custom-properties-api/initialValueJS-expected.txt: Added.
     11        * css-custom-properties-api/initialValueJS.html: Added.
     12        * css-custom-properties-api/registerProperty-expected.txt:
     13        * css-custom-properties-api/registerProperty.html:
     14        * platform/win/TestExpectations:
     15
    1162018-09-21  Ryan Haddad  <ryanhaddad@apple.com>
    217
  • trunk/LayoutTests/css-custom-properties-api/registerProperty-expected.txt

    r236273 r236379  
    22PASS registerProperty requires a Dictionary type
    33PASS registerProperty requires a name matching <custom-property-name>
    4 PASS registerProperty only allows omitting initialValue is syntax is '*'
     4PASS registerProperty always allows omitting initialValue and syntax, requires name and inherits
     5PASS registerProperty requires inherits and name
    56
  • trunk/LayoutTests/css-custom-properties-api/registerProperty.html

    r236273 r236379  
    1919    CSS.registerProperty({name: ['--name', 3], inherits: false});
    2020    // Invalid property names
    21     assert_throws(new TypeError(), () => CSS.registerProperty({}));
    22     assert_throws(new TypeError(), () => CSS.registerProperty({name: '--no-inherits'}));
    2321    //assert_throws(new SyntaxError(), () => CSS.registerProperty({name: 'no-leading-dash', inherits: false}));
    2422    //assert_throws(new SyntaxError(), () => CSS.registerProperty({name: '', inherits: false}));
     
    2826    CSS.registerProperty({name: '--syntax-test-1', inherits: false, syntax: '*'});
    2927    CSS.registerProperty({name: '--syntax-test-2', inherits: false, syntax: ' * '});
    30     /*assert_throws(new SyntaxError(),
    31         () => CSS.registerProperty({name: '--syntax-test-3', syntax: 'length'}));*/
    32 }, "registerProperty only allows omitting initialValue is syntax is '*'");
     28    CSS.registerProperty({name: '--syntax-test-3', inherits: false, initialValue: '500px'});
     29    assert_throws(new TypeError(), () => CSS.registerProperty({}));
     30    assert_throws(new TypeError(), () => CSS.registerProperty({name: '--no-inherits'}));
     31    assert_throws(new TypeError(), () => CSS.registerProperty({inherits: false}));
     32    // Repeated name
     33    assert_throws(null,
     34        () => CSS.registerProperty({name: '--syntax-test-3', inherits: false, initialValue: '500px'}));
     35}, "registerProperty always allows omitting initialValue and syntax, requires name and inherits");
     36test(function() {
     37    CSS.registerProperty({name: '--syntax-test-4', inherits: false, syntax: '*'});
     38    CSS.registerProperty({name: '--syntax-test-5', inherits: false, syntax: ' * '});
     39}, "registerProperty requires inherits and name");
    3340</script>
  • trunk/LayoutTests/platform/win/TestExpectations

    r236360 r236379  
    41614161imported/blink/http/tests/security/mixedContent/websocket [ Skip ]
    41624162
     4163# Feature flag only enabled for wk2
     4164css-custom-properties-api [ Skip ]
     4165
    41634166editing/pasteboard/drag-and-drop-color-input-events.html [ Skip ]
    41644167
  • trunk/Source/WebCore/ChangeLog

    r236378 r236379  
     12018-09-21  Justin Michaud  <justin_michaud@apple.com>
     2
     3        Implement initialValue support for CSS Custom Properties and Values API
     4        https://bugs.webkit.org/show_bug.cgi?id=189819
     5
     6        Reviewed by Simon Fraser.
     7
     8        * css/CSSComputedStyleDeclaration.cpp:
     9        (WebCore::ComputedStyleExtractor::customPropertyValue):
     10        * css/CSSCustomPropertyValue.cpp:
     11        (WebCore::CSSCustomPropertyValue::resolveVariableReferences const):
     12        * css/CSSCustomPropertyValue.h:
     13        * css/CSSRegisteredCustomProperty.h:
     14        * css/CSSVariableData.cpp:
     15        (WebCore::CSSVariableData::resolveVariableFallback const):
     16        (WebCore::CSSVariableData::resolveVariableReference const):
     17        (WebCore::CSSVariableData::resolveVariableReferences const):
     18        (WebCore::CSSVariableData::resolveTokenRange const):
     19        * css/CSSVariableData.h:
     20        * css/DOMCSSRegisterCustomProperty.cpp:
     21        (WebCore::DOMCSSRegisterCustomProperty::registerProperty):
     22        * css/DOMCSSRegisterCustomProperty.h:
     23        * css/DOMCSSRegisterCustomProperty.idl:
     24        * css/StyleResolver.cpp:
     25        (WebCore::StyleResolver::resolvedVariableValue):
     26        (WebCore::StyleResolver::applyCascadedProperties):
     27        * css/parser/CSSParser.cpp:
     28        (WebCore::CSSParser::parseValueWithVariableReferences):
     29        * css/parser/CSSParser.h:
     30        * dom/Document.h:
     31        (WebCore::Document::getCSSRegisteredCustomPropertySet const):
     32        * rendering/style/RenderStyle.cpp:
     33        (WebCore::RenderStyle::checkVariablesInCustomProperties):
     34        * rendering/style/RenderStyle.h:
     35
    1362018-09-21  Dean Jackson  <dino@apple.com>
    237
  • trunk/Source/WebCore/css/CSSComputedStyleDeclaration.cpp

    r234798 r236379  
    26252625        return nullptr;
    26262626
    2627     return style->customProperties().get(propertyName);
     2627    auto* value = style->customProperties().get(propertyName);
     2628    if (value)
     2629        return value;
     2630
     2631    auto* registered = styledElement->document().getCSSRegisteredCustomPropertySet().get(propertyName);
     2632    if (registered && registered->initialValue)
     2633        return registered->initialValue;
     2634
     2635    return nullptr;
    26282636}
    26292637
  • trunk/Source/WebCore/css/CSSCustomPropertyValue.cpp

    r218890 r236379  
    3838}
    3939
    40 void CSSCustomPropertyValue::resolveVariableReferences(const CustomPropertyValueMap& customProperties, Vector<Ref<CSSCustomPropertyValue>>& resolvedValues) const
     40void CSSCustomPropertyValue::resolveVariableReferences(const CustomPropertyValueMap& customProperties, const CSSRegisteredCustomPropertySet& registeredProperties, Vector<Ref<CSSCustomPropertyValue>>& resolvedValues) const
    4141{
    4242    ASSERT(containsVariables());
     
    4545   
    4646    ASSERT(m_value->needsVariableResolution());
    47     RefPtr<CSSVariableData> resolvedData = m_value->resolveVariableReferences(customProperties);
     47    RefPtr<CSSVariableData> resolvedData = m_value->resolveVariableReferences(customProperties, registeredProperties);
    4848    if (resolvedData)
    4949        resolvedValues.append(CSSCustomPropertyValue::createWithVariableData(m_name, resolvedData.releaseNonNull()));
  • trunk/Source/WebCore/css/CSSCustomPropertyValue.h

    r209666 r236379  
    2626#pragma once
    2727
     28#include "CSSRegisteredCustomProperty.h"
    2829#include "CSSValue.h"
    2930#include "CSSVariableData.h"
     
    7172    bool checkVariablesForCycles(const AtomicString& name, CustomPropertyValueMap&, HashSet<AtomicString>& seenProperties, HashSet<AtomicString>& invalidProperties) const;
    7273
    73     void resolveVariableReferences(const CustomPropertyValueMap&, Vector<Ref<CSSCustomPropertyValue>>&) const;
     74    void resolveVariableReferences(const CustomPropertyValueMap&, const CSSRegisteredCustomPropertySet&, Vector<Ref<CSSCustomPropertyValue>>&) const;
    7475
    7576    CSSValueID valueID() const { return m_valueId; }
  • trunk/Source/WebCore/css/CSSRegisteredCustomProperty.h

    r236273 r236379  
    3030namespace WebCore {
    3131
     32class CSSCustomPropertyValue;
     33
    3234struct CSSRegisteredCustomProperty {
    3335    const String name;
    34     /* TODO syntax, inherits, initialValue */
     36    /* TODO syntax, inherits */
     37    const RefPtr<CSSCustomPropertyValue> initialValue;
    3538};
    3639
     40using CSSRegisteredCustomPropertySet = HashMap<String, std::unique_ptr<CSSRegisteredCustomProperty>>;
     41
    3742}
  • trunk/Source/WebCore/css/CSSVariableData.cpp

    r222627 r236379  
    126126}
    127127
    128 bool CSSVariableData::resolveVariableFallback(const CustomPropertyValueMap& customProperties, CSSParserTokenRange range, Vector<CSSParserToken>& result) const
     128bool CSSVariableData::resolveVariableFallback(const CustomPropertyValueMap& customProperties, const CSSRegisteredCustomPropertySet& registeredProperties, CSSParserTokenRange range, Vector<CSSParserToken>& result) const
    129129{
    130130    if (range.atEnd())
     
    132132    ASSERT(range.peek().type() == CommaToken);
    133133    range.consume();
    134     return resolveTokenRange(customProperties, range, result);
     134    return resolveTokenRange(customProperties, registeredProperties, range, result);
    135135}
    136    
    137 bool CSSVariableData::resolveVariableReference(const CustomPropertyValueMap& customProperties, CSSParserTokenRange range, Vector<CSSParserToken>& result) const
     136
     137bool CSSVariableData::resolveVariableReference(const CustomPropertyValueMap& customProperties, const CSSRegisteredCustomPropertySet& registeredProperties, CSSParserTokenRange range, Vector<CSSParserToken>& result) const
    138138{
    139139    range.consumeWhitespace();
     
    143143   
    144144    RefPtr<CSSCustomPropertyValue> property = customProperties.get(variableName);
    145     if (!property || !property->value())
    146         return resolveVariableFallback(customProperties, range, result);
     145    if (!property || !property->value()) {
     146        auto* registered = registeredProperties.get(variableName);
     147        if (registered && registered->initialValue)
     148            property = registered->initialValue;
     149        else
     150            return resolveVariableFallback(customProperties, registeredProperties, range, result);
     151    }
     152    ASSERT(property);
    147153   
    148154    if (property->containsVariables()) {
    149155        // FIXME: Avoid doing this work more than once.
    150         RefPtr<CSSVariableData> resolvedData = property->value()->resolveVariableReferences(customProperties);
     156        RefPtr<CSSVariableData> resolvedData = property->value()->resolveVariableReferences(customProperties, registeredProperties);
    151157        if (!resolvedData)
    152158            return false;
     
    158164}
    159165
    160 RefPtr<CSSVariableData> CSSVariableData::resolveVariableReferences(const CustomPropertyValueMap& customProperties) const
     166RefPtr<CSSVariableData> CSSVariableData::resolveVariableReferences(const CustomPropertyValueMap& customProperties, const CSSRegisteredCustomPropertySet& registeredProperties) const
    161167{
    162168    Vector<CSSParserToken> resolvedTokens;
    163169    CSSParserTokenRange range = m_tokens;
    164     if (!resolveTokenRange(customProperties, range, resolvedTokens))
     170    if (!resolveTokenRange(customProperties, registeredProperties, range, resolvedTokens))
    165171        return nullptr;
    166172    return CSSVariableData::createResolved(resolvedTokens, *this);
    167173}
    168174   
    169 bool CSSVariableData::resolveTokenRange(const CustomPropertyValueMap& customProperties, CSSParserTokenRange range, Vector<CSSParserToken>& result) const
     175bool CSSVariableData::resolveTokenRange(const CustomPropertyValueMap& customProperties, const CSSRegisteredCustomPropertySet& registeredProperties, CSSParserTokenRange range, Vector<CSSParserToken>& result) const
    170176{
    171177    bool success = true;
    172178    while (!range.atEnd()) {
    173179        if (range.peek().functionId() == CSSValueVar || range.peek().functionId() == CSSValueEnv)
    174             success &= resolveVariableReference(customProperties, range.consumeBlock(), result);
     180            success &= resolveVariableReference(customProperties, registeredProperties, range.consumeBlock(), result);
    175181        else
    176182            result.append(range.consume());
  • trunk/Source/WebCore/css/CSSVariableData.h

    r218588 r236379  
    3232#include "CSSParserToken.h"
    3333#include "CSSParserTokenRange.h"
     34#include "CSSRegisteredCustomProperty.h"
    3435#include <wtf/HashSet.h>
    3536#include <wtf/text/WTFString.h>
     
    6364    bool checkVariablesForCycles(const AtomicString& name, CustomPropertyValueMap&, HashSet<AtomicString>& seenProperties, HashSet<AtomicString>& invalidProperties) const;
    6465
    65     RefPtr<CSSVariableData> resolveVariableReferences(const CustomPropertyValueMap& customProperties) const;
    66     bool resolveTokenRange(const CustomPropertyValueMap&, CSSParserTokenRange, Vector<CSSParserToken>&) const;
     66    RefPtr<CSSVariableData> resolveVariableReferences(const CustomPropertyValueMap& customProperties, const CSSRegisteredCustomPropertySet&) const;
     67    bool resolveTokenRange(const CustomPropertyValueMap&, const CSSRegisteredCustomPropertySet&, CSSParserTokenRange, Vector<CSSParserToken>&) const;
    6768
    6869private:
     
    8384   
    8485    bool checkVariablesForCyclesWithRange(CSSParserTokenRange, CustomPropertyValueMap&, HashSet<AtomicString>& seenProperties, HashSet<AtomicString>& invalidProperties) const;
    85     bool resolveVariableReference(const CustomPropertyValueMap&, CSSParserTokenRange, Vector<CSSParserToken>&) const;
    86     bool resolveVariableFallback(const CustomPropertyValueMap&, CSSParserTokenRange, Vector<CSSParserToken>&) const;
     86    bool resolveVariableReference(const CustomPropertyValueMap&, const CSSRegisteredCustomPropertySet&, CSSParserTokenRange, Vector<CSSParserToken>&) const;
     87    bool resolveVariableFallback(const CustomPropertyValueMap&, const CSSRegisteredCustomPropertySet&, CSSParserTokenRange, Vector<CSSParserToken>&) const;
    8788
    8889    String m_backingString;
  • trunk/Source/WebCore/css/DOMCSSRegisterCustomProperty.cpp

    r236273 r236379  
    2727#include "DOMCSSRegisterCustomProperty.h"
    2828
     29#include "CSSCustomPropertyValue.h"
    2930#include "CSSRegisteredCustomProperty.h"
     31#include "CSSTokenizer.h"
    3032#include "DOMCSSNamespace.h"
    3133#include "Document.h"
     
    3436namespace WebCore {
    3537
    36 void DOMCSSRegisterCustomProperty::registerProperty(Document& document, const DOMCSSCustomPropertyDescriptor& descriptor)
     38ExceptionOr<void> DOMCSSRegisterCustomProperty::registerProperty(Document& document, const DOMCSSCustomPropertyDescriptor& descriptor)
    3739{
    38     CSSRegisteredCustomProperty property { descriptor.name };
    39     if (!document.registerCSSProperty(WTFMove(property))) {
    40         /* TODO throw JS exception */
    41         return;
    42     }
     40    CSSTokenizer tokenizer(descriptor.initialValue);
     41    RefPtr<CSSCustomPropertyValue> initialValue;
     42    if (!tokenizer.tokenRange().atEnd())
     43        initialValue = CSSCustomPropertyValue::createWithVariableData(descriptor.name, CSSVariableData::create(tokenizer.tokenRange(), false));
     44
     45    CSSRegisteredCustomProperty property { descriptor.name, WTFMove(initialValue) };
     46    if (!document.registerCSSProperty(WTFMove(property)))
     47        return Exception { InvalidModificationError, "This property has already been registered." };
     48
     49    return { };
    4350}
    4451
  • trunk/Source/WebCore/css/DOMCSSRegisterCustomProperty.h

    r236273 r236379  
    2727
    2828#include "DOMCSSCustomPropertyDescriptor.h"
     29#include "ExceptionOr.h"
    2930#include "Supplementable.h"
    3031
     
    3839    explicit DOMCSSRegisterCustomProperty(DOMCSSNamespace&) { }
    3940
    40     static void registerProperty(Document&, const DOMCSSCustomPropertyDescriptor&);
     41    static ExceptionOr<void> registerProperty(Document&, const DOMCSSCustomPropertyDescriptor&);
    4142
    4243private:
  • trunk/Source/WebCore/css/DOMCSSRegisterCustomProperty.idl

    r236273 r236379  
    2727    EnabledAtRuntime=CSSCustomPropertiesAndValues
    2828] partial interface DOMCSSNamespace {
    29     [CallWith=Document] static void registerProperty(DOMCSSCustomPropertyDescriptor descriptor);
     29    [CallWith=Document, MayThrowException] static void registerProperty(DOMCSSCustomPropertyDescriptor descriptor);
    3030};
  • trunk/Source/WebCore/css/StyleResolver.cpp

    r236341 r236379  
    17091709{
    17101710    CSSParser parser(document());
    1711     return parser.parseValueWithVariableReferences(propID, value, m_state.style()->customProperties(), m_state.style()->direction(), m_state.style()->writingMode());
     1711    return parser.parseValueWithVariableReferences(propID, value, m_state.style()->customProperties(), document().getCSSRegisteredCustomPropertySet(), m_state.style()->direction(), m_state.style()->writingMode());
    17121712}
    17131713
     
    22862286
    22872287    if (firstProperty == CSSPropertyCustom)
    2288         m_state.style()->checkVariablesInCustomProperties();
     2288        m_state.style()->checkVariablesInCustomProperties(document().getCSSRegisteredCustomPropertySet());
    22892289}
    22902290
  • trunk/Source/WebCore/css/parser/CSSParser.cpp

    r235560 r236379  
    176176}
    177177
    178 RefPtr<CSSValue> CSSParser::parseValueWithVariableReferences(CSSPropertyID propID, const CSSValue& value, const CustomPropertyValueMap& customProperties, TextDirection direction, WritingMode writingMode)
     178RefPtr<CSSValue> CSSParser::parseValueWithVariableReferences(CSSPropertyID propID, const CSSValue& value, const CustomPropertyValueMap& customProperties, const CSSRegisteredCustomPropertySet& registeredProperties, TextDirection direction, WritingMode writingMode)
    179179{
    180180    if (value.isPendingSubstitutionValue()) {
     
    190190       
    191191        Vector<CSSParserToken> resolvedTokens;
    192         if (!variableData->resolveTokenRange(customProperties, variableData->tokens(), resolvedTokens))
     192        if (!variableData->resolveTokenRange(customProperties, registeredProperties, variableData->tokens(), resolvedTokens))
    193193            return nullptr;
    194194       
     
    211211       
    212212        Vector<CSSParserToken> resolvedTokens;
    213         if (!variableData->resolveTokenRange(customProperties, variableData->tokens(), resolvedTokens))
     213        if (!variableData->resolveTokenRange(customProperties, registeredProperties, variableData->tokens(), resolvedTokens))
    214214            return nullptr;
    215215       
  • trunk/Source/WebCore/css/parser/CSSParser.h

    r233520 r236379  
    2424
    2525#include "CSSParserContext.h"
     26#include "CSSRegisteredCustomProperty.h"
    2627#include "CSSValue.h"
    2728#include "WritingMode.h"
     
    7677    void parseSelector(const String&, CSSSelectorList&);
    7778
    78     RefPtr<CSSValue> parseValueWithVariableReferences(CSSPropertyID, const CSSValue&, const CustomPropertyValueMap& customProperties, TextDirection, WritingMode);
     79    RefPtr<CSSValue> parseValueWithVariableReferences(CSSPropertyID, const CSSValue&, const CustomPropertyValueMap&, const CSSRegisteredCustomPropertySet&, TextDirection, WritingMode);
    7980
    8081    static Color parseColor(const String&, bool strict = false);
  • trunk/Source/WebCore/dom/Document.h

    r236273 r236379  
    14931493    bool hasMainArticleElement() const { return !!m_mainArticleElement; }
    14941494
     1495    const CSSRegisteredCustomPropertySet& getCSSRegisteredCustomPropertySet() const { return m_CSSRegisteredPropertySet; }
    14951496    bool registerCSSProperty(CSSRegisteredCustomProperty&&);
    14961497
     
    20272028    std::unique_ptr<UserGestureIndicator> m_temporaryUserGesture;
    20282029
    2029     HashMap<String, std::unique_ptr<CSSRegisteredCustomProperty>> m_CSSRegisteredPropertySet;
     2030    CSSRegisteredCustomPropertySet m_CSSRegisteredPropertySet;
    20302031
    20312032    bool m_isRunningUserScripts { false };
  • trunk/Source/WebCore/rendering/style/RenderStyle.cpp

    r235560 r236379  
    22432243}
    22442244
    2245 void RenderStyle::checkVariablesInCustomProperties()
     2245void RenderStyle::checkVariablesInCustomProperties(const CSSRegisteredCustomPropertySet& registeredProperties)
    22462246{
    22472247    if (!m_rareInheritedData->customProperties->containsVariables)
     
    22742274        if (!entry.value->containsVariables())
    22752275            continue;
    2276         entry.value->resolveVariableReferences(customProperties, resolvedValues);
     2276        entry.value->resolveVariableReferences(customProperties, registeredProperties, resolvedValues);
    22772277    }
    22782278   
  • trunk/Source/WebCore/rendering/style/RenderStyle.h

    r235539 r236379  
    793793#endif
    794794
    795     void checkVariablesInCustomProperties();
     795    void checkVariablesInCustomProperties(const CSSRegisteredCustomPropertySet&);
    796796
    797797// attribute setter methods
Note: See TracChangeset for help on using the changeset viewer.