@Test void test321() { String html = "<head><title>test & blah</title><body><h1>test & blah<b>bold</b></h1></body>"; Jerry doc = Jerry.jerry(html); Jerry title = doc.$("title"); assertEquals("test & blah", title.eq(0).html()); assertEquals("test & blah", title.eq(0).text()); Jerry h1 = doc.$("h1"); assertEquals("test & blah<b>bold</b>", h1.eq(0).html()); assertEquals("test & blahbold", h1.eq(0).text()); }
Jerry form = find(formCssSelector); Jerry singleForm = new Jerry(this, node); singleForm.$("input").each(($inputTag, index) -> { String type = $inputTag.attr("type"); String name = $inputTag.attr("name"); if (name == null) { return true; String tagValue = $inputTag.attr("value"); singleForm.$("select").each(($selectTag, index) -> { final String name = $selectTag.attr("name"); $selectTag.$("option").each(($optionTag, index1) -> { if ($optionTag.nodes[0].hasAttribute("selected")) { String tagValue = $optionTag.attr("value"); singleForm.$("textarea").each(($textarea, index) -> { String name = $textarea.attr("name"); String value = $textarea.text();
/** * Gets the descendants of each element in the current set of matched elements, * filtered by a selector. */ public Jerry find(final String cssSelector) { final List<Node> result = new NodeList(); if (nodes.length > 0) { for (Node node : nodes) { NodeSelector nodeSelector = createNodeSelector(node); List<Node> filteredNodes = nodeSelector.select(cssSelector); result.addAll(filteredNodes); } } return new Jerry(this, result); }
@Test void testPseudoSelected() { String html = readFile("pseudoSelected.html"); String htmlOK = readFile("pseudoSelected-ok.html"); Jerry doc = jerry(html); final StringBuilder str = new StringBuilder(); doc.$("select option:selected").each(($this, index) -> { str.append($this.text()).append(' '); return true; }); doc.$("div").text(str.toString()); assertEquals(htmlOK, actualHtml(doc)); }
@Test void test279() { String html = "<html><body><div>x</div></body></html>"; Jerry $ = Jerry.jerry(html); $.$("body").html(""); assertEquals("<html><body></body></html>", $.html()); $.$("body").append(""); assertEquals("<html><body></body></html>", $.html()); $.$("body").before(""); assertEquals("<html><body></body></html>", $.html()); }
@Test void testPseudoInput() { String html = readFile("pseudoInput.html"); String htmlOK = readFile("pseudoInput-ok.html"); Jerry doc = jerry(html); Jerry allInputs = doc.$(":input"); Jerry formChildren = doc.$("form > *"); doc.$("#messages").text("Found " + allInputs.length() + " inputs and the form has " + formChildren.length() + " children."); assertEquals(htmlOK, actualHtml(doc)); }
@Test void testHtmlAll() { Jerry j = Jerry.jerry("<div><span>1</span></div><div><span>2</span></div>"); assertEquals(j.htmlAll(true), j.htmlAll(false)); j = j.$("div"); assertEquals("<span>1</span>", j.html()); assertEquals("<span>1</span><span>2</span>", j.htmlAll(false)); assertEquals("<div><span>1</span></div><div><span>2</span></div>", j.htmlAll(true)); Jerry j2 = j.find("span"); assertEquals(2, j2.length()); assertEquals("12", j2.text()); assertEquals("1", j2.html()); assertEquals("12", j2.htmlAll(false)); assertEquals("<span>1</span><span>2</span>", j2.htmlAll(true)); }
@Test void testIs() { String html = readFile("is.html"); String htmlOK = readFile("is-ok.html"); Jerry doc = jerry(html); doc.$("div").each(($this, index) -> { if ($this.is(":first-child")) { $this.text("Its the first div."); } else if ($this.is(".blue,.red")) { $this.text("Its a blue or red div."); } else if ($this.is(":contains(Peter)")) { $this.text("Its Peter!"); } else { $this.html("Its nothing <em>special</em>."); } return true; }); assertEquals(htmlOK, actualHtml(doc)); }
@Test void testChildrenVsContents() { String html = readFile("contents.html"); String htmlOK = readFile("contents-ok.html"); Jerry doc = jerry(html); doc.$("span.contents").contents().remove(); doc.$("span.children").children().remove(); int count = doc.$(".count").contents().length(); doc.$(".count").text(Integer.toString(count)); assertEquals(htmlOK, actualHtml(doc)); }
@Test void testPseudoCheckbox() { String html = readFile("pseudoCheckbox.html"); String htmlOK = readFile("pseudoCheckbox-ok.html"); Jerry doc = jerry(html); Jerry input = doc.$("form input:checkbox").wrap("<span></span>").parent().css("background", "yellow", "border", "3px red solid"); doc.$("div").text("For this type jQuery found " + input.length() + ".").css("color", "red"); assertEquals(htmlOK, actualHtml(doc)); }
@Test void testFirstNotDirectly() { Jerry doc = Jerry.jerry().parse("<html><div>one</div><p>two</p><div>three</div><p>four</p></html>"); assertEquals(2, doc.$("div").size()); assertEquals(2, doc.$("p").size()); assertEquals("one", doc.$("div").first().text()); assertEquals("two", doc.$("p").first().text()); assertEquals("four", doc.$("p").last().text()); assertEquals("three", doc.$("div").last().text()); }
@Test void testPseudoEq2() { String html = readFile("pseudoEq2.html"); String htmlOK = readFile("pseudoEq2-ok.html"); Jerry doc = jerry(html); doc.$("ul.nav li:eq(1)").css("backgroundColor", "#ff0"); doc.$("ul.nav").each(($this, index) -> { $this.find("li:eq(1)").css("fontStyle", "italic"); return true; }); doc.$("ul.nav li:nth-child(2)").css("color", "red"); assertEquals(htmlOK, actualHtml(doc)); }
@Override public String extract(String data) { Jerry doc = jerry(data); String result = ""; switch (outType) { case TYPE_TEXT: result = parse(doc.$(query).first().text()); break; case TYPE_HTML: result = parse(doc.$(query).first().html()); break; default: result = parse(doc.$(query).first().attr(outType)); break; } return result; }
@Test void testHtmlNodesOwner() { Jerry doc = Jerry.jerry().parse("<div>1<div id='x'>2</div>3</div>"); doc.$("#x").html("<span>wow</span>"); assertEquals("<div>1<div id=\"x\"><span>wow</span></div>3</div>", doc.html()); Element divx = doc.get(0).getChildElement(0).getChildElement(0); assertSame(doc.get(0), divx.getOwnerDocument()); assertEquals("span", divx.getChildElement(0).getNodeName()); assertSame(doc.get(0), divx.getChildElement(0).getOwnerDocument()); assertSame(doc.get(0), divx.getChildElement(0).getChild(0).getOwnerDocument()); }
@Test void testCustomerDetails() { Jerry doc = Jerry.jerry("<p>to<br>{customerDetails}</p>"); doc.$("p").each(($this, index) -> { String innerHtml = $this.html(); innerHtml = StringUtil.replace(innerHtml, "{customerDetails}", "Jodd <b>rocks</b>"); $this.html(innerHtml); return true; }); String newHtml = doc.html(); assertEquals("<p>to<br>Jodd <b>rocks</b></p>", newHtml); }