Title: [197165] trunk
Revision
197165
Author
[email protected]
Date
2016-02-26 06:57:01 -0800 (Fri, 26 Feb 2016)

Log Message

Implement ::slotted pseudo element
https://bugs.webkit.org/show_bug.cgi?id=149441
<rdar://problem/22731987>

Reviewed by Andreas Kling.

Source/WebCore:

Based on latest in https://github.com/w3c/webcomponents/issues/331

* css/CSSGrammar.y.in:

    Parse ::slotted.

* css/CSSParser.cpp:
(WebCore::CSSParser::detectFunctionTypeToken):
* css/CSSParserValues.cpp:
(WebCore::CSSParserSelector::parsePseudoElementCueFunctionSelector):
(WebCore::CSSParserSelector::parsePseudoElementSlottedFunctionSelector):

    Tokenize ::slotted.

(WebCore::CSSParserSelector::parsePseudoClassAndCompatibilityElementSelector):
* css/CSSParserValues.h:
* css/CSSSelector.cpp:
(WebCore::CSSSelector::pseudoId):
* css/CSSSelector.h:
* css/ElementRuleCollector.cpp:
(WebCore::ElementRuleCollector::matchAuthorRules):
(WebCore::ElementRuleCollector::matchHostPseudoClassRules):
(WebCore::ElementRuleCollector::matchSlottedPseudoElementRules):

    Match ::slotted selector.

(WebCore::ElementRuleCollector::collectSlottedPseudoElementRulesForSlot):

    Collect ::slotted rules that may apply to an element in a slot.

(WebCore::ElementRuleCollector::matchUserRules):
(WebCore::ElementRuleCollector::matchUARules):
(WebCore::findSlottedPseudoElementSelector):
(WebCore::ElementRuleCollector::ruleMatches):
* css/ElementRuleCollector.h:
* css/RuleSet.cpp:
(WebCore::RuleSet::addRule):

    Collect ::slotted rules.

(WebCore::RuleSet::shrinkToFit):
* css/RuleSet.h:
(WebCore::RuleSet::hostPseudoClassRules):
(WebCore::RuleSet::slottedPseudoElementRules):
(WebCore::RuleSet::focusPseudoClassRules):
(WebCore::RuleSet::universalRules):
* css/SelectorChecker.cpp:
(WebCore::SelectorChecker::checkOne):
* style/StyleSharingResolver.cpp:
(WebCore::Style::SharingResolver::resolve):

    Disable style sharing for children of shadow host. They may be affected by the shadow tree style
    which is not considered in style sharing checks.

LayoutTests:

* fast/shadow-dom/css-scoping-shadow-slotted-rule.html:

    Enable the test, fix it and update it to the current spec.

* fast/shadow-dom/slotted-pseudo-element-css-text-expected.txt: Added.
* fast/shadow-dom/slotted-pseudo-element-css-text.html: Added.

    Add parsing/cssText test based on a Blink test.
    There are a few failures due to * not roundtripping and the parser being too lenient with pseudo elements.

* platform/mac/TestExpectations:

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (197164 => 197165)


--- trunk/LayoutTests/ChangeLog	2016-02-26 11:49:49 UTC (rev 197164)
+++ trunk/LayoutTests/ChangeLog	2016-02-26 14:57:01 UTC (rev 197165)
@@ -1,3 +1,23 @@
+2016-02-26  Antti Koivisto  <[email protected]>
+
+        Implement ::slotted pseudo element
+        https://bugs.webkit.org/show_bug.cgi?id=149441
+        <rdar://problem/22731987>
+
+        Reviewed by Andreas Kling.
+
+        * fast/shadow-dom/css-scoping-shadow-slotted-rule.html:
+
+            Enable the test, fix it and update it to the current spec.
+
+        * fast/shadow-dom/slotted-pseudo-element-css-text-expected.txt: Added.
+        * fast/shadow-dom/slotted-pseudo-element-css-text.html: Added.
+
+            Add parsing/cssText test based on a Blink test.
+            There are a few failures due to * not roundtripping and the parser being too lenient with pseudo elements.
+
+        * platform/mac/TestExpectations:
+
 2016-02-26  Youenn Fablet  <[email protected]>
 
         Sync web-platform-tests up to revision 5ca8b46

Modified: trunk/LayoutTests/fast/shadow-dom/css-scoping-shadow-slotted-rule.html (197164 => 197165)


