-
Notifications
You must be signed in to change notification settings - Fork 21
/
10-templates.js
23 lines (17 loc) · 942 Bytes
/
10-templates.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Read the section on tagged template literals here:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_template_literals
// Write a function html that can be used as a template string tag, and produces
// a string in which all the interpolated pieces are escaped as HTML. Use the
// supplied escapeHTML function to do the escaping.
// Remember that a tag function gets an array of in-between strings as its first
// argument, and then the interpolated values as further arguments.
function html(...) {
// Your code here
}
const mustEscape = '<>&"'
console.log(html`You should escape the ${mustEscape.length} characters “${mustEscape}” in HTML`)
function escapeHTML(string) {
return String(string).replace(/"/g, """).replace(/</g, "<")
.replace(/>/g, ">").replace(/&/g, "&")
}
// source: http://marijnhaverbeke.nl/talks/es6_falsyvalues2015/exercises/#HTML_templating