Token replacer and HTML characters in the token

1
We have text generated by the CKEditor. Since it is rich text this text will be HTML encoded. We also use tokens in this text. Now let say we have a token with value 'This & That'. The problem is that the token replacer does not HTML encode this value. The result when using this a XHTML generated document template that the creation of this template breaks because the string should have been 'This &amp That'. Now on how to solve this. Since the original string was already HTML encoded you can not encode the string again. Searching for characters in the string that are not properly HTML encoded can be done but is not funny to do and should be done for each problamatic character. In my opinion the token replacer should have a boolean option to HTML encode the tokens. I wonder how other developers has handled this problem. Anyone?   Regards, Ronald  
asked
1 answers
3

Ronald,

 

You could use JTidy in a java action see http://jtidy.sourceforge.net/index.html

With a smaal java action you can get the escaped values as in the sample code below:

		String test = "<p>action  & prices</p>";
		InputStream stream = new ByteArrayInputStream(test.getBytes(StandardCharsets.UTF_8));
		Tidy tidy = new Tidy();
		tidy.setXHTML(true);
		tidy.setPrintBodyOnly(true);
		tidy.setShowErrors(0);
		tidy.setQuiet(true);
		tidy.setShowWarnings(false);
		tidy.parse(stream, System.out);

This code will take in the string with html and return the corrected version to the System.out, with some effort this should be easliy integrated into your Mendix app as a java action call.

answered