--- trunk/LayoutTests/fast/shadow-dom/css-scoping-shadow-slotted-rule.html	2016-02-26 11:49:49 UTC (rev 197164)
+++ trunk/LayoutTests/fast/shadow-dom/css-scoping-shadow-slotted-rule.html	2016-02-26 14:57:01 UTC (rev 197165)
@@ -1,7 +1,7 @@
 <!DOCTYPE html>
 <html>
 <head>
-    <title>CSS Scoping - :slotted pesudo element must allow selecting elements assigned to a slot element</title>
+    <title>CSS Scoping - :slotted pseudo element must allow selecting elements assigned to a slot element</title>
     <link rel="author" title="Ryosuke Niwa" href=""
     <link rel="help" href=""
     <link rel="match" href=""
@@ -12,7 +12,8 @@
             display: block;
             width: 100px;
             height: 100px;
-            background: red;
+            color: red;
+            background: green;
         }
         my-host > div, nested-host {
             display: block;
@@ -23,7 +24,7 @@
     <p>Test passes if you see a single 100px by 100px green box below.</p> 
     <my-host>
         <div class="green">FAIL1</div>
-        <div><span>FAIL2</span></div>
+        <myelem><span>FAIL2</span></myelem>
         <nested-host>
             <span>FAIL3</span>
         </nested-host>
@@ -36,15 +37,15 @@
         try {
             var shadowHost = document.querySelector('my-host');
             shadowRoot = shadowHost.attachShadow({mode: 'open'});
-            shadowRoot.innerHTML = '<slot></slot><style> ::slotted > .green, ::slotted span { color:green; } </style>';
+            shadowRoot.innerHTML = '<slot></slot><style> ::slotted(.green), ::slotted(myelem) { color:green; } </style>';
 
             shadowHost = document.querySelector('nested-host');
             shadowRoot = shadowHost.attachShadow({mode: 'open'});
-            shadowRoot.innerHTML = '<slot></slot>';
+            shadowRoot.innerHTML = '<style> .mydiv ::slotted(*) { color:green; } </style><div class=mydiv><slot></slot></div>';
 
             shadowHost = document.querySelector('another-host');
             shadowRoot = shadowHost.attachShadow({mode: 'open'});
-            shadowRoot.innerHTML = '<style> ::slotted { color:green; } </style><slot></slot>';
+            shadowRoot.innerHTML = '<style> ::slotted(*) { color:green; } </style><slot></slot>';
         } catch (exception) {
             document.body.appendChild(document.createTextNode(exception));
         }

Added: trunk/LayoutTests/fast/shadow-dom/slotted-pseudo-element-css-text-expected.txt (0 => 197165)


--- trunk/LayoutTests/fast/shadow-dom/slotted-pseudo-element-css-text-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/fast/shadow-dom/slotted-pseudo-element-css-text-expected.txt	2016-02-26 14:57:01 UTC (rev 197165)
@@ -0,0 +1,27 @@
+
+PASS ::slotted { display: block; } 
+PASS ::slotted() { display: block; } 
+PASS ::slotted(*) { display: block; } 
+FAIL *::slotted(*) { display: block; } assert_equals: expected "*::slotted(*) { display: block; }" but got "::slotted(*) { display: block; }"
+PASS ::slotted(div) { display: block; } 
+PASS ::slotted( div) { display: block; } 
+PASS ::slotted(div ) { display: block; } 
+PASS ::slotted(div::before) { display: block; } 
+PASS .foo::slotted(div) { color: blue; } 
+PASS #id::slotted(*) { color: blue; } 
+PASS [attr=foo]::slotted(*) { color: blue; } 
+PASS .foo .bar::slotted(div) { color: blue; } 
+FAIL .foo::before .bar::slotted(div) { color: blue; } assert_equals: expected "" but got ".foo::before .bar::slotted(div) { color: blue; }"
+FAIL .foo::slotted(div) .bar { color: blue; } assert_equals: expected "" but got ".foo::slotted(div) .bar { color: blue; }"
+PASS ::slotted(div, div) { color: blue; } 
+PASS ::slotted(div div) { color: blue; } 
+PASS slot::slotted(.green) { color: green; } 
+PASS slot::slotted(#green) { color: green; } 
+PASS slot::slotted([green=green]) { color: green; } 
+PASS slot::slotted(div.green) { color: green; } 
+PASS div ::slotted(div) { color: red; } 
+PASS div + slot::slotted(div) { color: red; } 
+PASS span::slotted(*) { color: red; } 
+FAIL ::slotted(span)::slotted(span) { color: red; } assert_equals: expected "" but got "::slotted(span)::slotted(span) { color: red; }"
+PASS ::slotted(::slotted(div)) { color: red; } 
+

Added: trunk/LayoutTests/fast/shadow-dom/slotted-pseudo-element-css-text.html (0 => 197165)


--- trunk/LayoutTests/fast/shadow-dom/slotted-pseudo-element-css-text.html	                        (rev 0)
+++ trunk/LayoutTests/fast/shadow-dom/slotted-pseudo-element-css-text.html	2016-02-26 14:57:01 UTC (rev 197165)
@@ -0,0 +1,76 @@
+<!DOCTYPE html>
+<script src=''></script>
+<script src=''></script>
+<script>
+var rules = [
+"::slotted { display: block; }", /* invalid - no parameter */
+"::slotted() { display: block; }", /* invalid - empty parameter */
+"::slotted(*) { display: block; }",
+"*::slotted(*) { display: block; }",
+"::slotted(div) { display: block; }", /* expects universal selector (*) on the left in cssText */
+"::slotted( div) { display: block; }", /* allow a space on left */
+"::slotted(div ) { display: block; }", /* allow a space on right */
+"::slotted(div::before) { display: block; }", /* having a pseudo element in () is invalid */
+".foo::slotted(div) { color: blue; }",
+"#id::slotted(*) { color: blue; }",
+"[attr=foo]::slotted(*) { color: blue; }",
+".foo .bar::slotted(div) { color: blue; }",
+".foo::before .bar::slotted(div) { color: blue; }", /* invalid, only one pseudo element is allowed at the rightmost compound */
+".foo::slotted(div) .bar { color: blue; }", /* invalid, same as above */
+"::slotted(div, div) { color: blue; }", /* invalid - selector list */
+"::slotted(div div) { color: blue; }", /* invalid - complex selector (combinator is used) */
+"slot::slotted(.green) { color: green; }",
+"slot::slotted(#green) { color: green; }",
+"slot::slotted([green=green]) { color: green; }",
+"slot::slotted(div.green) { color: green; }",
+"div ::slotted(div) { color: red; }",
+"div + slot::slotted(div) { color: red; }",
+"span::slotted(*) { color: red; }", /* never matches, but valid as a selector */
+"::slotted(span)::slotted(span) { color: red; }", /* invalid */
+"::slotted(::slotted(div)) { color: red; }" /* invalid */
+];
+
+var expectedCSSTexts = [
+"", 
+"",
+"::slotted(*) { display: block; }",
+"*::slotted(*) { display: block; }",
+"::slotted(div) { display: block; }",
+"::slotted(div) { display: block; }", 
+"::slotted(div) { display: block; }",
+"",
+".foo::slotted(div) { color: blue; }",
+"#id::slotted(*) { color: blue; }",
+"[attr=\"foo\"]::slotted(*) { color: blue; }",
+".foo .bar::slotted(div) { color: blue; }",
+"",
+"",
+"",
+"",
+"slot::slotted(.green) { color: green; }",
+"slot::slotted(#green) { color: green; }",
+"slot::slotted([green=\"green\"]) { color: green; }",
+"slot::slotted(div.green) { color: green; }",
+"div ::slotted(div) { color: red; }",
+"div + slot::slotted(div) { color: red; }",
+"span::slotted(*) { color: red; }",
+"",
+"",
+];
+
+function ruleCSSText(rule)
+{
+    var style = document.createElement("style");
+    style.innerText = rule;
+    document.head.appendChild(style);
+    if (style.sheet.cssRules.length == 0)
+        return "";
+    return style.sheet.cssRules.item(0).cssText;
+}
+
+for (var i = 0; i < rules.length; ++i) {
+    test(() => {
+        assert_equals(ruleCSSText(rules[i]), expectedCSSTexts[i]);
+    }, rules[i]);
+}
+</script>

Modified: trunk/LayoutTests/platform/mac/TestExpectations (197164 => 197165)


--- trunk/LayoutTests/platform/mac/TestExpectations	2016-02-26 11:49:49 UTC (rev 197164)
+++ trunk/LayoutTests/platform/mac/TestExpectations	2016-02-26 14:57:01 UTC (rev 197165)
@@ -1249,7 +1249,6 @@
 
 webkit.org/b/148695 fast/shadow-dom [ Pass ]
 webkit.org/b/149440 fast/shadow-dom/css-scoping-shadow-host-functional-rule.html [ ImageOnlyFailure ]
-webkit.org/b/149441 fast/shadow-dom/css-scoping-shadow-slotted-rule.html [ ImageOnlyFailure ]
 webkit.org/b/149441 fast/shadow-dom/css-scoping-shadow-slot-display-override.html [ ImageOnlyFailure ]
 
 # Touch events is not enabled on Mac

Modified: trunk/Source/WebCore/ChangeLog (197164 => 197165)


--- trunk/Source/WebCore/ChangeLog	2016-02-26 11:49:49 UTC (rev 197164)
+++ trunk/Source/WebCore/ChangeLog	2016-02-26 14:57:01 UTC (rev 197165)
@@ -1,3 +1,65 @@
+2016-02-25  Antti Koivisto  <[email protected]>
+
+        Implement ::slotted pseudo element
+        https://bugs.webkit.org/show_bug.cgi?id=149441
+        <rdar://problem/22731987>
+
+        Reviewed by Andreas Kling.
+
+        Based on latest in https://github.com/w3c/webcomponents/issues/331
+
+        * css/CSSGrammar.y.in:
+
+            Parse ::slotted.
+
+        * css/CSSParser.cpp:
+        (WebCore::CSSParser::detectFunctionTypeToken):
+        * css/CSSParserValues.cpp:
+        (WebCore::CSSParserSelector::parsePseudoElementCueFunctionSelector):
+        (WebCore::CSSParserSelector::parsePseudoElementSlottedFunctionSelector):
+
+            Tokenize ::slotted.
+
+        (WebCore::CSSParserSelector::parsePseudoClassAndCompatibilityElementSelector):
+        * css/CSSParserValues.h:
+        * css/CSSSelector.cpp:
+        (WebCore::CSSSelector::pseudoId):
+        * css/CSSSelector.h:
+        * css/ElementRuleCollector.cpp:
+        (WebCore::ElementRuleCollector::matchAuthorRules):
+        (WebCore::ElementRuleCollector::matchHostPseudoClassRules):
+        (WebCore::ElementRuleCollector::matchSlottedPseudoElementRules):
+
+            Match ::slotted selector.
+
+        (WebCore::ElementRuleCollector::collectSlottedPseudoElementRulesForSlot):
+
+            Collect ::slotted rules that may apply to an element in a slot.
+
+        (WebCore::ElementRuleCollector::matchUserRules):
+        (WebCore::ElementRuleCollector::matchUARules):
+        (WebCore::findSlottedPseudoElementSelector):
+        (WebCore::ElementRuleCollector::ruleMatches):
+        * css/ElementRuleCollector.h:
+        * css/RuleSet.cpp:
+        (WebCore::RuleSet::addRule):
+
+            Collect ::slotted rules.
+
+        (WebCore::RuleSet::shrinkToFit):
+        * css/RuleSet.h:
+        (WebCore::RuleSet::hostPseudoClassRules):
+        (WebCore::RuleSet::slottedPseudoElementRules):
+        (WebCore::RuleSet::focusPseudoClassRules):
+        (WebCore::RuleSet::universalRules):
+        * css/SelectorChecker.cpp:
+        (WebCore::SelectorChecker::checkOne):
+        * style/StyleSharingResolver.cpp:
+        (WebCore::Style::SharingResolver::resolve):
+
+            Disable style sharing for children of shadow host. They may be affected by the shadow tree style
+            which is not considered in style sharing checks.
+
 2016-02-25  Myles C. Maxfield  <[email protected]>
 
         Font size computed style is innaccurate

Modified: trunk/Source/WebCore/css/CSSGrammar.y.in (197164 => 197165)


--- trunk/Source/WebCore/css/CSSGrammar.y.in	2016-02-26 11:49:49 UTC (rev 197164)
+++ trunk/Source/WebCore/css/CSSGrammar.y.in	2016-02-26 14:57:01 UTC (rev 197165)
@@ -365,6 +365,12 @@
 
 #endif
 
+#if ENABLE_SHADOW_DOM
+
+%token <string> SLOTTEDFUNCTION
+
+#endif
+
 %%
 
 stylesheet:
@@ -1357,6 +1363,11 @@
         $$ = CSSParserSelector::parsePseudoElementCueFunctionSelector($3, $5);
     }
 #endif
+#if ENABLE_SHADOW_DOM
+    | ':' ':' SLOTTEDFUNCTION maybe_space compound_selector maybe_space ')' {
+        $$ = CSSParserSelector::parsePseudoElementSlottedFunctionSelector($3, $5);
+    }
+#endif
     // use by :-webkit-any.
     // FIXME: should we support generic selectors here or just simple_selectors?
     // Use simple_selector_list for now to match -moz-any.

Modified: trunk/Source/WebCore/css/CSSParser.cpp (197164 => 197165)


--- trunk/Source/WebCore/css/CSSParser.cpp	2016-02-26 11:49:49 UTC (rev 197164)
+++ trunk/Source/WebCore/css/CSSParser.cpp	2016-02-26 14:57:01 UTC (rev 197165)
@@ -11894,6 +11894,12 @@
             m_token = MATCHESFUNCTION;
             return true;
         }
