Reviewed by Hyatt.

        Refactor of change for <rdar://problem/5404899> REGRESSION: Mail 
        crash in WebCore::FontFallbackList::fontDataAt() after dragging 
        image into text multiple times

        The original fix that I made last night prevents the pending style 
        sheet count from being incremented until the element is in the 
        document. This fix prevents the style sheet from loading at all 
        until it is in the document.

        Here is the fix.
        * dom/StyleElement.cpp:
        (WebCore::StyleElement::insertedIntoDocument): Call process.
        (WebCore::StyleElement::removedFromDocument): This can be reverted 
        to its original state before my patch last night.
        (WebCore::StyleElement::process): childrenChanged is now called 
        process. Return early if your not in the document.
        (WebCore::StyleElement::createSheet): Revert change from last 
        night. The inDocument check is now in caller childrenChanged.
        * dom/StyleElement.h: insertedIntoDocument() must now accept an 
        element in addition to a document.

        This is an optimization to prevent calling updateStyleSelector() 
        too frequently.
        * dom/XMLTokenizer.cpp:
        (WebCore::XMLTokenizer::startElementNs):
        * html/HTMLStyleElement.cpp:
        (WebCore::HTMLStyleElement::HTMLStyleElement):
        (WebCore::HTMLStyleElement::finishedParsing):
        (WebCore::HTMLStyleElement::insertedIntoDocument):
        (WebCore::HTMLStyleElement::childrenChanged):
        (WebCore::HTMLStyleElement::sheetLoaded):
        * html/HTMLStyleElement.h:
        * ksvg2/svg/SVGStyleElement.cpp:
        (WebCore::SVGStyleElement::SVGStyleElement):
        (WebCore::SVGStyleElement::finishedParsing):
        (WebCore::SVGStyleElement::insertedIntoDocument):
        (WebCore::SVGStyleElement::childrenChanged):
        (WebCore::SVGStyleElement::sheetLoaded):
        * ksvg2/svg/SVGStyleElement.h:
        (WebCore::SVGStyleElement::setCreatedByParser):

        This is a name change. Document::stylesheetLoaded() 
        is now Document::removePendingSheet()
        * dom/Document.cpp:
        (WebCore::Document::removePendingSheet):
        * dom/Document.h:
        * dom/ProcessingInstruction.cpp:
        (WebCore::ProcessingInstruction::sheetLoaded):
        * html/HTMLLinkElement.cpp:
        (WebCore::HTMLLinkElement::~HTMLLinkElement):
        (WebCore::HTMLLinkElement::setDisabledState):
        (WebCore::HTMLLinkElement::process):
        (WebCore::HTMLLinkElement::sheetLoaded):
        * page/Frame.cpp:
        (WebCore::UserStyleSheetLoader::~UserStyleSheetLoader):
        (WebCore::UserStyleSheetLoader::setCSSStyleSheet):

        This is another name change. closeRenderer() is now 
        finishedParsing()
        * dom/Node.h:
        (WebCore::Node::finishedParsing):
        * dom/XMLTokenizer.cpp:
        (WebCore::XMLTokenizer::endElementNs):
        (WebCore::):
        * html/HTMLAppletElement.cpp:
        (WebCore::HTMLAppletElement::finishedParsing):
        * html/HTMLAppletElement.h:
        * html/HTMLGenericFormElement.cpp:
        (WebCore::HTMLFormControlElementWithState::finishedParsing):
        * html/HTMLGenericFormElement.h:
        * html/HTMLObjectElement.cpp:
        (WebCore::HTMLObjectElement::finishedParsing):
        * html/HTMLObjectElement.h:
        * html/HTMLParser.cpp:
        (WebCore::HTMLParser::insertNode):
        (WebCore::HTMLParser::popOneBlockCommon):
        * html/HTMLScriptElement.cpp:
        (WebCore::HTMLScriptElement::finishedParsing):
        * html/HTMLScriptElement.h:
        (WebCore::HTMLStyleElement::setCreatedByParser):
        * ksvg2/svg/SVGAnimationElement.cpp:
        (WebCore::SVGAnimationElement::finishedParsing):
        * ksvg2/svg/SVGAnimationElement.h:
        * ksvg2/svg/SVGElement.cpp:
        (WebCore::SVGElement::finishedParsing):
        * ksvg2/svg/SVGElement.h:



