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

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

Issue 1308663002: DevTools: simplify WI.CSSProperty and WI.CSSStyleDeclaration interfaces (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: fix test Created 5 years, 4 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/devtools/front_end/elements/StylesSidebarPane.js ('k') | no next file » | 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 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 646 matching lines...) Expand 10 before | Expand all | Expand 10 after
657 /** @type {!Map.<string, string>} */ 657 /** @type {!Map.<string, string>} */
658 this._shorthandValues = new Map(); 658 this._shorthandValues = new Map();
659 /** @type {!Set.<string>} */ 659 /** @type {!Set.<string>} */
660 this._shorthandIsImportant = new Set(); 660 this._shorthandIsImportant = new Set();
661 for (var i = 0; i < shorthandEntries.length; ++i) { 661 for (var i = 0; i < shorthandEntries.length; ++i) {
662 this._shorthandValues.set(shorthandEntries[i].name, shorthandEntries[i]. value); 662 this._shorthandValues.set(shorthandEntries[i].name, shorthandEntries[i]. value);
663 if (shorthandEntries[i].important) 663 if (shorthandEntries[i].important)
664 this._shorthandIsImportant.add(shorthandEntries[i].name); 664 this._shorthandIsImportant.add(shorthandEntries[i].name);
665 } 665 }
666 666
667 this._livePropertyMap = {}; // LIVE properties (source-based or style-based) : { name -> CSSProperty } 667 this._allProperties = [];
668 this._allProperties = []; // ALL properties: [ CSSProperty ] 668 for (var i = 0; i < payload.cssProperties.length; ++i) {
669 this.__disabledProperties = {}; // DISABLED properties: { index -> CSSProper ty }
670 var payloadPropertyCount = payload.cssProperties.length;
671
672 for (var i = 0; i < payloadPropertyCount; ++i) {
673 var property = WebInspector.CSSProperty.parsePayload(this, i, payload.cs sProperties[i]); 669 var property = WebInspector.CSSProperty.parsePayload(this, i, payload.cs sProperties[i]);
674 this._allProperties.push(property); 670 this._allProperties.push(property);
675 } 671 }
676 672
677 this._computeActiveProperties(); 673 this._generateSyntheticPropertiesIfNeeded();
674 this._computeInactiveProperties();
678 675
679 var propertyIndex = 0; 676 this._activePropertyMap = new Map();
680 for (var i = 0; i < this._allProperties.length; ++i) { 677 for (var property of this._allProperties) {
681 var property = this._allProperties[i]; 678 if (!property.activeInStyle())
682 if (property.disabled)
683 this.__disabledProperties[i] = property;
684 if (!property.active && !property.styleBased)
685 continue; 679 continue;
686 var name = property.name; 680 this._activePropertyMap.set(property.name, property);
687 this[propertyIndex] = name;
688 this._livePropertyMap[name] = property;
689 ++propertyIndex;
690 } 681 }
691 this.length = propertyIndex; 682
692 if ("cssText" in payload) 683 if ("cssText" in payload)
693 this.cssText = payload.cssText; 684 this.cssText = payload.cssText;
694 } 685 }
695 686
696 /** 687 /**
697 * @param {!WebInspector.CSSStyleModel} cssModel 688 * @param {!WebInspector.CSSStyleModel} cssModel
698 * @return {!WebInspector.CSSStyleDeclaration} 689 * @return {!WebInspector.CSSStyleDeclaration}
699 */ 690 */
700 WebInspector.CSSStyleDeclaration.createDummyStyle = function(cssModel) 691 WebInspector.CSSStyleDeclaration.createDummyStyle = function(cssModel)
701 { 692 {
702 var dummyPayload = { 693 var dummyPayload = {
703 shorthandEntries: [], 694 shorthandEntries: [],
704 cssProperties: [] 695 cssProperties: []
705 }; 696 };
706 return new WebInspector.CSSStyleDeclaration(cssModel, dummyPayload); 697 return new WebInspector.CSSStyleDeclaration(cssModel, dummyPayload);
707 } 698 }
708 699
709 /** 700 /**
710 * @param {!WebInspector.CSSStyleModel} cssModel 701 * @param {!WebInspector.CSSStyleModel} cssModel
711 * @param {!CSSAgent.CSSStyle} payload 702 * @param {!CSSAgent.CSSStyle} payload
712 * @return {!WebInspector.CSSStyleDeclaration} 703 * @return {!WebInspector.CSSStyleDeclaration}
713 */ 704 */
714 WebInspector.CSSStyleDeclaration.parsePayload = function(cssModel, payload) 705 WebInspector.CSSStyleDeclaration.parsePayload = function(cssModel, payload)
715 { 706 {
716 return new WebInspector.CSSStyleDeclaration(cssModel, payload); 707 return new WebInspector.CSSStyleDeclaration(cssModel, payload);
717 } 708 }
718 709
719 WebInspector.CSSStyleDeclaration.prototype = { 710 WebInspector.CSSStyleDeclaration.prototype = {
711 _generateSyntheticPropertiesIfNeeded: function()
712 {
713 if (this.range)
714 return;
715
716 if (!this._shorthandValues.size)
717 return;
718
719 var propertiesSet = new Set();
720 for (var property of this._allProperties)
721 propertiesSet.add(property.name);
722
723 var generatedProperties = [];
724 // For style-based properties, generate shorthands with values when poss ible.
725 for (var property of this._allProperties) {
726 // For style-based properties, try generating shorthands.
727 var shorthands = WebInspector.CSSMetadata.cssPropertiesMetainfo.shor thands(property.name) || [];
728 for (var shorthand of shorthands) {
729 if (propertiesSet.has(shorthand))
730 continue; // There already is a shorthand this longhands fa lls under.
731 var shorthandValue = this._shorthandValues.get(shorthand);
732 if (!shorthandValue)
733 continue; // Never generate synthetic shorthands when no va lue is available.
734
735 // Generate synthetic shorthand we have a value for.
736 var shorthandImportance = !!this._shorthandIsImportant.has(short hand);
737 var shorthandProperty = new WebInspector.CSSProperty(this, this. allProperties.length, shorthand, shorthandValue, shorthandImportance, false, tru e, false);
738 generatedProperties.push(shorthandProperty);
739 propertiesSet.add(shorthand);
740 }
741 }
742 this._allProperties = this._allProperties.concat(generatedProperties);
743 },
744
745 /**
746 * @return {!Array.<!WebInspector.CSSProperty>}
747 */
748 leadingProperties: function()
749 {
750 /**
751 * @param {!WebInspector.CSSProperty} property
752 * @return {boolean}
753 */
754 function propertyHasRange(property)
755 {
756 return !!property.range;
pfeldman 2015/08/25 02:00:24 This will always be this.range.
757 }
758
759 if (this.range)
760 return this._allProperties.filter(propertyHasRange);
761
762 var leadingProperties = [];
763 for (var property of this._allProperties) {
764 var shorthands = WebInspector.CSSMetadata.cssPropertiesMetainfo.shor thands(property.name) || [];
765 var belongToAnyShorthand = false;
766 for (var shorthand of shorthands) {
767 if (this._shorthandValues.get(shorthand)) {
768 belongToAnyShorthand = true;
769 break;
770 }
771 }
772 if (!belongToAnyShorthand)
773 leadingProperties.push(property);
774 }
775
776 return leadingProperties;
777 },
778
720 /** 779 /**
721 * @return {!WebInspector.Target} 780 * @return {!WebInspector.Target}
722 */ 781 */
723 target: function() 782 target: function()
724 { 783 {
725 return this._cssModel.target(); 784 return this._cssModel.target();
726 }, 785 },
727 786
728 /** 787 /**
729 * @return {!WebInspector.CSSStyleModel} 788 * @return {!WebInspector.CSSStyleModel}
(...skipping 11 matching lines...) Expand all
741 sourceStyleSheetEdited: function(styleSheetId, oldRange, newRange) 800 sourceStyleSheetEdited: function(styleSheetId, oldRange, newRange)
742 { 801 {
743 if (this.styleSheetId !== styleSheetId) 802 if (this.styleSheetId !== styleSheetId)
744 return; 803 return;
745 if (this.range) 804 if (this.range)
746 this.range = this.range.rebaseAfterTextEdit(oldRange, newRange); 805 this.range = this.range.rebaseAfterTextEdit(oldRange, newRange);
747 for (var i = 0; i < this._allProperties.length; ++i) 806 for (var i = 0; i < this._allProperties.length; ++i)
748 this._allProperties[i].sourceStyleSheetEdited(styleSheetId, oldRange , newRange); 807 this._allProperties[i].sourceStyleSheetEdited(styleSheetId, oldRange , newRange);
749 }, 808 },
750 809
751 _computeActiveProperties: function() 810 _computeInactiveProperties: function()
752 { 811 {
753 var activeProperties = {}; 812 var activeProperties = {};
754 for (var i = this._allProperties.length - 1; i >= 0; --i) { 813 for (var i = 0; i < this._allProperties.length; ++i) {
755 var property = this._allProperties[i]; 814 var property = this._allProperties[i];
756 if (property.styleBased || property.disabled) 815 if (property.disabled || !property.parsedOk) {
816 property._setActive(false);
757 continue; 817 continue;
758 property._setActive(false); 818 }
759 if (!property.parsedOk)
760 continue;
761 var canonicalName = WebInspector.CSSMetadata.canonicalPropertyName(p roperty.name); 819 var canonicalName = WebInspector.CSSMetadata.canonicalPropertyName(p roperty.name);
762 var activeProperty = activeProperties[canonicalName]; 820 var activeProperty = activeProperties[canonicalName];
763 if (!activeProperty || (!activeProperty.important && property.import ant)) 821 if (!activeProperty) {
764 activeProperties[canonicalName] = property; 822 activeProperties[canonicalName] = property;
765 } 823 } else if (!activeProperty.important || property.important) {
766 for (var propertyName in activeProperties) { 824 activeProperty._setActive(false);
767 var property = activeProperties[propertyName]; 825 activeProperties[canonicalName] = property;
768 property._setActive(true); 826 } else {
827 property._setActive(false);
828 }
769 } 829 }
770 }, 830 },
771 831
772 get allProperties() 832 get allProperties()
773 { 833 {
774 return this._allProperties; 834 return this._allProperties;
775 }, 835 },
776 836
777 /** 837 /**
778 * @param {string} name 838 * @param {string} name
779 * @return {?WebInspector.CSSProperty}
780 */
781 getLiveProperty: function(name)
782 {
783 return this._livePropertyMap[name] || null;
784 },
785
786 /**
787 * @param {string} name
788 * @return {string} 839 * @return {string}
789 */ 840 */
790 getPropertyValue: function(name) 841 getPropertyValue: function(name)
791 { 842 {
792 var property = this._livePropertyMap[name]; 843 var property = this._activePropertyMap.get(name);
793 return property ? property.value : ""; 844 return property ? property.value : "";
794 }, 845 },
795 846
796 /** 847 /**
797 * @param {string} name 848 * @param {string} name
798 * @return {boolean} 849 * @return {boolean}
799 */ 850 */
800 isPropertyImplicit: function(name) 851 isPropertyImplicit: function(name)
801 { 852 {
802 var property = this._livePropertyMap[name]; 853 var property = this._activePropertyMap.get(name);
803 return property ? property.implicit : ""; 854 return property ? property.implicit : "";
804 }, 855 },
805 856
806 /** 857 /**
807 * @param {string} name 858 * @param {string} name
808 * @return {!Array.<!WebInspector.CSSProperty>} 859 * @return {!Array.<!WebInspector.CSSProperty>}
809 */ 860 */
810 longhandProperties: function(name) 861 longhandProperties: function(name)
811 { 862 {
812 var longhands = WebInspector.CSSMetadata.cssPropertiesMetainfo.longhands (name); 863 var longhands = WebInspector.CSSMetadata.cssPropertiesMetainfo.longhands (name);
813 var result = []; 864 var result = [];
814 for (var i = 0; longhands && i < longhands.length; ++i) { 865 for (var i = 0; longhands && i < longhands.length; ++i) {
815 var property = this._livePropertyMap[longhands[i]]; 866 var property = this._activePropertyMap.get(longhands[i]);
816 if (property) 867 if (property)
817 result.push(property); 868 result.push(property);
818 } 869 }
819 return result; 870 return result;
820 }, 871 },
821 872
822 /** 873 /**
823 * @param {string} shorthandProperty
824 * @return {string}
825 */
826 shorthandValue: function(shorthandProperty)
827 {
828 return this._shorthandValues.get(shorthandProperty) || "";
829 },
830
831 /**
832 * @param {string} shorthandProperty
833 * @return {boolean}
834 */
835 shorthandIsImportant: function(shorthandProperty)
836 {
837 return this._shorthandIsImportant.has(shorthandProperty);
838 },
839
840 /**
841 * @param {number} index 874 * @param {number} index
842 * @return {?WebInspector.CSSProperty} 875 * @return {?WebInspector.CSSProperty}
843 */ 876 */
844 propertyAt: function(index) 877 propertyAt: function(index)
845 { 878 {
846 return (index < this.allProperties.length) ? this.allProperties[index] : null; 879 return (index < this.allProperties.length) ? this.allProperties[index] : null;
847 }, 880 },
848 881
849 /** 882 /**
850 * @return {number} 883 * @return {number}
(...skipping 18 matching lines...) Expand all
869 }, 902 },
870 903
871 /** 904 /**
872 * @param {number=} index 905 * @param {number=} index
873 * @return {!WebInspector.CSSProperty} 906 * @return {!WebInspector.CSSProperty}
874 */ 907 */
875 newBlankProperty: function(index) 908 newBlankProperty: function(index)
876 { 909 {
877 index = (typeof index === "undefined") ? this.pastLastSourcePropertyInde x() : index; 910 index = (typeof index === "undefined") ? this.pastLastSourcePropertyInde x() : index;
878 var property = new WebInspector.CSSProperty(this, index, "", "", false, false, true, false, "", this._insertionRange(index)); 911 var property = new WebInspector.CSSProperty(this, index, "", "", false, false, true, false, "", this._insertionRange(index));
879 property._setActive(true);
880 return property; 912 return property;
881 }, 913 },
882 914
883 /** 915 /**
884 * @param {string} text 916 * @param {string} text
885 * @param {boolean} majorChange 917 * @param {boolean} majorChange
886 * @return {!Promise.<?WebInspector.CSSStyleDeclaration>} 918 * @return {!Promise.<?WebInspector.CSSStyleDeclaration>}
887 */ 919 */
888 setText: function(text, majorChange) 920 setText: function(text, majorChange)
889 { 921 {
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
1144 */ 1176 */
1145 WebInspector.CSSProperty = function(ownerStyle, index, name, value, important, d isabled, parsedOk, implicit, text, range) 1177 WebInspector.CSSProperty = function(ownerStyle, index, name, value, important, d isabled, parsedOk, implicit, text, range)
1146 { 1178 {
1147 this.ownerStyle = ownerStyle; 1179 this.ownerStyle = ownerStyle;
1148 this.index = index; 1180 this.index = index;
1149 this.name = name; 1181 this.name = name;
1150 this.value = value; 1182 this.value = value;
1151 this.important = important; 1183 this.important = important;
1152 this.disabled = disabled; 1184 this.disabled = disabled;
1153 this.parsedOk = parsedOk; 1185 this.parsedOk = parsedOk;
1154 this.implicit = implicit; 1186 this.implicit = implicit; // A longhand, implicitly set by missing values of shorthand.
1155 this.text = text; 1187 this.text = text;
1156 this.range = range ? WebInspector.TextRange.fromObject(range) : null; 1188 this.range = range ? WebInspector.TextRange.fromObject(range) : null;
1189 this._active = true;
1157 } 1190 }
1158 1191
1159 /** 1192 /**
1160 * @param {?WebInspector.CSSStyleDeclaration} ownerStyle 1193 * @param {?WebInspector.CSSStyleDeclaration} ownerStyle
1161 * @param {number} index 1194 * @param {number} index
1162 * @param {!CSSAgent.CSSProperty} payload 1195 * @param {!CSSAgent.CSSProperty} payload
1163 * @return {!WebInspector.CSSProperty} 1196 * @return {!WebInspector.CSSProperty}
1164 */ 1197 */
1165 WebInspector.CSSProperty.parsePayload = function(ownerStyle, index, payload) 1198 WebInspector.CSSProperty.parsePayload = function(ownerStyle, index, payload)
1166 { 1199 {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1199 get propertyText() 1232 get propertyText()
1200 { 1233 {
1201 if (this.text !== undefined) 1234 if (this.text !== undefined)
1202 return this.text; 1235 return this.text;
1203 1236
1204 if (this.name === "") 1237 if (this.name === "")
1205 return ""; 1238 return "";
1206 return this.name + ": " + this.value + (this.important ? " !important" : "") + ";"; 1239 return this.name + ": " + this.value + (this.important ? " !important" : "") + ";";
1207 }, 1240 },
1208 1241
1209 get isLive() 1242 /**
1243 * @return {boolean}
1244 */
1245 activeInStyle: function()
1210 { 1246 {
1211 return this.active || this.styleBased; 1247 return this._active;
1212 },
1213
1214 get active()
1215 {
1216 return typeof this._active === "boolean" && this._active;
1217 },
1218
1219 get styleBased()
1220 {
1221 return !this.range;
1222 },
1223
1224 get inactive()
1225 {
1226 return typeof this._active === "boolean" && !this._active;
1227 }, 1248 },
1228 1249
1229 /** 1250 /**
1230 * @param {string} propertyText 1251 * @param {string} propertyText
1231 * @param {boolean} majorChange 1252 * @param {boolean} majorChange
1232 * @param {boolean} overwrite 1253 * @param {boolean} overwrite
1233 * @return {!Promise.<?WebInspector.CSSStyleDeclaration>} 1254 * @return {!Promise.<?WebInspector.CSSStyleDeclaration>}
1234 */ 1255 */
1235 setText: function(propertyText, majorChange, overwrite) 1256 setText: function(propertyText, majorChange, overwrite)
1236 { 1257 {
(...skipping 736 matching lines...) Expand 10 before | Expand all | Expand 10 after
1973 * @constructor 1994 * @constructor
1974 * @param {?WebInspector.CSSStyleDeclaration} inlineStyle 1995 * @param {?WebInspector.CSSStyleDeclaration} inlineStyle
1975 * @param {?WebInspector.CSSStyleDeclaration} attributesStyle 1996 * @param {?WebInspector.CSSStyleDeclaration} attributesStyle
1976 */ 1997 */
1977 WebInspector.CSSStyleModel.InlineStyleResult = function(inlineStyle, attributesS tyle) 1998 WebInspector.CSSStyleModel.InlineStyleResult = function(inlineStyle, attributesS tyle)
1978 { 1999 {
1979 this.inlineStyle = inlineStyle; 2000 this.inlineStyle = inlineStyle;
1980 this.attributesStyle = attributesStyle; 2001 this.attributesStyle = attributesStyle;
1981 } 2002 }
1982 2003
OLDNEW
« no previous file with comments | « Source/devtools/front_end/elements/StylesSidebarPane.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698