Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 |
|
lushnikov
2016/08/05 07:16:33
The ambiguity here was coming from the WI.CSSMetad
| |
| 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 Loading... | |
| 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 { |
| 85 if (propertyName === "line-height") | 81 if (propertyName === "line-height") |
|
dgozman
2016/08/05 16:38:21
toLowerCase
lushnikov
2016/08/05 17:12:01
Done.
| |
| 86 return false; | 82 return false; |
| 87 if (!WebInspector.CSSMetadata._distancePropertiesKeySet) | 83 if (!WebInspector.CSSMetadata._distancePropertiesKeySet) |
| 88 WebInspector.CSSMetadata._distancePropertiesKeySet = WebInspector.CSSMet adata._distanceProperties.keySet(); | 84 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; | 85 return WebInspector.CSSMetadata._distancePropertiesKeySet[propertyName] || p ropertyName.startsWith("margin") || propertyName.startsWith("padding") || proper tyName.indexOf("width") !== -1 || propertyName.indexOf("height") !== -1; |
| 90 } | 86 } |
| 91 | 87 |
| 92 /** | 88 /** |
| 93 * @param {string} propertyName | 89 * @param {string} propertyName |
| 94 * @return {boolean} | 90 * @return {boolean} |
| 95 */ | 91 */ |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 123 ].keySet(); | 119 ].keySet(); |
| 124 | 120 |
| 125 /** | 121 /** |
| 126 * @param {string} name | 122 * @param {string} name |
| 127 * @return {string} | 123 * @return {string} |
| 128 */ | 124 */ |
| 129 WebInspector.CSSMetadata.canonicalPropertyName = function(name) | 125 WebInspector.CSSMetadata.canonicalPropertyName = function(name) |
| 130 { | 126 { |
| 131 if (!name || name.length < 9 || name.charAt(0) !== "-") | 127 if (!name || name.length < 9 || name.charAt(0) !== "-") |
| 132 return name.toLowerCase(); | 128 return name.toLowerCase(); |
| 133 var match = name.match(/(?:-webkit-)(.+)/); | 129 var match = name.match(/(?:-webkit-)(.+)/); |
|
dgozman
2016/08/05 16:38:21
toLowerCase
lushnikov
2016/08/05 17:12:01
Done.
| |
| 134 var propertiesSet = WebInspector.CSSMetadata.cssPropertiesMetainfo.keySet(); | 130 if (!match || !WebInspector.CSSMetadata.cssPropertiesMetainfo.hasProperty(ma tch[1].toLowerCase())) |
| 135 var hasSupportedProperties = WebInspector.CSSMetadata.cssPropertiesMetainfo. _values.length > 0; | |
|
lushnikov
2016/08/05 07:16:33
cssPropertiesMetainfo could be empty iff the Suppo
| |
| 136 if (!match || (hasSupportedProperties && !propertiesSet.hasOwnProperty(match [1].toLowerCase()))) | |
| 137 return name.toLowerCase(); | 131 return name.toLowerCase(); |
| 138 return match[1].toLowerCase(); | 132 return match[1].toLowerCase(); |
| 139 } | 133 } |
| 140 | 134 |
| 141 /** | 135 /** |
| 142 * @param {string} propertyName | 136 * @param {string} propertyName |
| 143 * @return {boolean} | 137 * @return {boolean} |
| 144 */ | 138 */ |
| 145 WebInspector.CSSMetadata.isCSSPropertyName = function(propertyName) | 139 WebInspector.CSSMetadata.isCSSPropertyName = function(propertyName) |
| 146 { | 140 { |
| 141 propertyName = propertyName.toLowerCase(); | |
| 147 if (propertyName.startsWith("-moz-") || propertyName.startsWith("-o-") || pr opertyName.startsWith("-webkit-") || propertyName.startsWith("-ms-")) | 142 if (propertyName.startsWith("-moz-") || propertyName.startsWith("-o-") || pr opertyName.startsWith("-webkit-") || propertyName.startsWith("-ms-")) |
| 148 return true; | 143 return true; |
| 149 var hasSupportedProperties = WebInspector.CSSMetadata.cssPropertiesMetainfo. _values.length > 0; | 144 return WebInspector.CSSMetadata.cssPropertiesMetainfo.hasProperty(propertyNa me); |
| 150 return !hasSupportedProperties || WebInspector.CSSMetadata.cssPropertiesMeta info.keySet().hasOwnProperty(propertyName); | |
| 151 } | 145 } |
| 152 | 146 |
| 153 /** | 147 /** |
| 154 * @param {string} propertyName | 148 * @param {string} propertyName |
| 155 * @return {boolean} | 149 * @return {boolean} |
| 156 */ | 150 */ |
| 157 WebInspector.CSSMetadata.isPropertyInherited = function(propertyName) | 151 WebInspector.CSSMetadata.isPropertyInherited = function(propertyName) |
| 158 { | 152 { |
| 159 return !!(WebInspector.CSSMetadata.InheritedProperties[WebInspector.CSSMetad ata.canonicalPropertyName(propertyName)] | 153 return !!(WebInspector.CSSMetadata.InheritedProperties[WebInspector.CSSMetad ata.canonicalPropertyName(propertyName)] |
| 160 || WebInspector.CSSMetadata.NonStandardInheritedProperties[propertyN ame.toLowerCase()]); | 154 || WebInspector.CSSMetadata.NonStandardInheritedProperties[propertyN ame.toLowerCase()]); |
| (...skipping 475 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 636 "difference", "exclusion", "hue", "saturation", "color", "luminosity", " unset" | 630 "difference", "exclusion", "hue", "saturation", "color", "luminosity", " unset" |
| 637 ] }, | 631 ] }, |
| 638 "background-blend-mode": { values: [ | 632 "background-blend-mode": { values: [ |
| 639 "normal", "multiply", "screen", "overlay", "darken", "lighten", "color-d odge", "color-burn", "hard-light", "soft-light", | 633 "normal", "multiply", "screen", "overlay", "darken", "lighten", "color-d odge", "color-burn", "hard-light", "soft-light", |
| 640 "difference", "exclusion", "hue", "saturation", "color", "luminosity", " unset" | 634 "difference", "exclusion", "hue", "saturation", "color", "luminosity", " unset" |
| 641 ] }, | 635 ] }, |
| 642 } | 636 } |
| 643 | 637 |
| 644 /** | 638 /** |
| 645 * @param {string} propertyName | 639 * @param {string} propertyName |
| 646 * @return {!WebInspector.CSSMetadata} | 640 * @return {!Array<string>} |
| 647 */ | 641 */ |
| 648 WebInspector.CSSMetadata.keywordsForProperty = function(propertyName) | 642 WebInspector.CSSMetadata.propertyValues = function(propertyName) |
| 649 { | 643 { |
| 650 var acceptedKeywords = ["inherit", "initial"]; | 644 var acceptedKeywords = ["inherit", "initial"]; |
| 651 var descriptor = WebInspector.CSSMetadata.descriptor(propertyName); | 645 propertyName = propertyName.toLowerCase(); |
| 652 if (descriptor && descriptor.values) | 646 var unprefixedName = propertyName.replace(/^-webkit-/, ""); |
| 653 acceptedKeywords.push.apply(acceptedKeywords, descriptor.values); | 647 var entry = WebInspector.CSSMetadata._propertyDataMap[propertyName] || WebIn spector.CSSMetadata._propertyDataMap[unprefixedName]; |
| 648 if (entry && entry.values) | |
| 649 acceptedKeywords.pushAll(entry.values); | |
| 654 if (WebInspector.CSSMetadata.isColorAwareProperty(propertyName)) { | 650 if (WebInspector.CSSMetadata.isColorAwareProperty(propertyName)) { |
| 655 acceptedKeywords.push("currentColor"); | 651 acceptedKeywords.push("currentColor"); |
| 656 for (var color in WebInspector.Color.Nicknames) | 652 for (var color in WebInspector.Color.Nicknames) |
| 657 acceptedKeywords.push(color); | 653 acceptedKeywords.push(color); |
| 658 } | 654 } |
| 659 return new WebInspector.CSSMetadata(acceptedKeywords); | 655 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 } | 656 } |
| 677 | 657 |
| 678 WebInspector.CSSMetadata.initializeWithSupportedProperties = function(properties ) | 658 WebInspector.CSSMetadata.initializeWithSupportedProperties = function(properties ) |
| 679 { | 659 { |
| 680 WebInspector.CSSMetadata.cssPropertiesMetainfo = new WebInspector.CSSMetadat a(properties); | 660 WebInspector.CSSMetadata.cssPropertiesMetainfo = new WebInspector.CSSMetadat a(properties); |
| 681 } | 661 } |
| 682 | 662 |
| 683 // Weight of CSS properties based on their usage from https://www.chromestatus.c om/metrics/css/popularity | 663 // Weight of CSS properties based on their usage from https://www.chromestatus.c om/metrics/css/popularity |
| 684 WebInspector.CSSMetadata.Weight = { | 664 WebInspector.CSSMetadata.Weight = { |
| 685 "align-content": 57, | 665 "align-content": 57, |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 931 "width": 268, | 911 "width": 268, |
| 932 "will-change": 74, | 912 "will-change": 74, |
| 933 "word-break": 166, | 913 "word-break": 166, |
| 934 "word-spacing": 157, | 914 "word-spacing": 157, |
| 935 "word-wrap": 197, | 915 "word-wrap": 197, |
| 936 "writing-mode": 41, | 916 "writing-mode": 41, |
| 937 "z-index": 239, | 917 "z-index": 239, |
| 938 "zoom": 200 | 918 "zoom": 200 |
| 939 }; | 919 }; |
| 940 | 920 |
| 921 /** | |
| 922 * @param {!Array.<string>} properties | |
| 923 * @return {number} | |
| 924 */ | |
| 925 WebInspector.CSSMetadata.mostUsedProperty = function(properties) | |
| 926 { | |
| 927 var maxWeight = 0; | |
| 928 var index = 0; | |
| 929 for (var i = 0; i < properties.length; i++) { | |
| 930 var weight = WebInspector.CSSMetadata.Weight[properties[i]]; | |
| 931 if (!weight) | |
| 932 weight = WebInspector.CSSMetadata.Weight[WebInspector.CSSMetadata.ca nonicalPropertyName(properties[i])]; | |
| 933 if (weight > maxWeight) { | |
| 934 maxWeight = weight; | |
| 935 index = i; | |
| 936 } | |
| 937 } | |
| 938 return index; | |
| 939 } | |
| 941 | 940 |
| 942 WebInspector.CSSMetadata.prototype = { | 941 WebInspector.CSSMetadata.prototype = { |
| 943 /** | 942 /** |
| 944 * @param {string} prefix | 943 * @return {!Array<string>} |
| 945 * @return {!Array.<string>} | |
| 946 */ | 944 */ |
| 947 startsWith: function(prefix) | 945 allProperties: function() |
| 948 { | 946 { |
| 949 var firstIndex = this._firstIndexOfPrefix(prefix); | 947 return this._values; |
|
lushnikov
2016/08/05 07:16:33
this implementation of startsWith pretends to be f
| |
| 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 }, | 948 }, |
| 958 | 949 |
| 959 /** | 950 /** |
| 960 * @param {!Array.<string>} properties | 951 * @param {string} propertyName |
| 961 * @return {number} | 952 * @return {boolean} |
| 962 */ | 953 */ |
| 963 mostUsedOf: function(properties) | 954 hasProperty: function(propertyName) |
|
lushnikov
2016/08/05 07:16:33
renamed into WI.CSSMetadata.mostUserProperty
| |
| 964 { | 955 { |
| 965 var maxWeight = 0; | 956 if (!this._valuesSet) |
| 966 var index = 0; | 957 this._valuesSet = new Set(this._values); |
| 967 for (var i = 0; i < properties.length; i++) { | 958 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 }, | 959 }, |
| 1010 | 960 |
| 1011 /** | 961 /** |
| 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) | |
|
lushnikov
2016/08/05 07:16:33
next, previoius, _closest: dead code
| |
| 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 | 962 * @param {string} shorthand |
| 1069 * @return {?Array.<string>} | 963 * @return {?Array.<string>} |
| 1070 */ | 964 */ |
| 1071 longhands: function(shorthand) | 965 longhands: function(shorthand) |
|
dgozman
2016/08/05 16:38:21
Let's follow up with lazy cssMetadata() and all in
lushnikov
2016/08/05 17:12:01
Sounds good
| |
| 1072 { | 966 { |
| 1073 return this._longhands[shorthand]; | 967 return this._longhands[shorthand]; |
| 1074 }, | 968 }, |
| 1075 | 969 |
| 1076 /** | 970 /** |
| 1077 * @param {string} longhand | 971 * @param {string} longhand |
| 1078 * @return {?Array.<string>} | 972 * @return {?Array.<string>} |
| 1079 */ | 973 */ |
| 1080 shorthands: function(longhand) | 974 shorthands: function(longhand) |
| 1081 { | 975 { |
| 1082 return this._shorthands[longhand]; | 976 return this._shorthands[longhand]; |
| 1083 } | 977 } |
| 1084 } | 978 } |
| 1085 | 979 |
| 1086 WebInspector.CSSMetadata.initializeWithSupportedProperties([]); | 980 WebInspector.CSSMetadata.initializeWithSupportedProperties([]); |
| OLD | NEW |