Search in sources :

Example 1 with XHtmlPage

use of com.gargoylesoftware.htmlunit.html.XHtmlPage in project htmlunit by HtmlUnit.

the class HTMLParserTest method tableWithoutColgroup.

/**
 * @throws Exception failure
 */
@Test
public void tableWithoutColgroup() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head>\n" + "</head>\n" + "<body>\n" + "  <table><col width='7'/><col width='1'/><tbody><tr><td>seven</td><td>One</td></tr></tbody></table>\n" + "</body></html>";
    final WebClient webClient = getWebClient();
    final WebResponse webResponse = new StringWebResponse(html, URL_FIRST);
    final XHtmlPage page = new XHtmlPage(webResponse, webClient.getCurrentWindow());
    webClient.getCurrentWindow().setEnclosedPage(page);
    webClient.getPageCreator().getHtmlParser().parse(webResponse, page, true, false);
    final DomElement col = page.getElementsByTagName("col").get(0);
    assertEquals(col.getParentNode().getNodeName(), HtmlTableColumnGroup.TAG_NAME);
}
Also used : StringWebResponse(com.gargoylesoftware.htmlunit.StringWebResponse) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) StringWebResponse(com.gargoylesoftware.htmlunit.StringWebResponse) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) XHtmlPage(com.gargoylesoftware.htmlunit.html.XHtmlPage) WebClient(com.gargoylesoftware.htmlunit.WebClient) HtmlPageTest(com.gargoylesoftware.htmlunit.html.HtmlPageTest) Test(org.junit.Test)

Example 2 with XHtmlPage

use of com.gargoylesoftware.htmlunit.html.XHtmlPage in project htmlunit by HtmlUnit.

the class DefaultPageCreator method createXHtmlPage.

/**
 * Creates an XHtmlPage for this WebResponse.
 *
 * @param webResponse the page's source
 * @param webWindow the WebWindow to place the HtmlPage in
 * @return the newly created XHtmlPage
 * @throws IOException if the page could not be created
 */
protected XHtmlPage createXHtmlPage(final WebResponse webResponse, final WebWindow webWindow) throws IOException {
    final XHtmlPage page = new XHtmlPage(webResponse, webWindow);
    webWindow.setEnclosedPage(page);
    htmlParser_.parse(webResponse, page, true, false);
    return page;
}
Also used : XHtmlPage(com.gargoylesoftware.htmlunit.html.XHtmlPage)

Example 3 with XHtmlPage

use of com.gargoylesoftware.htmlunit.html.XHtmlPage in project htmlunit by HtmlUnit.

the class WebClient method loadHtmlCodeIntoCurrentWindow.

/**
 * Parses the given XHtml code string and loads the resulting XHtmlPage into
 * the current window.
 *
 * @param htmlCode the html code as string
 * @return the HtmlPage
 * @throws IOException in case of error
 */
public HtmlPage loadHtmlCodeIntoCurrentWindow(final String htmlCode) throws IOException {
    final HTMLParser htmlParser = getPageCreator().getHtmlParser();
    final WebWindow webWindow = getCurrentWindow();
    final StringWebResponse webResponse = new StringWebResponse(htmlCode, new URL("http://htmlunit.sourceforge.net/dummy.html"));
    final HtmlPage page = new HtmlPage(webResponse, webWindow);
    webWindow.setEnclosedPage(page);
    htmlParser.parse(webResponse, page, true, false);
    return page;
}
Also used : XHtmlPage(com.gargoylesoftware.htmlunit.html.XHtmlPage) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HTMLParser(com.gargoylesoftware.htmlunit.html.parser.HTMLParser) URL(java.net.URL)

Example 4 with XHtmlPage

use of com.gargoylesoftware.htmlunit.html.XHtmlPage in project htmlunit by HtmlUnit.

the class WebClient method loadXHtmlCodeIntoCurrentWindow.

/**
 * Parses the given XHtml code string and loads the resulting XHtmlPage into
 * the current window.
 *
 * @param xhtmlCode the xhtml code as string
 * @return the XHtmlPage
 * @throws IOException in case of error
 */
public XHtmlPage loadXHtmlCodeIntoCurrentWindow(final String xhtmlCode) throws IOException {
    final HTMLParser htmlParser = getPageCreator().getHtmlParser();
    final WebWindow webWindow = getCurrentWindow();
    final StringWebResponse webResponse = new StringWebResponse(xhtmlCode, new URL("http://htmlunit.sourceforge.net/dummy.html"));
    final XHtmlPage page = new XHtmlPage(webResponse, webWindow);
    webWindow.setEnclosedPage(page);
    htmlParser.parse(webResponse, page, true, false);
    return page;
}
Also used : XHtmlPage(com.gargoylesoftware.htmlunit.html.XHtmlPage) HTMLParser(com.gargoylesoftware.htmlunit.html.parser.HTMLParser) URL(java.net.URL)

Example 5 with XHtmlPage

use of com.gargoylesoftware.htmlunit.html.XHtmlPage in project htmlunit by HtmlUnit.

the class HtmlUnitNekoDOMBuilder method startElement.

/**
 * {@inheritDoc}
 */
