Search in sources :

Example 71 with Elements

use of org.jsoup.select.Elements in project flow by vaadin.

the class BootstrapHandlerTest method no_body_size_or_page_configurator_still_adds_margin_for_body.

// 2344
@Test
public void no_body_size_or_page_configurator_still_adds_margin_for_body() throws InvalidRouteConfigurationException {
    initUI(testUI, createVaadinRequest(), Collections.singleton(RootNavigationTarget.class));
    Document page = BootstrapHandler.getBootstrapPage(new BootstrapContext(request, null, session, testUI));
    Elements allElements = page.head().getAllElements();
    Optional<Element> styleTag = allElements.stream().filter(element -> element.tagName().equals("style")).findFirst();
    Assert.assertTrue("Expected a style element in head.", styleTag.isPresent());
    Assert.assertTrue("The first style tag should start with body style containing margin", styleTag.get().toString().startsWith("<style type=\"text/css\">body {margin:0;}"));
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) Arrays(java.util.Arrays) Component(com.vaadin.flow.component.Component) JavaScript(com.vaadin.flow.component.dependency.JavaScript) Inline(com.vaadin.flow.component.page.Inline) BootstrapContext(com.vaadin.flow.server.BootstrapHandler.BootstrapContext) TargetElement(com.vaadin.flow.component.page.TargetElement) Registration(com.vaadin.flow.shared.Registration) PageTitle(com.vaadin.flow.router.PageTitle) Router(com.vaadin.flow.router.Router) Route(com.vaadin.flow.router.Route) RouteAlias(com.vaadin.flow.router.RouteAlias) Theme(com.vaadin.flow.theme.Theme) Assert.assertThat(org.junit.Assert.assertThat) Locale(java.util.Locale) Element(org.jsoup.nodes.Element) After(org.junit.After) UI(com.vaadin.flow.component.UI) Set(java.util.Set) StandardCharsets(java.nio.charset.StandardCharsets) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Document(org.jsoup.nodes.Document) LoadMode(com.vaadin.flow.shared.ui.LoadMode) BodySize(com.vaadin.flow.component.page.BodySize) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) Elements(org.jsoup.select.Elements) ApplicationConstants(com.vaadin.flow.shared.ApplicationConstants) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) Matchers(org.mockito.Matchers) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) Dependency(com.vaadin.flow.shared.ui.Dependency) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) HttpServletRequest(javax.servlet.http.HttpServletRequest) Tag(com.vaadin.flow.component.Tag) AbstractTheme(com.vaadin.flow.theme.AbstractTheme) Before(org.junit.Before) Text(com.vaadin.flow.component.Text) RouterLayout(com.vaadin.flow.router.RouterLayout) Html(com.vaadin.flow.component.Html) StyleSheet(com.vaadin.flow.component.dependency.StyleSheet) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) InlineTemplate(com.vaadin.flow.template.angular.InlineTemplate) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) Mockito(org.mockito.Mockito) Assert(org.junit.Assert) Collections(java.util.Collections) Viewport(com.vaadin.flow.component.page.Viewport) ParentLayout(com.vaadin.flow.router.ParentLayout) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) TargetElement(com.vaadin.flow.component.page.TargetElement) Element(org.jsoup.nodes.Element) BootstrapContext(com.vaadin.flow.server.BootstrapHandler.BootstrapContext) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) Test(org.junit.Test)

Example 72 with Elements

use of org.jsoup.select.Elements in project flow by vaadin.

the class BootstrapHandlerTest method page_configurator_adds_link.

// 3036
@Test
public void page_configurator_adds_link() throws InvalidRouteConfigurationException {
    initUI(testUI, createVaadinRequest(), Collections.singleton(InitialPageConfiguratorLinks.class));
    Document page = BootstrapHandler.getBootstrapPage(new BootstrapContext(request, null, session, testUI));
    Elements allElements = page.head().getAllElements();
    Assert.assertEquals("<link href=\"icons/favicon.ico\" rel=\"shortcut icon\">", allElements.get(allElements.size() - 2).toString());
    Assert.assertEquals("<link href=\"icons/icon-192.png\" rel=\"icon\" sizes=\"192x192\">", allElements.get(allElements.size() - 1).toString());
}
Also used : BootstrapContext(com.vaadin.flow.server.BootstrapHandler.BootstrapContext) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) Test(org.junit.Test)

Example 73 with Elements

use of org.jsoup.select.Elements in project flow by vaadin.

the class BootstrapHandlerTest method testBootstrapListener.

