Search in sources :

Example 1 with HTMLParser

use of com.gargoylesoftware.htmlunit.html.parser.HTMLParser in project htmlunit by HtmlUnit.

the class DOMImplementation method createHTMLDocument.

/**
 * Creates an {@link HTMLDocument}.
 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createHTMLDocument">
 *   createHTMLDocument (MDN)</a>
 *
 * @param titleObj the document title
 * @return the newly created {@link HTMLDocument}
 */
@JsxFunction
public HTMLDocument createHTMLDocument(final Object titleObj) {
    if (Undefined.isUndefined(titleObj) && getBrowserVersion().hasFeature(JS_DOMIMPLEMENTATION_CREATE_HTMLDOCOMENT_REQUIRES_TITLE)) {
        throw Context.reportRuntimeError("Title is required");
    }
    // com.gargoylesoftware.htmlunit.javascript.host.dom.DOMParser.parseFromString(String, Object)
    try {
        final WebWindow webWindow = getWindow().getWebWindow();
        final String html;
        if (Undefined.isUndefined(titleObj)) {
            html = "<html><head></head><body></body></html>";
        } else {
            html = "<html><head><title>" + Context.toString(titleObj) + "</title></head><body></body></html>";
        }
        final WebResponse webResponse = new StringWebResponse(html, UrlUtils.URL_ABOUT_BLANK);
        final HtmlPage page = new HtmlPage(webResponse, webWindow);
        // According to spec and behavior of function in browsers new document
        // has no location object and is not connected with any window
        page.setEnclosingWindow(null);
        // document knows the window but is not the windows document
        final HTMLDocument document = new HTMLDocument();
        document.setParentScope(getWindow());
        document.setPrototype(getPrototype(document.getClass()));
        // document.setWindow(getWindow());
        document.setDomNode(page);
        final HTMLParser htmlParser = webWindow.getWebClient().getPageCreator().getHtmlParser();
        htmlParser.parse(webResponse, page, false, false);
        return page.getScriptableObject();
    } catch (final IOException e) {
        throw Context.reportRuntimeError("Parsing failed" + e.getMessage());
    }
}
Also used : StringWebResponse(com.gargoylesoftware.htmlunit.StringWebResponse) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) StringWebResponse(com.gargoylesoftware.htmlunit.StringWebResponse) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) HTMLParser(com.gargoylesoftware.htmlunit.html.parser.HTMLParser) IOException(java.io.IOException) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 2 with HTMLParser

use of com.gargoylesoftware.htmlunit.html.parser.HTMLParser in project htmlunit by HtmlUnit.

the class AttributesTest method getNewInstanceForClassUnderTest.

/**
 * Creates a new instance of the class being tested.
 * @return the new instance
 * @throws Exception if the new object cannot be created
 */
private DomElement getNewInstanceForClassUnderTest(final HtmlPage page) throws Exception {
    final HTMLParser htmlParser = page.getWebClient().getPageCreator().getHtmlParser();
    final DomElement newInstance;
    if (classUnderTest_ == HtmlTableRow.class) {
        newInstance = htmlParser.getFactory(HtmlTableRow.TAG_NAME).createElement(page, HtmlTableRow.TAG_NAME, null);
    } else if (classUnderTest_ == HtmlTableHeaderCell.class) {
        newInstance = htmlParser.getFactory(HtmlTableHeaderCell.TAG_NAME).createElement(page, HtmlTableHeaderCell.TAG_NAME, null);
    } else if (classUnderTest_ == HtmlTableDataCell.class) {
        newInstance = htmlParser.getFactory(HtmlTableDataCell.TAG_NAME).createElement(page, HtmlTableDataCell.TAG_NAME, null);
    } else {
        final String tagName = (String) classUnderTest_.getField("TAG_NAME").get(null);
        newInstance = htmlParser.getFactory(tagName).createElement(page, tagName, null);
    }
    return newInstance;
}
Also used : HTMLParser(com.gargoylesoftware.htmlunit.html.parser.HTMLParser)

