@@ -62,15 +62,9 @@ public Node buildTree() {
6262 Node node = nextNode ();
6363
6464 if (node != null ) {
65- if (
66- node instanceof TextNode &&
67- getLastSibling () instanceof TextNode &&
68- !interpreter .getConfig ().getLegacyOverrides ().isAllowAdjacentTextNodes ()
69- ) {
65+ if (node instanceof TextNode && getLastSibling () instanceof TextNode ) {
7066 // merge adjacent text nodes so whitespace control properly applies
71- ((TextToken ) getLastSibling ().getMaster ()).mergeImageAndContent (
72- (TextToken ) node .getMaster ()
73- );
67+ getLastSibling ().getMaster ().mergeImageAndContent (node .getMaster ());
7468 } else {
7569 parent .getChildren ().add (node );
7670 }
@@ -102,12 +96,6 @@ public Node buildTree() {
10296
10397 private Node nextNode () {
10498 Token token = scanner .next ();
105- if (token .isLeftTrim ()) {
106- final Node lastSibling = getLastSibling ();
107- if (lastSibling instanceof TextNode ) {
108- lastSibling .getMaster ().setRightTrim (true );
109- }
110- }
11199
112100 if (token .getType () == symbols .getFixed ()) {
113101 if (token instanceof UnclosedToken ) {
@@ -182,7 +170,7 @@ private Node text(TextToken textToken) {
182170 final Node lastSibling = getLastSibling ();
183171
184172 // if last sibling was a tag and has rightTrimAfterEnd, strip whitespace
185- if (lastSibling != null && isRightTrim (lastSibling )) {
173+ if (lastSibling instanceof TagNode && isRightTrim (( TagNode ) lastSibling )) {
186174 textToken .setLeftTrim (true );
187175 }
188176
@@ -198,21 +186,18 @@ private Node text(TextToken textToken) {
198186 return n ;
199187 }
200188
201- private boolean isRightTrim (Node lastSibling ) {
202- if (lastSibling instanceof TagNode ) {
203- return (
204- ((TagNode ) lastSibling ).getEndName () == null ||
205- (
206- ((TagNode ) lastSibling ).getTag () instanceof FlexibleTag &&
207- !((FlexibleTag ) ((TagNode ) lastSibling ).getTag ()).hasEndTag (
208- (TagToken ) lastSibling .getMaster ()
209- )
210- )
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+ )
211197 )
212- ? lastSibling .getMaster ().isRightTrim ()
213- : lastSibling .getMaster ().isRightTrimAfterEnd ();
214- }
215- return lastSibling .getMaster ().isRightTrim ();
198+ )
199+ ? lastSibling .getMaster ().isRightTrim ()
200+ : lastSibling .getMaster ().isRightTrimAfterEnd ();
216201 }
217202
218203 private Node expression (ExpressionToken expressionToken ) {
@@ -257,6 +242,14 @@ private Node tag(TagToken tagToken) {
257242 if (tag instanceof EndTag ) {
258243 endTag (tag , tagToken );
259244 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+ }
260253 }
261254
262255 TagNode node = new TagNode (tag , tagToken , symbols );
@@ -275,6 +268,16 @@ private Node tag(TagToken tagToken) {
275268 }
276269
277270 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+
278281 if (parent .getMaster () != null ) { // root node
279282 parent .getMaster ().setRightTrimAfterEnd (tagToken .isRightTrim ());
280283 }
0 commit comments