Title: [208886] trunk/Source/WebCore
Revision
208886
Author
[email protected]
Date
2016-11-18 11:09:13 -0800 (Fri, 18 Nov 2016)

Log Message

[CSS Parser] Hook up InspectorStyleSheet to the new CSS parser.
https://bugs.webkit.org/show_bug.cgi?id=164886

Reviewed by Dean Jackson.

* css/CSSGrammar.y.in:
Get rid of the CSSRuleSourceData type enum, since StyleRule's type
enum is exactly the same.

* css/CSSPropertySourceData.cpp:
(WebCore::CSSPropertySourceData::CSSPropertySourceData):
* css/CSSPropertySourceData.h:
Add a concept of disabled to CSSPropertySourceData. This is used for
commented out properties.

(WebCore::CSSRuleSourceData::create):
(WebCore::CSSRuleSourceData::createUnknown):
(WebCore::CSSRuleSourceData::CSSRuleSourceData):
Get rid of the CSSRuleSourceData type enum, since StyleRule's type
enum is exactly the same.

* css/parser/CSSParser.cpp:
(WebCore::CSSParserContext::CSSParserContext):
(WebCore::CSSParser::parseSheetForInspector):
(WebCore::CSSParser::parseDeclarationForInspector):
(WebCore::CSSParser::markSupportsRuleHeaderStart):
(WebCore::CSSParser::markRuleHeaderStart):
(WebCore::CSSParser::markPropertyEnd):
* css/parser/CSSParser.h:
Add functions that represent the new API for inspector sheet
and declaration parsing. Patch the old parse code to use StyleRule::Type
now that the CSSRuleSourceData type is gone.

* css/parser/CSSParserObserver.h:
Tweak the API for our memory management.

* inspector/InspectorStyleSheet.cpp:
(flattenSourceData):
(WebCore::parserContextForDocument):
(WebCore::StyleSheetHandler::StyleSheetHandler):
(WebCore::StyleSheetHandler::startRuleHeader):
(WebCore::StyleSheetHandler::setRuleHeaderEnd):
(WebCore::StyleSheetHandler::endRuleHeader):
(WebCore::StyleSheetHandler::observeSelector):
(WebCore::StyleSheetHandler::startRuleBody):
(WebCore::StyleSheetHandler::endRuleBody):
(WebCore::StyleSheetHandler::popRuleData):
(WebCore::fixUnparsedProperties):
(WebCore::StyleSheetHandler::fixUnparsedPropertyRanges):
(WebCore::StyleSheetHandler::observeProperty):
(WebCore::StyleSheetHandler::observeComment):
(WebCore::InspectorStyle::populateAllProperties):
(WebCore::isValidSelectorListString):
(WebCore::InspectorStyleSheet::ensureSourceData):
(WebCore::InspectorStyleSheetForInlineStyle::ensureParsedDataReady):
(WebCore::InspectorStyleSheetForInlineStyle::ruleSourceData):
(WebCore::createCSSParser): Deleted.
(WebCore::InspectorStyleSheetForInlineStyle::getStyleAttributeRanges): Deleted.
* inspector/InspectorStyleSheet.h:
(WebCore::InspectorStyleProperty::setRawTextFromStyleDeclaration):
Add the new implementation. This involves duplicating most of the old
parser code for this into a new class, StyleSheetHandler, that implements
the observer interface and builds up the same data structures as the old
parser did in response to the callbacks.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (208885 => 208886)


