Web Inspector: Convert CSSRule selectorText setter to setSelectorText method because it's asynchronous
https://bugs.webkit.org/show_bug.cgi?id=202840

Reviewed by Matt Baker.

* UserInterface/Models/CSSRule.js:
(WI.CSSRule.prototype.setSelectorText):
(WI.CSSRule.prototype._selectorRejected):
(WI.CSSRule.prototype._selectorResolved):
Remove WI.CSSRule.Event.SelectorChanged event and since it wasn't used anywhere else.

(WI.CSSRule):
(WI.CSSRule.prototype.set selectorText): Deleted.
(WI.CSSRule.prototype.setSelectorText): Added.
(WI.CSSRule.prototype._selectorRejected): Deleted.
(WI.CSSRule.Event.SelectorChanged): Deleted.
Remove `{valid: ...}` object since it wasn't used.

* UserInterface/Views/SpreadsheetCSSStyleDeclarationSection.js:
(WI.SpreadsheetCSSStyleDeclarationSection.prototype.spreadsheetSelectorFieldDidCommit):


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@251168 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebInspectorUI/ChangeLog b/Source/WebInspectorUI/ChangeLog
index 9fa2e7a..47e7df2 100644
--- a/Source/WebInspectorUI/ChangeLog
+++ b/Source/WebInspectorUI/ChangeLog
@@ -1,3 +1,26 @@
+2019-10-15  Nikita Vasilyev  <nvasilyev@apple.com>
+
+        Web Inspector: Convert CSSRule selectorText setter to setSelectorText method because it's asynchronous
+        https://bugs.webkit.org/show_bug.cgi?id=202840
+
+        Reviewed by Matt Baker.
+
+        * UserInterface/Models/CSSRule.js:
+        (WI.CSSRule.prototype.setSelectorText):
+        (WI.CSSRule.prototype._selectorRejected):
+        (WI.CSSRule.prototype._selectorResolved):
+        Remove WI.CSSRule.Event.SelectorChanged event and since it wasn't used anywhere else.
+
+        (WI.CSSRule):
+        (WI.CSSRule.prototype.set selectorText): Deleted.
+        (WI.CSSRule.prototype.setSelectorText): Added.
+        (WI.CSSRule.prototype._selectorRejected): Deleted.
+        (WI.CSSRule.Event.SelectorChanged): Deleted.
+        Remove `{valid: ...}` object since it wasn't used.
+
+        * UserInterface/Views/SpreadsheetCSSStyleDeclarationSection.js:
+        (WI.SpreadsheetCSSStyleDeclarationSection.prototype.spreadsheetSelectorFieldDidCommit):
+
 2019-10-15  Devin Rousso  <drousso@apple.com>
 
         Web Inspector: Local Resource Overrides: automatically create an image/font local override when dragging content over a non-overridden resource
diff --git a/Source/WebInspectorUI/UserInterface/Models/CSSRule.js b/Source/WebInspectorUI/UserInterface/Models/CSSRule.js
index ea39072..77665b9 100644
--- a/Source/WebInspectorUI/UserInterface/Models/CSSRule.js
+++ b/Source/WebInspectorUI/UserInterface/Models/CSSRule.js
@@ -62,13 +62,13 @@
         return this._selectorText;
     }
 
-    set selectorText(selectorText)
+    setSelectorText(selectorText)
     {
         console.assert(this.editable);
         if (!this.editable)
-            return;
+            return Promise.reject();
 
-        this._nodeStyles.changeRuleSelector(this, selectorText).then(this._selectorResolved.bind(this), this._selectorRejected.bind(this));
+        return this._nodeStyles.changeRuleSelector(this, selectorText).then(this._selectorResolved.bind(this));
     }
 
     update(sourceCodeLocation, selectorText, selectors, matchedSelectorIndices, style, groupings)
@@ -111,43 +111,36 @@
 
     // Private
 
