blob: a9634ddb9f4e878de10d86eb53be8a1ba36a9f57 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371// Copyright (c) 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Tim van der Lippe97611c92020-02-12 16:56:584
Jan Scheffler29905852021-04-14 11:30:315/* eslint-disable rulesdir/no_underscored_properties */
6
Tim van der Lippe76961572021-04-06 10:48:077import * as Common from '../../core/common/common.js';
Tim van der Lippebb352e62021-04-01 17:57:288import * as i18n from '../../core/i18n/i18n.js';
Tim van der Lippeaa1ed7a2021-03-31 14:38:279import * as Platform from '../../core/platform/platform.js';
Tim van der Lippee00b92f2021-03-31 16:52:1710import * as SDK from '../../core/sdk/sdk.js';
Tim van der Lippeaa61faf2021-04-07 15:32:0711import * as UI from '../../ui/legacy/legacy.js';
Tim van der Lippe97611c92020-02-12 16:56:5812
Tim van der Lippeaabc8302019-12-10 15:34:4513import {ElementsPanel} from './ElementsPanel.js';
14
Simon Zündfbfd1072021-03-01 07:38:5315const UIStrings = {
Vidal Guillermo Diazleal Ortega0cfa0ed2021-02-17 20:35:4916 /**
Peter Marshallcc995412021-02-19 08:07:4317 * @description Prompt text for a text field in the Classes Pane Widget of the Elements panel.
18 * Class refers to a CSS class.
Vidal Guillermo Diazleal Ortega0cfa0ed2021-02-17 20:35:4919 */
20 addNewClass: 'Add new class',
21 /**
Peter Marshallcc995412021-02-19 08:07:4322 * @description Screen reader announcement string when adding a CSS class via the Classes Pane Widget.
23 * @example {vbox flex-auto} PH1
Vidal Guillermo Diazleal Ortega0cfa0ed2021-02-17 20:35:4924 */
Peter Marshallcc995412021-02-19 08:07:4325 classesSAdded: 'Classes {PH1} added',
Vidal Guillermo Diazleal Ortega0cfa0ed2021-02-17 20:35:4926 /**
Peter Marshallcc995412021-02-19 08:07:4327 * @description Screen reader announcement string when adding a class via the Classes Pane Widget.
28 * @example {title-container} PH1
Vidal Guillermo Diazleal Ortega0cfa0ed2021-02-17 20:35:4929 */
Peter Marshallcc995412021-02-19 08:07:4330 classSAdded: 'Class {PH1} added',
Vidal Guillermo Diazleal Ortega0cfa0ed2021-02-17 20:35:4931 /**
Peter Marshallcc995412021-02-19 08:07:4332 * @description Accessible title read by screen readers for the Classes Pane Widget of the Elements
33 * panel. Element is a HTML DOM Element and classes refers to CSS classes.
Vidal Guillermo Diazleal Ortega0cfa0ed2021-02-17 20:35:4934 */
35 elementClasses: 'Element Classes',
36};
Jan Scheffler29905852021-04-14 11:30:3137const str_ = i18n.i18n.registerUIStrings('panels/elements/ClassesPaneWidget.ts', UIStrings);
Vidal Guillermo Diazleal Ortega0cfa0ed2021-02-17 20:35:4938const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);
Tim van der Lippe97611c92020-02-12 16:56:5839export class ClassesPaneWidget extends UI.Widget.Widget {
Jan Scheffler29905852021-04-14 11:30:3140 _input: HTMLElement;
41 _classesContainer: HTMLElement;
42 _prompt: ClassNamePrompt;
43 _mutatingNodes: Set<SDK.DOMModel.DOMNode>;
44 _pendingNodeClasses: Map<SDK.DOMModel.DOMNode, string>;
45 _updateNodeThrottler: Common.Throttler.Throttler;
46 _previousTarget: SDK.DOMModel.DOMNode|null;
47
Blink Reformat4c46d092018-04-07 15:32:3748 constructor() {
49 super(true);
Jack Franklin84442c62021-06-28 15:02:0650 this.registerRequiredCSS('panels/elements/classesPaneWidget.css');
Blink Reformat4c46d092018-04-07 15:32:3751 this.contentElement.className = 'styles-element-classes-pane';
52 const container = this.contentElement.createChild('div', 'title-container');
53 this._input = container.createChild('div', 'new-class-input monospace');
54 this.setDefaultFocusedElement(this._input);
55 this._classesContainer = this.contentElement.createChild('div', 'source-code');
56 this._classesContainer.classList.add('styles-element-classes-container');
Tim van der Lippe13f71fb2019-11-29 11:17:3957 this._prompt = new ClassNamePrompt(this._nodeClasses.bind(this));
Blink Reformat4c46d092018-04-07 15:32:3758 this._prompt.setAutocompletionTimeout(0);
59 this._prompt.renderAsBlock();
60
Jan Scheffler29905852021-04-14 11:30:3161 const proxyElement = (this._prompt.attach(this._input) as HTMLElement);
Vidal Guillermo Diazleal Ortega0cfa0ed2021-02-17 20:35:4962 this._prompt.setPlaceholder(i18nString(UIStrings.addNewClass));
Blink Reformat4c46d092018-04-07 15:32:3763 this._prompt.addEventListener(UI.TextPrompt.Events.TextChanged, this._onTextChanged, this);
64 proxyElement.addEventListener('keydown', this._onKeyDown.bind(this), false);
65
Sigurd Schneiderb9f6c792021-05-31 10:57:2466 SDK.TargetManager.TargetManager.instance().addModelListener(
Tim van der Lippe97611c92020-02-12 16:56:5867 SDK.DOMModel.DOMModel, SDK.DOMModel.Events.DOMMutated, this._onDOMMutated, this);
Blink Reformat4c46d092018-04-07 15:32:3768 this._mutatingNodes = new Set();
Blink Reformat4c46d092018-04-07 15:32:3769 this._pendingNodeClasses = new Map();
Tim van der Lippe97611c92020-02-12 16:56:5870 this._updateNodeThrottler = new Common.Throttler.Throttler(0);
Blink Reformat4c46d092018-04-07 15:32:3771 this._previousTarget = null;
Tim van der Lipped1a00aa2020-08-19 16:03:5672 UI.Context.Context.instance().addFlavorChangeListener(SDK.DOMModel.DOMNode, this._onSelectedNodeChanged, this);
Blink Reformat4c46d092018-04-07 15:32:3773 }
74
Jan Scheffler29905852021-04-14 11:30:3175 _splitTextIntoClasses(text: string): string[] {
Mathias Bynens3abc0952020-04-20 14:15:5276 return text.split(/[,\s]/).map(className => className.trim()).filter(className => className.length);
Blink Reformat4c46d092018-04-07 15:32:3777 }
78
Jan Scheffler29905852021-04-14 11:30:3179 _onKeyDown(event: KeyboardEvent): void {
Tim van der Lippebcd6b5c2021-01-13 12:31:5180 if (!(event.key === 'Enter') && !isEscKey(event)) {
Blink Reformat4c46d092018-04-07 15:32:3781 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:3482 }
Blink Reformat4c46d092018-04-07 15:32:3783
Tim van der Lippebcd6b5c2021-01-13 12:31:5184 if (event.key === 'Enter') {
Blink Reformat4c46d092018-04-07 15:32:3785 event.consume();
Tim van der Lippe1d6e57a2019-09-30 11:55:3486 if (this._prompt.acceptAutoComplete()) {
Blink Reformat4c46d092018-04-07 15:32:3787 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:3488 }
Blink Reformat4c46d092018-04-07 15:32:3789 }
90
Jan Scheffler29905852021-04-14 11:30:3191 const eventTarget = (event.target as HTMLElement);
92 let text: ''|string = (eventTarget.textContent as string);
Blink Reformat4c46d092018-04-07 15:32:3793 if (isEscKey(event)) {
Simon Zündda7058f2020-02-28 13:57:2894 if (!Platform.StringUtilities.isWhitespace(text)) {
Blink Reformat4c46d092018-04-07 15:32:3795 event.consume(true);
Tim van der Lippe1d6e57a2019-09-30 11:55:3496 }
Blink Reformat4c46d092018-04-07 15:32:3797 text = '';
98 }
99
100 this._prompt.clearAutocomplete();
Tim van der Lippe32f760f2020-10-01 10:52:15101 eventTarget.textContent = '';
Blink Reformat4c46d092018-04-07 15:32:37102
Tim van der Lipped1a00aa2020-08-19 16:03:56103 const node = UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode);
Tim van der Lippe1d6e57a2019-09-30 11:55:34104 if (!node) {
Blink Reformat4c46d092018-04-07 15:32:37105 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34106 }
Blink Reformat4c46d092018-04-07 15:32:37107
108 const classNames = this._splitTextIntoClasses(text);
Michael Liaocafccfd2020-04-02 17:11:59109 if (!classNames.length) {
Patrick Brosset78fa1f52020-08-17 14:33:48110 this._installNodeClasses(node);
Michael Liaocafccfd2020-04-02 17:11:59111 return;
112 }
113
Tim van der Lippe1d6e57a2019-09-30 11:55:34114 for (const className of classNames) {
Blink Reformat4c46d092018-04-07 15:32:37115 this._toggleClass(node, className, true);
Tim van der Lippe1d6e57a2019-09-30 11:55:34116 }
Michael Liaocafccfd2020-04-02 17:11:59117
118 // annoucementString is used for screen reader to announce that the class(es) has been added successfully.
119 const joinClassString = classNames.join(' ');
Vidal Guillermo Diazleal Ortega0cfa0ed2021-02-17 20:35:49120 const announcementString = classNames.length > 1 ? i18nString(UIStrings.classesSAdded, {PH1: joinClassString}) :
121 i18nString(UIStrings.classSAdded, {PH1: joinClassString});
Michael Liao7322dee2021-04-07 18:33:30122 UI.ARIAUtils.alert(announcementString);
Michael Liaocafccfd2020-04-02 17:11:59123
Blink Reformat4c46d092018-04-07 15:32:37124 this._installNodeClasses(node);
125 this._update();
126 }
127
Jan Scheffler29905852021-04-14 11:30:31128 _onTextChanged(): void {
Tim van der Lipped1a00aa2020-08-19 16:03:56129 const node = UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode);
Tim van der Lippe1d6e57a2019-09-30 11:55:34130 if (!node) {
Blink Reformat4c46d092018-04-07 15:32:37131 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34132 }
Blink Reformat4c46d092018-04-07 15:32:37133 this._installNodeClasses(node);
134 }
135
Jan Scheffler29905852021-04-14 11:30:31136 _onDOMMutated(event: Common.EventTarget.EventTargetEvent): void {
137 const node = (event.data as SDK.DOMModel.DOMNode);
Tim van der Lippe1d6e57a2019-09-30 11:55:34138 if (this._mutatingNodes.has(node)) {
Blink Reformat4c46d092018-04-07 15:32:37139 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34140 }
Tim van der Lippe32f760f2020-10-01 10:52:15141 cachedClassesMap.delete(node);
Blink Reformat4c46d092018-04-07 15:32:37142 this._update();
143 }
144
Jan Scheffler29905852021-04-14 11:30:31145 _onSelectedNodeChanged(event: Common.EventTarget.EventTargetEvent): void {
Blink Reformat4c46d092018-04-07 15:32:37146 if (this._previousTarget && this._prompt.text()) {
147 this._input.textContent = '';
148 this._installNodeClasses(this._previousTarget);
149 }
Jan Scheffler29905852021-04-14 11:30:31150 this._previousTarget = (event.data as SDK.DOMModel.DOMNode | null);
Blink Reformat4c46d092018-04-07 15:32:37151 this._update();
152 }
153
Jan Scheffler29905852021-04-14 11:30:31154 wasShown(): void {
Kriti Saprab2b29f22021-06-29 12:59:56155 super.wasShown();
Blink Reformat4c46d092018-04-07 15:32:37156 this._update();
157 }
158
Jan Scheffler29905852021-04-14 11:30:31159 _update(): void {
Tim van der Lippe1d6e57a2019-09-30 11:55:34160 if (!this.isShowing()) {
Blink Reformat4c46d092018-04-07 15:32:37161 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34162 }
Blink Reformat4c46d092018-04-07 15:32:37163
Tim van der Lipped1a00aa2020-08-19 16:03:56164 let node = UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode);
Tim van der Lippe1d6e57a2019-09-30 11:55:34165 if (node) {
Blink Reformat4c46d092018-04-07 15:32:37166 node = node.enclosingElementOrSelf();
Tim van der Lippe1d6e57a2019-09-30 11:55:34167 }
Blink Reformat4c46d092018-04-07 15:32:37168
169 this._classesContainer.removeChildren();
Tim van der Lippe32f760f2020-10-01 10:52:15170 // @ts-ignore this._input is a div, not an input element. So this line makes no sense at all
Blink Reformat4c46d092018-04-07 15:32:37171 this._input.disabled = !node;
172
Tim van der Lippe1d6e57a2019-09-30 11:55:34173 if (!node) {
Blink Reformat4c46d092018-04-07 15:32:37174 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34175 }
Blink Reformat4c46d092018-04-07 15:32:37176
177 const classes = this._nodeClasses(node);
Simon Zündf27be3d2020-02-11 14:46:27178 const keys = [...classes.keys()];
Tim van der Lippe32f760f2020-10-01 10:52:15179 keys.sort(Platform.StringUtilities.caseInsensetiveComparator);
180 for (const className of keys) {
Tim van der Lippe97611c92020-02-12 16:56:58181 const label = UI.UIUtils.CheckboxLabel.create(className, classes.get(className));
Blink Reformat4c46d092018-04-07 15:32:37182 label.classList.add('monospace');
183 label.checkboxElement.addEventListener('click', this._onClick.bind(this, className), false);
184 this._classesContainer.appendChild(label);
185 }
186 }
187
Jan Scheffler29905852021-04-14 11:30:31188 _onClick(className: string, event: Event): void {
Tim van der Lipped1a00aa2020-08-19 16:03:56189 const node = UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode);
Tim van der Lippe1d6e57a2019-09-30 11:55:34190 if (!node) {
Blink Reformat4c46d092018-04-07 15:32:37191 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34192 }
Jan Scheffler29905852021-04-14 11:30:31193 const enabled = (event.target as HTMLInputElement).checked;
Blink Reformat4c46d092018-04-07 15:32:37194 this._toggleClass(node, className, enabled);
195 this._installNodeClasses(node);
196 }
197
Jan Scheffler29905852021-04-14 11:30:31198 _nodeClasses(node: SDK.DOMModel.DOMNode): Map<string, boolean> {
Tim van der Lippe32f760f2020-10-01 10:52:15199 let result = cachedClassesMap.get(node);
Blink Reformat4c46d092018-04-07 15:32:37200 if (!result) {
201 const classAttribute = node.getAttribute('class') || '';
202 const classes = classAttribute.split(/\s/);
203 result = new Map();
204 for (let i = 0; i < classes.length; ++i) {
205 const className = classes[i].trim();
Tim van der Lippe1d6e57a2019-09-30 11:55:34206 if (!className.length) {
Blink Reformat4c46d092018-04-07 15:32:37207 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34208 }
Blink Reformat4c46d092018-04-07 15:32:37209 result.set(className, true);
210 }
Tim van der Lippe32f760f2020-10-01 10:52:15211 cachedClassesMap.set(node, result);
Blink Reformat4c46d092018-04-07 15:32:37212 }
213 return result;
214 }
215
Jan Scheffler29905852021-04-14 11:30:31216 _toggleClass(node: SDK.DOMModel.DOMNode, className: string, enabled: boolean): void {
Blink Reformat4c46d092018-04-07 15:32:37217 const classes = this._nodeClasses(node);
218 classes.set(className, enabled);
219 }
220
Jan Scheffler29905852021-04-14 11:30:31221 _installNodeClasses(node: SDK.DOMModel.DOMNode): void {
Blink Reformat4c46d092018-04-07 15:32:37222 const classes = this._nodeClasses(node);
Jan Scheffler29905852021-04-14 11:30:31223 const activeClasses = new Set<string>();
Blink Reformat4c46d092018-04-07 15:32:37224 for (const className of classes.keys()) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34225 if (classes.get(className)) {
Blink Reformat4c46d092018-04-07 15:32:37226 activeClasses.add(className);
Tim van der Lippe1d6e57a2019-09-30 11:55:34227 }
Blink Reformat4c46d092018-04-07 15:32:37228 }
229
230 const additionalClasses = this._splitTextIntoClasses(this._prompt.textWithCurrentSuggestion());
Tim van der Lippe1d6e57a2019-09-30 11:55:34231 for (const className of additionalClasses) {
Blink Reformat4c46d092018-04-07 15:32:37232 activeClasses.add(className);
Tim van der Lippe1d6e57a2019-09-30 11:55:34233 }
Blink Reformat4c46d092018-04-07 15:32:37234
Simon Zünda0d40622020-02-12 13:16:42235 const newClasses = [...activeClasses.values()].sort();
Blink Reformat4c46d092018-04-07 15:32:37236
237 this._pendingNodeClasses.set(node, newClasses.join(' '));
238 this._updateNodeThrottler.schedule(this._flushPendingClasses.bind(this));
239 }
240
Jan Scheffler29905852021-04-14 11:30:31241 async _flushPendingClasses(): Promise<void> {
Blink Reformat4c46d092018-04-07 15:32:37242 const promises = [];
243 for (const node of this._pendingNodeClasses.keys()) {
244 this._mutatingNodes.add(node);
Jan Scheffler29905852021-04-14 11:30:31245 const promise = node.setAttributeValuePromise('class', (this._pendingNodeClasses.get(node) as string))
Blink Reformat4c46d092018-04-07 15:32:37246 .then(onClassValueUpdated.bind(this, node));
247 promises.push(promise);
248 }
249 this._pendingNodeClasses.clear();
Tim van der Lippe32f760f2020-10-01 10:52:15250 await Promise.all(promises);
Blink Reformat4c46d092018-04-07 15:32:37251
Jan Scheffler29905852021-04-14 11:30:31252 function onClassValueUpdated(this: ClassesPaneWidget, node: SDK.DOMModel.DOMNode): void {
Blink Reformat4c46d092018-04-07 15:32:37253 this._mutatingNodes.delete(node);
254 }
255 }
Tim van der Lippe13f71fb2019-11-29 11:17:39256}
Blink Reformat4c46d092018-04-07 15:32:37257
Jan Scheffler29905852021-04-14 11:30:31258const cachedClassesMap = new WeakMap<SDK.DOMModel.DOMNode, Map<string, boolean>>();
Blink Reformat4c46d092018-04-07 15:32:37259
Jan Scheffler29905852021-04-14 11:30:31260let buttonProviderInstance: ButtonProvider;
Andres Olivares43c2d9f2021-02-10 16:48:39261
Jan Scheffler29905852021-04-14 11:30:31262export class ButtonProvider implements UI.Toolbar.Provider {
263 _button: UI.Toolbar.ToolbarToggle;
264 _view: ClassesPaneWidget;
265 private constructor() {
Vidal Guillermo Diazleal Ortega0cfa0ed2021-02-17 20:35:49266 this._button = new UI.Toolbar.ToolbarToggle(i18nString(UIStrings.elementClasses), '');
Blink Reformat4c46d092018-04-07 15:32:37267 this._button.setText('.cls');
268 this._button.element.classList.add('monospace');
Tim van der Lippe97611c92020-02-12 16:56:58269 this._button.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, this._clicked, this);
Tim van der Lippe13f71fb2019-11-29 11:17:39270 this._view = new ClassesPaneWidget();
Blink Reformat4c46d092018-04-07 15:32:37271 }
272
Jan Scheffler29905852021-04-14 11:30:31273 static instance(opts: {
274 forceNew: boolean|null,
275 } = {forceNew: null}): ButtonProvider {
Andres Olivares43c2d9f2021-02-10 16:48:39276 const {forceNew} = opts;
277 if (!buttonProviderInstance || forceNew) {
278 buttonProviderInstance = new ButtonProvider();
279 }
280
281 return buttonProviderInstance;
282 }
283
Jan Scheffler29905852021-04-14 11:30:31284 _clicked(): void {
Tim van der Lippeaabc8302019-12-10 15:34:45285 ElementsPanel.instance().showToolbarPane(!this._view.isShowing() ? this._view : null, this._button);
Blink Reformat4c46d092018-04-07 15:32:37286 }
287
Jan Scheffler29905852021-04-14 11:30:31288 item(): UI.Toolbar.ToolbarItem {
Blink Reformat4c46d092018-04-07 15:32:37289 return this._button;
290 }
Tim van der Lippe13f71fb2019-11-29 11:17:39291}
Blink Reformat4c46d092018-04-07 15:32:37292
Tim van der Lippe97611c92020-02-12 16:56:58293export class ClassNamePrompt extends UI.TextPrompt.TextPrompt {
Jan Scheffler29905852021-04-14 11:30:31294 _nodeClasses: (arg0: SDK.DOMModel.DOMNode) => Map<string, boolean>;
295 _selectedFrameId: string|null;
296 _classNamesPromise: Promise<string[]>|null;
297 constructor(nodeClasses: (arg0: SDK.DOMModel.DOMNode) => Map<string, boolean>) {
Blink Reformat4c46d092018-04-07 15:32:37298 super();
299 this._nodeClasses = nodeClasses;
300 this.initialize(this._buildClassNameCompletions.bind(this), ' ');
301 this.disableDefaultSuggestionForEmptyInput();
302 this._selectedFrameId = '';
303 this._classNamesPromise = null;
304 }
305
Jan Scheffler29905852021-04-14 11:30:31306 async _getClassNames(selectedNode: SDK.DOMModel.DOMNode): Promise<string[]> {
Blink Reformat4c46d092018-04-07 15:32:37307 const promises = [];
Jan Scheffler29905852021-04-14 11:30:31308 const completions = new Set<string>();
Blink Reformat4c46d092018-04-07 15:32:37309 this._selectedFrameId = selectedNode.frameId();
310
311 const cssModel = selectedNode.domModel().cssModel();
312 const allStyleSheets = cssModel.allStyleSheets();
313 for (const stylesheet of allStyleSheets) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34314 if (stylesheet.frameId !== this._selectedFrameId) {
Blink Reformat4c46d092018-04-07 15:32:37315 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34316 }
Mathias Bynens3abc0952020-04-20 14:15:52317 const cssPromise = cssModel.classNamesPromise(stylesheet.id).then(classes => {
318 for (const className of classes) {
319 completions.add(className);
320 }
321 });
Blink Reformat4c46d092018-04-07 15:32:37322 promises.push(cssPromise);
323 }
324
Jan Scheffler29905852021-04-14 11:30:31325 const ownerDocumentId = ((selectedNode.ownerDocument as SDK.DOMModel.DOMDocument).id);
Tim van der Lippe32f760f2020-10-01 10:52:15326
327 const domPromise = selectedNode.domModel().classNamesPromise(ownerDocumentId).then(classes => {
Mathias Bynens3abc0952020-04-20 14:15:52328 for (const className of classes) {
329 completions.add(className);
330 }
331 });
Blink Reformat4c46d092018-04-07 15:32:37332 promises.push(domPromise);
Mathias Bynens3abc0952020-04-20 14:15:52333 await Promise.all(promises);
334 return [...completions];
Blink Reformat4c46d092018-04-07 15:32:37335 }
336
Jan Scheffler29905852021-04-14 11:30:31337 async _buildClassNameCompletions(expression: string, prefix: string, force?: boolean):
338 Promise<UI.SuggestBox.Suggestions> {
Tim van der Lippe1d6e57a2019-09-30 11:55:34339 if (!prefix || force) {
Blink Reformat4c46d092018-04-07 15:32:37340 this._classNamesPromise = null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34341 }
Blink Reformat4c46d092018-04-07 15:32:37342
Tim van der Lipped1a00aa2020-08-19 16:03:56343 const selectedNode = UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode);
Tim van der Lippe1d6e57a2019-09-30 11:55:34344 if (!selectedNode || (!prefix && !force && !expression.trim())) {
Mathias Bynens41ea2632020-12-24 05:52:49345 return [];
Tim van der Lippe1d6e57a2019-09-30 11:55:34346 }
Blink Reformat4c46d092018-04-07 15:32:37347
Tim van der Lippe1d6e57a2019-09-30 11:55:34348 if (!this._classNamesPromise || this._selectedFrameId !== selectedNode.frameId()) {
Blink Reformat4c46d092018-04-07 15:32:37349 this._classNamesPromise = this._getClassNames(selectedNode);
Tim van der Lippe1d6e57a2019-09-30 11:55:34350 }
Blink Reformat4c46d092018-04-07 15:32:37351
Jan Scheffler29905852021-04-14 11:30:31352 let completions: string[] = await this._classNamesPromise;
353 const classesMap = this._nodeClasses((selectedNode as SDK.DOMModel.DOMNode));
Mathias Bynens41ea2632020-12-24 05:52:49354 completions = completions.filter(value => !classesMap.get(value));
Blink Reformat4c46d092018-04-07 15:32:37355
Mathias Bynens41ea2632020-12-24 05:52:49356 if (prefix[0] === '.') {
357 completions = completions.map(value => '.' + value);
358 }
359 return completions.filter(value => value.startsWith(prefix)).sort().map(completion => {
360 return {
361 text: completion,
362 title: undefined,
363 subtitle: undefined,
364 iconType: undefined,
365 priority: undefined,
366 isSecondary: undefined,
367 subtitleRenderer: undefined,
368 selectionRange: undefined,
369 hideGhostText: undefined,
370 iconElement: undefined,
371 };
Blink Reformat4c46d092018-04-07 15:32:37372 });
373 }
Tim van der Lippe13f71fb2019-11-29 11:17:39374}