--- trunk/Source/WebCore/ChangeLog	2016-11-18 18:57:17 UTC (rev 208885)
+++ trunk/Source/WebCore/ChangeLog	2016-11-18 19:09:13 UTC (rev 208886)
@@ -1,3 +1,70 @@
+2016-11-18  Dave Hyatt  <[email protected]>
+
+        [CSS Parser] Hook up InspectorStyleSheet to the new CSS parser.
+        https://bugs.webkit.org/show_bug.cgi?id=164886
+
+        Reviewed by Dean Jackson.
+
+        * css/CSSGrammar.y.in:
+        Get rid of the CSSRuleSourceData type enum, since StyleRule's type
+        enum is exactly the same.
+
+        * css/CSSPropertySourceData.cpp:
+        (WebCore::CSSPropertySourceData::CSSPropertySourceData):
+        * css/CSSPropertySourceData.h:
+        Add a concept of disabled to CSSPropertySourceData. This is used for
+        commented out properties.
+
+        (WebCore::CSSRuleSourceData::create):
+        (WebCore::CSSRuleSourceData::createUnknown):
+        (WebCore::CSSRuleSourceData::CSSRuleSourceData):
+        Get rid of the CSSRuleSourceData type enum, since StyleRule's type
+        enum is exactly the same.
+
+        * css/parser/CSSParser.cpp:
+        (WebCore::CSSParserContext::CSSParserContext):
+        (WebCore::CSSParser::parseSheetForInspector):
+        (WebCore::CSSParser::parseDeclarationForInspector):
+        (WebCore::CSSParser::markSupportsRuleHeaderStart):
+        (WebCore::CSSParser::markRuleHeaderStart):
+        (WebCore::CSSParser::markPropertyEnd):
+        * css/parser/CSSParser.h:
+        Add functions that represent the new API for inspector sheet
+        and declaration parsing. Patch the old parse code to use StyleRule::Type
+        now that the CSSRuleSourceData type is gone.
+
+        * css/parser/CSSParserObserver.h:
+        Tweak the API for our memory management.
+
+        * inspector/InspectorStyleSheet.cpp:
+        (flattenSourceData):
+        (WebCore::parserContextForDocument):
+        (WebCore::StyleSheetHandler::StyleSheetHandler):
+        (WebCore::StyleSheetHandler::startRuleHeader):
+        (WebCore::StyleSheetHandler::setRuleHeaderEnd):
+        (WebCore::StyleSheetHandler::endRuleHeader):
+        (WebCore::StyleSheetHandler::observeSelector):
+        (WebCore::StyleSheetHandler::startRuleBody):
+        (WebCore::StyleSheetHandler::endRuleBody):
+        (WebCore::StyleSheetHandler::popRuleData):
+        (WebCore::fixUnparsedProperties):
+        (WebCore::StyleSheetHandler::fixUnparsedPropertyRanges):
+        (WebCore::StyleSheetHandler::observeProperty):
+        (WebCore::StyleSheetHandler::observeComment):
+        (WebCore::InspectorStyle::populateAllProperties):
+        (WebCore::isValidSelectorListString):
+        (WebCore::InspectorStyleSheet::ensureSourceData):
+        (WebCore::InspectorStyleSheetForInlineStyle::ensureParsedDataReady):
+        (WebCore::InspectorStyleSheetForInlineStyle::ruleSourceData):
+        (WebCore::createCSSParser): Deleted.
+        (WebCore::InspectorStyleSheetForInlineStyle::getStyleAttributeRanges): Deleted.
+        * inspector/InspectorStyleSheet.h:
+        (WebCore::InspectorStyleProperty::setRawTextFromStyleDeclaration):
+        Add the new implementation. This involves duplicating most of the old
+        parser code for this into a new class, StyleSheetHandler, that implements
+        the observer interface and builds up the same data structures as the old
+        parser did in response to the callbacks.
+
 2016-11-18  Dan Bernstein  <[email protected]>
 
         Tried to fix some non-macOS builds.

Modified: trunk/Source/WebCore/css/CSSGrammar.y.in (208885 => 208886)


--- trunk/Source/WebCore/css/CSSGrammar.y.in	2016-11-18 18:57:17 UTC (rev 208885)
+++ trunk/Source/WebCore/css/CSSGrammar.y.in	2016-11-18 19:09:13 UTC (rev 208886)
@@ -516,7 +516,7 @@
 
 before_import_rule:
     /* empty */ {
-        parser->markRuleHeaderStart(CSSRuleSourceData::IMPORT_RULE);
+        parser->markRuleHeaderStart(StyleRule::Import);
     }
     ;
 
@@ -654,7 +654,7 @@
 
 before_media_rule:
     /* empty */ {
-        parser->markRuleHeaderStart(CSSRuleSourceData::MEDIA_RULE);
+        parser->markRuleHeaderStart(StyleRule::Media);
     }
     ;
 
@@ -699,7 +699,7 @@
 
 before_supports_rule:
     /* empty */ {
-        parser->markRuleHeaderStart(CSSRuleSourceData::SUPPORTS_RULE);
+        parser->markRuleHeaderStart(StyleRule::Supports);
         parser->markSupportsRuleHeaderStart();
     }
     ;
@@ -767,7 +767,7 @@
 
 before_keyframes_rule:
     /* empty */ {
-        parser->markRuleHeaderStart(CSSRuleSourceData::KEYFRAMES_RULE);
+        parser->markRuleHeaderStart(StyleRule::Keyframe);
     }
     ;
 
@@ -829,7 +829,7 @@
 
 before_page_rule:
     /* empty */ {
-        parser->markRuleHeaderStart(CSSRuleSourceData::PAGE_RULE);
+        parser->markRuleHeaderStart(StyleRule::Page);
     }
     ;
 
@@ -942,7 +942,7 @@
 
 before_font_face_rule:
     /* empty */ {
-        parser->markRuleHeaderStart(CSSRuleSourceData::FONT_FACE_RULE);
+        parser->markRuleHeaderStart(StyleRule::FontFace);
     }
     ;
 
@@ -965,7 +965,7 @@
 before_viewport_rule:
     /* empty */ {
         parser->markViewportRuleBodyStart();
-        parser->markRuleHeaderStart(CSSRuleSourceData::VIEWPORT_RULE);
+        parser->markRuleHeaderStart(StyleRule::Viewport);
     }
     ;
 
@@ -991,7 +991,7 @@
 
 before_region_rule:
     /* empty */ {
-        parser->markRuleHeaderStart(CSSRuleSourceData::REGION_RULE);
+        parser->markRuleHeaderStart(StyleRule::Region);
     }
     ;
 
