1

How can I escape a round bracket (right parenthesis) without closing it in a fragment?

"fragments/questionaire :: questionaire('00001_c-1',
        ${ 
            { 
            '...',
            '...', 
            'First 1) ...'
            }
        },
        ${
            { 
            '...',
            '...',
            '...' 
            }
        },'[0,2]')"

Kind of same effect is if keyword new is somewhere in the string.

already tried: 'First 1\) ...' ) ) inside utext

I use it to iterate over an object with questions and hints like:

th:fragment="questionaire(key, questions, hints, rightIndex)"

It's inside a form

<div th:each="aquestion, iterStat : ${questions}" th:with="qid=${key}+'_q-'+${iterStat.index},name='rb-'+${key}"> 
    <div> 
        <input type="checkbox" th:name="${name}+${'_q-'+iterStat.index}" th:value="0" th:data-s="${iterStat.index}" th:id="${qid}" autocomplete="off" th:checked="${bucket.read('questionaire.'+name+'_q-'+iterStat.index)}==('' + 1)"/> 
        <label th:for="${qid}" th:text="${aquestion}"></label> 
        <input type="hidden" th:name="${name}+${'_q-'+iterStat.index}" value="0">
    </div> 
    <div class="hint" th:id="${qid+'--hint'}" th:text="${hints[iterStat.index]}"></div> 
</div> 
<p class="submitContainer"> 
    <button th:data-qvalidate="|quest${key}|" th:text="#{q.check}">Check</button> 
</p>
th_lo
  • 461
  • 3
  • 18
  • Are you saying `
    ` does not work for you? If so, what happens? It works for me - no escaping needed, since `)` is inside a string literal. No need to enclose anything in a Thymeleaf fragment.
    – andrewJames Oct 25 '22 at 14:26
  • I got an error `Message: Error during execution of processor 'org.thymeleaf.standard.processor.StandardReplaceTagProcessor' (template: "default.html" - line 4, col 7)` `selector does not match selector syntax: ((/|//)?selector)?([@attrib="value" ((and|or) @attrib2="value")?])?([index])?` – th_lo Oct 25 '22 at 14:30
  • I've added an example of the fragment. What works if I put inside an apostrophe I can escape it with two apostrophe next to each other `''`. I need something like this for parenthesis – th_lo Oct 25 '22 at 14:37
  • Just a comment, not an answer: You can escape any character in a literal by [using a backslash](https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#text-literals). Not sure if that works in your specific scenario. Worth a try? – andrewJames Oct 25 '22 at 14:45
  • 1
    Do you mean like `'First 1\) ...'` ? Yes, already tried it, also to use `&rpar;` or `)` or `utext`. I think Thymeleaf processor thinks I'm closing the parameter list with `)` – th_lo Oct 25 '22 at 14:51
  • 1
    You can add what you have already tried into the body of your question (so we don't re-try it). – andrewJames Oct 25 '22 at 14:58
  • Side note: This is an unusual use of template fragment parameters (at least, it is to me). Can you explain what the end goal is, here? – andrewJames Oct 25 '22 at 14:58
  • I don't know if this will work for you, but instead of using a string literal containing a `)`, you can provide that string as a `${...}` variable. So, if you have `
    `, and if `${problemString}` resolves to `foo 1) bar`, then you are OK. I simplified your fragment to only have 1 parameter, for my case. But, yes, I don't know how to escape that `)` (or `(` for that matter) in the literal itself.
    – andrewJames Oct 26 '22 at 02:07

1 Answers1

0

Following on from my most recent comment in the question...

The least-worst solution I have is to create a Java string variable and add that to your model:

model.put("cp", ")");

where cp means "close parenthesis".

Then you can create a fragment parameter like this:

|foo 1${cp} bar|

And that passes the string foo 1) bar to the fragment without that "selector syntax" error you are currently getting.

<div th:replace="fragments/frag.html :: frag(|foo 1${cp} bar|)"></div>

And my fragment is just this:

<div th:fragment="frag(key)">
    <div th:text="${key}"></div>
</div>

I would be happy to learn of a better solution.

andrewJames
  • 19,570
  • 8
  • 19
  • 51