Skip to content

Commit

Permalink
Move the whole "combine adjacent strings" hack into the parser for now
Browse files Browse the repository at this point in the history
  • Loading branch information
hugithordarson committed Nov 17, 2024
1 parent e15b7e4 commit 3f4ba3e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,17 @@ public NGDynamicHTMLTag parent() {
return _parent;
}

public List<Object> children() {
return _children;
/**
* FIXME: Yeah, we need to fix this in the parser itself // Hugi 2024-11-17
*/
@Deprecated
public List<PNode> childrenWithStringsProcessedAndCombined() {

if( _children == null ) {
return null;
}

return combineAndWrapBareStrings( _children );
}

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

/**
* Iterates through the list, combining adjacent strings before wrapping them in a PHTMLNode
* Other nodes just get added to the list.
*/
private static List<PNode> combineAndWrapBareStrings( final List<Object> children ) {
final List<PNode> childElements = new ArrayList<>( children.size() );

final StringBuilder sb = new StringBuilder( 128 );

for( final Object currentChild : children ) {

if( currentChild instanceof String ) {
// If we encounter a string, we append it to the StringBuilder
sb.append( (String)currentChild );
}
else {
// If we encounter any other element and we still have unwrapped strings in our builder,
// we take the string data we've collected, wrap it up and add it to the element list.
if( sb.length() > 0 ) {
final PHTMLNode bareString = new PHTMLNode( sb.toString() );
childElements.add( bareString );
sb.setLength( 0 );
}

// ... and then add the element itself
childElements.add( (PNode)currentChild );
}
}

// If the last element happened to be a string, the StringBuilder will still have data so we wrap it here
if( sb.length() > 0 ) {
final PHTMLNode bareString = new PHTMLNode( sb.toString() );
childElements.add( bareString );
sb.setLength( 0 );
}

return childElements;
}

@Override
public String toString() {
return "NGDynamicHTMLTag [_declaration=" + _declaration + ", _children=" + _children + "]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private PNode parseHTML() throws NGHTMLFormatException, NGDeclarationFormatExcep
throw new NGHTMLFormatException( "There is an unbalanced dynamic tag named '%s'.".formatted( _currentDynamicTag.declaration().name() ) );
}

return new PGroupNode( _currentDynamicTag.children() );
return new PGroupNode( _currentDynamicTag.childrenWithStringsProcessedAndCombined() );
}

public void didParseOpeningWebObjectTag( String parsedString ) throws NGHTMLFormatException, NGDeclarationFormatException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private static NGElement toDynamicElement( final PNode node ) {

private static NGElement toDynamicElement( final NGDynamicHTMLTag tag ) {
try {
return NGApplication.dynamicElementWithName( tag.declaration().type(), toAssociations( tag.declaration() ), template( tag.children() ), Collections.emptyList() );
return NGApplication.dynamicElementWithName( tag.declaration().type(), toAssociations( tag.declaration() ), template( tag.childrenWithStringsProcessedAndCombined() ), Collections.emptyList() );
}
catch( NGElementNotFoundException e ) {
// FIXME:
Expand Down Expand Up @@ -120,17 +120,16 @@ private static NGAssociation associationForInlineBindingValue( String value ) {
/**
* @return The tag's template
*/
private static NGElement template( final List<Object> children ) {
private static NGElement template( final List<PNode> children ) {

// 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
if( children == null ) {
return null;
}

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

for( final PNode pNode : childNodes ) {
for( final PNode pNode : children ) {
childElements.add( toDynamicElement( pNode ) );
}

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

return NGDynamicGroup.of( childElements );
}

/**
* Iterates through the list, combining adjacent strings before wrapping them in a PHTMLNode
* Other nodes just get added to the list.
*/
private static List<PNode> combineAndWrapBareStrings( List<Object> children ) {
final List<PNode> childElements = new ArrayList<>( children.size() );

final StringBuilder sb = new StringBuilder( 128 );

for( final Object currentChild : children ) {

if( currentChild instanceof String ) {
// If we encounter a string, we append it to the StringBuilder
sb.append( (String)currentChild );
}
else {
// If we encounter any other element and we still have unwrapped strings in our builder,
// we take the string data we've collected, wrap it up and add it to the element list.
if( sb.length() > 0 ) {
final PHTMLNode bareString = new PHTMLNode( sb.toString() );
childElements.add( bareString );
sb.setLength( 0 );
}

// ... and then add the element itself
childElements.add( (PNode)currentChild );
}
}

// If the last element happened to be a string, the StringBuilder will still have data so we wrap it here
if( sb.length() > 0 ) {
final PHTMLNode bareString = new PHTMLNode( sb.toString() );
childElements.add( bareString );
sb.setLength( 0 );
}

return childElements;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.List;
import java.util.Objects;

public record PGroupNode( List<Object> children ) implements PNode {
public record PGroupNode( List<PNode> children ) implements PNode {

public PGroupNode {
Objects.requireNonNull( children );
Expand Down

0 comments on commit 3f4ba3e

Please sign in to comment.