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_comment_2() throws IOException { String s = "" + "<div %{--foo=\"${bar}\"--}%>\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(0, div.attributes().size()); }
@Test public void gsp_4() throws IOException { String s = "<div><script>\n" + " var a\n" + " for(var i=0; i<10;i++) {\n" + " a+=i;" + " }\n" + "</script></div>"; Node root = new GspParser().parse(s); Node div = root.children().get(0); Assert.assertEquals(1, div.children().size()); Node script = div.children.get(0); Assert.assertEquals("var a\n" + " for(var i=0; i<10;i++) {\n" + " a+=i;" + " }\n", script.text()); }
@Test public void test_exceptions() { Node n = new GspParser().parse("<div><g:if test=\"${flag}\">text</g:if>error<g:else>foo</g:else></div>"); try { new ViewRenderingService().prepare(n.children(), new RenderableList(), "", ""); assert false; } catch (IllegalStateException e) { Assert.assertEquals("If tag must be before Else tag", e.getMessage()); } n = new GspParser().parse("<div><g:if test=\"${flag}\">text</g:if>error<g:elseif test=\"${flag}\">foo</g:elseif></div>"); try { new ViewRenderingService().prepare(n.children(), new RenderableList(), "", ""); assert false; } catch (IllegalStateException e) { Assert.assertEquals("If tag must be before Else tag", e.getMessage()); } } }
@Test public void gsp_comment_1() throws IOException { String s = "" + "<div>\n" + " %{--<p>$test</p>--}%\n" + "</div>\n"; Node root = new GspParser().parse(s); Node div = root.children().get(0); Assert.assertEquals("div", div.name()); Assert.assertEquals(1, div.children().size()); Assert.assertEquals(0, div.attributes().size()); Assert.assertEquals("%{--<p>$test</p>--}%", div.children().get(0).textOwn()); }
@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")); }
@Test() public void test_ignore_comments() { String gsp = "<div>\n" + " before\n" + " %{--<p>text</p>--}%\n" + " after\n" + "</div>"; RenderableList renderable = new RenderableList(); new ViewRenderingService().prepare(new GspParser().parse(gsp).children(), renderable, "", ""); Assert.assertEquals("<div>\n" + " before\n" + " after\n" + "</div>\n", renderable.get(null).toString()); } }
@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")); }
@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")); }
@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()); }
@Test public void gsp_page() throws IOException { String s = "<%@ page contentType=\"text/html;charset=UTF-8\" %>\n" + "<!DOCTYPE html>\n" + "<html>\n" + "<head>\n" + " <title>title</title>\n" + " %{-- comment! --}%\n" + "</head>" + "</html>"; Node doc = new GspParser().parse(s); Assert.assertEquals(3, doc.children().size()); Assert.assertEquals("%@", doc.get(0).name()); Assert.assertEquals("!DOCTYPE", doc.get(1).name()); Assert.assertEquals("html", doc.get(2).name()); }
@Test public void gsp_page2() throws IOException { String s = "<%@ page contentType=\"text/html;charset=UTF-8\" %>\n" + "<!DOCTYPE html>\n" + "<html>\n" + "<head>\n" + " <title>title</title>\n" + "</head>" + "</html>"; Node doc = new GspParser().parse(s); Assert.assertEquals(3, doc.children().size()); Assert.assertEquals("%@", doc.get(0).name()); Assert.assertEquals("!DOCTYPE", doc.get(1).name()); Assert.assertEquals("html", doc.get(2).name()); } }
@Test public void gsp_comment_4() throws IOException { String s = "<div>\n" + " before\n" + " %{--<p>text</p>--}%\n" + " after\n" + "</div>"; Node root = new GspParser().parse(s); Node div = root.children().get(0); Assert.assertEquals("div", div.name()); Assert.assertEquals(3, div.children().size()); Assert.assertEquals("before", div.get(0).text()); Assert.assertEquals("%{--<p>text</p>--}%", div.get(1).text()); Assert.assertEquals("after", div.get(2).text()); }
@Test public void gsp_html_comment() throws IOException { String s = "<div>\n" + " before\n" + " <!--<p>text</p>-->\n" + " after\n" + "</div>"; Node root = new GspParser().parse(s); Node div = root.children().get(0); Assert.assertEquals("div", div.name()); Assert.assertEquals(3, div.children().size()); Assert.assertEquals("before", div.get(0).text()); Assert.assertEquals("<!--<p>text</p>-->", div.get(1).text()); Assert.assertEquals("after", div.get(2).text()); }
@Test public void render() { Node n = new GspParser().parse("<div style=\"width: 100px\"><a href=\"http://${host.toLowerCase()}\">yandex</a><br></div>"); RenderableList l = new RenderableList(); new ViewRenderingService().prepare(n.children(), l, "", ""); Model model = new Model(); model.put("host", "YA.ru"); RenderResult result = l.get(model); // System.out.println(result.toString()); Assert.assertEquals(3, l.size()); Assert.assertEquals("<div style=\"width: 100px\">\n" + " <a href=\"http://ya.ru\">\n" + " yandex\n" + " </a>\n" + " <br/>\n" + "</div>\n", result.toString()); }