File tree Expand file tree Collapse file tree 5 files changed +52
-14
lines changed
main/java/com/hubspot/jinjava/tree
java/com/hubspot/jinjava/tree
resources/parse/tokenizer Expand file tree Collapse file tree 5 files changed +52
-14
lines changed Original file line number Diff line number Diff line change @@ -170,14 +170,17 @@ private Node getLastSibling() {
170170
171171 private Node text (TextToken textToken ) {
172172 if (interpreter .getConfig ().isLstripBlocks ()) {
173- if (scanner .hasNext () && scanner .peek ().getType () == symbols .getTag ()) {
174- textToken =
175- new TextToken (
176- StringUtils .stripEnd (textToken .getImage (), "\t " ),
177- textToken .getLineNumber (),
178- textToken .getStartPosition (),
179- symbols
180- );
173+ if (scanner .hasNext ()) {
174+ final int nextTokenType = scanner .peek ().getType ();
175+ if (nextTokenType == symbols .getTag () || nextTokenType == symbols .getNote ()) {
176+ textToken =
177+ new TextToken (
178+ StringUtils .stripEnd (textToken .getImage (), "\t " ),
179+ textToken .getLineNumber (),
180+ textToken .getStartPosition (),
181+ symbols
182+ );
183+ }
181184 }
182185 }
183186
Original file line number Diff line number Diff line change @@ -253,13 +253,18 @@ private Token newToken(int kind) {
253253 lastStart - lastNewlinePos + 1
254254 );
255255
256- if (t instanceof TagToken ) {
257- if (config .isTrimBlocks () && currPost < length && is [currPost ] == '\n' ) {
258- lastNewlinePos = currPost ;
259- ++currPost ;
260- ++tokenStart ;
261- }
256+ if (
257+ (t instanceof TagToken || t instanceof NoteToken ) &&
258+ config .isTrimBlocks () &&
259+ currPost < length &&
260+ is [currPost ] == '\n'
261+ ) {
262+ lastNewlinePos = currPost ;
263+ ++currPost ;
264+ ++tokenStart ;
265+ }
262266
267+ if (t instanceof TagToken ) {
263268 TagToken tt = (TagToken ) t ;
264269 if ("raw" .equals (tt .getTagName ())) {
265270 inRaw = 1 ;
Original file line number Diff line number Diff line change @@ -147,6 +147,18 @@ public void trimAndLstripBlocks() {
147147 .isEqualTo ("<div>\n " + " yay\n " + "</div>\n " );
148148 }
149149
150+ @ Test
151+ public void trimAndLstripCommentBlocks () {
152+ interpreter =
153+ new Jinjava (
154+ JinjavaConfig .newBuilder ().withLstripBlocks (true ).withTrimBlocks (true ).build ()
155+ )
156+ .newInterpreter ();
157+
158+ assertThat (interpreter .render (parse ("parse/tokenizer/whitespace-comment-tags.jinja" )))
159+ .isEqualTo ("<div>\n " + " yay\n " + " whoop\n " + "</div>\n " );
160+ }
161+
150162 @ Test
151163 public void itWarnsAgainstMissingStartTags () {
152164 String expression = "{% if true %} foo {% endif %} {% endif %}" ;
Original file line number Diff line number Diff line change @@ -21,6 +21,16 @@ public void trimBlocksTrimsAfterTag() {
2121 assertThat (tokens .get (2 ).getImage ()).isEqualTo (" yay\n " );
2222 }
2323
24+ @ Test
25+ public void trimBlocksTrimsAfterCommentTag () {
26+ List <Token > tokens = scanTokens (
27+ "parse/tokenizer/whitespace-comment-tags.jinja" ,
28+ trimBlocksConfig ()
29+ );
30+ assertThat (tokens .get (2 ).getImage ()).isEqualTo (" yay\n " );
31+ assertThat (tokens .get (4 ).getImage ()).isEqualTo (" whoop\n </div>\n " );
32+ }
33+
2434 private List <Token > scanTokens (String srcPath , JinjavaConfig config ) {
2535 try {
2636 return Lists .newArrayList (
Original file line number Diff line number Diff line change 1+ <div >
2+ {# a comment #}
3+ yay
4+ {# another comment
5+ This time, over multiple lines
6+ #}
7+ whoop
8+ </div >
You can’t perform that action at this time.
0 commit comments