| 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 "config.h" | 5 #include "config.h" |
| 6 #include "core/css/parser/CSSParserImpl.h" | 6 #include "core/css/parser/CSSParserImpl.h" |
| 7 | 7 |
| 8 #include "core/css/CSSKeyframesRule.h" | 8 #include "core/css/CSSKeyframesRule.h" |
| 9 #include "core/css/CSSStyleSheet.h" | 9 #include "core/css/CSSStyleSheet.h" |
| 10 #include "core/css/CSSVariableValue.h" |
| 10 #include "core/css/StylePropertySet.h" | 11 #include "core/css/StylePropertySet.h" |
| 11 #include "core/css/StyleRuleImport.h" | 12 #include "core/css/StyleRuleImport.h" |
| 12 #include "core/css/StyleRuleKeyframe.h" | 13 #include "core/css/StyleRuleKeyframe.h" |
| 13 #include "core/css/StyleRuleNamespace.h" | 14 #include "core/css/StyleRuleNamespace.h" |
| 14 #include "core/css/StyleSheetContents.h" | 15 #include "core/css/StyleSheetContents.h" |
| 15 #include "core/css/parser/CSSParserObserver.h" | 16 #include "core/css/parser/CSSParserObserver.h" |
| 16 #include "core/css/parser/CSSParserObserverWrapper.h" | 17 #include "core/css/parser/CSSParserObserverWrapper.h" |
| 17 #include "core/css/parser/CSSParserValues.h" | 18 #include "core/css/parser/CSSParserValues.h" |
| 18 #include "core/css/parser/CSSPropertyParser.h" | 19 #include "core/css/parser/CSSPropertyParser.h" |
| 19 #include "core/css/parser/CSSSelectorParser.h" | 20 #include "core/css/parser/CSSSelectorParser.h" |
| 20 #include "core/css/parser/CSSSupportsParser.h" | 21 #include "core/css/parser/CSSSupportsParser.h" |
| 21 #include "core/css/parser/CSSTokenizer.h" | 22 #include "core/css/parser/CSSTokenizer.h" |
| 23 #include "core/css/parser/CSSVariableParser.h" |
| 22 #include "core/css/parser/MediaQueryParser.h" | 24 #include "core/css/parser/MediaQueryParser.h" |
| 23 #include "core/dom/Document.h" | 25 #include "core/dom/Document.h" |
| 24 #include "core/dom/Element.h" | 26 #include "core/dom/Element.h" |
| 25 #include "core/frame/UseCounter.h" | 27 #include "core/frame/UseCounter.h" |
| 26 #include "platform/TraceEvent.h" | 28 #include "platform/TraceEvent.h" |
| 27 #include "wtf/BitArray.h" | 29 #include "wtf/BitArray.h" |
| 28 | 30 |
| 29 namespace blink { | 31 namespace blink { |
| 30 | 32 |
| 31 CSSParserImpl::CSSParserImpl(const CSSParserContext& context, StyleSheetContents
* styleSheet) | 33 CSSParserImpl::CSSParserImpl(const CSSParserContext& context, StyleSheetContents
* styleSheet) |
| (...skipping 18 matching lines...) Expand all Loading... |
| 50 } | 52 } |
| 51 | 53 |
| 52 static inline void filterProperties(bool important, const WillBeHeapVector<CSSPr
operty, 256>& input, WillBeHeapVector<CSSProperty, 256>& output, size_t& unusedE
ntries, BitArray<numCSSProperties>& seenProperties) | 54 static inline void filterProperties(bool important, const WillBeHeapVector<CSSPr
operty, 256>& input, WillBeHeapVector<CSSProperty, 256>& output, size_t& unusedE
ntries, BitArray<numCSSProperties>& seenProperties) |
| 53 { | 55 { |
| 54 // Add properties in reverse order so that highest priority definitions are
reached first. Duplicate definitions can then be ignored when found. | 56 // Add properties in reverse order so that highest priority definitions are
reached first. Duplicate definitions can then be ignored when found. |
| 55 for (size_t i = input.size(); i--; ) { | 57 for (size_t i = input.size(); i--; ) { |
| 56 const CSSProperty& property = input[i]; | 58 const CSSProperty& property = input[i]; |
| 57 if (property.isImportant() != important) | 59 if (property.isImportant() != important) |
| 58 continue; | 60 continue; |
| 59 const unsigned propertyIDIndex = property.id() - firstCSSProperty; | 61 const unsigned propertyIDIndex = property.id() - firstCSSProperty; |
| 60 if (seenProperties.get(propertyIDIndex)) | 62 if (seenProperties.get(propertyIDIndex) && property.id() != CSSPropertyV
ariable) |
| 61 continue; | 63 continue; |
| 62 seenProperties.set(propertyIDIndex); | 64 seenProperties.set(propertyIDIndex); |
| 63 output[--unusedEntries] = property; | 65 output[--unusedEntries] = property; |
| 64 } | 66 } |
| 65 } | 67 } |
| 66 | 68 |
| 67 static PassRefPtrWillBeRawPtr<ImmutableStylePropertySet> createStylePropertySet(
WillBeHeapVector<CSSProperty, 256>& parsedProperties, CSSParserMode mode) | 69 static PassRefPtrWillBeRawPtr<ImmutableStylePropertySet> createStylePropertySet(
WillBeHeapVector<CSSProperty, 256>& parsedProperties, CSSParserMode mode) |
| 68 { | 70 { |
| 69 BitArray<numCSSProperties> seenProperties; | 71 BitArray<numCSSProperties> seenProperties; |
| 70 size_t unusedEntries = parsedProperties.size(); | 72 size_t unusedEntries = parsedProperties.size(); |
| (...skipping 578 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 649 m_observerWrapper->yieldCommentsBefore(range); | 651 m_observerWrapper->yieldCommentsBefore(range); |
| 650 m_observerWrapper->observer().endRuleBody(m_observerWrapper->endOffset(r
ange)); | 652 m_observerWrapper->observer().endRuleBody(m_observerWrapper->endOffset(r
ange)); |
| 651 } | 653 } |
| 652 } | 654 } |
| 653 | 655 |
| 654 void CSSParserImpl::consumeDeclaration(CSSParserTokenRange range, StyleRule::Typ
e ruleType) | 656 void CSSParserImpl::consumeDeclaration(CSSParserTokenRange range, StyleRule::Typ
e ruleType) |
| 655 { | 657 { |
| 656 CSSParserTokenRange rangeCopy = range; // For inspector callbacks | 658 CSSParserTokenRange rangeCopy = range; // For inspector callbacks |
| 657 | 659 |
| 658 ASSERT(range.peek().type() == IdentToken); | 660 ASSERT(range.peek().type() == IdentToken); |
| 659 CSSPropertyID unresolvedProperty = range.consumeIncludingWhitespace().parseA
sUnresolvedCSSPropertyID(); | 661 CSSParserToken token = range.consumeIncludingWhitespace(); |
| 662 CSSPropertyID unresolvedProperty = token.parseAsUnresolvedCSSPropertyID(); |
| 660 if (range.consume().type() != ColonToken) | 663 if (range.consume().type() != ColonToken) |
| 661 return; // Parse error | 664 return; // Parse error |
| 662 | 665 |
| 663 bool important = false; | 666 bool important = false; |
| 664 const CSSParserToken* declarationValueEnd = range.end(); | 667 const CSSParserToken* declarationValueEnd = range.end(); |
| 665 const CSSParserToken* last = range.end() - 1; | 668 const CSSParserToken* last = range.end() - 1; |
| 666 while (last->type() == WhitespaceToken) | 669 while (last->type() == WhitespaceToken) |
| 667 --last; | 670 --last; |
| 671 |
| 672 if (unresolvedProperty == CSSPropertyVariable) { |
| 673 ASSERT(RuntimeEnabledFeatures::cssVariablesEnabled()); |
| 674 AtomicString variableName = token.value(); |
| 675 m_styleSheet->parserSetUsesVariables(true); |
| 676 consumeVariableDeclarationValue(range.makeSubRange(&range.peek(), declar
ationValueEnd), variableName); |
| 677 return; |
| 678 } |
| 668 if (last->type() == IdentToken && last->valueEqualsIgnoringCase("important")
) { | 679 if (last->type() == IdentToken && last->valueEqualsIgnoringCase("important")
) { |
| 669 --last; | 680 --last; |
| 670 while (last->type() == WhitespaceToken) | 681 while (last->type() == WhitespaceToken) |
| 671 --last; | 682 --last; |
| 672 if (last->type() == DelimiterToken && last->delimiter() == '!') { | 683 if (last->type() == DelimiterToken && last->delimiter() == '!') { |
| 673 important = true; | 684 important = true; |
| 674 declarationValueEnd = last; | 685 declarationValueEnd = last; |
| 675 } | 686 } |
| 676 } | 687 } |
| 677 | 688 |
| 678 if (important && (ruleType == StyleRule::FontFace || ruleType == StyleRule::
Keyframes)) | 689 if (important && (ruleType == StyleRule::FontFace || ruleType == StyleRule::
Keyframes)) |
| 679 return; | 690 return; |
| 680 | 691 |
| 681 if (m_observerWrapper && ruleType == StyleRule::Style) { | 692 if (m_observerWrapper && ruleType == StyleRule::Style) { |
| 682 size_t propertiesCount = m_parsedProperties.size(); | 693 size_t propertiesCount = m_parsedProperties.size(); |
| 683 if (unresolvedProperty != CSSPropertyInvalid) | 694 if (unresolvedProperty != CSSPropertyInvalid) |
| 684 consumeDeclarationValue(range.makeSubRange(&range.peek(), declaratio
nValueEnd), unresolvedProperty, important, ruleType); | 695 consumeDeclarationValue(range.makeSubRange(&range.peek(), declaratio
nValueEnd), unresolvedProperty, important, ruleType); |
| 685 m_observerWrapper->observer().observeProperty( | 696 m_observerWrapper->observer().observeProperty( |
| 686 m_observerWrapper->startOffset(rangeCopy), m_observerWrapper->endOff
set(rangeCopy), | 697 m_observerWrapper->startOffset(rangeCopy), m_observerWrapper->endOff
set(rangeCopy), |
| 687 important, m_parsedProperties.size() != propertiesCount); | 698 important, m_parsedProperties.size() != propertiesCount); |
| 699 consumeDeclarationValue(range.makeSubRange(&range.peek(), declaratio
nValueEnd), unresolvedProperty, important, ruleType); |
| 688 return; | 700 return; |
| 689 } | 701 } |
| 690 | 702 |
| 691 if (unresolvedProperty == CSSPropertyInvalid) | 703 if (unresolvedProperty == CSSPropertyInvalid) |
| 692 return; | 704 return; |
| 693 | 705 |
| 694 consumeDeclarationValue(range.makeSubRange(&range.peek(), declarationValueEn
d), unresolvedProperty, important, ruleType); | 706 consumeDeclarationValue(range.makeSubRange(&range.peek(), declarationValueEn
d), unresolvedProperty, important, ruleType); |
| 695 } | 707 } |
| 696 | 708 |
| 709 void CSSParserImpl::consumeVariableDeclarationValue(CSSParserTokenRange range, c
onst AtomicString& variableName) |
| 710 { |
| 711 switch (CSSVariableParser::parseVariableDefinition(range)) { |
| 712 case CSSVariableParser::Invalid: |
| 713 return; |
| 714 case CSSVariableParser::Variable: |
| 715 m_parsedProperties.append(CSSProperty(CSSPropertyVariable, |
| 716 CSSVariableValue::create(variableName, CSSVariableData::create(range
, false)))); |
| 717 return; |
| 718 case CSSVariableParser::VariableWithReferences: |
| 719 m_parsedProperties.append(CSSProperty(CSSPropertyVariable, |
| 720 CSSVariableValue::create(variableName, CSSVariableData::create(range
, true)))); |
| 721 return; |
| 722 case CSSVariableParser::Initial: |
| 723 m_parsedProperties.append(CSSProperty(CSSPropertyVariable, |
| 724 CSSVariableValue::create(variableName, CSSValueInitial))); |
| 725 return; |
| 726 case CSSVariableParser::Inherit: |
| 727 m_parsedProperties.append(CSSProperty(CSSPropertyVariable, |
| 728 CSSVariableValue::create(variableName, CSSValueInherit))); |
| 729 return; |
| 730 } |
| 731 } |
| 732 |
| 697 void CSSParserImpl::consumeDeclarationValue(CSSParserTokenRange range, CSSProper
tyID unresolvedProperty, bool important, StyleRule::Type ruleType) | 733 void CSSParserImpl::consumeDeclarationValue(CSSParserTokenRange range, CSSProper
tyID unresolvedProperty, bool important, StyleRule::Type ruleType) |
| 698 { | 734 { |
| 699 bool usesRemUnits; | 735 bool usesRemUnits, usesVariables; |
| 700 CSSParserValueList valueList(range, usesRemUnits); | 736 CSSParserValueList valueList(range, usesRemUnits, usesVariables); |
| 701 if (!valueList.size()) | 737 if (!valueList.size()) |
| 702 return; // Parser error | 738 return; // Parser error |
| 703 if (usesRemUnits && m_styleSheet) | 739 if (m_styleSheet) { |
| 704 m_styleSheet->parserSetUsesRemUnits(true); | 740 if (usesRemUnits) |
| 741 m_styleSheet->parserSetUsesRemUnits(true); |
| 742 if (usesVariables) |
| 743 m_styleSheet->parserSetUsesVariables(true); |
| 744 } |
| 705 CSSPropertyParser::parseValue(unresolvedProperty, important, &valueList, m_c
ontext, m_parsedProperties, ruleType); | 745 CSSPropertyParser::parseValue(unresolvedProperty, important, &valueList, m_c
ontext, m_parsedProperties, ruleType); |
| 706 } | 746 } |
| 707 | 747 |
| 708 PassOwnPtr<Vector<double>> CSSParserImpl::consumeKeyframeKeyList(CSSParserTokenR
ange range) | 748 PassOwnPtr<Vector<double>> CSSParserImpl::consumeKeyframeKeyList(CSSParserTokenR
ange range) |
| 709 { | 749 { |
| 710 OwnPtr<Vector<double>> result = adoptPtr(new Vector<double>); | 750 OwnPtr<Vector<double>> result = adoptPtr(new Vector<double>); |
| 711 while (true) { | 751 while (true) { |
| 712 range.consumeWhitespace(); | 752 range.consumeWhitespace(); |
| 713 const CSSParserToken& token = range.consumeIncludingWhitespace(); | 753 const CSSParserToken& token = range.consumeIncludingWhitespace(); |
| 714 if (token.type() == PercentageToken && token.numericValue() >= 0 && toke
n.numericValue() <= 100) | 754 if (token.type() == PercentageToken && token.numericValue() >= 0 && toke
n.numericValue() <= 100) |
| 715 result->append(token.numericValue() / 100); | 755 result->append(token.numericValue() / 100); |
| 716 else if (token.type() == IdentToken && token.valueEqualsIgnoringCase("fr
om")) | 756 else if (token.type() == IdentToken && token.valueEqualsIgnoringCase("fr
om")) |
| 717 result->append(0); | 757 result->append(0); |
| 718 else if (token.type() == IdentToken && token.valueEqualsIgnoringCase("to
")) | 758 else if (token.type() == IdentToken && token.valueEqualsIgnoringCase("to
")) |
| 719 result->append(1); | 759 result->append(1); |
| 720 else | 760 else |
| 721 return nullptr; // Parser error, invalid value in keyframe selector | 761 return nullptr; // Parser error, invalid value in keyframe selector |
| 722 if (range.atEnd()) | 762 if (range.atEnd()) |
| 723 return result.release(); | 763 return result.release(); |
| 724 if (range.consume().type() != CommaToken) | 764 if (range.consume().type() != CommaToken) |
| 725 return nullptr; // Parser error | 765 return nullptr; // Parser error |
| 726 } | 766 } |
| 727 } | 767 } |
| 728 | 768 |
| 729 } // namespace blink | 769 } // namespace blink |
| OLD | NEW |