ultraq / thymeleafjs

A basic implementation of the Thymeleaf templating engine in JavaScript
Apache License 2.0
52 stars 8 forks source link

Link URL Expressions Bug #40

Open tao1991123 opened 3 years ago

tao1991123 commented 3 years ago

Code

const { TemplateEngine, STANDARD_CONFIGURATION } = require("thymeleaf");

const templateEngine = new TemplateEngine(STANDARD_CONFIGURATION);

(async () => {
  const content1 =
    '<script type="text/javascript"  th:src="@{//www.testcdn.com/test/{version}/index.js(version=${version})}"></script>';

  const content2 =
    '<script type="text/javascript"  th:src="@{//www.testcdn.com/test/{version}/index.js(version=${version}, cdn=${cdn})}"></script>';
  const content3 =
    '<script type="text/javascript"  th:src="@{//www.testcdn.com/test/{version}/{version}/index.js(version=${version})}"></script>';
  const content4 =
    '<script type="text/javascript"  th:src="@{//{cdn}/test/{version}/index.js(cdn=${cdn}, version=${version})}"></script>';
 const content5 =
  '<script type="text/javascript"  th:src="@{//www.testcdn.com/test/{VERSION}/index.js(version=${version})}"></script>';

  const html = await templateEngine.process(content1, {
    cdn: "www.testcdn.com",
    version: "2.0.0",
  });
  console.log("html", html);
})();

in Java, all cases are rendered successfully

content1

render successfully

<html><head><script type="text/javascript" src="//www.testcdn.com/test/2.0.0/index.js"></script></head><body></body></html>

content 2

there is a space character between 'index.js?' and 'cdn='。

<html><head><script type="text/javascript" src="//www.testcdn.com/test/2.0.0/index.js? cdn=www.testcdn.com"></script></head><body></body></html>

content 3 & content 4

render fail, it will be in a infinite loop.

content 5

render fail, it should throw a error rather than fall in a infinite loop.

ultraq commented 3 years ago

Looks like the regex for parsing URL expressions can't handle all these cases. There's a TODO in the expression language to turn it into a proper expression to parse/process instead of that regex.

https://github.com/ultraq/thymeleafjs/blob/997947d747b51346aab8d43a86048bf2c66748fd/source/standard/expressions/ThymeleafExpressionLanguage.js#L123-L124

Doing that should go some way to help fix this bug.