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

Side by Side Diff: Source/core/inspector/InspectorCSSAgent.cpp

Issue 1181213007: DevTools: introduce CSS.setStyleText, we'll migrate setPropertyText to it later. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: win fixed Created 5 years, 6 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/core/inspector/InspectorCSSAgent.h ('k') | Source/core/inspector/InspectorStyleSheet.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010, Google Inc. All rights reserved. 2 * Copyright (C) 2010, Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 private: 280 private:
281 RefPtrWillBeMember<InspectorStyleSheetBase> m_styleSheet; 281 RefPtrWillBeMember<InspectorStyleSheetBase> m_styleSheet;
282 unsigned m_ruleIndex; 282 unsigned m_ruleIndex;
283 unsigned m_propertyIndex; 283 unsigned m_propertyIndex;
284 String m_text; 284 String m_text;
285 String m_oldStyleText; 285 String m_oldStyleText;
286 String m_newStyleText; 286 String m_newStyleText;
287 bool m_overwrite; 287 bool m_overwrite;
288 }; 288 };
289 289
290 class InspectorCSSAgent::SetRuleSelectorOrMediaAction final : public InspectorCS SAgent::StyleSheetAction { 290 class InspectorCSSAgent::ModifyRuleAction final : public InspectorCSSAgent::Styl eSheetAction {
291 WTF_MAKE_NONCOPYABLE(SetRuleSelectorOrMediaAction); 291 WTF_MAKE_NONCOPYABLE(ModifyRuleAction);
292 public: 292 public:
293 SetRuleSelectorOrMediaAction(bool isMedia, InspectorStyleSheet* styleSheet, const SourceRange& range, const String& text) 293 enum Type {
294 : InspectorCSSAgent::StyleSheetAction("SetRuleSelectorOrMediaAction") 294 SetRuleSelector,
295 SetStyleText,
296 SetMediaRuleText
297 };
298
299 ModifyRuleAction(Type type, InspectorStyleSheet* styleSheet, const SourceRan ge& range, const String& text)
300 : InspectorCSSAgent::StyleSheetAction("ModifyRuleAction")
295 , m_styleSheet(styleSheet) 301 , m_styleSheet(styleSheet)
296 , m_isMedia(isMedia) 302 , m_type(type)
297 , m_range(range) 303 , m_newText(text)
298 , m_text(text) 304 , m_oldRange(range)
299 , m_cssRule(nullptr) 305 , m_cssRule(nullptr)
300 { 306 {
301 } 307 }
302 308
303 virtual bool perform(ExceptionState& exceptionState) override 309 virtual bool perform(ExceptionState& exceptionState) override
304 { 310 {
305 return redo(exceptionState); 311 return redo(exceptionState);
306 } 312 }
307 313
308 virtual bool undo(ExceptionState& exceptionState) override 314 virtual bool undo(ExceptionState& exceptionState) override
309 { 315 {
310 if (m_isMedia) 316 switch (m_type) {
317 case SetRuleSelector:
318 return m_styleSheet->setRuleSelector(m_newRange, m_oldText, nullptr, nullptr, exceptionState);
319 case SetStyleText:
320 return m_styleSheet->setStyleText(m_newRange, m_oldText, nullptr, nu llptr, exceptionState);
321 case SetMediaRuleText:
311 return m_styleSheet->setMediaRuleText(m_newRange, m_oldText, nullptr , nullptr, exceptionState); 322 return m_styleSheet->setMediaRuleText(m_newRange, m_oldText, nullptr , nullptr, exceptionState);
312 return m_styleSheet->setRuleSelector(m_newRange, m_oldText, nullptr, nul lptr, exceptionState); 323 default:
324 ASSERT_NOT_REACHED();
325 }
326 return false;
313 } 327 }
314 328
315 virtual bool redo(ExceptionState& exceptionState) override 329 virtual bool redo(ExceptionState& exceptionState) override
316 { 330 {
317 if (m_isMedia) 331 switch (m_type) {
318 m_cssRule = m_styleSheet->setMediaRuleText(m_range, m_text, &m_newRa nge, &m_oldText, exceptionState); 332 case SetRuleSelector:
319 else 333 m_cssRule = m_styleSheet->setRuleSelector(m_oldRange, m_newText, &m_ newRange, &m_oldText, exceptionState);
320 m_cssRule = m_styleSheet->setRuleSelector(m_range, m_text, &m_newRan ge, &m_oldText, exceptionState); 334 break;
335 case SetStyleText:
336 m_cssRule = m_styleSheet->setStyleText(m_oldRange, m_newText, &m_new Range, &m_oldText, exceptionState);
337 break;
338 case SetMediaRuleText:
339 m_cssRule = m_styleSheet->setMediaRuleText(m_oldRange, m_newText, &m _newRange, &m_oldText, exceptionState);
340 break;
341 default:
342 ASSERT_NOT_REACHED();
343 }
321 return m_cssRule; 344 return m_cssRule;
322 } 345 }
323 346
324 RefPtrWillBeRawPtr<CSSRule> takeRule() 347 RefPtrWillBeRawPtr<CSSRule> takeRule()
325 { 348 {
326 RefPtrWillBeRawPtr<CSSRule> result = m_cssRule; 349 RefPtrWillBeRawPtr<CSSRule> result = m_cssRule;
327 m_cssRule = nullptr; 350 m_cssRule = nullptr;
328 return result; 351 return result;
329 } 352 }
330 353
331 DEFINE_INLINE_VIRTUAL_TRACE() 354 DEFINE_INLINE_VIRTUAL_TRACE()
332 { 355 {
333 visitor->trace(m_styleSheet); 356 visitor->trace(m_styleSheet);
334 visitor->trace(m_cssRule); 357 visitor->trace(m_cssRule);
335 InspectorCSSAgent::StyleSheetAction::trace(visitor); 358 InspectorCSSAgent::StyleSheetAction::trace(visitor);
336 } 359 }
337 360
361 virtual String mergeId() override
362 {
363 return String::format("ModifyRuleAction:%d %s:%d", m_type, m_styleSheet- >id().utf8().data(), m_oldRange.start);
364 }
365
366 virtual void merge(PassRefPtrWillBeRawPtr<Action> action) override
367 {
368 ASSERT(action->mergeId() == mergeId());
369
370 ModifyRuleAction* other = static_cast<ModifyRuleAction*>(action.get());
371 m_newText = other->m_newText;
372 m_newRange = other->m_newRange;
373 }
374
338 private: 375 private:
339 RefPtrWillBeMember<InspectorStyleSheet> m_styleSheet; 376 RefPtrWillBeMember<InspectorStyleSheet> m_styleSheet;
340 bool m_isMedia; 377 Type m_type;
341 SourceRange m_range; 378 String m_oldText;
379 String m_newText;
380 SourceRange m_oldRange;
381 SourceRange m_newRange;
382 RefPtrWillBeMember<CSSRule> m_cssRule;
383 };
384
385 class InspectorCSSAgent::SetElementStyleAction final : public InspectorCSSAgent: :StyleSheetAction {
386 WTF_MAKE_NONCOPYABLE(SetElementStyleAction);
387 public:
388 SetElementStyleAction(InspectorStyleSheetForInlineStyle* styleSheet, const S tring& text)
389 : InspectorCSSAgent::StyleSheetAction("SetElementStyleAction")
390 , m_styleSheet(styleSheet)
391 , m_text(text)
392 {
393 }
394
395 virtual bool perform(ExceptionState& exceptionState) override
396 {
397 return redo(exceptionState);
398 }
399
400 virtual bool undo(ExceptionState& exceptionState) override
401 {
402 return m_styleSheet->setText(m_oldText, exceptionState);
403 }
404
405 virtual bool redo(ExceptionState& exceptionState) override
406 {
407 if (!m_styleSheet->getText(&m_oldText))
408 return false;
409 return m_styleSheet->setText(m_text, exceptionState);
410 }
411
412 DEFINE_INLINE_VIRTUAL_TRACE()
413 {
414 visitor->trace(m_styleSheet);
415 InspectorCSSAgent::StyleSheetAction::trace(visitor);
416 }
417
418 virtual String mergeId() override
419 {
420 return String::format("SetElementStyleAction:%s", m_styleSheet->id().utf 8().data());
421 }
422
423 virtual void merge(PassRefPtrWillBeRawPtr<Action> action) override
424 {
425 ASSERT(action->mergeId() == mergeId());
426
427 SetElementStyleAction* other = static_cast<SetElementStyleAction*>(actio n.get());
428 m_text = other->m_text;
429 }
430
431 private:
432 RefPtrWillBeMember<InspectorStyleSheetForInlineStyle> m_styleSheet;
342 String m_text; 433 String m_text;
343 String m_oldText; 434 String m_oldText;
344 SourceRange m_newRange;
345 RefPtrWillBeMember<CSSRule> m_cssRule;
346 }; 435 };
347 436
348 class InspectorCSSAgent::AddRuleAction final : public InspectorCSSAgent::StyleSh eetAction { 437 class InspectorCSSAgent::AddRuleAction final : public InspectorCSSAgent::StyleSh eetAction {
349 WTF_MAKE_NONCOPYABLE(AddRuleAction); 438 WTF_MAKE_NONCOPYABLE(AddRuleAction);
350 public: 439 public:
351 AddRuleAction(InspectorStyleSheet* styleSheet, const String& ruleText, const SourceRange& location) 440 AddRuleAction(InspectorStyleSheet* styleSheet, const String& ruleText, const SourceRange& location)
352 : InspectorCSSAgent::StyleSheetAction("AddRule") 441 : InspectorCSSAgent::StyleSheetAction("AddRule")
353 , m_styleSheet(styleSheet) 442 , m_styleSheet(styleSheet)
354 , m_ruleText(ruleText) 443 , m_ruleText(ruleText)
355 , m_location(location) 444 , m_location(location)
(...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after
959 InspectorStyleSheet* inspectorStyleSheet = assertInspectorStyleSheetForId(er rorString, styleSheetId); 1048 InspectorStyleSheet* inspectorStyleSheet = assertInspectorStyleSheetForId(er rorString, styleSheetId);
960 if (!inspectorStyleSheet) { 1049 if (!inspectorStyleSheet) {
961 *errorString = "Stylesheet not found"; 1050 *errorString = "Stylesheet not found";
962 return; 1051 return;
963 } 1052 }
964 SourceRange selectorRange; 1053 SourceRange selectorRange;
965 if (!jsonRangeToSourceRange(errorString, inspectorStyleSheet, range, &select orRange)) 1054 if (!jsonRangeToSourceRange(errorString, inspectorStyleSheet, range, &select orRange))
966 return; 1055 return;
967 1056
968 TrackExceptionState exceptionState; 1057 TrackExceptionState exceptionState;
969 RefPtrWillBeRawPtr<SetRuleSelectorOrMediaAction> action = adoptRefWillBeNoop (new SetRuleSelectorOrMediaAction(false, inspectorStyleSheet, selectorRange, sel ector)); 1058 RefPtrWillBeRawPtr<ModifyRuleAction> action = adoptRefWillBeNoop(new ModifyR uleAction(ModifyRuleAction::SetRuleSelector, inspectorStyleSheet, selectorRange, selector));
970 bool success = m_domAgent->history()->perform(action, exceptionState); 1059 bool success = m_domAgent->history()->perform(action, exceptionState);
971 if (success) { 1060 if (success) {
972 RefPtrWillBeRawPtr<CSSStyleRule> rule = InspectorCSSAgent::asCSSStyleRul e(action->takeRule().get()); 1061 RefPtrWillBeRawPtr<CSSStyleRule> rule = InspectorCSSAgent::asCSSStyleRul e(action->takeRule().get());
973 result = inspectorStyleSheet->buildObjectForRule(rule.get(), buildMediaL istChain(rule.get())); 1062 result = inspectorStyleSheet->buildObjectForRule(rule.get(), buildMediaL istChain(rule.get()));
974 } 1063 }
975 *errorString = InspectorDOMAgent::toErrorString(exceptionState); 1064 *errorString = InspectorDOMAgent::toErrorString(exceptionState);
976 } 1065 }
977 1066
1067 void InspectorCSSAgent::setStyleText(ErrorString* errorString, const String& sty leSheetId, const RefPtr<JSONObject>& range, const String& text, RefPtr<TypeBuild er::CSS::CSSStyle>& result)
1068 {
1069 InspectorStyleSheetBase* inspectorStyleSheet = assertStyleSheetForId(errorSt ring, styleSheetId);
1070 if (!inspectorStyleSheet) {
1071 *errorString = "Stylesheet not found";
1072 return;
1073 }
1074 SourceRange selectorRange;
1075 if (!jsonRangeToSourceRange(errorString, inspectorStyleSheet, range, &select orRange))
1076 return;
1077
1078 TrackExceptionState exceptionState;
1079 if (inspectorStyleSheet->isInlineStyle()) {
1080 InspectorStyleSheetForInlineStyle* inlineStyleSheet = static_cast<Inspec torStyleSheetForInlineStyle*>(inspectorStyleSheet);
1081 RefPtrWillBeRawPtr<SetElementStyleAction> action = adoptRefWillBeNoop(ne w SetElementStyleAction(inlineStyleSheet, text));
1082 bool success = m_domAgent->history()->perform(action, exceptionState);
1083 if (success)
1084 result = inspectorStyleSheet->buildObjectForStyle(inlineStyleSheet-> inlineStyle());
1085 } else {
1086 RefPtrWillBeRawPtr<ModifyRuleAction> action = adoptRefWillBeNoop(new Mod ifyRuleAction(ModifyRuleAction::SetStyleText, static_cast<InspectorStyleSheet*>( inspectorStyleSheet), selectorRange, text));
1087 bool success = m_domAgent->history()->perform(action, exceptionState);
1088 if (success) {
1089 RefPtrWillBeRawPtr<CSSStyleRule> rule = InspectorCSSAgent::asCSSStyl eRule(action->takeRule().get());
1090 result = inspectorStyleSheet->buildObjectForStyle(rule->style());
1091 }
1092 }
1093 *errorString = InspectorDOMAgent::toErrorString(exceptionState);
1094 }
1095
978 void InspectorCSSAgent::setMediaText(ErrorString* errorString, const String& sty leSheetId, const RefPtr<JSONObject>& range, const String& text, RefPtr<TypeBuild er::CSS::CSSMedia>& result) 1096 void InspectorCSSAgent::setMediaText(ErrorString* errorString, const String& sty leSheetId, const RefPtr<JSONObject>& range, const String& text, RefPtr<TypeBuild er::CSS::CSSMedia>& result)
979 { 1097 {
980 InspectorStyleSheet* inspectorStyleSheet = assertInspectorStyleSheetForId(er rorString, styleSheetId); 1098 InspectorStyleSheet* inspectorStyleSheet = assertInspectorStyleSheetForId(er rorString, styleSheetId);
981 if (!inspectorStyleSheet) { 1099 if (!inspectorStyleSheet) {
982 *errorString = "Stylesheet not found"; 1100 *errorString = "Stylesheet not found";
983 return; 1101 return;
984 } 1102 }
985 SourceRange textRange; 1103 SourceRange textRange;
986 if (!jsonRangeToSourceRange(errorString, inspectorStyleSheet, range, &textRa nge)) 1104 if (!jsonRangeToSourceRange(errorString, inspectorStyleSheet, range, &textRa nge))
987 return; 1105 return;
988 1106
989 TrackExceptionState exceptionState; 1107 TrackExceptionState exceptionState;
990 RefPtrWillBeRawPtr<SetRuleSelectorOrMediaAction> action = adoptRefWillBeNoop (new SetRuleSelectorOrMediaAction(true, inspectorStyleSheet, textRange, text)); 1108 RefPtrWillBeRawPtr<ModifyRuleAction> action = adoptRefWillBeNoop(new ModifyR uleAction(ModifyRuleAction::SetMediaRuleText, inspectorStyleSheet, textRange, te xt));
991 bool success = m_domAgent->history()->perform(action, exceptionState); 1109 bool success = m_domAgent->history()->perform(action, exceptionState);
992 if (success) { 1110 if (success) {
993 RefPtrWillBeRawPtr<CSSMediaRule> rule = InspectorCSSAgent::asCSSMediaRul e(action->takeRule().get()); 1111 RefPtrWillBeRawPtr<CSSMediaRule> rule = InspectorCSSAgent::asCSSMediaRul e(action->takeRule().get());
994 String sourceURL = rule->parentStyleSheet()->contents()->baseURL(); 1112 String sourceURL = rule->parentStyleSheet()->contents()->baseURL();
995 if (sourceURL.isEmpty()) 1113 if (sourceURL.isEmpty())
996 sourceURL = InspectorDOMAgent::documentURLString(rule->parentStyleSh eet()->ownerDocument()); 1114 sourceURL = InspectorDOMAgent::documentURLString(rule->parentStyleSh eet()->ownerDocument());
997 result = buildMediaObject(rule->media(), MediaListSourceMediaRule, sourc eURL, rule->parentStyleSheet()); 1115 result = buildMediaObject(rule->media(), MediaListSourceMediaRule, sourc eURL, rule->parentStyleSheet());
998 } 1116 }
999 *errorString = InspectorDOMAgent::toErrorString(exceptionState); 1117 *errorString = InspectorDOMAgent::toErrorString(exceptionState);
1000 } 1118 }
(...skipping 556 matching lines...) Expand 10 before | Expand all | Expand 10 after
1557 visitor->trace(m_documentToCSSStyleSheets); 1675 visitor->trace(m_documentToCSSStyleSheets);
1558 visitor->trace(m_invalidatedDocuments); 1676 visitor->trace(m_invalidatedDocuments);
1559 visitor->trace(m_nodeToInspectorStyleSheet); 1677 visitor->trace(m_nodeToInspectorStyleSheet);
1560 visitor->trace(m_documentToViaInspectorStyleSheet); 1678 visitor->trace(m_documentToViaInspectorStyleSheet);
1561 #endif 1679 #endif
1562 visitor->trace(m_inspectorUserAgentStyleSheet); 1680 visitor->trace(m_inspectorUserAgentStyleSheet);
1563 InspectorBaseAgent::trace(visitor); 1681 InspectorBaseAgent::trace(visitor);
1564 } 1682 }
1565 1683
1566 } // namespace blink 1684 } // namespace blink
1567
OLDNEW
« no previous file with comments | « Source/core/inspector/InspectorCSSAgent.h ('k') | Source/core/inspector/InspectorStyleSheet.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698