@@ -1024,7 +1024,7 @@
 
 before_selector_list:
     {
-        parser->markRuleHeaderStart(CSSRuleSourceData::STYLE_RULE);
+        parser->markRuleHeaderStart(StyleRule::Style);
         parser->markSelectorStart();
     }
   ;

Modified: trunk/Source/WebCore/css/CSSPropertySourceData.cpp (208885 => 208886)


--- trunk/Source/WebCore/css/CSSPropertySourceData.cpp	2016-11-18 18:57:17 UTC (rev 208885)
+++ trunk/Source/WebCore/css/CSSPropertySourceData.cpp	2016-11-18 19:09:13 UTC (rev 208886)
@@ -55,10 +55,11 @@
     return end - start;
 }
 
-CSSPropertySourceData::CSSPropertySourceData(const String& name, const String& value, bool important, bool parsedOk, const SourceRange& range)
+CSSPropertySourceData::CSSPropertySourceData(const String& name, const String& value, bool important, bool disabled, bool parsedOk, const SourceRange& range)
     : name(name)
     , value(value)
     , important(important)
+    , disabled(disabled)
     , parsedOk(parsedOk)
     , range(range)
 {
@@ -68,6 +69,7 @@
     : name(other.name)
     , value(other.value)
     , important(other.important)
+    , disabled(other.disabled)
     , parsedOk(other.parsedOk)
     , range(other.range)
 {
@@ -77,6 +79,7 @@
     : name(emptyString())
     , value(emptyString())
     , important(false)
+    , disabled(false)
     , parsedOk(false)
     , range(SourceRange(0, 0))
 {

Modified: trunk/Source/WebCore/css/CSSPropertySourceData.h (208885 => 208886)


--- trunk/Source/WebCore/css/CSSPropertySourceData.h	2016-11-18 18:57:17 UTC (rev 208885)
+++ trunk/Source/WebCore/css/CSSPropertySourceData.h	2016-11-18 19:09:13 UTC (rev 208886)
@@ -30,6 +30,7 @@
 
 #pragma once
 
+#include "StyleRule.h"
 #include <utility>
 #include <wtf/Forward.h>
 #include <wtf/HashMap.h>
@@ -51,7 +52,7 @@
 };
 
 struct CSSPropertySourceData {
-    CSSPropertySourceData(const String& name, const String& value, bool important, bool parsedOk, const SourceRange& range);
+    CSSPropertySourceData(const String& name, const String& value, bool important, bool disabled, bool parsedOk, const SourceRange&);
     CSSPropertySourceData(const CSSPropertySourceData& other);
     CSSPropertySourceData();
 
@@ -61,6 +62,7 @@
     String name;
     String value;
     bool important;
+    bool disabled;
     bool parsedOk;
     SourceRange range;
 };
@@ -79,21 +81,7 @@
 typedef Vector<SourceRange> SelectorRangeList;
 
 struct CSSRuleSourceData : public RefCounted<CSSRuleSourceData> {
-    enum Type {
-        UNKNOWN_RULE,
-        STYLE_RULE,
-        IMPORT_RULE,
-        MEDIA_RULE,
-        FONT_FACE_RULE,
-        PAGE_RULE,
-        KEYFRAMES_RULE,
-        REGION_RULE,
-        HOST_RULE,
-        VIEWPORT_RULE,
-        SUPPORTS_RULE,
-    };
-
-    static Ref<CSSRuleSourceData> create(Type type)
+    static Ref<CSSRuleSourceData> create(StyleRule::Type type)
     {
         return adoptRef(*new CSSRuleSourceData(type));
     }
@@ -100,17 +88,17 @@
 
     static Ref<CSSRuleSourceData> createUnknown()
     {
-        return adoptRef(*new CSSRuleSourceData(UNKNOWN_RULE));
+        return adoptRef(*new CSSRuleSourceData(StyleRule::Unknown));
     }
 
-    CSSRuleSourceData(Type type)
+    CSSRuleSourceData(StyleRule::Type type)
         : type(type)
     {
-        if (type == STYLE_RULE || type == FONT_FACE_RULE || type == PAGE_RULE)
+        if (type == StyleRule::Style || type == StyleRule::FontFace || type == StyleRule::Page)
             styleSourceData = CSSStyleSourceData::create();
     }
 
-    Type type;
+    StyleRule::Type type;
 
     // Range of the selector list in the enclosing source.
     SourceRange ruleHeaderRange;

Modified: trunk/Source/WebCore/css/parser/CSSParser.cpp (208885 => 208886)


--- trunk/Source/WebCore/css/parser/CSSParser.cpp	2016-11-18 18:57:17 UTC (rev 208885)
+++ trunk/Source/WebCore/css/parser/CSSParser.cpp	2016-11-18 19:09:13 UTC (rev 208886)
@@ -58,6 +58,7 @@
 #include "CSSPageRule.h"
 #include "CSSParserFastPaths.h"
 #include "CSSParserImpl.h"
+#include "CSSParserObserver.h"
 #include "CSSPendingSubstitutionValue.h"
 #include "CSSPrimitiveValue.h"
 #include "CSSPrimitiveValueMappings.h"
@@ -392,6 +393,11 @@
     m_logErrors = false;
 }
 
+void CSSParser::parseSheetForInspector(const CSSParserContext& context, StyleSheetContents* sheet, const String& string, CSSParserObserver& observer)
+{
+    return CSSParserImpl::parseStyleSheetForInspector(string, context, sheet, observer);
+}
+
 RefPtr<StyleRuleBase> CSSParser::parseRule(StyleSheetContents* sheet, const String& string)
 {
     if (m_context.useNewParser && m_context.mode != UASheetMode)
@@ -1515,6 +1521,11 @@
     return ok;
 }
 
+void CSSParser::parseDeclarationForInspector(const CSSParserContext& context, const String& string, CSSParserObserver& observer)
+{
+    CSSParserImpl::parseDeclarationListForInspector(string, context, observer);
+}
+
 static inline void filterProperties(bool important, const ParsedPropertyVector& input, Vector<CSSProperty, 256>& output, size_t& unusedEntries, std::bitset<numCSSProperties>& seenProperties, HashSet<AtomicString>& seenCustomProperties)
 {
     // Add properties in reverse order so that highest priority definitions are reached first. Duplicate definitions can then be ignored when found.
@@ -12925,7 +12936,7 @@
     if (!m_supportsRuleDataStack)
         m_supportsRuleDataStack = std::make_unique<RuleSourceDataList>();
 
-    auto data = ""
+    auto data = ""
     data->ruleHeaderRange.start = tokenStartOffset();
     m_supportsRuleDataStack->append(WTFMove(data));
 }
@@ -13325,7 +13336,7 @@
     fixUnparsedProperties<UChar>(m_dataStart16.get() + m_parsedTextPrefixLength, ruleData);
 }
 
-void CSSParser::markRuleHeaderStart(CSSRuleSourceData::Type ruleType)
+void CSSParser::markRuleHeaderStart(StyleRule::Type ruleType)
 {
     if (!isExtractingSourceData())
         return;
@@ -13450,7 +13461,7 @@
         // The property range is relative to the declaration start offset.
         SourceRange& topRuleBodyRange = m_currentRuleDataStack->last()->ruleBodyRange;
         m_currentRuleDataStack->last()->styleSourceData->propertyData.append(
-            CSSPropertySourceData(name, value, isImportantFound, isPropertyParsed, SourceRange(start - topRuleBodyRange.start, end - topRuleBodyRange.start)));
+            CSSPropertySourceData(name, value, isImportantFound, false, isPropertyParsed, SourceRange(start - topRuleBodyRange.start, end - topRuleBodyRange.start)));
     }
     resetPropertyRange();
 }