+#if ENABLE(SHADOW_DOM)
+        if (isEqualToCSSIdentifier(name, "slotted")) {
+            m_token = SLOTTEDFUNCTION;
+            return true;
+        }
+#endif
         return false;
 
     case 9:

Modified: trunk/Source/WebCore/css/CSSParserValues.cpp (197164 => 197165)


--- trunk/Source/WebCore/css/CSSParserValues.cpp	2016-02-26 11:49:49 UTC (rev 197164)
+++ trunk/Source/WebCore/css/CSSParserValues.cpp	2016-02-26 14:57:01 UTC (rev 197165)
@@ -231,6 +231,32 @@
 }
 #endif
 
+#if ENABLE(SHADOW_DOM)
+CSSParserSelector* CSSParserSelector::parsePseudoElementSlottedFunctionSelector(const CSSParserString& functionIdentifier, CSSParserSelector* parsedSelector)
+{
+    ASSERT_UNUSED(functionIdentifier, String(functionIdentifier) == "slotted(");
+
+    if (!parsedSelector)
+        return nullptr;
+
+    std::unique_ptr<CSSParserSelector> ownedParsedSelector(parsedSelector);
+
+    for (auto* component = parsedSelector; component; component = component->tagHistory()) {
+        if (component->matchesPseudoElement())
+            return nullptr;
+    }
+
+    auto selectorVector = std::make_unique<Vector<std::unique_ptr<CSSParserSelector>>>();
+    selectorVector->append(WTFMove(ownedParsedSelector));
+
+    auto selector = std::make_unique<CSSParserSelector>();
+    selector->m_selector->setMatch(CSSSelector::PseudoElement);
+    selector->m_selector->setPseudoElementType(CSSSelector::PseudoElementSlotted);
+    selector->adoptSelectorVector(*selectorVector);
+    return selector.release();
+}
+#endif
+
 CSSParserSelector* CSSParserSelector::parsePseudoClassAndCompatibilityElementSelector(CSSParserString& pseudoTypeString)
 {
     if (pseudoTypeString.length() && pseudoTypeString[pseudoTypeString.length() - 1] == '(')

Modified: trunk/Source/WebCore/css/CSSParserValues.h (197164 => 197165)


--- trunk/Source/WebCore/css/CSSParserValues.h	2016-02-26 11:49:49 UTC (rev 197164)
+++ trunk/Source/WebCore/css/CSSParserValues.h	2016-02-26 14:57:01 UTC (rev 197165)
@@ -203,7 +203,10 @@
 public:
     static CSSParserSelector* parsePagePseudoSelector(const CSSParserString& pseudoTypeString);
     static CSSParserSelector* parsePseudoElementSelector(CSSParserString& pseudoTypeString);
-    static CSSParserSelector* parsePseudoElementCueFunctionSelector(const CSSParserString& functionIdentifier, Vector<std::unique_ptr<CSSParserSelector>>* selectorVector);
+    static CSSParserSelector* parsePseudoElementCueFunctionSelector(const CSSParserString& functionIdentifier, Vector<std::unique_ptr<CSSParserSelector>>*);
+#if ENABLE(SHADOW_DOM)
+    static CSSParserSelector* parsePseudoElementSlottedFunctionSelector(const CSSParserString& functionIdentifier, CSSParserSelector*);
+#endif
     static CSSParserSelector* parsePseudoClassAndCompatibilityElementSelector(CSSParserString& pseudoTypeString);
 
     CSSParserSelector();

Modified: trunk/Source/WebCore/css/CSSSelector.cpp (197164 => 197165)


--- trunk/Source/WebCore/css/CSSSelector.cpp	2016-02-26 11:49:49 UTC (rev 197164)
+++ trunk/Source/WebCore/css/CSSSelector.cpp	2016-02-26 14:57:01 UTC (rev 197165)
@@ -299,6 +299,9 @@
 #if ENABLE(VIDEO_TRACK)
     case PseudoElementCue:
 #endif
+#if ENABLE(SHADOW_DOM)
+    case PseudoElementSlotted:
+#endif
     case PseudoElementUnknown:
     case PseudoElementUserAgentCustom:
     case PseudoElementWebKitCustom:
@@ -644,8 +647,18 @@
                 ASSERT_NOT_REACHED();
             }
         } else if (cs->match() == CSSSelector::PseudoElement) {
-            str.appendLiteral("::");
-            str.append(cs->value());
+            switch (cs->pseudoElementType()) {
+#if ENABLE(SHADOW_DOM)
+            case CSSSelector::PseudoElementSlotted:
+                str.appendLiteral("::slotted(");
+                cs->selectorList()->buildSelectorsText(str);
+                str.append(')');
+                break;
+#endif
+            default:
+                str.appendLiteral("::");
+                str.append(cs->value());
+            }
         } else if (cs->isAttributeSelector()) {
             str.append('[');
             const AtomicString& prefix = cs->attribute().prefix();

Modified: trunk/Source/WebCore/css/CSSSelector.h (197164 => 197165)


--- trunk/Source/WebCore/css/CSSSelector.h	2016-02-26 11:49:49 UTC (rev 197164)
+++ trunk/Source/WebCore/css/CSSSelector.h	2016-02-26 14:57:01 UTC (rev 197165)
@@ -181,6 +181,9 @@
             PseudoElementScrollbarTrack,
             PseudoElementScrollbarTrackPiece,
             PseudoElementSelection,
+#if ENABLE(SHADOW_DOM)
+            PseudoElementSlotted,
+#endif
             PseudoElementUserAgentCustom,
             PseudoElementWebKitCustom,
         };

