edc9444a37a60474d98531f810bb9d52665b31d4
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / CSSStyleDeclarationSection.js
1 /*
2  * Copyright (C) 2013, 2015 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 WebInspector.CSSStyleDeclarationSection = class CSSStyleDeclarationSection extends WebInspector.Object
27 {
28     constructor(delegate, style)
29     {
30         console.assert(style instanceof WebInspector.CSSStyleDeclaration, style);
31
32         super();
33
34         this._delegate = delegate || null;
35
36         this._style = style || null;
37         this._selectorElements = [];
38         this._ruleDisabled = false;
39         this._hasInvalidSelector = false;
40
41         this._element = document.createElement("div");
42         this._element.classList.add("style-declaration-section");
43
44         new WebInspector.KeyboardShortcut(WebInspector.KeyboardShortcut.Modifier.CommandOrControl, "S", this._save.bind(this), this._element);
45         new WebInspector.KeyboardShortcut(WebInspector.KeyboardShortcut.Modifier.CommandOrControl | WebInspector.KeyboardShortcut.Modifier.Shift, "S", this._save.bind(this), this._element);
46
47         this._headerElement = document.createElement("div");
48         this._headerElement.classList.add("header");
49
50         if (!style.editable) {
51             let lockedIconElement = this._headerElement.createChild("img", "locked-icon");
52
53             let styleLabel;
54             if (style.ownerRule && style.ownerRule.type === WebInspector.CSSStyleSheet.Type.UserAgent)
55                 styleLabel = WebInspector.UIString("User Agent Stylesheet");
56             else
57                 styleLabel = WebInspector.UIString("Style rule");
58
59             lockedIconElement.title = WebInspector.UIString("%s cannot be modified").format(styleLabel);
60         }
61
62         this._iconElement = this._headerElement.createChild("img", "icon");
63
64         if (this.selectorEditable) {
65             this._selectorInput = this._headerElement.createChild("textarea");
66             this._selectorInput.spellcheck = false;
67             this._selectorInput.dir = "ltr";
68             this._selectorInput.tabIndex = -1;
69             this._selectorInput.addEventListener("mouseover", this._handleMouseOver.bind(this));
70             this._selectorInput.addEventListener("mousemove", this._handleMouseMove.bind(this));
71             this._selectorInput.addEventListener("mouseout", this._handleMouseOut.bind(this));
72             this._selectorInput.addEventListener("keydown", this._handleKeyDown.bind(this));
73             this._selectorInput.addEventListener("keypress", this._handleKeyPress.bind(this));
74             this._selectorInput.addEventListener("input", this._handleInput.bind(this));
75             this._selectorInput.addEventListener("paste", this._handleSelectorPaste.bind(this));
76             this._selectorInput.addEventListener("blur", this._handleBlur.bind(this));
77         }
78
79         this._selectorElement = this._headerElement.createChild("span", "selector");
80         if (!this.selectorEditable) {
81             this._selectorElement.addEventListener("mouseover", this._handleMouseOver.bind(this));
82             this._selectorElement.addEventListener("mouseout", this._handleMouseOut.bind(this));
83         }
84
85         this._originElement = this._headerElement.createChild("span", "origin");
86
87         this._propertiesElement = document.createElement("div");
88         this._propertiesElement.classList.add("properties");
89
90         this._editorActive = false;
91         this._propertiesTextEditor = new WebInspector.CSSStyleDeclarationTextEditor(this, style);
92         this._propertiesTextEditor.addEventListener(WebInspector.CSSStyleDeclarationTextEditor.Event.ContentChanged, this._editorContentChanged.bind(this));
93         this._propertiesTextEditor.addEventListener(WebInspector.CSSStyleDeclarationTextEditor.Event.Blurred, this._editorBlurred.bind(this));
94         this._propertiesElement.appendChild(this._propertiesTextEditor.element);
95
96         this._element.appendChild(this._headerElement);
97         this._element.appendChild(this._propertiesElement);
98
99         let iconClassName = null;
100         switch (style.type) {
101         case WebInspector.CSSStyleDeclaration.Type.Rule:
102             console.assert(style.ownerRule);
103
104             if (style.inherited)
105                 iconClassName = WebInspector.CSSStyleDeclarationSection.InheritedStyleRuleIconStyleClassName;
106             else if (style.ownerRule.type === WebInspector.CSSStyleSheet.Type.Author)
107                 iconClassName = WebInspector.CSSStyleDeclarationSection.AuthorStyleRuleIconStyleClassName;
108             else if (style.ownerRule.type === WebInspector.CSSStyleSheet.Type.User)
109                 iconClassName = WebInspector.CSSStyleDeclarationSection.UserStyleRuleIconStyleClassName;
110             else if (style.ownerRule.type === WebInspector.CSSStyleSheet.Type.UserAgent)
111                 iconClassName = WebInspector.CSSStyleDeclarationSection.UserAgentStyleRuleIconStyleClassName;
112             else if (style.ownerRule.type === WebInspector.CSSStyleSheet.Type.Inspector)
113                 iconClassName = WebInspector.CSSStyleDeclarationSection.InspectorStyleRuleIconStyleClassName;
114             break;
115
116         case WebInspector.CSSStyleDeclaration.Type.Inline:
117         case WebInspector.CSSStyleDeclaration.Type.Attribute:
118             if (style.inherited)
119                 iconClassName = WebInspector.CSSStyleDeclarationSection.InheritedElementStyleRuleIconStyleClassName;
120             else
121                 iconClassName = WebInspector.DOMTreeElementPathComponent.DOMElementIconStyleClassName;
122             break;
123         }
124
125         if (style.editable) {
126             this._iconElement.classList.add("toggle-able");
127             this._iconElement.title = WebInspector.UIString("Comment All Properties");
128             this._iconElement.addEventListener("click", this._handleIconElementClicked.bind(this));
129         }
130
131         console.assert(iconClassName);
132         this._element.classList.add(iconClassName);
133
134         if (!style.editable)
135             this._element.classList.add(WebInspector.CSSStyleDeclarationSection.LockedStyleClassName);
136         else if (style.ownerRule)
137             this._style.ownerRule.addEventListener(WebInspector.CSSRule.Event.SelectorChanged, this._updateSelectorIcon.bind(this));
138         else
139             this._element.classList.add(WebInspector.CSSStyleDeclarationSection.SelectorLockedStyleClassName);
140
141         this.refresh();
142
143         this._headerElement.addEventListener("contextmenu", this._handleContextMenuEvent.bind(this));
144     }
145
146     // Public
147
148     get element()
149     {
150         return this._element;
151     }
152
153     get style()
154     {
155         return this._style;
156     }
157
158     get lastInGroup()
159     {
160         return this._element.classList.contains(WebInspector.CSSStyleDeclarationSection.LastInGroupStyleClassName);
161     }
162
163     set lastInGroup(last)
164     {
165         if (last)
166             this._element.classList.add(WebInspector.CSSStyleDeclarationSection.LastInGroupStyleClassName);
167         else
168             this._element.classList.remove(WebInspector.CSSStyleDeclarationSection.LastInGroupStyleClassName);
169     }
170
171     get focused()
172     {
173         return this._propertiesTextEditor.focused;
174     }
175
176     focus()
177     {
178         this._propertiesTextEditor.focus();
179     }
180
181     refresh()
182     {
183         this._selectorElement.removeChildren();
184         this._originElement.removeChildren();
185         this._selectorElements = [];
186
187         this._originElement.append(` ${emDash} `);
188
189         function appendSelector(selector, matched)
190         {
191             console.assert(selector instanceof WebInspector.CSSSelector);
192
193             let selectorElement = document.createElement("span");
194             selectorElement.textContent = selector.text;
195
196             if (matched)
197                 selectorElement.classList.add(WebInspector.CSSStyleDeclarationSection.MatchedSelectorElementStyleClassName);
198
199             let specificity = selector.specificity;
200             if (specificity) {
201                 let tooltip = WebInspector.UIString("Specificity: (%d, %d, %d)").format(specificity[0], specificity[1], specificity[2]);
202                 if (selector.dynamic) {
203                     tooltip += "\n";
204                     if (this._style.inherited)
205                         tooltip += WebInspector.UIString("Dynamically calculated for the parent element");
206                     else
207                         tooltip += WebInspector.UIString("Dynamically calculated for the selected element");
208                 }
209                 selectorElement.title = tooltip;
210             } else if (selector.dynamic) {
211                 let tooltip = WebInspector.UIString("Specificity: No value for selected element");
212                 tooltip += "\n";
213                 tooltip += WebInspector.UIString("Dynamically calculated for the selected element and did not match");
214                 selectorElement.title = tooltip;
215             }
216
217             this._selectorElement.appendChild(selectorElement);
218             this._selectorElements.push(selectorElement);
219         }
220
221         function appendSelectorTextKnownToMatch(selectorText)
222         {
223             let selectorElement = document.createElement("span");
224             selectorElement.textContent = selectorText;
225             selectorElement.classList.add(WebInspector.CSSStyleDeclarationSection.MatchedSelectorElementStyleClassName);
226             this._selectorElement.appendChild(selectorElement);
227         }
228
229         switch (this._style.type) {
230         case WebInspector.CSSStyleDeclaration.Type.Rule:
231             console.assert(this._style.ownerRule);
232
233             let selectors = this._style.ownerRule.selectors;
234             let matchedSelectorIndices = this._style.ownerRule.matchedSelectorIndices;
235             let alwaysMatch = !matchedSelectorIndices.length;
236             if (selectors.length) {
237                 let hasMatchingPseudoElementSelector = false;
238                 for (let i = 0; i < selectors.length; ++i) {
239                     appendSelector.call(this, selectors[i], alwaysMatch || matchedSelectorIndices.includes(i));
240                     if (i < selectors.length - 1)
241                         this._selectorElement.append(", ");
242
243                     if (matchedSelectorIndices.includes(i) && selectors[i].isPseudoElementSelector())
244                         hasMatchingPseudoElementSelector = true;
245                 }
246                 this._element.classList.toggle(WebInspector.CSSStyleDeclarationSection.PseudoElementSelectorStyleClassName, hasMatchingPseudoElementSelector);
247             } else
248                 appendSelectorTextKnownToMatch.call(this, this._style.ownerRule.selectorText);
249
250             if (this._style.ownerRule.sourceCodeLocation) {
251                 let options = {
252                     dontFloat: true,
253                     ignoreNetworkTab: true,
254                     ignoreSearchTab: true,
255                 };
256                 if (this._style.ownerStyleSheet.isInspectorStyleSheet()) {
257                     options.nameStyle = WebInspector.SourceCodeLocation.NameStyle.None;
258                     options.prefix = WebInspector.UIString("Inspector Style Sheet") + ":";
259                 }
260
261                 let sourceCodeLink = WebInspector.createSourceCodeLocationLink(this._style.ownerRule.sourceCodeLocation, options);
262                 this._originElement.appendChild(sourceCodeLink);
263             } else {
264                 let originString;
265                 switch (this._style.ownerRule.type) {
266                 case WebInspector.CSSStyleSheet.Type.Author:
267                     originString = WebInspector.UIString("Author Stylesheet");
268                     break;
269
270                 case WebInspector.CSSStyleSheet.Type.User:
271                     originString = WebInspector.UIString("User Stylesheet");
272                     break;
273
274                 case WebInspector.CSSStyleSheet.Type.UserAgent:
275                     originString = WebInspector.UIString("User Agent Stylesheet");
276                     break;
277
278                 case WebInspector.CSSStyleSheet.Type.Inspector:
279                     originString = WebInspector.UIString("Web Inspector");
280                     break;
281                 }
282
283                 console.assert(originString);
284                 if (originString)
285                     this._originElement.append(originString);
286             }
287
288             break;
289
290         case WebInspector.CSSStyleDeclaration.Type.Inline:
291             appendSelectorTextKnownToMatch.call(this, this._style.node.displayName);
292             this._originElement.append(WebInspector.UIString("Style Attribute"));
293             break;
294
295         case WebInspector.CSSStyleDeclaration.Type.Attribute:
296             appendSelectorTextKnownToMatch.call(this, this._style.node.displayName);
297             this._originElement.append(WebInspector.UIString("HTML Attributes"));
298             break;
299         }
300
301         this._updateSelectorIcon();
302         if (this._selectorInput)
303             this._selectorInput.value = this._selectorElement.textContent;
304     }
305
306     highlightProperty(property)
307     {
308         if (this._propertiesTextEditor.highlightProperty(property)) {
309             this._element.scrollIntoView();
310             return true;
311         }
312
313         return false;
314     }
315
316     findMatchingPropertiesAndSelectors(needle)
317     {
318         this._element.classList.remove(WebInspector.CSSStyleDetailsSidebarPanel.NoFilterMatchInSectionClassName, WebInspector.CSSStyleDetailsSidebarPanel.FilterMatchingSectionHasLabelClassName);
319
320         var hasMatchingSelector = false;
321
322         for (var selectorElement of this._selectorElements) {
323             selectorElement.classList.remove(WebInspector.CSSStyleDetailsSidebarPanel.FilterMatchSectionClassName);
324
325             if (needle && selectorElement.textContent.includes(needle)) {
326                 selectorElement.classList.add(WebInspector.CSSStyleDetailsSidebarPanel.FilterMatchSectionClassName);
327                 hasMatchingSelector = true;
328             }
329         }
330
331         if (!needle) {
332             this._propertiesTextEditor.resetFilteredProperties();
333             return false;
334         }
335
336         var hasMatchingProperty = this._propertiesTextEditor.findMatchingProperties(needle);
337
338         if (!hasMatchingProperty && !hasMatchingSelector) {
339             this._element.classList.add(WebInspector.CSSStyleDetailsSidebarPanel.NoFilterMatchInSectionClassName);
340             return false;
341         }
342
343         return true;
344     }
345
346     updateLayout()
347     {
348         this._propertiesTextEditor.updateLayout();
349     }
350
351     clearSelection()
352     {
353         this._propertiesTextEditor.clearSelection();
354     }
355
356     cssStyleDeclarationTextEditorFocused()
357     {
358         if (typeof this._delegate.cssStyleDeclarationSectionEditorFocused === "function")
359             this._delegate.cssStyleDeclarationSectionEditorFocused(this);
360     }
361
362     cssStyleDeclarationTextEditorSwitchRule(reverse)
363     {
364         if (!this._delegate)
365             return;
366
367         if (reverse && typeof this._delegate.cssStyleDeclarationSectionEditorPreviousRule === "function")
368             this._delegate.cssStyleDeclarationSectionEditorPreviousRule(this);
369         else if (!reverse && typeof this._delegate.cssStyleDeclarationSectionEditorNextRule === "function")
370             this._delegate.cssStyleDeclarationSectionEditorNextRule(this);
371     }
372
373     focusRuleSelector(reverse)
374     {
375         if (!this.selectorEditable && !this.locked) {
376             this.focus();
377             return;
378         }
379
380         if (this.locked) {
381             this.cssStyleDeclarationTextEditorSwitchRule(reverse);
382             return;
383         }
384
385         let selection = window.getSelection();
386         selection.removeAllRanges();
387
388         this._element.scrollIntoViewIfNeeded();
389
390         if (this._selectorInput) {
391             this._selectorInput.focus();
392             this._selectorInput.selectionStart = 0;
393             this._selectorInput.selectionEnd = this._selectorInput.value.length;
394         } else {
395             let range = document.createRange();
396             range.selectNodeContents(this._selectorElement);
397             selection.addRange(range);
398         }
399     }
400
401     selectLastProperty()
402     {
403         this._propertiesTextEditor.selectLastProperty();
404     }
405
406     get selectorEditable()
407     {
408         return !this.locked && this._style.ownerRule;
409     }
410
411     get locked()
412     {
413         return !this._style.editable;
414     }
415
416     get editorActive()
417     {
418         return this._editorActive;
419     }
420
421     // Private
422
423     get _currentSelectorText()
424     {
425         let selectorText = this.selectorEditable ? this._selectorInput.value : this._selectorElement.textContent;
426         if (!selectorText || !selectorText.length) {
427             if (!this._style.ownerRule)
428                 return "";
429
430             selectorText = this._style.ownerRule.selectorText;
431         }
432
433         return selectorText.trim();
434     }
435
436     _handleSelectorPaste(event)
437     {
438         if (this._style.type === WebInspector.CSSStyleDeclaration.Type.Inline || !this._style.ownerRule)
439             return;
440
441         if (!event || !event.clipboardData)
442             return;
443
444         let data = event.clipboardData.getData("text/plain");
445         if (!data)
446             return;
447
448         function parseTextForRule(text)
449         {
450             let containsBraces = /[\{\}]/;
451             if (!containsBraces.test(text))
452                 return [];
453
454             let match = text.match(/([^{]+){([\s\S]*)}/);
455             if (!match)
456                 return [];
457
458             // If the match "body" contains braces, parse that body as if it were a rule.
459             // This will usually happen if the user includes a media query in the copied text.
460             return containsBraces.test(match[2]) ? parseTextForRule(match[2]) : match;
461         }
462
463         let [selector, value] = parseTextForRule(data);
464         if (!selector || !value)
465             return;
466
467         this._style.nodeStyles.changeRule(this._style.ownerRule, selector.trim(), value);
468         event.preventDefault();
469     }
470
471     _handleContextMenuEvent(event)
472     {
473         if (window.getSelection().toString().length)
474             return;
475
476         let contextMenu = WebInspector.ContextMenu.createFromEvent(event);
477
478         contextMenu.appendItem(WebInspector.UIString("Copy Rule"), () => {
479             InspectorFrontendHost.copyText(this._style.generateCSSRuleString());
480         });
481
482         if (this._style.inherited)
483             return;
484
485         contextMenu.appendItem(WebInspector.UIString("Duplicate Selector"), () => {
486             if (this._delegate && typeof this._delegate.focusEmptySectionWithStyle === "function") {
487                 let existingRules = this._style.nodeStyles.rulesForSelector(this._currentSelectorText);
488                 for (let rule of existingRules) {
489                     if (this._delegate.focusEmptySectionWithStyle(rule.style))
490                         return;
491                 }
492             }
493
494             this._style.nodeStyles.addRule(this._currentSelectorText);
495         });
496
497         // Only used one colon temporarily since single-colon pseudo elements are valid CSS.
498         if (WebInspector.CSSStyleManager.PseudoElementNames.some((className) => this._style.selectorText.includes(":" + className)))
499             return;
500
501         if (WebInspector.CSSStyleManager.ForceablePseudoClasses.every((className) => !this._style.selectorText.includes(":" + className))) {
502             contextMenu.appendSeparator();
503
504             for (let pseudoClass of WebInspector.CSSStyleManager.ForceablePseudoClasses) {
505                 if (pseudoClass === "visited" && this._style.node.nodeName() !== "A")
506                     continue;
507
508                 let pseudoClassSelector = ":" + pseudoClass;
509
510                 contextMenu.appendItem(WebInspector.UIString("Add %s Rule").format(pseudoClassSelector), () => {
511                     this._style.node.setPseudoClassEnabled(pseudoClass, true);
512
513                     let selector;
514                     if (this._style.ownerRule)
515                         selector = this._style.ownerRule.selectors.map((selector) => selector.text + pseudoClassSelector).join(", ");
516                     else
517                         selector = this._currentSelectorText + pseudoClassSelector;
518
519                     this._style.nodeStyles.addRule(selector);
520                 });
521             }
522         }
523
524         contextMenu.appendSeparator();
525
526         for (let pseudoElement of WebInspector.CSSStyleManager.PseudoElementNames) {
527             let pseudoElementSelector = "::" + pseudoElement;
528             const styleText = "content: \"\";";
529
530             let existingSection = null;
531             if (this._delegate && typeof this._delegate.sectionForStyle === "function") {
532                 let existingRules = this._style.nodeStyles.rulesForSelector(this._currentSelectorText + pseudoElementSelector);
533                 if (existingRules.length) {
534                     // There shouldn't really ever be more than one pseudo-element rule
535                     // that is not in a media query. As such, just focus the first rule
536                     // on the assumption that it is the only one necessary.
537                     existingSection = this._delegate.sectionForStyle(existingRules[0].style);
538                 }
539             }
540
541             let title = existingSection ? WebInspector.UIString("Focus %s Rule") : WebInspector.UIString("Create %s Rule");
542             contextMenu.appendItem(title.format(pseudoElementSelector), () => {
543                 if (existingSection) {
544                     existingSection.focus();
545                     return;
546                 }
547
548                 let selector;
549                 if (this._style.ownerRule)
550                     selector = this._style.ownerRule.selectors.map((selector) => selector.text + pseudoElementSelector).join(", ");
551                 else
552                     selector = this._currentSelectorText + pseudoElementSelector;
553
554                 this._style.nodeStyles.addRule(selector, styleText);
555             });
556         }
557     }
558
559     _handleIconElementClicked()
560     {
561         if (this._hasInvalidSelector) {
562             // This will revert the selector text to the original valid value.
563             this.refresh();
564             return;
565         }
566
567         this._ruleDisabled = this._ruleDisabled ? !this._propertiesTextEditor.uncommentAllProperties() : this._propertiesTextEditor.commentAllProperties();
568         this._iconElement.title = this._ruleDisabled ? WebInspector.UIString("Uncomment All Properties") : WebInspector.UIString("Comment All Properties");
569         this._element.classList.toggle("rule-disabled", this._ruleDisabled);
570     }
571
572     _highlightNodesWithSelector()
573     {
574         if (!this._style.ownerRule) {
575             WebInspector.domTreeManager.highlightDOMNode(this._style.node.id);
576             return;
577         }
578
579         WebInspector.domTreeManager.highlightSelector(this._currentSelectorText, this._style.node.ownerDocument.frameIdentifier);
580     }
581
582     _hideDOMNodeHighlight()
583     {
584         WebInspector.domTreeManager.hideDOMNodeHighlight();
585     }
586
587     _handleMouseOver(event)
588     {
589         this._highlightNodesWithSelector();
590     }
591
592     _handleMouseMove(event)
593     {
594         if (this._hasInvalidSelector)
595             return;
596
597         // Attempts to find a selector element under the mouse so that the title (which contains the
598         // specificity information) can be applied to _selectorInput, which will then display the
599         // title if the user hovers long enough.
600         for (let element of this._selectorElements) {
601             let {top, right, bottom, left} = element.getBoundingClientRect();
602             if (event.clientX >= left && event.clientX <= right && event.clientY >= top && event.clientY <= bottom) {
603                 this._selectorInput.title = element.title;
604                 return;
605             }
606         }
607
608         this._selectorInput.title = "";
609     }
610
611     _handleMouseOut(event)
612     {
613         this._hideDOMNodeHighlight();
614     }
615
616     _save(event)
617     {
618         event.preventDefault();
619         event.stopPropagation();
620
621         if (this._style.type !== WebInspector.CSSStyleDeclaration.Type.Rule) {
622             // FIXME: Can't save CSS inside <style></style> <https://webkit.org/b/150357>
623             InspectorFrontendHost.beep();
624             return;
625         }
626
627         console.assert(this._style.ownerRule instanceof WebInspector.CSSRule);
628         console.assert(this._style.ownerRule.sourceCodeLocation instanceof WebInspector.SourceCodeLocation);
629
630         let sourceCode = this._style.ownerRule.sourceCodeLocation.sourceCode;
631         if (sourceCode.type !== WebInspector.Resource.Type.Stylesheet) {
632             // FIXME: Can't save CSS inside style="" <https://webkit.org/b/150357>
633             InspectorFrontendHost.beep();
634             return;
635         }
636
637         var url;
638         if (sourceCode.urlComponents.scheme === "data") {
639             let mainResource = WebInspector.frameResourceManager.mainFrame.mainResource;
640             let pathDirectory = mainResource.url.slice(0, -mainResource.urlComponents.lastPathComponent.length);
641             url = pathDirectory + "base64.css";
642         } else
643             url = sourceCode.url;
644
645         const saveAs = event.shiftKey;
646         WebInspector.saveDataToFile({url: url, content: sourceCode.content}, saveAs);
647     }
648
649     _handleKeyDown(event)
650     {
651         if (event.keyCode === WebInspector.KeyboardShortcut.Key.Enter.keyCode) {
652             event.preventDefault();
653             this.focus();
654             return;
655         }
656
657         if (event.keyCode !== WebInspector.KeyboardShortcut.Key.Tab.keyCode) {
658             this._highlightNodesWithSelector();
659             return;
660         }
661
662         if (event.shiftKey && this._delegate && typeof this._delegate.cssStyleDeclarationSectionEditorPreviousRule === "function") {
663             event.preventDefault();
664             this._delegate.cssStyleDeclarationSectionEditorPreviousRule(this, true);
665             return;
666         }
667
668         if (!event.metaKey) {
669             event.preventDefault();
670             this.focus();
671             this._propertiesTextEditor.selectFirstProperty();
672             return;
673         }
674     }
675
676     _handleKeyPress(event)
677     {
678         if (!event.altGraphKey && !event.altKey && !event.ctrlKey && !event.metaKey) {
679             // Ensures that <textarea> does not scroll with added characters.  Since a
680             // <textarea> does not expand to fit its content, appending the pressed character to the
681             // end of the original (non-editable) selector element will ensure that the <textarea>
682             // will be large enough to fit the selector without scrolling.
683             this._selectorElement.append(String.fromCharCode(event.keyCode));
684         }
685     }
686
687     _handleInput(event)
688     {
689         this._selectorElement.textContent = this._selectorInput.value;
690
691         this._highlightNodesWithSelector();
692     }
693
694     _handleBlur(event)
695     {
696         this._hideDOMNodeHighlight();
697
698         let newSelectorText = this._currentSelectorText.trim();
699         if (!newSelectorText) {
700             // Revert to the current selector (by doing a refresh) since the new selector is empty.
701             this.refresh();
702             return;
703         }
704
705         if (event.relatedTarget && event.relatedTarget.isDescendant(this.element)) {
706             this._editorActive = true;
707             this.focus();
708         }
709
710         this._style.ownerRule.selectorText = newSelectorText;
711     }
712
713     _updateSelectorIcon(event)
714     {
715         if (!this._style.ownerRule || !this._style.editable)
716             return;
717
718         this._hasInvalidSelector = event && event.data && !event.data.valid;
719         this._element.classList.toggle("invalid-selector", !!this._hasInvalidSelector);
720         if (this._hasInvalidSelector) {
721             this._iconElement.title = WebInspector.UIString("The selector ā€œ%sā€ is invalid.\nClick to revert to the previous selector.").format(this._selectorElement.textContent.trim());
722             this._selectorInput.title = WebInspector.UIString("Using previous selector ā€œ%sā€").format(this._style.ownerRule.selectorText);
723             return;
724         }
725
726         this._iconElement.title = this._ruleDisabled ? WebInspector.UIString("Uncomment All Properties") : WebInspector.UIString("Comment All Properties");
727         this._selectorInput.title = "";
728     }
729
730     _editorContentChanged(event)
731     {
732         this._editorActive = true;
733     }
734
735     _editorBlurred(event)
736     {
737         this._editorActive = false;
738         this.dispatchEventToListeners(WebInspector.CSSStyleDeclarationSection.Event.Blurred);
739     }
740 };
741
742 WebInspector.CSSStyleDeclarationSection.Event = {
743     Blurred: "css-style-declaration-sections-blurred"
744 };
745
746 WebInspector.CSSStyleDeclarationSection.LockedStyleClassName = "locked";
747 WebInspector.CSSStyleDeclarationSection.SelectorLockedStyleClassName = "selector-locked";
748 WebInspector.CSSStyleDeclarationSection.LastInGroupStyleClassName = "last-in-group";
749 WebInspector.CSSStyleDeclarationSection.MatchedSelectorElementStyleClassName = "matched";
750 WebInspector.CSSStyleDeclarationSection.PseudoElementSelectorStyleClassName = "pseudo-element-selector";
751
752 WebInspector.CSSStyleDeclarationSection.AuthorStyleRuleIconStyleClassName = "author-style-rule-icon";
753 WebInspector.CSSStyleDeclarationSection.UserStyleRuleIconStyleClassName = "user-style-rule-icon";
754 WebInspector.CSSStyleDeclarationSection.UserAgentStyleRuleIconStyleClassName = "user-agent-style-rule-icon";
755 WebInspector.CSSStyleDeclarationSection.InspectorStyleRuleIconStyleClassName = "inspector-style-rule-icon";
756 WebInspector.CSSStyleDeclarationSection.InheritedStyleRuleIconStyleClassName = "inherited-style-rule-icon";
757 WebInspector.CSSStyleDeclarationSection.InheritedElementStyleRuleIconStyleClassName = "inherited-element-style-rule-icon";