Modified: trunk/Source/WebCore/css/parser/CSSParser.h (208885 => 208886)


--- trunk/Source/WebCore/css/parser/CSSParser.h	2016-11-18 18:57:17 UTC (rev 208885)
+++ trunk/Source/WebCore/css/parser/CSSParser.h	2016-11-18 19:09:13 UTC (rev 208886)
@@ -50,6 +50,7 @@
 class AnimationParseContext;
 class CSSBorderImageSliceValue;
 class CSSContentDistributionValue;
+class CSSParserObserver;
 class CSSPrimitiveValue;
 class CSSSelectorList;
 class CSSValue;
@@ -132,6 +133,9 @@
     RefPtr<StyleKeyframe> parseKeyframeRule(StyleSheetContents*, const String&);
     bool parseSupportsCondition(const String&);
 
+    static void parseSheetForInspector(const CSSParserContext&, StyleSheetContents*, const String&, CSSParserObserver&);
+    static void parseDeclarationForInspector(const CSSParserContext&, const String&, CSSParserObserver&);
+
     static ParseResult parseValue(MutableStyleProperties&, CSSPropertyID, const String&, bool important, const CSSParserContext&, StyleSheetContents*);
     static ParseResult parseCustomPropertyValue(MutableStyleProperties&, const AtomicString& propertyName, const String&, bool important, const CSSParserContext&, StyleSheetContents* contextStyleSheet);
 
@@ -141,6 +145,7 @@
     static RefPtr<CSSValueList> parseFontFaceValue(const AtomicString&);
     RefPtr<CSSPrimitiveValue> parseValidPrimitive(CSSValueID ident, ValueWithCalculation&);
 
+    // FIXME-NEWPARSER: Can remove the last two arguments once the new parser is turned on.
     WEBCORE_EXPORT bool parseDeclaration(MutableStyleProperties&, const String&, RefPtr<CSSRuleSourceData>&&, StyleSheetContents* contextStyleSheet);
     static Ref<ImmutableStyleProperties> parseInlineStyleDeclaration(const String&, Element*);
 
@@ -456,7 +461,7 @@
     RuleSourceDataList* m_ruleSourceDataResult { nullptr };
 
     void fixUnparsedPropertyRanges(CSSRuleSourceData&);
