- Common ways to obtain Jerry$JerryParser
private void myMethod () {Jerry$JerryParser j =
new JerryParser()
DOMBuilder domBuilder;new JerryParser(domBuilder)
- Smart code suggestions by Codota
}
@Test void testIterator2() { Jerry doc = Jerry.jerry().parse("<div id='one' class='foo'>one</div><div id='two' class='foo'>two</div>"); Iterator<Jerry> iterator = doc.find(".notfound").iterator(); String result = ""; while (iterator.hasNext()) { Jerry j = iterator.next(); result += j.attr("id"); } assertEquals("", result); }
@Test void testAbstractFormController() throws IOException { File file = new File(testDataRoot, "AbstractFormController.html"); String content = FileUtil.readString(file); Jerry.JerryParser jerryParser = new Jerry.JerryParser(); //jerryParser.getDOMBuilder().setCalculatePosition(true); Jerry doc = jerryParser.parse(content); // parse doc.$("a").each(($this, index) -> { assertEquals("<a name=\"navbar_top\"><!-- --></a>", $this.get()[0].getHtml()); return false; }); }
@Test void testNamespaces() throws IOException { File file = new File(testDataRoot, "namespace.xml"); LagartoDOMBuilder lagartoDOMBuilder = new LagartoDOMBuilder(); lagartoDOMBuilder.enableXmlMode(); lagartoDOMBuilder.getConfig().setCalculatePosition(true); Document doc = lagartoDOMBuilder.parse(FileUtil.readString(file)); assertTrue(doc.check()); Element cfgTestElement = (Element) doc.getChild(1); assertEquals("cfg:test", cfgTestElement.getNodeName()); Element cfgNode = (Element) cfgTestElement.getChild(0); assertEquals("cfg:node", cfgNode.getNodeName()); Jerry.JerryParser jerryParser = new Jerry.JerryParser(); ((LagartoDOMBuilder) jerryParser.getDOMBuilder()).enableXmlMode(); Jerry jerry = jerryParser.parse(FileUtil.readString(file)); final StringBuilder result = new StringBuilder(); jerry.$("cfg\\:test").each(($this, index) -> { result.append($this.$("cfg\\:node").text()); return true; }); assertEquals("This is a text", result.toString()); }
@Test void testAppend2() { Jerry.JerryParser jerryParser = Jerry.jerry(); ((LagartoDOMBuilder) jerryParser.getDOMBuilder()).enableXmlMode(); Jerry doc = jerryParser.parse("<xml><book><name>Foo</name></book></xml>"); Jerry book = doc.$("book"); book.append("<br>"); assertEquals("<xml><book><name>Foo</name><br></br></book></xml>", doc.html()); }
@Test void testAppendContent3() { Jerry.JerryParser jerryParser = Jerry.jerry(); ((LagartoDOMBuilder) jerryParser.getDOMBuilder()).enableXhtmlMode(); Jerry doc = jerryParser.parse("<xml><book isbn='123'><name>Foo</name></book></xml>"); Jerry book = doc.$("book"); book.append("<br>"); assertEquals("<xml><book isbn=\"123\"><name>Foo</name><br/></book></xml>", doc.html()); }
/** * Creates new {@link jodd.jerry.Jerry.JerryParser Jerry runner} with * provided implementation of {@link jodd.lagarto.dom.DOMBuilder}. */ public static JerryParser jerry(final DOMBuilder domBuilder) { return new JerryParser(domBuilder); }
@Test void testAppend1() { Jerry.JerryParser jerryParser = Jerry.jerry(); ((LagartoDOMBuilder) jerryParser.getDOMBuilder()).enableHtmlMode(); Jerry doc = jerryParser.parse("<xml><book><name>Foo</name></book></xml>"); Jerry book = doc.$("book"); book.append("<br>"); assertEquals("<xml><book><name>Foo</name><br></book></xml>", doc.html()); }
@Test void testPortletUtils() throws IOException { File file = new File(testDataRoot, "PortletUtils.html"); String content = FileUtil.readString(file); Jerry.JerryParser jerryParser = new Jerry.JerryParser(); //jerryParser.getDOMBuilder().setCalculatePosition(true); Jerry doc = jerryParser.parse(content); // parse doc.$("a").each(($this, index) -> { assertEquals("<a name=\"navbar_top\"><!-- --></a>", $this.get()[0].getHtml()); return false; }); }
@Test void testConditionalTags() { Jerry.JerryParser jerry = new Jerry.JerryParser(); ((LagartoDOMBuilder) jerry.getDOMBuilder()).getConfig().setIgnoreComments(true); Jerry doc = jerry.parse( "<html>" + " <!--[if lt IE 7]> <body class=\"ie ie6 ie_lte_9 ie_lte_8 ie_lte_7\"> <![endif]-->\n" + " <!--[if IE 7]> <body class=\"ie ie7 ie_lte_9 ie_lte_8 ie_lte_7\"> <![endif]-->\n" + " <!--[if IE 8]> <body class=\"ie ie8 ie_lte_9 ie_lte_8\"> <![endif]-->\n" + " <!--[if IE 9]> <body class=\"ie ie9 ie_lte_9\"> <![endif]-->\n" + " <!--[if gt IE 9]> <body class=\"ie\"> <![endif]-->\n" + " <!--[if !IE]>xxx--><body><!--<![endif]-->\n" + "</body></html>" ); String html = doc.html(); html = StringUtil.removeChars(html, " \n\r"); assertEquals("<html><body></body></html>", html); }
@Test void testConditionalTags2() { Jerry.JerryParser jerry = new Jerry.JerryParser(); ((LagartoDOMBuilder) jerry.getDOMBuilder()).getConfig() .setIgnoreComments(true) .setEnableConditionalComments(true) .setCondCommentIEVersion(8); Jerry doc = jerry.parse( "<html>" + " <!--[if lt IE 7]> <body class=\"ie ie6 ie_lte_9 ie_lte_8 ie_lte_7\"> <![endif]-->\n" + " <!--[if IE 7]> <body class=\"ie ie7 ie_lte_9 ie_lte_8 ie_lte_7\"> <![endif]-->\n" + " <!--[if IE 8]> <body class=\"ie ie8 ie_lte_9 ie_lte_8\"> <![endif]-->\n" + " <!--[if IE 9]> <body class=\"ie ie9 ie_lte_9\"> <![endif]-->\n" + " <!--[if gt IE 9]> <body class=\"ie\"> <![endif]-->\n" + " <!--[if !IE]>xxx--><body><!--<![endif]-->\n" + "</body></html>" ); String html = doc.html(); html = StringUtil.removeChars(html, " \n\r"); assertEquals("<html><bodyclass=\"ieie8ie_lte_9ie_lte_8\"></body></html>", html); }
@Test void testKelkoo() throws Exception { File file = new File(testDataRoot, "kelkoo.html"); Jerry jerry; try { jerry = Jerry.jerry().parse(FileUtil.readString(file)); } catch (Exception ex) { fail(ex.toString()); throw ex; } Element script = (Element) jerry.$("script").get(0); assertEquals("script", script.getNodeName()); assertEquals(6, script.getAttributesCount()); assertEquals("src", script.getAttribute(0).getName()); assertEquals("data-config", script.getAttribute(1).getName()); assertEquals("ext\\u00e9rieur|barbecue,", script.getAttribute(2).getName()); assertEquals("planchaaccessoires\":\"http:\\", script.getAttribute(3).getName()); assertEquals("www.kelkoo.fr\"}'", script.getAttribute(4).getName()); assertEquals("data-adsense-append", script.getAttribute(5).getName()); }
@Test void testAppendContent2() { Jerry.JerryParser jerryParser = Jerry.jerry(); ((LagartoDOMBuilder) jerryParser.getDOMBuilder()).enableXmlMode(); Jerry doc = jerryParser.parse("<xml><book isbn='123'><name>Foo</name></book></xml>"); Jerry book = doc.$("book"); book.append("<br>"); assertEquals("<xml><book isbn=\"123\"><name>Foo</name><br></br></book></xml>", doc.html()); }
@Test void testConditionalTags3() { Jerry.JerryParser jerry = new Jerry.JerryParser(); ((LagartoDOMBuilder) jerry.getDOMBuilder()).getConfig() .setIgnoreComments(true) .setEnableConditionalComments(true) .setCondCommentIEVersion(10); Jerry doc = jerry.parse( "<html>" + " <!--[if lt IE 7]> <body class=\"ie ie6 ie_lte_9 ie_lte_8 ie_lte_7\"> <![endif]-->\n" + " <!--[if IE 7]> <body class=\"ie ie7 ie_lte_9 ie_lte_8 ie_lte_7\"> <![endif]-->\n" + " <!--[if IE 8]> <body class=\"ie ie8 ie_lte_9 ie_lte_8\"> <![endif]-->\n" + " <!--[if IE 9]> <body class=\"ie ie9 ie_lte_9\"> <![endif]-->\n" + " <!--[if gt IE 9]> <body class=\"ie\"> <![endif]-->\n" + " <!--[if !IE]><!--> <body> <!--<![endif]--> \n" + "</body></html>" ); String html = doc.html(); html = StringUtil.removeChars(html, " \n\r"); assertEquals("<html><bodyclass=\"ie\"></body></html>", html); } }
@Test void testCustomPseudoFunction() { PseudoFunctionSelector.registerPseudoFunction(MyPseudoFunction.class); Jerry doc = Jerry.jerry().parse("<body><p>not found</p><div>This!</div></body>"); Jerry p = doc.$(":super-fn(3)"); assertEquals(1, p.size()); assertEquals("This!", p.text()); }
@Test void testStuck() throws IOException { File file = new File(testDataRoot, "stuck.html.gz"); InputStream in = new GZIPInputStream(new FileInputStream(file)); ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamUtil.copy(in, out); in.close(); Jerry.JerryParser jerryParser = new Jerry.JerryParser(); // LagartoDOMBuilder lagartoDOMBuilder = (LagartoDOMBuilder) jerryParser.getDOMBuilder(); // lagartoDOMBuilder.setParsingErrorLogLevelName("ERROR"); Jerry doc = jerryParser.parse(out.toString("UTF-8")); // parse try { doc.$("a").each(($this, index) -> { assertEquals("Go to Database Directory", $this.html().trim()); return false; }); } catch (StackOverflowError stackOverflowError) { fail("stack overflow!"); } } }
@Test void testCustomPseudoClass() { PseudoClassSelector.registerPseudoClass(MyPseudoClass.class); Jerry doc = Jerry.jerry().parse("<body><p jodd-attr='1'>found</p><p>not found</p></body>"); Jerry p = doc.$("p:jjjjj"); assertEquals(1, p.size()); assertEquals("found", p.text()); }
/** * Just creates new {@link jodd.jerry.Jerry.JerryParser Jerry runner} to separate * parser creation and creation of new Jerry instances. */ public static JerryParser jerry() { return new JerryParser(); }
((LagartoDOMBuilder) jerryParser.getDOMBuilder()).enableHtmlMode(); Jerry doc = jerryParser.parse(str); Document document = (Document) doc.get(0); Element divNode = (Element) document.getChild(0); ((LagartoDOMBuilder) jerryParser.getDOMBuilder()).getConfig().setCaseSensitive(true); doc = jerryParser.parse(str); document = (Document) doc.get(0); divNode = (Element) document.getChild(0);
/** * Parses input content and creates new <code>Jerry</code>. */ public static Jerry jerry(final String content) { return jerry().parse(content); }
/** * Parses input sequence and creates new <code>Jerry</code>. */ public static Jerry jerry(final char[] content) { return jerry().parse(content); }