@Override
public void startElement(String namespaceURI, final String localName, final String qName, final Attributes atts) throws SAXException {
    if (snippetStartNodeOverwritten_) {
        snippetStartNodeOverwritten_ = false;
        return;
    }
    handleCharacters();
    final String tagLower = localName.toLowerCase(Locale.ROOT);
    if (page_.isParsingHtmlSnippet() && ("html".equals(tagLower) || "body".equals(tagLower))) {
        return;
    }
    if ("head".equals(tagLower)) {
        if (headParsed_ == HeadParsed.YES || page_.isParsingHtmlSnippet()) {
            return;
        }
        headParsed_ = lastTagWasSynthesized_ ? HeadParsed.SYNTHESIZED : HeadParsed.YES;
    }
    if (namespaceURI != null) {
        namespaceURI = namespaceURI.trim();
    } else // add a head if none was there
    if (headParsed_ == HeadParsed.NO && ("body".equals(tagLower) || "frameset".equals(tagLower))) {
        final ElementFactory factory = htmlParser_.getElementFactory(page_, null, "head", insideSvg_, false);
        final DomElement newElement = factory.createElement(page_, "head", null);
        appendChild(currentNode_, newElement);
        headParsed_ = HeadParsed.SYNTHESIZED;
    }
    // If we're adding a body element, keep track of any temporary synthetic ones
    // that we may have had to create earlier (for document.write(), for example).
    HtmlBody oldBody = null;
    if ("body".equals(qName) && page_.getBody() instanceof HtmlBody) {
        oldBody = (HtmlBody) page_.getBody();
    }
    // end tag.
    if ("form".equals(tagLower)) {
        formWaitingForLostChildren_ = null;
    }
    // Add the new node.
    if (!(page_ instanceof XHtmlPage) && Html.XHTML_NAMESPACE.equals(namespaceURI)) {
        namespaceURI = null;
    }
    final ElementFactory factory = htmlParser_.getElementFactory(page_, namespaceURI, qName, insideSvg_, false);
    if (factory == HtmlUnitNekoHtmlParser.SVG_FACTORY) {
        namespaceURI = Html.SVG_NAMESPACE;
    }
    final DomElement newElement = factory.createElementNS(page_, namespaceURI, qName, atts, true);
    newElement.setStartLocation(locator_.getLineNumber(), locator_.getColumnNumber());
    // parse can't replace everything as it does not buffer elements while parsing
    addNodeToRightParent(currentNode_, newElement);
    if ("svg".equals(tagLower)) {
        insideSvg_ = true;
    }
    // remove the old body and move its children to the real body element we just added.
    if (oldBody != null) {
        oldBody.quietlyRemoveAndMoveChildrenTo(newElement);
    }
    if ("body".equals(tagLower)) {
        body_ = (HtmlElement) newElement;
    } else if ("meta".equals(tagLower) && page_.hasFeature(META_X_UA_COMPATIBLE)) {
        final HtmlMeta meta = (HtmlMeta) newElement;
        if ("X-UA-Compatible".equals(meta.getHttpEquivAttribute())) {
            final String content = meta.getContentAttribute();
            if (content.startsWith("IE=")) {
                final String mode = content.substring(3).trim();
                final int version = page_.getWebClient().getBrowserVersion().getBrowserVersionNumeric();
                try {
                    int value = Integer.parseInt(mode);
                    if (value > version) {
                        value = version;
                    }
                    ((HTMLDocument) page_.getScriptableObject()).forceDocumentMode(value);
                } catch (final Exception e) {
                // ignore
                }
            }
        }
    } else if (createdByJavascript_ && "script".equals(tagLower)) {
        final ScriptElement script = (ScriptElement) newElement;
        script.markAsCreatedByDomParser();
    }
    currentNode_ = newElement;
    stack_.push(currentNode_);
}
Also used : DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HtmlBody(com.gargoylesoftware.htmlunit.html.HtmlBody) XHtmlPage(com.gargoylesoftware.htmlunit.html.XHtmlPage) ScriptElement(com.gargoylesoftware.htmlunit.html.ScriptElement) HtmlMeta(com.gargoylesoftware.htmlunit.html.HtmlMeta) XNIException(org.apache.xerces.xni.XNIException) SAXException(org.xml.sax.SAXException) ObjectInstantiationException(com.gargoylesoftware.htmlunit.ObjectInstantiationException) IOException(java.io.IOException) ElementFactory(com.gargoylesoftware.htmlunit.html.ElementFactory)

Aggregations

XHtmlPage (com.gargoylesoftware.htmlunit.html.XHtmlPage)6 WebClient (com.gargoylesoftware.htmlunit.WebClient)2 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)2 HTMLParser (com.gargoylesoftware.htmlunit.html.parser.HTMLParser)2 URL (java.net.URL)2 Test (org.junit.Test)2 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)1 ObjectInstantiationException (com.gargoylesoftware.htmlunit.ObjectInstantiationException)1 StringWebResponse (com.gargoylesoftware.htmlunit.StringWebResponse)1 WebResponse (com.gargoylesoftware.htmlunit.WebResponse)1 ElementFactory (com.gargoylesoftware.htmlunit.html.ElementFactory)1 HtmlBody (com.gargoylesoftware.htmlunit.html.HtmlBody)1 HtmlMeta (com.gargoylesoftware.htmlunit.html.HtmlMeta)1 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)1 HtmlPageTest (com.gargoylesoftware.htmlunit.html.HtmlPageTest)1 ScriptElement (com.gargoylesoftware.htmlunit.html.ScriptElement)1 IOException (java.io.IOException)1 XNIException (org.apache.xerces.xni.XNIException)1 SAXException (org.xml.sax.SAXException)1