Skip to content

Enable processing of HTML event handler attributes in JAVASCRIPT template mode #707

Closed

Description

@danielfernandez

HTML event handler attributes (th:on*) such as:

<a th:onclick="...">

...could benefit from their values being considered inlined javascript blocks just like the bodies of <script th:inline="javascript"> tags. This would allow its contents be applied all the features, including security benefits, made available by Thymeleaf to inlined JavaScript code (including JavaScript-text-literal escaping).

So such an event handler could now be written as:

<a th:onclick="doSomething([[${textdata}]]);">

And the code above, if textdata has the value One "JavaScript" message will result in, first, the textdata variable being output as a JavaScript literal (and therefore escaped) as "One \"JavaScript\" message", and then the whole value of the HTML attribute being HTML-escaped as expected:

<a onclick="doSomething(&quot;One \&quot;JavaScript\&quot; message&quot;);">

Note this new feature will alleaviate the restriction on the evaluation of variables in these handlers imposed by means of #705, while protecting from unintented output of JavaScript-unescaped data that might not be trustable.

Activity

  1. added this to the Thymeleaf 3.0 milestone on Jul 23, 2018
  2. added a commit that references this issue on Jul 23, 2018
    fe44b93
  3. danielfernandez commented on Jul 23, 2018

    @danielfernandez
    MemberAuthor

    Already in 3.0.10-SNAPSHOT

  4. added a commit that references this issue on Jul 18, 2023
    155914d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions