Implement :host pseudo class
authorantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 7 Oct 2015 19:59:32 +0000 (19:59 +0000)
committerantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 7 Oct 2015 19:59:32 +0000 (19:59 +0000)
https://bugs.webkit.org/show_bug.cgi?id=149440
Source/WebCore:

rdar://problem/22731953

Reviewed by Ryosuke Niwa.

This implements the basic non-function :host syntax.

* css/CSSSelector.cpp:
(WebCore::CSSSelector::selectorText):
* css/CSSSelector.h:
* css/ElementRuleCollector.cpp:
(WebCore::ElementRuleCollector::matchAuthorRules):
(WebCore::ElementRuleCollector::matchHostPseudoClassRules):
(WebCore::ElementRuleCollector::matchUserRules):
* css/ElementRuleCollector.h:
* css/RuleSet.cpp:
(WebCore::computeMatchBasedOnRuleHash):
(WebCore::RuleSet::addRule):
* css/RuleSet.h:
(WebCore::RuleSet::cuePseudoRules):
(WebCore::RuleSet::hostPseudoClassRules):
(WebCore::RuleSet::focusPseudoClassRules):
(WebCore::RuleSet::universalRules):
* css/SelectorChecker.cpp:
(WebCore::SelectorChecker::checkOne):
* css/SelectorPseudoClassAndCompatibilityElementMap.in:
* cssjit/SelectorCompiler.cpp:
(WebCore::SelectorCompiler::addPseudoClassType):

LayoutTests:

Reviewed by Ryosuke Niwa.

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

    Fix and expand the test case.

* platform/mac/TestExpectations:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@190680 268f45cc-cd09-0410-ab3c-d52691b4dbfc

13 files changed:
LayoutTests/ChangeLog
LayoutTests/fast/shadow-dom/css-scoping-shadow-host-rule.html
LayoutTests/platform/mac/TestExpectations
Source/WebCore/ChangeLog
Source/WebCore/css/CSSSelector.cpp
Source/WebCore/css/CSSSelector.h
Source/WebCore/css/ElementRuleCollector.cpp
Source/WebCore/css/ElementRuleCollector.h
Source/WebCore/css/RuleSet.cpp
Source/WebCore/css/RuleSet.h
Source/WebCore/css/SelectorChecker.cpp
Source/WebCore/css/SelectorPseudoClassAndCompatibilityElementMap.in
Source/WebCore/cssjit/SelectorCompiler.cpp

index 68ea285..457dcfc 100644 (file)
@@ -1,3 +1,16 @@
+2015-10-07  Antti Koivisto  <antti@apple.com>
+
+        Implement :host pseudo class
+        https://bugs.webkit.org/show_bug.cgi?id=149440
+
+        Reviewed by Ryosuke Niwa.
+
+        * fast/shadow-dom/css-scoping-shadow-host-rule.html:
+
+            Fix and expand the test case.
+
+        * platform/mac/TestExpectations:
+
 2015-10-07  Brian Burg  <bburg@apple.com>
 
         Unreviewed, more Mac test gardening after r190629.
index a921b52..8d343e9 100644 (file)
@@ -8,41 +8,56 @@
 </head>
 <body>
     <style>