Modified: trunk/Source/WebCore/css/ElementRuleCollector.cpp (197164 => 197165)


--- trunk/Source/WebCore/css/ElementRuleCollector.cpp	2016-02-26 11:49:49 UTC (rev 197164)
+++ trunk/Source/WebCore/css/ElementRuleCollector.cpp	2016-02-26 14:57:01 UTC (rev 197165)
@@ -36,6 +36,7 @@
 #include "CSSSelectorList.h"
 #include "CSSValueKeywords.h"
 #include "HTMLElement.h"
+#include "HTMLSlotElement.h"
 #include "InspectorInstrumentation.h"
 #include "NodeRenderStyle.h"
 #include "RenderRegion.h"
@@ -206,6 +207,10 @@
 #if ENABLE(SHADOW_DOM)
     if (m_element.shadowRoot())
         matchHostPseudoClassRules(includeEmptyRules);
+
+    auto* parent = m_element.parentNode();
+    if (parent && parent->shadowRoot())
+        matchSlottedPseudoElementRules(includeEmptyRules);
 #endif
 
     clearMatchedRules();
@@ -241,6 +246,62 @@
     // FIXME: Match the spec when it is finalized.
     sortAndTransferMatchedRules();
 }
+
+void ElementRuleCollector::matchSlottedPseudoElementRules(bool includeEmptyRules)
+{
+    auto* hostShadowRoot = m_element.parentNode()->shadowRoot();
+    ASSERT(hostShadowRoot);
+    auto* slot = hostShadowRoot->findAssignedSlot(m_element);
+    if (!slot)
+        return;
+    auto* shadowAuthorStyle = hostShadowRoot->styleResolver().ruleSets().authorStyle();
+    if (!shadowAuthorStyle)
+        return;
+    // Find out if there are any ::slotted rules in the shadow tree matching the current slot.
+    // FIXME: This is really part of the slot style and could be cached when resolving it.
+    ElementRuleCollector collector(*slot, *shadowAuthorStyle, nullptr);
+    auto slottedPseudoElementRules = collector.collectSlottedPseudoElementRulesForSlot(includeEmptyRules);
+    if (slottedPseudoElementRules.isEmpty())
+        return;
+
+    clearMatchedRules();
+    m_result.ranges.lastAuthorRule = m_result.matchedProperties().size() - 1;
+
+    {
+        // Match in the current scope.
+        TemporaryChange<bool> change(m_isMatchingSlottedPseudoElements, true);
+
+        MatchRequest matchRequest(nullptr, includeEmptyRules);
+        auto ruleRange = m_result.ranges.authorRuleRange();
+        collectMatchingRulesForList(&slottedPseudoElementRules, matchRequest, ruleRange);
+    }
+
+    // FIXME: What is the correct order?
+    sortAndTransferMatchedRules();
+}
+
+RuleSet::RuleDataVector ElementRuleCollector::collectSlottedPseudoElementRulesForSlot(bool includeEmptyRules)
+{
+    ASSERT(is<HTMLSlotElement>(m_element));
+
+    clearMatchedRules();
+
+    m_mode = SelectorChecker::Mode::CollectingRules;
+
+    // Match global author rules.
+    MatchRequest matchRequest(&m_authorStyle, includeEmptyRules);
+    StyleResolver::RuleRange ruleRange = m_result.ranges.authorRuleRange();
+    collectMatchingRulesForList(&m_authorStyle.slottedPseudoElementRules(), matchRequest, ruleRange);
+
+    if (m_matchedRules.isEmpty())
+        return { };
+
+    RuleSet::RuleDataVector ruleDataVector;
+    ruleDataVector.reserveInitialCapacity(m_matchedRules.size());
+    for (auto& matchedRule : m_matchedRules)
+        ruleDataVector.uncheckedAppend(*matchedRule.ruleData);
+    return ruleDataVector;
+}
 #endif
 
 void ElementRuleCollector::matchUserRules(bool includeEmptyRules)
