Skip to content

Commit

Permalink
Use dynamic tag's own stored declaration for element construction
Browse files Browse the repository at this point in the history
  • Loading branch information
hugithordarson committed Oct 20, 2024
1 parent 83e0574 commit 161b7f9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ public NGDynamicHTMLTag( final NGDeclaration declaration, final NGDynamicHTMLTag
_declaration = declaration;
}

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

public NGDynamicHTMLTag parent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ else if( token.startsWith( "</" ) ) {
return token;
}

private void startOfWebObjectTag( String token ) throws NGHTMLFormatException {
private void startOfWebObjectTag( String token ) throws NGHTMLFormatException, NGDeclarationFormatException {
didParseText();
_contentText.append( token );
didParseOpeningWebObjectTag();
Expand All @@ -276,7 +276,7 @@ private void didParseText() {
}
}

private void didParseOpeningWebObjectTag() throws NGHTMLFormatException {
private void didParseOpeningWebObjectTag() throws NGHTMLFormatException, NGDeclarationFormatException {
logger.debug( "Parsed Opening WebObject ({}) : {}", _contentText.length(), _contentText );

if( _contentText.length() > 0 ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ private PNode parseHTML() throws NGHTMLFormatException, NGDeclarationFormatExcep
new NGHTMLParser( this, _htmlString ).parseHTML();

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

return new PGroupNode( _currentDynamicTag );
}

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

final int spaceIndex = parsedString.indexOf( ' ' );
int colonIndex;
Expand All @@ -80,12 +80,18 @@ public void didParseOpeningWebObjectTag( String parsedString ) throws NGHTMLForm

if( isInlineTag ) {
final NGDeclaration declaration = parseInlineTag( parsedString, colonIndex, _inlineBindingCount++ );
_declarations.put( declaration.name(), declaration );
// FIXME: It's become unnecessary for us to store an inline tag's declaration with the original parsed declarations. Just leaving this in while we're still refactoring // Hugi 2024-10-20
// _declarations.put( declaration.name(), declaration );
_currentDynamicTag = new NGDynamicHTMLTag( declaration, _currentDynamicTag );
}
else {
final String declarationName = extractDeclarationName( parsedString );
final NGDeclaration declaration = _declarations.get( declarationName );

if( declaration == null ) {
throw new NGDeclarationFormatException( "No declaration for dynamic element (or component) named '%s'".formatted( declarationName ) );
}

_currentDynamicTag = new NGDynamicHTMLTag( declaration, _currentDynamicTag );
}
}
Expand All @@ -98,7 +104,7 @@ public void didParseClosingWebObjectTag( final String parsedString ) throws NGDe
throw new NGHTMLFormatException( message );
}

final PNode node = dynamicTagToNode( _currentDynamicTag, _declarations );
final PNode node = dynamicTagToNode( _currentDynamicTag );
_currentDynamicTag = parentDynamicTag;
_currentDynamicTag.addChild( node );
}
Expand Down Expand Up @@ -147,14 +153,8 @@ static String extractDeclarationName( final String tagPart ) throws NGHTMLFormat
throw new NGHTMLFormatException( "Can't initialize dynamic tag '%s', no 'name' attribute found".formatted( tagPart ) );
}

private static PNode dynamicTagToNode( NGDynamicHTMLTag tag, final Map<String, NGDeclaration> declarations ) throws NGDeclarationFormatException {
final NGDeclaration declaration = declarations.get( tag.declarationName() );

if( declaration == null ) {
throw new NGDeclarationFormatException( "No declaration for dynamic element (or component) named '%s'".formatted( tag.declarationName() ) );
}

return new PBasicNode( tag, declaration );
private static PNode dynamicTagToNode( NGDynamicHTMLTag tag ) throws NGDeclarationFormatException {
return new PBasicNode( tag );
}

private static NGDeclaration parseInlineTag( final String tag, final int colonIndex, final int nextInlineBindingNumber ) throws NGHTMLFormatException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ public NGElement parse() throws NGDeclarationFormatException, NGHTMLFormatExcept

private static NGElement toDynamicElement( final PNode node ) throws NGDeclarationFormatException {
return switch( node ) {
case PBasicNode n -> toDynamicElement( n.tag(), n.declaration() );
case PBasicNode n -> toDynamicElement( n.tag() );
case PGroupNode n -> new NGDynamicGroup( null, null, template( n.tag() ) );
case PHTMLNode n -> new NGHTMLBareString( n.value() );
case PCommentNode n -> new NGHTMLCommentString( n.value() );
};
}

private static NGElement toDynamicElement( final NGDynamicHTMLTag tag, final NGDeclaration declaration ) throws NGDeclarationFormatException {
private static NGElement toDynamicElement( final NGDynamicHTMLTag tag ) throws NGDeclarationFormatException {
try {
return NGApplication.dynamicElementWithName( declaration.type(), toAssociations( declaration ), template( tag ), Collections.emptyList() );
return NGApplication.dynamicElementWithName( tag.declaration().type(), toAssociations( tag.declaration() ), template( tag ), Collections.emptyList() );
}
catch( NGElementNotFoundException e ) {
// FIXME:
Expand All @@ -70,7 +70,7 @@ public void appendToResponse( NGResponse response, NGContext context ) {
<a href="%s" style="padding: 10px; border: 2px solid rgba(50,50,200,0.6); box-shadow: 4px 4px 1px red; background-color: rgba(0,0,200,0.5); border-radius: 4px; text-decoration: none; color: white">
Can't find an element/component '<strong>%s</strong>'. Would you like to create it?
</a>
""".formatted( context.componentActionURL(), declaration.type() );
""".formatted( context.componentActionURL(), tag.declaration().type() );
response.appendContentString( s );
};

Expand Down Expand Up @@ -101,9 +101,10 @@ private static NGAssociation toAssociation( NGDeclaration declaration, NGBinding

if( declaration.isInline() ) {
try {
return bindingValueForInlineBindingString( bindingValue.value() );
return associationForInlineBindingString( bindingValue.value() );
}
catch( NGHTMLFormatException e ) {
// FIXME: Don't throw RuntimeException here. Awaits cleanup of inline binding association construction // Hugi 2024-10-20
throw new RuntimeException( e );
}
}
Expand All @@ -112,7 +113,7 @@ private static NGAssociation toAssociation( NGDeclaration declaration, NGBinding
}
}

private static NGAssociation bindingValueForInlineBindingString( String value ) throws NGHTMLFormatException {
private static NGAssociation associationForInlineBindingString( String value ) throws NGHTMLFormatException {
Objects.requireNonNull( value );

if( value.startsWith( "\"" ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import java.util.Objects;

public record PBasicNode( NGDynamicHTMLTag tag, NGDeclaration declaration ) implements PNode {
public record PBasicNode( NGDynamicHTMLTag tag ) implements PNode {

public PBasicNode {
Objects.requireNonNull( tag );
Objects.requireNonNull( declaration );
}
}

0 comments on commit 161b7f9

Please sign in to comment.