Changeset 121551 in webkit


Ignore:
Timestamp:
Jun 29, 2012, 5:06:11 AM (13 years ago)
Author:
apavlov@chromium.org
Message:

Web Inspector: Provide source data for all known rule types in CSSParser, except "keyframe" and "region"
https://bugs.webkit.org/show_bug.cgi?id=88420

Reviewed by Antti Koivisto.

This change transitions the CSS source code model from a flat list of style rules to a tree of all types of CSS rules
(some of them lack actual source code data), which is crucial to model-based CSS stylesheet source editing
(add/remove CSS rule) and navigation.
As a side effect, the CSS parsing performance on PerformanceTests/Parser/css-parser-yui.html is improved roughly by 2%:

  • originally: median= 282.051282051 runs/s, stdev= 1.51236798322 runs/s, min= 278.481012658 runs/s, max= 283.870967742 runs/s
  • with patch applied: median= 287.206266319 runs/s, stdev= 1.31518320219 runs/s, min= 282.051282051 runs/s, max= 288.713910761 runs/s

No new tests, as there is no client-visible behavior change. Existing Inspector tests will be modified
to test the new data provided, along with the necessary Inspector plumbing.

  • css/CSSGrammar.y:
  • css/CSSMediaRule.cpp:

(WebCore::CSSMediaRule::reattach): Check for mediaQueries() validity before reattaching.

  • css/CSSParser.cpp: Unless explicitly specified below, the method changes are related to the extension of the

source-based CSS model provided by the parser.
(WebCore::CSSParser::CSSParser):
(WebCore::CSSParser::setupParser):
(WebCore::CSSParser::parseDeclaration): Accept a CSSRuleSourceData for filling, since it now contains
the related style source range.
(WebCore::CSSParser::createImportRule):
(WebCore::CSSParser::createMediaRule): Create CSSMediaRule even if media and rules are empty,
which is consistent with Mozilla.
(WebCore::CSSParser::processAndAddNewRuleToSourceTreeIfNeeded):
(WebCore):
(WebCore::CSSParser::addNewRuleToSourceTree):
(WebCore::CSSParser::createKeyframesRule):
(WebCore::CSSParser::createStyleRule):
(WebCore::CSSParser::createFontFaceRule):
(WebCore::CSSParser::createPageRule):
(WebCore::CSSParser::createRegionRule):
(WebCore::CSSParser::fixUnparsedPropertyRanges):
(WebCore::CSSParser::markRuleHeaderStart):
(WebCore::CSSParser::markRuleHeaderEnd):
(WebCore::CSSParser::markRuleBodyStart):
(WebCore::CSSParser::markRuleBodyEnd):
(WebCore::CSSParser::markPropertyStart):
(WebCore::CSSParser::markPropertyEnd):

  • css/CSSParser.h:

(CSSParser):

  • css/CSSPropertySourceData.h: Extend the model to handle more types of rules and their containments.

(WebCore):
(WebCore::CSSRuleSourceData::create):
(WebCore::CSSRuleSourceData::createUnknown):
(CSSRuleSourceData):
(WebCore::CSSRuleSourceData::CSSRuleSourceData):

  • inspector/InspectorStyleSheet.cpp: Follow the CSSParser API changes but retain the flat stored CSS rules structure.

(ParsedStyleSheet):
(flattenSourceData): Flatten the rule tree to retain the existing rule-handling code intact.
(ParsedStyleSheet::setSourceData):
(ParsedStyleSheet::ruleSourceDataAt):
(WebCore::InspectorStyle::buildObjectForStyle):
(WebCore::InspectorStyle::setPropertyText):
(WebCore::InspectorStyle::styleText):
(WebCore::InspectorStyleSheet::setRuleSelector):
(WebCore::InspectorStyleSheet::deleteRule):
(WebCore::InspectorStyleSheet::buildObjectForRule):
(WebCore::InspectorStyleSheet::buildObjectForStyle):
(WebCore::InspectorStyleSheet::ensureSourceData):
(WebCore::InspectorStyleSheet::styleSheetTextWithChangedStyle):
(WebCore::InspectorStyleSheetForInlineStyle::ensureParsedDataReady):
(WebCore::InspectorStyleSheetForInlineStyle::getStyleAttributeRanges):

  • inspector/InspectorStyleSheet.h:
