| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "core/css/parser/CSSParserImpl.h" | 5 #include "core/css/parser/CSSParserImpl.h" |
| 6 | 6 |
| 7 #include "core/css/CSSCustomIdentValue.h" | 7 #include "core/css/CSSCustomIdentValue.h" |
| 8 #include "core/css/CSSCustomPropertyDeclaration.h" | 8 #include "core/css/CSSCustomPropertyDeclaration.h" |
| 9 #include "core/css/CSSKeyframesRule.h" | 9 #include "core/css/CSSKeyframesRule.h" |
| 10 #include "core/css/CSSStyleSheet.h" | 10 #include "core/css/CSSStyleSheet.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "core/css/parser/CSSSelectorParser.h" | 21 #include "core/css/parser/CSSSelectorParser.h" |
| 22 #include "core/css/parser/CSSSupportsParser.h" | 22 #include "core/css/parser/CSSSupportsParser.h" |
| 23 #include "core/css/parser/CSSTokenizer.h" | 23 #include "core/css/parser/CSSTokenizer.h" |
| 24 #include "core/css/parser/CSSVariableParser.h" | 24 #include "core/css/parser/CSSVariableParser.h" |
| 25 #include "core/css/parser/MediaQueryParser.h" | 25 #include "core/css/parser/MediaQueryParser.h" |
| 26 #include "core/dom/Document.h" | 26 #include "core/dom/Document.h" |
| 27 #include "core/dom/Element.h" | 27 #include "core/dom/Element.h" |
| 28 #include "core/frame/Deprecation.h" | 28 #include "core/frame/Deprecation.h" |
| 29 #include "core/frame/UseCounter.h" | 29 #include "core/frame/UseCounter.h" |
| 30 #include "platform/TraceEvent.h" | 30 #include "platform/TraceEvent.h" |
| 31 #include "wtf/PtrUtil.h" | |
| 32 #include <bitset> | 31 #include <bitset> |
| 33 #include <memory> | |
| 34 | 32 |
| 35 namespace blink { | 33 namespace blink { |
| 36 | 34 |
| 37 CSSParserImpl::CSSParserImpl(const CSSParserContext& context, StyleSheetContents
* styleSheet) | 35 CSSParserImpl::CSSParserImpl(const CSSParserContext& context, StyleSheetContents
* styleSheet) |
| 38 : m_context(context) | 36 : m_context(context) |
| 39 , m_styleSheet(styleSheet) | 37 , m_styleSheet(styleSheet) |
| 40 , m_observerWrapper(nullptr) | 38 , m_observerWrapper(nullptr) |
| 41 { | 39 { |
| 42 } | 40 } |
| 43 | 41 |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 range.consume(); | 197 range.consume(); |
| 200 if (range.peek().type() != IdentToken) | 198 if (range.peek().type() != IdentToken) |
| 201 return CSSSelectorList(); | 199 return CSSSelectorList(); |
| 202 pseudo = range.consume().value().toAtomicString(); | 200 pseudo = range.consume().value().toAtomicString(); |
| 203 } | 201 } |
| 204 | 202 |
| 205 range.consumeWhitespace(); | 203 range.consumeWhitespace(); |
| 206 if (!range.atEnd()) | 204 if (!range.atEnd()) |
| 207 return CSSSelectorList(); // Parse error; extra tokens in @page selector | 205 return CSSSelectorList(); // Parse error; extra tokens in @page selector |
| 208 | 206 |
| 209 std::unique_ptr<CSSParserSelector> selector; | 207 OwnPtr<CSSParserSelector> selector; |
| 210 if (!typeSelector.isNull() && pseudo.isNull()) { | 208 if (!typeSelector.isNull() && pseudo.isNull()) { |
| 211 selector = CSSParserSelector::create(QualifiedName(nullAtom, typeSelecto
r, styleSheet->defaultNamespace())); | 209 selector = CSSParserSelector::create(QualifiedName(nullAtom, typeSelecto
r, styleSheet->defaultNamespace())); |
| 212 } else { | 210 } else { |
| 213 selector = CSSParserSelector::create(); | 211 selector = CSSParserSelector::create(); |
| 214 if (!pseudo.isNull()) { | 212 if (!pseudo.isNull()) { |
| 215 selector->setMatch(CSSSelector::PagePseudoClass); | 213 selector->setMatch(CSSSelector::PagePseudoClass); |
| 216 selector->updatePseudoType(pseudo.lower()); | 214 selector->updatePseudoType(pseudo.lower()); |
| 217 if (selector->pseudoType() == CSSSelector::PseudoUnknown) | 215 if (selector->pseudoType() == CSSSelector::PseudoUnknown) |
| 218 return CSSSelectorList(); | 216 return CSSSelectorList(); |
| 219 } | 217 } |
| 220 if (!typeSelector.isNull()) { | 218 if (!typeSelector.isNull()) { |
| 221 selector->prependTagSelector(QualifiedName(nullAtom, typeSelector, s
tyleSheet->defaultNamespace())); | 219 selector->prependTagSelector(QualifiedName(nullAtom, typeSelector, s
tyleSheet->defaultNamespace())); |
| 222 } | 220 } |
| 223 } | 221 } |
| 224 | 222 |
| 225 selector->setForPage(); | 223 selector->setForPage(); |
| 226 Vector<std::unique_ptr<CSSParserSelector>> selectorVector; | 224 Vector<OwnPtr<CSSParserSelector>> selectorVector; |
| 227 selectorVector.append(std::move(selector)); | 225 selectorVector.append(std::move(selector)); |
| 228 CSSSelectorList selectorList = CSSSelectorList::adoptSelectorVector(selector
Vector); | 226 CSSSelectorList selectorList = CSSSelectorList::adoptSelectorVector(selector
Vector); |
| 229 return selectorList; | 227 return selectorList; |
| 230 } | 228 } |
| 231 | 229 |
| 232 ImmutableStylePropertySet* CSSParserImpl::parseCustomPropertySet(CSSParserTokenR
ange range) | 230 ImmutableStylePropertySet* CSSParserImpl::parseCustomPropertySet(CSSParserTokenR
ange range) |
| 233 { | 231 { |
| 234 range.consumeWhitespace(); | 232 range.consumeWhitespace(); |
| 235 if (range.peek().type() != LeftBraceToken) | 233 if (range.peek().type() != LeftBraceToken) |
| 236 return nullptr; | 234 return nullptr; |
| 237 CSSParserTokenRange block = range.consumeBlock(); | 235 CSSParserTokenRange block = range.consumeBlock(); |
| 238 range.consumeWhitespace(); | 236 range.consumeWhitespace(); |
| 239 if (!range.atEnd()) | 237 if (!range.atEnd()) |
| 240 return nullptr; | 238 return nullptr; |
| 241 CSSParserImpl parser(strictCSSParserContext()); | 239 CSSParserImpl parser(strictCSSParserContext()); |
| 242 parser.consumeDeclarationList(block, StyleRule::Style); | 240 parser.consumeDeclarationList(block, StyleRule::Style); |
| 243 | 241 |
| 244 // Drop nested @apply rules. Seems nicer to do this here instead of making | 242 // Drop nested @apply rules. Seems nicer to do this here instead of making |
| 245 // a different StyleRule type | 243 // a different StyleRule type |
| 246 for (size_t i = parser.m_parsedProperties.size(); i--; ) { | 244 for (size_t i = parser.m_parsedProperties.size(); i--; ) { |
| 247 if (parser.m_parsedProperties[i].id() == CSSPropertyApplyAtRule) | 245 if (parser.m_parsedProperties[i].id() == CSSPropertyApplyAtRule) |
| 248 parser.m_parsedProperties.remove(i); | 246 parser.m_parsedProperties.remove(i); |
| 249 } | 247 } |
| 250 | 248 |
| 251 return createStylePropertySet(parser.m_parsedProperties, HTMLStandardMode); | 249 return createStylePropertySet(parser.m_parsedProperties, HTMLStandardMode); |
| 252 } | 250 } |
| 253 | 251 |
| 254 std::unique_ptr<Vector<double>> CSSParserImpl::parseKeyframeKeyList(const String
& keyList) | 252 PassOwnPtr<Vector<double>> CSSParserImpl::parseKeyframeKeyList(const String& key
List) |
| 255 { | 253 { |
| 256 return consumeKeyframeKeyList(CSSTokenizer::Scope(keyList).tokenRange()); | 254 return consumeKeyframeKeyList(CSSTokenizer::Scope(keyList).tokenRange()); |
| 257 } | 255 } |
| 258 | 256 |
| 259 bool CSSParserImpl::supportsDeclaration(CSSParserTokenRange& range) | 257 bool CSSParserImpl::supportsDeclaration(CSSParserTokenRange& range) |
| 260 { | 258 { |
| 261 ASSERT(m_parsedProperties.isEmpty()); | 259 ASSERT(m_parsedProperties.isEmpty()); |
| 262 consumeDeclaration(range, StyleRule::Style); | 260 consumeDeclaration(range, StyleRule::Style); |
| 263 bool result = !m_parsedProperties.isEmpty(); | 261 bool result = !m_parsedProperties.isEmpty(); |
| 264 m_parsedProperties.clear(); | 262 m_parsedProperties.clear(); |
| (...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 641 const CSSParserToken& ident = prelude.consumeIncludingWhitespace(); | 639 const CSSParserToken& ident = prelude.consumeIncludingWhitespace(); |
| 642 if (!prelude.atEnd() || !CSSVariableParser::isValidVariableName(ident)) | 640 if (!prelude.atEnd() || !CSSVariableParser::isValidVariableName(ident)) |
| 643 return; // Parse error, expected a single custom property name | 641 return; // Parse error, expected a single custom property name |
| 644 m_parsedProperties.append(CSSProperty( | 642 m_parsedProperties.append(CSSProperty( |
| 645 CSSPropertyApplyAtRule, | 643 CSSPropertyApplyAtRule, |
| 646 *CSSCustomIdentValue::create(ident.value().toString()))); | 644 *CSSCustomIdentValue::create(ident.value().toString()))); |
| 647 } | 645 } |
| 648 | 646 |
| 649 StyleRuleKeyframe* CSSParserImpl::consumeKeyframeStyleRule(CSSParserTokenRange p
relude, CSSParserTokenRange block) | 647 StyleRuleKeyframe* CSSParserImpl::consumeKeyframeStyleRule(CSSParserTokenRange p
relude, CSSParserTokenRange block) |
| 650 { | 648 { |
| 651 std::unique_ptr<Vector<double>> keyList = consumeKeyframeKeyList(prelude); | 649 OwnPtr<Vector<double>> keyList = consumeKeyframeKeyList(prelude); |
| 652 if (!keyList) | 650 if (!keyList) |
| 653 return nullptr; | 651 return nullptr; |
| 654 | 652 |
| 655 if (m_observerWrapper) { | 653 if (m_observerWrapper) { |
| 656 m_observerWrapper->observer().startRuleHeader(StyleRule::Keyframe, m_obs
erverWrapper->startOffset(prelude)); | 654 m_observerWrapper->observer().startRuleHeader(StyleRule::Keyframe, m_obs
erverWrapper->startOffset(prelude)); |
| 657 m_observerWrapper->observer().endRuleHeader(m_observerWrapper->endOffset
(prelude)); | 655 m_observerWrapper->observer().endRuleHeader(m_observerWrapper->endOffset
(prelude)); |
| 658 } | 656 } |
| 659 | 657 |
| 660 consumeDeclarationList(block, StyleRule::Keyframe); | 658 consumeDeclarationList(block, StyleRule::Keyframe); |
| 661 return StyleRuleKeyframe::create(std::move(keyList), createStylePropertySet(
m_parsedProperties, m_context.mode())); | 659 return StyleRuleKeyframe::create(std::move(keyList), createStylePropertySet(
m_parsedProperties, m_context.mode())); |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 798 { | 796 { |
| 799 if (CSSCustomPropertyDeclaration* value = CSSVariableParser::parseDeclaratio
nValue(variableName, range)) | 797 if (CSSCustomPropertyDeclaration* value = CSSVariableParser::parseDeclaratio
nValue(variableName, range)) |
| 800 m_parsedProperties.append(CSSProperty(CSSPropertyVariable, *value, impor
tant)); | 798 m_parsedProperties.append(CSSProperty(CSSPropertyVariable, *value, impor
tant)); |
| 801 } | 799 } |
| 802 | 800 |
| 803 void CSSParserImpl::consumeDeclarationValue(CSSParserTokenRange range, CSSProper
tyID unresolvedProperty, bool important, StyleRule::RuleType ruleType) | 801 void CSSParserImpl::consumeDeclarationValue(CSSParserTokenRange range, CSSProper
tyID unresolvedProperty, bool important, StyleRule::RuleType ruleType) |
| 804 { | 802 { |
| 805 CSSPropertyParser::parseValue(unresolvedProperty, important, range, m_contex
t, m_parsedProperties, ruleType); | 803 CSSPropertyParser::parseValue(unresolvedProperty, important, range, m_contex
t, m_parsedProperties, ruleType); |
| 806 } | 804 } |
| 807 | 805 |
| 808 std::unique_ptr<Vector<double>> CSSParserImpl::consumeKeyframeKeyList(CSSParserT
okenRange range) | 806 PassOwnPtr<Vector<double>> CSSParserImpl::consumeKeyframeKeyList(CSSParserTokenR
ange range) |
| 809 { | 807 { |
| 810 std::unique_ptr<Vector<double>> result = wrapUnique(new Vector<double>); | 808 OwnPtr<Vector<double>> result = adoptPtr(new Vector<double>); |
| 811 while (true) { | 809 while (true) { |
| 812 range.consumeWhitespace(); | 810 range.consumeWhitespace(); |
| 813 const CSSParserToken& token = range.consumeIncludingWhitespace(); | 811 const CSSParserToken& token = range.consumeIncludingWhitespace(); |
| 814 if (token.type() == PercentageToken && token.numericValue() >= 0 && toke
n.numericValue() <= 100) | 812 if (token.type() == PercentageToken && token.numericValue() >= 0 && toke
n.numericValue() <= 100) |
| 815 result->append(token.numericValue() / 100); | 813 result->append(token.numericValue() / 100); |
| 816 else if (token.type() == IdentToken && token.valueEqualsIgnoringASCIICas
e("from")) | 814 else if (token.type() == IdentToken && token.valueEqualsIgnoringASCIICas
e("from")) |
| 817 result->append(0); | 815 result->append(0); |
| 818 else if (token.type() == IdentToken && token.valueEqualsIgnoringASCIICas
e("to")) | 816 else if (token.type() == IdentToken && token.valueEqualsIgnoringASCIICas
e("to")) |
| 819 result->append(1); | 817 result->append(1); |
| 820 else | 818 else |
| 821 return nullptr; // Parser error, invalid value in keyframe selector | 819 return nullptr; // Parser error, invalid value in keyframe selector |
| 822 if (range.atEnd()) | 820 if (range.atEnd()) |
| 823 return result; | 821 return result; |
| 824 if (range.consume().type() != CommaToken) | 822 if (range.consume().type() != CommaToken) |
| 825 return nullptr; // Parser error | 823 return nullptr; // Parser error |
| 826 } | 824 } |
| 827 } | 825 } |
| 828 | 826 |
| 829 } // namespace blink | 827 } // namespace blink |
| OLD | NEW |