source: webkit/trunk/Source/WebCore/css/CSSStyleRule.cpp

Last change on this file was 268741, checked in by Antti Koivisto, 5 years ago

Implement <forgiving-selector-list> for :is/:where
https://bugs.webkit.org/show_bug.cgi?id=217814
<rdar://problem/70384483>

Reviewed by Sam Weinig.

LayoutTests/imported/w3c:

  • web-platform-tests/css/selectors/is-where-error-recovery-expected.txt:
  • web-platform-tests/css/selectors/is-where-not-expected.txt:

Source/WebCore:

The spec now says :is/:where should parse as a forgiving selector list: https://drafts.csswg.org/selectors/#matches

  • contentextensions/ContentExtensionParser.cpp:

(WebCore::ContentExtensions::isValidCSSSelector):

  • css/CSSPageRule.cpp:

(WebCore::CSSPageRule::setSelectorText):

  • css/CSSSelectorList.h:

(WebCore::CSSSelectorList::isEmpty const):
(WebCore::CSSSelectorList::isValid const): Deleted.

An empty CSSSelectorList is now valid.
Represent invalid CSSSelectorList with Optional where needed.

  • css/CSSStyleRule.cpp:

(WebCore::CSSStyleRule::setSelectorText):

  • css/parser/CSSParser.cpp:

(WebCore::CSSParser::parseSelector):

  • css/parser/CSSParser.h:
  • css/parser/CSSParserImpl.cpp:

(WebCore::CSSParserImpl::consumePageRule):
(WebCore::CSSParserImpl::consumeStyleRule):

  • css/parser/CSSSelectorParser.cpp:

(WebCore::parseCSSSelector):
(WebCore::CSSSelectorParser::consumeComplexSelectorList):
(WebCore::CSSSelectorParser::consumeComplexForgivingSelectorList):

Add function for consuming <forgiving-selector-list>.

(WebCore::CSSSelectorParser::consumePseudo):

Use it for :is/:where.

  • css/parser/CSSSelectorParser.h:
  • dom/SelectorQuery.cpp:
  • inspector/InspectorStyleSheet.cpp:

(WebCore::isValidSelectorListString):

  • inspector/agents/InspectorDOMAgent.cpp:

(WebCore::InspectorDOMAgent::highlightSelector):

  • style/RuleSet.cpp:

(WebCore::Style::RuleSet::addStyleRule):

LayoutTests:

Update some selector parsing tests to log the results instead of PASS/FAIL as forgiving
parsing allows many combinations involving :is/:where.

Remove a few repetetive tests that don't add anything.

  • fast/css/parsing-css-attribute-case-insensitive-value-4-expected.txt: Removed.
  • fast/css/parsing-css-attribute-case-insensitive-value-4.html: Removed.
  • fast/css/parsing-css-is-5-expected.txt:
  • fast/css/parsing-css-is-5.html:
  • fast/css/parsing-css-is-6-expected.txt:
  • fast/css/parsing-css-is-6.html:
  • fast/css/parsing-css-is-7-expected.txt:
  • fast/css/parsing-css-is-7.html:
  • fast/css/parsing-css-is-8-expected.txt:
  • fast/css/parsing-css-is-8.html:
  • fast/css/parsing-css-matches-5-expected.txt: Removed.
  • fast/css/parsing-css-matches-5.html: Removed.
  • fast/css/parsing-css-matches-6-expected.txt: Removed.
  • fast/css/parsing-css-matches-6.html: Removed.
  • fast/css/parsing-css-matches-7-expected.txt: Removed.
  • fast/css/parsing-css-matches-7.html: Removed.
  • fast/css/parsing-css-matches-8-expected.txt: Removed.
  • fast/css/parsing-css-matches-8.html: Removed.
  • fast/css/parsing-css-not-5-expected.txt:
  • fast/css/parsing-css-not-5.html:
  • fast/css/parsing-css-not-6-expected.txt:
  • fast/css/parsing-css-not-6.html:
  • fast/css/parsing-css-not-7-expected.txt:
  • fast/css/parsing-css-not-7.html:
  • fast/css/parsing-css-not-8-expected.txt:
  • fast/css/parsing-css-not-8.html:
  • fast/css/parsing-css-not-9-expected.txt:
  • fast/css/parsing-css-not-9.html:
  • fast/css/parsing-css-nth-child-of-4-expected.txt:
  • fast/css/parsing-css-nth-child-of-4.html:
  • fast/css/parsing-css-nth-last-child-of-4-expected.txt:
  • fast/css/parsing-css-nth-last-child-of-4.html:
  • fast/selectors/invalid-functional-pseudo-class-expected.txt:
  • fast/selectors/invalid-functional-pseudo-class.html:
  • fast/selectors/pseudo-element-in-is-where-expected.html:
  • fast/selectors/pseudo-element-in-is-where.html:
  • Property svn:eol-style set to native
