@@ -62,12 +62,7 @@ public Node buildTree() {
6262 Node node = nextNode ();
6363
6464 if (node != null ) {
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- }
65+ parent .getChildren ().add (node );
7166 }
7267 }
7368
@@ -96,6 +91,12 @@ public Node buildTree() {
9691
9792 private Node nextNode () {
9893 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+ }
99100
100101 if (token .getType () == symbols .getFixed ()) {
101102 if (token instanceof UnclosedToken ) {
@@ -170,7 +171,7 @@ private Node text(TextToken textToken) {
170171 final Node lastSibling = getLastSibling ();
171172
172173 // if last sibling was a tag and has rightTrimAfterEnd, strip whitespace
173- if (lastSibling instanceof TagNode && isRightTrim (( TagNode ) lastSibling )) {
174+ if (lastSibling != null && isRightTrim (lastSibling )) {
174175 textToken .setLeftTrim (true );
175176 }
176177
@@ -186,18 +187,21 @@ private Node text(TextToken textToken) {
186187 return n ;
187188 }
188189
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- )
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+ )
197200 )
198- )
199- ? lastSibling .getMaster ().isRightTrim ()
200- : lastSibling .getMaster ().isRightTrimAfterEnd ();
201+ ? lastSibling .getMaster ().isRightTrim ()
202+ : lastSibling .getMaster ().isRightTrimAfterEnd ();
203+ }
204+ return lastSibling .getMaster ().isRightTrim ();
201205 }
202206
203207 private Node expression (ExpressionToken expressionToken ) {
@@ -242,14 +246,6 @@ private Node tag(TagToken tagToken) {
242246 if (tag instanceof EndTag ) {
243247 endTag (tag , tagToken );
244248 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- }
253249 }
254250
255251 TagNode node = new TagNode (tag , tagToken , symbols );
@@ -268,16 +264,6 @@ private Node tag(TagToken tagToken) {
268264 }
269265
270266 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-
281267 if (parent .getMaster () != null ) { // root node
282268 parent .getMaster ().setRightTrimAfterEnd (tagToken .isRightTrim ());
283269 }
0 commit comments