-        my-host, good-host, other-host, other-good-host {
+        my-host, my-host2, my-host3, my-host4 {
             display: block;
             width: 100px;
-            height: 50px;
+            height: 25px;
+        }
+        my-host2 {
+            background: green;
+        }
+        my-host3 {
             background: red;
+            color: green;
         }
-        good-host, other-good-host {
+        my-host4 {
             background: green;
+            color: green;
         }
     </style>
     <p>Test passes if you see a single 100px by 100px green box below.</p> 
     <my-host>
         <div>FAIL</div>
     </my-host>
+    <my-host2>
+        <div>FAIL</div>
+    </my-host2>
+    <my-host3>
+        <div>FAIL</div>
+    </my-host3>
     <div class="container">
-        <good-host>
+        <my-host4>
             <div>FAIL</div>
-        </good-host>
+        </my-host4>
     </div>
-    <other-host id="bar" class="foo" name="baz">
-        <div>FAIL</div>
-    </other-host>
-    <other-good-host>
-        <div class="child">FAIL</div>
-    </other-good-host>
     <script>
 
         try {
             var shadowHost = document.querySelector('my-host');
+            var shadowRoot = shadowHost.attachShadow({mode: 'open'});
+            shadowRoot.innerHTML = '<style> :host { color: green; background: green; } </style><div>FAIL</div>';
+
+            shadowHost = document.querySelector('my-host2');
+            shadowRoot = shadowHost.attachShadow({mode: 'open'});
+            shadowRoot.innerHTML = '<style> :host { color: red; background: red; } div { color: green }</style><div>FAIL</div>';
+
+            shadowHost = document.querySelector('my-host3');
             shadowRoot = shadowHost.attachShadow({mode: 'open'});
-            shadowRoot.innerHTML = '<style> :host { background: green; } </style>';
+            shadowRoot.innerHTML = '<style> :host { background: green !important; color: green !important; } </style><div>FAIL</div>';
 
-            shadowHost = document.querySelector('good-host');
+            shadowHost = document.querySelector('my-host4');
             shadowRoot = shadowHost.attachShadow({mode: 'open'});
-            shadowRoot.innerHTML = '<style> .container :host { background: red; } </style>';
+            shadowRoot.innerHTML = '<style> .container :host { background: red !important; } </style><div>FAIL</div>';
         } catch (exception) {
             document.body.appendChild(document.createTextNode(exception));
         }
index 25baec9..21265ee 100644 (file)
@@ -1325,7 +1325,6 @@ webkit.org/b/143258 [ Mavericks ] http/tests/cache/disk-cache/disk-cache-validat
 webkit.org/b/149128 fast/text/control-characters [ ImageOnlyFailure ]
 
 webkit.org/b/148695 fast/shadow-dom [ Pass ]
-webkit.org/b/149440 fast/shadow-dom/css-scoping-shadow-host-rule.html [ ImageOnlyFailure ]
 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 ]
index d0be42f..631d502 100644 (file)
@@ -1,3 +1,35 @@
+2015-10-07  Antti Koivisto  <antti@apple.com>
+
+        Implement :host pseudo class
+        https://bugs.webkit.org/show_bug.cgi?id=149440
+        rdar://problem/22731953
+
+        Reviewed by Ryosuke Niwa.
+
+        This implements the basic non-function :host syntax.
+
+        * css/CSSSelector.cpp:
+        (WebCore::CSSSelector::selectorText):
+        * css/CSSSelector.h:
+        * css/ElementRuleCollector.cpp:
+        (WebCore::ElementRuleCollector::matchAuthorRules):
+        (WebCore::ElementRuleCollector::matchHostPseudoClassRules):
+        (WebCore::ElementRuleCollector::matchUserRules):
+        * css/ElementRuleCollector.h:
+        * css/RuleSet.cpp:
+        (WebCore::computeMatchBasedOnRuleHash):
+        (WebCore::RuleSet::addRule):
+        * css/RuleSet.h:
+        (WebCore::RuleSet::cuePseudoRules):
+        (WebCore::RuleSet::hostPseudoClassRules):
+        (WebCore::RuleSet::focusPseudoClassRules):
+        (WebCore::RuleSet::universalRules):
+        * css/SelectorChecker.cpp:
+        (WebCore::SelectorChecker::checkOne):
+        * css/SelectorPseudoClassAndCompatibilityElementMap.in:
+        * cssjit/SelectorCompiler.cpp:
+        (WebCore::SelectorCompiler::addPseudoClassType):
+
 2015-10-07  Nan Wang  <n_wang@apple.com>
 
         AX: ARIA 1.1 @aria-placeholder
index 440e89c..f775c12 100644 (file)
@@ -632,6 +632,11 @@ String CSSSelector::selectorText(const String& rightSide) const
             case CSSSelector::PseudoClassWindowInactive:
                 str.appendLiteral(":window-inactive");
                 break;
+#if ENABLE(SHADOW_DOM)
+            case CSSSelector::PseudoClassHost:
+                str.appendLiteral(":host");
+                break;
+#endif
             case CSSSelector::PseudoClassUnknown:
                 ASSERT_NOT_REACHED();
             }
