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]);
        }
    }
}
0

There are 0 best solutions below