-    void markRuleHeaderStart(CSSRuleSourceData::Type);
+    void markRuleHeaderStart(StyleRule::Type);
     void markRuleHeaderEnd();
 
     void startNestedSelectorList() { ++m_nestedSelectorLevel; }

Modified: trunk/Source/WebCore/css/parser/CSSParserObserver.h (208885 => 208886)


--- trunk/Source/WebCore/css/parser/CSSParserObserver.h	2016-11-18 18:57:17 UTC (rev 208885)
+++ trunk/Source/WebCore/css/parser/CSSParserObserver.h	2016-11-18 19:09:13 UTC (rev 208886)
@@ -42,11 +42,6 @@
     virtual void endRuleBody(unsigned offset) = 0;
     virtual void observeProperty(unsigned startOffset, unsigned endOffset, bool isImportant, bool isParsed) = 0;
     virtual void observeComment(unsigned startOffset, unsigned endOffset) = 0;
-    // FIXME: Unused, should be removed
-    virtual void startMediaQueryExp(unsigned offset) = 0;
-    virtual void endMediaQueryExp(unsigned offset) = 0;
-    virtual void startMediaQuery() = 0;
-    virtual void endMediaQuery() = 0;
 };
 
 } // namespace WebCore

Modified: trunk/Source/WebCore/inspector/InspectorStyleSheet.cpp (208885 => 208886)


--- trunk/Source/WebCore/inspector/InspectorStyleSheet.cpp	2016-11-18 18:57:17 UTC (rev 208885)
+++ trunk/Source/WebCore/inspector/InspectorStyleSheet.cpp	2016-11-18 19:09:13 UTC (rev 208886)
@@ -29,6 +29,7 @@
 #include "CSSKeyframesRule.h"
 #include "CSSMediaRule.h"
 #include "CSSParser.h"
+#include "CSSParserObserver.h"
 #include "CSSPropertyNames.h"
 #include "CSSPropertySourceData.h"
 #include "CSSRule.h"
@@ -100,11 +101,11 @@
 static void flattenSourceData(RuleSourceDataList& dataList, RuleSourceDataList& target)
 {
     for (auto& data : dataList) {
-        if (data->type == CSSRuleSourceData::STYLE_RULE)
+        if (data->type == WebCore::StyleRule::Style)
             target.append(data.copyRef());
-        else if (data->type == CSSRuleSourceData::MEDIA_RULE)
+        else if (data->type == WebCore::StyleRule::Media)
             flattenSourceData(data->childRules, target);
-        else if (data->type == CSSRuleSourceData::SUPPORTS_RULE)
+        else if (data->type == WebCore::StyleRule::Supports)
             flattenSourceData(data->childRules, target);
     }
 }