Location:
trunk/Source/WebCore
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r121549 r121551  
     12012-06-25  Alexander Pavlov  <apavlov@chromium.org>
     2
     3        Web Inspector: Provide source data for all known rule types in CSSParser, except "keyframe" and "region"
     4        https://bugs.webkit.org/show_bug.cgi?id=88420
     5
     6        Reviewed by Antti Koivisto.
     7
     8        This change transitions the CSS source code model from a flat list of style rules to a tree of all types of CSS rules
     9        (some of them lack actual source code data), which is crucial to model-based CSS stylesheet source editing
     10        (add/remove CSS rule) and navigation.
     11        As a side effect, the CSS parsing performance on PerformanceTests/Parser/css-parser-yui.html is improved roughly by 2%:
     12        - originally: median= 282.051282051 runs/s, stdev= 1.51236798322 runs/s, min= 278.481012658 runs/s, max= 283.870967742 runs/s
     13        - with patch applied: median= 287.206266319 runs/s, stdev= 1.31518320219 runs/s, min= 282.051282051 runs/s, max= 288.713910761 runs/s
     14
     15        No new tests, as there is no client-visible behavior change. Existing Inspector tests will be modified
     16        to test the new data provided, along with the necessary Inspector plumbing.
     17
     18        * css/CSSGrammar.y:
     19        * css/CSSMediaRule.cpp:
     20        (WebCore::CSSMediaRule::reattach): Check for mediaQueries() validity before reattaching.
     21        * css/CSSParser.cpp: Unless explicitly specified below, the method changes are related to the extension of the
     22        source-based CSS model provided by the parser.
     23        (WebCore::CSSParser::CSSParser):
     24        (WebCore::CSSParser::setupParser):
     25        (WebCore::CSSParser::parseDeclaration): Accept a CSSRuleSourceData for filling, since it now contains
     26        the related style source range.
     27        (WebCore::CSSParser::createImportRule):
     28        (WebCore::CSSParser::createMediaRule): Create CSSMediaRule even if media and rules are empty,
     29        which is consistent with Mozilla.
     30        (WebCore::CSSParser::processAndAddNewRuleToSourceTreeIfNeeded):
     31        (WebCore):
     32        (WebCore::CSSParser::addNewRuleToSourceTree):
     33        (WebCore::CSSParser::createKeyframesRule):
     34        (WebCore::CSSParser::createStyleRule):
     35        (WebCore::CSSParser::createFontFaceRule):
     36        (WebCore::CSSParser::createPageRule):
     37        (WebCore::CSSParser::createRegionRule):
     38        (WebCore::CSSParser::fixUnparsedPropertyRanges):
     39        (WebCore::CSSParser::markRuleHeaderStart):
     40        (WebCore::CSSParser::markRuleHeaderEnd):
     41        (WebCore::CSSParser::markRuleBodyStart):
     42        (WebCore::CSSParser::markRuleBodyEnd):
     43        (WebCore::CSSParser::markPropertyStart):
     44        (WebCore::CSSParser::markPropertyEnd):
     45        * css/CSSParser.h:
     46        (CSSParser):
     47        * css/CSSPropertySourceData.h: Extend the model to handle more types of rules and their containments.
     48        (WebCore):
     49        (WebCore::CSSRuleSourceData::create):
     50        (WebCore::CSSRuleSourceData::createUnknown):
     51        (CSSRuleSourceData):
     52        (WebCore::CSSRuleSourceData::CSSRuleSourceData):
     53        * inspector/InspectorStyleSheet.cpp: Follow the CSSParser API changes but retain the flat stored CSS rules structure.
     54        (ParsedStyleSheet):
     55        (flattenSourceData): Flatten the rule tree to retain the existing rule-handling code intact.
     56        (ParsedStyleSheet::setSourceData):
     57        (ParsedStyleSheet::ruleSourceDataAt):
     58        (WebCore::InspectorStyle::buildObjectForStyle):
     59        (WebCore::InspectorStyle::setPropertyText):
     60        (WebCore::InspectorStyle::styleText):
     61        (WebCore::InspectorStyleSheet::setRuleSelector):
     62        (WebCore::InspectorStyleSheet::deleteRule):
     63        (WebCore::InspectorStyleSheet::buildObjectForRule):
     64        (WebCore::InspectorStyleSheet::buildObjectForStyle):
     65        (WebCore::InspectorStyleSheet::ensureSourceData):
     66        (WebCore::InspectorStyleSheet::styleSheetTextWithChangedStyle):
     67        (WebCore::InspectorStyleSheetForInlineStyle::ensureParsedDataReady):
     68        (WebCore::InspectorStyleSheetForInlineStyle::getStyleAttributeRanges):
     69        * inspector/InspectorStyleSheet.h:
     70
    1712012-06-29  Vsevolod Vlasov  <vsevik@chromium.org>
    272
  • trunk/Source/WebCore/css/CSSGrammar.y

    r121531 r121551  
    388388     if (p->m_styleSheet)
    389389         p->m_styleSheet->parserSetEncodingFromCharsetRule($3);
     390     if (p->isExtractingSourceData() && p->m_currentRuleDataStack->isEmpty() && p->m_ruleSourceDataResult)
     391         p->addNewRuleToSourceTree(CSSRuleSourceData::createUnknown());
    390392     $$ = 0;
    391393  }
     
    463465  ;
    464466
     467at_import_header_end_maybe_space:
     468    maybe_space {
     469        CSSParser* p = static_cast<CSSParser*>(parser);
     470        p->markRuleHeaderEnd();
     471        p->markRuleBodyStart();
     472    }
     473    ;
     474
     475before_import_rule:
     476    /* empty */ {
     477        static_cast<CSSParser*>(parser)->markRuleHeaderStart(CSSRuleSourceData::IMPORT_RULE);
     478    }
     479    ;
    465480
    466481import:
    467     IMPORT_SYM maybe_space string_or_uri maybe_space maybe_media_list ';' {
    468         $$ = static_cast<CSSParser*>(parser)->createImportRule($3, $5);
    469     }
    470   | IMPORT_SYM maybe_space string_or_uri maybe_space maybe_media_list TOKEN_EOF {
    471         $$ = static_cast<CSSParser*>(parser)->createImportRule($3, $5);
    472     }
    473   | IMPORT_SYM maybe_space string_or_uri maybe_space maybe_media_list invalid_block {
    474         $$ = 0;
    475     }
    476   | IMPORT_SYM error ';' {
    477         $$ = 0;
    478     }
    479   | IMPORT_SYM error invalid_block {
    480         $$ = 0;
     482    before_import_rule IMPORT_SYM at_import_header_end_maybe_space string_or_uri maybe_space maybe_media_list ';' {
     483        $$ = static_cast<CSSParser*>(parser)->createImportRule($4, $6);
     484    }
     485  | before_import_rule IMPORT_SYM at_import_header_end_maybe_space string_or_uri maybe_space maybe_media_list TOKEN_EOF {
     486        $$ = static_cast<CSSParser*>(parser)->createImportRule($4, $6);
     487    }
     488  | before_import_rule IMPORT_SYM at_import_header_end_maybe_space string_or_uri maybe_space maybe_media_list invalid_block {
     489        $$ = 0;
     490        static_cast<CSSParser*>(parser)->popRuleData();
     491    }
     492  | before_import_rule IMPORT_SYM error ';' {
     493        $$ = 0;
     494        static_cast<CSSParser*>(parser)->popRuleData();
     495    }
     496  | before_import_rule IMPORT_SYM error invalid_block {
     497        $$ = 0;
     498        static_cast<CSSParser*>(parser)->popRuleData();
    481499    }
    482500  ;
     
    603621    ;
    604622
     623at_rule_body_start:
     624    /* empty */ {
     625        static_cast<CSSParser*>(parser)->markRuleBodyStart();
     626    }
     627    ;
     628
     629before_media_rule:
     630    /* empty */ {
     631        static_cast<CSSParser*>(parser)->markRuleHeaderStart(CSSRuleSourceData::MEDIA_RULE);
     632    }
     633    ;
     634
     635at_rule_header_end_maybe_space:
     636    maybe_space {
     637        static_cast<CSSParser*>(parser)->markRuleHeaderEnd();
     638    }
     639    ;
     640
    605641media:
    606     MEDIA_SYM maybe_space media_list '{' maybe_space block_rule_list save_block {
    607         $$ = static_cast<CSSParser*>(parser)->createMediaRule($3, $6);
    608     }
    609     | MEDIA_SYM maybe_space '{' maybe_space block_rule_list save_block {
    610         $$ = static_cast<CSSParser*>(parser)->createMediaRule(0, $5);
    611     }
    612     | MEDIA_SYM maybe_space ';' {
    613         $$ = 0;
     642    before_media_rule MEDIA_SYM maybe_space media_list at_rule_header_end '{' at_rule_body_start maybe_space block_rule_list save_block {
     643        $$ = static_cast<CSSParser*>(parser)->createMediaRule($4, $9);
     644    }
     645    | before_media_rule MEDIA_SYM at_rule_header_end_maybe_space '{' at_rule_body_start maybe_space block_rule_list save_block {
     646        $$ = static_cast<CSSParser*>(parser)->createMediaRule(0, $7);
     647    }
     648    | before_media_rule MEDIA_SYM at_rule_header_end_maybe_space ';' {
     649        $$ = 0;
     650        static_cast<CSSParser*>(parser)->popRuleData();
    614651    }
    615652    ;
     
    621658  ;
    622659
     660before_keyframes_rule:
     661    /* empty */ {
     662        static_cast<CSSParser*>(parser)->markRuleHeaderStart(CSSRuleSourceData::KEYFRAMES_RULE);
     663    }
     664    ;
     665
    623666keyframes:
    624     WEBKIT_KEYFRAMES_SYM maybe_space keyframe_name maybe_space '{' maybe_space keyframes_rule closing_brace {
    625         $$ = static_cast<CSSParser*>(parser)->createKeyframesRule($3, static_cast<CSSParser*>(parser)->sinkFloatingKeyframeVector($7));
     667    before_keyframes_rule WEBKIT_KEYFRAMES_SYM maybe_space keyframe_name at_rule_header_end_maybe_space '{' at_rule_body_start maybe_space keyframes_rule closing_brace {
     668        $$ = static_cast<CSSParser*>(parser)->createKeyframesRule($4, static_cast<CSSParser*>(parser)->sinkFloatingKeyframeVector($9));
    626669    }
    627670    ;
     
    675718    ;
    676719
     720before_page_rule:
     721    /* empty */ {
     722        static_cast<CSSParser*>(parser)->markRuleHeaderStart(CSSRuleSourceData::PAGE_RULE);
     723    }
     724    ;
     725
    677726page:
    678     PAGE_SYM maybe_space page_selector maybe_space
    679     '{' maybe_space declarations_and_margins closing_brace {
    680         CSSParser* p = static_cast<CSSParser*>(parser);
    681         if ($3)
    682             $$ = p->createPageRule(p->sinkFloatingSelector($3));
     727    before_page_rule PAGE_SYM maybe_space page_selector at_rule_header_end_maybe_space
     728    '{' at_rule_body_start maybe_space_before_declaration declarations_and_margins closing_brace {
     729        CSSParser* p = static_cast<CSSParser*>(parser);
     730        if ($4)
     731            $$ = p->createPageRule(p->sinkFloatingSelector($4));
    683732        else {
    684733            // Clear properties in the invalid @page rule.
     
    686735            // Also clear margin at-rules here once we fully implement margin at-rules parsing.
    687736            $$ = 0;
     737            static_cast<CSSParser*>(parser)->popRuleData();
    688738        }
    689739    }
    690     | PAGE_SYM error invalid_block {
     740    | before_page_rule PAGE_SYM error invalid_block {
     741      static_cast<CSSParser*>(parser)->popRuleData();
    691742      $$ = 0;
    692743    }
    693     | PAGE_SYM error ';' {
     744    | before_page_rule PAGE_SYM error ';' {
     745      static_cast<CSSParser*>(parser)->popRuleData();
    694746      $$ = 0;
    695747    }
     
    787839    ;
    788840
     841before_font_face_rule:
     842    /* empty */ {
     843        static_cast<CSSParser*>(parser)->markRuleHeaderStart(CSSRuleSourceData::FONT_FACE_RULE);
     844    }
     845    ;
     846
    789847font_face:
    790     FONT_FACE_SYM maybe_space
    791     '{' maybe_space declaration_list closing_brace {
     848    before_font_face_rule FONT_FACE_SYM at_rule_header_end_maybe_space
     849    '{' at_rule_body_start maybe_space_before_declaration declaration_list closing_brace {
    792850        $$ = static_cast<CSSParser*>(parser)->createFontFaceRule();
    793851    }
    794     | FONT_FACE_SYM error invalid_block {
     852    | before_font_face_rule FONT_FACE_SYM error invalid_block {
    795853      $$ = 0;
    796     }
    797     | FONT_FACE_SYM error ';' {
     854      static_cast<CSSParser*>(parser)->popRuleData();
     855    }
     856    | before_font_face_rule FONT_FACE_SYM error ';' {
    798857      $$ = 0;
     858      static_cast<CSSParser*>(parser)->popRuleData();
    799859    }
    800860;
     
    838898maybe_space_before_declaration:
    839899    maybe_space {
    840         CSSParser* p = static_cast<CSSParser*>(parser);
    841         p->markPropertyStart();
     900        static_cast<CSSParser*>(parser)->markPropertyStart();
    842901    }
    843902  ;
     
    845904before_selector_list:
    846905    /* empty */ {
    847         CSSParser* p = static_cast<CSSParser*>(parser);
    848         p->markSelectorListStart();
    849     }
    850   ;
    851 
    852 before_rule_opening_brace:
     906        static_cast<CSSParser*>(parser)->markRuleHeaderStart(CSSRuleSourceData::STYLE_RULE);
     907    }
     908  ;
     909
     910at_rule_header_end:
    853911    /* empty */ {
    854         CSSParser* p = static_cast<CSSParser*>(parser);
    855         p->markSelectorListEnd();
     912        static_cast<CSSParser*>(parser)->markRuleHeaderEnd();
    856913    }
    857914  ;
    858915
    859916ruleset:
    860     before_selector_list selector_list before_rule_opening_brace '{' maybe_space_before_declaration declaration_list closing_brace {
     917    before_selector_list selector_list at_rule_header_end '{' maybe_space_before_declaration declaration_list closing_brace {
    861918        CSSParser* p = static_cast<CSSParser*>(parser);
    862919        $$ = p->createStyleRule($2);
  • trunk/Source/WebCore/css/CSSMediaRule.cpp

    r118194 r121551  
    167167    ASSERT(rule);
    168168    m_mediaRule = rule;
    169     if (m_mediaCSSOMWrapper)
     169    if (m_mediaCSSOMWrapper && m_mediaRule->mediaQueries())
    170170        m_mediaCSSOMWrapper->reattach(m_mediaRule->mediaQueries());
    171171    for (unsigned i = 0; i < m_childRuleCSSOMWrappers.size(); ++i) {
  • trunk/Source/WebCore/css/CSSParser.cpp

    r121531 r121551  
    252252    , m_defaultNamespace(starAtom)
    253253    , m_parsedTextPrefixLength(0)
    254     , m_inStyleRuleOrDeclaration(false)
    255     , m_selectorListRange(0, 0)
    256     , m_ruleBodyRange(0, 0)
    257254    , m_propertyRange(UINT_MAX, UINT_MAX)
    258255    , m_ruleSourceDataResult(0)
     
    318315
    319316    m_currentCharacter = m_tokenStart = m_dataStart.get();
    320     resetRuleBodyMarks();
    321317}
    322318
     
    12221218}
    12231219
    1224 bool CSSParser::parseDeclaration(StylePropertySet* declaration, const String& string, PassRefPtr<CSSStyleSourceData> prpStyleSourceData, StyleSheetContents* contextStyleSheet)
     1220bool CSSParser::parseDeclaration(StylePropertySet* declaration, const String& string, PassRefPtr<CSSRuleSourceData> prpRuleSourceData, StyleSheetContents* contextStyleSheet)
    12251221{
    12261222    // Length of the "@-webkit-decls{" prefix.
     
    12291225    setStyleSheet(contextStyleSheet);
    12301226
    1231     RefPtr<CSSStyleSourceData> styleSourceData = prpStyleSourceData;
    1232     if (styleSourceData) {
     1227    RefPtr<CSSRuleSourceData> ruleSourceData = prpRuleSourceData;
     1228    if (ruleSourceData) {
    12331229        m_currentRuleDataStack = adoptPtr(new RuleSourceDataList());
    1234         RefPtr<CSSRuleSourceData> data = CSSRuleSourceData::create();
    1235         data->styleSourceData = styleSourceData;
    1236         m_currentRuleDataStack->append(data);
    1237         m_inStyleRuleOrDeclaration = true;
     1230        m_currentRuleDataStack->append(ruleSourceData);
    12381231    }
    12391232
     
    12511244    }
    12521245
    1253     if (styleSourceData) {
    1254         ASSERT(!m_currentRuleDataStack->isEmpty());
    1255         CSSRuleSourceData* ruleData = m_currentRuleDataStack->last().get();
    1256         ruleData->styleSourceData->styleBodyRange.start = 0;
    1257         ruleData->styleSourceData->styleBodyRange.end = string.length();
    1258         for (size_t i = 0, size = ruleData->styleSourceData->propertyData.size(); i < size; ++i) {
    1259             CSSPropertySourceData& propertyData = ruleData->styleSourceData->propertyData.at(i);
     1246    if (ruleSourceData) {
     1247        ASSERT(m_currentRuleDataStack->size() == 1);
     1248        ruleSourceData->ruleBodyRange.start = 0;
     1249        ruleSourceData->ruleBodyRange.end = string.length();
     1250        for (size_t i = 0, size = ruleSourceData->styleSourceData->propertyData.size(); i < size; ++i) {
     1251            CSSPropertySourceData& propertyData = ruleSourceData->styleSourceData->propertyData.at(i);
    12601252            propertyData.range.start -= prefixLength;
    12611253            propertyData.range.end -= prefixLength;
    12621254        }
    1263         fixUnparsedPropertyRanges(ruleData);
    1264 
     1255
     1256        fixUnparsedPropertyRanges(ruleSourceData.get());
    12651257        m_currentRuleDataStack.clear();
    1266         m_inStyleRuleOrDeclaration = false;
    12671258    }
    12681259
     
    94029393    StyleRuleImport* result = rule.get();
    94039394    m_parsedRules.append(rule.release());
     9395    processAndAddNewRuleToSourceTreeIfNeeded();
    94049396    return result;
    94059397}
     
    94079399StyleRuleBase* CSSParser::createMediaRule(MediaQuerySet* media, RuleList* rules)
    94089400{
    9409     if (!media || !rules)
    9410         return 0;
    94119401    m_allowImportRules = m_allowNamespaceDeclarations = false;
    9412     RefPtr<StyleRuleMedia> rule = StyleRuleMedia::create(media, *rules);
     9402    RefPtr<StyleRuleMedia> rule;
     9403    if (rules)
     9404        rule = StyleRuleMedia::create(media, *rules);
     9405    else {
     9406        RuleList emptyRules;
     9407        rule = StyleRuleMedia::create(media, emptyRules);
     9408    }
    94139409    StyleRuleMedia* result = rule.get();
    94149410    m_parsedRules.append(rule.release());
     9411    processAndAddNewRuleToSourceTreeIfNeeded();
    94159412    return result;
    94169413}
     
    94239420    m_parsedRuleLists.append(list.release());
    94249421    return listPtr;
     9422}
     9423
     9424void CSSParser::processAndAddNewRuleToSourceTreeIfNeeded()
     9425{
     9426    if (!isExtractingSourceData())
     9427        return;
     9428    markRuleBodyEnd();
     9429    RefPtr<CSSRuleSourceData> rule = popRuleData();
     9430    fixUnparsedPropertyRanges(rule.get());
     9431    addNewRuleToSourceTree(rule.release());
    94259432}
    94269433
     
    94309437    if (!m_ruleSourceDataResult)
    94319438        return;
    9432 
    9433     // FIXME: This temporarily builds a flat style rule data list, to avoid the client code breakage.
    9434     m_ruleSourceDataResult->append(rule);
     9439    if (m_currentRuleDataStack->isEmpty())
     9440        m_ruleSourceDataResult->append(rule);
     9441    else
     9442        m_currentRuleDataStack->last()->childRules.append(rule);
    94359443}
    94369444
     
    94569464    StyleRuleKeyframes* rulePtr = rule.get();
    94579465    m_parsedRules.append(rule.release());
     9466    processAndAddNewRuleToSourceTreeIfNeeded();
    94589467    return rulePtr;
    94599468}
     
    94629471{
    94639472    StyleRule* result = 0;
    9464     markRuleBodyEnd();
    94659473    if (selectors) {
    94669474        m_allowImportRules = m_allowNamespaceDeclarations = false;
     
    94729480        result = rule.get();
    94739481        m_parsedRules.append(rule.release());
    9474         if (isExtractingSourceData()) {
    9475             RefPtr<CSSRuleSourceData> currentRuleData = popRuleData();
    9476             currentRuleData->styleSourceData->styleBodyRange = m_ruleBodyRange;
    9477             currentRuleData->selectorListRange = m_selectorListRange;
    9478             fixUnparsedPropertyRanges(currentRuleData.get());
    9479             addNewRuleToSourceTree(currentRuleData.release());
    9480             m_inStyleRuleOrDeclaration = false;
    9481         }
    9482     }
    9483     if (isExtractingSourceData()) {
    9484         resetSelectorListMarks();
    9485         resetRuleBodyMarks();
     9482        processAndAddNewRuleToSourceTreeIfNeeded();
    94869483    }
    94879484    clearProperties();
     
    95109507    StyleRuleFontFace* result = rule.get();
    95119508    m_parsedRules.append(rule.release());
     9509    processAndAddNewRuleToSourceTreeIfNeeded();
    95129510    return result;
    95139511}
     
    95899587        pageRule = rule.get();
    95909588        m_parsedRules.append(rule.release());
     9589        processAndAddNewRuleToSourceTreeIfNeeded();
    95919590    }
    95929591    clearProperties();
     
    96119610    StyleRuleRegion* result = regionRule.get();
    96129611    m_parsedRules.append(regionRule.release());
     9612    if (isExtractingSourceData())
     9613        addNewRuleToSourceTree(CSSRuleSourceData::createUnknown());
    96139614
    96149615    return result;
     
    96929693void CSSParser::fixUnparsedPropertyRanges(CSSRuleSourceData* ruleData)
    96939694{
     9695    if (!ruleData->styleSourceData)
     9696        return;
    96949697    Vector<CSSPropertySourceData>& propertyData = ruleData->styleSourceData->propertyData;
    96959698    unsigned size = propertyData.size();
     
    96979700        return;
    96989701
    9699     unsigned styleStart = ruleData->styleSourceData->styleBodyRange.start;
     9702    unsigned styleStart = ruleData->ruleBodyRange.start;
    97009703    const UChar* characters = m_dataStart.get() + m_parsedTextPrefixLength;
    97019704    CSSPropertySourceData* nextData = &(propertyData.at(0));
     
    97119714        unsigned propertyEndInStyleSheet;
    97129715        if (!nextData)
    9713             propertyEndInStyleSheet = ruleData->styleSourceData->styleBodyRange.end - 1;
     9716            propertyEndInStyleSheet = ruleData->ruleBodyRange.end - 1;
    97149717        else
    97159718            propertyEndInStyleSheet = styleStart + nextData->range.start - 1;
     
    97359738}
    97369739
    9737 void CSSParser::markSelectorListStart()
     9740void CSSParser::markRuleHeaderStart(CSSRuleSourceData::Type ruleType)
    97389741{
    97399742    if (!isExtractingSourceData())
    97409743        return;
    9741     m_selectorListRange.start = m_tokenStart - m_dataStart.get();
    9742 }
    9743 
    9744 void CSSParser::markSelectorListEnd()
     9744    RefPtr<CSSRuleSourceData> data = CSSRuleSourceData::create(ruleType);
     9745    data->ruleHeaderRange.start = m_tokenStart - m_dataStart.get();
     9746    m_currentRuleDataStack->append(data.release());
     9747}
     9748
     9749void CSSParser::markRuleHeaderEnd()
    97459750{
    97469751    if (!isExtractingSourceData())
    97479752        return;
     9753    ASSERT(!m_currentRuleDataStack->isEmpty());
    97489754    UChar* listEnd = m_tokenStart;
    97499755    while (listEnd > m_dataStart.get() + 1) {
     
    97539759            break;
    97549760    }
    9755     m_selectorListRange.end = listEnd - m_dataStart.get();
    9756     RefPtr<CSSRuleSourceData> data = CSSRuleSourceData::create();
    9757     data->styleSourceData = CSSStyleSourceData::create();
    9758     m_currentRuleDataStack->append(data);
     9761    m_currentRuleDataStack->last()->ruleHeaderRange.end = listEnd - m_dataStart.get();
    97599762}
    97609763
     
    97669769    if (*m_tokenStart == '{')
    97679770        ++offset; // Skip the rule body opening brace.
    9768     if (offset > m_ruleBodyRange.start)
    9769         m_ruleBodyRange.start = offset;
    9770     m_inStyleRuleOrDeclaration = true;
     9771    ASSERT(!m_currentRuleDataStack->isEmpty());
     9772    m_currentRuleDataStack->last()->ruleBodyRange.start = offset;
    97719773}
    97729774
    97739775void CSSParser::markRuleBodyEnd()
     9776{
     9777    // Precondition: (!isExtractingSourceData())
     9778    unsigned offset = m_tokenStart - m_dataStart.get();
     9779    ASSERT(!m_currentRuleDataStack->isEmpty());
     9780    m_currentRuleDataStack->last()->ruleBodyRange.end = offset;
     9781}
     9782
     9783void CSSParser::markPropertyStart()
    97749784{
    97759785    if (!isExtractingSourceData())
    97769786        return;
    9777     unsigned offset = m_tokenStart - m_dataStart.get();
    9778     if (offset > m_ruleBodyRange.end)
    9779         m_ruleBodyRange.end = offset;
    9780 }
    9781 
    9782 void CSSParser::markPropertyStart()
    9783 {
    9784     if (!m_inStyleRuleOrDeclaration)
     9787    if (m_currentRuleDataStack->isEmpty() || !m_currentRuleDataStack->last()->styleSourceData)
    97859788        return;
     9789
    97869790    m_propertyRange.start = m_tokenStart - m_dataStart.get();
    97879791}
     
    97899793void CSSParser::markPropertyEnd(bool isImportantFound, bool isPropertyParsed)
    97909794{
    9791     if (!m_inStyleRuleOrDeclaration)
     9795    if (!isExtractingSourceData())
     9796        return;
     9797    if (m_currentRuleDataStack->isEmpty() || !m_currentRuleDataStack->last()->styleSourceData)
    97929798        return;
    97939799
     
    98109816        String value = propertyString.substring(colonIndex + 1, propertyString.length()).stripWhiteSpace();
    98119817        // The property range is relative to the declaration start offset.
     9818        SourceRange& topRuleBodyRange = m_currentRuleDataStack->last()->ruleBodyRange;
    98129819        m_currentRuleDataStack->last()->styleSourceData->propertyData.append(
    9813             CSSPropertySourceData(name, value, isImportantFound, isPropertyParsed, SourceRange(start - m_ruleBodyRange.start, end - m_ruleBodyRange.start)));
     9820            CSSPropertySourceData(name, value, isImportantFound, isPropertyParsed, SourceRange(start - topRuleBodyRange.start, end - topRuleBodyRange.start)));
    98149821    }
    98159822    resetPropertyRange();
  • trunk/Source/WebCore/css/CSSParser.h

    r121531 r121551  
    7979    static PassRefPtr<CSSValueList> parseFontFaceValue(const AtomicString&);
    8080    PassRefPtr<CSSPrimitiveValue> parseValidPrimitive(int ident, CSSParserValue*);
    81     bool parseDeclaration(StylePropertySet*, const String&, PassRefPtr<CSSStyleSourceData>, StyleSheetContents* contextStyleSheet);
     81    bool parseDeclaration(StylePropertySet*, const String&, PassRefPtr<CSSRuleSourceData>, StyleSheetContents* contextStyleSheet);
    8282    PassOwnPtr<MediaQuery> parseMediaQuery(const String&);
    8383
     
    325325    // tokenizer methods and data
    326326    size_t m_parsedTextPrefixLength;
    327     bool m_inStyleRuleOrDeclaration;
    328     SourceRange m_selectorListRange;
    329     SourceRange m_ruleBodyRange;
    330327    SourceRange m_propertyRange;
    331328    OwnPtr<RuleSourceDataList> m_currentRuleDataStack;
     
    333330
    334331    void fixUnparsedPropertyRanges(CSSRuleSourceData*);
    335     void markStyleRuleHeaderStart();
     332    void markRuleHeaderStart(CSSRuleSourceData::Type);
    336333    void markRuleHeaderEnd();
    337 
    338     void markSelectorListStart();
    339     void markSelectorListEnd();
    340334    void markRuleBodyStart();
    341335    void markRuleBodyEnd();
    342336    void markPropertyStart();
    343337    void markPropertyEnd(bool isImportantFound, bool isPropertyParsed);
     338    void processAndAddNewRuleToSourceTreeIfNeeded();
    344339    void addNewRuleToSourceTree(PassRefPtr<CSSRuleSourceData>);
    345340    PassRefPtr<CSSRuleSourceData> popRuleData();
    346     void resetSelectorListMarks() { m_selectorListRange.start = m_selectorListRange.end = 0; }
    347     void resetRuleBodyMarks() { m_ruleBodyRange.start = m_ruleBodyRange.end = 0; }
    348341    void resetPropertyRange() { m_propertyRange.start = m_propertyRange.end = UINT_MAX; }
    349342    bool isExtractingSourceData() const { return !!m_currentRuleDataStack; }
  • trunk/Source/WebCore/css/CSSPropertySourceData.h

    r120474 r121551  
    4141namespace WebCore {
    4242
    43 class StyleRule;
     43class StyleRuleBase;
    4444
    4545struct SourceRange {
     
    7979    }
    8080
    81     // Range of the style text in the enclosing source.
    82     SourceRange styleBodyRange;
    8381    Vector<CSSPropertySourceData> propertyData;
    8482};
     
    8886
    8987struct CSSRuleSourceData : public RefCounted<CSSRuleSourceData> {
    90     static PassRefPtr<CSSRuleSourceData> create()
     88    enum Type {
     89        UNKNOWN_RULE,
     90        STYLE_RULE,
     91        CHARSET_RULE,
     92        IMPORT_RULE,
     93        MEDIA_RULE,
     94        FONT_FACE_RULE,
     95        PAGE_RULE,
     96        KEYFRAMES_RULE
     97    };
     98
     99    static PassRefPtr<CSSRuleSourceData> create(Type type)
    91100    {
    92         return adoptRef(new CSSRuleSourceData());
     101        return adoptRef(new CSSRuleSourceData(type));
    93102    }
    94103
     104    static PassRefPtr<CSSRuleSourceData> createUnknown()
     105    {
     106        return adoptRef(new CSSRuleSourceData(UNKNOWN_RULE));
     107    }
     108
     109    CSSRuleSourceData(Type type)
     110        : type(type)
     111    {
     112        if (type == STYLE_RULE || type == FONT_FACE_RULE || type == PAGE_RULE)
     113            styleSourceData = CSSStyleSourceData::create();
     114    }
     115
     116    Type type;
     117
    95118    // Range of the selector list in the enclosing source.
    96     SourceRange selectorListRange;
     119    SourceRange ruleHeaderRange;
     120
     121    // Range of the rule body (e.g. style text for style rules) in the enclosing source.
     122    SourceRange ruleBodyRange;
     123
     124    // Only for CSSStyleRules, CSSFontFaceRules, and CSSPageRules.
    97125    RefPtr<CSSStyleSourceData> styleSourceData;
     126
     127    // Only for CSSMediaRules.
     128    RuleSourceDataList childRules;
    98129};
    99 typedef HashMap<StyleRule*, RefPtr<CSSRuleSourceData> > StyleRuleRangeMap;
    100130
    101131} // namespace WebCore
  • trunk/Source/WebCore/inspector/InspectorStyleSheet.cpp

    r120469 r121551  
    5151#include "StyleResolver.h"
    5252#include "StyleRule.h"
     53#include "StyleRuleImport.h"
    5354#include "StyleSheetContents.h"
    5455#include "StyleSheetList.h"
     
    6263using WebCore::TypeBuilder::Array;
    6364using WebCore::RuleSourceDataList;
     65using WebCore::CSSRuleSourceData;
    6466
    6567class ParsedStyleSheet {
     
    7476    void setSourceData(PassOwnPtr<RuleSourceDataList>);
    7577    bool hasSourceData() const { return m_sourceData; }
    76     RefPtr<WebCore::CSSRuleSourceData> ruleSourceDataAt(unsigned index) const;
     78    PassRefPtr<WebCore::CSSRuleSourceData> ruleSourceDataAt(unsigned) const;
    7779
    7880private:
     
    98100}
    99101
     102static void flattenSourceData(RuleSourceDataList* dataList, RuleSourceDataList* target)
     103{
     104    for (size_t i = 0; i < dataList->size(); ++i) {
     105        RefPtr<CSSRuleSourceData>& data = dataList->at(i);
     106        if (data->type == CSSRuleSourceData::STYLE_RULE)
     107            target->append(data);
     108        else if (data->type == CSSRuleSourceData::MEDIA_RULE)
     109            flattenSourceData(&data->childRules, target);
     110    }
     111}
     112
    100113void ParsedStyleSheet::setSourceData(PassOwnPtr<RuleSourceDataList> sourceData)
    101114{
    102     m_sourceData = sourceData;
    103 }
    104 
    105 RefPtr<WebCore::CSSRuleSourceData> ParsedStyleSheet::ruleSourceDataAt(unsigned index) const
     115    if (!sourceData) {
     116        m_sourceData.clear();
     117        return;
     118    }
     119
     120    m_sourceData = adoptPtr(new RuleSourceDataList());
     121
     122    // FIXME: This is a temporary solution to retain the original flat sourceData structure
     123    // containing only style rules, even though CSSParser now provides the full rule source data tree.
     124    // Normally, we should just assign m_sourceData = sourceData;
     125    flattenSourceData(sourceData.get(), m_sourceData.get());
     126}
     127
     128PassRefPtr<WebCore::CSSRuleSourceData> ParsedStyleSheet::ruleSourceDataAt(unsigned index) const
    106129{
    107130    if (!hasSourceData() || index >= m_sourceData->size())
     
    272295    RefPtr<CSSRuleSourceData> sourceData = m_parentStyleSheet ? m_parentStyleSheet->ruleSourceDataFor(m_style.get()) : 0;
    273296    if (sourceData)
    274         result->setRange(buildSourceRangeObject(sourceData->styleSourceData->styleBodyRange));
     297        result->setRange(buildSourceRangeObject(sourceData->ruleBodyRange));
    275298
    276299    return result.release();
     
    315338    if (propertyText.stripWhiteSpace().length()) {
    316339        RefPtr<StylePropertySet> tempMutableStyle = StylePropertySet::create();
    317         RefPtr<CSSStyleSourceData> sourceData = CSSStyleSourceData::create();
     340        RefPtr<CSSRuleSourceData> sourceData = CSSRuleSourceData::create(CSSRuleSourceData::STYLE_RULE);
    318341        CSSParser p(CSSStrictMode);
    319342        p.parseDeclaration(tempMutableStyle.get(), propertyText + " " + bogusPropertyName + ": none", sourceData, m_style->parentStyleSheet()->contents());
    320         Vector<CSSPropertySourceData>& propertyData = sourceData->propertyData;
     343        Vector<CSSPropertySourceData>& propertyData = sourceData->styleSourceData->propertyData;
    321344        unsigned propertyCount = propertyData.size();
    322345
     
    356379        editor.replaceProperty(index, propertyText);
    357380    } else
    358         editor.insertProperty(index, propertyText, sourceData->styleSourceData->styleBodyRange.length());
     381        editor.insertProperty(index, propertyText, sourceData->ruleBodyRange.length());
    359382
    360383    return applyStyleText(editor.styleText());
     
    414437        return false;
    415438
    416     SourceRange& bodyRange = sourceData->styleSourceData->styleBodyRange;
     439    SourceRange& bodyRange = sourceData->ruleBodyRange;
    417440    *result = styleSheetText.substring(bodyRange.start, bodyRange.end - bodyRange.start);
    418441    return true;
     
    772795
    773796    String sheetText = m_parsedStyleSheet->text();
    774     sheetText.replace(sourceData->selectorListRange.start, sourceData->selectorListRange.end - sourceData->selectorListRange.start, selector);
     797    sheetText.replace(sourceData->ruleHeaderRange.start, sourceData->ruleHeaderRange.length(), selector);
    775798    m_parsedStyleSheet->setText(sheetText);
    776799    fireStyleSheetChanged();
     
    831854
    832855    String sheetText = m_parsedStyleSheet->text();
    833     sheetText.remove(sourceData->selectorListRange.start, sourceData->styleSourceData->styleBodyRange.end - sourceData->selectorListRange.start + 1);
     856    sheetText.remove(sourceData->ruleHeaderRange.start, sourceData->ruleBodyRange.end - sourceData->ruleHeaderRange.start + 1);
    834857    m_parsedStyleSheet->setText(sheetText);
    835858    fireStyleSheetChanged();
     
    912935    if (ensureParsedDataReady())
    913936        sourceData = ruleSourceDataFor(rule->style());
    914     if (sourceData) {
    915         RefPtr<TypeBuilder::CSS::SourceRange> selectorRange = TypeBuilder::CSS::SourceRange::create()
    916             .setStart(sourceData->selectorListRange.start)
    917             .setEnd(sourceData->selectorListRange.end);
    918         result->setSelectorRange(selectorRange.release());
    919     }
     937    if (sourceData)
     938        result->setSelectorRange(buildSourceRangeObject(sourceData->ruleHeaderRange));
    920939
    921940    RefPtr<Array<TypeBuilder::CSS::CSSMedia> > mediaArray = Array<TypeBuilder::CSS::CSSMedia>::create();
     
    949968        bool success = getText(&sheetText);
    950969        if (success) {
    951             const SourceRange& bodyRange = sourceData->styleSourceData->styleBodyRange;
     970            const SourceRange& bodyRange = sourceData->ruleBodyRange;
    952971            result->setCssText(sheetText.substring(bodyRange.start, bodyRange.end - bodyRange.start));
    953972        }
     
    10991118    RefPtr<StyleSheetContents> newStyleSheet = StyleSheetContents::create();
    11001119    CSSParser p(CSSStrictMode);
    1101     OwnPtr<RuleSourceDataList> rangesVector(adoptPtr(new RuleSourceDataList()));
    1102     p.parseSheet(newStyleSheet.get(), m_parsedStyleSheet->text(), 0, rangesVector.get());
    1103     m_parsedStyleSheet->setSourceData(rangesVector.release());
     1120    OwnPtr<RuleSourceDataList> ruleSourceDataResult = adoptPtr(new RuleSourceDataList());
     1121    p.parseSheet(newStyleSheet.get(), m_parsedStyleSheet->text(), 0, ruleSourceDataResult.get());
     1122    m_parsedStyleSheet->setSourceData(ruleSourceDataResult.release());
    11041123    return m_parsedStyleSheet->hasSourceData();
    11051124}
     
    11451164
    11461165    RefPtr<CSSRuleSourceData> sourceData = ruleSourceDataFor(style);
    1147     unsigned bodyStart = sourceData->styleSourceData->styleBodyRange.start;
    1148     unsigned bodyEnd = sourceData->styleSourceData->styleBodyRange.end;
     1166    unsigned bodyStart = sourceData->ruleBodyRange.start;
     1167    unsigned bodyEnd = sourceData->ruleBodyRange.end;
    11491168    ASSERT(bodyStart <= bodyEnd);
    11501169
     
    13271346        return true;
    13281347
    1329     m_ruleSourceData = CSSRuleSourceData::create();
    1330     RefPtr<CSSStyleSourceData> sourceData = CSSStyleSourceData::create();
    1331     bool success = getStyleAttributeRanges(&sourceData);
     1348    m_ruleSourceData = CSSRuleSourceData::create(CSSRuleSourceData::STYLE_RULE);
     1349    bool success = getStyleAttributeRanges(m_ruleSourceData.get());
    13321350    if (!success)
    13331351        return false;
    13341352
    1335     m_ruleSourceData->styleSourceData = sourceData.release();
    13361353    return true;
    13371354}
     
    13531370}
    13541371
    1355 bool InspectorStyleSheetForInlineStyle::getStyleAttributeRanges(RefPtr<CSSStyleSourceData>* result) const
     1372bool InspectorStyleSheetForInlineStyle::getStyleAttributeRanges(CSSRuleSourceData* result) const
    13561373{
    13571374    if (!m_element->isStyledElement())
     
    13591376
    13601377    if (m_styleText.isEmpty()) {
    1361         (*result)->styleBodyRange.start = 0;
    1362         (*result)->styleBodyRange.end = 0;
     1378        result->ruleBodyRange.start = 0;
     1379        result->ruleBodyRange.end = 0;
    13631380        return true;
    13641381    }
     
    13661383    RefPtr<StylePropertySet> tempDeclaration = StylePropertySet::create();
    13671384    CSSParser p(m_element->document());
    1368     p.parseDeclaration(tempDeclaration.get(), m_styleText, *result, m_element->document()->elementSheet()->contents());
     1385    p.parseDeclaration(tempDeclaration.get(), m_styleText, result, m_element->document()->elementSheet()->contents());
    13691386    return true;
    13701387}
  • trunk/Source/WebCore/inspector/InspectorStyleSheet.h

    r120469 r121551  
    273273    CSSStyleDeclaration* inlineStyle() const;
    274274    const String& elementStyleText() const;
    275     bool getStyleAttributeRanges(RefPtr<CSSStyleSourceData>* result) const;
     275    bool getStyleAttributeRanges(CSSRuleSourceData* result) const;
    276276
    277277    RefPtr<Element> m_element;
Note: See TracChangeset for help on using the changeset viewer.