git-svn-id: http://svn.webkit.org/repository/webkit/trunk@25082 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/WebCore/ChangeLog b/WebCore/ChangeLog
index 0a36311..184664f 100644
--- a/WebCore/ChangeLog
+++ b/WebCore/ChangeLog
@@ -1,3 +1,94 @@
+2007-08-14  Beth Dakin  <bdakin@apple.com>
+
+        Reviewed by Hyatt.
+
+        Refactor of change for <rdar://problem/5404899> REGRESSION: Mail 
+        crash in WebCore::FontFallbackList::fontDataAt() after dragging 
+        image into text multiple times
+
+        The original fix that I made last night prevents the pending style 
+        sheet count from being incremented until the element is in the 
+        document. This fix prevents the style sheet from loading at all 
+        until it is in the document.
+
+        Here is the fix.
+        * dom/StyleElement.cpp:
+        (WebCore::StyleElement::insertedIntoDocument): Call process.
+        (WebCore::StyleElement::removedFromDocument): This can be reverted 
+        to its original state before my patch last night.
+        (WebCore::StyleElement::process): childrenChanged is now called 
+        process. Return early if your not in the document.
+        (WebCore::StyleElement::createSheet): Revert change from last 
+        night. The inDocument check is now in caller childrenChanged.
+        * dom/StyleElement.h: insertedIntoDocument() must now accept an 
+        element in addition to a document.
+
+        This is an optimization to prevent calling updateStyleSelector() 
+        too frequently.
+        * dom/XMLTokenizer.cpp:
+        (WebCore::XMLTokenizer::startElementNs):
+        * html/HTMLStyleElement.cpp:
+        (WebCore::HTMLStyleElement::HTMLStyleElement):
+        (WebCore::HTMLStyleElement::finishedParsing):
+        (WebCore::HTMLStyleElement::insertedIntoDocument):
+        (WebCore::HTMLStyleElement::childrenChanged):
+        (WebCore::HTMLStyleElement::sheetLoaded):
+        * html/HTMLStyleElement.h:
+        * ksvg2/svg/SVGStyleElement.cpp:
+        (WebCore::SVGStyleElement::SVGStyleElement):
+        (WebCore::SVGStyleElement::finishedParsing):
+        (WebCore::SVGStyleElement::insertedIntoDocument):
+        (WebCore::SVGStyleElement::childrenChanged):
+        (WebCore::SVGStyleElement::sheetLoaded):
+        * ksvg2/svg/SVGStyleElement.h:
+        (WebCore::SVGStyleElement::setCreatedByParser):
+
+        This is a name change. Document::stylesheetLoaded() 
+        is now Document::removePendingSheet()
+        * dom/Document.cpp:
+        (WebCore::Document::removePendingSheet):
+        * dom/Document.h:
+        * dom/ProcessingInstruction.cpp:
+        (WebCore::ProcessingInstruction::sheetLoaded):
+        * html/HTMLLinkElement.cpp:
+        (WebCore::HTMLLinkElement::~HTMLLinkElement):
+        (WebCore::HTMLLinkElement::setDisabledState):
+        (WebCore::HTMLLinkElement::process):
+        (WebCore::HTMLLinkElement::sheetLoaded):
+        * page/Frame.cpp:
+        (WebCore::UserStyleSheetLoader::~UserStyleSheetLoader):
+        (WebCore::UserStyleSheetLoader::setCSSStyleSheet):
+
+        This is another name change. closeRenderer() is now 
+        finishedParsing()
+        * dom/Node.h:
+        (WebCore::Node::finishedParsing):
+        * dom/XMLTokenizer.cpp:
+        (WebCore::XMLTokenizer::endElementNs):
+        (WebCore::):
+        * html/HTMLAppletElement.cpp:
+        (WebCore::HTMLAppletElement::finishedParsing):
+        * html/HTMLAppletElement.h:
+        * html/HTMLGenericFormElement.cpp:
+        (WebCore::HTMLFormControlElementWithState::finishedParsing):
+        * html/HTMLGenericFormElement.h:
+        * html/HTMLObjectElement.cpp:
+        (WebCore::HTMLObjectElement::finishedParsing):
+        * html/HTMLObjectElement.h:
+        * html/HTMLParser.cpp:
+        (WebCore::HTMLParser::insertNode):
+        (WebCore::HTMLParser::popOneBlockCommon):
+        * html/HTMLScriptElement.cpp:
+        (WebCore::HTMLScriptElement::finishedParsing):
+        * html/HTMLScriptElement.h:
+        (WebCore::HTMLStyleElement::setCreatedByParser):
+        * ksvg2/svg/SVGAnimationElement.cpp:
+        (WebCore::SVGAnimationElement::finishedParsing):
+        * ksvg2/svg/SVGAnimationElement.h:
+        * ksvg2/svg/SVGElement.cpp:
+        (WebCore::SVGElement::finishedParsing):
+        * ksvg2/svg/SVGElement.h:
+
 2007-08-14  Brady Eidson  <beidson@apple.com>
 
         Reviewed by Darin, John, Maciej, Oliver, and Tim
