Skip to content

Fix identation textarea (related to issue #117) #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/main/java/htmlflow/visitor/HtmlVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<elementName"} and it leaves the element
Expand All @@ -159,7 +176,9 @@ public final void visitElement(Element element) {
@Override
public final void visitParent(Element element) {
depth--;
newlineAndIndent();
if(!element.getName().equals("textarea")){
newlineAndIndent();
}
endTag(out, element.getName()); // </elementName>
}
/**
Expand Down Expand Up @@ -195,7 +214,7 @@ public final void visitAttributeBoolean(String name, String value) {

@Override
public final <R> void visitText(Text<? extends Element, R> text) {
newlineAndIndent();
newline();
write(HtmlEscapers.htmlEscaper().escape(text.getValue()));
}
/**
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/htmlflow/test/TestIndentation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
"<div>" + lineSeparator() +
"\t<textarea>"+lineSeparator() +
"Sample text" + lineSeparator() +
"foo" + lineSeparator() +
"bar</textarea>" + lineSeparator() +
"\t<script>" + lineSeparator() +
"\t\t// some comment" + lineSeparator() +
"console.log('Hello world');" + lineSeparator() +
"\t</script>" + lineSeparator() +
"</div>";

String actual = view.setIndented(true).render();
assertEquals(expectedResult, actual);
}
@Test
public void viewAsyncWithNoIndent() {
HtmlViewAsync<?> view = HtmlFlow.viewAsync(page -> page
Expand Down