DevTools: [CSS] cache lineEndings in InspectorStyleSheet

lineEndings are used for creation of source range in the protocol
CSS domain. This significantly speeds up all CSS domain protocol methods.

BUG=423823
R=vsevik, pfeldman

Review URL: https://codereview.chromium.org/666883002

git-svn-id: svn://svn.chromium.org/blink/trunk@183981 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 38c55b69
...@@ -18,6 +18,9 @@ Dump styles after editing: ...@@ -18,6 +18,9 @@ Dump styles after editing:
[expanded] [expanded]
element.style { () element.style { ()
[expanded]
#testDiv, my-custom-tag { (inspector-stylesheet:1 -> inspector-stylesheet:1:1)
[expanded] [expanded]
#testDiv { (style.css:1 -> style.css:1:1) #testDiv { (style.css:1 -> style.css:1:1)
color: green; color: green;
...@@ -32,6 +35,9 @@ Dump styles after inspector was reopened: ...@@ -32,6 +35,9 @@ Dump styles after inspector was reopened:
[expanded] [expanded]
element.style { () element.style { ()
[expanded]
#testdiv, my-custom-tag { ()
[expanded] [expanded]
#testDiv { (style.css:1 -> style.css:1:1) #testDiv { (style.css:1 -> style.css:1:1)
color: green; color: green;
......
...@@ -44,9 +44,9 @@ function test() ...@@ -44,9 +44,9 @@ function test()
function testFirstOpen() function testFirstOpen()
{ {
InspectorTest.addResult("Dump initial styles:"); InspectorTest.addResult("Dump initial styles:");
dump(step2); dump(onStylesDumped);
function step2() function onStylesDumped()
{ {
treeElement = InspectorTest.getMatchedStylePropertyTreeItem("color"); treeElement = InspectorTest.getMatchedStylePropertyTreeItem("color");
treeElement.startEditing(); treeElement.startEditing();
...@@ -57,14 +57,29 @@ function test() ...@@ -57,14 +57,29 @@ function test()
treeElement.valueElement.textContent = "green"; treeElement.valueElement.textContent = "green";
treeElement.valueElement.firstChild.select(); treeElement.valueElement.firstChild.select();
treeElement.valueElement.dispatchEvent(InspectorTest.createKeyEvent("Enter")); treeElement.valueElement.dispatchEvent(InspectorTest.createKeyEvent("Enter"));
InspectorTest.runAfterPendingDispatches(step3); InspectorTest.waitForStyleApplied(onPropertyEdited);
} }
function step3() function onPropertyEdited()
{
InspectorTest.addNewRule("#testDiv, my-custom-tag", onFirstRuleAdded);
}
function onFirstRuleAdded()
{
InspectorTest.addNewRule("#testDiv, another-custom-tag", onSecondRuleAdded);
}
function onSecondRuleAdded()
{
WebInspector.domModel.undo(onRuleRemoved);
}
function onRuleRemoved()
{ {
InspectorTest.selectNodeAndWaitForStyles("other", step4); InspectorTest.selectNodeAndWaitForStyles("other", step4);
} }
function step4() function step4()
{ {
InspectorTest.addResult("Dump styles after editing:"); InspectorTest.addResult("Dump styles after editing:");
...@@ -81,7 +96,7 @@ function test() ...@@ -81,7 +96,7 @@ function test()
function dump(callback) function dump(callback)
{ {
InspectorTest.selectNodeAndWaitForStyles("testDiv", step2); InspectorTest.selectNodeAndWaitForStyles("testDiv", step2);
function step2() function step2()
{ {
InspectorTest.dumpSelectedElementStyles(true); InspectorTest.dumpSelectedElementStyles(true);
......
...@@ -496,7 +496,7 @@ enum MediaListSource { ...@@ -496,7 +496,7 @@ enum MediaListSource {
MediaListSourceImportRule MediaListSourceImportRule
}; };
static PassRefPtr<TypeBuilder::CSS::SourceRange> buildSourceRangeObject(const SourceRange& range, Vector<unsigned>* lineEndings) static PassRefPtr<TypeBuilder::CSS::SourceRange> buildSourceRangeObject(const SourceRange& range, const LineEndings* lineEndings)
{ {
if (!lineEndings) if (!lineEndings)
return nullptr; return nullptr;
...@@ -547,7 +547,7 @@ PassRefPtr<TypeBuilder::CSS::CSSStyle> InspectorStyle::buildObjectForStyle() con ...@@ -547,7 +547,7 @@ PassRefPtr<TypeBuilder::CSS::CSSStyle> InspectorStyle::buildObjectForStyle() con
RefPtrWillBeRawPtr<CSSRuleSourceData> sourceData = extractSourceData(); RefPtrWillBeRawPtr<CSSRuleSourceData> sourceData = extractSourceData();
if (sourceData) if (sourceData)
result->setRange(buildSourceRangeObject(sourceData->ruleBodyRange, m_parentStyleSheet->lineEndings().get())); result->setRange(buildSourceRangeObject(sourceData->ruleBodyRange, m_parentStyleSheet->lineEndings()));
return result.release(); return result.release();
} }
...@@ -694,7 +694,6 @@ PassRefPtr<TypeBuilder::CSS::CSSStyle> InspectorStyle::styleWithProperties() con ...@@ -694,7 +694,6 @@ PassRefPtr<TypeBuilder::CSS::CSSStyle> InspectorStyle::styleWithProperties() con
RefPtr<Array<TypeBuilder::CSS::CSSProperty> > propertiesObject = Array<TypeBuilder::CSS::CSSProperty>::create(); RefPtr<Array<TypeBuilder::CSS::CSSProperty> > propertiesObject = Array<TypeBuilder::CSS::CSSProperty>::create();
RefPtr<Array<TypeBuilder::CSS::ShorthandEntry> > shorthandEntries = Array<TypeBuilder::CSS::ShorthandEntry>::create(); RefPtr<Array<TypeBuilder::CSS::ShorthandEntry> > shorthandEntries = Array<TypeBuilder::CSS::ShorthandEntry>::create();
HashSet<String> foundShorthands; HashSet<String> foundShorthands;
OwnPtr<Vector<unsigned> > lineEndings(m_parentStyleSheet ? m_parentStyleSheet->lineEndings() : PassOwnPtr<Vector<unsigned> >());
RefPtrWillBeRawPtr<CSSRuleSourceData> sourceData = extractSourceData(); RefPtrWillBeRawPtr<CSSRuleSourceData> sourceData = extractSourceData();
WillBeHeapVector<InspectorStyleProperty> properties; WillBeHeapVector<InspectorStyleProperty> properties;
...@@ -718,7 +717,7 @@ PassRefPtr<TypeBuilder::CSS::CSSStyle> InspectorStyle::styleWithProperties() con ...@@ -718,7 +717,7 @@ PassRefPtr<TypeBuilder::CSS::CSSStyle> InspectorStyle::styleWithProperties() con
if (propertyEntry.important) if (propertyEntry.important)
property->setImportant(true); property->setImportant(true);
if (it->hasSource) { if (it->hasSource) {
property->setRange(buildSourceRangeObject(propertyEntry.range, lineEndings.get())); property->setRange(buildSourceRangeObject(propertyEntry.range, m_parentStyleSheet ? m_parentStyleSheet->lineEndings() : nullptr));
if (!propertyEntry.disabled) { if (!propertyEntry.disabled) {
ASSERT_UNUSED(sourceData, sourceData); ASSERT_UNUSED(sourceData, sourceData);
property->setImplicit(false); property->setImplicit(false);
...@@ -859,6 +858,7 @@ void InspectorStyle::trace(Visitor* visitor) ...@@ -859,6 +858,7 @@ void InspectorStyle::trace(Visitor* visitor)
InspectorStyleSheetBase::InspectorStyleSheetBase(const String& id, Listener* listener) InspectorStyleSheetBase::InspectorStyleSheetBase(const String& id, Listener* listener)
: m_id(id) : m_id(id)
, m_listener(listener) , m_listener(listener)
, m_lineEndings(adoptPtr(new LineEndings()))
{ {
} }
...@@ -880,8 +880,9 @@ bool InspectorStyleSheetBase::getStyleText(const InspectorCSSId& id, String* tex ...@@ -880,8 +880,9 @@ bool InspectorStyleSheetBase::getStyleText(const InspectorCSSId& id, String* tex
return inspectorStyle->styleText(text); return inspectorStyle->styleText(text);
} }
void InspectorStyleSheetBase::fireStyleSheetChanged() void InspectorStyleSheetBase::onStyleSheetTextChanged()
{ {
m_lineEndings = adoptPtrWillBeNoop(new LineEndings());
if (listener()) if (listener())
listener()->styleSheetChanged(this); listener()->styleSheetChanged(this);
} }
...@@ -915,17 +916,19 @@ PassRefPtr<TypeBuilder::CSS::CSSStyle> InspectorStyleSheetBase::buildObjectForSt ...@@ -915,17 +916,19 @@ PassRefPtr<TypeBuilder::CSS::CSSStyle> InspectorStyleSheetBase::buildObjectForSt
return result.release(); return result.release();
} }
PassOwnPtr<Vector<unsigned> > InspectorStyleSheetBase::lineEndings() const LineEndings* InspectorStyleSheetBase::lineEndings()
{ {
if (m_lineEndings->size() > 0)
return m_lineEndings.get();
String text; String text;
if (!getText(&text)) if (getText(&text))
return PassOwnPtr<Vector<unsigned> >(); m_lineEndings = WTF::lineEndings(text);
return WTF::lineEndings(text); return m_lineEndings.get();
} }
bool InspectorStyleSheetBase::lineNumberAndColumnToOffset(unsigned lineNumber, unsigned columnNumber, unsigned* offset) bool InspectorStyleSheetBase::lineNumberAndColumnToOffset(unsigned lineNumber, unsigned columnNumber, unsigned* offset)
{ {
OwnPtr<Vector<unsigned> > endings = lineEndings(); const LineEndings* endings = lineEndings();
if (lineNumber >= endings->size()) if (lineNumber >= endings->size())
return false; return false;
unsigned charactersInLine = lineNumber > 0 ? endings->at(lineNumber) - endings->at(lineNumber - 1) - 1 : endings->at(0); unsigned charactersInLine = lineNumber > 0 ? endings->at(lineNumber) - endings->at(lineNumber - 1) - 1 : endings->at(0);
...@@ -1037,7 +1040,7 @@ bool InspectorStyleSheet::setText(const String& text, ExceptionState& exceptionS ...@@ -1037,7 +1040,7 @@ bool InspectorStyleSheet::setText(const String& text, ExceptionState& exceptionS
if (listener()) if (listener())
listener()->didReparseStyleSheet(); listener()->didReparseStyleSheet();
fireStyleSheetChanged(); onStyleSheetTextChanged();
m_pageStyleSheet->ownerDocument()->styleResolverChanged(FullStyleUpdate); m_pageStyleSheet->ownerDocument()->styleResolverChanged(FullStyleUpdate);
return true; return true;
} }
...@@ -1075,7 +1078,7 @@ bool InspectorStyleSheet::setRuleSelector(const InspectorCSSId& id, const String ...@@ -1075,7 +1078,7 @@ bool InspectorStyleSheet::setRuleSelector(const InspectorCSSId& id, const String
String sheetText = m_parsedStyleSheet->text(); String sheetText = m_parsedStyleSheet->text();
sheetText.replace(sourceData->ruleHeaderRange.start, sourceData->ruleHeaderRange.length(), selector); sheetText.replace(sourceData->ruleHeaderRange.start, sourceData->ruleHeaderRange.length(), selector);
updateText(sheetText); updateText(sheetText);
fireStyleSheetChanged(); onStyleSheetTextChanged();
return true; return true;
} }
...@@ -1211,10 +1214,10 @@ CSSStyleRule* InspectorStyleSheet::addRule(const String& ruleText, const SourceR ...@@ -1211,10 +1214,10 @@ CSSStyleRule* InspectorStyleSheet::addRule(const String& ruleText, const SourceR
text.insert(ruleText, location.start); text.insert(ruleText, location.start);
m_parsedStyleSheet->setText(text); updateText(text);
m_flatRules.clear(); m_flatRules.clear();
fireStyleSheetChanged(); onStyleSheetTextChanged();
return styleRule; return styleRule;
} }
...@@ -1261,9 +1264,9 @@ bool InspectorStyleSheet::deleteRule(const InspectorCSSId& id, const String& old ...@@ -1261,9 +1264,9 @@ bool InspectorStyleSheet::deleteRule(const InspectorCSSId& id, const String& old
if (exceptionState.hadException()) if (exceptionState.hadException())
return false; return false;
m_parsedStyleSheet->setText(oldText); updateText(oldText);
m_flatRules.clear(); m_flatRules.clear();
fireStyleSheetChanged(); onStyleSheetTextChanged();
return true; return true;
} }
...@@ -1275,7 +1278,6 @@ void InspectorStyleSheet::updateText(const String& newText) ...@@ -1275,7 +1278,6 @@ void InspectorStyleSheet::updateText(const String& newText)
m_parsedStyleSheet->setText(newText); m_parsedStyleSheet->setText(newText);
} }
CSSStyleRule* InspectorStyleSheet::ruleForId(const InspectorCSSId& id) const CSSStyleRule* InspectorStyleSheet::ruleForId(const InspectorCSSId& id) const
{ {
ASSERT(!id.isEmpty()); ASSERT(!id.isEmpty());
...@@ -1329,7 +1331,7 @@ PassRefPtr<TypeBuilder::Array<TypeBuilder::CSS::Selector> > InspectorStyleSheet: ...@@ -1329,7 +1331,7 @@ PassRefPtr<TypeBuilder::Array<TypeBuilder::CSS::Selector> > InspectorStyleSheet:
RefPtr<TypeBuilder::CSS::Selector> simpleSelector = TypeBuilder::CSS::Selector::create() RefPtr<TypeBuilder::CSS::Selector> simpleSelector = TypeBuilder::CSS::Selector::create()
.setValue(selector.stripWhiteSpace()); .setValue(selector.stripWhiteSpace());
simpleSelector->setRange(buildSourceRangeObject(range, lineEndings().get())); simpleSelector->setRange(buildSourceRangeObject(range, lineEndings()));
result->addItem(simpleSelector.release()); result->addItem(simpleSelector.release());
} }
return result.release(); return result.release();
...@@ -1418,7 +1420,7 @@ PassRefPtr<TypeBuilder::CSS::SourceRange> InspectorStyleSheet::ruleHeaderSourceR ...@@ -1418,7 +1420,7 @@ PassRefPtr<TypeBuilder::CSS::SourceRange> InspectorStyleSheet::ruleHeaderSourceR
if (index == kNotFound || index >= m_parsedStyleSheet->ruleCount()) if (index == kNotFound || index >= m_parsedStyleSheet->ruleCount())
return nullptr; return nullptr;
RefPtrWillBeRawPtr<CSSRuleSourceData> sourceData = m_parsedStyleSheet->ruleSourceDataAt(static_cast<unsigned>(index)); RefPtrWillBeRawPtr<CSSRuleSourceData> sourceData = m_parsedStyleSheet->ruleSourceDataAt(static_cast<unsigned>(index));
return buildSourceRangeObject(sourceData->ruleHeaderRange, lineEndings().get()); return buildSourceRangeObject(sourceData->ruleHeaderRange, lineEndings());
} }
PassRefPtr<TypeBuilder::CSS::SourceRange> InspectorStyleSheet::mediaQueryExpValueSourceRange(const CSSRule* rule, size_t mediaQueryIndex, size_t mediaQueryExpIndex) PassRefPtr<TypeBuilder::CSS::SourceRange> InspectorStyleSheet::mediaQueryExpValueSourceRange(const CSSRule* rule, size_t mediaQueryIndex, size_t mediaQueryExpIndex)
...@@ -1435,7 +1437,7 @@ PassRefPtr<TypeBuilder::CSS::SourceRange> InspectorStyleSheet::mediaQueryExpValu ...@@ -1435,7 +1437,7 @@ PassRefPtr<TypeBuilder::CSS::SourceRange> InspectorStyleSheet::mediaQueryExpValu
RefPtrWillBeRawPtr<CSSMediaQuerySourceData> mediaQueryData = sourceData->mediaSourceData->queryData.at(mediaQueryIndex); RefPtrWillBeRawPtr<CSSMediaQuerySourceData> mediaQueryData = sourceData->mediaSourceData->queryData.at(mediaQueryIndex);
if (mediaQueryExpIndex >= mediaQueryData->expData.size()) if (mediaQueryExpIndex >= mediaQueryData->expData.size())
return nullptr; return nullptr;
return buildSourceRangeObject(mediaQueryData->expData.at(mediaQueryExpIndex).valueRange, lineEndings().get()); return buildSourceRangeObject(mediaQueryData->expData.at(mediaQueryExpIndex).valueRange, lineEndings());
} }
PassRefPtrWillBeRawPtr<InspectorStyle> InspectorStyleSheet::inspectorStyleForId(const InspectorCSSId& id) PassRefPtrWillBeRawPtr<InspectorStyle> InspectorStyleSheet::inspectorStyleForId(const InspectorCSSId& id)
...@@ -1653,7 +1655,7 @@ bool InspectorStyleSheet::setStyleText(const InspectorCSSId& id, const String& t ...@@ -1653,7 +1655,7 @@ bool InspectorStyleSheet::setStyleText(const InspectorCSSId& id, const String& t
style->setCSSText(text, exceptionState); style->setCSSText(text, exceptionState);
if (!exceptionState.hadException()) { if (!exceptionState.hadException()) {
updateText(patchedStyleSheetText); updateText(patchedStyleSheetText);
fireStyleSheetChanged(); onStyleSheetTextChanged();
} }
return !exceptionState.hadException(); return !exceptionState.hadException();
...@@ -1759,8 +1761,6 @@ bool InspectorStyleSheetForInlineStyle::setText(const String& text, ExceptionSta ...@@ -1759,8 +1761,6 @@ bool InspectorStyleSheetForInlineStyle::setText(const String& text, ExceptionSta
bool success = setStyleText(InspectorCSSId(id(), 0), text); bool success = setStyleText(InspectorCSSId(id(), 0), text);
if (!success) if (!success)
exceptionState.throwDOMException(SyntaxError, "Style sheet text is invalid."); exceptionState.throwDOMException(SyntaxError, "Style sheet text is invalid.");
else
fireStyleSheetChanged();
return success; return success;
} }
...@@ -1790,7 +1790,7 @@ bool InspectorStyleSheetForInlineStyle::setStyleText(const InspectorCSSId& id, c ...@@ -1790,7 +1790,7 @@ bool InspectorStyleSheetForInlineStyle::setStyleText(const InspectorCSSId& id, c
m_styleText = text; m_styleText = text;
m_isStyleTextValid = true; m_isStyleTextValid = true;
m_ruleSourceData.clear(); m_ruleSourceData.clear();
fireStyleSheetChanged(); onStyleSheetTextChanged();
} }
return !exceptionState.hadException(); return !exceptionState.hadException();
} }
......
...@@ -54,6 +54,7 @@ class InspectorStyleSheetBase; ...@@ -54,6 +54,7 @@ class InspectorStyleSheetBase;
typedef WillBeHeapVector<RefPtrWillBeMember<CSSRule> > CSSRuleVector; typedef WillBeHeapVector<RefPtrWillBeMember<CSSRule> > CSSRuleVector;
typedef String ErrorString; typedef String ErrorString;
typedef Vector<unsigned> LineEndings;
class InspectorCSSId { class InspectorCSSId {
public: public:
...@@ -168,8 +169,8 @@ protected: ...@@ -168,8 +169,8 @@ protected:
InspectorStyleSheetBase(const String& id, Listener*); InspectorStyleSheetBase(const String& id, Listener*);
Listener* listener() const { return m_listener; } Listener* listener() const { return m_listener; }
void fireStyleSheetChanged(); void onStyleSheetTextChanged();
PassOwnPtr<Vector<unsigned> > lineEndings(); const LineEndings* lineEndings();
virtual PassRefPtrWillBeRawPtr<InspectorStyle> inspectorStyleForId(const InspectorCSSId&) = 0; virtual PassRefPtrWillBeRawPtr<InspectorStyle> inspectorStyleForId(const InspectorCSSId&) = 0;
virtual unsigned ruleCount() = 0; virtual unsigned ruleCount() = 0;
...@@ -183,6 +184,7 @@ private: ...@@ -183,6 +184,7 @@ private:
String m_id; String m_id;
Listener* m_listener; Listener* m_listener;
OwnPtr<LineEndings> m_lineEndings;
}; };
class InspectorStyleSheet : public InspectorStyleSheetBase { class InspectorStyleSheet : public InspectorStyleSheetBase {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment