@@ -62,9 +62,15 @@ public Node buildTree() {
6262 Node node = nextNode ();
6363
6464 if (node != null ) {
65- if (node instanceof TextNode && getLastSibling () instanceof TextNode ) {
65+ if (
66+ node instanceof TextNode &&
67+ getLastSibling () instanceof TextNode &&
68+ !interpreter .getConfig ().getLegacyOverrides ().isAllowAdjacentTextNodes ()
69+ ) {
6670 // merge adjacent text nodes so whitespace control properly applies
67- getLastSibling ().getMaster ().mergeImageAndContent (node .getMaster ());
71+ ((TextToken ) getLastSibling ().getMaster ()).mergeImageAndContent (
72+ (TextToken ) node .getMaster ()
73+ );
6874 } else {
6975 parent .getChildren ().add (node );
7076 }
@@ -96,6 +102,12 @@ public Node buildTree() {
96102
97103 private Node nextNode () {
98104 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+ }
99111
100112 if (token .getType () == symbols .getFixed ()) {
101113 if (token instanceof UnclosedToken ) {
@@ -170,7 +182,7 @@ private Node text(TextToken textToken) {
170182 final Node lastSibling = getLastSibling ();
171183
172184 // if last sibling was a tag and has rightTrimAfterEnd, strip whitespace
173- if (lastSibling instanceof TagNode && isRightTrim (( TagNode ) lastSibling )) {
185+ if (lastSibling != null && isRightTrim (lastSibling )) {
174186 textToken .setLeftTrim (true );
175187 }
176188
@@ -186,18 +198,21 @@ private Node text(TextToken textToken) {
186198 return n ;
187199 }
188200
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- )
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+ )
197211 )
198- )
199- ? lastSibling .getMaster ().isRightTrim ()
200- : lastSibling .getMaster ().isRightTrimAfterEnd ();
212+ ? lastSibling .getMaster ().isRightTrim ()
213+ : lastSibling .getMaster ().isRightTrimAfterEnd ();
214+ }
215+ return lastSibling .getMaster ().isRightTrim ();
201216 }
202217
203218 private Node expression (ExpressionToken expressionToken ) {
@@ -242,14 +257,6 @@ private Node tag(TagToken tagToken) {
242257 if (tag instanceof EndTag ) {
243258 endTag (tag , tagToken );
244259 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- }
253260 }
254261
255262 TagNode node = new TagNode (tag , tagToken , symbols );
@@ -268,16 +275,6 @@ private Node tag(TagToken tagToken) {
268275 }
269276
270277 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-
281278 if (parent .getMaster () != null ) { // root node
282279 parent .getMaster ().setRightTrimAfterEnd (tagToken .isRightTrim ());
283280 }
0 commit comments