Skip to content

Commit

Permalink
fix: don't replace div with nested view-component instead just insert…
Browse files Browse the repository at this point in the history
… text and add attributes
  • Loading branch information
tschuehly committed Jul 22, 2023
1 parent 70d8d75 commit df35477
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import org.thymeleaf.context.WebEngineContext
import org.thymeleaf.engine.AttributeName
import org.thymeleaf.engine.EngineEventUtils
import org.thymeleaf.exceptions.TemplateProcessingException
import org.thymeleaf.model.AttributeValueQuotes
import org.thymeleaf.model.IProcessableElementTag
import org.thymeleaf.processor.element.AbstractAttributeTagProcessor
import org.thymeleaf.processor.element.IElementTagStructureHandler
import org.thymeleaf.spring6.SpringTemplateEngine
import org.thymeleaf.spring6.context.SpringContextUtils
import org.thymeleaf.templatemode.TemplateMode
import java.lang.ClassCastException


class ThymeleafViewComponentProcessor(dialectPrefix: String) :
Expand Down Expand Up @@ -50,9 +48,11 @@ class ThymeleafViewComponentProcessor(dialectPrefix: String) :
val webContext = context as WebEngineContext
val viewContext = try {
expression.execute(webContext) as IViewContext
} catch (e: ClassCastException){
throw ViewComponentExpressionException("Could not execute expression: \"${expression.stringRepresentation}\" " +
"as ViewContext, did you forget the brackets? \${${expression.stringRepresentation}}, did you pass \"${expression.stringRepresentation}\" as ViewContextProperty?")
} catch (e: ClassCastException) {
throw ViewComponentExpressionException(
"Could not execute expression: \"${expression.stringRepresentation}\" " +
"as ViewContext, did you forget the brackets? \${${expression.stringRepresentation}}, did you pass \"${expression.stringRepresentation}\" as ViewContextProperty?"
)
} catch (e: TemplateProcessingException) {
throw ViewComponentProcessingException(e.message, e.cause)
}
Expand All @@ -67,27 +67,14 @@ class ThymeleafViewComponentProcessor(dialectPrefix: String) :
webContext.setVariables(viewContext.contextAttributes.toMap())

val modelFactory = webContext.modelFactory
val model = modelFactory.createModel().let { model ->
model.add(
modelFactory.createOpenElementTag(
"div",
mapOf(
"id" to viewComponentName,
ViewActionConstant.nestedViewComponentAttributeName to ""
),
AttributeValueQuotes.DOUBLE,
false
)
)
model.add(
modelFactory.createText(
engine.process(viewContext.componentTemplate, webContext)
)
)
model.add(modelFactory.createCloseElementTag("div"))
return@let model
}
structureHandler.replaceWith(model, false)
val viewComponentBody = modelFactory.createText(
engine.process(viewContext.componentTemplate, webContext)
)

structureHandler.setAttribute("id", viewComponentName)
structureHandler.setAttribute(ViewActionConstant.nestedViewComponentAttributeName, "")
structureHandler.removeAttribute("view:component")
structureHandler.setBody(viewComponentBody, true)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ThymeleafViewComponentIntegrationTest(
) {
@Test
fun testIndexComponent() {
val expectedHtml = """<html><div><h1>This is the IndexViewComponent</h1></div><a href="/">IndexViewComponent</a><br><a href="/simple">SimpleViewComponent</a><br><a href="/layout">LayoutViewComponent</a><br><a href="/action">ActionViewComponent</a><br><a href="/nested-action">NestedActionViewComponent</a><br></br></br></br></br></br></html>"""
val expectedHtml = """<html><div><h1>This is the IndexViewComponent</h1></div><a href="/">IndexViewComponent</a><br><a href="/simple">SimpleViewComponent</a><br><a href="/layout">LayoutViewComponent</a><br><a href="/action">ActionViewComponent</a><br><a href="/nested-action">NestedActionViewComponent</a></html>"""
assertEndpointReturns("/",expectedHtml)
}
@Test
Expand All @@ -34,7 +34,7 @@ class ThymeleafViewComponentIntegrationTest(
}
@Test
fun testNestedActionComponent() {
val expectedHtml = """<html><nav>This is a Navbar</nav><body><div id="actionviewcomponent" nestedviewcomponent=""><div><script defer="" src="/webjars/htmx.org/dist/htmx.min.js"></script><h2>ViewAction Get CountUp</h2><button hx-get="/custompath/countup" hx-target="#actionviewcomponent" hx-swap="outerHTML">Default ViewAction [GET]</button><h3>0</h3><h2>ViewAction Post AddItem</h2><form hx-post="/actionviewcomponent/additem" hx-target="#actionviewcomponent" hx-swap="outerHTML"><input type="text" name="item"><button type="submit">Save Item</button></input></form><table><tr><th>Item</th><th>Action</th></tr></table><h2>ViewAction Put/Patch Person Form</h2><form style="display: inline-grid; gap: 0.5rem"><label>Name<input type="text" id="name" name="name" value="Thomas"></input></label><label>Age: <input type="number" id="age" name="age" value="23"></input></label><label>Location: <input type="text" id="location" name="location" value="Ludwigsburg"></input></label><button type="submit" hx-put="/actionviewcomponent/savepersonput" hx-target="#actionviewcomponent" hx-swap="outerHTML">Save Changes using Put</button><button type="submit" hx-patch="/actionviewcomponent/savepersonpatch" hx-target="#actionviewcomponent" hx-swap="outerHTML">Save Changes using Patch</button></form></div></div></body><footer>This is a footer</footer></html>"""
val expectedHtml = """<nav>This is a Navbar</nav><div id="actionviewcomponent" nestedviewcomponent=""><div><script defer src="/webjars/htmx.org/dist/htmx.min.js"></script><h2>ViewAction Get CountUp</h2><button hx-get="/custompath/countup" hx-target="#actionviewcomponent" hx-swap="outerHTML">Default ViewAction [GET]</button><h3>0</h3><h2>ViewAction Post AddItem</h2><form hx-post="/actionviewcomponent/additem" hx-target="#actionviewcomponent" hx-swap="outerHTML"><input type="text" name="item"><button type="submit">Save Item</button></form><table><tbody><tr><th>Item</th><th>Action</th></tr></tbody></table><h2>ViewAction Put/Patch Person Form</h2><form style="display: inline-grid; gap: 0.5rem"><label>Name<input type="text" id="name" name="name" value="Thomas"></label><label>Age: <input type="number" id="age" name="age" value="23"></label><label>Location: <input type="text" id="location" name="location" value="Ludwigsburg"></label><button type="submit" hx-put="/actionviewcomponent/savepersonput" hx-target="#actionviewcomponent" hx-swap="outerHTML">Save Changes using Put</button><button type="submit" hx-patch="/actionviewcomponent/savepersonpatch" hx-target="#actionviewcomponent" hx-swap="outerHTML">Save Changes using Patch</button></form></div></div><footer>This is a footer</footer>"""
assertEndpointReturns("/nested-action",expectedHtml)
}
@Test
Expand Down

0 comments on commit df35477

Please sign in to comment.