CVS Commit: src/xdocs: Children of removed nodes were erroneously still

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

CVS Commit: src/xdocs: Children of removed nodes were erroneously still

mguillem
Log Message:
-----------
Children of removed nodes were erroneously still available by id using HtmlPage.getElementById. Fix for bug 1233519.

Modified Files:
--------------
    htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html:
        HtmlPageTest.java
        (http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlPageTest.java)
    htmlunit/src/java/com/gargoylesoftware/htmlunit/html:
        HtmlPage.java
        (http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java)
    htmlunit/src/xdocs:
        changes.xml
        (http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/xdocs/changes.xml)

Revision Data
-------------
Index: HtmlPageTest.java
===================================================================
RCS file: /cvsroot/htmlunit/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlPageTest.java,v
retrieving revision 1.39
retrieving revision 1.40
diff -Lsrc/test/java/com/gargoylesoftware/htmlunit/html/HtmlPageTest.java -Lsrc/test/java/com/gargoylesoftware/htmlunit/html/HtmlPageTest.java -u -d -r1.39 -r1.40
--- src/test/java/com/gargoylesoftware/htmlunit/html/HtmlPageTest.java
+++ src/test/java/com/gargoylesoftware/htmlunit/html/HtmlPageTest.java
@@ -48,6 +48,7 @@
 
 import junit.framework.AssertionFailedError;
 
+import com.gargoylesoftware.htmlunit.ElementNotFoundException;
 import com.gargoylesoftware.htmlunit.ImmediateRefreshHandler;
 import com.gargoylesoftware.htmlunit.KeyValuePair;
 import com.gargoylesoftware.htmlunit.MockWebConnection;
@@ -1105,4 +1106,40 @@
         assertTrue(HtmlPage.isJavaScript("text/javascript", null));
         assertTrue(HtmlPage.isJavaScript("text/JavaScript", null));
     }
+
+    /**
+     * Regression test for bug 1233519
+     * @exception  Exception If the test fails
+     */
+    public void testGetHtmlElementByIdAfterRemove() throws Exception {
+        final String htmlContent
+            = "<html><head><title>foo</title></head>\n"
+            + "<body>"
+            + "<div id='div1'>"
+            + "<div id='div2'>"
+            + "</div>"
+            + "</div>"
+            + "</body>"
+            + "</html>";
+        
+        final HtmlPage page = loadPage(htmlContent);
+        final HtmlElement div1 = page.getHtmlElementById("div1");
+        page.getHtmlElementById("div2"); // would throw if not found
+        div1.remove();
+        try {
+            page.getHtmlElementById("div1"); // throws if not found
+            fail("div1 should have been removed");
+        }
+        catch (final ElementNotFoundException e) {
+            // nothing
+        }
+        
+        try {
+            page.getHtmlElementById("div2"); // throws if not found
+            fail("div2 should have been removed");
+        }
+        catch (final ElementNotFoundException e) {
+            // nothing
+        }
+    }
 }
Index: HtmlPage.java
===================================================================
RCS file: /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java,v
retrieving revision 1.114
retrieving revision 1.115
diff -Lsrc/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java -Lsrc/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java -u -d -r1.114 -r1.115
--- src/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java
+++ src/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java
@@ -1318,12 +1318,18 @@
     }
 
     /**
-     * Remove an element from the ID map.
+     * Remove an element and its children from the ID map.
      *
      * @param idElement the element with an ID attribute to remove.
      */
     void removeIdElement(final HtmlElement idElement) {
         idMap_.remove(idElement.getAttributeValue("id"));
+        
+        // remove ids from children
+        for (final Iterator iter=idElement.getChildElementsIterator(); iter.hasNext();) {
+            final HtmlElement child = (HtmlElement) iter.next();
+            idMap_.remove(child.getAttributeValue("id"));
+        }
     }
     
     private void insertTbodyTagsAsNeeded() {
Index: changes.xml
===================================================================
RCS file: /cvsroot/htmlunit/htmlunit/src/xdocs/changes.xml,v
retrieving revision 1.408
retrieving revision 1.409
diff -Lsrc/xdocs/changes.xml -Lsrc/xdocs/changes.xml -u -d -r1.408 -r1.409
--- src/xdocs/changes.xml
+++ src/xdocs/changes.xml
@@ -7,6 +7,10 @@
 
     <body>
         <release version="NextRelease" date="Unknown">
+            <action type="update" dev="mguillem" id="1233519">
+             Children of removed nodes were erroneously still available by id using
+             HtmlPage.getElementById.
+            </action>
             <action type="update" dev="yourgod">
                 Upgraded commons-jelly to version 1.0
             </action>


-------------------------------------------------------
This SF.Net email is sponsored by the 'Do More With Dual!' webinar happening
July 14 at 8am PDT/11am EDT. We invite you to explore the latest in dual
core and dual graphics technology at this free one hour event hosted by HP,
AMD, and NVIDIA.  To register visit http://www.hp.com/go/dualwebinar
_______________________________________________
HtmlUnit-develop mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/htmlunit-develop
Loading...