@@ -284,6 +345,20 @@
     sortAndTransferMatchedRules();
 }
 
+#if ENABLE(SHADOW_DOM)
+static const CSSSelector* findSlottedPseudoElementSelector(const CSSSelector* selector)
+{
+    for (; selector; selector = selector->tagHistory()) {
+        if (selector->match() == CSSSelector::PseudoElement && selector->pseudoElementType() == CSSSelector::PseudoElementSlotted) {
+            if (auto* list = selector->selectorList())
+                return list->first();
+            break;
+        }
+    };
+    return nullptr;
+}
+#endif
+
 inline bool ElementRuleCollector::ruleMatches(const RuleData& ruleData, unsigned& specificity)
 {
     // We know a sufficiently simple single part selector matches simply because we found it from the rule hash when filtering the RuleSet.
@@ -356,9 +431,17 @@
     } else
 #endif // ENABLE(CSS_SELECTOR_JIT)
     {
+        auto* selector = ruleData.selector();
+#if ENABLE(SHADOW_DOM)
+        if (m_isMatchingSlottedPseudoElements) {
+            selector = findSlottedPseudoElementSelector(ruleData.selector());
+            if (!selector)
+                return false;
+        }
+#endif
         // Slow path.
         SelectorChecker selectorChecker(m_element.document());
-        selectorMatches = selectorChecker.match(*ruleData.selector(), m_element, context, specificity);
+        selectorMatches = selectorChecker.match(*selector, m_element, context, specificity);
     }
 
     commitStyleRelations(context.styleRelations);

