Reviewed by darin.
authorvicki <vicki@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 4 May 2005 21:11:23 +0000 (21:11 +0000)
committervicki <vicki@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 4 May 2005 21:11:23 +0000 (21:11 +0000)
- fixed <rdar://problem/3986228> Not able to load additional script blocks dynamically

Run scripts when they're inserted into the document. Use createdByParser bit to make sure
that scripts aren't run twice, once while parsing and again when inserting.

        * khtml/html/html_headimpl.cpp:
        (HTMLScriptElementImpl::HTMLScriptElementImpl):
        (HTMLScriptElementImpl::~HTMLScriptElementImpl):
        (HTMLScriptElementImpl::insertedIntoDocument):
        (HTMLScriptElementImpl::removedFromDocument):
        (HTMLScriptElementImpl::notifyFinished):
        * khtml/html/html_headimpl.h:
        (DOM::HTMLScriptElementImpl::setCreatedByParser):
        * khtml/html/htmlparser.cpp:
        (KHTMLParser::getElement):
        * khtml/xml/xml_tokenizer.cpp:
        (khtml::XMLTokenizer::startElement):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@9105 268f45cc-cd09-0410-ab3c-d52691b4dbfc

WebCore/ChangeLog-2005-08-23
WebCore/khtml/html/html_headimpl.cpp
WebCore/khtml/html/html_headimpl.h
WebCore/khtml/html/htmlparser.cpp
WebCore/khtml/xml/xml_tokenizer.cpp

index 641e7b6e6c1f7bc70105b503a51f4a1b19f61a98..515bcf49ff7827bb2d4ee01a8b2b3e87c64925ea 100644 (file)
@@ -1,3 +1,25 @@
+2005-05-04  Vicki Murley  <vicki@apple.com>
+
+        Reviewed by darin.
+
+       - fixed <rdar://problem/3986228> Not able to load additional script blocks dynamically
+
+       Run scripts when they're inserted into the document. Use createdByParser bit to make sure
+       that scripts aren't run twice, once while parsing and again when inserting.
+
+        * khtml/html/html_headimpl.cpp:
+        (HTMLScriptElementImpl::HTMLScriptElementImpl):
+        (HTMLScriptElementImpl::~HTMLScriptElementImpl):
+        (HTMLScriptElementImpl::insertedIntoDocument): 
+        (HTMLScriptElementImpl::removedFromDocument): 
+        (HTMLScriptElementImpl::notifyFinished): 
+        * khtml/html/html_headimpl.h:
+        (DOM::HTMLScriptElementImpl::setCreatedByParser):
+        * khtml/html/htmlparser.cpp:
+        (KHTMLParser::getElement): 
+        * khtml/xml/xml_tokenizer.cpp:
+        (khtml::XMLTokenizer::startElement): 
+
 2005-05-03  David Hyatt  <hyatt@apple.com>
 
        Normalize all our custom properties in our implementation to be -khtml (remove all the -apple).
index 87cbf7c51023919d5813b4f7caeb472106e4d867..5c431daec9fa1e8f61cc0de2b240c844aa78fc4d 100644 (file)
@@ -29,6 +29,7 @@
 
 #include "khtmlview.h"
 #include "khtml_part.h"
+#include "kjs_proxy.h"
 
 #include "misc/htmlhashes.h"
 #include "misc/loader.h"
@@ -370,12 +371,78 @@ void HTMLMetaElementImpl::process()
 
 // -------------------------------------------------------------------------
 
