Skip to content

Enhance compile time un-matched closing element #114

Open

Description

@benjamin-dreux

One thing that is especially anoying with html is unmatched closing elements.

In a general purpose language the compiler will help with it.

In this case, I don't get why java's s type system doesn't help
I'm not even sure that we can enhance html fow to give a better feedback faster

Here is the case

public static void template (Div<?> container) {
        container
            .div()
                .h2()
                    
                .__() //h2
                .div().attrClass("row")
                    .div().attrClass("col-md-12")
                        
                        .__() //img
                    .__() //div
                .__() //div
            .__();
    }

Notice the closing element for img tag, that is missing.
Here is the error message that is produced at run time, when the templated is compiled

2024-10-05T19:11:00.145-04:00 ERROR 13800 --- [ifsr] [  restartedMain] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personController' defined in file [/Users/benjamindreux/Dépot/ifsr/target/classes/org/uqam/ifsr/person/PersonController.class]: Failed to instantiate [org.uqam.ifsr.person.PersonController]: Constructor threw exception
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:318) ~[spring-beans-6.1.13.jar:6.1.13]
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:306) ~[spring-beans-6.1.13.jar:6.1.13]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1375) ~[spring-beans-6.1.13.jar:6.1.13]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1212) ~[spring-beans-6.1.13.jar:6.1.13]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:562) ~[spring-beans-6.1.13.jar:6.1.13]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522) ~[spring-beans-6.1.13.jar:6.1.13]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:337) ~[spring-beans-6.1.13.jar:6.1.13]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.1.13.jar:6.1.13]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:335) ~[spring-beans-6.1.13.jar:6.1.13]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.1.13.jar:6.1.13]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:975) ~[spring-beans-6.1.13.jar:6.1.13]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:971) ~[spring-context-6.1.13.jar:6.1.13]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:625) ~[spring-context-6.1.13.jar:6.1.13]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.3.4.jar:3.3.4]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-3.3.4.jar:3.3.4]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456) ~[spring-boot-3.3.4.jar:3.3.4]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) ~[spring-boot-3.3.4.jar:3.3.4]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1363) ~[spring-boot-3.3.4.jar:3.3.4]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1352) ~[spring-boot-3.3.4.jar:3.3.4]
	at org.uqam.ifsr.IfsrApplication.main(IfsrApplication.java:13) ~[classes/:na]
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:580) ~[na:na]
	at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:50) ~[spring-boot-devtools-3.3.4.jar:3.3.4]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.uqam.ifsr.person.PersonController]: Constructor threw exception
	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:221) ~[spring-beans-6.1.13.jar:6.1.13]
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117) ~[spring-beans-6.1.13.jar:6.1.13]
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:315) ~[spring-beans-6.1.13.jar:6.1.13]
	... 22 common frames omitted
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 1000
	at htmlflow.visitor.Indentation.tabs(Indentation.java:72) ~[htmlflow-4.0.jar:na]
	at htmlflow.visitor.HtmlVisitor.newlineAndIndent(HtmlVisitor.java:125) ~[htmlflow-4.0.jar:na]
	at htmlflow.visitor.HtmlVisitor.visitParent(HtmlVisitor.java:161) ~[htmlflow-4.0.jar:na]
	at org.xmlet.htmlapifaster.ElementVisitor.visitParentHtml(ElementVisitor.java:421) ~[htmlApiFaster-1.0.12.jar:na]
	at org.xmlet.htmlapifaster.Html.__(Html.java:29) ~[htmlApiFaster-1.0.12.jar:na]
	at org.uqam.ifsr.baseTemplate.Layout.lambda$view$1(Layout.java:45) ~[classes/:na]
	at htmlflow.HtmlFlow.preprocessing(HtmlFlow.java:52) ~[htmlflow-4.0.jar:na]
	at htmlflow.HtmlFlow.view(HtmlFlow.java:101) ~[htmlflow-4.0.jar:na]
	at org.uqam.ifsr.baseTemplate.Layout.view(Layout.java:12) ~[classes/:na]
	at org.uqam.ifsr.person.PersonController.<init>(PersonController.java:15) ~[classes/:na]
	at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62) ~[na:na]
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502) ~[na:na]
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486) ~[na:na]
	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:208) ~[spring-beans-6.1.13.jar:6.1.13]
	... 24 common frames omitted

If this can't be identified at compile time, then the error message should be rewritted in order to give a better context on what is happening.

Something along the line of "This is generally due to miss match in closing elements"

Hope this helps

Activity

  1. fmcarvalho commented on Jan 15, 2025

    @fmcarvalho
    Member

    It’s not possible to enforce this check at compile time.

    However, we can handle this scenario at runtime by checking for negative values of indentation. If such a case occurs, we can throw a meaningful exception, for example:
    IllegalStateException("Closing unmatched tag!");

    Additionally, the stack trace could be made more concise by excluding intermediate calls like newLineAndIndent() and tabs(). Instead, the exception would be thrown at:

    htmlflow.visitor.HtmlVisitor.visitParent(HtmlVisitor.java:161) ~[htmlflow-4.0.jar:na]
    ...
    

    Nevertheless, this implementation will require a performance analysis.

  2. fmcarvalho commented on Jun 12, 2025

    @fmcarvalho
    Member

    I have tried a prototype of a PreprocessingCheckMatchingTags extending ElementVisitor, which overrides visitElement and visitParent to control matching tags. However I cannot use it as a visitor of HtmlView , which requires a HtmlVisitor rather than a ElementVisitor.
    (Note that class HtmlVisitor extends ElementVisitor)

    Also, I cannot extend HtmlVisitor instead, because visitElement and visitParent are final and I want to keep them like that.

    For now I am not figuring out a good way of implementing this feature. My first tentative is next branch feature-check-un-matched-tags.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions