diff --git a/src/main/java/htmlflow/visitor/HtmlVisitor.java b/src/main/java/htmlflow/visitor/HtmlVisitor.java index 54ec03da..79b3a1f7 100644 --- a/src/main/java/htmlflow/visitor/HtmlVisitor.java +++ b/src/main/java/htmlflow/visitor/HtmlVisitor.java @@ -134,6 +134,23 @@ public final void newlineAndIndent(){ isClosed = true; } } + /** + * Adds a new line. + * Checks whether the parent element is still opened or not (!isClosed). + * If it is open then it closes the parent begin tag with ">" (!isClosed). + */ + public final void newline(){ + if (isClosed){ + write("\n"); + } else { + depth++; + write(FINISH_TAG); + if(isIndented) + write("\n"); + + isClosed = true; + } + } /** * This method appends the String {@code " } /** @@ -195,7 +214,7 @@ public final void visitAttributeBoolean(String name, String value) { @Override public final void visitText(Text text) { - newlineAndIndent(); + newline(); write(HtmlEscapers.htmlEscaper().escape(text.getValue())); } /** diff --git a/src/test/java/htmlflow/test/TestIndentation.java b/src/test/java/htmlflow/test/TestIndentation.java index 8de96c9d..0ffef9b0 100644 --- a/src/test/java/htmlflow/test/TestIndentation.java +++ b/src/test/java/htmlflow/test/TestIndentation.java @@ -77,6 +77,36 @@ public void viewWithNoIndent() { String actual = view.setIndented(false).render(); assertEquals(expected, actual); } + + @Test + public void viewWithTextAreaAndIndent() { + HtmlView view = HtmlFlow.view(page -> page + .div() + .textarea() + .text("Sample text\nfoo\nbar") + .__() + .script() + .raw("// some comment" + lineSeparator() + + "console.log('Hello world');") + .__() // script + .__()); // div + + + String expectedResult = + "
" + lineSeparator() + + "\t" + lineSeparator() + + "\t" + lineSeparator() + + "
"; + + String actual = view.setIndented(true).render(); + assertEquals(expectedResult, actual); + } @Test public void viewAsyncWithNoIndent() { HtmlViewAsync view = HtmlFlow.viewAsync(page -> page