Java Code Examples for org.jsoup.nodes.Document#appendElement()

The following examples show how to use org.jsoup.nodes.Document#appendElement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UsageStatisticsExporterTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void should_append_script_element_to_the_body(){
    Document document = new Document("");
    Element html = document.appendElement("html");
    html.appendElement("body");

    UsageStatisticsExporter.exportUsageStatisticsToDocument(document);

    String entries = UsageStatistics.getEntries().map(entry -> {
        JsonObject json = Json.createObject();

        json.put("is", entry.getName());
        json.put("version", entry.getVersion());

        return json.toString();
    }).collect(Collectors.joining(","));

    String expected = StringUtil.normaliseWhitespace(
            "window.Vaadin = window.Vaadin || {};\n" +
                    "window.Vaadin.registrations = window.Vaadin.registrations || [];\n"
                    + "window.Vaadin.registrations.push(" + entries
                    + ");");

    Elements bodyInlineElements = document.body().getElementsByTag("script");
    assertEquals(StringUtil.normaliseWhitespace(expected), bodyInlineElements.get(0).childNode(0).outerHtml());
}
 
Example 2
Source File: BootstrapHandler.java    From flow with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the bootstrap page for the given context.
 *
 * @param context
 *            Context to generate bootstrap page for.
 * @return A document with the corresponding HTML page.
 */
@Override
public Document getBootstrapPage(BootstrapContext context) {
    DeploymentConfiguration config = context.getSession()
            .getConfiguration();

    Document document = new Document("");
    DocumentType doctype = new DocumentType("html", "", "");
    document.appendChild(doctype);

    Element html = document.appendElement("html");
    html.attr("lang", context.getUI().getLocale().getLanguage());
    Element head = html.appendElement("head");
    html.appendElement("body");

    List<Element> dependenciesToInlineInBody = setupDocumentHead(head,
            context);
    dependenciesToInlineInBody.forEach(
            dependency -> document.body().appendChild(dependency));
    setupDocumentBody(document);

    document.outputSettings().prettyPrint(false);

    BootstrapUtils.getInlineTargets(context)
            .ifPresent(targets -> handleInlineTargets(context, head,
                    document.body(), targets));

    BootstrapUtils.getInitialPageSettings(context).ifPresent(
            initialPageSettings -> handleInitialPageSettings(context,
                    head, initialPageSettings));

    if (!config.isProductionMode()) {
        UsageStatisticsExporter
                .exportUsageStatisticsToDocument(document);
    }

    setupPwa(document, context);

    if (!config.isProductionMode()) {
        showWebpackErrors(document);
    }

    BootstrapPageResponse response = new BootstrapPageResponse(
            context.getRequest(), context.getSession(),
            context.getResponse(), document, context.getUI(),
            context.getUriResolver());
    context.getSession().getService().modifyBootstrapPage(response);

    return document;
}