Modified: trunk/Source/WebCore/css/ElementRuleCollector.h (197164 => 197165)


--- trunk/Source/WebCore/css/ElementRuleCollector.h	2016-02-26 11:49:49 UTC (rev 197164)
+++ trunk/Source/WebCore/css/ElementRuleCollector.h	2016-02-26 14:57:01 UTC (rev 197165)
@@ -73,6 +73,8 @@
     void matchUARules(RuleSet*);
 #if ENABLE(SHADOW_DOM)
     void matchHostPseudoClassRules(bool includeEmptyRules);
+    void matchSlottedPseudoElementRules(bool includeEmptyRules);
+    RuleSet::RuleDataVector collectSlottedPseudoElementRulesForSlot(bool includeEmptyRules);
 #endif
 
     void collectMatchingRules(const MatchRequest&, StyleResolver::RuleRange&);
@@ -98,6 +100,9 @@
     PseudoStyleRequest m_pseudoStyleRequest { NOPSEUDO };
     bool m_sameOriginOnly { false };
     SelectorChecker::Mode m_mode { SelectorChecker::Mode::ResolvingStyle };
+#if ENABLE(SHADOW_DOM)
+    bool m_isMatchingSlottedPseudoElements { false };
+#endif
 
     Vector<MatchedRule, 64> m_matchedRules;
 

