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

Side by Side Diff: Source/devtools/front_end/sdk/CSSStyleModel.js

Issue 1187193005: DevTools: migrate from CSS.setPropertyText to CSS.setStyleText (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: for landing 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
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 are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 649 matching lines...) Expand 10 before | Expand all | Expand 10 after
660 { 660 {
661 this._cssModel = cssModel; 661 this._cssModel = cssModel;
662 this.styleSheetId = payload.styleSheetId; 662 this.styleSheetId = payload.styleSheetId;
663 this.range = payload.range ? WebInspector.TextRange.fromObject(payload.range ) : null; 663 this.range = payload.range ? WebInspector.TextRange.fromObject(payload.range ) : null;
664 this._shorthandValues = WebInspector.CSSStyleDeclaration.buildShorthandValue Map(payload.shorthandEntries); 664 this._shorthandValues = WebInspector.CSSStyleDeclaration.buildShorthandValue Map(payload.shorthandEntries);
665 this._livePropertyMap = {}; // LIVE properties (source-based or style-based) : { name -> CSSProperty } 665 this._livePropertyMap = {}; // LIVE properties (source-based or style-based) : { name -> CSSProperty }
666 this._allProperties = []; // ALL properties: [ CSSProperty ] 666 this._allProperties = []; // ALL properties: [ CSSProperty ]
667 this.__disabledProperties = {}; // DISABLED properties: { index -> CSSProper ty } 667 this.__disabledProperties = {}; // DISABLED properties: { index -> CSSProper ty }
668 var payloadPropertyCount = payload.cssProperties.length; 668 var payloadPropertyCount = payload.cssProperties.length;
669 669
670
671 for (var i = 0; i < payloadPropertyCount; ++i) { 670 for (var i = 0; i < payloadPropertyCount; ++i) {
672 var property = WebInspector.CSSProperty.parsePayload(this, i, payload.cs sProperties[i]); 671 var property = WebInspector.CSSProperty.parsePayload(this, i, payload.cs sProperties[i]);
673 this._allProperties.push(property); 672 this._allProperties.push(property);
674 } 673 }
675 674
676 this._computeActiveProperties(); 675 this._computeActiveProperties();
677 676
678 var propertyIndex = 0; 677 var propertyIndex = 0;
679 for (var i = 0; i < this._allProperties.length; ++i) { 678 for (var i = 0; i < this._allProperties.length; ++i) {
680 var property = this._allProperties[i]; 679 var property = this._allProperties[i];
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
890 */ 889 */
891 newBlankProperty: function(index) 890 newBlankProperty: function(index)
892 { 891 {
893 index = (typeof index === "undefined") ? this.pastLastSourcePropertyInde x() : index; 892 index = (typeof index === "undefined") ? this.pastLastSourcePropertyInde x() : index;
894 var property = new WebInspector.CSSProperty(this, index, "", "", false, false, true, false, "", this._insertionRange(index)); 893 var property = new WebInspector.CSSProperty(this, index, "", "", false, false, true, false, "", this._insertionRange(index));
895 property._setActive(true); 894 property._setActive(true);
896 return property; 895 return property;
897 }, 896 },
898 897
899 /** 898 /**
899 * @param {string} text
900 * @param {boolean} majorChange
901 * @param {function(?WebInspector.CSSStyleDeclaration)} callback
902 */
903 setText: function(text, majorChange, callback)
904 {
905 if (!this.styleSheetId) {
906 callback(null);
907 return;
908 }
909
910 /**
911 * @param {?Protocol.Error} error
912 * @param {!CSSAgent.CSSStyle} stylePayload
913 * @this {WebInspector.CSSStyleDeclaration}
914 */
915 function mycallback(error, stylePayload)
916 {
917 this._cssModel._pendingCommandsMajorState.pop();
918 if (!error) {
919 if (majorChange)
920 this._cssModel._domModel.markUndoableState();
921 callback(WebInspector.CSSStyleDeclaration.parsePayload(this._css Model, stylePayload));
922 return;
923 }
924 callback(null);
925 }
926
927 this._cssModel._pendingCommandsMajorState.push(majorChange);
928 this._cssModel._agent.setStyleText(this.styleSheetId, this.range.seriali zeToObject(), text, mycallback.bind(this));
929 },
930
931 /**
900 * @param {number} index 932 * @param {number} index
901 * @param {string} name 933 * @param {string} name
902 * @param {string} value 934 * @param {string} value
903 * @param {function(?WebInspector.CSSStyleDeclaration)=} userCallback 935 * @param {function(?WebInspector.CSSStyleDeclaration)=} userCallback
904 */ 936 */
905 insertPropertyAt: function(index, name, value, userCallback) 937 insertPropertyAt: function(index, name, value, userCallback)
906 { 938 {
907 /** 939 this.newBlankProperty(index).setText(name + ": " + value + ";", false, t rue, userCallback);
908 * @param {?string} error
909 * @param {!CSSAgent.CSSStyle} payload
910 * @this {!WebInspector.CSSStyleDeclaration}
911 */
912 function callback(error, payload)
913 {
914 this._cssModel._pendingCommandsMajorState.pop();
915 if (!userCallback)
916 return;
917
918 if (error) {
919 console.error(error);
920 userCallback(null);
921 } else
922 userCallback(WebInspector.CSSStyleDeclaration.parsePayload(this. _cssModel, payload));
923 }
924
925 if (!this.styleSheetId)
926 throw "No stylesheet id";
927
928 this._cssModel._pendingCommandsMajorState.push(true);
929 this._cssModel._agent.setPropertyText(this.styleSheetId, this._insertion Range(index), name + ": " + value + ";", callback.bind(this));
930 }, 940 },
931 941
932 /** 942 /**
933 * @param {string} name 943 * @param {string} name
934 * @param {string} value 944 * @param {string} value
935 * @param {function(?WebInspector.CSSStyleDeclaration)=} userCallback 945 * @param {function(?WebInspector.CSSStyleDeclaration)=} userCallback
936 */ 946 */
937 appendProperty: function(name, value, userCallback) 947 appendProperty: function(name, value, userCallback)
938 { 948 {
939 this.insertPropertyAt(this.allProperties.length, name, value, userCallba ck); 949 this.insertPropertyAt(this.allProperties.length, name, value, userCallba ck);
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
1234 }, 1244 },
1235 1245
1236 /** 1246 /**
1237 * @param {string} propertyText 1247 * @param {string} propertyText
1238 * @param {boolean} majorChange 1248 * @param {boolean} majorChange
1239 * @param {boolean} overwrite 1249 * @param {boolean} overwrite
1240 * @param {function(?WebInspector.CSSStyleDeclaration)=} userCallback 1250 * @param {function(?WebInspector.CSSStyleDeclaration)=} userCallback
1241 */ 1251 */
1242 setText: function(propertyText, majorChange, overwrite, userCallback) 1252 setText: function(propertyText, majorChange, overwrite, userCallback)
1243 { 1253 {
1244 /**
1245 * @param {?WebInspector.CSSStyleDeclaration} style
1246 */
1247 function enabledCallback(style)
1248 {
1249 if (userCallback)
1250 userCallback(style);
1251 }
1252
1253 /**
1254 * @param {?string} error
1255 * @param {!CSSAgent.CSSStyle} stylePayload
1256 * @this {WebInspector.CSSProperty}
1257 */
1258 function callback(error, stylePayload)
1259 {
1260 this.ownerStyle._cssModel._pendingCommandsMajorState.pop();
1261 if (!error) {
1262 if (majorChange)
1263 this.ownerStyle._cssModel._domModel.markUndoableState();
1264 var style = WebInspector.CSSStyleDeclaration.parsePayload(this.o wnerStyle._cssModel, stylePayload);
1265 var newProperty = style.allProperties[this.index];
1266
1267 if (newProperty && this.disabled && !propertyText.match(/^\s*$/) ) {
1268 newProperty.setDisabled(false, enabledCallback);
1269 return;
1270 }
1271 if (userCallback)
1272 userCallback(style);
1273 } else {
1274 if (userCallback)
1275 userCallback(null);
1276 }
1277 }
1278
1279 if (!this.ownerStyle) 1254 if (!this.ownerStyle)
1280 throw "No ownerStyle for property"; 1255 throw "No ownerStyle for property";
1281 1256
1282 if (!this.ownerStyle.styleSheetId) 1257 if (!this.ownerStyle.styleSheetId)
1283 throw "No owner style id"; 1258 throw "No owner style id";
1284 1259
1260 if (!this.range || !this.ownerStyle.range)
1261 throw "Style not editable";
1262
1285 if (majorChange) 1263 if (majorChange)
1286 WebInspector.userMetrics.StyleRuleEdited.record(); 1264 WebInspector.userMetrics.StyleRuleEdited.record();
1287 1265
1288 if (overwrite && propertyText === this.propertyText) { 1266 if (overwrite && propertyText === this.propertyText) {
1289 if (majorChange) 1267 if (majorChange)
1290 this.ownerStyle._cssModel._domModel.markUndoableState(); 1268 this.ownerStyle._cssModel._domModel.markUndoableState();
1291 if (userCallback) 1269 if (userCallback)
1292 userCallback(this.ownerStyle); 1270 userCallback(this.ownerStyle);
1293 return; 1271 return;
1294 } 1272 }
1295 1273
1296 // An index past all the properties adds a new property to the style. 1274 var range = this.range.relativeTo(this.ownerStyle.range.startLine, this. ownerStyle.range.startColumn);
1297 var cssModel = this.ownerStyle._cssModel; 1275 var indentation = this.ownerStyle.cssText ? this._detectIndentation(this .ownerStyle.cssText) : WebInspector.moduleSetting("textEditorIndent").get();
1298 cssModel._pendingCommandsMajorState.push(majorChange); 1276 var endIntentation = this.ownerStyle.cssText ? indentation.substring(0, this.ownerStyle.range.endColumn) : "";
1299 var range = /** @type {!WebInspector.TextRange} */ (this.range); 1277 var newStyleText = range.replaceInText(this.ownerStyle.cssText || "", "; " + propertyText);
1300 cssModel._agent.setPropertyText(this.ownerStyle.styleSheetId, overwrite ? range : range.collapseToStart(), propertyText, callback.bind(this)); 1278
1279 this._formatStyle(newStyleText, indentation, endIntentation, setStyleTex t.bind(this));
1280
1281 /**
1282 * @param {string} styleText
1283 * @this {WebInspector.CSSProperty}
1284 */
1285 function setStyleText(styleText)
1286 {
1287 this.ownerStyle.setText(styleText, majorChange, callback);
1288 }
1289
1290 /**
1291 * @param {?WebInspector.CSSStyleDeclaration} style
1292 */
1293 function callback(style)
1294 {
1295 if (userCallback)
1296 userCallback(style);
1297 }
1298
1299 /**
1300 * @param {?WebInspector.CSSStyleDeclaration} style
1301 */
1302 function enabledCallback(style)
1303 {
1304 if (userCallback)
1305 userCallback(style);
1306 }
1301 }, 1307 },
1302 1308
1303 /** 1309 /**
1310 * @param {string} styleText
1311 * @param {string} indentation
1312 * @param {string} endIndentation
1313 * @param {function(string)} callback
1314 */
1315 _formatStyle: function(styleText, indentation, endIndentation, callback)
1316 {
1317 self.runtime.instancePromise(WebInspector.TokenizerFactory).then(process Tokens);
1318 var result = "";
1319
1320 /**
1321 * @param {!WebInspector.TokenizerFactory} tokenizerFactory
1322 */
1323 function processTokens(tokenizerFactory)
1324 {
1325 var tokenize = tokenizerFactory.createTokenizer("text/css");
1326 tokenize(styleText, processToken);
1327 callback(result + (indentation ? "\n" + endIndentation : ""));
1328 }
1329
1330 var lastWasSemicolon = true;
1331 var insideProperty = false;
1332 /**
1333 * @param {string} token
1334 * @param {?string} tokenType
1335 * @param {number} column
1336 * @param {number} newColumn
1337 */
1338 function processToken(token, tokenType, column, newColumn)
1339 {
1340 var isSemicolon = token === ";";
1341 if (isSemicolon && lastWasSemicolon)
1342 return;
1343 lastWasSemicolon = isSemicolon || (lastWasSemicolon && tokenType === "css-comment") || (lastWasSemicolon && !token.trim());
1344
1345 // No formatting, only remove dupe ;
1346 if (!indentation) {
1347 result += token;
1348 return;
1349 }
1350
1351 // Format line breaks.
1352 if (!insideProperty && !token.trim())
1353 return;
1354 if (tokenType === "css-comment" && token.includes(":") && token.incl udes(";")) {
1355 result += "\n" + indentation + token;
1356 insideProperty = false;
1357 return;
1358 }
1359
1360 if (isSemicolon)
1361 insideProperty = false;
1362
1363 if (tokenType === "css-tag") {
1364 result += "\n" + indentation;
1365 insideProperty = true;
1366 }
1367 result += token;
1368 }
1369 },
1370
1371 /**
1372 * @param {string} text
1373 * @return {string}
1374 */
1375 _detectIndentation: function(text)
1376 {
1377 var lines = text.split("\n");
1378 if (lines.length < 2)
1379 return "";
1380 return WebInspector.TextUtils.lineIndent(lines[1]);
1381 },
1382
1383 /**
1304 * @param {string} newValue 1384 * @param {string} newValue
1305 * @param {boolean} majorChange 1385 * @param {boolean} majorChange
1306 * @param {boolean} overwrite 1386 * @param {boolean} overwrite
1307 * @param {function(?WebInspector.CSSStyleDeclaration)=} userCallback 1387 * @param {function(?WebInspector.CSSStyleDeclaration)=} userCallback
1308 */ 1388 */
1309 setValue: function(newValue, majorChange, overwrite, userCallback) 1389 setValue: function(newValue, majorChange, overwrite, userCallback)
1310 { 1390 {
1311 var text = this.name + ": " + newValue + (this.important ? " !important" : "") + ";"; 1391 var text = this.name + ": " + newValue + (this.important ? " !important" : "") + ";";
1312 this.setText(text, majorChange, overwrite, userCallback); 1392 this.setText(text, majorChange, overwrite, userCallback);
1313 }, 1393 },
(...skipping 535 matching lines...) Expand 10 before | Expand all | Expand 10 after
1849 } 1929 }
1850 1930
1851 /** 1931 /**
1852 * @param {!WebInspector.DOMNode} node 1932 * @param {!WebInspector.DOMNode} node
1853 * @return {!WebInspector.CSSStyleModel} 1933 * @return {!WebInspector.CSSStyleModel}
1854 */ 1934 */
1855 WebInspector.CSSStyleModel.fromNode = function(node) 1935 WebInspector.CSSStyleModel.fromNode = function(node)
1856 { 1936 {
1857 return /** @type {!WebInspector.CSSStyleModel} */ (WebInspector.CSSStyleMode l.fromTarget(node.target())); 1937 return /** @type {!WebInspector.CSSStyleModel} */ (WebInspector.CSSStyleMode l.fromTarget(node.target()));
1858 } 1938 }
OLDNEW
« no previous file with comments | « Source/devtools/front_end/externs.js ('k') | Source/devtools/front_end/ui/DOMSyntaxHighlighter.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698