Skip to content

Commit

Permalink
Fixed handling of literal text blocks enclosed in {literal} command.
Browse files Browse the repository at this point in the history
  • Loading branch information
oujesky committed Nov 3, 2015
1 parent e253853 commit da91775
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ List<PhpExpr> execOnChildren(ParentSoyNode<?> node) {
*/
@Override protected void visitRawTextNode(RawTextNode node) {
// Escape special characters in the text before writing as a string.
String exprText = BaseUtils.escapeToSoyString(node.getRawText(), false);
String exprText = PhpExprUtils.escapeLiteralString(node.getRawText());
phpExprs.add(new PhpStringExpr(exprText));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,41 @@ public static String escapePhpMethodName(String phpMethodName) {

return phpMethodName;
}

/**
* Escapes raw literal string to PHP string
*
* Based on BaseUtil.escapeSoyString(), but adjusted to work with PHP apostrophe enclosed strings
*
* Line breaks are transformed as PHP_EOL constants, backslashes and apostrohpes are escaped,
* other characters are left in their literal form
*
* @param value
* @return Prepared PHP string
*/
public static String escapeLiteralString(String value) {

int len = value.length();
StringBuilder out = new StringBuilder(len * 9 / 8);
out.append('\'');

int codePoint;
for (int i = 0; i < len; i += Character.charCount(codePoint)) {
codePoint = value.codePointAt(i);

switch (codePoint) {
case '\n': out.append("'.PHP_EOL.'"); break;
// remove \r completely, line breaks are handled on \n
case '\r': out.append(""); break;
case '\\': out.append("\\\\"); break;
case '\'': out.append("\\'"); break;
default:
out.appendCodePoint(codePoint);
break;
}
}

out.append('\'');
return out.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,18 @@ public void testCallReturnsString() {

assertThatSoyCode(soyCode).compilesTo(expectedPhpCode);
}

public void testLiteral() {
String soyCode = "{literal}\n" +
" /* some\n" +
" multiline\n" +
" string */\n" +
" {$a}\n" +
"\t'\"test&\n" +
"{/literal}";

String expectedPhpCode = "$output .= ''.PHP_EOL.' /* some'.PHP_EOL.' multiline'.PHP_EOL.' string */'.PHP_EOL.' {$a}'.PHP_EOL.'\t\\'\"test&'.PHP_EOL.'';\n";

assertThatSoyCode(soyCode).compilesTo(expectedPhpCode);
}
}

0 comments on commit da91775

Please sign in to comment.