<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[194717] trunk/Source/WebInspectorUI</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/194717">194717</a></dd>
<dt>Author</dt> <dd>commit-queue@webkit.org</dd>
<dt>Date</dt> <dd>2016-01-07 13:27:48 -0800 (Thu, 07 Jan 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>Web Inspector: Make creating new rules in the Styles sidebar simpler
https://bugs.webkit.org/show_bug.cgi?id=152726

Patch by Devin Rousso &lt;dcrousso+webkit@gmail.com&gt; on 2016-01-07
Reviewed by Timothy Hatcher.

When creating a new rules of any kind, first look to see if it already exists in
another rule that has no properties. If found, focus/select it instead of creating
a new rule. Otherwise, create the new rule like normal.

Also changed the way in which previously-focused sections/tree-items are saved
to rely upon the selector of the new rule and whether the section for that rule
is empty and an inspector rule.

* Localizations/en.lproj/localizedStrings.js:

* UserInterface/Models/CSSStyleDeclaration.js:
(WebInspector.CSSStyleDeclaration.prototype.isInspectorRule):
Returns true if the style is an inspector rule.

(WebInspector.CSSStyleDeclaration.prototype.hasProperties):
Returns true if the style has CSS properties.

* UserInterface/Models/DOMNodeStyles.js:
(WebInspector.DOMNodeStyles.prototype.rulesForSelector.ruleHasSelector):
(WebInspector.DOMNodeStyles.prototype.rulesForSelector):
Returns a list of CSSRule that match the given selector and are not
in media queries.

* UserInterface/Views/BoxModelDetailsSectionRow.js:
(WebInspector.BoxModelDetailsSectionRow.prototype._updateMetrics):

* UserInterface/Views/CSSStyleDeclarationSection.js:
(WebInspector.CSSStyleDeclarationSection.prototype._handleContextMenuEvent):

* UserInterface/Views/CSSStyleDeclarationTextEditor.js:
(WebInspector.CSSStyleDeclarationTextEditor.prototype.commentAllProperties):

* UserInterface/Views/RulesStyleDetailsPanel.js:
(WebInspector.RulesStyleDetailsPanel.prototype.refresh.appendStyleSection):
(WebInspector.RulesStyleDetailsPanel.prototype.refresh.cssStyleDeclarationSectionEditorFocused):
Now saves the newly focused section as the previously-focused section.

(WebInspector.RulesStyleDetailsPanel.prototype.cssStyleDeclarationSectionFocusNewInspectorRuleWithSelector):
Renamed from cssStyleDeclarationSectionFocusNextNewInspectorRule. Now needs
a selector argument.

(WebInspector.RulesStyleDetailsPanel.prototype.newRuleButtonClicked):
(WebInspector.RulesStyleDetailsPanel.prototype.sectionForStyle):
Returns the first section that has a style with matching selector.

(WebInspector.RulesStyleDetailsPanel.prototype.focusEmptySectionWithStyle):
Finds the section corresponding to the given style and, if empty, focuses it.

(WebInspector.RulesStyleDetailsPanel.prototype.nodeStylesRefreshed):
No longer clears the previously-focused section.

* UserInterface/Views/VisualStyleSelectorSection.js:
(WebInspector.VisualStyleSelectorSection):
(WebInspector.VisualStyleSelectorSection.prototype.update.createSelectorItem):
(WebInspector.VisualStyleSelectorSection.prototype.update):
(WebInspector.VisualStyleSelectorSection.prototype.treeItemForStyle):
Returns the first tree item that has a style with matching selector.

(WebInspector.VisualStyleSelectorSection.prototype.selectEmptyStyleTreeItem):
Finds the tree item corresponding to the given style and, if empty, selects it.

(WebInspector.VisualStyleSelectorSection.prototype._addNewRule):

* UserInterface/Views/VisualStyleSelectorTreeItem.js:
(WebInspector.VisualStyleSelectorTreeItem):
Now requires a delegate object.

(WebInspector.VisualStyleSelectorTreeItem.prototype._handleContextMenuEvent):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebInspectorUIChangeLog">trunk/Source/WebInspectorUI/ChangeLog</a></li>
<li><a href="#trunkSourceWebInspectorUILocalizationsenlprojlocalizedStringsjs">trunk/Source/WebInspectorUI/Localizations/en.lproj/localizedStrings.js</a></li>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceModelsCSSStyleDeclarationjs">trunk/Source/WebInspectorUI/UserInterface/Models/CSSStyleDeclaration.js</a></li>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceModelsDOMNodeStylesjs">trunk/Source/WebInspectorUI/UserInterface/Models/DOMNodeStyles.js</a></li>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceViewsBoxModelDetailsSectionRowjs">trunk/Source/WebInspectorUI/UserInterface/Views/BoxModelDetailsSectionRow.js</a></li>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceViewsCSSStyleDeclarationSectionjs">trunk/Source/WebInspectorUI/UserInterface/Views/CSSStyleDeclarationSection.js</a></li>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceViewsCSSStyleDeclarationTextEditorjs">trunk/Source/WebInspectorUI/UserInterface/Views/CSSStyleDeclarationTextEditor.js</a></li>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceViewsRulesStyleDetailsPaneljs">trunk/Source/WebInspectorUI/UserInterface/Views/RulesStyleDetailsPanel.js</a></li>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceViewsVisualStyleSelectorSectionjs">trunk/Source/WebInspectorUI/UserInterface/Views/VisualStyleSelectorSection.js</a></li>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceViewsVisualStyleSelectorTreeItemjs">trunk/Source/WebInspectorUI/UserInterface/Views/VisualStyleSelectorTreeItem.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebInspectorUIChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/ChangeLog (194716 => 194717)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/ChangeLog        2016-01-07 21:20:37 UTC (rev 194716)
+++ trunk/Source/WebInspectorUI/ChangeLog        2016-01-07 21:27:48 UTC (rev 194717)
</span><span class="lines">@@ -1,3 +1,79 @@
</span><ins>+2016-01-07  Devin Rousso  &lt;dcrousso+webkit@gmail.com&gt;
+
+        Web Inspector: Make creating new rules in the Styles sidebar simpler
+        https://bugs.webkit.org/show_bug.cgi?id=152726
+
+        Reviewed by Timothy Hatcher.
+
+        When creating a new rules of any kind, first look to see if it already exists in
+        another rule that has no properties. If found, focus/select it instead of creating
+        a new rule. Otherwise, create the new rule like normal.
+
+        Also changed the way in which previously-focused sections/tree-items are saved
+        to rely upon the selector of the new rule and whether the section for that rule
+        is empty and an inspector rule.
+
+        * Localizations/en.lproj/localizedStrings.js:
+
+        * UserInterface/Models/CSSStyleDeclaration.js:
+        (WebInspector.CSSStyleDeclaration.prototype.isInspectorRule):
+        Returns true if the style is an inspector rule.
+
+        (WebInspector.CSSStyleDeclaration.prototype.hasProperties):
+        Returns true if the style has CSS properties.
+
+        * UserInterface/Models/DOMNodeStyles.js:
+        (WebInspector.DOMNodeStyles.prototype.rulesForSelector.ruleHasSelector):
+        (WebInspector.DOMNodeStyles.prototype.rulesForSelector):
+        Returns a list of CSSRule that match the given selector and are not
+        in media queries.
+
+        * UserInterface/Views/BoxModelDetailsSectionRow.js:
+        (WebInspector.BoxModelDetailsSectionRow.prototype._updateMetrics):
+
+        * UserInterface/Views/CSSStyleDeclarationSection.js:
+        (WebInspector.CSSStyleDeclarationSection.prototype._handleContextMenuEvent):
+
+        * UserInterface/Views/CSSStyleDeclarationTextEditor.js:
+        (WebInspector.CSSStyleDeclarationTextEditor.prototype.commentAllProperties):
+
+        * UserInterface/Views/RulesStyleDetailsPanel.js:
+        (WebInspector.RulesStyleDetailsPanel.prototype.refresh.appendStyleSection):
+        (WebInspector.RulesStyleDetailsPanel.prototype.refresh.cssStyleDeclarationSectionEditorFocused):
+        Now saves the newly focused section as the previously-focused section.
+
+        (WebInspector.RulesStyleDetailsPanel.prototype.cssStyleDeclarationSectionFocusNewInspectorRuleWithSelector):
+        Renamed from cssStyleDeclarationSectionFocusNextNewInspectorRule. Now needs
+        a selector argument.
+
+        (WebInspector.RulesStyleDetailsPanel.prototype.newRuleButtonClicked):
+        (WebInspector.RulesStyleDetailsPanel.prototype.sectionForStyle):
+        Returns the first section that has a style with matching selector.
+
+        (WebInspector.RulesStyleDetailsPanel.prototype.focusEmptySectionWithStyle):
+        Finds the section corresponding to the given style and, if empty, focuses it.
+
+        (WebInspector.RulesStyleDetailsPanel.prototype.nodeStylesRefreshed):
+        No longer clears the previously-focused section.
+
+        * UserInterface/Views/VisualStyleSelectorSection.js:
+        (WebInspector.VisualStyleSelectorSection):
+        (WebInspector.VisualStyleSelectorSection.prototype.update.createSelectorItem):
+        (WebInspector.VisualStyleSelectorSection.prototype.update):
+        (WebInspector.VisualStyleSelectorSection.prototype.treeItemForStyle):
+        Returns the first tree item that has a style with matching selector.
+
+        (WebInspector.VisualStyleSelectorSection.prototype.selectEmptyStyleTreeItem):
+        Finds the tree item corresponding to the given style and, if empty, selects it.
+
+        (WebInspector.VisualStyleSelectorSection.prototype._addNewRule):
+
+        * UserInterface/Views/VisualStyleSelectorTreeItem.js:
+        (WebInspector.VisualStyleSelectorTreeItem):
+        Now requires a delegate object.
+
+        (WebInspector.VisualStyleSelectorTreeItem.prototype._handleContextMenuEvent):
+
</ins><span class="cx"> 2016-01-06  Joseph Pecoraro  &lt;pecoraro@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Web Inspector: CRASH Attempting to pause on CSP violation not inside of script
</span></span></pre></div>
<a id="trunkSourceWebInspectorUILocalizationsenlprojlocalizedStringsjs"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/Localizations/en.lproj/localizedStrings.js (194716 => 194717)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/Localizations/en.lproj/localizedStrings.js        2016-01-07 21:20:37 UTC (rev 194716)
+++ trunk/Source/WebInspectorUI/Localizations/en.lproj/localizedStrings.js        2016-01-07 21:27:48 UTC (rev 194717)
</span><span class="lines">@@ -299,6 +299,7 @@
</span><span class="cx"> localizedStrings[&quot;Float&quot;] = &quot;Float&quot;;
</span><span class="cx"> localizedStrings[&quot;Float and Clear&quot;] = &quot;Float and Clear&quot;;
</span><span class="cx"> localizedStrings[&quot;Flows&quot;] = &quot;Flows&quot;;
</span><ins>+localizedStrings[&quot;Focus %s Rule&quot;] = &quot;Focus %s Rule&quot;;
</ins><span class="cx"> localizedStrings[&quot;Focused&quot;] = &quot;Focused&quot;;
</span><span class="cx"> localizedStrings[&quot;Font&quot;] = &quot;Font&quot;;
</span><span class="cx"> localizedStrings[&quot;Fonts&quot;] = &quot;Fonts&quot;;
</span><span class="lines">@@ -551,6 +552,7 @@
</span><span class="cx"> localizedStrings[&quot;Search Resource Content&quot;] = &quot;Search Resource Content&quot;;
</span><span class="cx"> localizedStrings[&quot;Secure&quot;] = &quot;Secure&quot;;
</span><span class="cx"> localizedStrings[&quot;Security Issue&quot;] = &quot;Security Issue&quot;;
</span><ins>+localizedStrings[&quot;Select %s Rule&quot;] = &quot;Select %s Rule&quot;;
</ins><span class="cx"> localizedStrings[&quot;Selected&quot;] = &quot;Selected&quot;;
</span><span class="cx"> localizedStrings[&quot;Selected Element&quot;] = &quot;Selected Element&quot;;
</span><span class="cx"> localizedStrings[&quot;Selected Frames&quot;] = &quot;Selected Frames&quot;;
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceModelsCSSStyleDeclarationjs"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/UserInterface/Models/CSSStyleDeclaration.js (194716 => 194717)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/Models/CSSStyleDeclaration.js        2016-01-07 21:20:37 UTC (rev 194716)
+++ trunk/Source/WebInspectorUI/UserInterface/Models/CSSStyleDeclaration.js        2016-01-07 21:27:48 UTC (rev 194717)
</span><span class="lines">@@ -332,6 +332,16 @@
</span><span class="cx">         return styleText;
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    isInspectorRule()
+    {
+        return this._ownerRule &amp;&amp; this._ownerRule.type === WebInspector.CSSStyleSheet.Type.Inspector;
+    }
+
+    hasProperties()
+    {
+        return !!this._properties.length;
+    }
+
</ins><span class="cx">     // Protected
</span><span class="cx"> 
</span><span class="cx">     get nodeStyles()
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceModelsDOMNodeStylesjs"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/UserInterface/Models/DOMNodeStyles.js (194716 => 194717)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/Models/DOMNodeStyles.js        2016-01-07 21:20:37 UTC (rev 194716)
+++ trunk/Source/WebInspectorUI/UserInterface/Models/DOMNodeStyles.js        2016-01-07 21:27:48 UTC (rev 194717)
</span><span class="lines">@@ -268,6 +268,22 @@
</span><span class="cx">         WebInspector.cssStyleManager.preferredInspectorStyleSheetForFrame(this._node.frame, inspectorStyleSheetAvailable.bind(this));
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    rulesForSelector(selector)
+    {
+        selector = selector || this._node.appropriateSelectorFor(true);
+
+        function ruleHasSelector(rule) {
+            return !rule.mediaList.length &amp;&amp; rule.selectorText === selector;
+        }
+
+        let rules = this._matchedRules.filter(ruleHasSelector);
+
+        for (let id in this._pseudoElements)
+            rules = rules.concat(this._pseudoElements[id].matchedRules.filter(ruleHasSelector));
+
+        return rules;
+    }
+
</ins><span class="cx">     get matchedRules()
</span><span class="cx">     {
</span><span class="cx">         return this._matchedRules;
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceViewsBoxModelDetailsSectionRowjs"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/UserInterface/Views/BoxModelDetailsSectionRow.js (194716 => 194717)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/Views/BoxModelDetailsSectionRow.js        2016-01-07 21:20:37 UTC (rev 194716)
+++ trunk/Source/WebInspectorUI/UserInterface/Views/BoxModelDetailsSectionRow.js        2016-01-07 21:27:48 UTC (rev 194717)
</span><span class="lines">@@ -193,7 +193,7 @@
</span><span class="cx">         this._boxElements = [];
</span><span class="cx">         var boxes = [&quot;content&quot;, &quot;padding&quot;, &quot;border&quot;, &quot;margin&quot;, &quot;position&quot;];
</span><span class="cx"> 
</span><del>-        if (!style.properties.length) {
</del><ins>+        if (!style.hasProperties()) {
</ins><span class="cx">             this.showEmptyMessage();
</span><span class="cx">             return;
</span><span class="cx">         }
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceViewsCSSStyleDeclarationSectionjs"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/UserInterface/Views/CSSStyleDeclarationSection.js (194716 => 194717)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/Views/CSSStyleDeclarationSection.js        2016-01-07 21:20:37 UTC (rev 194716)
+++ trunk/Source/WebInspectorUI/UserInterface/Views/CSSStyleDeclarationSection.js        2016-01-07 21:27:48 UTC (rev 194717)
</span><span class="lines">@@ -444,9 +444,17 @@
</span><span class="cx">             return;
</span><span class="cx"> 
</span><span class="cx">         contextMenu.appendItem(WebInspector.UIString(&quot;Duplicate Selector&quot;), () =&gt; {
</span><del>-            if (this._delegate &amp;&amp; typeof this._delegate.cssStyleDeclarationSectionFocusNextNewInspectorRule === &quot;function&quot;)
-                this._delegate.cssStyleDeclarationSectionFocusNextNewInspectorRule();
</del><ins>+            if (this._delegate &amp;&amp; typeof this._delegate.focusEmptySectionWithStyle === &quot;function&quot;) {
+                let existingRules = this._style.nodeStyles.rulesForSelector(this._currentSelectorText);
+                for (let rule of existingRules) {
+                    if (this._delegate.focusEmptySectionWithStyle(rule.style))
+                        return;
+                }
+            }
</ins><span class="cx"> 
</span><ins>+            if (this._delegate &amp;&amp; typeof this._delegate.cssStyleDeclarationSectionFocusNewInspectorRuleWithSelector === &quot;function&quot;)
+                this._delegate.cssStyleDeclarationSectionFocusNewInspectorRuleWithSelector(this._currentSelectorText);
+
</ins><span class="cx">             this._style.nodeStyles.addRule(this._currentSelectorText);
</span><span class="cx">         });
</span><span class="cx"> 
</span><span class="lines">@@ -464,14 +472,16 @@
</span><span class="cx">                 contextMenu.appendItem(WebInspector.UIString(&quot;Add %s Rule&quot;).format(pseudoClassSelector), () =&gt; {
</span><span class="cx">                     this._style.node.setPseudoClassEnabled(pseudoClass, true);
</span><span class="cx"> 
</span><del>-                    if (this._delegate &amp;&amp; typeof this._delegate.cssStyleDeclarationSectionFocusNextNewInspectorRule === &quot;function&quot;)
-                        this._delegate.cssStyleDeclarationSectionFocusNextNewInspectorRule();
</del><ins>+                    let selector;
+                    if (this._style.ownerRule)
+                        selector = this._style.ownerRule.selectors.map((selector) =&gt; selector.text + pseudoClassSelector).join(&quot;, &quot;);
+                    else
+                        selector = this._currentSelectorText + pseudoClassSelector;
</ins><span class="cx"> 
</span><del>-                    if (this._style.ownerRule) {
-                        let pseudoSelectors = this._style.ownerRule.selectors.map((selector) =&gt; selector.text + pseudoClassSelector);
-                        this._style.nodeStyles.addRule(pseudoSelectors.join(&quot;,&quot;));
-                    } else
-                        this._style.nodeStyles.addRule(this._currentSelectorText + pseudoClassSelector);
</del><ins>+                    if (this._delegate &amp;&amp; typeof this._delegate.cssStyleDeclarationSectionFocusNewInspectorRuleWithSelector === &quot;function&quot;)
+                        this._delegate.cssStyleDeclarationSectionFocusNewInspectorRuleWithSelector(selector);
+
+                    this._style.nodeStyles.addRule(selector);
</ins><span class="cx">                 });
</span><span class="cx">             }
</span><span class="cx">         }
</span><span class="lines">@@ -480,15 +490,34 @@
</span><span class="cx">             let pseudoElementSelector = &quot;::&quot; + pseudoElement;
</span><span class="cx">             const styleText = &quot;content: \&quot;\&quot;;&quot;;
</span><span class="cx"> 
</span><del>-            contextMenu.appendItem(WebInspector.UIString(&quot;Create %s Rule&quot;).format(pseudoElementSelector), () =&gt; {
-                if (this._delegate &amp;&amp; typeof this._delegate.cssStyleDeclarationSectionFocusNextNewInspectorRule === &quot;function&quot;)
-                    this._delegate.cssStyleDeclarationSectionFocusNextNewInspectorRule();
</del><ins>+            let existingSection = null;
+            if (this._delegate &amp;&amp; typeof this._delegate.sectionForStyle === &quot;function&quot;) {
+                let existingRules = this._style.nodeStyles.rulesForSelector(this._currentSelectorText + pseudoElementSelector);
+                if (existingRules.length) {
+                    // There shouldn't really ever be more than one pseudo-element rule
+                    // that is not in a media query. As such, just focus the first rule
+                    // on the assumption that it is the only one necessary.
+                    existingSection = this._delegate.sectionForStyle(existingRules[0].style);
+                }
+            }
</ins><span class="cx"> 
</span><del>-                if (this._style.ownerRule) {
-                    let pseudoSelectors = this._style.ownerRule.selectors.map((selector) =&gt; selector.text + pseudoElementSelector);
-                    this._style.nodeStyles.addRule(pseudoSelectors.join(&quot;,&quot;), styleText);
-                } else
-                    this._style.nodeStyles.addRule(this._currentSelectorText + pseudoElementSelector, styleText);
</del><ins>+            let title = existingSection ? WebInspector.UIString(&quot;Focus %s Rule&quot;) : WebInspector.UIString(&quot;Create %s Rule&quot;);
+            contextMenu.appendItem(title.format(pseudoElementSelector), () =&gt; {
+                if (existingSection) {
+                    existingSection.focus();
+                    return;
+                }
+
+                let selector;
+                if (this._style.ownerRule)
+                    selector = this._style.ownerRule.selectors.map((selector) =&gt; selector.text + pseudoElementSelector).join(&quot;, &quot;);
+                else
+                    selector = this._currentSelectorText + pseudoElementSelector;
+
+                if (this._delegate &amp;&amp; typeof this._delegate.cssStyleDeclarationSectionFocusNewInspectorRuleWithSelector === &quot;function&quot;)
+                    this._delegate.cssStyleDeclarationSectionFocusNewInspectorRuleWithSelector(selector);
+
+                this._style.nodeStyles.addRule(selector, styleText);
</ins><span class="cx">             });
</span><span class="cx">         }
</span><span class="cx">     }
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceViewsCSSStyleDeclarationTextEditorjs"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/UserInterface/Views/CSSStyleDeclarationTextEditor.js (194716 => 194717)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/Views/CSSStyleDeclarationTextEditor.js        2016-01-07 21:20:37 UTC (rev 194716)
+++ trunk/Source/WebInspectorUI/UserInterface/Views/CSSStyleDeclarationTextEditor.js        2016-01-07 21:27:48 UTC (rev 194717)
</span><span class="lines">@@ -338,7 +338,7 @@
</span><span class="cx"> 
</span><span class="cx">     commentAllProperties()
</span><span class="cx">     {
</span><del>-        if (!this._style.properties.length)
</del><ins>+        if (!this._style.hasProperties())
</ins><span class="cx">             return false;
</span><span class="cx"> 
</span><span class="cx">         for (var property of this._style.properties) {
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceViewsRulesStyleDetailsPaneljs"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/UserInterface/Views/RulesStyleDetailsPanel.js (194716 => 194717)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/Views/RulesStyleDetailsPanel.js        2016-01-07 21:20:37 UTC (rev 194716)
+++ trunk/Source/WebInspectorUI/UserInterface/Views/RulesStyleDetailsPanel.js        2016-01-07 21:27:48 UTC (rev 194717)
</span><span class="lines">@@ -153,9 +153,9 @@
</span><span class="cx">             } else
</span><span class="cx">                 section.refresh();
</span><span class="cx"> 
</span><del>-            if (this._focusNextNewInspectorRule &amp;&amp; style.ownerRule &amp;&amp; style.ownerRule.type === WebInspector.CSSStyleSheet.Type.Inspector) {
</del><ins>+            if (style.isInspectorRule() &amp;&amp; this._newInspectorRuleSelector === style.selectorText &amp;&amp; !style.hasProperties()) {
</ins><span class="cx">                 this._previousFocusedSection = section;
</span><del>-                delete this._focusNextNewInspectorRule;
</del><ins>+                this._newInspectorRuleSelector = null;
</ins><span class="cx">             }
</span><span class="cx"> 
</span><span class="cx">             // Reset lastInGroup in case the order/grouping changed.
</span><span class="lines">@@ -311,12 +311,13 @@
</span><span class="cx">         return false;
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    cssStyleDeclarationSectionEditorFocused(ignoredSection)
</del><ins>+    cssStyleDeclarationSectionEditorFocused(focusedSection)
</ins><span class="cx">     {
</span><del>-        for (var section of this._sections) {
-            if (section !== ignoredSection)
</del><ins>+        for (let section of this._sections) {
+            if (section !== focusedSection)
</ins><span class="cx">                 section.clearSelection();
</span><span class="cx">         }
</span><ins>+        this._previousFocusedSection = focusedSection;
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     cssStyleDeclarationSectionEditorNextRule(currentSection)
</span><span class="lines">@@ -377,9 +378,9 @@
</span><span class="cx">         this.element.classList.toggle(&quot;filter-non-matching&quot;, !matchFound);
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    cssStyleDeclarationSectionFocusNextNewInspectorRule()
</del><ins>+    cssStyleDeclarationSectionFocusNewInspectorRuleWithSelector(selector)
</ins><span class="cx">     {
</span><del>-        this._focusNextNewInspectorRule = true;
</del><ins>+        this._newInspectorRuleSelector = selector;
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     newRuleButtonClicked()
</span><span class="lines">@@ -387,10 +388,40 @@
</span><span class="cx">         if (this.nodeStyles.node.isInShadowTree())
</span><span class="cx">             return;
</span><span class="cx"> 
</span><del>-        this._focusNextNewInspectorRule = true;
-        this.nodeStyles.addRule();
</del><ins>+        for (let existingRule of this.nodeStyles.rulesForSelector()) {
+            if (this.focusEmptySectionWithStyle(existingRule.style))
+                return;
+        }
+
+        this._newInspectorRuleSelector = this.nodeStyles.node.appropriateSelectorFor(true);
+        this.nodeStyles.addRule(this._newInspectorRuleSelector);
</ins><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    sectionForStyle(style)
+    {
+        if (style.__rulesSection)
+            return style.__rulesSection;
+
+        for (let section of this._sections) {
+            if (section.style === style)
+                return section;
+        }
+        return null;
+    }
+
+    focusEmptySectionWithStyle(style)
+    {
+        if (style.hasProperties())
+            return false;
+
+        let section = this.sectionForStyle(style);
+        if (!section)
+            return false;
+
+        section.focus();
+        return true;
+    }
+
</ins><span class="cx">     // Protected
</span><span class="cx"> 
</span><span class="cx">     shown()
</span><span class="lines">@@ -431,10 +462,8 @@
</span><span class="cx">             this._propertyToSelectAndHighlight = null;
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        if (this._previousFocusedSection &amp;&amp; this._visible) {
</del><ins>+        if (this._previousFocusedSection &amp;&amp; this._visible)
</ins><span class="cx">             this._previousFocusedSection.focus();
</span><del>-            this._previousFocusedSection = null;
-        }
</del><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     // Private
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceViewsVisualStyleSelectorSectionjs"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/UserInterface/Views/VisualStyleSelectorSection.js (194716 => 194717)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/Views/VisualStyleSelectorSection.js        2016-01-07 21:20:37 UTC (rev 194716)
+++ trunk/Source/WebInspectorUI/UserInterface/Views/VisualStyleSelectorSection.js        2016-01-07 21:27:48 UTC (rev 194717)
</span><span class="lines">@@ -57,7 +57,7 @@
</span><span class="cx">         this._selectors = new WebInspector.TreeOutline(selectorListElement);
</span><span class="cx">         this._selectors.addEventListener(WebInspector.TreeOutline.Event.SelectionDidChange, this._selectorChanged, this);
</span><span class="cx"> 
</span><del>-        this._focusNextNewInspectorRule = false;
</del><ins>+        this._newInspectorRuleSelector = null;
</ins><span class="cx"> 
</span><span class="cx">         let addGlyphElement = useSVGSymbol(&quot;Images/Plus13.svg&quot;, &quot;visual-style-selector-section-add-rule&quot;, WebInspector.UIString(&quot;Click to add a new rule.&quot;));
</span><span class="cx">         addGlyphElement.addEventListener(&quot;click&quot;, this._addNewRule.bind(this));
</span><span class="lines">@@ -96,16 +96,16 @@
</span><span class="cx">             orderedPseudoRules.reverse();
</span><span class="cx"> 
</span><span class="cx">         function createSelectorItem(style, title, subtitle) {
</span><del>-            let selector = new WebInspector.VisualStyleSelectorTreeItem(style, title, subtitle);
</del><ins>+            let selector = new WebInspector.VisualStyleSelectorTreeItem(this, style, title, subtitle);
</ins><span class="cx">             selector.addEventListener(WebInspector.VisualStyleSelectorTreeItem.Event.StyleTextReset, this._styleTextReset, this);
</span><span class="cx">             selector.addEventListener(WebInspector.VisualStyleSelectorTreeItem.Event.CheckboxChanged, this._treeElementCheckboxToggled, this);
</span><span class="cx">             this._selectors.appendChild(selector);
</span><span class="cx"> 
</span><del>-            if (this._focusNextNewInspectorRule &amp;&amp; style.ownerRule &amp;&amp; style.ownerRule.type === WebInspector.CSSStyleSheet.Type.Inspector) {
</del><ins>+            if (style.isInspectorRule() &amp;&amp; this._newInspectorRuleSelector === style.selectorText &amp;&amp; !style.hasProperties()) {
</ins><span class="cx">                 selector.select(true);
</span><span class="cx">                 selector.element.scrollIntoView();
</span><span class="cx">                 this._nodeStyles[WebInspector.VisualStyleSelectorSection.LastSelectedRuleSymbol] = style;
</span><del>-                this._focusNextNewInspectorRule = false;
</del><ins>+                this._newInspectorRuleSelector = null;
</ins><span class="cx">                 return;
</span><span class="cx">             }
</span><span class="cx"> 
</span><span class="lines">@@ -201,7 +201,7 @@
</span><span class="cx">             }
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        this._focusNextNewInspectorRule = false;
</del><ins>+        this._newInspectorRuleSelector = null;
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     currentStyle()
</span><span class="lines">@@ -212,6 +212,28 @@
</span><span class="cx">         return this._selectors.selectedTreeElement.representedObject;
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    treeItemForStyle(style)
+    {
+        for (let item of this._selectors.children) {
+            if (item.representedObject === style)
+                return item;
+        }
+        return null;
+    }
+
+    selectEmptyStyleTreeItem(style)
+    {
+        if (style.hasProperties())
+            return false;
+
+        let treeItem = this.treeItemForStyle(style);
+        if (!treeItem)
+            return false;
+
+        treeItem.select(true, true);
+        return true;
+    }
+
</ins><span class="cx">     // Private
</span><span class="cx"> 
</span><span class="cx">     _selectorChanged(event)
</span><span class="lines">@@ -242,11 +264,18 @@
</span><span class="cx"> 
</span><span class="cx">     _addNewRule(event)
</span><span class="cx">     {
</span><del>-        if (!this._nodeStyles)
</del><ins>+        if (!this._nodeStyles || this._nodeStyles.node.isInShadowTree())
</ins><span class="cx">             return;
</span><span class="cx"> 
</span><del>-        this._nodeStyles.addRule();
-        this._focusNextNewInspectorRule = true;
</del><ins>+        let selector = this.currentStyle().selectorText;
+        let existingRules = this._nodeStyles.rulesForSelector(selector);
+        for (let rule of existingRules) {
+            if (this.selectEmptyStyleTreeItem(rule.style))
+                return;
+        }
+
+        this._newInspectorRuleSelector = selector;
+        this._nodeStyles.addRule(selector);
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     _treeElementCheckboxToggled(event)
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceViewsVisualStyleSelectorTreeItemjs"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/UserInterface/Views/VisualStyleSelectorTreeItem.js (194716 => 194717)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/Views/VisualStyleSelectorTreeItem.js        2016-01-07 21:20:37 UTC (rev 194716)
+++ trunk/Source/WebInspectorUI/UserInterface/Views/VisualStyleSelectorTreeItem.js        2016-01-07 21:27:48 UTC (rev 194717)
</span><span class="lines">@@ -25,7 +25,7 @@
</span><span class="cx"> 
</span><span class="cx"> WebInspector.VisualStyleSelectorTreeItem = class VisualStyleSelectorTreeItem extends WebInspector.GeneralTreeElement
</span><span class="cx"> {
</span><del>-    constructor(style, title, subtitle)
</del><ins>+    constructor(delegate, style, title, subtitle)
</ins><span class="cx">     {
</span><span class="cx">         let iconClassName;
</span><span class="cx">         switch (style.type) {
</span><span class="lines">@@ -57,6 +57,8 @@
</span><span class="cx"> 
</span><span class="cx">         super([&quot;visual-style-selector-item&quot;, iconClassName], title, subtitle, style);
</span><span class="cx"> 
</span><ins>+        this._delegate = delegate;
+
</ins><span class="cx">         this._iconClassName = iconClassName;
</span><span class="cx">         this._lastValue = title;
</span><span class="cx">         this._enableEditing = true;
</span><span class="lines">@@ -141,10 +143,12 @@
</span><span class="cx">             InspectorFrontendHost.copyText(this.representedObject.generateCSSRuleString());
</span><span class="cx">         });
</span><span class="cx"> 
</span><del>-        contextMenu.appendItem(WebInspector.UIString(&quot;Reset&quot;), () =&gt; {
-            this.representedObject.resetText();
-            this.dispatchEventToListeners(WebInspector.VisualStyleSelectorTreeItem.Event.StyleTextReset);
-        });
</del><ins>+        if (this.representedObject.modified) {
+            contextMenu.appendItem(WebInspector.UIString(&quot;Reset&quot;), () =&gt; {
+                this.representedObject.resetText();
+                this.dispatchEventToListeners(WebInspector.VisualStyleSelectorTreeItem.Event.StyleTextReset);
+            });
+        }
</ins><span class="cx"> 
</span><span class="cx">         if (!this.representedObject.ownerRule)
</span><span class="cx">             return;
</span><span class="lines">@@ -169,12 +173,8 @@
</span><span class="cx"> 
</span><span class="cx">                 contextMenu.appendItem(WebInspector.UIString(&quot;Add %s Rule&quot;).format(pseudoClassSelector), () =&gt; {
</span><span class="cx">                     this.representedObject.node.setPseudoClassEnabled(pseudoClass, true);
</span><del>-
-                    if (this.representedObject.ownerRule) {
-                        let pseudoSelectors = this.representedObject.ownerRule.selectors.map((selector) =&gt; selector.text + pseudoClassSelector);
-                        this.representedObject.nodeStyles.addRule(pseudoSelectors.join(&quot;,&quot;));
-                    } else
-                        this.representedObject.nodeStyles.addRule(this._currentSelectorText + pseudoClassSelector);
</del><ins>+                    let pseudoSelectors = this.representedObject.ownerRule.selectors.map((selector) =&gt; selector.text + pseudoClassSelector);
+                    this.representedObject.nodeStyles.addRule(pseudoSelectors.join(&quot;, &quot;));
</ins><span class="cx">                 });
</span><span class="cx">             }
</span><span class="cx">         }
</span><span class="lines">@@ -183,12 +183,27 @@
</span><span class="cx">             let pseudoElementSelector = &quot;::&quot; + pseudoElement;
</span><span class="cx">             const styleText = &quot;content: \&quot;\&quot;;&quot;;
</span><span class="cx"> 
</span><del>-            contextMenu.appendItem(WebInspector.UIString(&quot;Create %s Rule&quot;).format(pseudoElementSelector), () =&gt; {
-                if (this.representedObject.ownerRule) {
-                    let pseudoSelectors = this.representedObject.ownerRule.selectors.map((selector) =&gt; selector.text + pseudoElementSelector);
-                    this.representedObject.nodeStyles.addRule(pseudoSelectors.join(&quot;,&quot;), styleText);
-                } else
-                    this.representedObject.nodeStyles.addRule(this._currentSelectorText + pseudoElementSelector, styleText);
</del><ins>+            let existingTreeItem = null;
+            if (this._delegate &amp;&amp; typeof this._delegate.treeItemForStyle === &quot;function&quot;) {
+                let selectorText = this.representedObject.ownerRule.selectorText;
+                let existingRules = this.representedObject.nodeStyles.rulesForSelector(selectorText + pseudoElementSelector);
+                if (existingRules.length) {
+                    // There shouldn't really ever be more than one pseudo-element rule
+                    // that is not in a media query. As such, just focus the first rule
+                    // on the assumption that it is the only one necessary.
+                    existingTreeItem = this._delegate.treeItemForStyle(existingRules[0].style);
+                }
+            }
+
+            let title = existingTreeItem ? WebInspector.UIString(&quot;Select %s Rule&quot;) : WebInspector.UIString(&quot;Create %s Rule&quot;);
+            contextMenu.appendItem(title.format(pseudoElementSelector), () =&gt; {
+                if (existingTreeItem) {
+                    existingTreeItem.select(true, true);
+                    return;
+                }
+
+                let pseudoSelectors = this.representedObject.ownerRule.selectors.map((selector) =&gt; selector.text + pseudoElementSelector);
+                this.representedObject.nodeStyles.addRule(pseudoSelectors.join(&quot;, &quot;), styleText);
</ins><span class="cx">             });
</span><span class="cx">         }
</span><span class="cx">     }
</span></span></pre>
</div>
</div>

</body>
</html>