[webkit-changes] cvs commit: WebCore/layout-tests/fast/dom/resources script3.js script4.js script5.js

Darin darin at opensource.apple.com
Sat Jun 18 22:44:29 PDT 2005


darin       05/06/18 22:44:29

  Modified:    .        ChangeLog
               khtml/dom html_head.cpp
               khtml/html html_headimpl.cpp html_headimpl.h
  Added:       layout-tests/fast/dom script-add-expected.txt
                        script-add.html
               layout-tests/fast/dom/resources script3.js script4.js
                        script5.js
  Log:
          Changes by Anders Carlsson.
          Reviewed by me.
  
          Test cases added:
          * layout-tests/fast/dom/resources/script3.js: Added.
          * layout-tests/fast/dom/resources/script4.js: Added.
          * layout-tests/fast/dom/resources/script5.js: Added.
          * layout-tests/fast/dom/script-add-expected.txt: Added.
          * layout-tests/fast/dom/script-add.html: Added.
  
          - fixed <http://bugzilla.opendarwin.org/show_bug.cgi?id=3526>
            improve support for dynamically added <script> elements
  
          * khtml/html/html_headimpl.h: Added override of childrenChanged, new evaluateScript
          function, and m_evaluated boolean.
          * khtml/html/html_headimpl.cpp:
          (HTMLScriptElementImpl::HTMLScriptElementImpl): Init m_evaluated to false.
          (HTMLScriptElementImpl::childrenChanged): Added code to evaluate the script the first
          time a child is added (except for nodes created by parsing).
          (HTMLScriptElementImpl::insertedIntoDocument): Refactored to use the new text and
          evaluateScript functions. Also changed to not try to evaluate the script if the element
          is entirely empty. This allows you to declare an empty <script> element and fill the
          content in later using DHTML.
          (HTMLScriptElementImpl::notifyFinished): Refactored to use the new evaluateScript function.
          (HTMLScriptElementImpl::evaluateScript): Added. Checks and sets a new m_evaluated boolean
          to implement the "evaluate each script only once" rule.
          (HTMLScriptElementImpl::text): Replaced bogus implementation with one that works.
          (HTMLScriptElementImpl::setText): Ditto.
  
          * khtml/dom/html_head.cpp:
          (HTMLScriptElement::text): Replaced bogus implementation with one that calls through to
          the implementation class. This change doesn't really matter since we don't compile this file.
          (HTMLScriptElement::setText): Ditto.
  
  Revision  Changes    Path
  1.4281    +36 -0     WebCore/ChangeLog
  
  Index: ChangeLog
  ===================================================================
  RCS file: /cvs/root/WebCore/ChangeLog,v
  retrieving revision 1.4280
  retrieving revision 1.4281
  diff -u -r1.4280 -r1.4281
  --- ChangeLog	19 Jun 2005 05:31:56 -0000	1.4280
  +++ ChangeLog	19 Jun 2005 05:44:22 -0000	1.4281
  @@ -1,5 +1,41 @@
   2005-06-18  Darin Adler  <darin at apple.com>
   
  +        Changes by Anders Carlsson.
  +        Reviewed by me.
  +
  +        Test cases added:
  +        * layout-tests/fast/dom/resources/script3.js: Added.
  +        * layout-tests/fast/dom/resources/script4.js: Added.
  +        * layout-tests/fast/dom/resources/script5.js: Added.
  +        * layout-tests/fast/dom/script-add-expected.txt: Added.
  +        * layout-tests/fast/dom/script-add.html: Added.
  +
  +        - fixed <http://bugzilla.opendarwin.org/show_bug.cgi?id=3526>
  +          improve support for dynamically added <script> elements
  +
  +        * khtml/html/html_headimpl.h: Added override of childrenChanged, new evaluateScript
  +        function, and m_evaluated boolean.
  +        * khtml/html/html_headimpl.cpp:
  +        (HTMLScriptElementImpl::HTMLScriptElementImpl): Init m_evaluated to false.
  +        (HTMLScriptElementImpl::childrenChanged): Added code to evaluate the script the first
  +        time a child is added (except for nodes created by parsing).
  +        (HTMLScriptElementImpl::insertedIntoDocument): Refactored to use the new text and
  +        evaluateScript functions. Also changed to not try to evaluate the script if the element
  +        is entirely empty. This allows you to declare an empty <script> element and fill the
  +        content in later using DHTML.
  +        (HTMLScriptElementImpl::notifyFinished): Refactored to use the new evaluateScript function.
  +        (HTMLScriptElementImpl::evaluateScript): Added. Checks and sets a new m_evaluated boolean
  +        to implement the "evaluate each script only once" rule.
  +        (HTMLScriptElementImpl::text): Replaced bogus implementation with one that works.
  +        (HTMLScriptElementImpl::setText): Ditto.
  +
  +        * khtml/dom/html_head.cpp:
  +        (HTMLScriptElement::text): Replaced bogus implementation with one that calls through to
  +        the implementation class. This change doesn't really matter since we don't compile this file.
  +        (HTMLScriptElement::setText): Ditto.
  +
  +2005-06-18  Darin Adler  <darin at apple.com>
  +
           * khtml/html/html_formimpl.cpp: (DOM::HTMLLabelElementImpl::formElement):
           Oops, checked in a version that didn't even compile! Fixed now.
   
  
  
  
  1.9       +2 -2      WebCore/khtml/dom/html_head.cpp
  
  Index: html_head.cpp
  ===================================================================
  RCS file: /cvs/root/WebCore/khtml/dom/html_head.cpp,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- html_head.cpp	3 Nov 2003 06:32:04 -0000	1.8
  +++ html_head.cpp	19 Jun 2005 05:44:27 -0000	1.9
  @@ -329,12 +329,12 @@
   DOMString HTMLScriptElement::text() const
   {
       if(!impl) return DOMString();
  -    return ((ElementImpl *)impl)->getAttribute(ATTR_TEXT);
  +    return ((HTMLScriptElementImpl *)impl)->text();
   }
   
   void HTMLScriptElement::setText( const DOMString &value )
   {
  -    if(impl) ((ElementImpl *)impl)->setAttribute(ATTR_TEXT, value);
  +    if(impl) ((HTMLScriptElementImpl *)impl)->setText(value);
   }
   
   DOMString HTMLScriptElement::htmlFor() const
  
  
  
  1.49      +50 -22    WebCore/khtml/html/html_headimpl.cpp
  
  Index: html_headimpl.cpp
  ===================================================================
  RCS file: /cvs/root/WebCore/khtml/html/html_headimpl.cpp,v
  retrieving revision 1.48
  retrieving revision 1.49
  diff -u -r1.48 -r1.49
  --- html_headimpl.cpp	11 May 2005 05:49:34 -0000	1.48
  +++ html_headimpl.cpp	19 Jun 2005 05:44:28 -0000	1.49
  @@ -512,7 +512,7 @@
   // -------------------------------------------------------------------------
   
   HTMLScriptElementImpl::HTMLScriptElementImpl(DocumentPtr *doc)
  -    : HTMLElementImpl(doc), m_cachedScript(0), m_createdByParser(false)
  +    : HTMLElementImpl(doc), m_cachedScript(0), m_createdByParser(false), m_evaluated(false)
   {
   }
   
  @@ -532,6 +532,15 @@
       return attr->id() == ATTR_SRC;
   }
   
  +void HTMLScriptElementImpl::childrenChanged()
  +{
  +    // If a node is inserted as a child of the script element
  +    // and the script element has been inserted in the document
  +    // we evaluate the script.
  +    if (!m_createdByParser && inDocument() && firstChild())
  +        evaluateScript(getDocument()->URL(), text());
  +}
  +
   void HTMLScriptElementImpl::insertedIntoDocument()
   {
       HTMLElementImpl::insertedIntoDocument();
  @@ -549,21 +558,12 @@
           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(), 0);
  -    DocumentImpl::updateDocumentsRendering();
  +    // If there's an empty script node, we shouldn't evaluate the script
  +    // because if a script is inserted afterwards (by setting text or innerText)
  +    // it should be evaluated, and evaluateScript only evaluates a script once.
  +    DOMString scriptString = text();    
  +    if (!scriptString.isEmpty())
  +        evaluateScript(getDocument()->URL(), scriptString);
   }
   
   void HTMLScriptElementImpl::removedFromDocument()
  @@ -582,27 +582,55 @@
   
       assert(cs == m_cachedScript);
   
  +    evaluateScript(cs->url().string(), cs->script());
  +
  +    cs->deref(this);
  +    m_cachedScript = 0;
  +}
  +
  +void HTMLScriptElementImpl::evaluateScript(const QString &URL, const DOMString &script)
  +{
  +    if (m_evaluated)
  +        return;
  +    
       KHTMLPart *part = getDocument()->part();
       if (part) {
           KJSProxy *proxy = KJSProxy::proxy(part);
           if (proxy) {
  -            proxy->evaluate(cs->url().string(), 0, cs->script().string(), 0); 
  +            m_evaluated = true;
  +            proxy->evaluate(URL, 0, script.string(), 0);
               DocumentImpl::updateDocumentsRendering();
           }
       }
  -
  -    cs->deref(this);
  -    m_cachedScript = 0;
   }
   
   DOMString HTMLScriptElementImpl::text() const
   {
  -    return getAttribute(ATTR_TEXT);
  +    DOMString val = "";
  +    
  +    for (NodeImpl *n = firstChild(); n; n = n->nextSibling()) {
  +        if (n->isTextNode())
  +            val += static_cast<TextImpl *>(n)->data();
  +    }
  +    
  +    return val;
   }
   
   void HTMLScriptElementImpl::setText(const DOMString &value)
   {
  -    setAttribute(ATTR_TEXT, value);
  +    int exceptioncode = 0;
  +    int numChildren = childNodeCount();
  +    
  +    if (numChildren == 1 && firstChild()->isTextNode()) {
  +        static_cast<DOM::TextImpl *>(firstChild())->setData(value, exceptioncode);
  +        return;
  +    }
  +    
  +    if (numChildren > 0) {
  +        removeChildren();
  +    }
  +    
  +    appendChild(getDocument()->createTextNode(value.implementation()), exceptioncode);
   }
   
   DOMString HTMLScriptElementImpl::htmlFor() const
  
  
  
  1.20      +5 -1      WebCore/khtml/html/html_headimpl.h
  
  Index: html_headimpl.h
  ===================================================================
  RCS file: /cvs/root/WebCore/khtml/html/html_headimpl.h,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- html_headimpl.h	11 May 2005 05:49:34 -0000	1.19
  +++ html_headimpl.h	19 Jun 2005 05:44:28 -0000	1.20
  @@ -189,12 +189,15 @@
       virtual void insertedIntoDocument();
       virtual void removedFromDocument();
       virtual void notifyFinished(khtml::CachedObject *finishedObj);
  -
  +    virtual void childrenChanged();
  +    
       virtual Id id() const;
       virtual bool isURLAttribute(AttributeImpl *attr) const;
   
       void setCreatedByParser(bool createdByParser) { m_createdByParser = createdByParser; }
   
  +    void evaluateScript(const QString &, const DOMString &);
  +
       DOMString text() const;
       void setText(const DOMString &);
   
  @@ -219,6 +222,7 @@
   private:
       khtml::CachedScript *m_cachedScript;
       bool m_createdByParser;
  +    bool m_evaluated;
   };
   
   // -------------------------------------------------------------------------
  
  
  
  1.1                  WebCore/layout-tests/fast/dom/script-add-expected.txt
  
  Index: script-add-expected.txt
  ===================================================================
  This tests 5 different ways of dynamically loading a script by creating a script element.
  
  If it works you should see 5 messages below about successfully loading a script.
  
  Loaded script 1.
  Loaded script 2.
  Loaded script 3.
  Loaded script 4.
  Loaded script 5.
  
  
  
  
  1.1                  WebCore/layout-tests/fast/dom/script-add.html
  
  Index: script-add.html
  ===================================================================
  <html>
      <head>
          <script type="text/javascript">
              function log(number)
              {
                  var item = document.createElement("li");
                  item.appendChild(document.createTextNode("Loaded script " + number + "."));
                  document.getElementById("console").appendChild(item);
              }
              function test1()
              {
                  var oHead = document.getElementsByTagName('head')[0];
                  var oScript = document.createElement('script');
                  oScript.setAttribute('type', 'text/javascript');
                  oHead.appendChild(oScript);
                  oScript.text = 'log("1")';
              }
              function test2()
              {
                  var oHead = document.getElementsByTagName('head')[0];
                  var oScript = document.createElement('script');
                  oScript.setAttribute('type', 'text/javascript');
                  oHead.appendChild(oScript);
                  oScript.innerText = 'log("2")';
              }
              function test3()
              {
                  var oScript = document.createElement('SCRIPT');
                  var oHead = document.getElementsByTagName('HEAD')[0];
                  oScript.setAttribute('type', 'text/javascript');
                  oScript.setAttribute('src', 'resources/script3.js');
                  oHead.appendChild(oScript);
              }
              function test4()
              {
                  var oScript = document.createElement('SCRIPT');
                  var oHead = document.getElementsByTagName('HEAD')[0];
                  oScript.language = 'javascript';
                  oScript.src = 'resources/script4.js';
                  oHead.appendChild(oScript);
              }
              function test5()
              {
                  var oScript = document.createElement('script');
                  var oBody = document.getElementsByTagName('body')[0];
                  oScript.setAttribute('type', 'text/javascript');
                  oScript.setAttribute('src', 'resources/script5.js');
                  oBody.appendChild(oScript);
              }
              function test()
              {
                  if (window.layoutTestController)
                      layoutTestController.dumpAsText();
  
                  test1();
                  test2();
                  test3();
                  test4();
                  test5();
              }
  	</script>
      </head>
      <body onload="test()">
          <p>This tests 5 different ways of dynamically loading a script by creating a script element.</p>
          <p>If it works you should see 5 messages below about successfully loading a script.</p>
          <hr>
          <p><ol id="console"></ol></p>
      </body>
  </html>
  
  
  
  1.1                  WebCore/layout-tests/fast/dom/resources/script3.js
  
  Index: script3.js
  ===================================================================
  log("3");
  
  
  
  1.1                  WebCore/layout-tests/fast/dom/resources/script4.js
  
  Index: script4.js
  ===================================================================
  log("4");
  
  
  
  1.1                  WebCore/layout-tests/fast/dom/resources/script5.js
  
  Index: script5.js
  ===================================================================
  log("5");
  
  
  



More information about the webkit-changes mailing list