Modified: trunk/Source/WebCore/css/RuleSet.cpp (197164 => 197165)


--- trunk/Source/WebCore/css/RuleSet.cpp	2016-02-26 11:49:49 UTC (rev 197164)
+++ trunk/Source/WebCore/css/RuleSet.cpp	2016-02-26 14:57:01 UTC (rev 197165)
@@ -266,6 +266,12 @@
             m_hostPseudoClassRules.append(ruleData);
             return;
         }
+        if (selector->match() == CSSSelector::PseudoElement && selector->pseudoElementType() == CSSSelector::PseudoElementSlotted) {
+            // ::slotted pseudo elements work accross shadow boundary making filtering difficult.
+            ruleData.disableSelectorFiltering();
+            m_slottedPseudoElementRules.append(ruleData);
+            return;
+        }
 #endif
         if (selector->relation() != CSSSelector::SubSelector)
             break;
@@ -422,6 +428,10 @@
 #if ENABLE(VIDEO_TRACK)
     m_cuePseudoRules.shrinkToFit();
 #endif
+#if ENABLE(SHADOW_DOM)
+    m_hostPseudoClassRules.shrinkToFit();
+    m_slottedPseudoElementRules.shrinkToFit();
+#endif
     m_focusPseudoClassRules.shrinkToFit();
     m_universalRules.shrinkToFit();
     m_pageRules.shrinkToFit();

