Skip to content

Commit

Permalink
Store declaration of a parsed dynamic tag with the tag itself
Browse files Browse the repository at this point in the history
  • Loading branch information
hugithordarson committed Oct 20, 2024
1 parent b5fb5d0 commit 83e0574
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class NGDynamicHTMLTag {
/**
* Name of the tag (i.e. the value of the 'name' attribute, that links to the declaration
*/
private final String _declarationName;
private final NGDeclaration _declaration;

/**
* Parent tag
Expand All @@ -27,18 +27,19 @@ public class NGDynamicHTMLTag {

public NGDynamicHTMLTag() {
_parent = null;
_declarationName = null;
_declaration = null;
}

public NGDynamicHTMLTag( final String declarationName, final NGDynamicHTMLTag parentTag ) throws NGHTMLFormatException {
Objects.requireNonNull( declarationName );
public NGDynamicHTMLTag( final NGDeclaration declaration, final NGDynamicHTMLTag parentTag ) throws NGHTMLFormatException {
Objects.requireNonNull( declaration );

_parent = parentTag;
_declarationName = declarationName;
_declaration = declaration;
}

@Deprecated
public String declarationName() {
return _declarationName;
return _declaration.name();
}

public NGDynamicHTMLTag parent() {
Expand All @@ -59,8 +60,20 @@ public void addChild( final Object stringOrElement ) {
_children.add( stringOrElement );
}

/**
* @return true if the given tag is the root of the element tree.
*
* FIXME:
* The implementation of this kind of sucks. The only tag in the tree without a declaration is currently the root tag
* We should probably introduce another specific marker for the root tage, or give it a different type. Just anything else than a null check, that's just a consequence of an implementation detail.
* // Hugi 2024-10-20
*/
public boolean isRoot() {
return _declaration == null;
}

@Override
public String toString() {
return "NGDynamicHTMLTag [_declarationName=" + _declarationName + ", _children=" + _children + "]";
return "NGDynamicHTMLTag [_declaration=" + _declaration + ", _children=" + _children + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@ private PNode parseHTML() throws NGHTMLFormatException, NGDeclarationFormatExcep

new NGHTMLParser( this, _htmlString ).parseHTML();

final String currentDynamicTagName = _currentDynamicTag.declarationName();

if( currentDynamicTagName != null ) {
throw new NGHTMLFormatException( "There is an unbalanced dynamic tag named '%s'.".formatted( currentDynamicTagName ) );
if( !_currentDynamicTag.isRoot() ) {
throw new NGHTMLFormatException( "There is an unbalanced dynamic tag named '%s'.".formatted( _currentDynamicTag.declarationName() ) );
}

return new PGroupNode( _currentDynamicTag );
Expand All @@ -83,10 +81,12 @@ public void didParseOpeningWebObjectTag( String parsedString ) throws NGHTMLForm
if( isInlineTag ) {
final NGDeclaration declaration = parseInlineTag( parsedString, colonIndex, _inlineBindingCount++ );
_declarations.put( declaration.name(), declaration );
_currentDynamicTag = new NGDynamicHTMLTag( declaration.name(), _currentDynamicTag );
_currentDynamicTag = new NGDynamicHTMLTag( declaration, _currentDynamicTag );
}
else {
_currentDynamicTag = new NGDynamicHTMLTag( extractDeclarationName( parsedString ), _currentDynamicTag );
final String declarationName = extractDeclarationName( parsedString );
final NGDeclaration declaration = _declarations.get( declarationName );
_currentDynamicTag = new NGDynamicHTMLTag( declaration, _currentDynamicTag );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ private static NGElement template( NGDynamicHTMLTag tag ) throws NGDeclarationFo
final NGElement onlyElement = childElements.get( 0 );

if( onlyElement instanceof NGComponentReference ) {
return new NGDynamicGroup( tag.declarationName(), null, onlyElement );
return new NGDynamicGroup( null, null, onlyElement );
}

return onlyElement;
}

return new NGDynamicGroup( tag.declarationName(), null, childElements );
return new NGDynamicGroup( null, null, childElements );
}

/**
Expand Down

0 comments on commit 83e0574

Please sign in to comment.