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

Unified Diff: Source/core/inspector/InspectorCSSAgent.cpp

Issue 172593003: DevTools: [CSS] Add CSS.editRangeInStyleSheetText() to the protocol (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: fix glitch Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/core/inspector/InspectorCSSAgent.cpp
diff --git a/Source/core/inspector/InspectorCSSAgent.cpp b/Source/core/inspector/InspectorCSSAgent.cpp
index 6eb982980782b86fd8c67b081a7cd12d87fb8ac4..419119dc4da52660a2051512daff2b448fc9c656 100644
--- a/Source/core/inspector/InspectorCSSAgent.cpp
+++ b/Source/core/inspector/InspectorCSSAgent.cpp
@@ -224,6 +224,68 @@ private:
String m_oldText;
};
+class InspectorCSSAgent::EditRangeInStyleSheetTextAction FINAL : public InspectorCSSAgent::StyleSheetAction {
+ WTF_MAKE_NONCOPYABLE(EditRangeInStyleSheetTextAction);
+public:
+ EditRangeInStyleSheetTextAction(InspectorStyleSheet* styleSheet, const String& text, unsigned rangeStart, unsigned rangeEnd)
+ : InspectorCSSAgent::StyleSheetAction("EditRangeInStyleSheetTextAction", styleSheet)
+ , m_text(text)
+ , m_rangeStart(rangeStart)
+ , m_rangeEnd(rangeEnd)
+ , m_externalEditRangeResult(0)
+ {
+ }
+
+ virtual bool perform(ExceptionState& exceptionState) OVERRIDE
+ {
+ return redo(exceptionState);
+ }
+
+ virtual bool undo(ExceptionState& exceptionState) OVERRIDE
+ {
+ if (m_styleSheet->setText(m_oldText, exceptionState)) {
+ m_styleSheet->reparseStyleSheet(m_oldText);
+ return true;
+ }
+ return false;
+ }
+
+ virtual bool redo(ExceptionState& exceptionState) OVERRIDE
+ {
+ if (!m_styleSheet->getText(&m_oldText))
+ return false;
+ InspectorStyleSheet::EditRangeResult placeholder;
+ InspectorStyleSheet::EditRangeResult* editRangeResult = m_externalEditRangeResult ? m_externalEditRangeResult : &placeholder;
+ m_externalEditRangeResult = 0;
+ return m_styleSheet->editRange(m_text, m_rangeStart, m_rangeEnd, editRangeResult, exceptionState);
+ }
+
+ virtual String mergeId() OVERRIDE
+ {
+ return String::format("EditRangeInStyleSheetTextAction %s:%u:%u", m_styleSheet->id().utf8().data(), m_rangeStart, m_rangeEnd);
+ }
+
+ virtual void merge(PassOwnPtr<Action> action) OVERRIDE
+ {
+ ASSERT(action->mergeId() == mergeId());
+
+ EditRangeInStyleSheetTextAction* other = static_cast<EditRangeInStyleSheetTextAction*>(action.get());
+ m_text = other->m_text;
+ }
+
+ void setExternalEditRangeResult(InspectorStyleSheet::EditRangeResult* editRangeResult)
+ {
+ m_externalEditRangeResult = editRangeResult;
+ }
+
+private:
+ String m_text;
+ unsigned m_rangeStart;
+ unsigned m_rangeEnd;
+ String m_oldText;
+ InspectorStyleSheet::EditRangeResult* m_externalEditRangeResult;
+};
+
class InspectorCSSAgent::SetPropertyTextAction FINAL : public InspectorCSSAgent::StyleSheetAction {
WTF_MAKE_NONCOPYABLE(SetPropertyTextAction);
public:
@@ -828,7 +890,7 @@ void InspectorCSSAgent::setStyleSheetText(ErrorString* errorString, const String
{
InspectorStyleSheet* inspectorStyleSheet = assertStyleSheetForId(errorString, styleSheetId);
if (!inspectorStyleSheet) {
- *errorString = "Style sheet with id " + styleSheetId + " not found.";
+ *errorString = "Style sheet with id " + styleSheetId + " not found";
return;
}
@@ -837,6 +899,63 @@ void InspectorCSSAgent::setStyleSheetText(ErrorString* errorString, const String
*errorString = InspectorDOMAgent::toErrorString(exceptionState);
}
+static bool extractRangeComponent(ErrorString* errorString, const RefPtr<JSONObject>& range, const String& component, unsigned& result)
+{
+ int parsedValue;
+ if (!range->getNumber(component, &parsedValue) || parsedValue < 0) {
+ *errorString = "range." + component + " must be a non-negative integer";
+ return false;
+ }
+ result = parsedValue;
+ return true;
+}
+
+void InspectorCSSAgent::editRangeInStyleSheetText(ErrorString* errorString, const String& styleSheetId, const RefPtr<JSONObject>& range, const String& text, RefPtr<TypeBuilder::CSS::CSSRule>& rule, RefPtr<TypeBuilder::CSS::CSSStyle>& style)
+{
+ InspectorStyleSheet* inspectorStyleSheet = assertStyleSheetForId(errorString, styleSheetId);
+ if (!inspectorStyleSheet) {
+ *errorString = "Stylesheet with id " + styleSheetId + " not found";
+ return;
+ }
+ unsigned startLineNumber;
+ unsigned startColumn;
+ unsigned endLineNumber;
+ unsigned endColumn;
+ if (!extractRangeComponent(errorString, range, "startLine", startLineNumber)
+ || !extractRangeComponent(errorString, range, "startColumn", startColumn)
+ || !extractRangeComponent(errorString, range, "endLine", endLineNumber)
+ || !extractRangeComponent(errorString, range, "endColumn", endColumn))
+ return;
+
+ unsigned startOffset;
+ unsigned endOffset;
+ bool success = inspectorStyleSheet->lineNumberAndColumnToOffset(startLineNumber, startColumn, &startOffset)
+ && inspectorStyleSheet->lineNumberAndColumnToOffset(endLineNumber, endColumn, &endOffset);
+ if (!success) {
+ *errorString = "Specified range is out of bounds";
+ return;
+ }
+
+ if (startOffset > endOffset) {
+ *errorString = "Range start must not succeed its end";
+ return;
+ }
+
+ TrackExceptionState exceptionState;
+ OwnPtr<EditRangeInStyleSheetTextAction> editAction = adoptPtr(new EditRangeInStyleSheetTextAction(inspectorStyleSheet, text, startOffset, endOffset));
+ InspectorStyleSheet::EditRangeResult editRangeResult;
+ editAction->setExternalEditRangeResult(&editRangeResult);
vsevik 2014/03/13 10:44:51 Wouldn't it be simpler to add an actionResult gett
+ if (!m_domAgent->history()->perform(editAction.release(), exceptionState)) {
+ *errorString = InspectorDOMAgent::toErrorString(exceptionState);
+ return;
+ }
+ if (editRangeResult.rule) {
+ rule = inspectorStyleSheet->buildObjectForRule(editRangeResult.rule.get(), buildMediaListChain(editRangeResult.rule.get()));
+ } else if (editRangeResult.style) {
+ style = inspectorStyleSheet->buildObjectForStyle(editRangeResult.style.get());
+ }
+}
+
void InspectorCSSAgent::setPropertyText(ErrorString* errorString, const RefPtr<JSONObject>& fullStyleId, int propertyIndex, const String& text, bool overwrite, RefPtr<TypeBuilder::CSS::CSSStyle>& result)
{
InspectorCSSId compoundId(fullStyleId);

Powered by Google App Engine
This is Rietveld 408576698