0

I am using Java 8 and Handlebars version 4.1.2.

I am sending numbers as a string from UI and the handlebars are not returning the entire string in it.

Here are the scenarios which are failing:

Issue1:

Numbers followed by space followed by text (alphabets or numbers or special characters). In this case, only the starting number is displayed

  • Ex 1: 102 somename - only 102 is displayed.
  • Ex 2: 100 009 - only 100 is displayed

Issue2:

Text within double quotes followed by text. In this case only string within the text is displayed.

  • Ex 1: "title Fix" count - "title Fix" is displayed

But this should not be the case, they should display as 102 somename, 100 009, "title Fix" count.

I have been trying with multiple versions of handlebars but none worked.

Here is my file content:

Hbs file content:

{ "title": "{{#escapeSpecialCharactersForTitle title}}{{/escapeSpecialCharactersForTitle}}"

}

Helper class:

public static void registerEscapeSpecialCharactersForTitle(Handlebars handlebars) {

    handlebars.registerHelper("escapeSpecialCharactersForTitle",
            (str,options) -> {
                if(str!= null && !isEmpty(str.toString())) {
                    String title = str.toString();
                    escapeAllSpecialCharacatersFromString(title);
                    String finalString = JSONValue.toJSONString(title);
                    return finalString;
                }
                return "";
            });
}

public static void escapeAllSpecialCharacatersFromString(String finalString) {
     String[] specialCharacters = {"^","$","{","}","[","]","(",")",".","*","+","?","|","<",">","-","&","%","'","\\","/","="};

    for(int i=0;i<specialCharacters.length;i++) {
        if(finalString.contains(specialCharacters[i])) {
            finalString = finalString.replace(specialCharacters[i],"\\"+specialCharacters[i]);
        }
    }
}
  • And FYI, for the text which includes quotes (""), I am escaping the double quotes So from UI its sent as \"title Fix\" count – Harshini R May 09 '23 at 15:46
  • I can think of no reason why a portion of a string after a space would get chopped-off. – 76484 May 10 '23 at 13:44
  • but in my scenario I have a custom helper function created where we pass the text with specialCharacters to escape them and handlebar internally assumes the string as a IntNode and truncates anything beyond the number for that , example 123 123 – Harshini R May 10 '23 at 16:19
  • Then I think you should share the code of your custom helper. – 76484 May 10 '23 at 16:23
  • Thank you, I have added the custom helper code under the question details. – Harshini R May 10 '23 at 16:51
  • Why are you using this helper as a Block Helper (ie., `{{#escapeSpecialCharactersForTitle title}}` - starting with the '#'). It doesn't look like you want this to be a Block Helper. – 76484 May 10 '23 at 17:57
  • I will need to use the blockHelper here based on my project structure. Anyway, even without that helper call in hbs file, I still receive the truncated string in case of 100 009 as 100 – Harshini R May 11 '23 at 14:08
  • If the problem is not with the helper then it must be somewhere else in your code and so I don't think I will be able to help you. – 76484 May 11 '23 at 14:18

0 Answers0