diff --git a/WebCore/dom/Document.cpp b/WebCore/dom/Document.cpp
index 4944b13..469b6f1 100644
--- a/WebCore/dom/Document.cpp
+++ b/WebCore/dom/Document.cpp
@@ -1936,7 +1936,7 @@
 }
 
 // This method is called whenever a top-level stylesheet has finished loading.
-void Document::stylesheetLoaded()
+void Document::removePendingSheet()
 {
     // Make sure we knew this sheet was pending, and that our count isn't out of sync.
     ASSERT(m_pendingStylesheets > 0);
diff --git a/WebCore/dom/Document.h b/WebCore/dom/Document.h
index 6d07fb7..bf31994 100644
--- a/WebCore/dom/Document.h
+++ b/WebCore/dom/Document.h
@@ -251,7 +251,7 @@
     /**
      * Updates the pending sheet count and then calls updateStyleSelector.
      */
-    void stylesheetLoaded();
+    void removePendingSheet();
 
     /**
      * This method returns true if all top-level stylesheets have loaded (including
diff --git a/WebCore/dom/Node.h b/WebCore/dom/Node.h
index 863ba10..40625a8 100644
--- a/WebCore/dom/Node.h
+++ b/WebCore/dom/Node.h
@@ -208,7 +208,7 @@
     // until they know all of their nested <param>s. [Radar 3603191, 4040848].
     // Also used for script elements and some SVG elements for similar purposes,
     // but making parsing a special case in this respect should be avoided if possible.
-    virtual void closeRenderer() { }
+    virtual void finishedParsing() { }
 
     // Called by the frame right before dispatching an unloadEvent. [Radar 4532113]
     // This is needed for HTMLInputElements to tell the frame that it is done editing 
diff --git a/WebCore/dom/ProcessingInstruction.cpp b/WebCore/dom/ProcessingInstruction.cpp
index d6d009e..39af5b8 100644
--- a/WebCore/dom/ProcessingInstruction.cpp
+++ b/WebCore/dom/ProcessingInstruction.cpp
@@ -191,7 +191,7 @@
 bool ProcessingInstruction::sheetLoaded()
 {
     if (!isLoading()) {
-        document()->stylesheetLoaded();
+        document()->removePendingSheet();
         return true;
     }
     return false;
diff --git a/WebCore/dom/StyleElement.cpp b/WebCore/dom/StyleElement.cpp
index b765c94..10e4297 100644
--- a/WebCore/dom/StyleElement.cpp
+++ b/WebCore/dom/StyleElement.cpp
@@ -40,30 +40,22 @@
     return m_sheet.get();
 }
 
-void StyleElement::insertedIntoDocument(Document* document)
+void StyleElement::insertedIntoDocument(Document* document, Element* element)
 {
-    if (m_sheet) {
-        if (static_cast<CSSStyleSheet *>(m_sheet.get())->isLoading()
-            && (m_sheet->type().isEmpty() || m_sheet->type() == "text/css"))
-            document->addPendingSheet();
-        document->updateStyleSelector();
-    }
+    process(element);
 }
 
 void StyleElement::removedFromDocument(Document* document)
 {
-    if (m_sheet) {
-        if (static_cast<CSSStyleSheet *>(m_sheet.get())->isLoading()
-            && (m_sheet->type().isEmpty() || m_sheet->type() == "text/css"))
-            document->stylesheetLoaded();
+    if (m_sheet)
         document->updateStyleSelector();
-    }
 }
 
-void StyleElement::childrenChanged(Element* e)
+void StyleElement::process(Element* e)
 {
-    if (!e)
+    if (!e || !e->inDocument())
         return;
+
     String text = "";
 
     for (Node* c = e->firstChild(); c; c = c->nextSibling())
@@ -77,8 +69,8 @@
 {
     Document* document = e->document();
     if (m_sheet) {
-        if (static_cast<CSSStyleSheet *>(m_sheet.get())->isLoading())
-            document->stylesheetLoaded(); // Remove ourselves from the sheet list.
+        if (static_cast<CSSStyleSheet*>(m_sheet.get())->isLoading())
+            document->removePendingSheet();
         m_sheet = 0;
     }
 
@@ -88,8 +80,7 @@
         MediaQueryEvaluator screenEval("screen", true);
         MediaQueryEvaluator printEval("print", true);
         if (screenEval.eval(mediaList.get()) || printEval.eval(mediaList.get())) {
-            if (e->inDocument())
-                document->addPendingSheet();
+            document->addPendingSheet();
             setLoading(true);
             m_sheet = new CSSStyleSheet(e, String(), document->inputEncoding());
             m_sheet->parseString(text, !document->inCompatMode());
@@ -99,7 +90,7 @@
         }
     }
 
-    if (m_sheet && e->inDocument())
+    if (m_sheet)
         m_sheet->checkLoaded();
 }
 
diff --git a/WebCore/dom/StyleElement.h b/WebCore/dom/StyleElement.h
index 8b90ba7..a637d2d 100644
--- a/WebCore/dom/StyleElement.h
+++ b/WebCore/dom/StyleElement.h
@@ -41,9 +41,9 @@
     virtual const AtomicString& type() const = 0;
     virtual const AtomicString& media() const = 0;
 
-    void insertedIntoDocument(Document*);
+    void insertedIntoDocument(Document*, Element*);
     void removedFromDocument(Document*);
-    void childrenChanged(Element*);
+    void process(Element*);
 
     void createSheet(Element* e, const String& text = String());
 
diff --git a/WebCore/dom/XMLTokenizer.cpp b/WebCore/dom/XMLTokenizer.cpp
index 9aaa926..ec098a4 100644
--- a/WebCore/dom/XMLTokenizer.cpp
+++ b/WebCore/dom/XMLTokenizer.cpp
@@ -41,12 +41,14 @@
 #include "FrameView.h"
 #include "HTMLNames.h"
 #include "HTMLScriptElement.h"
+#include "HTMLStyleElement.h"
 #include "HTMLTableSectionElement.h"
 #include "HTMLTokenizer.h"
 #include "ProcessingInstruction.h"
 #include "ResourceHandle.h"
 #include "ResourceRequest.h"
 #include "ResourceResponse.h"
+#include "SVGStyleElement.h"
 #ifndef USE_QXMLSTREAM
 #include <libxml/parser.h>
 #include <libxml/parserInternals.h>
@@ -718,6 +720,14 @@
 
     if (newElement->hasTagName(scriptTag))
         static_cast<HTMLScriptElement*>(newElement.get())->setCreatedByParser(true);
+    else if (newElement->hasTagName(HTMLNames::styleTag))
+        static_cast<HTMLStyleElement*>(newElement.get())->setCreatedByParser(true);
+#if ENABLE(SVG)
+    else if (newElement->hasTagName(SVGNames::scriptTag))
+        static_cast<HTMLScriptElement*>(newElement.get())->setCreatedByParser(true);
+    else if (newElement->hasTagName(SVGNames::styleTag))
+        static_cast<SVGStyleElement*>(newElement.get())->setCreatedByParser(true);
+#endif
     
     if (newElement->hasTagName(HTMLNames::scriptTag)
 #if ENABLE(SVG)
@@ -750,7 +760,7 @@
 
     Node* n = m_currentNode;
     RefPtr<Node> parent = n->parentNode();
-    n->closeRenderer();
+    n->finishedParsing();
     
     // don't load external scripts for standalone documents (for now)
     if (n->isElementNode() && m_view && (static_cast<Element*>(n)->hasTagName(scriptTag) 
@@ -1773,7 +1783,7 @@
 
     Node* n = m_currentNode;
     RefPtr<Node> parent = n->parentNode();
-    n->closeRenderer();
+    n->finishedParsing();
 
     // don't load external scripts for standalone documents (for now)
     if (n->isElementNode() && m_view && (static_cast<Element*>(n)->hasTagName(scriptTag) 
diff --git a/WebCore/html/HTMLAppletElement.cpp b/WebCore/html/HTMLAppletElement.cpp
index 9a4c2f6..982fddf 100644
--- a/WebCore/html/HTMLAppletElement.cpp
+++ b/WebCore/html/HTMLAppletElement.cpp
@@ -159,13 +159,13 @@
 }
 #endif
 
-void HTMLAppletElement::closeRenderer()
+void HTMLAppletElement::finishedParsing()
 {
     // The parser just reached </applet>, so all the params are available now.
     m_allParamsAvailable = true;
     if (renderer())
         renderer()->setNeedsLayout(true); // This will cause it to create its widget & the Java applet
-    HTMLPlugInElement::closeRenderer();
+    HTMLPlugInElement::finishedParsing();
 }
 
 void HTMLAppletElement::detach()
diff --git a/WebCore/html/HTMLAppletElement.h b/WebCore/html/HTMLAppletElement.h
index 43b8f9d..0c3ac0d 100644
--- a/WebCore/html/HTMLAppletElement.h
+++ b/WebCore/html/HTMLAppletElement.h
@@ -50,7 +50,7 @@
     
     virtual bool rendererIsNeeded(RenderStyle*);
     virtual RenderObject* createRenderer(RenderArena*, RenderStyle*);
-    virtual void closeRenderer();
+    virtual void finishedParsing();
     virtual void detach();
     
 #if USE(JAVASCRIPTCORE_BINDINGS)
diff --git a/WebCore/html/HTMLGenericFormElement.cpp b/WebCore/html/HTMLGenericFormElement.cpp
index fcb3ada..18ac7cb 100644
--- a/WebCore/html/HTMLGenericFormElement.cpp
+++ b/WebCore/html/HTMLGenericFormElement.cpp
@@ -230,7 +230,7 @@
     HTMLGenericFormElement::didMoveToNewOwnerDocument();
 }
 
-void HTMLFormControlElementWithState::closeRenderer()
+void HTMLFormControlElementWithState::finishedParsing()
 {
     Document* doc = document();
     if (doc->hasStateForNewFormElements()) {
diff --git a/WebCore/html/HTMLGenericFormElement.h b/WebCore/html/HTMLGenericFormElement.h
index db2e85f..f0f731b 100644
--- a/WebCore/html/HTMLGenericFormElement.h
+++ b/WebCore/html/HTMLGenericFormElement.h
@@ -106,7 +106,7 @@
     HTMLFormControlElementWithState(const QualifiedName& tagName, Document*, HTMLFormElement*);
     virtual ~HTMLFormControlElementWithState();
 
-    virtual void closeRenderer();
+    virtual void finishedParsing();
 
     virtual bool saveState(String& value) const = 0;
 
diff --git a/WebCore/html/HTMLLinkElement.cpp b/WebCore/html/HTMLLinkElement.cpp
index faf2cca..c9c19ae 100644
--- a/WebCore/html/HTMLLinkElement.cpp
+++ b/WebCore/html/HTMLLinkElement.cpp
@@ -55,7 +55,7 @@
     if (m_cachedSheet) {
         m_cachedSheet->deref(this);
         if (m_loading && !isDisabled() && !isAlternate())
-            document()->stylesheetLoaded();
+            document()->removePendingSheet();
     }
 }
 
@@ -71,7 +71,7 @@
             // a main sheet or a sheet that was previously enabled via script, then we need
             // to remove it from the list of pending sheets.
             if (m_disabledState == 2 && (!m_alternate || oldDisabledState == 1))
-                document()->stylesheetLoaded();
+                document()->removePendingSheet();
 
             // Check #2: An alternate sheet becomes enabled while it is still loading.
             if (m_alternate && m_disabledState == 1)
@@ -186,7 +186,7 @@
             
             if (m_cachedSheet) {
                 if (m_loading)
-                    document()->stylesheetLoaded();
+                    document()->removePendingSheet();
                 m_cachedSheet->deref(this);
             }
             m_loading = true;
@@ -195,7 +195,7 @@
                 m_cachedSheet->ref(this);
             else if (!isAlternate()) { // request may have been denied if stylesheet is local and document is remote.
                 m_loading = false;
-                document()->stylesheetLoaded();
+                document()->removePendingSheet();
             }
         }
     } else if (m_sheet) {
@@ -242,7 +242,7 @@
 bool HTMLLinkElement::sheetLoaded()
 {
     if (!isLoading() && !isDisabled() && !isAlternate()) {
-        document()->stylesheetLoaded();
+        document()->removePendingSheet();
         return true;
     }
     return false;
diff --git a/WebCore/html/HTMLObjectElement.cpp b/WebCore/html/HTMLObjectElement.cpp
index 545ca42..a49f258 100644
--- a/WebCore/html/HTMLObjectElement.cpp
+++ b/WebCore/html/HTMLObjectElement.cpp
@@ -193,12 +193,12 @@
     }
 }
 
-void HTMLObjectElement::closeRenderer()
+void HTMLObjectElement::finishedParsing()
 {
     // The parser just reached </object>.
     setComplete(true);
     
-    HTMLPlugInElement::closeRenderer();
+    HTMLPlugInElement::finishedParsing();
 }
 
 void HTMLObjectElement::setComplete(bool complete)
diff --git a/WebCore/html/HTMLObjectElement.h b/WebCore/html/HTMLObjectElement.h
index 5b9baa7..5783f72 100644
--- a/WebCore/html/HTMLObjectElement.h
+++ b/WebCore/html/HTMLObjectElement.h
@@ -45,7 +45,7 @@
     virtual void attach();
     virtual bool rendererIsNeeded(RenderStyle*);
     virtual RenderObject* createRenderer(RenderArena*, RenderStyle*);
-    virtual void closeRenderer();
+    virtual void finishedParsing();
     virtual void detach();
     virtual void insertedIntoDocument();
     virtual void removedFromDocument();
diff --git a/WebCore/html/HTMLParser.cpp b/WebCore/html/HTMLParser.cpp
index b58d473..c4b222f 100644
--- a/WebCore/html/HTMLParser.cpp
+++ b/WebCore/html/HTMLParser.cpp
@@ -326,7 +326,7 @@
     } else {
         if (parentAttached && !n->attached() && !m_isParsingFragment)
             n->attach();
-        n->closeRenderer();
+        n->finishedParsing();
     }
 
     return true;
@@ -1297,7 +1297,7 @@
     // Form elements restore their state during the parsing process.
     // Also, a few elements (<applet>, <object>) need to know when all child elements (<param>s) are available.
     if (current && elem->node != current)
-        current->closeRenderer();
+        current->finishedParsing();
 
     blockStack = elem->next;
     current = elem->node;
diff --git a/WebCore/html/HTMLScriptElement.cpp b/WebCore/html/HTMLScriptElement.cpp
index 75caea7..7253a34 100644
--- a/WebCore/html/HTMLScriptElement.cpp
+++ b/WebCore/html/HTMLScriptElement.cpp
@@ -92,13 +92,13 @@
         HTMLElement::parseMappedAttribute(attr);
 }
 
-void HTMLScriptElement::closeRenderer()
+void HTMLScriptElement::finishedParsing()
 {
     // The parser just reached </script>. If we have no src and no text,
     // allow dynamic loading later.
     if (getAttribute(srcAttr).isEmpty() && text().isEmpty())
         setCreatedByParser(false);
-    HTMLElement::closeRenderer();
+    HTMLElement::finishedParsing();
 }
 
 void HTMLScriptElement::insertedIntoDocument()
diff --git a/WebCore/html/HTMLScriptElement.h b/WebCore/html/HTMLScriptElement.h
index 0c20e88..52a8422 100644
--- a/WebCore/html/HTMLScriptElement.h
+++ b/WebCore/html/HTMLScriptElement.h
@@ -51,7 +51,7 @@
     virtual bool isURLAttribute(Attribute *attr) const;
 
     void setCreatedByParser(bool createdByParser) { m_createdByParser = createdByParser; }
-    virtual void closeRenderer();
+    virtual void finishedParsing();
 
     bool shouldExecuteAsJavaScript();
     void evaluateScript(const String &URL, const String &script);
diff --git a/WebCore/html/HTMLStyleElement.cpp b/WebCore/html/HTMLStyleElement.cpp
index 0833f21..99b88cd 100644
--- a/WebCore/html/HTMLStyleElement.cpp
+++ b/WebCore/html/HTMLStyleElement.cpp
@@ -35,6 +35,7 @@
 HTMLStyleElement::HTMLStyleElement(Document* doc)
     : HTMLElement(styleTag, doc)
     , m_loading(false)
+    , m_createdByParser(false)
 {
 }
 
@@ -49,10 +50,18 @@
         HTMLElement::parseMappedAttribute(attr);
 }
 
+void HTMLStyleElement::finishedParsing()
+{
+    StyleElement::process(this);
+    HTMLElement::finishedParsing();
+}
+
 void HTMLStyleElement::insertedIntoDocument()
 {
     HTMLElement::insertedIntoDocument();
-    StyleElement::insertedIntoDocument(document());
+
+    if (!m_createdByParser)
+        StyleElement::insertedIntoDocument(document(), this);
 }
 
 void HTMLStyleElement::removedFromDocument()
@@ -63,7 +72,7 @@
 
 void HTMLStyleElement::childrenChanged()
 {
-    StyleElement::childrenChanged(this);
+    StyleElement::process(this);
 }
 
 StyleSheet* HTMLStyleElement::sheet()
@@ -83,7 +92,7 @@
 bool HTMLStyleElement::sheetLoaded()
 {
     if (!isLoading()) {
-        document()->stylesheetLoaded();
+        document()->removePendingSheet();
         return true;
     }
     return false;
diff --git a/WebCore/html/HTMLStyleElement.h b/WebCore/html/HTMLStyleElement.h
index 29593d2..6a6a910 100644
--- a/WebCore/html/HTMLStyleElement.h
+++ b/WebCore/html/HTMLStyleElement.h
@@ -45,6 +45,9 @@
     virtual void removedFromDocument();
     virtual void childrenChanged();
 
+    void setCreatedByParser(bool createdByParser) { m_createdByParser = createdByParser; }
+    virtual void finishedParsing();
+
     virtual bool isLoading() const;
     virtual bool sheetLoaded();
 
@@ -64,6 +67,7 @@
 protected:
     String m_media;
     bool m_loading;
+    bool m_createdByParser;
 };
 
 } //namespace
diff --git a/WebCore/ksvg2/svg/SVGAnimationElement.cpp b/WebCore/ksvg2/svg/SVGAnimationElement.cpp
index a4e283c..477c61d 100644
--- a/WebCore/ksvg2/svg/SVGAnimationElement.cpp
+++ b/WebCore/ksvg2/svg/SVGAnimationElement.cpp
@@ -411,10 +411,10 @@
     return result;
 }
 
-void SVGAnimationElement::closeRenderer()
+void SVGAnimationElement::finishedParsing()
 {
     ownerSVGElement()->timeScheduler()->addTimer(this, lround(getStartTime()));
-    SVGElement::closeRenderer();
+    SVGElement::finishedParsing();
 }
 
 String SVGAnimationElement::targetAttributeAnimatedValue() const
diff --git a/WebCore/ksvg2/svg/SVGAnimationElement.h b/WebCore/ksvg2/svg/SVGAnimationElement.h
index 57f6940..f849c92 100644
--- a/WebCore/ksvg2/svg/SVGAnimationElement.h
+++ b/WebCore/ksvg2/svg/SVGAnimationElement.h
@@ -98,7 +98,7 @@
     
         virtual void parseMappedAttribute(MappedAttribute* attr);
 
-        virtual void closeRenderer();
+        virtual void finishedParsing();
 
         virtual bool updateAnimationBaseValueFromElement();
         bool updateAnimatedValueForElapsedSeconds(double elapsedSeconds);
diff --git a/WebCore/ksvg2/svg/SVGElement.cpp b/WebCore/ksvg2/svg/SVGElement.cpp
index 90fe360..f8b9096 100644
--- a/WebCore/ksvg2/svg/SVGElement.cpp
+++ b/WebCore/ksvg2/svg/SVGElement.cpp
@@ -172,9 +172,9 @@
     }
 }
 
-void SVGElement::closeRenderer()
+void SVGElement::finishedParsing()
 {
-    // closeRenderer() is called when the close tag is reached for an element (e.g. </svg>)
+    // finishedParsing() is called when the close tag is reached for an element (e.g. </svg>)
     // we send SVGLoad events here if we can, otherwise they'll be sent when any required loads finish
     sendSVGLoadEventIfPossible();
 }
diff --git a/WebCore/ksvg2/svg/SVGElement.h b/WebCore/ksvg2/svg/SVGElement.h
index 4ac0f9c..bcd7dae 100644
--- a/WebCore/ksvg2/svg/SVGElement.h
+++ b/WebCore/ksvg2/svg/SVGElement.h
@@ -213,7 +213,7 @@
         // For SVGTests
         virtual bool isValid() const { return true; }
   
-        virtual void closeRenderer();
+        virtual void finishedParsing();
         virtual bool rendererIsNeeded(RenderStyle*) { return false; }
         virtual bool childShouldCreateRenderer(Node*) const;
 
diff --git a/WebCore/ksvg2/svg/SVGStyleElement.cpp b/WebCore/ksvg2/svg/SVGStyleElement.cpp
index 629b5db..0f4648a 100644
--- a/WebCore/ksvg2/svg/SVGStyleElement.cpp
+++ b/WebCore/ksvg2/svg/SVGStyleElement.cpp
@@ -37,6 +37,7 @@
 
 SVGStyleElement::SVGStyleElement(const QualifiedName& tagName, Document* doc)
      : SVGElement(tagName, doc)
+     , m_createdByParser(false)
 {
 }
 
@@ -92,10 +93,18 @@
         SVGElement::parseMappedAttribute(attr);
 }
 
+void SVGStyleElement::finishedParsing()
+{
+    StyleElement::process(this);
+    SVGElement::finishedParsing();
+}
+
 void SVGStyleElement::insertedIntoDocument()
 {
     SVGElement::insertedIntoDocument();
-    StyleElement::insertedIntoDocument(document());
+
+    if (!m_createdByParser)
+        StyleElement::insertedIntoDocument(document(), this);
 }
 
 void SVGStyleElement::removedFromDocument()
@@ -106,7 +115,7 @@
 
 void SVGStyleElement::childrenChanged()
 {
-    StyleElement::childrenChanged(this);
+    StyleElement::process(this);
 }
 
 StyleSheet* SVGStyleElement::sheet()
@@ -116,7 +125,7 @@
 
 bool SVGStyleElement::sheetLoaded()
 {
-    document()->stylesheetLoaded();
+    document()->removePendingSheet();
     return true;
 }
 
diff --git a/WebCore/ksvg2/svg/SVGStyleElement.h b/WebCore/ksvg2/svg/SVGStyleElement.h
index 4f6c080..cce1970 100644
--- a/WebCore/ksvg2/svg/SVGStyleElement.h
+++ b/WebCore/ksvg2/svg/SVGStyleElement.h
@@ -39,6 +39,9 @@
         virtual void removedFromDocument();
         virtual void childrenChanged();
 
+        void setCreatedByParser(bool createdByParser) { m_createdByParser = createdByParser; }
+        virtual void finishedParsing();
+
         // 'SVGStyleElement' functions
         const AtomicString& xmlspace() const;
         void setXmlspace(const AtomicString&, ExceptionCode&);
@@ -55,6 +58,9 @@
         void setTitle(const AtomicString&, ExceptionCode&);
 
         StyleSheet* sheet();
+        
+    protected:
+        bool m_createdByParser;
     };
 
 } // namespace WebCore
diff --git a/WebCore/page/Frame.cpp b/WebCore/page/Frame.cpp
index d9c18fc..9407d4b 100644
--- a/WebCore/page/Frame.cpp
+++ b/WebCore/page/Frame.cpp
@@ -105,13 +105,13 @@
     ~UserStyleSheetLoader()
     {
         if (!m_cachedSheet->isLoaded())
-            m_document->stylesheetLoaded();        
+            m_document->removePendingSheet();        
         m_cachedSheet->deref(this);
     }
 private:
     virtual void setCSSStyleSheet(const String& /*URL*/, const String& /*charset*/, const String& sheet)
     {
-        m_document->stylesheetLoaded();
+        m_document->removePendingSheet();
         if (Frame* frame = m_document->frame())
             frame->setUserStyleSheet(sheet);
     }