-    _selectorRejected(error)
-    {
-        this.dispatchEventToListeners(WI.CSSRule.Event.SelectorChanged, {valid: !error});
-    }
-
+    // This method only needs to be called for CSS rules that don't match the selected DOM node.
+    // CSS rules that match the selected DOM node get updated by WI.DOMNodeStyles.prototype._parseRulePayload.
     _selectorResolved(rulePayload)
     {
-        if (rulePayload) {
-            let selectorText = rulePayload.selectorList.text;
-            if (selectorText !== this._selectorText) {
-                let selectors = WI.DOMNodeStyles.parseSelectorListPayload(rulePayload.selectorList);
+        if (!rulePayload)
+            return;
 
-                let sourceCodeLocation = null;
-                let sourceRange = rulePayload.selectorList.range;
-                if (sourceRange) {
-                    sourceCodeLocation = WI.DOMNodeStyles.createSourceCodeLocation(rulePayload.sourceURL, {
-                        line: sourceRange.startLine,
-                        column: sourceRange.startColumn,
-                        documentNode: this._nodeStyles.node.ownerDocument,
-                    });
-                }
+        let selectorText = rulePayload.selectorList.text;
+        if (selectorText === this._selectorText)
+            return;
 
-                if (this._ownerStyleSheet) {
-                    if (!sourceCodeLocation && this._ownerStyleSheet.isInspectorStyleSheet())
-                        sourceCodeLocation = this._ownerStyleSheet.createSourceCodeLocation(sourceRange.startLine, sourceRange.startColumn);
+        let selectors = WI.DOMNodeStyles.parseSelectorListPayload(rulePayload.selectorList);
 
-                    sourceCodeLocation = this._ownerStyleSheet.offsetSourceCodeLocation(sourceCodeLocation);
-                }
-
-                this.update(sourceCodeLocation, selectorText, selectors, [], this._style, this._groupings);
-            }
+        let sourceCodeLocation = null;
+        let sourceRange = rulePayload.selectorList.range;
+        if (sourceRange) {
+            sourceCodeLocation = WI.DOMNodeStyles.createSourceCodeLocation(rulePayload.sourceURL, {
+                line: sourceRange.startLine,
+                column: sourceRange.startColumn,
+                documentNode: this._nodeStyles.node.ownerDocument,
+            });
         }
 
-        this.dispatchEventToListeners(WI.CSSRule.Event.SelectorChanged, {valid: !!rulePayload});
-    }
-};
+        if (this._ownerStyleSheet) {
+            if (!sourceCodeLocation && this._ownerStyleSheet.isInspectorStyleSheet())
+                sourceCodeLocation = this._ownerStyleSheet.createSourceCodeLocation(sourceRange.startLine, sourceRange.startColumn);
 
-WI.CSSRule.Event = {
-    SelectorChanged: "css-rule-invalid-selector"
+            sourceCodeLocation = this._ownerStyleSheet.offsetSourceCodeLocation(sourceCodeLocation);
+        }
+
+        this.update(sourceCodeLocation, selectorText, selectors, [], this._style, this._groupings);
+    }
 };
diff --git a/Source/WebInspectorUI/UserInterface/Views/SpreadsheetCSSStyleDeclarationSection.js b/Source/WebInspectorUI/UserInterface/Views/SpreadsheetCSSStyleDeclarationSection.js
index a2c586d..d889f4b 100644
--- a/Source/WebInspectorUI/UserInterface/Views/SpreadsheetCSSStyleDeclarationSection.js
+++ b/Source/WebInspectorUI/UserInterface/Views/SpreadsheetCSSStyleDeclarationSection.js
@@ -231,8 +231,7 @@
         let selectorText = this._selectorElement.textContent.trim();
         if (selectorText && changed) {
             this.dispatchEventToListeners(WI.SpreadsheetCSSStyleDeclarationSection.Event.SelectorWillChange);
-            this._style.ownerRule.singleFireEventListener(WI.CSSRule.Event.SelectorChanged, this._renderSelector, this);
-            this._style.ownerRule.selectorText = selectorText;
+            this._style.ownerRule.setSelectorText(selectorText).finally(this._renderSelector.bind(this));
         } else
             this._discardSelectorChange();
     }