@@ -136,6 +137,247 @@
 
 namespace WebCore {
 
+static CSSParserContext parserContextForDocument(Document* document)
+{
+    return document ? CSSParserContext(*document) : strictCSSParserContext();
+}
+
+class StyleSheetHandler : public CSSParserObserver {
+public:
+    StyleSheetHandler(const String& parsedText, Document* document, RuleSourceDataList* result)
+        : m_parsedText(parsedText)
+        , m_document(document)
+        , m_ruleSourceDataResult(result)
+    {
+        ASSERT(m_ruleSourceDataResult);
+    }
+    
+private:
+    void startRuleHeader(StyleRule::Type, unsigned) override;
+    void endRuleHeader(unsigned) override;
+    void observeSelector(unsigned startOffset, unsigned endOffset) override;
+    void startRuleBody(unsigned) override;
+    void endRuleBody(unsigned) override;
+    void observeProperty(unsigned startOffset, unsigned endOffset, bool isImportant, bool isParsed) override;
+    void observeComment(unsigned startOffset, unsigned endOffset) override;
+    
+    Ref<CSSRuleSourceData> popRuleData();
+    template <typename CharacterType> inline void setRuleHeaderEnd(const CharacterType*, unsigned);
+    void fixUnparsedPropertyRanges(CSSRuleSourceData*);
+    
+    const String& m_parsedText;
+    Document* m_document;
+    
+    RuleSourceDataList m_currentRuleDataStack;
+    RefPtr<CSSRuleSourceData> m_currentRuleData;
+    RuleSourceDataList* m_ruleSourceDataResult { nullptr };
+};
+
+void StyleSheetHandler::startRuleHeader(StyleRule::Type type, unsigned offset)
+{
+    // Pop off data for a previous invalid rule.
+    if (m_currentRuleData)
+        m_currentRuleDataStack.removeLast();
+    
+    auto data = ""
+    data->ruleHeaderRange.start = offset;
+    m_currentRuleData = data.copyRef();
+    m_currentRuleDataStack.append(WTFMove(data));
+}
+
+template <typename CharacterType> inline void StyleSheetHandler::setRuleHeaderEnd(const CharacterType* dataStart, unsigned listEndOffset)
+{
+    while (listEndOffset > 1) {
+        if (isHTMLSpace<CharacterType>(*(dataStart + listEndOffset - 1)))
+            --listEndOffset;
+        else
+            break;
+    }
+
+    m_currentRuleDataStack.last()->ruleHeaderRange.end = listEndOffset;
+    if (!m_currentRuleDataStack.last()->selectorRanges.isEmpty())
+        m_currentRuleDataStack.last()->selectorRanges.last().end = listEndOffset;
+}
+
+void StyleSheetHandler::endRuleHeader(unsigned offset)
+{
+    ASSERT(!m_currentRuleDataStack.isEmpty());
+    
+    if (m_parsedText.is8Bit())
+        setRuleHeaderEnd<LChar>(m_parsedText.characters8(), offset);
+    else
+        setRuleHeaderEnd<UChar>(m_parsedText.characters16(), offset);
+}
+
+void StyleSheetHandler::observeSelector(unsigned startOffset, unsigned endOffset)
+{
+    ASSERT(m_currentRuleDataStack.size());
+    m_currentRuleDataStack.last()->selectorRanges.append(SourceRange(startOffset, endOffset));
+}
+
+void StyleSheetHandler::startRuleBody(unsigned offset)
+{
+    m_currentRuleData = nullptr;
+    ASSERT(!m_currentRuleDataStack.isEmpty());
+    
+    // Skip the rule body opening brace.
+    if (m_parsedText[offset] == '{')
+        ++offset;
+
+    m_currentRuleDataStack.last()->ruleBodyRange.start = offset;
+}
+
+void StyleSheetHandler::endRuleBody(unsigned offset)
+{
+    ASSERT(!m_currentRuleDataStack.isEmpty());
+    m_currentRuleDataStack.last()->ruleBodyRange.end = offset;
+    auto rule = popRuleData();
+    fixUnparsedPropertyRanges(rule.ptr());
+    if (m_currentRuleDataStack.isEmpty())
+        m_ruleSourceDataResult->append(WTFMove(rule));
+    else
+        m_currentRuleDataStack.last()->childRules.append(WTFMove(rule));
+}
+
+Ref<CSSRuleSourceData> StyleSheetHandler::popRuleData()
+{
+    ASSERT(!m_currentRuleDataStack.isEmpty());
+    m_currentRuleData = nullptr;
+    auto data = ""
+    m_currentRuleDataStack.removeLast();
+    return data;
+}
+
+template <typename CharacterType>
+static inline void fixUnparsedProperties(const CharacterType* characters, CSSRuleSourceData* ruleData)
+{
+    Vector<CSSPropertySourceData>& propertyData = ruleData->styleSourceData->propertyData;
+    unsigned size = propertyData.size();
+    if (!size)
+        return;
+    
+    unsigned styleStart = ruleData->ruleBodyRange.start;
+    
+    CSSPropertySourceData* nextData = &(propertyData.at(0));
+    for (unsigned i = 0; i < size; ++i) {
+        CSSPropertySourceData* currentData = nextData;
+        nextData = i < size - 1 ? &(propertyData.at(i + 1)) : nullptr;
+        
+        if (currentData->parsedOk)
+            continue;
+        if (currentData->range.end > 0 && characters[styleStart + currentData->range.end - 1] == ';')
+            continue;
+        
+        unsigned propertyEnd;
+        if (!nextData)
+            propertyEnd = ruleData->ruleBodyRange.end - 1;
+        else
+            propertyEnd = styleStart + nextData->range.start - 1;
+        
+        while (isHTMLSpace<CharacterType>(characters[propertyEnd]))
+            --propertyEnd;
+        
+        // propertyEnd points at the last property text character.
+        unsigned newPropertyEnd = propertyEnd + styleStart + 1;
+        if (currentData->range.end != newPropertyEnd) {
+            currentData->range.end = newPropertyEnd;
+            unsigned valueStart = styleStart + currentData->range.start + currentData->name.length();
+            while (valueStart < propertyEnd && characters[valueStart] != ':')
+                ++valueStart;
+            
+            // Shift past the ':'.
+            if (valueStart < propertyEnd)
+                ++valueStart;
+
+            while (valueStart < propertyEnd && isHTMLSpace<CharacterType>(characters[valueStart]))
+                ++valueStart;
+            
+            // Need to exclude the trailing ';' from the property value.
+            currentData->value = String(characters + valueStart, propertyEnd - valueStart + (characters[propertyEnd] == ';' ? 0 : 1));
+        }
+    }
+}
+
+void StyleSheetHandler::fixUnparsedPropertyRanges(CSSRuleSourceData* ruleData)
+{
+    if (!ruleData->styleSourceData)
+        return;
+    
+    if (m_parsedText.is8Bit()) {
+        fixUnparsedProperties<LChar>(m_parsedText.characters8(), ruleData);
+        return;
+    }
+    
+    fixUnparsedProperties<UChar>(m_parsedText.characters16(), ruleData);
+}
+
+void StyleSheetHandler::observeProperty(unsigned startOffset, unsigned endOffset, bool isImportant, bool isParsed)
+{
+    if (m_currentRuleDataStack.isEmpty() || !m_currentRuleDataStack.last()->styleSourceData)
+        return;
+    
+    ASSERT(endOffset <= m_parsedText.length());
+    
+    // Include semicolon in the property text.
+    if (endOffset < m_parsedText.length() && m_parsedText[endOffset] == ';')
+        ++endOffset;
+    
+    ASSERT(startOffset < endOffset);
+    String propertyString = m_parsedText.substring(startOffset, endOffset - startOffset).stripWhiteSpace();
+    if (propertyString.endsWith(';'))
+        propertyString = propertyString.left(propertyString.length() - 1);
+    size_t colonIndex = propertyString.find(':');
+    ASSERT(colonIndex != notFound);
+
+    String name = propertyString.left(colonIndex).stripWhiteSpace();
+    String value = propertyString.substring(colonIndex + 1, propertyString.length()).stripWhiteSpace();
+    
+    // FIXME-NEWPARSER: The property range is relative to the declaration start offset, but no
+    // good reason for it, and it complicates fixUnparsedProperties.
+    SourceRange& topRuleBodyRange = m_currentRuleDataStack.last()->ruleBodyRange;
+    m_currentRuleDataStack.last()->styleSourceData->propertyData.append(CSSPropertySourceData(name, value, isImportant, false, isParsed, SourceRange(startOffset - topRuleBodyRange.start, endOffset - topRuleBodyRange.start)));
+}
+
+void StyleSheetHandler::observeComment(unsigned startOffset, unsigned endOffset)
+{
+    ASSERT(endOffset <= m_parsedText.length());
+
+    if (m_currentRuleDataStack.isEmpty() || !m_currentRuleDataStack.last()->ruleHeaderRange.end || !m_currentRuleDataStack.last()->styleSourceData)
+        return;
+    
+    // The lexer is not inside a property AND it is scanning a declaration-aware
+    // rule body.
+    String commentText = m_parsedText.substring(startOffset, endOffset - startOffset);
+    
+    ASSERT(commentText.startsWith("/*"));
+    commentText = commentText.substring(2);
+    
+    // Require well-formed comments.
+    if (!commentText.endsWith("*/"))
+        return;
+    commentText = commentText.substring(0, commentText.length() - 2).stripWhiteSpace();
+    if (commentText.isEmpty())
+        return;
+    
+    // FIXME: Use the actual rule type rather than STYLE_RULE?
+    RuleSourceDataList sourceData;
+    
+    StyleSheetHandler handler(commentText, m_document, &sourceData);
+    CSSParser::parseDeclarationForInspector(parserContextForDocument(m_document), commentText, handler);
+    Vector<CSSPropertySourceData>& commentPropertyData = sourceData.first()->styleSourceData->propertyData;
+    if (commentPropertyData.size() != 1)
+        return;
+    CSSPropertySourceData& propertyData = commentPropertyData.at(0);
+    bool parsedOk = propertyData.parsedOk || propertyData.name.startsWith("-moz-") || propertyData.name.startsWith("-o-") || propertyData.name.startsWith("-webkit-") || propertyData.name.startsWith("-ms-");
+    if (!parsedOk || propertyData.range.length() != commentText.length())
+        return;
+    
+    // FIXME-NEWPARSER: The property range is relative to the declaration start offset, but no
+    // good reason for it, and it complicates fixUnparsedProperties.
+    SourceRange& topRuleBodyRange = m_currentRuleDataStack.last()->ruleBodyRange;
+    m_currentRuleDataStack.last()->styleSourceData->propertyData.append(CSSPropertySourceData(propertyData.name, propertyData.value, false, true, true, SourceRange(startOffset - topRuleBodyRange.start, endOffset - topRuleBodyRange.start)));
+}
+
 enum MediaListSource {
     MediaListSourceLinkedSheet,
     MediaListSourceInlineSheet,
@@ -273,11 +515,6 @@
     }
 }
 
