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:
[expanded]
element.style { ()
[expanded]
#testDiv, my-custom-tag { (inspector-stylesheet:1 -> inspector-stylesheet:1:1)
[expanded]
#testDiv { (style.css:1 -> style.css:1:1)
color: green;
......@@ -32,6 +35,9 @@ Dump styles after inspector was reopened:
[expanded]
element.style { ()
[expanded]
#testdiv, my-custom-tag { ()
[expanded]
#testDiv { (style.css:1 -> style.css:1:1)
color: green;
......
......@@ -44,9 +44,9 @@ function test()
function testFirstOpen()
{
InspectorTest.addResult("Dump initial styles:");
dump(step2);
function step2()
dump(onStylesDumped);
function onStylesDumped()
{
treeElement = InspectorTest.getMatchedStylePropertyTreeItem("color");
treeElement.startEditing();
......@@ -57,14 +57,29 @@ function test()
treeElement.valueElement.textContent = "green";
treeElement.valueElement.firstChild.select();
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);
}
function step4()
{
InspectorTest.addResult("Dump styles after editing:");
......@@ -81,7 +96,7 @@ function test()
function dump(callback)
{
InspectorTest.selectNodeAndWaitForStyles("testDiv", step2);
function step2()
{
InspectorTest.dumpSelectedElementStyles(true);
......
......@@ -496,7 +496,7 @@ enum MediaListSource {
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)
return nullptr;
......@@ -547,7 +547,7 @@ PassRefPtr<TypeBuilder::CSS::CSSStyle> InspectorStyle::buildObjectForStyle() con
RefPtrWillBeRawPtr<CSSRuleSourceData> sourceData = extractSourceData();
if (sourceData)
result->setRange(buildSourceRangeObject(sourceData->ruleBodyRange, m_parentStyleSheet->lineEndings().get()));
result->setRange(buildSourceRangeObject(sourceData->ruleBodyRange, m_parentStyleSheet->lineEndings()));
return result.release();
}
......@@ -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::ShorthandEntry> > shorthandEntries = Array<TypeBuilder::CSS::ShorthandEntry>::create();
HashSet<String> foundShorthands;
OwnPtr<Vector<unsigned> > lineEndings(m_parentStyleSheet ? m_parentStyleSheet->lineEndings() : PassOwnPtr<Vector<unsigned> >());
RefPtrWillBeRawPtr<CSSRuleSourceData> sourceData = extractSourceData();
WillBeHeapVector<InspectorStyleProperty> properties;
......@@ -718,7 +717,7 @@ PassRefPtr<TypeBuilder::CSS::CSSStyle> InspectorStyle::styleWithProperties() con
if (propertyEntry.important)
property->setImportant(true);
if (it->hasSource) {
property->setRange(buildSourceRangeObject(propertyEntry.range, lineEndings.get()));
property->setRange(buildSourceRangeObject(propertyEntry.range, m_parentStyleSheet ? m_parentStyleSheet->lineEndings() : nullptr));
if (!propertyEntry.disabled) {
ASSERT_UNUSED(sourceData, sourceData);
property->setImplicit(false);
......@@ -859,6 +858,7 @@ void InspectorStyle::trace(Visitor* visitor)
InspectorStyleSheetBase::InspectorStyleSheetBase(const String& id, Listener* listener)
: m_id(id)
, m_listener(listener)
, m_lineEndings(adoptPtr(new LineEndings()))
{
}
......@@ -880,8 +880,9 @@ bool InspectorStyleSheetBase::getStyleText(const InspectorCSSId& id, String* tex
return inspectorStyle->styleText(text);
}
void InspectorStyleSheetBase::fireStyleSheetChanged()
void InspectorStyleSheetBase::onStyleSheetTextChanged()
{
m_lineEndings = adoptPtrWillBeNoop(new LineEndings());
if (listener())
listener()->styleSheetChanged(this);
}
......@@ -915,17 +916,19 @@ PassRefPtr<TypeBuilder::CSS::CSSStyle> InspectorStyleSheetBase::buildObjectForSt
return result.release();
}
PassOwnPtr<Vector<unsigned> > InspectorStyleSheetBase::lineEndings()
const LineEndings* InspectorStyleSheetBase::lineEndings()
{
if (m_lineEndings->size() > 0)
return m_lineEndings.get();
String text;
if (!getText(&text))
return PassOwnPtr<Vector<unsigned> >();
return WTF::lineEndings(text);
if (getText(&text))
m_lineEndings = WTF::lineEndings(text);
return m_lineEndings.get();
}
bool InspectorStyleSheetBase::lineNumberAndColumnToOffset(unsigned lineNumber, unsigned columnNumber, unsigned* offset)
{
OwnPtr<Vector<unsigned> > endings = lineEndings();
const LineEndings* endings = lineEndings();
if (lineNumber >= endings->size())
return false;
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
if (listener())
listener()->didReparseStyleSheet();
fireStyleSheetChanged();
onStyleSheetTextChanged();
m_pageStyleSheet->ownerDocument()->styleResolverChanged(FullStyleUpdate);
return true;
}
......@@ -1075,7 +1078,7 @@ bool InspectorStyleSheet::setRuleSelector(const InspectorCSSId& id, const String
String sheetText = m_parsedStyleSheet->text();
sheetText.replace(sourceData->ruleHeaderRange.start, sourceData->ruleHeaderRange.length(), selector);
updateText(sheetText);
fireStyleSheetChanged();
onStyleSheetTextChanged();
return true;
}
......@@ -1211,10 +1214,10 @@ CSSStyleRule* InspectorStyleSheet::addRule(const String& ruleText, const SourceR
text.insert(ruleText, location.start);
m_parsedStyleSheet->setText(text);
updateText(text);
m_flatRules.clear();
fireStyleSheetChanged();
onStyleSheetTextChanged();
return styleRule;
}
......@@ -1261,9 +1264,9 @@ bool InspectorStyleSheet::deleteRule(const InspectorCSSId& id, const String& old
if (exceptionState.hadException())
return false;
m_parsedStyleSheet->setText(oldText);
updateText(oldText);
m_flatRules.clear();
fireStyleSheetChanged();
onStyleSheetTextChanged();
return true;
}
......@@ -1275,7 +1278,6 @@ void InspectorStyleSheet::updateText(const String& newText)
m_parsedStyleSheet->setText(newText);
}
CSSStyleRule* InspectorStyleSheet::ruleForId(const InspectorCSSId& id) const
{
ASSERT(!id.isEmpty());
......@@ -1329,7 +1331,7 @@ PassRefPtr<TypeBuilder::Array<TypeBuilder::CSS::Selector> > InspectorStyleSheet:
RefPtr<TypeBuilder::CSS::Selector> simpleSelector = TypeBuilder::CSS::Selector::create()
.setValue(selector.stripWhiteSpace());
simpleSelector->setRange(buildSourceRangeObject(range, lineEndings().get()));
simpleSelector->setRange(buildSourceRangeObject(range, lineEndings()));
result->addItem(simpleSelector.release());
}
return result.release();
......@@ -1418,7 +1420,7 @@ PassRefPtr<TypeBuilder::CSS::SourceRange> InspectorStyleSheet::ruleHeaderSourceR
if (index == kNotFound || index >= m_parsedStyleSheet->ruleCount())
return nullptr;
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)
......@@ -1435,7 +1437,7 @@ PassRefPtr<TypeBuilder::CSS::SourceRange> InspectorStyleSheet::mediaQueryExpValu
RefPtrWillBeRawPtr<CSSMediaQuerySourceData> mediaQueryData = sourceData->mediaSourceData->queryData.at(mediaQueryIndex);
if (mediaQueryExpIndex >= mediaQueryData->expData.size())
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)
......@@ -1653,7 +1655,7 @@ bool InspectorStyleSheet::setStyleText(const InspectorCSSId& id, const String& t
style->setCSSText(text, exceptionState);
if (!exceptionState.hadException()) {
updateText(patchedStyleSheetText);
fireStyleSheetChanged();
onStyleSheetTextChanged();
}
return !exceptionState.hadException();
......@@ -1759,8 +1761,6 @@ bool InspectorStyleSheetForInlineStyle::setText(const String& text, ExceptionSta
bool success = setStyleText(InspectorCSSId(id(), 0), text);
if (!success)
exceptionState.throwDOMException(SyntaxError, "Style sheet text is invalid.");
else
fireStyleSheetChanged();
return success;
}
......@@ -1790,7 +1790,7 @@ bool InspectorStyleSheetForInlineStyle::setStyleText(const InspectorCSSId& id, c
m_styleText = text;
m_isStyleTextValid = true;
m_ruleSourceData.clear();
fireStyleSheetChanged();
onStyleSheetTextChanged();
}
return !exceptionState.hadException();
}
......
......@@ -54,6 +54,7 @@ class InspectorStyleSheetBase;
typedef WillBeHeapVector<RefPtrWillBeMember<CSSRule> > CSSRuleVector;
typedef String ErrorString;
typedef Vector<unsigned> LineEndings;
class InspectorCSSId {
public:
......@@ -168,8 +169,8 @@ protected:
InspectorStyleSheetBase(const String& id, Listener*);
Listener* listener() const { return m_listener; }
void fireStyleSheetChanged();
PassOwnPtr<Vector<unsigned> > lineEndings();
void onStyleSheetTextChanged();
const LineEndings* lineEndings();
virtual PassRefPtrWillBeRawPtr<InspectorStyle> inspectorStyleForId(const InspectorCSSId&) = 0;
virtual unsigned ruleCount() = 0;
......@@ -183,6 +184,7 @@ private:
String m_id;
Listener* m_listener;
OwnPtr<LineEndings> m_lineEndings;
};
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