-HTMLScriptElementImpl::HTMLScriptElementImpl(DocumentPtr *doc) : HTMLElementImpl(doc)
+HTMLScriptElementImpl::HTMLScriptElementImpl(DocumentPtr *doc)
+    : HTMLElementImpl(doc), m_cachedScript(0), m_createdByParser(false)
 {
 }
 
 HTMLScriptElementImpl::~HTMLScriptElementImpl()
 {
+    if (m_cachedScript)
+        m_cachedScript->deref(this);
+}
+
+void HTMLScriptElementImpl::insertedIntoDocument()
+{
+    HTMLElementImpl::insertedIntoDocument();
+
+    assert(!m_cachedScript);
+
+    if (m_createdByParser)
+        return;
+    
+    QString url = getAttribute(ATTR_SRC).string();
+    if (!url.isEmpty()) {
+        QString charset = getAttribute(ATTR_CHARSET).string();
+        m_cachedScript = getDocument()->docLoader()->requestScript(DOMString(url), charset);
+        m_cachedScript->ref(this);
+        return;
+    }
+
+    DOMString scriptString = "";
+    for (NodeImpl *n = firstChild(); n; n = n->nextSibling())
+        if (n->isTextNode()) 
+            scriptString += static_cast<TextImpl*>(n)->data();
+
+    DocumentImpl *doc = getDocument();
+    KHTMLPart *part = doc->part();
+    if (!part)
+        return;
+    KJSProxy *proxy = KJSProxy::proxy(part);
+    if (!proxy)
+        return;
+
+    proxy->evaluate(doc->URL(), 0, scriptString.string(), Node());
+    DocumentImpl::updateDocumentsRendering();
+}
+
+void HTMLScriptElementImpl::removedFromDocument()
+{
+    HTMLElementImpl::removedFromDocument();
+
+    if (m_cachedScript) {
+        m_cachedScript->deref(this);
+        m_cachedScript = 0;
+    }
+}
+
+void HTMLScriptElementImpl::notifyFinished(CachedObject* o)
+{
+    CachedScript *cs = static_cast<CachedScript *>(o);
+
+    assert(cs == m_cachedScript);
+
+    KHTMLPart *part = getDocument()->part();
+    if (part) {
+        KJSProxy *proxy = KJSProxy::proxy(part);
+        if (proxy) {
+            proxy->evaluate(cs->url().string(), 0, cs->script().string(), Node()); 
+            DocumentImpl::updateDocumentsRendering();
+        }
+    }
+
+    cs->deref(this);
+    m_cachedScript = 0;
 }
 
 NodeImpl::Id HTMLScriptElementImpl::id() const
index 193662b1826bcc8522ae59cf8f556720e460b273..77d3b645ae8a305658caa1ec313ef147a01c4d01 100644 (file)
@@ -32,7 +32,8 @@ class KHTMLView;
 
 namespace khtml {
     class CachedCSSStyleSheet;
-};
+    class CachedScript;
+}
 
 
 namespace DOM {
@@ -137,17 +138,24 @@ protected:
 
 // -------------------------------------------------------------------------
 
-class HTMLScriptElementImpl : public HTMLElementImpl
+class HTMLScriptElementImpl : public HTMLElementImpl, public khtml::CachedObjectClient
 {
 public:
     HTMLScriptElementImpl(DocumentPtr *doc);
-
     ~HTMLScriptElementImpl();
+    
+    virtual void insertedIntoDocument();
+    virtual void removedFromDocument();
+    virtual void notifyFinished(khtml::CachedObject *finishedObj);
 
     virtual Id id() const;
-    
     virtual bool isURLAttribute(AttributeImpl *attr) const;
-    
+
+    void setCreatedByParser(bool createdByParser) { m_createdByParser = createdByParser; }
+
+private:
+    khtml::CachedScript *m_cachedScript;
+    bool m_createdByParser;
 };
 
 // -------------------------------------------------------------------------
index 896af996b1c401ae7d0a2fb965d14c3154530427..c94932b69d6ef9fc0494daee70f7bc8b9903504f 100644 (file)
@@ -943,6 +943,13 @@ NodeImpl *KHTMLParser::getElement(Token* t)
         if (!includesCommentsInDOM)
             return 0;
         break;
+
+    case ID_SCRIPT:
+        {
+            HTMLScriptElementImpl *scriptElement = new HTMLScriptElementImpl(document);
+            scriptElement->setCreatedByParser(true);
+            return scriptElement;
+        }
     }
 
     return document->document()->createHTMLElement(t->id);
index a48b2f41baaa5a819fad21969fb7d46bb26fb6ff..e2855dac4626cb099131b3c3dd86d0ffd5073ece 100644 (file)
@@ -333,6 +333,9 @@ void XMLTokenizer::startElement(const xmlChar *name, const xmlChar **libxmlAttri
         m_currentNode = implicitTBody;
     }
 
+    if (newElement->id() == ID_SCRIPT)
+        static_cast<HTMLScriptElementImpl *>(newElement)->setCreatedByParser(true);
+
     if (m_currentNode->addChild(newElement)) {
         if (m_view && !newElement->attached())
             newElement->attach();