-static std::unique_ptr<CSSParser> createCSSParser(Document* document)
-{
-    return std::make_unique<CSSParser>(document ? CSSParserContext(*document) : strictCSSParserContext());
-}
-
 Ref<InspectorStyle> InspectorStyle::create(const InspectorCSSId& styleId, RefPtr<CSSStyleDeclaration>&& style, InspectorStyleSheet* parentStyleSheet)
 {
     return adoptRef(*new InspectorStyle(styleId, WTFMove(style), parentStyleSheet));
@@ -363,7 +600,7 @@
         ASSERT(!styleDeclarationOrException.hasException());
         String styleDeclaration = styleDeclarationOrException.hasException() ? emptyString() : styleDeclarationOrException.releaseReturnValue();
         for (auto& sourceData : *sourcePropertyData) {
-            InspectorStyleProperty p(sourceData, true, false);
+            InspectorStyleProperty p(sourceData, true, sourceData.disabled);
             p.setRawTextFromStyleDeclaration(styleDeclaration);
             result->append(p);
             sourcePropertyNames.add(lowercasePropertyName(sourceData.name));
@@ -373,7 +610,7 @@
     for (int i = 0, size = m_style->length(); i < size; ++i) {
         String name = m_style->item(i);
         if (sourcePropertyNames.add(lowercasePropertyName(name)))
-            result->append(InspectorStyleProperty(CSSPropertySourceData(name, m_style->getPropertyValue(name), !m_style->getPropertyPriority(name).isEmpty(), true, SourceRange()), false, false));
+            result->append(InspectorStyleProperty(CSSPropertySourceData(name, m_style->getPropertyValue(name), !m_style->getPropertyPriority(name).isEmpty(), false, true, SourceRange()), false, false));
     }
 }
 
@@ -635,7 +872,8 @@
 static bool isValidSelectorListString(const String& selector, Document* document)
 {
     CSSSelectorList selectorList;
-    createCSSParser(document)->parseSelector(selector, selectorList);
+    CSSParser parser(parserContextForDocument(document));
+    parser.parseSelector(selector, selectorList);
     return selectorList.isValid();
 }
 
@@ -1089,7 +1327,13 @@
 
     RefPtr<StyleSheetContents> newStyleSheet = StyleSheetContents::create();
     auto ruleSourceDataResult = std::make_unique<RuleSourceDataList>();
-    createCSSParser(m_pageStyleSheet->ownerDocument())->parseSheet(newStyleSheet.get(), m_parsedStyleSheet->text(), TextPosition(), ruleSourceDataResult.get(), false);
+    
+    CSSParserContext context(parserContextForDocument(m_pageStyleSheet->ownerDocument()));
+    if (context.useNewParser) {
+        StyleSheetHandler handler(m_parsedStyleSheet->text(), m_pageStyleSheet->ownerDocument(), ruleSourceDataResult.get());
+        CSSParser::parseSheetForInspector(context, newStyleSheet.get(), m_parsedStyleSheet->text(), handler);
+    } else
+        CSSParser(context).parseSheet(newStyleSheet.get(), m_parsedStyleSheet->text(), TextPosition(), ruleSourceDataResult.get(), false);
     m_parsedStyleSheet->setSourceData(WTFMove(ruleSourceDataResult));
     return m_parsedStyleSheet->hasSourceData();
 }
