SF.net SVN: htmlunit: [1230] trunk/htmlunit/src

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

SF.net SVN: htmlunit: [1230] trunk/htmlunit/src

mguillem
Revision: 1230
          http://svn.sourceforge.net/htmlunit/?rev=1230&view=rev
Author:   mguillem
Date:     2006-09-08 07:17:17 -0700 (Fri, 08 Sep 2006)

Log Message:
-----------
Parser: automatically add an <head> element if none present in html source (as done by Internet Explorer and Firefox).

Modified Paths:
--------------
    trunk/htmlunit/src/java/com/gargoylesoftware/htmlunit/html/HTMLParser.java
    trunk/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/host/HTMLElement.java
    trunk/htmlunit/src/xdocs/changes.xml

Added Paths:
-----------
    trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlHeadTest.java

Modified: trunk/htmlunit/src/java/com/gargoylesoftware/htmlunit/html/HTMLParser.java
===================================================================
--- trunk/htmlunit/src/java/com/gargoylesoftware/htmlunit/html/HTMLParser.java 2006-09-07 15:30:46 UTC (rev 1229)
+++ trunk/htmlunit/src/java/com/gargoylesoftware/htmlunit/html/HTMLParser.java 2006-09-08 14:17:17 UTC (rev 1230)
@@ -78,7 +78,7 @@
 
     private static final Map ELEMENT_FACTORIES = new HashMap();
     private static boolean IgnoreOutsideContent_ = false;
-    
+
     static {
         ELEMENT_FACTORIES.put("input", InputElementFactory.instance);
 
@@ -162,7 +162,7 @@
 
     /**
      * Set the flag to control validation of the HTML content that is outside of the
-     * BODY and HTML tags.  This flag is false by default to maintain compatability with
+     * BODY and HTML tags.  This flag is false by default to maintain compatability with
      * current NekoHTML defaults.
      * @param ignoreOutsideContent - boolean flag to set
      */
@@ -196,7 +196,7 @@
 
     /**
      * You should never need to create one of these!
-     */    
+     */
     private HTMLParser() {
     }
 
@@ -226,7 +226,7 @@
         domBuilder.parse(in);
         return domBuilder.page_;
     }
-    
+
     /**
      * <p>Return true if the specified charset is supported on this platform.</p>
      * @param charset The charset to check.
@@ -263,6 +263,7 @@
 
         private DomNode currentNode_;
         private StringBuffer characters_;
+        private boolean headParsed_ = false;
 
         /**
          * create a new builder for parsing the given response contents
@@ -344,9 +345,19 @@
             handleCharacters();
 
             final String tagLower = localName.toLowerCase();
-            
-            // add a <tbody> if a <tr> is directly in <table>
-            if (tagLower.equals("tr") && currentNode_.getNodeName().equals("table")) {
+
+            if (tagLower.equals("head")) {
+                headParsed_ = true;
+            }
+            // add a head if none was there
+            else if (!headParsed_ && (tagLower.equals("body") || tagLower.equals("frameset"))) {
+                final IElementFactory factory = getElementFactory("head");
+                final HtmlElement newElement = factory.createElement(page_, "head", null);
+                currentNode_.appendChild(newElement);
+                headParsed_ = true;
+            }
+            // add a <tbody> if a <tr> is directly in <table>
+            else if (tagLower.equals("tr") && currentNode_.getNodeName().equals("table")) {
                 final IElementFactory factory = getElementFactory("tbody");
                 final HtmlElement newElement = factory.createElement(page_, "tbody", null);
                 currentNode_.appendChild(newElement);
@@ -463,21 +474,21 @@
     }
 
     /** @see DefaultErrorHandler#error(String,String,XMLParseException) */