index 9808e71..e9f1d17 100644 (file)
@@ -159,6 +159,9 @@ namespace WebCore {
             PseudoClassDir,
             PseudoClassRole,
 #endif
+#if ENABLE(SHADOW_DOM)
+            PseudoClassHost,
+#endif
         };
 
         enum PseudoElementType {
index 16f03b8..9228510 100644 (file)
@@ -188,6 +188,11 @@ void ElementRuleCollector::sortAndTransferMatchedRules()
 
 void ElementRuleCollector::matchAuthorRules(bool includeEmptyRules)
 {
+#if ENABLE(SHADOW_DOM)
+    if (m_element.shadowRoot())
+        matchHostPseudoClassRules(includeEmptyRules);
+#endif
+
     clearMatchedRules();
     m_result.ranges.lastAuthorRule = m_result.matchedProperties().size() - 1;
 
@@ -200,6 +205,29 @@ void ElementRuleCollector::matchAuthorRules(bool includeEmptyRules)
     sortAndTransferMatchedRules();
 }
 
+#if ENABLE(SHADOW_DOM)
+void ElementRuleCollector::matchHostPseudoClassRules(bool includeEmptyRules)
+{
+    ASSERT(m_element.shadowRoot());
+    auto& shadowAuthorStyle = *m_element.shadowRoot()->styleResolver().ruleSets().authorStyle();
+    auto& shadowHostRules = shadowAuthorStyle.hostPseudoClassRules();
+    if (shadowHostRules.isEmpty())
+        return;
+
+    clearMatchedRules();
+    m_result.ranges.lastAuthorRule = m_result.matchedProperties().size() - 1;
+
+    auto ruleRange = m_result.ranges.authorRuleRange();
+    MatchRequest matchRequest(&shadowAuthorStyle, includeEmptyRules);
+    collectMatchingRulesForList(&shadowHostRules, matchRequest, ruleRange);
+
+    // We just sort the host rules before other author rules. This matches the current vague spec language
+    // but is not necessarily exactly what is needed.
+    // FIXME: Match the spec when it is finalized.
+    sortAndTransferMatchedRules();
+}
+#endif
+
 void ElementRuleCollector::matchUserRules(bool includeEmptyRules)
 {
     if (!m_ruleSets.userStyle())
index abf922c..c3b0259 100644 (file)
@@ -77,6 +77,9 @@ private:
     void addElementStyleProperties(const StyleProperties*, bool isCacheable = true);
 
     void matchUARules(RuleSet*);
+#if ENABLE(SHADOW_DOM)
+    void matchHostPseudoClassRules(bool includeEmptyRules);
+#endif
 
     void collectMatchingRules(const MatchRequest&, StyleResolver::RuleRange&);
     void collectMatchingRulesForRegion(const MatchRequest&, StyleResolver::RuleRange&);
index 472a02d..b4af108 100644 (file)
@@ -70,6 +70,10 @@ static inline MatchBasedOnRuleHash computeMatchBasedOnRuleHash(const CSSSelector
     }
     if (SelectorChecker::isCommonPseudoClassSelector(&selector))
         return MatchBasedOnRuleHash::ClassB;
+#if ENABLE(SHADOW_DOM)
+    if (selector.match() == CSSSelector::PseudoClass && selector.pseudoClassType() == CSSSelector::PseudoClassHost)
+        return MatchBasedOnRuleHash::ClassB;
+#endif
     if (selector.match() == CSSSelector::Id)
         return MatchBasedOnRuleHash::ClassA;
     if (selector.match() == CSSSelector::Class)
@@ -258,6 +262,12 @@ void RuleSet::addRule(StyleRule* rule, unsigned selectorIndex, AddRuleFlags addR
             }
         }
 
+#if ENABLE(SHADOW_DOM)
+        if (selector->match() == CSSSelector::PseudoClass && selector->pseudoClassType() == CSSSelector::PseudoClassHost) {
+            m_hostPseudoClassRules.append(ruleData);
+            return;
+        }
+#endif
         if (selector->relation() != CSSSelector::SubSelector)
             break;
         selector = selector->tagHistory();
index 320cc7d..2c7454e 100644 (file)
@@ -179,6 +179,9 @@ public:
 #if ENABLE(VIDEO_TRACK)
     const RuleDataVector* cuePseudoRules() const { return &m_cuePseudoRules; }
 #endif
+#if ENABLE(SHADOW_DOM)
+    const RuleDataVector& hostPseudoClassRules() const { return m_hostPseudoClassRules; }
+#endif
     const RuleDataVector* focusPseudoClassRules() const { return &m_focusPseudoClassRules; }
     const RuleDataVector* universalRules() const { return &m_universalRules; }
 
@@ -202,6 +205,9 @@ private:
 #if ENABLE(VIDEO_TRACK)
     RuleDataVector m_cuePseudoRules;
 #endif
+#if ENABLE(SHADOW_DOM)
+    RuleDataVector m_hostPseudoClassRules;
+#endif
     RuleDataVector m_focusPseudoClassRules;
     RuleDataVector m_universalRules;
     Vector<StyleRulePage*> m_pageRules;
index 9d153b1..311b2b2 100644 (file)
@@ -1013,7 +1013,11 @@ bool SelectorChecker::checkOne(const CheckingContextWithStatus& context, PseudoI
                     return true;
                 break;
             }
-
+#if ENABLE(SHADOW_DOM)
+        case CSSSelector::PseudoClassHost:
+            // :host matches based on context. Cases that reach selector checker don't match.
+            return false;
+#endif
         case CSSSelector::PseudoClassWindowInactive:
             return isWindowInactive(element);
 
index e420ae3..afb1bb6 100644 (file)
@@ -819,7 +819,11 @@ static inline FunctionType addPseudoClassType(const CSSSelector& selector, Selec
 
             return functionType;
         }
-
+#if ENABLE(SHADOW_DOM)
+    case CSSSelector::PseudoClassHost:
+        // :host matches based on context. Cases that reach selector checker don't match.
+        return FunctionType::CannotMatchAnything;
+#endif
     case CSSSelector::PseudoClassUnknown:
         ASSERT_NOT_REACHED();
         return FunctionType::CannotMatchAnything;