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

Side by Side Diff: Source/core/inspector/InspectorStyleSheet.cpp

Issue 216433003: RFC: Make BisonCSSParser purely stack allocated. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Review feedback Created 6 years, 8 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
« no previous file with comments | « Source/core/css/parser/CSSParserObserver.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010, Google Inc. All rights reserved. 2 * Copyright (C) 2010, Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 #include "wtf/OwnPtr.h" 52 #include "wtf/OwnPtr.h"
53 #include "wtf/PassOwnPtr.h" 53 #include "wtf/PassOwnPtr.h"
54 #include "wtf/text/StringBuilder.h" 54 #include "wtf/text/StringBuilder.h"
55 #include "wtf/text/TextPosition.h" 55 #include "wtf/text/TextPosition.h"
56 56
57 using WebCore::TypeBuilder::Array; 57 using WebCore::TypeBuilder::Array;
58 using WebCore::RuleSourceDataList; 58 using WebCore::RuleSourceDataList;
59 using WebCore::CSSRuleSourceData; 59 using WebCore::CSSRuleSourceData;
60 using WebCore::CSSStyleSheet; 60 using WebCore::CSSStyleSheet;
61 61
62 static PassOwnPtr<WebCore::BisonCSSParser> createCSSParser(WebCore::Document* do cument)
63 {
64 return adoptPtr(new WebCore::BisonCSSParser(document ? WebCore::CSSParserCon text(*document, 0) : WebCore::strictCSSParserContext()));
65 }
66
67 namespace { 62 namespace {
68 63
69 using namespace WebCore; 64 using namespace WebCore;
70 65
66 static CSSParserContext parserContextForDocument(Document *document)
67 {
68 return document ? CSSParserContext(*document, 0) : strictCSSParserContext();
69 }
70
71 class StyleSheetHandler FINAL : public CSSParserObserver { 71 class StyleSheetHandler FINAL : public CSSParserObserver {
72 public: 72 public:
73 StyleSheetHandler(const String& parsedText, Document* document, StyleSheetCo ntents* styleSheetContents, RuleSourceDataList* result) 73 StyleSheetHandler(const String& parsedText, Document* document, StyleSheetCo ntents* styleSheetContents, RuleSourceDataList* result)
74 : m_parsedText(parsedText) 74 : m_parsedText(parsedText)
75 , m_document(document) 75 , m_document(document)
76 , m_styleSheetContents(styleSheetContents) 76 , m_styleSheetContents(styleSheetContents)
77 , m_result(result) 77 , m_result(result)
78 , m_commentParser(parserContextForDocument(document))
78 , m_propertyRangeStart(UINT_MAX) 79 , m_propertyRangeStart(UINT_MAX)
79 , m_selectorRangeStart(UINT_MAX) 80 , m_selectorRangeStart(UINT_MAX)
80 , m_commentRangeStart(UINT_MAX) 81 , m_commentRangeStart(UINT_MAX)
81 { 82 {
82 ASSERT(m_result); 83 ASSERT(m_result);
83 } 84 }
84 85
85 private: 86 private:
86 virtual void startRuleHeader(CSSRuleSourceData::Type, unsigned) OVERRIDE; 87 virtual void startRuleHeader(CSSRuleSourceData::Type, unsigned) OVERRIDE;
87 virtual void endRuleHeader(unsigned) OVERRIDE; 88 virtual void endRuleHeader(unsigned) OVERRIDE;
(...skipping 11 matching lines...) Expand all
99 PassRefPtr<CSSRuleSourceData> popRuleData(); 100 PassRefPtr<CSSRuleSourceData> popRuleData();
100 template <typename CharacterType> inline void setRuleHeaderEnd(const Charact erType*, unsigned); 101 template <typename CharacterType> inline void setRuleHeaderEnd(const Charact erType*, unsigned);
101 void fixUnparsedPropertyRanges(CSSRuleSourceData*); 102 void fixUnparsedPropertyRanges(CSSRuleSourceData*);
102 103
103 const String& m_parsedText; 104 const String& m_parsedText;
104 Document* m_document; 105 Document* m_document;
105 StyleSheetContents* m_styleSheetContents; 106 StyleSheetContents* m_styleSheetContents;
106 RuleSourceDataList* m_result; 107 RuleSourceDataList* m_result;
107 RuleSourceDataList m_currentRuleDataStack; 108 RuleSourceDataList m_currentRuleDataStack;
108 RefPtr<CSSRuleSourceData> m_currentRuleData; 109 RefPtr<CSSRuleSourceData> m_currentRuleData;
109 OwnPtr<BisonCSSParser> m_commentParser; 110 BisonCSSParser m_commentParser;
110 unsigned m_propertyRangeStart; 111 unsigned m_propertyRangeStart;
111 unsigned m_selectorRangeStart; 112 unsigned m_selectorRangeStart;
112 unsigned m_commentRangeStart; 113 unsigned m_commentRangeStart;
113 }; 114 };
114 115
115 void StyleSheetHandler::startRuleHeader(CSSRuleSourceData::Type type, unsigned o ffset) 116 void StyleSheetHandler::startRuleHeader(CSSRuleSourceData::Type type, unsigned o ffset)
116 { 117 {
117 // Pop off data for a previous invalid rule. 118 // Pop off data for a previous invalid rule.
118 if (m_currentRuleData) 119 if (m_currentRuleData)
119 m_currentRuleDataStack.removeLast(); 120 m_currentRuleDataStack.removeLast();
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 commentText = commentText.substring(2); 327 commentText = commentText.substring(2);
327 328
328 // Require well-formed comments. 329 // Require well-formed comments.
329 if (!commentText.endsWith("*/")) 330 if (!commentText.endsWith("*/"))
330 return; 331 return;
331 commentText = commentText.substring(0, commentText.length() - 2).stripWhiteS pace(); 332 commentText = commentText.substring(0, commentText.length() - 2).stripWhiteS pace();
332 if (commentText.isEmpty()) 333 if (commentText.isEmpty())
333 return; 334 return;
334 335
335 // FIXME: Use the actual rule type rather than STYLE_RULE? 336 // FIXME: Use the actual rule type rather than STYLE_RULE?
336 if (!m_commentParser)
337 m_commentParser = createCSSParser(m_document);
338 RuleSourceDataList sourceData; 337 RuleSourceDataList sourceData;
339 338
340 // FIXME: Use another subclass of BisonCSSParser::SourceDataHandler and asse rt that 339 // FIXME: Use another subclass of BisonCSSParser::SourceDataHandler and asse rt that
341 // no comments are encountered (will not need m_document and m_styleSheetCon tents). 340 // no comments are encountered (will not need m_document and m_styleSheetCon tents).
342 StyleSheetHandler handler(commentText, m_document, m_styleSheetContents, &so urceData); 341 StyleSheetHandler handler(commentText, m_document, m_styleSheetContents, &so urceData);
343 RefPtrWillBeRawPtr<MutableStylePropertySet> tempMutableStyle = MutableStyleP ropertySet::create(); 342 RefPtrWillBeRawPtr<MutableStylePropertySet> tempMutableStyle = MutableStyleP ropertySet::create();
344 m_commentParser->parseDeclaration(tempMutableStyle.get(), commentText, &hand ler, m_styleSheetContents); 343 m_commentParser.parseDeclaration(tempMutableStyle.get(), commentText, &handl er, m_styleSheetContents);
345 Vector<CSSPropertySourceData>& commentPropertyData = sourceData.first()->sty leSourceData->propertyData; 344 Vector<CSSPropertySourceData>& commentPropertyData = sourceData.first()->sty leSourceData->propertyData;
346 if (commentPropertyData.size() != 1) 345 if (commentPropertyData.size() != 1)
347 return; 346 return;
348 CSSPropertySourceData& propertyData = commentPropertyData.at(0); 347 CSSPropertySourceData& propertyData = commentPropertyData.at(0);
349 if (propertyData.range.length() != commentText.length()) 348 if (propertyData.range.length() != commentText.length())
350 return; 349 return;
351 350
352 unsigned topRuleBodyRangeStart = m_currentRuleDataStack.last()->ruleBodyRang e.start; 351 unsigned topRuleBodyRangeStart = m_currentRuleDataStack.last()->ruleBodyRang e.start;
353 m_currentRuleDataStack.last()->styleSourceData->propertyData.append( 352 m_currentRuleDataStack.last()->styleSourceData->propertyData.append(
354 CSSPropertySourceData(propertyData.name, propertyData.value, false, true , true, SourceRange(startOffset - topRuleBodyRangeStart, offset - topRuleBodyRan geStart))); 353 CSSPropertySourceData(propertyData.name, propertyData.value, false, true , true, SourceRange(startOffset - topRuleBodyRangeStart, offset - topRuleBodyRan geStart)));
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 { 411 {
413 if (hasSourceData()) 412 if (hasSourceData())
414 return true; 413 return true;
415 414
416 if (!hasText()) 415 if (!hasText())
417 return false; 416 return false;
418 417
419 RefPtrWillBeRawPtr<StyleSheetContents> newStyleSheet = StyleSheetContents::c reate(strictCSSParserContext()); 418 RefPtrWillBeRawPtr<StyleSheetContents> newStyleSheet = StyleSheetContents::c reate(strictCSSParserContext());
420 OwnPtr<RuleSourceDataList> result = adoptPtr(new RuleSourceDataList()); 419 OwnPtr<RuleSourceDataList> result = adoptPtr(new RuleSourceDataList());
421 StyleSheetHandler handler(text(), m_pageStyleSheet->ownerDocument(), newStyl eSheet.get(), result.get()); 420 StyleSheetHandler handler(text(), m_pageStyleSheet->ownerDocument(), newStyl eSheet.get(), result.get());
422 createCSSParser(m_pageStyleSheet->ownerDocument())->parseSheet(newStyleSheet .get(), text(), TextPosition::minimumPosition(), &handler); 421 BisonCSSParser(parserContextForDocument(m_pageStyleSheet->ownerDocument())). parseSheet(newStyleSheet.get(), text(), TextPosition::minimumPosition(), &handle r);
423 setSourceData(result.release()); 422 setSourceData(result.release());
424 return hasSourceData(); 423 return hasSourceData();
425 } 424 }
426 425
427 void ParsedStyleSheet::setSourceData(PassOwnPtr<RuleSourceDataList> sourceData) 426 void ParsedStyleSheet::setSourceData(PassOwnPtr<RuleSourceDataList> sourceData)
428 { 427 {
429 if (!sourceData) { 428 if (!sourceData) {
430 m_sourceData.clear(); 429 m_sourceData.clear();
431 return; 430 return;
432 } 431 }
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
549 } 548 }
550 549
551 bool InspectorStyle::verifyPropertyText(const String& propertyText, bool canOmit Semicolon) 550 bool InspectorStyle::verifyPropertyText(const String& propertyText, bool canOmit Semicolon)
552 { 551 {
553 DEFINE_STATIC_LOCAL(String, bogusPropertyName, ("-webkit-boguz-propertee")); 552 DEFINE_STATIC_LOCAL(String, bogusPropertyName, ("-webkit-boguz-propertee"));
554 RefPtrWillBeRawPtr<MutableStylePropertySet> tempMutableStyle = MutableStyleP ropertySet::create(); 553 RefPtrWillBeRawPtr<MutableStylePropertySet> tempMutableStyle = MutableStyleP ropertySet::create();
555 RuleSourceDataList sourceData; 554 RuleSourceDataList sourceData;
556 RefPtrWillBeRawPtr<StyleSheetContents> styleSheetContents = StyleSheetConten ts::create(strictCSSParserContext()); 555 RefPtrWillBeRawPtr<StyleSheetContents> styleSheetContents = StyleSheetConten ts::create(strictCSSParserContext());
557 String declarationText = propertyText + (canOmitSemicolon ? ";" : " ") + bog usPropertyName + ": none"; 556 String declarationText = propertyText + (canOmitSemicolon ? ";" : " ") + bog usPropertyName + ": none";
558 StyleSheetHandler handler(declarationText, ownerDocument(), styleSheetConten ts.get(), &sourceData); 557 StyleSheetHandler handler(declarationText, ownerDocument(), styleSheetConten ts.get(), &sourceData);
559 createCSSParser(ownerDocument())->parseDeclaration(tempMutableStyle.get(), d eclarationText, &handler, styleSheetContents.get()); 558 BisonCSSParser(parserContextForDocument(ownerDocument())).parseDeclaration(t empMutableStyle.get(), declarationText, &handler, styleSheetContents.get());
560 Vector<CSSPropertySourceData>& propertyData = sourceData.first()->styleSourc eData->propertyData; 559 Vector<CSSPropertySourceData>& propertyData = sourceData.first()->styleSourc eData->propertyData;
561 unsigned propertyCount = propertyData.size(); 560 unsigned propertyCount = propertyData.size();
562 561
563 // At least one property + the bogus property added just above should be pre sent. 562 // At least one property + the bogus property added just above should be pre sent.
564 if (propertyCount < 2) 563 if (propertyCount < 2)
565 return false; 564 return false;
566 565
567 // Check for the proper propertyText termination (the parser could at least restore to the PROPERTY_NAME state). 566 // Check for the proper propertyText termination (the parser could at least restore to the PROPERTY_NAME state).
568 if (propertyData.at(propertyCount - 1).name != bogusPropertyName) 567 if (propertyData.at(propertyCount - 1).name != bogusPropertyName)
569 return false; 568 return false;
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after
987 String sheetText = m_parsedStyleSheet->text(); 986 String sheetText = m_parsedStyleSheet->text();
988 sheetText.replace(sourceData->ruleHeaderRange.start, sourceData->ruleHeaderR ange.length(), selector); 987 sheetText.replace(sourceData->ruleHeaderRange.start, sourceData->ruleHeaderR ange.length(), selector);
989 m_parsedStyleSheet->setText(sheetText); 988 m_parsedStyleSheet->setText(sheetText);
990 fireStyleSheetChanged(); 989 fireStyleSheetChanged();
991 return true; 990 return true;
992 } 991 }
993 992
994 static bool checkStyleRuleSelector(Document* document, const String& selector) 993 static bool checkStyleRuleSelector(Document* document, const String& selector)
995 { 994 {
996 CSSSelectorList selectorList; 995 CSSSelectorList selectorList;
997 createCSSParser(document)->parseSelector(selector, selectorList); 996 BisonCSSParser(parserContextForDocument(document)).parseSelector(selector, s electorList);
998 return selectorList.isValid(); 997 return selectorList.isValid();
999 } 998 }
1000 999
1001 CSSStyleRule* InspectorStyleSheet::addRule(const String& selector, ExceptionStat e& exceptionState) 1000 CSSStyleRule* InspectorStyleSheet::addRule(const String& selector, ExceptionStat e& exceptionState)
1002 { 1001 {
1003 if (!checkStyleRuleSelector(m_pageStyleSheet->ownerDocument(), selector)) { 1002 if (!checkStyleRuleSelector(m_pageStyleSheet->ownerDocument(), selector)) {
1004 exceptionState.throwDOMException(SyntaxError, "The selector '" + selecto r + "' could not be added."); 1003 exceptionState.throwDOMException(SyntaxError, "The selector '" + selecto r + "' could not be added.");
1005 return 0; 1004 return 0;
1006 } 1005 }
1007 1006
(...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after
1589 if (m_styleText.isEmpty()) { 1588 if (m_styleText.isEmpty()) {
1590 RefPtr<CSSRuleSourceData> result = CSSRuleSourceData::create(CSSRuleSour ceData::STYLE_RULE); 1589 RefPtr<CSSRuleSourceData> result = CSSRuleSourceData::create(CSSRuleSour ceData::STYLE_RULE);
1591 result->ruleBodyRange.start = 0; 1590 result->ruleBodyRange.start = 0;
1592 result->ruleBodyRange.end = 0; 1591 result->ruleBodyRange.end = 0;
1593 return result.release(); 1592 return result.release();
1594 } 1593 }
1595 1594
1596 RefPtrWillBeRawPtr<MutableStylePropertySet> tempDeclaration = MutableStylePr opertySet::create(); 1595 RefPtrWillBeRawPtr<MutableStylePropertySet> tempDeclaration = MutableStylePr opertySet::create();
1597 RuleSourceDataList ruleSourceDataResult; 1596 RuleSourceDataList ruleSourceDataResult;
1598 StyleSheetHandler handler(m_styleText, &m_element->document(), m_element->do cument().elementSheet().contents(), &ruleSourceDataResult); 1597 StyleSheetHandler handler(m_styleText, &m_element->document(), m_element->do cument().elementSheet().contents(), &ruleSourceDataResult);
1599 createCSSParser(&m_element->document())->parseDeclaration(tempDeclaration.ge t(), m_styleText, &handler, m_element->document().elementSheet().contents()); 1598 BisonCSSParser(parserContextForDocument(&m_element->document())).parseDeclar ation(tempDeclaration.get(), m_styleText, &handler, m_element->document().elemen tSheet().contents());
1600 return ruleSourceDataResult.first().release(); 1599 return ruleSourceDataResult.first().release();
1601 } 1600 }
1602 1601
1603 } // namespace WebCore 1602 } // namespace WebCore
1604 1603
OLDNEW
« no previous file with comments | « Source/core/css/parser/CSSParserObserver.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698