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

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

Issue 1192983003: CSS Custom Properties (Variables) (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Use m_unit Created 5 years, 1 month 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/CSSCustomPropertyDeclaration.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/CSSAtRuleID.h" 16 #include "core/css/parser/CSSAtRuleID.h"
16 #include "core/css/parser/CSSParserObserver.h" 17 #include "core/css/parser/CSSParserObserver.h"
17 #include "core/css/parser/CSSParserObserverWrapper.h" 18 #include "core/css/parser/CSSParserObserverWrapper.h"
18 #include "core/css/parser/CSSParserSelector.h" 19 #include "core/css/parser/CSSParserSelector.h"
19 #include "core/css/parser/CSSPropertyParser.h" 20 #include "core/css/parser/CSSPropertyParser.h"
20 #include "core/css/parser/CSSSelectorParser.h" 21 #include "core/css/parser/CSSSelectorParser.h"
21 #include "core/css/parser/CSSSupportsParser.h" 22 #include "core/css/parser/CSSSupportsParser.h"
22 #include "core/css/parser/CSSTokenizer.h" 23 #include "core/css/parser/CSSTokenizer.h"
24 #include "core/css/parser/CSSVariableParser.h"
23 #include "core/css/parser/MediaQueryParser.h" 25 #include "core/css/parser/MediaQueryParser.h"
24 #include "core/dom/Document.h" 26 #include "core/dom/Document.h"
25 #include "core/dom/Element.h" 27 #include "core/dom/Element.h"
26 #include "core/frame/UseCounter.h" 28 #include "core/frame/UseCounter.h"
27 #include "platform/TraceEvent.h" 29 #include "platform/TraceEvent.h"
28 #include "wtf/BitArray.h" 30 #include "wtf/BitArray.h"
29 31
30 namespace blink { 32 namespace blink {
31 33
32 CSSParserImpl::CSSParserImpl(const CSSParserContext& context, StyleSheetContents * styleSheet) 34 CSSParserImpl::CSSParserImpl(const CSSParserContext& context, StyleSheetContents * styleSheet)
(...skipping 16 matching lines...) Expand all
49 return declaration->addParsedProperties(parser.m_parsedProperties); 51 return declaration->addParsedProperties(parser.m_parsedProperties);
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();
60 if (seenProperties.get(propertyIDIndex)) 62 // All custom properties use the same CSSPropertyID so we can't remove r epeated definitions
61 continue; 63 if (property.id() != CSSPropertyVariable) {
62 seenProperties.set(propertyIDIndex); 64 if (seenProperties.get(propertyIDIndex))
65 continue;
66 seenProperties.set(propertyIDIndex);
67 }
63 output[--unusedEntries] = property; 68 output[--unusedEntries] = property;
64 } 69 }
65 } 70 }
66 71
67 static PassRefPtrWillBeRawPtr<ImmutableStylePropertySet> createStylePropertySet( WillBeHeapVector<CSSProperty, 256>& parsedProperties, CSSParserMode mode) 72 static PassRefPtrWillBeRawPtr<ImmutableStylePropertySet> createStylePropertySet( WillBeHeapVector<CSSProperty, 256>& parsedProperties, CSSParserMode mode)
68 { 73 {
69 BitArray<numCSSProperties> seenProperties; 74 BitArray<numCSSProperties> seenProperties;
70 size_t unusedEntries = parsedProperties.size(); 75 size_t unusedEntries = parsedProperties.size();
71 WillBeHeapVector<CSSProperty, 256> results(unusedEntries); 76 WillBeHeapVector<CSSProperty, 256> results(unusedEntries);
72 77
(...skipping 592 matching lines...) Expand 10 before | Expand all | Expand 10 after
665 m_observerWrapper->yieldCommentsBefore(range); 670 m_observerWrapper->yieldCommentsBefore(range);
666 m_observerWrapper->observer().endRuleBody(m_observerWrapper->endOffset(r ange)); 671 m_observerWrapper->observer().endRuleBody(m_observerWrapper->endOffset(r ange));
667 } 672 }
668 } 673 }
669 674
670 void CSSParserImpl::consumeDeclaration(CSSParserTokenRange range, StyleRule::Typ e ruleType) 675 void CSSParserImpl::consumeDeclaration(CSSParserTokenRange range, StyleRule::Typ e ruleType)
671 { 676 {
672 CSSParserTokenRange rangeCopy = range; // For inspector callbacks 677 CSSParserTokenRange rangeCopy = range; // For inspector callbacks
673 678
674 ASSERT(range.peek().type() == IdentToken); 679 ASSERT(range.peek().type() == IdentToken);
675 CSSPropertyID unresolvedProperty = range.consumeIncludingWhitespace().parseA sUnresolvedCSSPropertyID(); 680 const CSSParserToken& token = range.consumeIncludingWhitespace();
681 CSSPropertyID unresolvedProperty = token.parseAsUnresolvedCSSPropertyID();
676 if (range.consume().type() != ColonToken) 682 if (range.consume().type() != ColonToken)
677 return; // Parse error 683 return; // Parse error
678 684
679 bool important = false; 685 bool important = false;
680 const CSSParserToken* declarationValueEnd = range.end(); 686 const CSSParserToken* declarationValueEnd = range.end();
681 const CSSParserToken* last = range.end() - 1; 687 const CSSParserToken* last = range.end() - 1;
682 while (last->type() == WhitespaceToken) 688 while (last->type() == WhitespaceToken)
683 --last; 689 --last;
684 if (last->type() == IdentToken && last->valueEqualsIgnoringCase("important") ) { 690 if (last->type() == IdentToken && last->valueEqualsIgnoringCase("important") ) {
685 --last; 691 --last;
686 while (last->type() == WhitespaceToken) 692 while (last->type() == WhitespaceToken)
687 --last; 693 --last;
688 if (last->type() == DelimiterToken && last->delimiter() == '!') { 694 if (last->type() == DelimiterToken && last->delimiter() == '!') {
689 important = true; 695 important = true;
690 declarationValueEnd = last; 696 declarationValueEnd = last;
691 } 697 }
692 } 698 }
699 if (RuntimeEnabledFeatures::cssVariablesEnabled() && unresolvedProperty == C SSPropertyInvalid && CSSVariableParser::isValidVariableName(token)) {
700 AtomicString variableName = token.value();
701 consumeVariableDeclarationValue(range.makeSubRange(&range.peek(), declar ationValueEnd), variableName, important);
702 return;
703 }
693 704
694 if (important && (ruleType == StyleRule::FontFace || ruleType == StyleRule:: Keyframes)) 705 if (important && (ruleType == StyleRule::FontFace || ruleType == StyleRule:: Keyframes))
695 return; 706 return;
696 707
697 if (m_observerWrapper && ruleType == StyleRule::Style) { 708 if (m_observerWrapper && ruleType == StyleRule::Style) {
698 size_t propertiesCount = m_parsedProperties.size(); 709 size_t propertiesCount = m_parsedProperties.size();
699 if (unresolvedProperty != CSSPropertyInvalid) 710 if (unresolvedProperty != CSSPropertyInvalid)
700 consumeDeclarationValue(range.makeSubRange(&range.peek(), declaratio nValueEnd), unresolvedProperty, important, ruleType); 711 consumeDeclarationValue(range.makeSubRange(&range.peek(), declaratio nValueEnd), unresolvedProperty, important, ruleType);
701 m_observerWrapper->observer().observeProperty( 712 m_observerWrapper->observer().observeProperty(
702 m_observerWrapper->startOffset(rangeCopy), m_observerWrapper->endOff set(rangeCopy), 713 m_observerWrapper->startOffset(rangeCopy), m_observerWrapper->endOff set(rangeCopy),
703 important, m_parsedProperties.size() != propertiesCount); 714 important, m_parsedProperties.size() != propertiesCount);
704 return; 715 return;
705 } 716 }
706 717
707 if (unresolvedProperty == CSSPropertyInvalid) 718 if (unresolvedProperty == CSSPropertyInvalid)
708 return; 719 return;
709 720
710 consumeDeclarationValue(range.makeSubRange(&range.peek(), declarationValueEn d), unresolvedProperty, important, ruleType); 721 consumeDeclarationValue(range.makeSubRange(&range.peek(), declarationValueEn d), unresolvedProperty, important, ruleType);
711 } 722 }
712 723
724 void CSSParserImpl::consumeVariableDeclarationValue(CSSParserTokenRange range, c onst AtomicString& variableName, bool important)
725 {
726 if (RefPtrWillBeRawPtr<CSSCustomPropertyDeclaration> value = CSSVariablePars er::parseDeclarationValue(variableName, range))
727 m_parsedProperties.append(CSSProperty(CSSPropertyVariable, value.release (), important));
728 }
729
713 void CSSParserImpl::consumeDeclarationValue(CSSParserTokenRange range, CSSProper tyID unresolvedProperty, bool important, StyleRule::Type ruleType) 730 void CSSParserImpl::consumeDeclarationValue(CSSParserTokenRange range, CSSProper tyID unresolvedProperty, bool important, StyleRule::Type ruleType)
714 { 731 {
715 CSSPropertyParser::parseValue(unresolvedProperty, important, range, m_contex t, m_parsedProperties, ruleType); 732 CSSPropertyParser::parseValue(unresolvedProperty, important, range, m_contex t, m_parsedProperties, ruleType);
716 } 733 }
717 734
718 PassOwnPtr<Vector<double>> CSSParserImpl::consumeKeyframeKeyList(CSSParserTokenR ange range) 735 PassOwnPtr<Vector<double>> CSSParserImpl::consumeKeyframeKeyList(CSSParserTokenR ange range)
719 { 736 {
720 OwnPtr<Vector<double>> result = adoptPtr(new Vector<double>); 737 OwnPtr<Vector<double>> result = adoptPtr(new Vector<double>);
721 while (true) { 738 while (true) {
722 range.consumeWhitespace(); 739 range.consumeWhitespace();
723 const CSSParserToken& token = range.consumeIncludingWhitespace(); 740 const CSSParserToken& token = range.consumeIncludingWhitespace();
724 if (token.type() == PercentageToken && token.numericValue() >= 0 && toke n.numericValue() <= 100) 741 if (token.type() == PercentageToken && token.numericValue() >= 0 && toke n.numericValue() <= 100)
725 result->append(token.numericValue() / 100); 742 result->append(token.numericValue() / 100);
726 else if (token.type() == IdentToken && token.valueEqualsIgnoringCase("fr om")) 743 else if (token.type() == IdentToken && token.valueEqualsIgnoringCase("fr om"))
727 result->append(0); 744 result->append(0);
728 else if (token.type() == IdentToken && token.valueEqualsIgnoringCase("to ")) 745 else if (token.type() == IdentToken && token.valueEqualsIgnoringCase("to "))
729 result->append(1); 746 result->append(1);
730 else 747 else
731 return nullptr; // Parser error, invalid value in keyframe selector 748 return nullptr; // Parser error, invalid value in keyframe selector
732 if (range.atEnd()) 749 if (range.atEnd())
733 return result.release(); 750 return result.release();
734 if (range.consume().type() != CommaToken) 751 if (range.consume().type() != CommaToken)
735 return nullptr; // Parser error 752 return nullptr; // Parser error
736 } 753 }
737 } 754 }
738 755
739 } // namespace blink 756 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/css/parser/CSSParserImpl.h ('k') | third_party/WebKit/Source/core/css/parser/CSSParserToken.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698