Skip to content

Commit

Permalink
simplify whitespaces when stripSpacesAroundTags
Browse files Browse the repository at this point in the history
- do not strip NL before tags, only spaces
- strip spaces and only the first NL after tags
  • Loading branch information
ilg-ul committed Aug 23, 2017
1 parent 81b848d commit 98ef5b2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
21 changes: 15 additions & 6 deletions src/main/antlr3/liqp/parser/Liquid.g
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,14 @@ tokens {

int indexLA = 1;

while(Character.isSpaceChar(input.LA(indexLA)) || input.LA(indexLA) == '\r' || input.LA(indexLA) == '\n') {
indexLA++;
if (stripSpacesAroundTags) {
while(Character.isSpaceChar(input.LA(indexLA)) ) {
indexLA++;
}
} else {
while(Character.isSpaceChar(input.LA(indexLA)) || input.LA(indexLA) == '\r' || input.LA(indexLA) == '\n') {
indexLA++;
}
}

return stripSpacesAroundTags
Expand Down Expand Up @@ -413,10 +419,10 @@ other_than_tag_end
;

/* lexer rules */
OutStartDefaultStrip : {stripSpacesAroundTags}?=> WhitespaceChar* '{{' {inTag=true; $type=OutStart;};
OutEndDefaultStrip : {stripSpacesAroundTags}?=> '}}' WhitespaceChar* {inTag=false; $type=OutEnd;};
TagStartDefaultStrip : {stripSpacesAroundTags}?=> WhitespaceChar* '{%' {inTag=true; $type=TagStart;};
TagEndDefaultStrip : {stripSpacesAroundTags}?=> '%}' WhitespaceChar* {inTag=false; $type=TagEnd;};
OutStartDefaultStrip : {stripSpacesAroundTags}?=> MyWhitespaceChar* '{{' {inTag=true; $type=OutStart;};
OutEndDefaultStrip : {stripSpacesAroundTags}?=> '}}' MyWhitespaceChar* MyNewLineChar? {inTag=false; $type=OutEnd;};
TagStartDefaultStrip : {stripSpacesAroundTags}?=> MyWhitespaceChar* '{%' {inTag=true; $type=TagStart;};
TagEndDefaultStrip : {stripSpacesAroundTags}?=> '%}' MyWhitespaceChar* MyNewLineChar? {inTag=false; $type=TagEnd;};

OutStartStrip : WhitespaceChar* '{{-' {inTag=true; $type=OutStart;};
OutEndStrip : '-}}' WhitespaceChar* {inTag=false; $type=OutEnd;};
Expand Down Expand Up @@ -508,6 +514,9 @@ NoSpace
/* fragment rules */
fragment WhitespaceChar : ' ' | '\t' | '\r' | '\n';

fragment MyWhitespaceChar : ' ' | '\t';
fragment MyNewLineChar : '\r' | '\n';

fragment Letter : 'a'..'z' | 'A'..'Z';
fragment Digit : '0'..'9';
fragment SStr : '\'' ~'\''* '\'' {setText(strip($text, true));};
Expand Down
19 changes: 15 additions & 4 deletions src/test/java/liqp/tags/WhitespaceControlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ public void twoRhsStrip() throws RecognitionException {
@Test
public void allStrip() throws RecognitionException {

String source = "a \n {%- assign letter = 'b' -%} \n{{- letter -}}\n c";
String source = "a \n {%- assign letter = 'b' -%} \nx\n {{- letter -}}\n c";
Template template = Template.parse(source);
String rendered = String.valueOf(template.render().replace(' ', '.'));

assertThat(rendered, is("abc"));
assertThat(rendered, is("axbc"));
}

@Test
Expand All @@ -88,7 +88,18 @@ public void defaultStrip() throws RecognitionException {
ParseSettings settings = new ParseSettings.Builder().withStripSpaceAroundTags(true).build();
Template template = Template.parse(source, settings);
String rendered = String.valueOf(template.render().replace(' ', '.'));

assertThat(rendered, is("a..\nb..c"));
}

@Test
public void defaultMultiNlStrip() throws RecognitionException {

String source = "a \n\n {% assign letter = 'b' %} \n{{ letter }}\n\n c";
ParseSettings settings = new ParseSettings.Builder().withStripSpaceAroundTags(true).build();
Template template = Template.parse(source, settings);
String rendered = String.valueOf(template.render().replace(' ', '.'));

assertThat(rendered, is("abc"));
assertThat(rendered, is("a..\n\nb\n..c"));
}
}

0 comments on commit 98ef5b2

Please sign in to comment.