File size: 3.8 KB
Line 
1/*
2 * (C) 1999-2003 Lars Knoll ([email protected])
3 * (C) 2002-2003 Dirk Mueller ([email protected])
4 * Copyright (C) 2002, 2005, 2006, 2008, 2012, 2013 Apple Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22#include "config.h"
23#include "CSSStyleRule.h"
24
25#include "CSSParser.h"
26#include "CSSStyleSheet.h"
27#include "PropertySetCSSStyleDeclaration.h"
28#include "RuleSet.h"
29#include "StyleProperties.h"
30#include "StyleRule.h"
31#include <wtf/NeverDestroyed.h>
32#include <wtf/text/StringBuilder.h>
33
34namespace WebCore {
35
36typedef HashMap<const CSSStyleRule*, String> SelectorTextCache;
37static SelectorTextCache& selectorTextCache()
38{
39 static NeverDestroyed<SelectorTextCache> cache;
40 return cache;
41}
42
43CSSStyleRule::CSSStyleRule(StyleRule& styleRule, CSSStyleSheet* parent)
44 : CSSRule(parent)
45 , m_styleRule(styleRule)
46{
47}
48
49CSSStyleRule::~CSSStyleRule()
50{
51 if (m_propertiesCSSOMWrapper)
52 m_propertiesCSSOMWrapper->clearParentRule();
53
54 if (hasCachedSelectorText()) {
55 selectorTextCache().remove(this);
56 setHasCachedSelectorText(false);
57 }
58}
59
60CSSStyleDeclaration& CSSStyleRule::style()
61{
62 if (!m_propertiesCSSOMWrapper)
63 m_propertiesCSSOMWrapper = StyleRuleCSSStyleDeclaration::create(m_styleRule->mutableProperties(), *this);
64 return *m_propertiesCSSOMWrapper;
65}
66
67String CSSStyleRule::generateSelectorText() const
68{
69 return m_styleRule->selectorList().selectorsText();
70}
71
72String CSSStyleRule::selectorText() const
73{
74 if (hasCachedSelectorText()) {
75 ASSERT(selectorTextCache().contains(this));
76 return selectorTextCache().get(this);
77 }
78
79 ASSERT(!selectorTextCache().contains(this));
80 String text = generateSelectorText();
81 selectorTextCache().set(this, text);
82 setHasCachedSelectorText(true);
83 return text;
84}
85
86void CSSStyleRule::setSelectorText(const String& selectorText)
87{
88 // FIXME: getMatchedCSSRules can return CSSStyleRules that are missing parent stylesheet pointer while
89 // referencing StyleRules that are part of stylesheet. Disallow mutations in this case.
90 if (!parentStyleSheet())
91 return;
92
93 CSSParser p(parserContext());
94 auto selectorList = p.parseSelector(selectorText);
95 if (!selectorList)
96 return;
97
98 // NOTE: The selector list has to fit into RuleData. <http://webkit.org/b/118369>
99 if (selectorList->componentCount() > Style::RuleData::maximumSelectorComponentCount)
100 return;
101
102 CSSStyleSheet::RuleMutationScope mutationScope(this);
103
104 m_styleRule->wrapperAdoptSelectorList(WTFMove(*selectorList));
105
106 if (hasCachedSelectorText()) {
107 selectorTextCache().remove(this);
108 setHasCachedSelectorText(false);
109 }
110}
111
112String CSSStyleRule::cssText() const
113{
114 String declarations = m_styleRule->properties().asText();
115 if (declarations.isEmpty())
116 return makeString(selectorText(), " { }");
117 return makeString(selectorText(), " { ", declarations, " }");
118}
119
120void CSSStyleRule::reattach(StyleRuleBase& rule)
121{
122 m_styleRule = downcast<StyleRule>(rule);
123 if (m_propertiesCSSOMWrapper)
124 m_propertiesCSSOMWrapper->reattach(m_styleRule->mutableProperties());
125}
126
127} // namespace WebCore
Note: See TracBrowser for help on using the repository browser.