@@ -1295,11 +1539,10 @@
     if (m_ruleSourceData)
         return true;
 
-    m_ruleSourceData = CSSRuleSourceData::create(CSSRuleSourceData::STYLE_RULE);
-    bool success = getStyleAttributeRanges(m_ruleSourceData.get());
-    if (!success)
+    if (!m_element->isStyledElement())
         return false;
 
+    m_ruleSourceData = ruleSourceData();
     return true;
 }
 
@@ -1319,20 +1562,27 @@
     return m_element->getAttribute("style").string();
 }
 
-bool InspectorStyleSheetForInlineStyle::getStyleAttributeRanges(CSSRuleSourceData* result) const
+Ref<CSSRuleSourceData> InspectorStyleSheetForInlineStyle::ruleSourceData() const
 {
-    if (!m_element->isStyledElement())
-        return false;
-
     if (m_styleText.isEmpty()) {
+        auto result = CSSRuleSourceData::create(StyleRule::Style);
         result->ruleBodyRange.start = 0;
         result->ruleBodyRange.end = 0;
-        return true;
+        return result;
     }
 
+    CSSParserContext context(parserContextForDocument(&m_element->document()));
+    if (context.useNewParser) {
+        RuleSourceDataList ruleSourceDataResult;
+        StyleSheetHandler handler(m_styleText, &m_element->document(), &ruleSourceDataResult);
+        CSSParser::parseDeclarationForInspector(context, m_styleText, handler);
+        return WTFMove(ruleSourceDataResult.first());
+    }
+    
+    auto result = CSSRuleSourceData::create(StyleRule::Style);
     auto tempDeclaration = MutableStyleProperties::create();
-    createCSSParser(&m_element->document())->parseDeclaration(tempDeclaration, m_styleText, result, nullptr);
-    return true;
+    CSSParser(context).parseDeclaration(tempDeclaration, m_styleText, result.ptr(), nullptr);
+    return result;
 }
 
 } // namespace WebCore

Modified: trunk/Source/WebCore/inspector/InspectorStyleSheet.h (208885 => 208886)


--- trunk/Source/WebCore/inspector/InspectorStyleSheet.h	2016-11-18 18:57:17 UTC (rev 208885)
+++ trunk/Source/WebCore/inspector/InspectorStyleSheet.h	2016-11-18 19:09:13 UTC (rev 208886)
@@ -251,7 +251,7 @@
 private:
     CSSStyleDeclaration* inlineStyle() const;
     const String& elementStyleText() const;
-    bool getStyleAttributeRanges(CSSRuleSourceData* result) const;
+    Ref<CSSRuleSourceData> ruleSourceData() const;
 
     RefPtr<Element> m_element;
     RefPtr<CSSRuleSourceData> m_ruleSourceData;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to