@Test
public void testBootstrapListener() throws ServiceException {
    List<BootstrapListener> listeners = new ArrayList<>(3);
    AtomicReference<VaadinUriResolver> resolver = new AtomicReference<>();
    listeners.add(evt -> evt.getDocument().head().getElementsByTag("script").remove());
    listeners.add(evt -> {
        resolver.set(evt.getUriResolver());
        evt.getDocument().head().appendElement("script").attr("src", "testing.1");
    });
    listeners.add(evt -> evt.getDocument().head().appendElement("script").attr("src", "testing.2"));
    Mockito.when(service.createInstantiator()).thenReturn(new MockInstantiator(event -> listeners.forEach(event::addBootstrapListener)));
    initUI(testUI);
    BootstrapContext bootstrapContext = new BootstrapContext(request, null, session, testUI);
    Document page = BootstrapHandler.getBootstrapPage(bootstrapContext);
    Elements scripts = page.head().getElementsByTag("script");
    assertEquals(2, scripts.size());
    assertEquals("testing.1", scripts.get(0).attr("src"));
    assertEquals("testing.2", scripts.get(1).attr("src"));
    Assert.assertNotNull(resolver.get());
    Assert.assertEquals(bootstrapContext.getUriResolver(), resolver.get());
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) Arrays(java.util.Arrays) Component(com.vaadin.flow.component.Component) JavaScript(com.vaadin.flow.component.dependency.JavaScript) Inline(com.vaadin.flow.component.page.Inline) BootstrapContext(com.vaadin.flow.server.BootstrapHandler.BootstrapContext) TargetElement(com.vaadin.flow.component.page.TargetElement) Registration(com.vaadin.flow.shared.Registration) PageTitle(com.vaadin.flow.router.PageTitle) Router(com.vaadin.flow.router.Router) Route(com.vaadin.flow.router.Route) RouteAlias(com.vaadin.flow.router.RouteAlias) Theme(com.vaadin.flow.theme.Theme) Assert.assertThat(org.junit.Assert.assertThat) Locale(java.util.Locale) Element(org.jsoup.nodes.Element) After(org.junit.After) UI(com.vaadin.flow.component.UI) Set(java.util.Set) StandardCharsets(java.nio.charset.StandardCharsets) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Document(org.jsoup.nodes.Document) LoadMode(com.vaadin.flow.shared.ui.LoadMode) BodySize(com.vaadin.flow.component.page.BodySize) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) Elements(org.jsoup.select.Elements) ApplicationConstants(com.vaadin.flow.shared.ApplicationConstants) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) Matchers(org.mockito.Matchers) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) Dependency(com.vaadin.flow.shared.ui.Dependency) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) HttpServletRequest(javax.servlet.http.HttpServletRequest) Tag(com.vaadin.flow.component.Tag) AbstractTheme(com.vaadin.flow.theme.AbstractTheme) Before(org.junit.Before) Text(com.vaadin.flow.component.Text) RouterLayout(com.vaadin.flow.router.RouterLayout) Html(com.vaadin.flow.component.Html) StyleSheet(com.vaadin.flow.component.dependency.StyleSheet) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) InlineTemplate(com.vaadin.flow.template.angular.InlineTemplate) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) Mockito(org.mockito.Mockito) Assert(org.junit.Assert) Collections(java.util.Collections) Viewport(com.vaadin.flow.component.page.Viewport) ParentLayout(com.vaadin.flow.router.ParentLayout) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) AtomicReference(java.util.concurrent.atomic.AtomicReference) BootstrapContext(com.vaadin.flow.server.BootstrapHandler.BootstrapContext) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) Test(org.junit.Test)

Example 74 with Elements

use of org.jsoup.select.Elements in project AisenWeiBo by wangdan.

the class VideoService method getPicture.

public static void getPicture(VideoBean video) throws Exception {
    if (TextUtils.isEmpty(AppContext.getAccount().getCookie())) {
        throw new TaskException("123", "解析链接失败");
    }
    HttpConfig config = new HttpConfig();
    config.baseUrl = video.getShortUrl();
    config.cookie = AppContext.getAccount().getCookie();
    config.addHeader("Content-Type", "text/html;charset=utf-8");
    Setting action = new Setting();
    action.setType("");
    action.setValue("");
    action.setDescription("");
    String response = new DefHttpUtility().doGet(config, action, null, String.class);
    Document dom = Jsoup.parse(response);
    video.setIdStr(KeyGenerator.generateMD5(video.getShortUrl()));
    Elements divs = dom.select("img");
    if (divs != null && divs.size() > 0) {
        video.setImage(divs.get(0).attr("src"));
        video.setImage(video.getImage().replace("bmiddle", "small").replace("thumbnail", "small"));
    }
    if (TextUtils.isEmpty(video.getImage())) {
        String longUrl = video.getLongUrl();
        if (TextUtils.isEmpty(longUrl)) {
            UrlsBean urlsBean = SinaSDK.getInstance(AppContext.getAccount().getAccessToken()).urlShort2Long(video.getShortUrl());
            if (urlsBean.getUrls() != null && urlsBean.getUrls().size() > 0) {
                longUrl = urlsBean.getUrls().get(0).getUrl_long();
                longUrl.replace("bmiddle", "small").replace("thumbnail", "small");
            }
        }
        if (!TextUtils.isEmpty(longUrl)) {
            video.setImage(longUrl);
        }
    }
}
Also used : TaskException(org.aisen.android.network.task.TaskException) Setting(org.aisen.android.common.setting.Setting) HttpConfig(org.aisen.android.network.http.HttpConfig) SpannableString(android.text.SpannableString) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) UrlsBean(org.aisen.weibo.sina.sinasdk.bean.UrlsBean) DefHttpUtility(org.aisen.android.network.http.DefHttpUtility)

