Labeling: Automatically escape HTML characters when using Formatting Tags

330
0
07-13-2023 11:23 AM
Status: Open
Labels (1)
JohannesLindner
MVP Frequent Contributor

Whey you use Formatting Tags in your label expression, the expression breaks for features that have special characters like "<", ">", or "&" in their label fields. See this question for examples.

This is because these characters are reserved in HTML: they make up the syntax. To solve this, you have to replace those characters with their HTML entity, eg

 

var txt = $feature.TextField
txt = Replace("&", "&amp;")
txt = Replace(txt, "<", "&lt;")
txt = Replace(txt, ">", "&gt;")
return "<COL red='255'>" + txt + "</COL>"

 

 

This is a lot of work, you can really easily forget to do this, you have to remember or look up the HTML entities, and users inexperienced with HTML (and probably many experienced ones, too) don't have a clue why their expression breaks for some features.

Solution: Just do those replacements behind the scenes.