Modified: trunk/Source/WebCore/css/RuleSet.h (197164 => 197165)


--- trunk/Source/WebCore/css/RuleSet.h	2016-02-26 11:49:49 UTC (rev 197164)
+++ trunk/Source/WebCore/css/RuleSet.h	2016-02-26 14:57:01 UTC (rev 197165)
@@ -184,6 +184,7 @@
 #endif
 #if ENABLE(SHADOW_DOM)
     const RuleDataVector& hostPseudoClassRules() const { return m_hostPseudoClassRules; }
+    const RuleDataVector& slottedPseudoElementRules() const { return m_slottedPseudoElementRules; }
 #endif
     const RuleDataVector* focusPseudoClassRules() const { return &m_focusPseudoClassRules; }
     const RuleDataVector* universalRules() const { return &m_universalRules; }
@@ -210,6 +211,7 @@
 #endif
 #if ENABLE(SHADOW_DOM)
     RuleDataVector m_hostPseudoClassRules;
+    RuleDataVector m_slottedPseudoElementRules;
 #endif
     RuleDataVector m_focusPseudoClassRules;
     RuleDataVector m_universalRules;

Modified: trunk/Source/WebCore/css/SelectorChecker.cpp (197164 => 197165)


--- trunk/Source/WebCore/css/SelectorChecker.cpp	2016-02-26 11:49:49 UTC (rev 197164)
+++ trunk/Source/WebCore/css/SelectorChecker.cpp	2016-02-26 14:57:01 UTC (rev 197165)
@@ -44,6 +44,7 @@
 #include "HTMLOptionElement.h"
 #include "HTMLParserIdioms.h"
 #include "HTMLProgressElement.h"
+#include "HTMLSlotElement.h"
 #include "HTMLStyleElement.h"
 #include "InspectorInstrumentation.h"
 #include "Page.h"
@@ -1038,7 +1039,13 @@
         return false;
     }
 #endif
-    // ### add the rest of the checks...
+#if ENABLE(SHADOW_DOM)
+    if (selector.match() == CSSSelector::PseudoElement && selector.pseudoElementType() == CSSSelector::PseudoElementSlotted) {
+        // We see ::slotted() pseudo elements when collecting slotted rules from the slot shadow tree only.
+        ASSERT(checkingContext.resolvingMode == Mode::CollectingRules);
+        return is<HTMLSlotElement>(element);
+    }
+#endif
     return true;
 }
 

Modified: trunk/Source/WebCore/style/StyleSharingResolver.cpp (197164 => 197165)


--- trunk/Source/WebCore/style/StyleSharingResolver.cpp	2016-02-26 11:49:49 UTC (rev 197164)
+++ trunk/Source/WebCore/style/StyleSharingResolver.cpp	2016-02-26 14:57:01 UTC (rev 197165)
@@ -76,6 +76,8 @@
     if (!element.parentElement())
         return nullptr;
     auto& parentElement = *element.parentElement();
+    if (parentElement.shadowRoot())
+        return nullptr;
     if (!parentElement.renderStyle())
         return nullptr;
     // If the element has inline style it is probably unique.
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to