Skip to content

Commit

Permalink
Escape popped state content
Browse files Browse the repository at this point in the history
The contents of captured named states needs to be escaped when writing
it to ensure that any "$" signs that may have been escaped or expanded
in the original content are passed through as-is.
  • Loading branch information
mtdowling committed Oct 14, 2019
1 parent 2b47ab6 commit 2150120
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,8 @@ public CodeWriter popState() {
} else {
// Sections can be added that are just placeholders. In those cases,
// do not write anything unless the section emitted a non-empty string.
writeOptional(result);
// Note that this has already been formatted, so escape "$".
writeOptional(result.replace("$", "$$"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,4 +497,12 @@ public void hasOpenBlockRunnable5() {

assertThat(result, equalTo("public 1 2 3 4 5 {\n hi();\n}\n"));
}

@Test
public void poppedSectionsEscapeDollars() {
CodeWriter writer = CodeWriter.createDefault();
String result = writer.pushState("foo").write("$$Hello").popState().toString();

assertThat(result, equalTo("$Hello\n"));
}
}

0 comments on commit 2150120

Please sign in to comment.