Web Inspector: Add Changes panel to Elements tab
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / SpreadsheetStyleProperty.js
1 /*
2  * Copyright (C) 2017 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 WI.SpreadsheetStyleProperty = class SpreadsheetStyleProperty extends WI.Object
27 {
28     constructor(delegate, property, options = {})
29     {
30         super();
31
32         console.assert(property instanceof WI.CSSProperty);
33
34         this._delegate = delegate || null;
35         this._property = property;
36         this._readOnly = options.readOnly || false;
37         this._element = document.createElement("div");
38
39         this._contentElement = null;
40         this._nameElement = null;
41         this._valueElement = null;
42
43         this._nameTextField = null;
44         this._valueTextField = null;
45
46         this._selected = false;
47         this._hasInvalidVariableValue = false;
48
49         this.update();
50         property.addEventListener(WI.CSSProperty.Event.OverriddenStatusChanged, this.updateStatus, this);
51         property.addEventListener(WI.CSSProperty.Event.Changed, this.updateStatus, this);
52
53         if (!this._readOnly) {
54             this._element.tabIndex = -1;
55
56             this._element.addEventListener("blur", (event) => {
57                 // Keep selection after tabbing out of Web Inspector window and back.
58                 if (document.activeElement === this._element)
59                     return;
60
61                 if (this._delegate.spreadsheetStylePropertyBlur)
62                     this._delegate.spreadsheetStylePropertyBlur(event, this);
63             });
64
65             this._element.addEventListener("mouseenter", (event) => {
66                 if (this._delegate.spreadsheetStylePropertyMouseEnter)
67                     this._delegate.spreadsheetStylePropertyMouseEnter(event, this);
68             });
69
70             this._element.copyHandler = this;
71         }
72     }
73
74     // Public
75
76     get element() { return this._element; }
77     get property() { return this._property; }
78     get nameTextField() { return this._nameTextField; }
79     get valueTextField() { return this._valueTextField; }
80     get enabled() { return this._property.enabled; }
81
82     set index(index)
83     {
84         this._element.dataset.propertyIndex = index;
85     }
86
87     get selected()
88     {
89         return this._selected;
90     }
91
92     set selected(value)
93     {
94         if (value === this._selected)
95             return;
96
97         this._selected = value;
98         this.updateStatus();
99     }
100
101     startEditingName()
102     {
103         if (!this._nameTextField)
104             return;
105
106         this._nameTextField.startEditing();
107     }
108
109     startEditingValue()
110     {
111         if (!this._valueTextField)
112             return;
113
114         this._valueTextField.startEditing();
115     }
116
117     detached()
118     {
119         if (this._nameTextField)
120             this._nameTextField.detached();
121
122         if (this._valueTextField)
123             this._valueTextField.detached();
124     }
125
126     hidden()
127     {
128         if (this._nameTextField && this._nameTextField.editing)
129             this._nameTextField.element.blur();
130         else if (this._valueTextField && this._valueTextField.editing)
131             this._valueTextField.element.blur();
132     }
133
134     remove(replacement = null)
135     {
136         console.assert(this._property.ownerStyle.locked, `Removed property was unlocked (${this._property.name})`);
137         this.element.remove();
138
139         if (replacement)
140             this._property.replaceWithText(replacement);
141         else
142             this._property.remove();
143
144         this.detached();
145
146         if (this._delegate && typeof this._delegate.spreadsheetStylePropertyRemoved === "function")
147             this._delegate.spreadsheetStylePropertyRemoved(this);
148     }
149
150     update()
151     {
152         this.element.removeChildren();
153
154         if (this._isEditable()) {
155             this._checkboxElement = this.element.appendChild(document.createElement("input"));
156             this._checkboxElement.classList.add("property-toggle");
157             this._checkboxElement.type = "checkbox";
158             this._checkboxElement.checked = this._property.enabled;
159             this._checkboxElement.tabIndex = -1;
160             this._checkboxElement.addEventListener("click", (event) => {
161                 console.assert(this._property.ownerStyle.locked, `Toggled property was unlocked (${this._property.name})`);
162                 event.stopPropagation();
163                 let disabled = !this._checkboxElement.checked;
164                 this._property.commentOut(disabled);
165                 this.update();
166             });
167         }
168
169         this._contentElement = this.element.appendChild(document.createElement("span"));
170         this._contentElement.className = "content";
171
172         if (!this._property.enabled)
173             this._contentElement.append("/* ");
174
175         this._nameElement = this._contentElement.appendChild(document.createElement("span"));
176         this._nameElement.classList.add("name");
177         this._nameElement.textContent = this._property.name;
178
179         let colonElement = this._contentElement.appendChild(document.createElement("span"));
180         colonElement.classList.add("colon");
181         colonElement.textContent = ": ";
182
183         this._valueElement = this._contentElement.appendChild(document.createElement("span"));
184         this._valueElement.classList.add("value");
185         this._renderValue(this._property.rawValue);
186
187         if (this._isEditable() && this._property.enabled) {
188             this._nameElement.tabIndex = 0;
189             this._nameElement.addEventListener("beforeinput", this._handleNameBeforeInput.bind(this));
190             this._nameElement.addEventListener("paste", this._handleNamePaste.bind(this));
191
192             this._nameTextField = new WI.SpreadsheetTextField(this, this._nameElement, this._nameCompletionDataProvider.bind(this));
193
194             this._valueElement.tabIndex = 0;
195             this._valueElement.addEventListener("beforeinput", this._handleValueBeforeInput.bind(this));
196
197             this._valueTextField = new WI.SpreadsheetTextField(this, this._valueElement, this._valueCompletionDataProvider.bind(this));
198         }
199
200         if (this._isEditable()) {
201             this._setupJumpToSymbol(this._nameElement);
202             this._setupJumpToSymbol(this._valueElement);
203         }
204
205         let semicolonElement = this._contentElement.appendChild(document.createElement("span"));
206         semicolonElement.classList.add("semicolon");
207         semicolonElement.textContent = ";";
208
209         if (this._property.enabled) {
210             this._warningElement = this.element.appendChild(document.createElement("span"));
211             this._warningElement.className = "warning";
212         } else
213             this._contentElement.append(" */");
214
215         if (!this._property.implicit && this._property.ownerStyle.type === WI.CSSStyleDeclaration.Type.Computed) {
216             let effectiveProperty = this._property.ownerStyle.nodeStyles.effectivePropertyForName(this._property.name);
217             if (effectiveProperty && !effectiveProperty.styleSheetTextRange)
218                 effectiveProperty = effectiveProperty.relatedShorthandProperty;
219
220             let ownerRule = effectiveProperty ? effectiveProperty.ownerStyle.ownerRule : null;
221
222             let arrowElement = this._contentElement.appendChild(WI.createGoToArrowButton());
223             arrowElement.addEventListener("click", (event) => {
224                 if (!effectiveProperty || !ownerRule || !event.altKey) {
225                     if (this._delegate.spreadsheetStylePropertyShowProperty)
226                         this._delegate.spreadsheetStylePropertyShowProperty(this, this._property);
227                     return;
228                 }
229
230                 let sourceCode = ownerRule.sourceCodeLocation.sourceCode;
231                 let {startLine, startColumn} = effectiveProperty.styleSheetTextRange;
232                 WI.showSourceCodeLocation(sourceCode.createSourceCodeLocation(startLine, startColumn), {
233                     ignoreNetworkTab: true,
234                     ignoreSearchTab: true,
235                 });
236             });
237
238             if (effectiveProperty && ownerRule)
239                 arrowElement.title = WI.UIString("Option-click to show source");
240         }
241
242         this.updateStatus();
243     }
244
245     updateStatus()
246     {
247         let duplicatePropertyExistsBelow = (cssProperty) => {
248             let propertyFound = false;
249
250             for (let property of this._property.ownerStyle.enabledProperties) {
251                 if (property === cssProperty)
252                     propertyFound = true;
253                 else if (property.name === cssProperty.name && propertyFound)
254                     return true;
255             }
256
257             return false;
258         };
259
260         let classNames = [WI.SpreadsheetStyleProperty.StyleClassName];
261         let elementTitle = "";
262
263         if (this._property.overridden) {
264             classNames.push("overridden");
265             if (duplicatePropertyExistsBelow(this._property)) {
266                 classNames.push("has-warning");
267                 elementTitle = WI.UIString("Duplicate property");
268             }
269         }
270
271         if (this._property.implicit)
272             classNames.push("implicit");
273
274         if (this._property.ownerStyle.inherited && !this._property.inherited)
275             classNames.push("not-inherited");
276
277         if (!this._property.valid && this._property.hasOtherVendorNameOrKeyword())
278             classNames.push("other-vendor");
279         else if (this._hasInvalidVariableValue || (!this._property.valid && this._property.value !== "")) {
280             let propertyNameIsValid = false;
281             if (WI.CSSCompletions.cssNameCompletions)
282                 propertyNameIsValid = WI.CSSCompletions.cssNameCompletions.isValidPropertyName(this._property.name);
283
284             classNames.push("has-warning");
285
286             if (!propertyNameIsValid) {
287                 classNames.push("invalid-name");
288                 elementTitle = WI.UIString("Unsupported property name");
289             } else {
290                 classNames.push("invalid-value");
291                 elementTitle = WI.UIString("Unsupported property value");
292             }
293         }
294
295         if (!this._property.enabled)
296             classNames.push("disabled");
297
298         if (this._property.modified)
299             classNames.push("modified");
300
301         if (this._selected)
302             classNames.push("selected");
303
304         this._element.className = classNames.join(" ");
305         this._element.title = elementTitle;
306     }
307
308     applyFilter(filterText)
309     {
310         let matchesName = this._nameElement.textContent.includes(filterText);
311         this._nameElement.classList.toggle(WI.GeneralStyleDetailsSidebarPanel.FilterMatchSectionClassName, !!matchesName);
312
313         let matchesValue = this._valueElement.textContent.includes(filterText);
314         this._valueElement.classList.toggle(WI.GeneralStyleDetailsSidebarPanel.FilterMatchSectionClassName, !!matchesValue);
315
316         let matches = matchesName || matchesValue;
317         this._contentElement.classList.toggle(WI.GeneralStyleDetailsSidebarPanel.NoFilterMatchInPropertyClassName, !matches);
318         return matches;
319     }
320
321     handleCopyEvent(event)
322     {
323         this._delegate.spreadsheetStylePropertyCopy(event, this);
324     }
325
326     // SpreadsheetTextField delegate
327
328     spreadsheetTextFieldWillStartEditing(textField)
329     {
330         let isEditingName = textField === this._nameTextField;
331         textField.value = isEditingName ? this._property.name : this._property.rawValue;
332     }
333
334     spreadsheetTextFieldDidChange(textField)
335     {
336         if (textField === this._valueTextField)
337             this._handleValueChange();
338         else if (textField === this._nameTextField)
339             this._handleNameChange();
340     }
341
342     spreadsheetTextFieldDidCommit(textField, {direction})
343     {
344         let propertyName = this._nameTextField.value.trim();
345         let propertyValue = this._valueTextField.value.trim();
346         let willRemoveProperty = false;
347         let isEditingName = textField === this._nameTextField;
348
349         if (!propertyName || (!propertyValue && !isEditingName && direction === "forward"))
350             willRemoveProperty = true;
351
352         if (!isEditingName && !willRemoveProperty)
353             this._renderValue(propertyValue);
354
355         if (direction === "forward") {
356             if (isEditingName && !willRemoveProperty) {
357                 // Move focus from the name to the value.
358                 this._valueTextField.startEditing();
359                 return;
360             }
361         } else {
362             if (!isEditingName) {
363                 // Move focus from the value to the name.
364                 this._nameTextField.startEditing();
365                 return;
366             }
367         }
368
369         if (typeof this._delegate.spreadsheetStylePropertyFocusMoved === "function") {
370             // Move focus away from the current property, to the next or previous one, if exists, or to the next or previous rule, if exists.
371             this._delegate.spreadsheetStylePropertyFocusMoved(this, {direction, willRemoveProperty});
372         }
373
374         if (willRemoveProperty)
375             this.remove();
376     }
377
378     spreadsheetTextFieldDidBlur(textField, event, changed)
379     {
380         let focusedOutsideThisProperty = event.relatedTarget !== this._nameElement && event.relatedTarget !== this._valueElement;
381         if (focusedOutsideThisProperty && (!this._nameTextField.value.trim() || !this._valueTextField.value.trim())) {
382             this.remove();
383             return;
384         }
385
386         if (textField === this._valueTextField)
387             this._renderValue(this._valueElement.textContent);
388
389         if (typeof this._delegate.spreadsheetStylePropertyFocusMoved === "function")
390             this._delegate.spreadsheetStylePropertyFocusMoved(this, {direction: null});
391
392         if (changed && window.DOMAgent)
393             DOMAgent.markUndoableState();
394     }
395
396     spreadsheetTextFieldDidBackspace(textField)
397     {
398         if (textField === this._nameTextField)
399             this.spreadsheetTextFieldDidCommit(textField, {direction: "backward"});
400         else if (textField === this._valueTextField)
401             this._nameTextField.startEditing();
402     }
403
404     spreadsheetTextFieldDidPressEsc(textField, textBeforeEditing)
405     {
406         let isNewProperty = !textBeforeEditing;
407         if (isNewProperty)
408             this.remove();
409         else if (this._delegate.spreadsheetStylePropertyDidPressEsc)
410             this._delegate.spreadsheetStylePropertyDidPressEsc(this);
411     }
412
413     // Private
414
415     _isEditable()
416     {
417         return !this._readOnly && this._property.editable;
418     }
419
420     _renderValue(value)
421     {
422         this._hasInvalidVariableValue = false;
423
424         const maxValueLength = 150;
425         let tokens = WI.tokenizeCSSValue(value);
426
427         if (this._property.enabled) {
428             // FIXME: <https://webkit.org/b/178636> Web Inspector: Styles: Make inline widgets work with CSS functions (var(), calc(), etc.)
429
430             // CSS variables may contain color - display color picker for them.
431             if (this._property.variable || WI.CSSKeywordCompletions.isColorAwareProperty(this._property.name)) {
432                 tokens = this._addGradientTokens(tokens);
433                 tokens = this._addColorTokens(tokens);
434             }
435             tokens = this._addTimingFunctionTokens(tokens, "cubic-bezier");
436             tokens = this._addTimingFunctionTokens(tokens, "spring");
437             tokens = this._addVariableTokens(tokens);
438         }
439
440         tokens = tokens.map((token) => {
441             if (token instanceof Element)
442                 return token;
443
444             let className = "";
445
446             if (token.type) {
447                 if (token.type.includes("string"))
448                     className = "token-string";
449                 else if (token.type.includes("link"))
450                     className = "token-link";
451                 else if (token.type.includes("comment"))
452                     className = "token-comment";
453             }
454
455             if (className) {
456                 let span = document.createElement("span");
457                 span.classList.add(className);
458                 span.textContent = token.value.truncateMiddle(maxValueLength);
459
460                 if (token.type && token.type.includes("link"))
461                     span.addEventListener("contextmenu", this._handleLinkContextMenu.bind(this, token));
462
463                 return span;
464             }
465
466             return token.value;
467         });
468
469         this._valueElement.removeChildren();
470         this._valueElement.append(...tokens);
471     }
472
473     _createInlineSwatch(type, text, valueObject)
474     {
475         let tokenElement = document.createElement("span");
476         let innerElement = document.createElement("span");
477         innerElement.textContent = text;
478
479         let readOnly = !this._isEditable();
480         let swatch = new WI.InlineSwatch(type, valueObject, readOnly);
481
482         swatch.addEventListener(WI.InlineSwatch.Event.ValueChanged, (event) => {
483             let value = event.data.value && event.data.value.toString();
484             if (!value)
485                 return;
486
487             innerElement.textContent = value;
488             this._handleValueChange();
489         }, this);
490
491         if (typeof this._delegate.stylePropertyInlineSwatchActivated === "function") {
492             swatch.addEventListener(WI.InlineSwatch.Event.Activated, () => {
493                 this._swatchActive = true;
494                 this._delegate.stylePropertyInlineSwatchActivated();
495             });
496         }
497
498         if (typeof this._delegate.stylePropertyInlineSwatchDeactivated === "function") {
499             swatch.addEventListener(WI.InlineSwatch.Event.Deactivated, () => {
500                 this._swatchActive = false;
501                 this._delegate.stylePropertyInlineSwatchDeactivated();
502             });
503         }
504
505         tokenElement.append(swatch.element, innerElement);
506
507         // Prevent the value from editing when clicking on the swatch.
508         swatch.element.addEventListener("click", (event) => {
509             if (this._swatchActive || event.shiftKey)
510                 event.stop();
511         });
512
513         return tokenElement;
514     }
515
516     _addGradientTokens(tokens)
517     {
518         let gradientRegex = /^(repeating-)?(linear|radial)-gradient$/i;
519         let newTokens = [];
520         let gradientStartIndex = NaN;
521         let openParenthesis = 0;
522
523         for (let i = 0; i < tokens.length; i++) {
524             let token = tokens[i];
525             if (token.type && token.type.includes("atom") && gradientRegex.test(token.value)) {
526                 gradientStartIndex = i;
527                 openParenthesis = 0;
528             } else if (token.value === "(" && !isNaN(gradientStartIndex))
529                 openParenthesis++;
530             else if (token.value === ")" && !isNaN(gradientStartIndex)) {
531                 openParenthesis--;
532                 if (openParenthesis > 0) {
533                     // Matched a CSS function inside of the gradient.
534                     continue;
535                 }
536
537                 let rawTokens = tokens.slice(gradientStartIndex, i + 1);
538                 let text = rawTokens.map((token) => token.value).join("");
539                 let gradient = WI.Gradient.fromString(text);
540                 if (gradient)
541                     newTokens.push(this._createInlineSwatch(WI.InlineSwatch.Type.Gradient, text, gradient));
542                 else
543                     newTokens.push(...rawTokens);
544
545                 gradientStartIndex = NaN;
546             } else if (isNaN(gradientStartIndex))
547                 newTokens.push(token);
548         }
549
550         return newTokens;
551     }
552
553     _addColorTokens(tokens)
554     {
555         let newTokens = [];
556
557         let pushPossibleColorToken = (text, ...rawTokens) => {
558             let color = WI.Color.fromString(text);
559             if (color)
560                 newTokens.push(this._createInlineSwatch(WI.InlineSwatch.Type.Color, text, color));
561             else
562                 newTokens.push(...rawTokens);
563         };
564
565         let colorFunctionStartIndex = NaN;
566
567         for (let i = 0; i < tokens.length; i++) {
568             let token = tokens[i];
569             if (token.type && token.type.includes("hex-color")) {
570                 // Hex
571                 pushPossibleColorToken(token.value, token);
572             } else if (WI.Color.FunctionNames.has(token.value) && token.type && (token.type.includes("atom") || token.type.includes("keyword"))) {
573                 // Color Function start
574                 colorFunctionStartIndex = i;
575             } else if (isNaN(colorFunctionStartIndex) && token.type && (token.type.includes("atom") || token.type.includes("keyword"))) {
576                 // Color keyword
577                 pushPossibleColorToken(token.value, token);
578             } else if (!isNaN(colorFunctionStartIndex)) {
579                 // Color Function end
580                 if (token.value !== ")")
581                     continue;
582
583                 let rawTokens = tokens.slice(colorFunctionStartIndex, i + 1);
584                 let text = rawTokens.map((token) => token.value).join("");
585                 pushPossibleColorToken(text, ...rawTokens);
586                 colorFunctionStartIndex = NaN;
587             } else
588                 newTokens.push(token);
589         }
590
591         return newTokens;
592     }
593
594     _addTimingFunctionTokens(tokens, tokenType)
595     {
596         let newTokens = [];
597         let startIndex = NaN;
598         let openParenthesis = 0;
599
600         for (let i = 0; i < tokens.length; i++) {
601             let token = tokens[i];
602             if (token.value === tokenType && token.type && token.type.includes("atom")) {
603                 startIndex = i;
604                 openParenthesis = 0;
605             } else if (token.value === "(" && !isNaN(startIndex))
606                 openParenthesis++;
607             else if (token.value === ")" && !isNaN(startIndex)) {
608
609                 openParenthesis--;
610                 if (openParenthesis > 0)
611                     continue;
612
613                 let rawTokens = tokens.slice(startIndex, i + 1);
614                 let text = rawTokens.map((token) => token.value).join("");
615
616                 let valueObject;
617                 let inlineSwatchType;
618                 if (tokenType === "cubic-bezier") {
619                     valueObject = WI.CubicBezier.fromString(text);
620                     inlineSwatchType = WI.InlineSwatch.Type.Bezier;
621                 } else if (tokenType === "spring") {
622                     valueObject = WI.Spring.fromString(text);
623                     inlineSwatchType = WI.InlineSwatch.Type.Spring;
624                 }
625
626                 if (valueObject)
627                     newTokens.push(this._createInlineSwatch(inlineSwatchType, text, valueObject));
628                 else
629                     newTokens.push(...rawTokens);
630
631                 startIndex = NaN;
632             } else if (isNaN(startIndex))
633                 newTokens.push(token);
634         }
635
636         return newTokens;
637     }
638
639     _addVariableTokens(tokens)
640     {
641         let newTokens = [];
642         let startIndex = NaN;
643         let openParenthesis = 0;
644
645         for (let i = 0; i < tokens.length; i++) {
646             let token = tokens[i];
647             if (token.value === "var" && token.type && token.type.includes("atom")) {
648                 startIndex = i;
649                 openParenthesis = 0;
650             } else if (token.value === "(" && !isNaN(startIndex))
651                 ++openParenthesis;
652             else if (token.value === ")" && !isNaN(startIndex)) {
653                 --openParenthesis;
654                 if (openParenthesis > 0)
655                     continue;
656
657                 let rawTokens = tokens.slice(startIndex, i + 1);
658                 let tokenValues = rawTokens.map((token) => token.value);
659                 let variableName = tokenValues.find((value, i) => value.startsWith("--") && /\bvariable-2\b/.test(rawTokens[i].type));
660
661                 const dontCreateIfMissing = true;
662                 let variableProperty = this._property.ownerStyle.nodeStyles.computedStyle.propertyForName(variableName, dontCreateIfMissing);
663                 if (variableProperty) {
664                     let valueObject = variableProperty.value.trim();
665                     newTokens.push(this._createInlineSwatch(WI.InlineSwatch.Type.Variable, tokenValues.join(""), valueObject));
666                 } else {
667                     this._hasInvalidVariableValue = true;
668                     newTokens.push(...rawTokens);
669                 }
670
671                 startIndex = NaN;
672             } else if (isNaN(startIndex))
673                 newTokens.push(token);
674         }
675
676         return newTokens;
677     }
678
679     _handleNameChange()
680     {
681         console.assert(this._property.ownerStyle.locked, `Modified property was unlocked (${this._property.name})`);
682
683         this._property.name = this._nameElement.textContent.trim();
684     }
685
686     _handleValueChange()
687     {
688         console.assert(this._property.ownerStyle.locked, `Modified property was unlocked (${this._property.name})`);
689
690         this._property.rawValue = this._valueElement.textContent.trim();
691     }
692
693     _handleNameBeforeInput(event)
694     {
695         if (event.data !== ":" || event.inputType !== "insertText")
696             return;
697
698         event.preventDefault();
699         this._nameTextField.discardCompletion();
700         this._valueTextField.startEditing();
701     }
702
703     _handleNamePaste(event)
704     {
705         let text = event.clipboardData.getData("text/plain");
706         if (!text || !text.includes(":"))
707             return;
708
709         event.preventDefault();
710
711         this.remove(text);
712
713         if (this._delegate.spreadsheetStylePropertyAddBlankPropertySoon) {
714             this._delegate.spreadsheetStylePropertyAddBlankPropertySoon(this, {
715                 index: parseInt(this._element.dataset.propertyIndex) + 1,
716             });
717         }
718     }
719
720     _nameCompletionDataProvider(prefix)
721     {
722         return WI.CSSCompletions.cssNameCompletions.startsWith(prefix);
723     }
724
725     _handleValueBeforeInput(event)
726     {
727         if (event.data !== ";" || event.inputType !== "insertText")
728             return;
729
730         let text = this._valueTextField.valueWithoutSuggestion();
731         let selection = window.getSelection();
732         if (!selection.rangeCount || selection.getRangeAt(0).endOffset !== text.length)
733             return;
734
735         // Find the first and last index (if any) of a quote character to ensure that the string
736         // doesn't contain unbalanced quotes. If so, then there's no way that the semicolon could be
737         // part of a string within the value, so we can assume that it's the property "terminator".
738         const quoteRegex = /["']/g;
739         let start = -1;
740         let end = text.length;
741         let match = null;
742         while (match = quoteRegex.exec(text)) {
743             if (start < 0)
744                 start = match.index;
745             end = match.index + 1;
746         }
747
748         if (start !== -1 && !text.substring(start, end).hasMatchingEscapedQuotes())
749             return;
750
751         event.preventDefault();
752         this._valueTextField.stopEditing();
753         this.spreadsheetTextFieldDidCommit(this._valueTextField, {direction: "forward"});
754     }
755
756     _valueCompletionDataProvider(prefix)
757     {
758         let propertyName = this._nameElement.textContent.trim();
759         return WI.CSSKeywordCompletions.forProperty(propertyName).startsWith(prefix);
760     }
761
762     _setupJumpToSymbol(element)
763     {
764         element.addEventListener("mousedown", (event) => {
765             if (event.button !== 0)
766                 return;
767
768             if (!WI.modifierKeys.metaKey)
769                 return;
770
771             if (element.isContentEditable)
772                 return;
773
774             let sourceCodeLocation = null;
775             if (this._property.ownerStyle.ownerRule)
776                 sourceCodeLocation = this._property.ownerStyle.ownerRule.sourceCodeLocation;
777
778             if (!sourceCodeLocation)
779                 return;
780
781             let range = this._property.styleSheetTextRange;
782             const options = {
783                 ignoreNetworkTab: true,
784                 ignoreSearchTab: true,
785             };
786             let sourceCode = sourceCodeLocation.sourceCode;
787             WI.showSourceCodeLocation(sourceCode.createSourceCodeLocation(range.startLine, range.startColumn), options);
788         });
789     }
790
791     _handleLinkContextMenu(token, event)
792     {
793         let contextMenu = WI.ContextMenu.createFromEvent(event);
794
795         let resolveURL = (url) => {
796             let ownerStyle = this._property.ownerStyle;
797             if (!ownerStyle)
798                 return url;
799
800             let ownerStyleSheet = ownerStyle.ownerStyleSheet;
801             if (!ownerStyleSheet) {
802                 let ownerRule = ownerStyle.ownerRule;
803                 if (ownerRule)
804                     ownerStyleSheet = ownerRule.ownerStyleSheet;
805             }
806             if (ownerStyleSheet) {
807                 if (ownerStyleSheet.url)
808                     return absoluteURL(url, ownerStyleSheet.url);
809
810                 let parentFrame = ownerStyleSheet.parentFrame;
811                 if (parentFrame)
812                     return absoluteURL(url, parentFrame.url);
813             }
814
815             let node = ownerStyle.node;
816             if (!node) {
817                 let nodeStyles = ownerStyle.nodeStyles;
818                 if (!nodeStyles) {
819                     let ownerRule = ownerStyle.ownerRule;
820                     if (ownerRule)
821                         nodeStyles = ownerRule.nodeStyles;
822                 }
823                 if (nodeStyles)
824                     node = nodeStyles.node;
825             }
826             if (node) {
827                 let ownerDocument = node.ownerDocument;
828                 if (ownerDocument)
829                     return absoluteURL(url, node.ownerDocument.documentURL);
830             }
831
832             return url;
833         };
834
835         WI.appendContextMenuItemsForURL(contextMenu, resolveURL(token.value));
836     }
837 };
838
839 WI.SpreadsheetStyleProperty.StyleClassName = "property";