@@ -62,7 +62,12 @@ public Node buildTree() {
6262 Node node = nextNode ();
6363
6464 if (node != null ) {
65- parent .getChildren ().add (node );
65+ if (node instanceof TextNode && getLastSibling () instanceof TextNode ) {
66+ // merge adjacent text nodes so whitespace control properly applies
67+ getLastSibling ().getMaster ().mergeImageAndContent (node .getMaster ());
68+ } else {
69+ parent .getChildren ().add (node );
70+ }
6671 }
6772 }
6873
@@ -91,12 +96,6 @@ public Node buildTree() {
9196
9297 private Node nextNode () {
9398 Token token = scanner .next ();
94- if (token .isLeftTrim ()) {
95- final Node lastSibling = getLastSibling ();
96- if (lastSibling instanceof TextNode ) {
97- lastSibling .getMaster ().setRightTrim (true );
98- }
99- }
10099
101100 if (token .getType () == symbols .getFixed ()) {
102101 if (token instanceof UnclosedToken ) {
@@ -171,7 +170,7 @@ private Node text(TextToken textToken) {
171170 final Node lastSibling = getLastSibling ();
172171
173172 // if last sibling was a tag and has rightTrimAfterEnd, strip whitespace
174- if (lastSibling != null && isRightTrim (lastSibling )) {
173+ if (lastSibling instanceof TagNode && isRightTrim (( TagNode ) lastSibling )) {
175174 textToken .setLeftTrim (true );
176175 }
177176
@@ -187,21 +186,18 @@ private Node text(TextToken textToken) {
187186 return n ;
188187 }
189188
190- private boolean isRightTrim (Node lastSibling ) {
191- if (lastSibling instanceof TagNode ) {
192- return (
193- ((TagNode ) lastSibling ).getEndName () == null ||
194- (
195- ((TagNode ) lastSibling ).getTag () instanceof FlexibleTag &&
196- !((FlexibleTag ) ((TagNode ) lastSibling ).getTag ()).hasEndTag (
197- (TagToken ) lastSibling .getMaster ()
198- )
199- )
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+ )
200197 )
201- ? lastSibling .getMaster ().isRightTrim ()
202- : lastSibling .getMaster ().isRightTrimAfterEnd ();
203- }
204- return lastSibling .getMaster ().isRightTrim ();
198+ )
199+ ? lastSibling .getMaster ().isRightTrim ()
200+ : lastSibling .getMaster ().isRightTrimAfterEnd ();
205201 }
206202
207203 private Node expression (ExpressionToken expressionToken ) {
@@ -246,6 +242,14 @@ private Node tag(TagToken tagToken) {
246242 if (tag instanceof EndTag ) {
247243 endTag (tag , tagToken );
248244 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+ }
249253 }
250254
251255 TagNode node = new TagNode (tag , tagToken , symbols );
@@ -264,6 +268,16 @@ private Node tag(TagToken tagToken) {
264268 }
265269
266270 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+
267281 if (parent .getMaster () != null ) { // root node
268282 parent .getMaster ().setRightTrimAfterEnd (tagToken .isRightTrim ());
269283 }
0 commit comments