-    public void error(final String domain, final String key,
+    public void error(final String domain, final String key,
             final XMLParseException exception) throws XNIException {
-        listener_.error(exception.getMessage(),
+        listener_.error(exception.getMessage(),
                 url_,
-                exception.getLineNumber(),
+                exception.getLineNumber(),
                 exception.getColumnNumber(),
                 key);
     }
 
     /** @see DefaultErrorHandler#warning(String,String,XMLParseException) */
-    public void warning(final String domain, final String key,
-            final XMLParseException exception) throws XNIException {          
-        listener_.warning(exception.getMessage(),
+    public void warning(final String domain, final String key,
+            final XMLParseException exception) throws XNIException {
+        listener_.warning(exception.getMessage(),
                 url_,
-                exception.getLineNumber(),
+                exception.getLineNumber(),
                 exception.getColumnNumber(),
                 key);
     }

Modified: trunk/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/host/HTMLElement.java
===================================================================
--- trunk/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/host/HTMLElement.java 2006-09-07 15:30:46 UTC (rev 1229)
+++ trunk/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/host/HTMLElement.java 2006-09-08 14:17:17 UTC (rev 1230)
@@ -110,7 +110,7 @@
     private int scrollTop_ = 0;
 
     /**
-     * The tag names of the objects for which outerHTML is readonly
+     * The tag names of the objects for which outerHTML is readonly
      */
     private static final List OUTER_HTML_READONLY =
         Arrays.asList(new String[] {
@@ -260,7 +260,7 @@
      * Set an attribute.
      * See also <a href="http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-F68F082">
      * the DOM reference</a>
-     *
+     *
      * @param name Name of the attribute to set
      * @param value Value to set the attribute to
      */
@@ -270,7 +270,7 @@
 
     /**
      * Remove an attribute.
-     *
+     *
      * @param name Name of the attribute to remove
      */
     public void jsxFunction_removeAttribute(final String name) {
@@ -366,7 +366,7 @@
      * Gets the outerHTML of the node.
      * @see <a href="http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/outerhtml.asp">
      * MSDN documentation</a>
-     * @return the contents of this node as html
+     * @return the contents of this node as html
      * (note: the formatting isn't currently exactly the same as IE)
      */
     public String jsxGet_outerHTML() {
@@ -509,7 +509,7 @@
                     }
                 };
                 final HtmlPage pseudoPage = HTMLParser.parse(webResp, pseudoWindow);
-                final HtmlBody body = (HtmlBody) pseudoPage.getDocumentElement().getFirstChild();
+                final HtmlBody body = (HtmlBody) pseudoPage.getDocumentElement().getLastChild();
 
                 final Collection nodes = new ArrayList();
                 for (final Iterator iter = body.getChildIterator(); iter.hasNext();) {
@@ -538,10 +538,10 @@
     /**
      * Copies the node to make it available to the page.
      * All this stuff just to change the htmlPage_ property on all nodes!
-     *
+     *
      * @param node The node to copy.
      * @param page The page containing the node.
-     * @return a node with the same properties but bound to the page.
+     * @return a node with the same properties but bound to the page.
      */
     private DomNode copy(final DomNode node, final HtmlPage page) {
         final DomNode copy;
@@ -562,7 +562,7 @@
     }
 
     /**
-     * Gets the attributes of the element in the form of a {@link org.xml.sax.Attributes}
+     * Gets the attributes of the element in the form of a {@link org.xml.sax.Attributes}
      * @param element the element to read the attributes from
      * @return the attributes
      */

Added: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlHeadTest.java
===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlHeadTest.java                        (rev 0)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlHeadTest.java 2006-09-08 14:17:17 UTC (rev 1230)
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2002-2006 Gargoyle Software Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ * 3. The end-user documentation included with the redistribution, if any, must
+ *    include the following acknowledgment:
+ *
+ *       "This product includes software developed by Gargoyle Software Inc.
+ *        (http://www.GargoyleSoftware.com/)."
+ *
+ *    Alternately, this acknowledgment may appear in the software itself, if
+ *    and wherever such third-party acknowledgments normally appear.
+ * 4. The name "Gargoyle Software" must not be used to endorse or promote
+ *    products derived from this software without prior written permission.
+ *    For written permission, please contact [hidden email].
+ * 5. Products derived from this software may not be called "HtmlUnit", nor may
+ *    "HtmlUnit" appear in their name, without prior written permission of
+ *    Gargoyle Software Inc.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
+ * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.gargoylesoftware.htmlunit.html;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.gargoylesoftware.htmlunit.WebTestCase;
+
+/**
+ * Tests for {@link HtmlHead}
+ * @version $Revision: 1176 $
+ * @author Marc Guillemot
+ */
+public class HtmlHeadTest extends WebTestCase {
+    /**
+     *  Create an instance
+     *
+     * @param  name The name of the test
+     */
+    public HtmlHeadTest( final String name ) {
+        super( name );
+    }
+
+    /**
+     * IE and FF both add an head element when it's not present in the html code
+     * @throws Exception if the test fails
+     */
+    public void testAddedWhenMissing() throws Exception {
+        final String htmlContent = "<html><body>"
+            + "<script>"
+            + "alert(document.firstChild.firstChild.tagName);"
+            + "</script>"
+            + "</body></html>";
+
+        final List collectedAlerts = new ArrayList();
+        final String[] expectedAlerts = { "HEAD" };
+        createTestPageForRealBrowserIfNeeded(htmlContent, expectedAlerts);
+
+        loadPage(htmlContent, collectedAlerts);
+        assertEquals(expectedAlerts, collectedAlerts);
+    }
+}

Modified: trunk/htmlunit/src/xdocs/changes.xml
===================================================================
--- trunk/htmlunit/src/xdocs/changes.xml 2006-09-07 15:30:46 UTC (rev 1229)
+++ trunk/htmlunit/src/xdocs/changes.xml 2006-09-08 14:17:17 UTC (rev 1230)
@@ -7,6 +7,10 @@
 
     <body>
         <release version="next">
+            <action type="update" dev="mguillem">
+                Parser: automatically add an &lt;head&gt; element if none present in html source
+                (as done by Internet Explorer and Firefox).
+            </action>
             <action type="update" dev="mguillem" id="1491743">
                 JavaScript: Location.replace wrongly loaded new url in top window.
             </action>


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
HtmlUnit-develop mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/htmlunit-develop