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

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/elements/StylesSidebarPane.js

Issue 2637593002: DevTools: Autocomplete CSS variables in StylesSidebar (Closed)
Patch Set: Created 3 years, 11 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
« no previous file with comments | « third_party/WebKit/LayoutTests/inspector/elements/styles-3/style-autocomplete-expected.txt ('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) 2007 Apple Inc. All rights reserved. 2 * Copyright (C) 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Joseph Pecoraro 3 * Copyright (C) 2009 Joseph Pecoraro
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 2750 matching lines...) Expand 10 before | Expand all | Expand 10 after
2761 /** 2761 /**
2762 * @param {!Array<string>} cssCompletions 2762 * @param {!Array<string>} cssCompletions
2763 * @param {!Elements.StylePropertyTreeElement} treeElement 2763 * @param {!Elements.StylePropertyTreeElement} treeElement
2764 * @param {boolean} isEditingName 2764 * @param {boolean} isEditingName
2765 */ 2765 */
2766 constructor(cssCompletions, treeElement, isEditingName) { 2766 constructor(cssCompletions, treeElement, isEditingName) {
2767 // Use the same callback both for applyItemCallback and acceptItemCallback. 2767 // Use the same callback both for applyItemCallback and acceptItemCallback.
2768 super(); 2768 super();
2769 this.initialize(this._buildPropertyCompletions.bind(this), UI.StyleValueDeli miters); 2769 this.initialize(this._buildPropertyCompletions.bind(this), UI.StyleValueDeli miters);
2770 this._cssCompletions = cssCompletions; 2770 this._cssCompletions = cssCompletions;
2771 this._cssVariables = Elements.StylesSidebarPane.findCSSVariables();
2771 this._treeElement = treeElement; 2772 this._treeElement = treeElement;
2772 this._isEditingName = isEditingName; 2773 this._isEditingName = isEditingName;
2773 2774
2774 if (!isEditingName) { 2775 if (!isEditingName) {
2775 this.disableDefaultSuggestionForEmptyInput(); 2776 this.disableDefaultSuggestionForEmptyInput();
2776 2777
2777 // If a CSS value is being edited that has a numeric or hex substring, hin t that precision modifier shortcuts are available. 2778 // If a CSS value is being edited that has a numeric or hex substring, hin t that precision modifier shortcuts are available.
2778 if (treeElement && treeElement.valueElement) { 2779 if (treeElement && treeElement.valueElement) {
2779 var cssValueText = treeElement.valueElement.textContent; 2780 var cssValueText = treeElement.valueElement.textContent;
2780 if (cssValueText.match(/#[\da-f]{3,6}$/i)) { 2781 if (cssValueText.match(/#[\da-f]{3,6}$/i)) {
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
2880 } 2881 }
2881 2882
2882 /** 2883 /**
2883 * @param {string} word 2884 * @param {string} word
2884 * @return {boolean} 2885 * @return {boolean}
2885 */ 2886 */
2886 _isValueSuggestion(word) { 2887 _isValueSuggestion(word) {
2887 if (!word) 2888 if (!word)
2888 return false; 2889 return false;
2889 word = word.toLowerCase(); 2890 word = word.toLowerCase();
2890 return this._cssCompletions.indexOf(word) !== -1; 2891 return this._cssCompletions.indexOf(word) !== -1 || word.startsWith('--');
2891 } 2892 }
2892 2893
2893 /** 2894 /**
2894 * @param {string} expression 2895 * @param {string} expression
2895 * @param {string} query 2896 * @param {string} query
2896 * @param {boolean=} force 2897 * @param {boolean=} force
2897 * @return {!Promise<!UI.SuggestBox.Suggestions>} 2898 * @return {!Promise<!UI.SuggestBox.Suggestions>}
2898 */ 2899 */
2899 _buildPropertyCompletions(expression, query, force) { 2900 _buildPropertyCompletions(expression, query, force) {
2900 var lowerQuery = query.toLowerCase(); 2901 var lowerQuery = query.toLowerCase();
2901 if (!query && !force && (this._isEditingName || expression)) 2902 var editingName = this._isEditingName;
2903 var editingVariable = expression.trim().endsWith('var(');
2904 if (!query && !force && !editingVariable && (editingName || expression))
2902 return Promise.resolve([]); 2905 return Promise.resolve([]);
2903 2906
2904 var prefixResults = []; 2907 var prefixResults = [];
2905 var anywhereResults = []; 2908 var anywhereResults = [];
2906 this._cssCompletions.forEach(filterCompletions.bind(this)); 2909 if (!editingVariable)
2907 var results = prefixResults.concat(anywhereResults); 2910 this._cssCompletions.forEach(filterCompletions);
2908 2911
2909 if (!this._isEditingName && !results.length && query.length > 1 && '!importa nt'.startsWith(lowerQuery)) 2912 if (editingName || editingVariable) {
2910 results.push({title: '!important'}); 2913 return this._cssVariables.then(variables => {
2911 var userEnteredText = query.replace('-', ''); 2914 variables.forEach(filterCompletions);
2912 if (userEnteredText && (userEnteredText === userEnteredText.toUpperCase())) { 2915 return returnResults();
2913 for (var i = 0; i < results.length; ++i) 2916 });
2914 results[i].title = results[i].title.toUpperCase();
2915 } 2917 }
2916 return Promise.resolve(results); 2918 return Promise.resolve(returnResults());
2917 2919
2918 /** 2920 /**
2919 * @param {string} completion 2921 * @param {string} completion
2920 * @this {Elements.StylesSidebarPane.CSSPropertyPrompt}
2921 */ 2922 */
2922 function filterCompletions(completion) { 2923 function filterCompletions(completion) {
2923 var index = completion.indexOf(lowerQuery); 2924 var index = completion.toLowerCase().indexOf(lowerQuery);
2924 if (index === 0) { 2925 if (index === 0) {
2925 var priority = this._isEditingName ? SDK.cssMetadata().propertyUsageWeig ht(completion) : 1; 2926 var priority = editingName ? SDK.cssMetadata().propertyUsageWeight(compl etion) : 1;
2926 prefixResults.push({title: completion, priority: priority}); 2927 prefixResults.push({title: completion, priority: priority});
2927 } else if (index > -1) { 2928 } else if (index > -1) {
2928 anywhereResults.push({title: completion}); 2929 anywhereResults.push({title: completion});
2929 } 2930 }
2930 } 2931 }
2932
2933 /**
2934 * @return {!UI.SuggestBox.Suggestions}
2935 */
2936 function returnResults() {
2937 var results = prefixResults.concat(anywhereResults);
2938 if (!editingName && !results.length && query.length > 1 && '!important'.st artsWith(lowerQuery))
2939 results.push({title: '!important'});
2940 var userEnteredText = query.replace('-', '');
2941 if (userEnteredText && (userEnteredText === userEnteredText.toUpperCase()) ) {
2942 for (var i = 0; i < results.length; ++i) {
2943 if (!results[i].title.startsWith('--'))
2944 results[i].title = results[i].title.toUpperCase();
2945 }
2946 }
2947 if (editingVariable)
2948 results.forEach(result => result.title += ')');
2949 return results;
2950 }
2931 } 2951 }
2932 }; 2952 };
2933 2953
2934 /** 2954 /**
2935 * @unrestricted 2955 * @unrestricted
2936 */ 2956 */
2937 Elements.StylesSidebarPropertyRenderer = class { 2957 Elements.StylesSidebarPropertyRenderer = class {
2938 /** 2958 /**
2939 * @param {?SDK.CSSRule} rule 2959 * @param {?SDK.CSSRule} rule
2940 * @param {?SDK.DOMNode} node 2960 * @param {?SDK.DOMNode} node
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
3080 } 3100 }
3081 3101
3082 /** 3102 /**
3083 * @override 3103 * @override
3084 * @return {!UI.ToolbarItem} 3104 * @return {!UI.ToolbarItem}
3085 */ 3105 */
3086 item() { 3106 item() {
3087 return this._button; 3107 return this._button;
3088 } 3108 }
3089 }; 3109 };
3110
3111 /**
3112 * @return {!Promise<!Array<string>>}
3113 */
3114 Elements.StylesSidebarPane.findCSSVariables = function() {
lushnikov 2017/01/23 05:06:47 fetching & parsing everything is a lot of work. ca
3115 var stylesheetPromises = [];
3116 /** @type {!Set<string>} */
3117 var variables = new Set();
3118 for (var target of SDK.targetManager.targets(SDK.Target.Capability.DOM)) {
3119 var cssModel = SDK.CSSModel.fromTarget(target);
3120 for (var stylesheet of cssModel.allStyleSheets())
3121 stylesheetPromises.push(stylesheet.requestContent().then(parseContent));
3122 }
3123 return Promise.all(stylesheetPromises).then(() => Array.from(variables));
3124
3125 /**
3126 * @param {?string} text
3127 */
3128 function parseContent(text) {
3129 if (!text)
3130 return;
3131 var regexResult = text.match(/[^A-Za-z0-9\-]--[A-Za-z0-9\-]+/g) || [];
dgozman 2017/01/18 16:29:38 I see we now show defined variables in SSP unless
3132 for (var v of regexResult)
3133 variables.add(v.substring(1));
3134 }
3135 };
OLDNEW
« no previous file with comments | « third_party/WebKit/LayoutTests/inspector/elements/styles-3/style-autocomplete-expected.txt ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698