Web Inspector: Adding new rules broken if a <style> tag is added to document dynamically
https://bugs.webkit.org/show_bug.cgi?id=111299

Reviewed by Pavel Feldman.

Source/WebCore:

The CSSStyleSheet instance should be retrieved directly from the HTMLStyleElement just created,
not from the document.styleSheets list.

Test: inspector/styles/add-new-rule-with-style-after-body.html

* inspector/InspectorCSSAgent.cpp:
(WebCore::InspectorCSSAgent::viaInspectorStyleSheet):

LayoutTests:

* inspector/styles/add-new-rule-with-style-after-body-expected.txt: Added.
* inspector/styles/add-new-rule-with-style-after-body.html: Added.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@144622 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/inspector/InspectorCSSAgent.cpp b/Source/WebCore/inspector/InspectorCSSAgent.cpp
index 51996a3..934d09d1 100644
--- a/Source/WebCore/inspector/InspectorCSSAgent.cpp
+++ b/Source/WebCore/inspector/InspectorCSSAgent.cpp
@@ -40,6 +40,7 @@
 #include "DOMWindow.h"
 #include "ExceptionCodePlaceholder.h"
 #include "HTMLHeadElement.h"
+#include "HTMLStyleElement.h"
 #include "InspectorDOMAgent.h"
 #include "InspectorHistory.h"
 #include "InspectorState.h"
@@ -1080,11 +1081,16 @@
     }
     if (ec)
         return 0;
-    StyleSheetList* styleSheets = document->styleSheets();
-    StyleSheet* styleSheet = styleSheets->item(styleSheets->length() - 1);
-    if (!styleSheet || !styleSheet->isCSSStyleSheet())
+
+    HTMLElement* htmlElement = toHTMLElement(styleElement.get());
+    if (!htmlElement || !htmlElement->hasTagName(HTMLNames::styleTag))
         return 0;
-    CSSStyleSheet* cssStyleSheet = static_cast<CSSStyleSheet*>(styleSheet);
+
+    HTMLStyleElement* htmlStyleElement = static_cast<HTMLStyleElement*>(htmlElement);
+    CSSStyleSheet* cssStyleSheet = htmlStyleElement->sheet();
+    if (!cssStyleSheet)
+        return 0;
+
     String id = String::number(m_lastStyleSheetId++);
     inspectorStyleSheet = InspectorStyleSheet::create(m_domAgent->pageAgent(), id, cssStyleSheet, TypeBuilder::CSS::StyleSheetOrigin::Inspector, InspectorDOMAgent::documentURLString(document), this);
     m_idToInspectorStyleSheet.set(id, inspectorStyleSheet);