use of org.loboevolution.html.dom.HTMLElement in project LoboEvolution by LoboEvolution.
the class LoboUnitTest method checkSelectorsTest.
/**
* <p>checkSelectorsTest.</p>
*
* @param html a {@link java.lang.String} object.
* @param result1 a {@link java.lang.String} object.
* @param result2 a {@link java.lang.String} object.
*/
public void checkSelectorsTest(final String html, final String result1, final String result2) {
HTMLDocumentImpl doc = loadHtml(html);
Window window = doc.getDefaultView();
HTMLElement div = (HTMLElement) doc.getElementById("myId");
HTMLElement div2 = (HTMLElement) doc.getElementById("myId2");
ComputedCSSStyleDeclaration computedStyle = (ComputedCSSStyleDeclaration) window.getComputedStyle(div);
ComputedCSSStyleDeclaration computedStyle2 = (ComputedCSSStyleDeclaration) window.getComputedStyle(div2);
assertEquals(result1, computedStyle.getColor());
assertEquals(result2, computedStyle2.getColor());
}
use of org.loboevolution.html.dom.HTMLElement in project LoboEvolution by LoboEvolution.
the class HtmlContent method getMediaList.
/**
* <p>getMediaList.</p>
*
* @return a {@link java.util.List} object.
*/
public List<MetaInfo> getMediaList() {
final List<MetaInfo> infoList = new ArrayList<>();
final HTMLDocumentImpl doc = (HTMLDocumentImpl) this.document;
HTMLCollectionImpl nodeList = (HTMLCollectionImpl) doc.getElementsByTagName("img");
if (nodeList == null) {
return null;
}
nodeList.forEach(node -> {
if (node instanceof HTMLElement) {
final HTMLElement element = (HTMLElement) node;
String src = element.getAttribute("src");
if (Strings.isNotBlank(src)) {
if (!Urls.isAbsolute(src)) {
src = doc.getFullURL(src).toString();
}
if (src.startsWith("//")) {
src = "http:" + src;
}
infoList.add(MetaInfo.builder().name(src).build());
}
}
});
nodeList = (HTMLCollectionImpl) doc.getElementsByTagName("link");
nodeList.forEach(node -> {
if (node instanceof HTMLElement) {
final HTMLElement element = (HTMLElement) node;
final String rel = element.getAttribute("rel");
String href = element.getAttribute("href");
if ("icon".equalsIgnoreCase(rel) && Strings.isNotBlank(href)) {
if (!Urls.isAbsolute(href)) {
href = doc.getFullURL(href).toString();
}
if (href.startsWith("//")) {
href = "http:" + href;
}
infoList.add(MetaInfo.builder().name(href).build());
}
}
});
return infoList;
}
use of org.loboevolution.html.dom.HTMLElement in project LoboEvolution by LoboEvolution.
the class HTMLDocumentTest method testCreateElement.
@Test
public void testCreateElement() {
Element elm = document.createElement("link");
assertTrue(elm instanceof HTMLLinkElement);
elm = document.createElement("LINK");
assertTrue(elm instanceof HTMLLinkElement);
assertEquals("LINK", elm.getLocalName());
assertEquals("LINK", elm.getTagName());
elm = document.createElement("style");
assertTrue(elm instanceof HTMLLinkElement);
elm = document.createElement("STYLE");
assertTrue(elm instanceof HTMLLinkElement);
assertEquals("style", elm.getLocalName());
HTMLElement html = (HTMLElement) document.createElement("html");
try {
elm.appendChild(html);
fail("Must throw exception");
} catch (DOMException e) {
assertEquals(DOMException.HIERARCHY_REQUEST_ERR, e.getCode());
}
try {
document.createElement(null);
fail("Must throw exception");
} catch (DOMException e) {
assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
}
try {
document.createElement("");
fail("Must throw exception");
} catch (DOMException e) {
assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
}
try {
document.createElement("\u0000");
fail("Must throw exception");
} catch (DOMException e) {
assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
}
try {
document.createElement("<");
fail("Must throw exception");
} catch (DOMException e) {
assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
}
try {
document.createElement(">");
fail("Must throw exception");
} catch (DOMException e) {
assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
}
}
use of org.loboevolution.html.dom.HTMLElement in project LoboEvolution by LoboEvolution.
the class HTMLDocumentTest method testReplaceChild.
@Test
public void testReplaceChild() {
HTMLElement html = (HTMLElement) document.getDocumentElement();
Element head = (HTMLElement) html.getElementsByTagName("head").item(0);
Element body = (HTMLElement) html.getElementsByTagName("body").item(0);
Element newBody = document.createElement("body");
Element newHead = document.createElement("head");
try {
html.replaceChild(newBody, head);
fail("Should throw an exception");
} catch (DOMException e) {
assertEquals(DOMException.HIERARCHY_REQUEST_ERR, e.getCode());
}
try {
html.replaceChild(newHead, body);
fail("Should throw an exception");
} catch (DOMException e) {
assertEquals(DOMException.HIERARCHY_REQUEST_ERR, e.getCode());
}
}
use of org.loboevolution.html.dom.HTMLElement in project LoboEvolution by LoboEvolution.
the class HTMLDocumentTest method testContains.
@Test
public void testContains() {
HTMLElement docelm = (HTMLElement) document.getDocumentElement();
assertTrue(document.contains(document));
assertTrue(document.contains(docelm));
assertTrue(docelm.contains(docelm));
assertFalse(docelm.contains(document));
Element h1 = document.getElementById("h1");
Element span1 = document.getElementById("span1");
assertTrue(document.contains(h1));
assertTrue(document.contains(span1));
assertTrue(docelm.contains(h1));
assertTrue(docelm.contains(span1));
assertFalse(h1.contains(docelm));
assertFalse(span1.contains(docelm));
assertFalse(h1.contains(document));
assertFalse(span1.contains(document));
assertFalse(h1.contains(span1));
assertFalse(span1.contains(h1));
}
Aggregations