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

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/sdk/CSSMetadata.js

Issue 2215253002: DevTools: do not use CSSMetadata instances for autocompletion (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@css-meta-data
Patch Set: address comments Created 4 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
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Nikita Vasilyev. All rights reserved. 2 * Copyright (C) 2010 Nikita Vasilyev. All rights reserved.
3 * Copyright (C) 2010 Joseph Pecoraro. All rights reserved. 3 * Copyright (C) 2010 Joseph Pecoraro. All rights reserved.
4 * Copyright (C) 2010 Google Inc. All rights reserved. 4 * Copyright (C) 2010 Google Inc. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are 7 * modification, are permitted provided that the following conditions are
8 * met: 8 * met:
9 * 9 *
10 * * Redistributions of source code must retain the above copyright 10 * * Redistributions of source code must retain the above copyright
(...skipping 14 matching lines...) Expand all
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */ 31 */
32 32
33 /** 33 /**
34 * @constructor 34 * @constructor
35 * @param {!Array.<!{name: string, longhands: !Array.<string>}|string>} properti es 35 * @param {!Array.<!{name: string, longhands: !Array.<string>}>} properties
36 */ 36 */
37 WebInspector.CSSMetadata = function(properties) 37 WebInspector.CSSMetadata = function(properties)
38 { 38 {
39 this._values = /** !Array.<string> */ ([]); 39 this._values = /** !Array.<string> */ ([]);
40 this._longhands = {}; 40 this._longhands = {};
41 this._shorthands = {}; 41 this._shorthands = {};
42 for (var i = 0; i < properties.length; ++i) { 42 for (var i = 0; i < properties.length; ++i) {
43 var property = properties[i]; 43 var property = properties[i];
44 if (typeof property === "string") {
45 this._values.push(property);
46 continue;
47 }
48 var propertyName = property.name; 44 var propertyName = property.name;
49 if (!CSS.supports(propertyName, "initial")) 45 if (!CSS.supports(propertyName, "initial"))
50 continue; 46 continue;
51 this._values.push(propertyName); 47 this._values.push(propertyName);
52 48
53 var longhands = properties[i].longhands; 49 var longhands = properties[i].longhands;
54 if (longhands) { 50 if (longhands) {
55 this._longhands[propertyName] = longhands; 51 this._longhands[propertyName] = longhands;
56 for (var j = 0; j < longhands.length; ++j) { 52 for (var j = 0; j < longhands.length; ++j) {
57 var longhandName = longhands[j]; 53 var longhandName = longhands[j];
(...skipping 17 matching lines...) Expand all
75 { 71 {
76 return !!WebInspector.CSSMetadata._colorAwareProperties[propertyName.toLower Case()] || WebInspector.CSSMetadata.isCustomProperty(propertyName.toLowerCase()) ; 72 return !!WebInspector.CSSMetadata._colorAwareProperties[propertyName.toLower Case()] || WebInspector.CSSMetadata.isCustomProperty(propertyName.toLowerCase()) ;
77 } 73 }
78 74
79 /** 75 /**
80 * @param {string} propertyName 76 * @param {string} propertyName
81 * @return {boolean} 77 * @return {boolean}
82 */ 78 */
83 WebInspector.CSSMetadata.isLengthProperty = function(propertyName) 79 WebInspector.CSSMetadata.isLengthProperty = function(propertyName)
84 { 80 {
81 propertyName = propertyName.toLowerCase();
85 if (propertyName === "line-height") 82 if (propertyName === "line-height")
86 return false; 83 return false;
87 if (!WebInspector.CSSMetadata._distancePropertiesKeySet) 84 if (!WebInspector.CSSMetadata._distancePropertiesKeySet)
88 WebInspector.CSSMetadata._distancePropertiesKeySet = WebInspector.CSSMet adata._distanceProperties.keySet(); 85 WebInspector.CSSMetadata._distancePropertiesKeySet = WebInspector.CSSMet adata._distanceProperties.keySet();
89 return WebInspector.CSSMetadata._distancePropertiesKeySet[propertyName] || p ropertyName.startsWith("margin") || propertyName.startsWith("padding") || proper tyName.indexOf("width") !== -1 || propertyName.indexOf("height") !== -1; 86 return WebInspector.CSSMetadata._distancePropertiesKeySet[propertyName] || p ropertyName.startsWith("margin") || propertyName.startsWith("padding") || proper tyName.indexOf("width") !== -1 || propertyName.indexOf("height") !== -1;
90 } 87 }
91 88
92 /** 89 /**
93 * @param {string} propertyName 90 * @param {string} propertyName
94 * @return {boolean} 91 * @return {boolean}
(...skipping 26 matching lines...) Expand all
121 WebInspector.CSSMetadata.NonStandardInheritedProperties = [ 118 WebInspector.CSSMetadata.NonStandardInheritedProperties = [
122 "-webkit-font-smoothing" 119 "-webkit-font-smoothing"
123 ].keySet(); 120 ].keySet();
124 121
125 /** 122 /**
126 * @param {string} name 123 * @param {string} name
127 * @return {string} 124 * @return {string}
128 */ 125 */
129 WebInspector.CSSMetadata.canonicalPropertyName = function(name) 126 WebInspector.CSSMetadata.canonicalPropertyName = function(name)
130 { 127 {
128 name = name.toLowerCase();
131 if (!name || name.length < 9 || name.charAt(0) !== "-") 129 if (!name || name.length < 9 || name.charAt(0) !== "-")
132 return name.toLowerCase(); 130 return name;
133 var match = name.match(/(?:-webkit-)(.+)/); 131 var match = name.match(/(?:-webkit-)(.+)/);
134 var propertiesSet = WebInspector.CSSMetadata.cssPropertiesMetainfo.keySet(); 132 if (!match || !WebInspector.CSSMetadata.cssPropertiesMetainfo.hasProperty(ma tch[1]))
135 var hasSupportedProperties = WebInspector.CSSMetadata.cssPropertiesMetainfo. _values.length > 0; 133 return name;
136 if (!match || (hasSupportedProperties && !propertiesSet.hasOwnProperty(match [1].toLowerCase()))) 134 return match[1];
137 return name.toLowerCase();
138 return match[1].toLowerCase();
139 } 135 }
140 136
141 /** 137 /**
142 * @param {string} propertyName 138 * @param {string} propertyName
143 * @return {boolean} 139 * @return {boolean}
144 */ 140 */
145 WebInspector.CSSMetadata.isCSSPropertyName = function(propertyName) 141 WebInspector.CSSMetadata.isCSSPropertyName = function(propertyName)
146 { 142 {
143 propertyName = propertyName.toLowerCase();
147 if (propertyName.startsWith("-moz-") || propertyName.startsWith("-o-") || pr opertyName.startsWith("-webkit-") || propertyName.startsWith("-ms-")) 144 if (propertyName.startsWith("-moz-") || propertyName.startsWith("-o-") || pr opertyName.startsWith("-webkit-") || propertyName.startsWith("-ms-"))
148 return true; 145 return true;
149 var hasSupportedProperties = WebInspector.CSSMetadata.cssPropertiesMetainfo. _values.length > 0; 146 return WebInspector.CSSMetadata.cssPropertiesMetainfo.hasProperty(propertyNa me);
150 return !hasSupportedProperties || WebInspector.CSSMetadata.cssPropertiesMeta info.keySet().hasOwnProperty(propertyName);
151 } 147 }
152 148
153 /** 149 /**
154 * @param {string} propertyName 150 * @param {string} propertyName
155 * @return {boolean} 151 * @return {boolean}
156 */ 152 */
157 WebInspector.CSSMetadata.isPropertyInherited = function(propertyName) 153 WebInspector.CSSMetadata.isPropertyInherited = function(propertyName)
158 { 154 {
159 return !!(WebInspector.CSSMetadata.InheritedProperties[WebInspector.CSSMetad ata.canonicalPropertyName(propertyName)] 155 return !!(WebInspector.CSSMetadata.InheritedProperties[WebInspector.CSSMetad ata.canonicalPropertyName(propertyName)]
160 || WebInspector.CSSMetadata.NonStandardInheritedProperties[propertyN ame.toLowerCase()]); 156 || WebInspector.CSSMetadata.NonStandardInheritedProperties[propertyN ame.toLowerCase()]);
(...skipping 475 matching lines...) Expand 10 before | Expand all | Expand 10 after
636 "difference", "exclusion", "hue", "saturation", "color", "luminosity", " unset" 632 "difference", "exclusion", "hue", "saturation", "color", "luminosity", " unset"
637 ] }, 633 ] },
638 "background-blend-mode": { values: [ 634 "background-blend-mode": { values: [
639 "normal", "multiply", "screen", "overlay", "darken", "lighten", "color-d odge", "color-burn", "hard-light", "soft-light", 635 "normal", "multiply", "screen", "overlay", "darken", "lighten", "color-d odge", "color-burn", "hard-light", "soft-light",
640 "difference", "exclusion", "hue", "saturation", "color", "luminosity", " unset" 636 "difference", "exclusion", "hue", "saturation", "color", "luminosity", " unset"
641 ] }, 637 ] },
642 } 638 }
643 639
644 /** 640 /**
645 * @param {string} propertyName 641 * @param {string} propertyName
646 * @return {!WebInspector.CSSMetadata} 642 * @return {!Array<string>}
647 */ 643 */
648 WebInspector.CSSMetadata.keywordsForProperty = function(propertyName) 644 WebInspector.CSSMetadata.propertyValues = function(propertyName)
649 { 645 {
650 var acceptedKeywords = ["inherit", "initial"]; 646 var acceptedKeywords = ["inherit", "initial"];
651 var descriptor = WebInspector.CSSMetadata.descriptor(propertyName); 647 propertyName = propertyName.toLowerCase();
652 if (descriptor && descriptor.values) 648 var unprefixedName = propertyName.replace(/^-webkit-/, "");
653 acceptedKeywords.push.apply(acceptedKeywords, descriptor.values); 649 var entry = WebInspector.CSSMetadata._propertyDataMap[propertyName] || WebIn spector.CSSMetadata._propertyDataMap[unprefixedName];
650 if (entry && entry.values)
651 acceptedKeywords.pushAll(entry.values);
654 if (WebInspector.CSSMetadata.isColorAwareProperty(propertyName)) { 652 if (WebInspector.CSSMetadata.isColorAwareProperty(propertyName)) {
655 acceptedKeywords.push("currentColor"); 653 acceptedKeywords.push("currentColor");
656 for (var color in WebInspector.Color.Nicknames) 654 for (var color in WebInspector.Color.Nicknames)
657 acceptedKeywords.push(color); 655 acceptedKeywords.push(color);
658 } 656 }
659 return new WebInspector.CSSMetadata(acceptedKeywords); 657 return acceptedKeywords.sort();
660 }
661
662 /**
663 * @param {string} propertyName
664 * @return {?Object}
665 */
666 WebInspector.CSSMetadata.descriptor = function(propertyName)
667 {
668 if (!propertyName)
669 return null;
670 var unprefixedName = propertyName.replace(/^-webkit-/, "");
671 propertyName = propertyName.toLowerCase();
672 var entry = WebInspector.CSSMetadata._propertyDataMap[propertyName];
673 if (!entry && unprefixedName !== propertyName)
674 entry = WebInspector.CSSMetadata._propertyDataMap[unprefixedName];
675 return entry || null;
676 } 658 }
677 659
678 WebInspector.CSSMetadata.initializeWithSupportedProperties = function(properties ) 660 WebInspector.CSSMetadata.initializeWithSupportedProperties = function(properties )
679 { 661 {
680 WebInspector.CSSMetadata.cssPropertiesMetainfo = new WebInspector.CSSMetadat a(properties); 662 WebInspector.CSSMetadata.cssPropertiesMetainfo = new WebInspector.CSSMetadat a(properties);
681 } 663 }
682 664
683 // Weight of CSS properties based on their usage from https://www.chromestatus.c om/metrics/css/popularity 665 // Weight of CSS properties based on their usage from https://www.chromestatus.c om/metrics/css/popularity
684 WebInspector.CSSMetadata.Weight = { 666 WebInspector.CSSMetadata.Weight = {
685 "align-content": 57, 667 "align-content": 57,
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
931 "width": 268, 913 "width": 268,
932 "will-change": 74, 914 "will-change": 74,
933 "word-break": 166, 915 "word-break": 166,
934 "word-spacing": 157, 916 "word-spacing": 157,
935 "word-wrap": 197, 917 "word-wrap": 197,
936 "writing-mode": 41, 918 "writing-mode": 41,
937 "z-index": 239, 919 "z-index": 239,
938 "zoom": 200 920 "zoom": 200
939 }; 921 };
940 922
923 /**
924 * @param {!Array.<string>} properties
925 * @return {number}
926 */
927 WebInspector.CSSMetadata.mostUsedProperty = function(properties)
928 {
929 var maxWeight = 0;
930 var index = 0;
931 for (var i = 0; i < properties.length; i++) {
932 var weight = WebInspector.CSSMetadata.Weight[properties[i]];
933 if (!weight)
934 weight = WebInspector.CSSMetadata.Weight[WebInspector.CSSMetadata.ca nonicalPropertyName(properties[i])];
935 if (weight > maxWeight) {
936 maxWeight = weight;
937 index = i;
938 }
939 }
940 return index;
941 }
941 942
942 WebInspector.CSSMetadata.prototype = { 943 WebInspector.CSSMetadata.prototype = {
943 /** 944 /**
944 * @param {string} prefix 945 * @return {!Array<string>}
945 * @return {!Array.<string>}
946 */ 946 */
947 startsWith: function(prefix) 947 allProperties: function()
948 { 948 {
949 var firstIndex = this._firstIndexOfPrefix(prefix); 949 return this._values;
950 if (firstIndex === -1)
951 return [];
952
953 var results = [];
954 while (firstIndex < this._values.length && this._values[firstIndex].star tsWith(prefix))
955 results.push(this._values[firstIndex++]);
956 return results;
957 }, 950 },
958 951
959 /** 952 /**
960 * @param {!Array.<string>} properties 953 * @param {string} propertyName
961 * @return {number} 954 * @return {boolean}
962 */ 955 */
963 mostUsedOf: function(properties) 956 hasProperty: function(propertyName)
964 { 957 {
965 var maxWeight = 0; 958 if (!this._valuesSet)
966 var index = 0; 959 this._valuesSet = new Set(this._values);
967 for (var i = 0; i < properties.length; i++) { 960 return this._valuesSet.has(propertyName);
968 var weight = WebInspector.CSSMetadata.Weight[properties[i]];
969 if (!weight)
970 weight = WebInspector.CSSMetadata.Weight[WebInspector.CSSMetadat a.canonicalPropertyName(properties[i])];
971 if (weight > maxWeight) {
972 maxWeight = weight;
973 index = i;
974 }
975 }
976 return index;
977 },
978
979 _firstIndexOfPrefix: function(prefix)
980 {
981 if (!this._values.length)
982 return -1;
983 if (!prefix)
984 return 0;
985
986 var maxIndex = this._values.length - 1;
987 var minIndex = 0;
988 var foundIndex;
989
990 do {
991 var middleIndex = (maxIndex + minIndex) >> 1;
992 if (this._values[middleIndex].startsWith(prefix)) {
993 foundIndex = middleIndex;
994 break;
995 }
996 if (this._values[middleIndex] < prefix)
997 minIndex = middleIndex + 1;
998 else
999 maxIndex = middleIndex - 1;
1000 } while (minIndex <= maxIndex);
1001
1002 if (foundIndex === undefined)
1003 return -1;
1004
1005 while (foundIndex && this._values[foundIndex - 1].startsWith(prefix))
1006 foundIndex--;
1007
1008 return foundIndex;
1009 }, 961 },
1010 962
1011 /** 963 /**
1012 * @return {!Object.<string, boolean>}
1013 */
1014 keySet: function()
1015 {
1016 if (!this._keySet)
1017 this._keySet = this._values.keySet();
1018 return this._keySet;
1019 },
1020
1021 /**
1022 * @param {string} str
1023 * @param {string} prefix
1024 * @return {string}
1025 */
1026 next: function(str, prefix)
1027 {
1028 return this._closest(str, prefix, 1);
1029 },
1030
1031 /**
1032 * @param {string} str
1033 * @param {string} prefix
1034 * @return {string}
1035 */
1036 previous: function(str, prefix)
1037 {
1038 return this._closest(str, prefix, -1);
1039 },
1040
1041 /**
1042 * @param {string} str
1043 * @param {string} prefix
1044 * @param {number} shift
1045 * @return {string}
1046 */
1047 _closest: function(str, prefix, shift)
1048 {
1049 if (!str)
1050 return "";
1051
1052 var index = this._values.indexOf(str);
1053 if (index === -1)
1054 return "";
1055
1056 if (!prefix) {
1057 index = (index + this._values.length + shift) % this._values.length;
1058 return this._values[index];
1059 }
1060
1061 var propertiesWithPrefix = this.startsWith(prefix);
1062 var j = propertiesWithPrefix.indexOf(str);
1063 j = (j + propertiesWithPrefix.length + shift) % propertiesWithPrefix.len gth;
1064 return propertiesWithPrefix[j];
1065 },
1066
1067 /**
1068 * @param {string} shorthand 964 * @param {string} shorthand
1069 * @return {?Array.<string>} 965 * @return {?Array.<string>}
1070 */ 966 */
1071 longhands: function(shorthand) 967 longhands: function(shorthand)
1072 { 968 {
1073 return this._longhands[shorthand]; 969 return this._longhands[shorthand];
1074 }, 970 },
1075 971
1076 /** 972 /**
1077 * @param {string} longhand 973 * @param {string} longhand
1078 * @return {?Array.<string>} 974 * @return {?Array.<string>}
1079 */ 975 */
1080 shorthands: function(longhand) 976 shorthands: function(longhand)
1081 { 977 {
1082 return this._shorthands[longhand]; 978 return this._shorthands[longhand];
1083 } 979 }
1084 } 980 }
1085 981
1086 WebInspector.CSSMetadata.initializeWithSupportedProperties([]); 982 WebInspector.CSSMetadata.initializeWithSupportedProperties([]);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698