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;}"));
}
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());
}
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());
}
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);
}
}
}
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));
}
Aggregations