Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(306)

Side by Side Diff: Source/core/css/parser/CSSParserImpl.cpp

Issue 1192983003: CSS Custom Properties (Variables) (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: ToTed Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/CSSCustomVariableValue.h"
8 #include "core/css/CSSKeyframesRule.h" 9 #include "core/css/CSSKeyframesRule.h"
9 #include "core/css/CSSStyleSheet.h" 10 #include "core/css/CSSStyleSheet.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
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 585 matching lines...) Expand 10 before | Expand all | Expand 10 after
656 } 658 }
657 } 659 }
658 660
659 // Yield remaining comments 661 // Yield remaining comments
660 if (useObserver) { 662 if (useObserver) {
661 m_observerWrapper->yieldCommentsBefore(range); 663 m_observerWrapper->yieldCommentsBefore(range);
662 m_observerWrapper->observer().endRuleBody(m_observerWrapper->endOffset(r ange)); 664 m_observerWrapper->observer().endRuleBody(m_observerWrapper->endOffset(r ange));
663 } 665 }
664 } 666 }
665 667
668 template <typename CharacterType>
669 static bool isVariableDefinition(const CharacterType* propertyName, unsigned len gth)
Timothy Loh 2015/08/25 09:21:10 isCustomProperty
670 {
671 return (length >= 2 && propertyName[0] == '-' && propertyName[1] == '-');
672 }
673
674 inline bool isVariableDefinition(CSSParserToken token)
675 {
676 CSSParserString string = token.value();
677 return string.is8Bit() ? isVariableDefinition(string.characters8(), string.l ength()) : isVariableDefinition(string.characters16(), string.length());
678 }
679
666 void CSSParserImpl::consumeDeclaration(CSSParserTokenRange range, StyleRule::Typ e ruleType) 680 void CSSParserImpl::consumeDeclaration(CSSParserTokenRange range, StyleRule::Typ e ruleType)
667 { 681 {
668 CSSParserTokenRange rangeCopy = range; // For inspector callbacks 682 CSSParserTokenRange rangeCopy = range; // For inspector callbacks
669 683
670 ASSERT(range.peek().type() == IdentToken); 684 ASSERT(range.peek().type() == IdentToken);
671 CSSPropertyID unresolvedProperty = range.consumeIncludingWhitespace().parseA sUnresolvedCSSPropertyID(); 685 CSSParserToken token = range.consumeIncludingWhitespace();
Timothy Loh 2015/08/25 09:21:10 const CSSParserToken&
686 CSSPropertyID unresolvedProperty = token.parseAsUnresolvedCSSPropertyID();
672 if (range.consume().type() != ColonToken) 687 if (range.consume().type() != ColonToken)
673 return; // Parse error 688 return; // Parse error
674 689
675 bool important = false; 690 bool important = false;
676 const CSSParserToken* declarationValueEnd = range.end(); 691 const CSSParserToken* declarationValueEnd = range.end();
677 const CSSParserToken* last = range.end() - 1; 692 const CSSParserToken* last = range.end() - 1;
678 while (last->type() == WhitespaceToken) 693 while (last->type() == WhitespaceToken)
679 --last; 694 --last;
695
Timothy Loh 2015/08/25 09:21:10 unrelated (this block is about consuming !importan
680 if (last->type() == IdentToken && last->valueEqualsIgnoringCase("important") ) { 696 if (last->type() == IdentToken && last->valueEqualsIgnoringCase("important") ) {
681 --last; 697 --last;
682 while (last->type() == WhitespaceToken) 698 while (last->type() == WhitespaceToken)
683 --last; 699 --last;
684 if (last->type() == DelimiterToken && last->delimiter() == '!') { 700 if (last->type() == DelimiterToken && last->delimiter() == '!') {
685 important = true; 701 important = true;
686 declarationValueEnd = last; 702 declarationValueEnd = last;
687 } 703 }
688 } 704 }
705 if (unresolvedProperty == CSSPropertyInvalid && isVariableDefinition(token)) {
706 ASSERT(RuntimeEnabledFeatures::cssVariablesEnabled());
707 AtomicString variableName = token.value();
708 consumeVariableDeclarationValue(range.makeSubRange(&range.peek(), declar ationValueEnd), variableName, important);
709 return;
710 }
689 711
690 if (important && (ruleType == StyleRule::FontFace || ruleType == StyleRule:: Keyframes)) 712 if (important && (ruleType == StyleRule::FontFace || ruleType == StyleRule:: Keyframes))
691 return; 713 return;
692 714
693 if (m_observerWrapper && ruleType == StyleRule::Style) { 715 if (m_observerWrapper && ruleType == StyleRule::Style) {
694 size_t propertiesCount = m_parsedProperties.size(); 716 size_t propertiesCount = m_parsedProperties.size();
695 if (unresolvedProperty != CSSPropertyInvalid) 717 if (unresolvedProperty != CSSPropertyInvalid)
696 consumeDeclarationValue(range.makeSubRange(&range.peek(), declaratio nValueEnd), unresolvedProperty, important, ruleType); 718 consumeDeclarationValue(range.makeSubRange(&range.peek(), declaratio nValueEnd), unresolvedProperty, important, ruleType);
697 m_observerWrapper->observer().observeProperty( 719 m_observerWrapper->observer().observeProperty(
698 m_observerWrapper->startOffset(rangeCopy), m_observerWrapper->endOff set(rangeCopy), 720 m_observerWrapper->startOffset(rangeCopy), m_observerWrapper->endOff set(rangeCopy),
699 important, m_parsedProperties.size() != propertiesCount); 721 important, m_parsedProperties.size() != propertiesCount);
700 return; 722 return;
701 } 723 }
702 724
703 if (unresolvedProperty == CSSPropertyInvalid) 725 if (unresolvedProperty == CSSPropertyInvalid)
704 return; 726 return;
705 727
706 consumeDeclarationValue(range.makeSubRange(&range.peek(), declarationValueEn d), unresolvedProperty, important, ruleType); 728 consumeDeclarationValue(range.makeSubRange(&range.peek(), declarationValueEn d), unresolvedProperty, important, ruleType);
707 } 729 }
708 730
731 void CSSParserImpl::consumeVariableDeclarationValue(CSSParserTokenRange range, c onst AtomicString& variableName, bool important)
732 {
733 if (PassRefPtrWillBeRawPtr<CSSCustomVariableValue> value = CSSVariableParser ::parseDeclarationValue(variableName, range))
734 m_parsedProperties.append(CSSProperty(CSSPropertyVariable, value, import ant));
735 }
736
709 void CSSParserImpl::consumeDeclarationValue(CSSParserTokenRange range, CSSProper tyID unresolvedProperty, bool important, StyleRule::Type ruleType) 737 void CSSParserImpl::consumeDeclarationValue(CSSParserTokenRange range, CSSProper tyID unresolvedProperty, bool important, StyleRule::Type ruleType)
710 { 738 {
711 CSSParserValueList valueList(range); 739 CSSParserValueList valueList(range);
712 if (!valueList.size()) 740 if (!valueList.size())
713 return; // Parser error 741 return; // Parser error
714 CSSPropertyParser::parseValue(unresolvedProperty, important, &valueList, m_c ontext, m_parsedProperties, ruleType); 742 CSSPropertyParser::parseValue(unresolvedProperty, important, &valueList, m_c ontext, m_parsedProperties, ruleType);
715 } 743 }
716 744
717 PassOwnPtr<Vector<double>> CSSParserImpl::consumeKeyframeKeyList(CSSParserTokenR ange range) 745 PassOwnPtr<Vector<double>> CSSParserImpl::consumeKeyframeKeyList(CSSParserTokenR ange range)
718 { 746 {
(...skipping 10 matching lines...) Expand all
729 else 757 else
730 return nullptr; // Parser error, invalid value in keyframe selector 758 return nullptr; // Parser error, invalid value in keyframe selector
731 if (range.atEnd()) 759 if (range.atEnd())
732 return result.release(); 760 return result.release();
733 if (range.consume().type() != CommaToken) 761 if (range.consume().type() != CommaToken)
734 return nullptr; // Parser error 762 return nullptr; // Parser error
735 } 763 }
736 } 764 }
737 765
738 } // namespace blink 766 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698