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

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: all tests passing 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
1285 if (majorChange) 1260 if (majorChange)
1286 WebInspector.userMetrics.StyleRuleEdited.record(); 1261 WebInspector.userMetrics.StyleRuleEdited.record();
1287 1262
1288 if (overwrite && propertyText === this.propertyText) { 1263 if (overwrite && propertyText === this.propertyText) {
1289 if (majorChange) 1264 if (majorChange)
1290 this.ownerStyle._cssModel._domModel.markUndoableState(); 1265 this.ownerStyle._cssModel._domModel.markUndoableState();
1291 if (userCallback) 1266 if (userCallback)
1292 userCallback(this.ownerStyle); 1267 userCallback(this.ownerStyle);
1293 return; 1268 return;
1294 } 1269 }
1295 1270
lushnikov 2015/06/19 15:34:21 remove typecast
pfeldman 2015/06/19 15:59:28 Done.
1296 // An index past all the properties adds a new property to the style. 1271 var range = /** @type {!WebInspector.TextRange} */ (this.range).relative To(this.ownerStyle.range.startLine, this.ownerStyle.range.startColumn);
1297 var cssModel = this.ownerStyle._cssModel; 1272 var indentation = this.ownerStyle.cssText ? this._detectIndentation(this .ownerStyle.cssText) : WebInspector.moduleSetting("textEditorIndent").get();
1298 cssModel._pendingCommandsMajorState.push(majorChange); 1273 var endIntentation = this.ownerStyle.cssText ? indentation.substring(0, this.ownerStyle.range.endColumn) : "";
1299 var range = /** @type {!WebInspector.TextRange} */ (this.range); 1274 var newStyleText = range.replaceInText(this.ownerStyle.cssText || "", "; " + propertyText);
1300 cssModel._agent.setPropertyText(this.ownerStyle.styleSheetId, overwrite ? range : range.collapseToStart(), propertyText, callback.bind(this)); 1275
1276 this._formatStyle(newStyleText, indentation, endIntentation, setStyleTex t.bind(this));
1277
1278 /**
1279 * @param {string} styleText
1280 * @this {WebInspector.CSSProperty}
1281 */
1282 function setStyleText(styleText)
1283 {
1284 this.ownerStyle.setText(styleText, majorChange, callback.bind(this)) ;
1285 }
1286
1287 /**
1288 * @param {?WebInspector.CSSStyleDeclaration} style
1289 * @this {WebInspector.CSSProperty}
1290 */
1291 function callback(style)
1292 {
1293 if (style) {
1294 var newProperty = style.allProperties[this.index];
1295 if (newProperty && this.disabled && !propertyText.match(/^\s*$/) ) {
1296 newProperty.setDisabled(false, enabledCallback);
lushnikov 2015/06/19 15:34:21 you swear you can remove this
pfeldman 2015/06/19 15:59:28 Done.
1297 return;
1298 }
1299 }
1300 if (userCallback)
1301 userCallback(style);
1302 }
1303
1304 /**
1305 * @param {?WebInspector.CSSStyleDeclaration} style
1306 */
1307 function enabledCallback(style)
1308 {
1309 if (userCallback)
1310 userCallback(style);
1311 }
1301 }, 1312 },
1302 1313
1303 /** 1314 /**
1315 * @param {string} styleText
1316 * @param {string} indentation
1317 * @param {string} endIndentation
1318 * @param {function(string)} callback
1319 */
1320 _formatStyle: function(styleText, indentation, endIndentation, callback)
1321 {
1322 self.runtime.instancePromise(WebInspector.TokenizerFactory).then(process Tokens);
1323 var result = "";
1324
1325 /**
1326 * @param {!WebInspector.TokenizerFactory} tokenizerFactory
1327 */
1328 function processTokens(tokenizerFactory)
1329 {
1330 var tokenize = tokenizerFactory.createTokenizer("text/css");
1331 tokenize(styleText, processToken);
1332 callback(result + (indentation ? "\n" + endIndentation : ""));
1333 }
1334
1335 var lastWasSemicolon = true;
1336 var trimWhitespace = true;
lushnikov 2015/06/19 15:34:21 insideProperty
pfeldman 2015/06/19 15:59:28 Done.
1337 /**
1338 * @param {string} token
1339 * @param {?string} tokenType
1340 * @param {number} column
1341 * @param {number} newColumn
1342 */
1343 function processToken(token, tokenType, column, newColumn)
1344 {
1345 var isSemicolon = token === ";";
1346 if (isSemicolon && lastWasSemicolon)
1347 return;
1348 lastWasSemicolon = isSemicolon || (lastWasSemicolon && tokenType === "css-comment") || (lastWasSemicolon && !token.trim());
1349
1350 // No formatting, only remove dupe ;
1351 if (!indentation) {
1352 result += token;
1353 return;
1354 }
1355
1356 // Format line breaks.
1357 if (trimWhitespace && !token.trim())
1358 return;
1359 if (tokenType === "css-comment" && token.includes(":") && token.incl udes(";")) {
1360 result += "\n" + indentation + token;
1361 trimWhitespace = true;
1362 return;
1363 }
1364
1365 if (isSemicolon)
1366 trimWhitespace = true;
1367 if (tokenType === "css-tag") {
1368 result += "\n" + indentation;
1369 trimWhitespace = false;
1370 }
1371 result += token;
1372 }
1373 },
1374
1375 /**
1376 * @param {string} text
1377 * @return {string}
1378 */
1379 _detectIndentation: function(text)
1380 {
1381 var lines = text.split("\n");
1382 if (lines.length < 2)
1383 return "";
1384 return WebInspector.TextUtils.lineIndent(lines[1]);
1385 },
1386
1387 /**
1304 * @param {string} newValue 1388 * @param {string} newValue
1305 * @param {boolean} majorChange 1389 * @param {boolean} majorChange
1306 * @param {boolean} overwrite 1390 * @param {boolean} overwrite
1307 * @param {function(?WebInspector.CSSStyleDeclaration)=} userCallback 1391 * @param {function(?WebInspector.CSSStyleDeclaration)=} userCallback
1308 */ 1392 */
1309 setValue: function(newValue, majorChange, overwrite, userCallback) 1393 setValue: function(newValue, majorChange, overwrite, userCallback)
1310 { 1394 {
1311 var text = this.name + ": " + newValue + (this.important ? " !important" : "") + ";"; 1395 var text = this.name + ": " + newValue + (this.important ? " !important" : "") + ";";
1312 this.setText(text, majorChange, overwrite, userCallback); 1396 this.setText(text, majorChange, overwrite, userCallback);
1313 }, 1397 },
(...skipping 535 matching lines...) Expand 10 before | Expand all | Expand 10 after
1849 } 1933 }
1850 1934
1851 /** 1935 /**
1852 * @param {!WebInspector.DOMNode} node 1936 * @param {!WebInspector.DOMNode} node
1853 * @return {!WebInspector.CSSStyleModel} 1937 * @return {!WebInspector.CSSStyleModel}
1854 */ 1938 */
1855 WebInspector.CSSStyleModel.fromNode = function(node) 1939 WebInspector.CSSStyleModel.fromNode = function(node)
1856 { 1940 {
1857 return /** @type {!WebInspector.CSSStyleModel} */ (WebInspector.CSSStyleMode l.fromTarget(node.target())); 1941 return /** @type {!WebInspector.CSSStyleModel} */ (WebInspector.CSSStyleMode l.fromTarget(node.target()));
1858 } 1942 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698