Escaping is a technique used to include special characters within text that would otherwise be seen as having a different meaning. Different channels require different escaping mechanisms depending on how the text will be interpreted.
In this instance, the text will be interpreted by 3 different channels:
- JavaScript string literal
- Vue template compiler, similar to HTML format
- Template expressions treated as JavaScript, possibly including additional string literals
While JavaScript string literals use the backslash (\) character for escape sequences, Vue templates and HTML use entities prefixed with &. We need to carefully consider how to escape each part of the text starting with the expressions within the template, followed by the HTML-like syntax in the Vue template.
Firstly, we escape any expressions within the template such as $emit("enlarge-text")
, which does not require any further escaping as it contains no special characters.
Next, we handle the HTML formatting in the Vue template. This includes dealing with attributes like @click
that may contain double-quotes. To properly escape these quotes, we can use "
entities instead. For example:
<button @click="$emit("enlarge-text")">Enlarge text</button>
.
It's important to note that if working with a template written as a string literal, another level of escaping using backslashes (\) is required. This additional layer of escaping ensures that all necessary characters are properly handled without any confusion or errors.
Remembering common conventions when working with string templates, such as using backticks for the template string itself, double-quotes around attributes, and single-quotes for strings within expressions, can help minimize the need for extensive escaping. Following these guidelines simplifies the process and avoids potential issues with overlapping delimiters at different levels.