1717
1818import com .google .common .collect .Iterators ;
1919import com .google .common .collect .PeekingIterator ;
20+ import com .hubspot .jinjava .JinjavaConfig ;
2021import com .hubspot .jinjava .interpret .DisabledException ;
2122import com .hubspot .jinjava .interpret .JinjavaInterpreter ;
2223import com .hubspot .jinjava .interpret .MissingEndTagException ;
@@ -62,9 +63,15 @@ public Node buildTree() {
6263 Node node = nextNode ();
6364
6465 if (node != null ) {
65- if (node instanceof TextNode && getLastSibling () instanceof TextNode ) {
66+ if (
67+ node instanceof TextNode &&
68+ getLastSibling () instanceof TextNode &&
69+ !interpreter .getConfig ().getLegacyOverrides ().isAllowAdjacentTextNodes ()
70+ ) {
6671 // merge adjacent text nodes so whitespace control properly applies
67- getLastSibling ().getMaster ().mergeImageAndContent (node .getMaster ());
72+ ((TextToken ) getLastSibling ().getMaster ()).mergeImageAndContent (
73+ (TextToken ) node .getMaster ()
74+ );
6875 } else {
6976 parent .getChildren ().add (node );
7077 }
@@ -96,6 +103,12 @@ public Node buildTree() {
96103
97104 private Node nextNode () {
98105 Token token = scanner .next ();
106+ if (token .isLeftTrim () && isTrimmingEnabledForToken (token , interpreter .getConfig ())) {
107+ final Node lastSibling = getLastSibling ();
108+ if (lastSibling instanceof TextNode ) {
109+ lastSibling .getMaster ().setRightTrim (true );
110+ }
111+ }
99112
100113 if (token .getType () == symbols .getFixed ()) {
101114 if (token instanceof UnclosedToken ) {
@@ -170,7 +183,11 @@ private Node text(TextToken textToken) {
170183 final Node lastSibling = getLastSibling ();
171184
172185 // if last sibling was a tag and has rightTrimAfterEnd, strip whitespace
173- if (lastSibling instanceof TagNode && isRightTrim ((TagNode ) lastSibling )) {
186+ if (
187+ lastSibling != null &&
188+ isRightTrim (lastSibling ) &&
189+ isTrimmingEnabledForToken (lastSibling .getMaster (), interpreter .getConfig ())
190+ ) {
174191 textToken .setLeftTrim (true );
175192 }
176193
@@ -186,18 +203,21 @@ private Node text(TextToken textToken) {
186203 return n ;
187204 }
188205
189- private boolean isRightTrim (TagNode lastSibling ) {
190- return (
191- lastSibling .getEndName () == null ||
192- (
193- lastSibling .getTag () instanceof FlexibleTag &&
194- !((FlexibleTag ) lastSibling .getTag ()).hasEndTag (
195- (TagToken ) lastSibling .getMaster ()
196- )
206+ private boolean isRightTrim (Node lastSibling ) {
207+ if (lastSibling instanceof TagNode ) {
208+ return (
209+ ((TagNode ) lastSibling ).getEndName () == null ||
210+ (
211+ ((TagNode ) lastSibling ).getTag () instanceof FlexibleTag &&
212+ !((FlexibleTag ) ((TagNode ) lastSibling ).getTag ()).hasEndTag (
213+ (TagToken ) lastSibling .getMaster ()
214+ )
215+ )
197216 )
198- )
199- ? lastSibling .getMaster ().isRightTrim ()
200- : lastSibling .getMaster ().isRightTrimAfterEnd ();
217+ ? lastSibling .getMaster ().isRightTrim ()
218+ : lastSibling .getMaster ().isRightTrimAfterEnd ();
219+ }
220+ return lastSibling .getMaster ().isRightTrim ();
201221 }
202222
203223 private Node expression (ExpressionToken expressionToken ) {
@@ -242,14 +262,6 @@ private Node tag(TagToken tagToken) {
242262 if (tag instanceof EndTag ) {
243263 endTag (tag , tagToken );
244264 return null ;
245- } else {
246- // if a tag has left trim, mark the last sibling to trim right whitespace
247- if (tagToken .isLeftTrim ()) {
248- final Node lastSibling = getLastSibling ();
249- if (lastSibling instanceof TextNode ) {
250- lastSibling .getMaster ().setRightTrim (true );
251- }
252- }
253265 }
254266
255267 TagNode node = new TagNode (tag , tagToken , symbols );
@@ -268,16 +280,6 @@ private Node tag(TagToken tagToken) {
268280 }
269281
270282 private void endTag (Tag tag , TagToken tagToken ) {
271- final Node lastSibling = getLastSibling ();
272-
273- if (
274- parent instanceof TagNode &&
275- tagToken .isLeftTrim () &&
276- lastSibling instanceof TextNode
277- ) {
278- lastSibling .getMaster ().setRightTrim (true );
279- }
280-
281283 if (parent .getMaster () != null ) { // root node
282284 parent .getMaster ().setRightTrimAfterEnd (tagToken .isRightTrim ());
283285 }
@@ -318,4 +320,11 @@ private void endTag(Tag tag, TagToken tagToken) {
318320 );
319321 }
320322 }
323+
324+ private boolean isTrimmingEnabledForToken (Token token , JinjavaConfig jinjavaConfig ) {
325+ if (token instanceof TagToken || token instanceof TextToken ) {
326+ return true ;
327+ }
328+ return jinjavaConfig .getLegacyOverrides ().isUseTrimmingForNotesAndExpressions ();
329+ }
321330}
0 commit comments