Example 75 with Elements

use of org.jsoup.select.Elements in project flow by vaadin.

the class BootstrapHandler method handleInitialPageSettings.

private static void handleInitialPageSettings(BootstrapContext context, Element head, InitialPageSettings initialPageSettings) {
    if (initialPageSettings.getViewport() != null) {
        Elements viewport = head.getElementsByAttributeValue("name", VIEWPORT);
        if (!viewport.isEmpty() && viewport.size() == 1) {
            viewport.get(0).attr(CONTENT_ATTRIBUTE, initialPageSettings.getViewport());
        } else {
            head.appendElement(META_TAG).attr("name", VIEWPORT).attr(CONTENT_ATTRIBUTE, initialPageSettings.getViewport());
        }
    }
    initialPageSettings.getInline(InitialPageSettings.Position.PREPEND).stream().map(dependency -> createDependencyElement(context, dependency)).forEach(element -> insertElements(element, head::prependChild));
    initialPageSettings.getInline(InitialPageSettings.Position.APPEND).stream().map(dependency -> createDependencyElement(context, dependency)).forEach(element -> insertElements(element, head::appendChild));
    initialPageSettings.getElement(InitialPageSettings.Position.PREPEND).forEach(element -> insertElements(element, head::prependChild));
    initialPageSettings.getElement(InitialPageSettings.Position.APPEND).forEach(element -> insertElements(element, head::appendChild));
}
Also used : Inline(com.vaadin.flow.component.page.Inline) TargetElement(com.vaadin.flow.component.page.TargetElement) DocumentType(org.jsoup.nodes.DocumentType) LoggerFactory(org.slf4j.LoggerFactory) Json(elemental.json.Json) JsonArray(elemental.json.JsonArray) Parser(org.jsoup.parser.Parser) JsonValue(elemental.json.JsonValue) WebComponents(com.vaadin.flow.component.WebComponents) PushMode(com.vaadin.flow.shared.communication.PushMode) JsonUtil(elemental.json.impl.JsonUtil) Locale(java.util.Locale) Map(java.util.Map) Element(org.jsoup.nodes.Element) UI(com.vaadin.flow.component.UI) EnumMap(java.util.EnumMap) AnnotationReader(com.vaadin.flow.internal.AnnotationReader) PushConfiguration(com.vaadin.flow.component.PushConfiguration) Tag(org.jsoup.parser.Tag) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) Stream(java.util.stream.Stream) Document(org.jsoup.nodes.Document) LoadMode(com.vaadin.flow.shared.ui.LoadMode) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Jsoup(org.jsoup.Jsoup) DataNode(org.jsoup.nodes.DataNode) Elements(org.jsoup.select.Elements) ApplicationConstants(com.vaadin.flow.shared.ApplicationConstants) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) Dependency(com.vaadin.flow.shared.ui.Dependency) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) ArrayList(java.util.ArrayList) OutputStreamWriter(java.io.OutputStreamWriter) Properties(java.util.Properties) Logger(org.slf4j.Logger) UTF_8(java.nio.charset.StandardCharsets.UTF_8) BufferedWriter(java.io.BufferedWriter) ReflectTools(com.vaadin.flow.internal.ReflectTools) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Consumer(java.util.function.Consumer) AtmospherePushConnection(com.vaadin.flow.server.communication.AtmospherePushConnection) JsonObject(elemental.json.JsonObject) BufferedReader(java.io.BufferedReader) UidlWriter(com.vaadin.flow.server.communication.UidlWriter) Collections(java.util.Collections) InputStream(java.io.InputStream) Push(com.vaadin.flow.component.page.Push) Elements(org.jsoup.select.Elements)

Aggregations

Elements (org.jsoup.select.Elements)709 Element (org.jsoup.nodes.Element)490 Document (org.jsoup.nodes.Document)362 ArrayList (java.util.ArrayList)213 IOException (java.io.IOException)151 Test (org.junit.Test)110 URL (java.net.URL)58 List (java.util.List)47 Matcher (java.util.regex.Matcher)42 Pattern (java.util.regex.Pattern)34 HashMap (java.util.HashMap)30 InputStream (java.io.InputStream)29 Jsoup (org.jsoup.Jsoup)28 Configuration (com.vaadin.addon.charts.model.Configuration)27 File (java.io.File)26 JSONObject (org.json.JSONObject)26 JSONException (org.json.JSONException)25 Collectors (java.util.stream.Collectors)23 URISyntaxException (java.net.URISyntaxException)22 BootstrapContext (com.vaadin.flow.server.BootstrapHandler.BootstrapContext)20