[webkit-changes] cvs commit: WebCore/layout-tests/fast/forms element-order-expected.txt element-order.html

Darin darin at opensource.apple.com
Sun Jun 19 22:52:17 PDT 2005


darin       05/06/19 22:52:16

  Modified:    .        ChangeLog
               khtml/html html_formimpl.cpp html_formimpl.h
  Added:       layout-tests/fast/forms element-order-expected.txt
                        element-order.html
  Log:
          Changes by both Anders Carlsson and me.
          Reviewed by Maciej and me.
  
          Test cases added:
          * layout-tests/fast/forms/element-order-expected.txt: Added.
          * layout-tests/fast/forms/element-order.html: Added.
  
          - fixed <http://bugzilla.opendarwin.org/show_bug.cgi?id=3503>
            form.elements[] not order-preserving when elements added via DOM
  
          * khtml/html/html_formimpl.h: Added formElementIndex.
          * khtml/html/html_formimpl.cpp:
          (DOM::insertIntoVector): Added.
          (DOM::HTMLFormElementImpl::formElementIndex): Added. Computes an appropriate
          index for a form element, given the document position relative to the other elements.
          (DOM::HTMLFormElementImpl::registerFormElement): Use formElementIndex to determine
          where to insert the element in the formElements array.
  
  Revision  Changes    Path
  1.4286    +20 -0     WebCore/ChangeLog
  
  Index: ChangeLog
  ===================================================================
  RCS file: /cvs/root/WebCore/ChangeLog,v
  retrieving revision 1.4285
  retrieving revision 1.4286
  diff -u -r1.4285 -r1.4286
  --- ChangeLog	20 Jun 2005 05:24:22 -0000	1.4285
  +++ ChangeLog	20 Jun 2005 05:52:12 -0000	1.4286
  @@ -1,5 +1,25 @@
   2005-06-19  Darin Adler  <darin at apple.com>
   
  +        Changes by both Anders Carlsson and me.
  +        Reviewed by Maciej and me.
  +
  +        Test cases added:
  +        * layout-tests/fast/forms/element-order-expected.txt: Added.
  +        * layout-tests/fast/forms/element-order.html: Added.
  +
  +        - fixed <http://bugzilla.opendarwin.org/show_bug.cgi?id=3503>
  +          form.elements[] not order-preserving when elements added via DOM
  +
  +        * khtml/html/html_formimpl.h: Added formElementIndex.
  +        * khtml/html/html_formimpl.cpp:
  +        (DOM::insertIntoVector): Added.
  +        (DOM::HTMLFormElementImpl::formElementIndex): Added. Computes an appropriate
  +        index for a form element, given the document position relative to the other elements.
  +        (DOM::HTMLFormElementImpl::registerFormElement): Use formElementIndex to determine
  +        where to insert the element in the formElements array.
  +
  +2005-06-19  Darin Adler  <darin at apple.com>
  +
           Changes by Anders Carlsson and me.
           Reviewed by Maciej and me.
   
  
  
  
  1.167     +37 -8     WebCore/khtml/html/html_formimpl.cpp
  
  Index: html_formimpl.cpp
  ===================================================================
  RCS file: /cvs/root/WebCore/khtml/html/html_formimpl.cpp,v
  retrieving revision 1.166
  retrieving revision 1.167
  diff -u -r1.166 -r1.167
  --- html_formimpl.cpp	20 Jun 2005 05:24:25 -0000	1.166
  +++ html_formimpl.cpp	20 Jun 2005 05:52:16 -0000	1.167
  @@ -717,32 +717,61 @@
       }
   }
   
  +template<class T> static void insertIntoVector(QPtrVector<T> &vec, unsigned pos, T* item)
  +{
  +    unsigned size = vec.size();
  +    unsigned count = vec.count();
  +    assert(pos <= count);
  +    if (size == count)
  +        vec.resize(size == 0 ? 8 : size * 3 / 2);
  +    for (unsigned i = count; i > pos; --i)
  +        vec.insert(i, vec[i - 1]);    
  +    vec.insert(pos, item);
  +}
  +
   template<class T> static void appendToVector(QPtrVector<T> &vec, T *item)
   {
       unsigned size = vec.size();
       unsigned count = vec.count();
       if (size == count)
  -        vec.resize(size == 0 ? 8 : (int)(size * 1.5));
  +        vec.resize(size == 0 ? 8 : size * 3 / 2);
       vec.insert(count, item);
   }
   
   template<class T> static void removeFromVector(QPtrVector<T> &vec, T *item)
   {
       int pos = vec.findRef(item);
  -    int count = vec.count();
  -
       if (pos < 0)
           return;
  +    unsigned count = vec.count();
  +    for (unsigned i = pos; i < count - 1; ++i)
  +        vec.insert(i, vec[i + 1]);
  +    vec.remove(count - 1);
  +}
   
  -    for (int i = pos; i < count - 1; i++) {
  -        vec.insert(i, vec[i+1]);
  +unsigned HTMLFormElementImpl::formElementIndex(HTMLGenericFormElementImpl *e)
  +{
  +    // Check for the special case where this element is the very last thing in
  +    // the form's tree of children; we don't want to walk the entire tree in that
  +    // common case that occurs during parsing; instead we'll just return a value
  +    // that says "add this form element to the end of the array".
  +    if (e->traverseNextNode(this)) {
  +        unsigned i = 0;
  +        for (NodeImpl *node = this; node; node = node->traverseNextNode(this)) {
  +            if (node == e)
  +                return i;
  +            if (node->isHTMLElement()
  +                    && static_cast<HTMLElementImpl *>(node)->isGenericFormElement()
  +                    && static_cast<HTMLGenericFormElementImpl *>(node)->form() == this)
  +                ++i;
  +        }
       }
  -    vec.remove(count - 1);
  +    return formElements.count();
   }
   
   void HTMLFormElementImpl::registerFormElement(HTMLGenericFormElementImpl *e)
   {
  -    appendToVector(formElements, e);
  +    insertIntoVector(formElements, formElementIndex(e), e);
       removeFromVector(dormantFormElements, e);
   }
   
  @@ -921,7 +950,7 @@
           m_form->registerFormElement(this);
   
       m_dormant = false;
  -   
  +
       HTMLElementImpl::insertedIntoDocument();
   }
   
  
  
  
  1.74      +2 -0      WebCore/khtml/html/html_formimpl.h
  
  Index: html_formimpl.h
  ===================================================================
  RCS file: /cvs/root/WebCore/khtml/html/html_formimpl.h,v
  retrieving revision 1.73
  retrieving revision 1.74
  diff -u -r1.73 -r1.74
  --- html_formimpl.h	19 Jun 2005 05:00:24 -0000	1.73
  +++ html_formimpl.h	20 Jun 2005 05:52:16 -0000	1.74
  @@ -147,6 +147,8 @@
       void parseEnctype(const DOMString &);
       bool formData(khtml::FormData &) const;
   
  +    unsigned formElementIndex(HTMLGenericFormElementImpl *);
  +
       QString oldIdAttr;
       QString oldNameAttr;
   };
  
  
  
  1.1                  WebCore/layout-tests/fast/forms/element-order-expected.txt
  
  Index: element-order-expected.txt
  ===================================================================
  This test checks that the elements array of a form element returns the elements in document order rather than order of insertion into the document. Older versions of WebKit returned the elements in order of insertion.
  
  If the test works properly, the text at the bottom will most likely say "x" then "y" then "z" in that order. If it fails, it will most likely say "x", then "z", then "y".
  
  
    
  x
  y
  z
  
  
  
  
  
  1.1                  WebCore/layout-tests/fast/forms/element-order.html
  
  Index: element-order.html
  ===================================================================
  <html>
  <head>
  <script>
  function test() {
      if (window.layoutTestController)
          layoutTestController.dumpAsText();
  
      var f = window.document.forms[0];
  
      var e = document.createElement('input');
      e.type = 'text'
      e.name = 'y';
      e.value = 'y';
      f.replaceChild(e, f.y);
  
      for (var i = 0; i < f.elements.length; i++) {
          var line = document.createElement("div");
          line.appendChild(document.createTextNode(f.elements[i].name));
          document.getElementById("result").appendChild(line);
      }
  }
  </script>
  <body onload="test()">
  <p>This test checks that the elements array of a form element returns the elements in document order
  rather than order of insertion into the document. Older versions of WebKit returned the elements in
  order of insertion.</p>
  <p>If the test works properly, the text at the bottom will most likely say "x" then "y" then "z" in that order.
  If it fails, it will most likely say "x", then "z", then "y".</p>
  <hr>
  <p>
  <form>
  <input type='text' name='x' value='x'>
  <input type='text' name='y' value='y'>
  <input type='text' name='z' value='z'>
  </form>
  </p>
  <hr>
  <p id="result"></p>
  </body>
  </html>
  
  
  



More information about the webkit-changes mailing list