public Node attr(String attributeName, Object value) { return attr(attributeName, String.valueOf(value)); }
public boolean checkTagLib(final Node n, RenderableList l, String dir, String offset, Set<String> imports) { if (n.name().equals("g:render")) { String model = n.attr("model"); if (model == null) model = "${[:]}"; String template = n.attr("template"); int i = template.lastIndexOf("/"); String path = ""; String collection = n.attr("collection"); if (collection != null) { Node fakeEach = new Node("g:each") .attr("in", n.attributes().remove("collection")) .add(n); var = "it"; n.attr("model", "[" + var + ": it]"); return checkTagLib(fakeEach, l, dir, offset, imports); } else {
@Test public void html_2() throws IOException { String s = "<div width=100px></div>"; Node root = new HtmlParser().parse(s); Node div = root.children().get(0); Assert.assertEquals(1, div.attributes().size()); Assert.assertEquals(0, div.children().size()); Assert.assertEquals("100px", div.attr("width")); s = "<div width=100px height=50px></div>"; root = new HtmlParser().parse(s); div = root.children().get(0); Assert.assertEquals(2, div.attributes().size()); Assert.assertEquals(0, div.children().size()); Assert.assertEquals("100px", div.attr("width")); Assert.assertEquals("50px", div.attr("height")); }
public boolean check(Node n) { boolean b = false; if (command.equals("text()")) { b = result.equals(n.text()); } else if (command.startsWith("@")) { b = result.equals(n.attr(command.substring(1))); } return b && (next == null || next.check(n)); } }
@Test public void gsp_comment_3() throws IOException { String s = "" + "<div foo=\"bar%{--_$id--}%_0\">\n" + " \n" + "</div>\n"; Node root = new GspParser().parse(s); Node div = root.children().get(0); Assert.assertEquals("div", div.name()); Assert.assertEquals(0, div.children().size()); Assert.assertEquals(1, div.attributes().size()); Assert.assertEquals("bar_0", div.attr("foo")); }
protected void readImports(Node html, Set<String> imports) { Node page = html.get("%@"); if (page != null && page.hasAttr("page")) { html.children().remove(page); if (page.hasAttr("import")) { imports.addAll(Arrays.asList(page.attr("import").split("; *"))); } } }
@Test public void gsp_1() throws IOException { String s = "<div><g:textField name=\"${it.key}\" placeholder=\"${[].collect({it})}\"/></div>"; Node root = new GspParser().parse(s); Node div = root.children().get(0); Assert.assertEquals("div", div.name()); Assert.assertEquals(1, div.children().size()); Node textField = div.children().get(0); Assert.assertEquals("g:textField", textField.name()); Assert.assertEquals(0, textField.children().size()); Assert.assertEquals(2, textField.attributes().size()); Assert.assertEquals("${it.key}", textField.attr("name")); Assert.assertEquals("${[].collect({it})}", textField.attr("placeholder")); }
@Override public void decorate(Node from, Node to) { Node title = from.get("html/head/title"); if (title != null) title.parent().children().remove(title); Node layoutTitle = to.find("g:layoutTitle"); String defaultTitle = layoutTitle.attr("default"); if (defaultTitle == null) defaultTitle = ""; replace(layoutTitle, title != null ? title : new Node("title").addText(defaultTitle)); }
@Test public void gsp_2() throws IOException { String s = "<div><g:textField name=\"${it.key}\" placeholder=\"${String.valueOf(it.getValue()).replace(\"\\\"\", \"\")}\"/></div>"; Node root = new GspParser().parse(s); Node div = root.children().get(0); Assert.assertEquals("div", div.name()); Assert.assertEquals(1, div.children().size()); Node textField = div.children().get(0); Assert.assertEquals("g:textField", textField.name()); Assert.assertEquals(0, textField.children().size()); Assert.assertEquals(2, textField.attributes().size()); Assert.assertEquals("${it.key}", textField.attr("name")); Assert.assertEquals("${String.valueOf(it.getValue()).replace(\"\\\"\", \"\")}", textField.attr("placeholder")); }
protected RenderableList prepareView(String view, String offset) { String template = resourceTools.getResourceAsString(view); if (template == null) throw new IllegalArgumentException("view '" + view + "' not found"); String dir = view.substring(0, view.lastIndexOf("/") + 1); RenderableList l = new RenderableList(); Node html = new GspParser().parse(template); Set<String> imports = new HashSet<>(); readImports(html, imports); Node layoutTag = html.get("html/head/meta[@name=layout]"); if (layoutTag != null) { layoutTag.parent().children().remove(layoutTag); String layoutContent = resourceTools.getResourceAsString("views/layouts/" + layoutTag.attr("content") + ".gsp"); if (layoutContent == null) throw new IllegalArgumentException("Layout '" + layoutTag.attr("content") + "' not found"); Node layout = new GspParser().parse(layoutContent); readImports(layout, imports); for (Decorator decorator : DependencyFactory.get(DecoratorLib.class).list()) { decorator.decorate(html, layout); } html = layout; } prepare(html.children(), l, dir, offset, imports); return l; }
@Test public void gsp_3() throws IOException { String s = "<div id=\"${id}\"><span>foo:</span>${foo}</div>"; Node root = new GspParser().parse(s); Node div = root.children().get(0); Assert.assertEquals("div", div.name()); Assert.assertEquals(2, div.children().size()); Assert.assertEquals(1, div.attributes().size()); Assert.assertEquals("${id}", div.attr("id")); Node span = div.children().get(0); Assert.assertEquals("span", span.name()); Assert.assertEquals(1, span.children().size()); Assert.assertEquals("foo:", span.text()); Node foo = div.children().get(1); Assert.assertEquals("${foo}", foo.text()); }
Assert.assertEquals("qwerty", new XmlParser().parse(s).attr("attr")); s = "<xml attr=\"qwerty\" attr2/>"; Assert.assertEquals("qwerty", new XmlParser().parse(s).attr("attr")); s = "<xml attr2 attr=\"qwerty\"/>"; Assert.assertEquals("qwerty", new XmlParser().parse(s).attr("attr")); s = "<xml><child attr=\"qwerty\"/></xml>"; Assert.assertEquals(1, new XmlParser().parse(s).size()); Assert.assertEquals("qwerty", new XmlParser().parse(s).first().attr("attr"));