Skip to content

Commit 3f4ba3e

Browse files
Move the whole "combine adjacent strings" hack into the parser for now
1 parent e15b7e4 commit 3f4ba3e

File tree

4 files changed

+55
-47
lines changed

4 files changed

+55
-47
lines changed

ng-appserver/src/main/java/ng/appserver/templating/NGDynamicHTMLTag.java

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,17 @@ public NGDynamicHTMLTag parent() {
4545
return _parent;
4646
}
4747

48-
public List<Object> children() {
49-
return _children;
48+
/**
49+
* FIXME: Yeah, we need to fix this in the parser itself // Hugi 2024-11-17
50+
*/
51+
@Deprecated
52+
public List<PNode> childrenWithStringsProcessedAndCombined() {
53+
54+
if( _children == null ) {
55+
return null;
56+
}
57+
58+
return combineAndWrapBareStrings( _children );
5059
}
5160

5261
public void addChild( final Object stringOrElement ) {
@@ -71,6 +80,45 @@ public boolean isRoot() {
7180
return _declaration == null;
7281
}
7382

83+
/**
84+
* Iterates through the list, combining adjacent strings before wrapping them in a PHTMLNode
85+
* Other nodes just get added to the list.
86+
*/
87+
private static List<PNode> combineAndWrapBareStrings( final List<Object> children ) {
88+
final List<PNode> childElements = new ArrayList<>( children.size() );
89+
90+
final StringBuilder sb = new StringBuilder( 128 );
91+
92+
for( final Object currentChild : children ) {
93+
94+
if( currentChild instanceof String ) {
95+
// If we encounter a string, we append it to the StringBuilder
96+
sb.append( (String)currentChild );
97+
}
98+
else {
99+
// If we encounter any other element and we still have unwrapped strings in our builder,
100+
// we take the string data we've collected, wrap it up and add it to the element list.
101+
if( sb.length() > 0 ) {
102+
final PHTMLNode bareString = new PHTMLNode( sb.toString() );
103+
childElements.add( bareString );
104+
sb.setLength( 0 );
105+
}
106+
107+
// ... and then add the element itself
108+
childElements.add( (PNode)currentChild );
109+
}
110+
}
111+
112+
// If the last element happened to be a string, the StringBuilder will still have data so we wrap it here
113+
if( sb.length() > 0 ) {
114+
final PHTMLNode bareString = new PHTMLNode( sb.toString() );
115+
childElements.add( bareString );
116+
sb.setLength( 0 );
117+
}
118+
119+
return childElements;
120+
}
121+
74122
@Override
75123
public String toString() {
76124
return "NGDynamicHTMLTag [_declaration=" + _declaration + ", _children=" + _children + "]";

ng-appserver/src/main/java/ng/appserver/templating/NGTemplateParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private PNode parseHTML() throws NGHTMLFormatException, NGDeclarationFormatExcep
6464
throw new NGHTMLFormatException( "There is an unbalanced dynamic tag named '%s'.".formatted( _currentDynamicTag.declaration().name() ) );
6565
}
6666

67-
return new PGroupNode( _currentDynamicTag.children() );
67+
return new PGroupNode( _currentDynamicTag.childrenWithStringsProcessedAndCombined() );
6868
}
6969

7070
public void didParseOpeningWebObjectTag( String parsedString ) throws NGHTMLFormatException, NGDeclarationFormatException {

ng-appserver/src/main/java/ng/appserver/templating/NGTemplateParserProxy.java

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private static NGElement toDynamicElement( final PNode node ) {
5252

5353
private static NGElement toDynamicElement( final NGDynamicHTMLTag tag ) {
5454
try {
55-
return NGApplication.dynamicElementWithName( tag.declaration().type(), toAssociations( tag.declaration() ), template( tag.children() ), Collections.emptyList() );
55+
return NGApplication.dynamicElementWithName( tag.declaration().type(), toAssociations( tag.declaration() ), template( tag.childrenWithStringsProcessedAndCombined() ), Collections.emptyList() );
5656
}
5757
catch( NGElementNotFoundException e ) {
5858
// FIXME:
@@ -120,17 +120,16 @@ private static NGAssociation associationForInlineBindingValue( String value ) {
120120
/**
121121
* @return The tag's template
122122
*/
123-
private static NGElement template( final List<Object> children ) {
123+
private static NGElement template( final List<PNode> children ) {
124124

125125
// FIXME: Children should never really be null. I'm still hesitant to replace it with an empty list though, since in my mind that represents an empty container tag. Food for thought... // Hugi 2024-11-15
126126
if( children == null ) {
127127
return null;
128128
}
129129

130-
final List<PNode> childNodes = combineAndWrapBareStrings( children );
131130
final List<NGElement> childElements = new ArrayList<>();
132131

133-
for( final PNode pNode : childNodes ) {
132+
for( final PNode pNode : children ) {
134133
childElements.add( toDynamicElement( pNode ) );
135134
}
136135

@@ -149,43 +148,4 @@ private static NGElement template( final List<Object> children ) {
149148

150149
return NGDynamicGroup.of( childElements );
151150
}
152-
153-
/**
154-
* Iterates through the list, combining adjacent strings before wrapping them in a PHTMLNode
155-
* Other nodes just get added to the list.
156-
*/
157-
private static List<PNode> combineAndWrapBareStrings( List<Object> children ) {
158-
final List<PNode> childElements = new ArrayList<>( children.size() );
159-
160-
final StringBuilder sb = new StringBuilder( 128 );
161-
162-
for( final Object currentChild : children ) {
163-
164-
if( currentChild instanceof String ) {
165-
// If we encounter a string, we append it to the StringBuilder
166-
sb.append( (String)currentChild );
167-
}
168-
else {
169-
// If we encounter any other element and we still have unwrapped strings in our builder,
170-
// we take the string data we've collected, wrap it up and add it to the element list.
171-
if( sb.length() > 0 ) {
172-
final PHTMLNode bareString = new PHTMLNode( sb.toString() );
173-
childElements.add( bareString );
174-
sb.setLength( 0 );
175-
}
176-
177-
// ... and then add the element itself
178-
childElements.add( (PNode)currentChild );
179-
}
180-
}
181-
182-
// If the last element happened to be a string, the StringBuilder will still have data so we wrap it here
183-
if( sb.length() > 0 ) {
184-
final PHTMLNode bareString = new PHTMLNode( sb.toString() );
185-
childElements.add( bareString );
186-
sb.setLength( 0 );
187-
}
188-
189-
return childElements;
190-
}
191151
}

ng-appserver/src/main/java/ng/appserver/templating/PGroupNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.List;
44
import java.util.Objects;
55

6-
public record PGroupNode( List<Object> children ) implements PNode {
6+
public record PGroupNode( List<PNode> children ) implements PNode {
77

88
public PGroupNode {
99
Objects.requireNonNull( children );

0 commit comments

Comments
 (0)