Example 3 with HTMLParser

use of com.gargoylesoftware.htmlunit.html.parser.HTMLParser 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 HTMLParser

use of com.gargoylesoftware.htmlunit.html.parser.HTMLParser 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 HTMLParser

use of com.gargoylesoftware.htmlunit.html.parser.HTMLParser in project htmlunit by HtmlUnit.

the class DOMParser method parseFromString.

/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Parses the given Unicode string into a DOM document.
 * @param scriptable the ScriptableObject this belongs to
 * @param str the Unicode string to be parsed
 * @param type the MIME type of the string -
 *        <code>text/html</code>, <code>text/xml</code>, <code>application/xml</code>,
 *        <code>application/xhtml+xml</code>, <code>image/svg+xml</code>. Must not be {@code null}.
 * @return the generated document
 * @throws IOException in case of error
 */
public static Document parseFromString(final HtmlUnitScriptable scriptable, final String str, final Object type) throws IOException {
    if (type == null || Undefined.isUndefined(type)) {
        throw Context.reportRuntimeError("Missing 'type' parameter");
    }
    if (MimeType.TEXT_XML.equals(type) || MimeType.APPLICATION_XML.equals(type) || MimeType.APPLICATION_XHTML.equals(type) || "image/svg+xml".equals(type)) {
        final XMLDocument document = new XMLDocument();
        document.setParentScope(scriptable.getParentScope());
        document.setPrototype(scriptable.getPrototype(XMLDocument.class));
        document.loadXML(str);
        return document;
    }
    if (MimeType.TEXT_HTML.equals(type)) {
        final WebWindow webWindow = scriptable.getWindow().getWebWindow();
        final WebClient webClient = webWindow.getWebClient();
        final WebResponse webResponse = new StringWebResponse(str, webWindow.getEnclosedPage().getUrl());
        // a similar impl is in
        // com.gargoylesoftware.htmlunit.javascript.host.dom.DOMImplementation.createHTMLDocument(Object)
        final HtmlPage page = new HtmlPage(webResponse, webWindow);
        page.setEnclosingWindow(null);
        final Window window = webWindow.getScriptableObject();
        // document knows the window but is not the windows document
        final HTMLDocument document = new HTMLDocument();
        document.setParentScope(window);
        document.setPrototype(window.getPrototype(document.getClass()));
        // document.setWindow(window);
        document.setDomNode(page);
        final HTMLParser htmlParser = webClient.getPageCreator().getHtmlParser();
        htmlParser.parse(webResponse, page, false, true);
        return page.getScriptableObject();
    }
    return null;
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) StringWebResponse(com.gargoylesoftware.htmlunit.StringWebResponse) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) StringWebResponse(com.gargoylesoftware.htmlunit.StringWebResponse) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) HTMLParser(com.gargoylesoftware.htmlunit.html.parser.HTMLParser) WebClient(com.gargoylesoftware.htmlunit.WebClient) XMLDocument(com.gargoylesoftware.htmlunit.javascript.host.xml.XMLDocument) WebWindow(com.gargoylesoftware.htmlunit.WebWindow)

Aggregations

HTMLParser (com.gargoylesoftware.htmlunit.html.parser.HTMLParser)5 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)3 StringWebResponse (com.gargoylesoftware.htmlunit.StringWebResponse)2 WebResponse (com.gargoylesoftware.htmlunit.WebResponse)2 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)2 XHtmlPage (com.gargoylesoftware.htmlunit.html.XHtmlPage)2 HTMLDocument (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument)2 URL (java.net.URL)2 WebClient (com.gargoylesoftware.htmlunit.WebClient)1 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)1 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)1 XMLDocument (com.gargoylesoftware.htmlunit.javascript.host.xml.XMLDocument)1 IOException (java.io.IOException)1