├── .gitignore ├── Makefile ├── README.md ├── ecmarkup.css ├── ecmarkup.js ├── index.html ├── package.json └── proposal.html /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | index.html: proposal.html 2 | ecmarkup "$<" "$@" --css ecmarkup.css --js ecmarkup.js 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Template Literals Revision 2 | 3 | For details see [the proposal](https://tc39.github.io/proposal-template-literal-revision/). 4 | -------------------------------------------------------------------------------- /ecmarkup.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-size: 18px; 3 | line-height: 1.5; 4 | font-family: Cambria, Palatino Linotype, Palatino, Liberation Serif, serif; 5 | padding: 0; 6 | color: #333; 7 | margin: 0 2% 0 31%; 8 | } 9 | 10 | body.oldtoc { 11 | margin: 0 auto; 12 | } 13 | 14 | a { 15 | text-decoration: none; 16 | color: #206ca7; 17 | } 18 | 19 | a:visited { 20 | color: #206ca7; 21 | } 22 | 23 | a:hover { 24 | text-decoration: underline; 25 | color: #239dee; 26 | } 27 | 28 | 29 | code { 30 | font-weight: bold; 31 | font-family: Consolas, Monaco, monospace; 32 | white-space: pre; 33 | } 34 | 35 | pre code { 36 | font-weight: inherit; 37 | } 38 | 39 | pre code.hljs { 40 | background-color: #fff; 41 | margin: 0; 42 | padding: 0; 43 | } 44 | 45 | ol.toc { 46 | list-style: none; 47 | padding-left: 0; 48 | } 49 | 50 | ol.toc ol.toc { 51 | padding-left: 2ex; 52 | list-style: none; 53 | } 54 | 55 | var { 56 | color: #2aa198; 57 | transition: background-color 0.25s ease; 58 | cursor: pointer; 59 | } 60 | 61 | var.referenced { 62 | background-color: #ffff33; 63 | } 64 | 65 | emu-const { 66 | font-family: sans-serif; 67 | } 68 | 69 | emu-val { 70 | font-weight: bold; 71 | } 72 | emu-alg ol, emu-alg ol ol ol ol { 73 | list-style-type: decimal; 74 | } 75 | 76 | emu-alg ol ol, emu-alg ol ol ol ol ol { 77 | list-style-type: lower-alpha; 78 | } 79 | 80 | emu-alg ol ol ol, ol ol ol ol ol ol { 81 | list-style-type: lower-roman; 82 | } 83 | 84 | emu-eqn { 85 | display: block; 86 | margin-left: 4em; 87 | } 88 | 89 | emu-eqn div:first-child { 90 | margin-left: -2em; 91 | } 92 | 93 | emu-eqn.inline { 94 | display: inline; 95 | margin: 0; 96 | white-space: nowrap; 97 | } 98 | 99 | emu-note { 100 | display: block; 101 | margin: 1em 0 1em 6em; 102 | color: #666; 103 | } 104 | 105 | emu-note span.note { 106 | text-transform: uppercase; 107 | margin-left: -6em; 108 | display: block; 109 | float: left; 110 | } 111 | 112 | emu-example { 113 | display: block; 114 | margin: 1em 3em; 115 | } 116 | 117 | emu-example figure figcaption { 118 | margin-top: 0.5em; 119 | text-align: left; 120 | } 121 | 122 | emu-production { 123 | display: block; 124 | margin-top: 1em; 125 | margin-bottom: 1em; 126 | } 127 | 128 | 129 | emu-grammar.inline, emu-production.inline, 130 | emu-grammar.inline emu-production emu-rhs, emu-production.inline emu-rhs { 131 | display: inline; 132 | } 133 | 134 | emu-grammar[collapsed] emu-production, emu-production[collapsed] { 135 | margin: 0; 136 | } 137 | 138 | emu-grammar[collapsed] emu-production emu-rhs, emu-production[collapsed] emu-rhs { 139 | display: inline; 140 | padding-left: 1ex; 141 | } 142 | 143 | emu-constraints { 144 | font-size: .75em; 145 | margin-right: 1ex; 146 | } 147 | 148 | emu-gann { 149 | margin-right: 1ex; 150 | } 151 | 152 | emu-gann emu-t:last-child, 153 | emu-gann emu-nt:last-child { 154 | margin-right: 0; 155 | } 156 | 157 | emu-geq { 158 | margin-left: 1ex; 159 | font-weight: bold; 160 | } 161 | 162 | emu-oneof { 163 | font-weight: bold; 164 | margin-left: 1ex; 165 | } 166 | 167 | emu-nt { 168 | display: inline-block; 169 | font-style: italic; 170 | white-space: nowrap; 171 | text-indent: 0; 172 | } 173 | 174 | emu-nt a, emu-nt a:visited { 175 | color: #333; 176 | } 177 | 178 | emu-rhs emu-nt { 179 | margin-right: 1ex; 180 | } 181 | 182 | emu-t { 183 | display: inline-block; 184 | font-family: monospace; 185 | font-weight: bold; 186 | white-space: nowrap; 187 | text-indent: 0; 188 | } 189 | 190 | emu-production emu-t { 191 | margin-right: 1ex; 192 | } 193 | 194 | emu-rhs { 195 | display: block; 196 | padding-left: 75px; 197 | text-indent: -25px; 198 | } 199 | 200 | emu-mods { 201 | font-size: .85em; 202 | vertical-align: sub; 203 | font-style: normal; 204 | font-weight: normal; 205 | } 206 | 207 | emu-production[collapsed] emu-mods { 208 | display: none; 209 | } 210 | 211 | emu-params, emu-opt { 212 | margin-right: 1ex; 213 | font-family: monospace; 214 | } 215 | 216 | emu-params, emu-constraints { 217 | color: #2aa198; 218 | } 219 | 220 | emu-opt { 221 | color: #b58900; 222 | } 223 | 224 | emu-gprose { 225 | font-size: 0.9em; 226 | font-family: Helvetica, Arial, sans-serif; 227 | } 228 | 229 | h1.shortname { 230 | color: #f60; 231 | font-size: 1.5em; 232 | margin: 0; 233 | } 234 | h1.version { 235 | color: #f60; 236 | font-size: 1.5em; 237 | margin: 0; 238 | } 239 | h1.title { 240 | margin-top: 0; 241 | color: #f60; 242 | } 243 | h1, h2, h3, h4, h5, h6 { 244 | position: relative; 245 | } 246 | h1 .secnum { 247 | position: absolute; 248 | text-align: right; 249 | right: 100%; 250 | margin-right: 1ex; 251 | white-space: nowrap; 252 | } 253 | 254 | h1 { font-size: 2.67em; } 255 | h2 { font-size: 2em; } 256 | h3 { font-size: 1.56em; } 257 | h4 { font-size: 1.25em; } 258 | h5 { font-size: 1.11em; } 259 | h6 { font-size: 1em; } 260 | 261 | h1 span.utils, 262 | h2 span.utils, 263 | h3 span.utils, 264 | h4 span.utils, 265 | h5 span.utils, 266 | h6 span.utils { 267 | padding-left: 1em; 268 | } 269 | 270 | h1 span.utils span.anchor a, 271 | h2 span.utils span.anchor a, 272 | h3 span.utils span.anchor a, 273 | h4 span.utils span.anchor a, 274 | h5 span.utils span.anchor a, 275 | h6 span.utils span.anchor a { 276 | color: #ccc; 277 | text-decoration: none; 278 | } 279 | 280 | h1 span.utils span.anchor a:hover, 281 | h2 span.utils span.anchor a:hover, 282 | h3 span.utils span.anchor a:hover, 283 | h4 span.utils span.anchor a:hover, 284 | h5 span.utils span.anchor a:hover, 285 | h6 span.utils span.anchor a:hover { 286 | color: #333; 287 | } 288 | 289 | emu-intro h1, emu-clause h1, emu-annex h1 { font-size: 2em; } 290 | emu-intro h2, emu-clause h2, emu-annex h2 { font-size: 1.56em; } 291 | emu-intro h3, emu-clause h3, emu-annex h3 { font-size: 1.25em; } 292 | emu-intro h4, emu-clause h4, emu-annex h4 { font-size: 1.11em; } 293 | emu-intro h5, emu-clause h5, emu-annex h5 { font-size: 1em; } 294 | emu-intro h6, emu-clause h6, emu-annex h6 { font-size: 0.9em; } 295 | emu-intro emu-intro h1, emu-clause emu-clause h1, emu-annex emu-annex h1 { font-size: 1.56em; } 296 | emu-intro emu-intro h2, emu-clause emu-clause h2, emu-annex emu-annex h2 { font-size: 1.25em; } 297 | emu-intro emu-intro h3, emu-clause emu-clause h3, emu-annex emu-annex h3 { font-size: 1.11em; } 298 | emu-intro emu-intro h4, emu-clause emu-clause h4, emu-annex emu-annex h4 { font-size: 1em; } 299 | emu-intro emu-intro h5, emu-clause emu-clause h5, emu-annex emu-annex h5 { font-size: 0.9em; } 300 | emu-intro emu-intro emu-intro h1, emu-clause emu-clause emu-clause h1, emu-annex emu-annex emu-annex h1 { font-size: 1.25em; } 301 | emu-intro emu-intro emu-intro h2, emu-clause emu-clause emu-clause h2, emu-annex emu-annex emu-annex h2 { font-size: 1.11em; } 302 | emu-intro emu-intro emu-intro h3, emu-clause emu-clause emu-clause h3, emu-annex emu-annex emu-annex h3 { font-size: 1em; } 303 | emu-intro emu-intro emu-intro h4, emu-clause emu-clause emu-clause h4, emu-annex emu-annex emu-annex h4 { font-size: 0.9em; } 304 | emu-intro emu-intro emu-intro emu-intro h1, emu-clause emu-clause emu-clause emu-clause h1, emu-annex emu-annex emu-annex emu-annex h1 { font-size: 1.11em; } 305 | emu-intro emu-intro emu-intro emu-intro h2, emu-clause emu-clause emu-clause emu-clause h2, emu-annex emu-annex emu-annex emu-annex h2 { font-size: 1em; } 306 | emu-intro emu-intro emu-intro emu-intro h3, emu-clause emu-clause emu-clause emu-clause h3, emu-annex emu-annex emu-annex emu-annex h3 { font-size: 0.9em; } 307 | emu-intro emu-intro emu-intro emu-intro emu-intro h1, emu-clause emu-clause emu-clause emu-clause emu-clause h1, emu-annex emu-annex emu-annex emu-annex emu-annex h1 { font-size: 1em; } 308 | emu-intro emu-intro emu-intro emu-intro emu-intro h2, emu-clause emu-clause emu-clause emu-clause emu-clause h2, emu-annex emu-annex emu-annex emu-annex emu-annex h2 { font-size: 0.9em; } 309 | emu-intro emu-intro emu-intro emu-intro emu-intro emu-intro h1, emu-clause emu-clause emu-clause emu-clause emu-clause emu-clause h1, emu-annex emu-annex emu-annex emu-annex emu-annex emu-annex h1 { font-size: 0.9em } 310 | 311 | emu-clause { 312 | display: block; 313 | } 314 | 315 | /* Figures and tables */ 316 | figure { display: block; margin: 1em 0 3em 0; } 317 | figure object { display: block; margin: 0 auto; } 318 | figure table.real-table { margin: 0 auto; } 319 | figure figcaption { 320 | display: block; 321 | color: #555555; 322 | font-weight: bold; 323 | text-align: center; 324 | } 325 | 326 | emu-table table { 327 | margin: 0 auto; 328 | } 329 | 330 | emu-table table, table.real-table { 331 | border-collapse: collapse; 332 | } 333 | 334 | emu-table td, emu-table th, table.real-table td, table.real-table th { 335 | border: 1px solid black; 336 | padding: 0.4em; 337 | vertical-align: baseline; 338 | } 339 | emu-table th, emu-table thead td, table.real-table th { 340 | background-color: #eeeeee; 341 | } 342 | 343 | /* Note: the left content edges of table.lightweight-table >tbody >tr >td 344 | and div.display line up. */ 345 | table.lightweight-table { 346 | border-collapse: collapse; 347 | margin: 0 0 0 1.5em; 348 | } 349 | table.lightweight-table td, table.lightweight-table th { 350 | border: none; 351 | padding: 0 0.5em; 352 | vertical-align: baseline; 353 | } 354 | 355 | /* diff styles */ 356 | ins, tr.ins > td, li.ins { 357 | background-color: #e0f8e0; 358 | text-decoration: none; 359 | border-bottom: 1px solid #396; 360 | } 361 | 362 | ins.block { 363 | display: block; 364 | } 365 | 366 | del, tr.del > td, li.del { 367 | background-color: #fee; 368 | text-decoration: line-through; 369 | } 370 | 371 | del.block { 372 | display: block; 373 | } 374 | 375 | tr.ins > td > ins, li.ins > ins { 376 | border: 0; 377 | } 378 | 379 | /* Menu Styles */ 380 | #menu-toggle { 381 | font-size: 2em; 382 | 383 | position: fixed; 384 | top: 0; 385 | left: 0; 386 | width: 1.5em; 387 | height: 1.5em; 388 | z-index: 3; 389 | visibility: hidden; 390 | 391 | background-color: #111; 392 | color: #B6C8E4; 393 | 394 | line-height: 1.5em; 395 | text-align: center; 396 | -webkit-touch-callout: none; 397 | -webkit-user-select: none; 398 | -khtml-user-select: none; 399 | -moz-user-select: none; 400 | -ms-user-select: none; 401 | user-select: none;; 402 | 403 | cursor: pointer; 404 | } 405 | 406 | #menu { 407 | position: fixed; 408 | left: 0; 409 | top: 0; 410 | height: 100%; 411 | width: 24%; 412 | z-index: 2; 413 | overflow-x: hidden; 414 | overflow-y: auto; 415 | box-sizing: border-box; 416 | 417 | background-color: #111; 418 | 419 | transition: opacity 0.1s linear; 420 | } 421 | 422 | #menu.active { 423 | display: block; 424 | opacity: 1; 425 | } 426 | 427 | #menu-toc > ol { 428 | padding: 0; 429 | } 430 | 431 | #menu-toc > ol , #menu-toc > ol ol { 432 | list-style-type: none; 433 | } 434 | 435 | #menu-toc > ol ol { 436 | padding-left: 0.75em; 437 | } 438 | 439 | #menu-toc li { 440 | text-overflow: ellipsis; 441 | overflow: hidden; 442 | white-space: nowrap; 443 | } 444 | 445 | #menu-toc .item-toggle { 446 | display: inline-block; 447 | transform: rotate(-45deg) translate(-5px, -5px); 448 | transition: transform 0.1s ease; 449 | width: 1em; 450 | 451 | color: #555F6E; 452 | 453 | -webkit-touch-callout: none; 454 | -webkit-user-select: none; 455 | -khtml-user-select: none; 456 | -moz-user-select: none; 457 | -ms-user-select: none; 458 | user-select: none;; 459 | 460 | cursor: pointer; 461 | } 462 | 463 | #menu-toc .item-toggle-none { 464 | display: inline-block; 465 | width: 1em; 466 | } 467 | 468 | #menu-toc li.active > .item-toggle { 469 | transform: rotate(45deg) translate(-5px, -5px); 470 | } 471 | 472 | #menu-toc li > ol { 473 | display: none; 474 | } 475 | 476 | #menu-toc li.active > ol { 477 | display: block; 478 | } 479 | 480 | #menu-toc li > a { 481 | padding-left: 0.25em; 482 | color: #B6C8E4; 483 | } 484 | 485 | #menu-search { 486 | color: #B6C8E4; 487 | } 488 | 489 | #menu-search-box { 490 | display: block; 491 | width: 90%; 492 | margin: 5px auto; 493 | font-size: 1em; 494 | padding: 2px; 495 | } 496 | 497 | #menu-search-results.inactive { 498 | display: none; 499 | } 500 | 501 | #menu-search-results ul { 502 | list-style-type: square; 503 | padding: 0 0 0 35px; 504 | margin: 0; 505 | } 506 | 507 | #menu-search-results li { 508 | white-space: nowrap; 509 | } 510 | 511 | #menu-search-results a { 512 | color: #b6c8e4; 513 | } 514 | 515 | @media (max-width: 1366px) { 516 | body { 517 | margin: 0 0 0 150px; 518 | } 519 | 520 | #menu { 521 | display: none; 522 | padding-top: 3em; 523 | width: 323px; 524 | } 525 | 526 | #menu-toggle { 527 | visibility: visible; 528 | } 529 | } 530 | 531 | @media only screen and (max-width: 800px) { 532 | body { 533 | margin: 2em 10px 0 10px; 534 | } 535 | 536 | #menu { 537 | width: 100%; 538 | } 539 | 540 | h1 .secnum { 541 | display: inline; 542 | position: inherit; 543 | left: 0; 544 | right: 0; 545 | } 546 | 547 | h1 .secnum:empty { 548 | margin: 0; padding: 0; 549 | } 550 | } 551 | -------------------------------------------------------------------------------- /ecmarkup.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | function Menu() { 4 | this.$toggle = document.getElementById('menu-toggle'); 5 | this.$menu = document.getElementById('menu'); 6 | this.$searchBox = document.getElementById('menu-search-box'); 7 | this.$searchResults = document.getElementById('menu-search-results'); 8 | this.initSearch(); 9 | 10 | this.$toggle.addEventListener('click', this.toggle.bind(this)); 11 | 12 | this.$searchBox.addEventListener('keydown', function (e) { 13 | if (e.keyCode === 191 && e.target.value.length === 0) { 14 | e.preventDefault(); 15 | e.stopPropagation(); 16 | } else if (e.keyCode === 13) { 17 | e.preventDefault(); 18 | e.stopPropagation(); 19 | this.selectResult(); 20 | } 21 | }.bind(this)); 22 | 23 | this.$searchBox.addEventListener('keyup', debounce(function (e) { 24 | e.stopPropagation(); 25 | this.search(e.target.value); 26 | }.bind(this))); 27 | 28 | 29 | var tocItems = this.$menu.querySelectorAll('#menu-toc li'); 30 | for (var i = 0; i < tocItems.length; i++) { 31 | var $item = tocItems[i]; 32 | $item.addEventListener('click', function($item, event) { 33 | $item.classList.toggle('active'); 34 | event.stopPropagation(); 35 | }.bind(null, $item)); 36 | } 37 | 38 | var tocLinks = this.$menu.querySelectorAll('#menu-toc li > a'); 39 | for (var i = 0; i < tocLinks.length; i++) { 40 | var $link = tocLinks[i]; 41 | $link.addEventListener('click', function(event) { 42 | this.toggle(); 43 | event.stopPropagation(); 44 | }.bind(this)); 45 | } 46 | } 47 | 48 | Menu.prototype.toggle = function () { 49 | this.$menu.classList.toggle('active'); 50 | } 51 | 52 | Menu.prototype.show = function () { 53 | this.$menu.classList.add('active'); 54 | } 55 | 56 | Menu.prototype.hide = function () { 57 | this.$menu.classList.remove('active'); 58 | } 59 | 60 | Menu.prototype.isVisible = function() { 61 | return this.$menu.classList.contains('active'); 62 | } 63 | 64 | Menu.prototype.initSearch = function () { 65 | var $biblio = document.getElementById('menu-search-biblio'); 66 | if (!$biblio) { 67 | this.biblio = {}; 68 | } else { 69 | this.biblio = JSON.parse($biblio.textContent); 70 | } 71 | 72 | this.biblio.ops = this.biblio.filter(function (e) { return e.type === 'op' }); 73 | this.biblio.clauses = this.biblio.filter(function (e) { return e.type === 'clause' }); 74 | this.biblio.productions = this.biblio.filter(function (e) { return e.type === 'production' }); 75 | 76 | document.addEventListener('keydown', function (e) { 77 | if (e.keyCode === 191) { 78 | e.preventDefault(); 79 | e.stopPropagation(); 80 | 81 | if(this.isVisible()) { 82 | this._closeAfterSearch = false; 83 | } else { 84 | this._closeAfterSearch = true; 85 | this.show(); 86 | } 87 | 88 | this.show(); 89 | this.$searchBox.focus(); 90 | } 91 | }.bind(this)) 92 | } 93 | 94 | Menu.prototype.search = function (needle) { 95 | if (needle.length < 2) { 96 | this.hideSearch(); 97 | } else { 98 | this.showSearch(); 99 | } 100 | 101 | needle = needle.toLowerCase(); 102 | 103 | var results = {}; 104 | var seenClauses = {}; 105 | 106 | results.ops = this.biblio.ops.filter(function(op) { 107 | return fuzzysearch(needle, op.aoid.toLowerCase()); 108 | }); 109 | 110 | results.ops.forEach(function(op) { 111 | seenClauses[op.refId] = true; 112 | }); 113 | 114 | results.productions = this.biblio.productions.filter(function(prod) { 115 | return fuzzysearch(needle, prod.name.toLowerCase()); 116 | }); 117 | 118 | results.clauses = this.biblio.clauses.filter(function(clause) { 119 | return !seenClauses[clause.id] && (clause.number.indexOf(needle) === 0 || fuzzysearch(needle, clause.title.toLowerCase())); 120 | }); 121 | 122 | if (results.length > 50) { 123 | results = results.slice(0, 50); 124 | } 125 | 126 | this.displayResults(results); 127 | } 128 | 129 | Menu.prototype.displayResults = function (results) { 130 | var totalResults = Object.keys(results).reduce(function (sum, record) { return sum + record.length }, 0); 131 | 132 | if (totalResults > 0) { 133 | this.$searchResults.classList.remove('no-results'); 134 | 135 | var html = '' 150 | 151 | this.$searchResults.innerHTML = html; 152 | } else { 153 | this.$searchResults.classList.add('no-results'); 154 | } 155 | } 156 | 157 | Menu.prototype.hideSearch = function () { 158 | this.$searchResults.classList.add('inactive'); 159 | } 160 | 161 | Menu.prototype.showSearch = function () { 162 | this.$searchResults.classList.remove('inactive'); 163 | } 164 | 165 | Menu.prototype.selectResult = function () { 166 | var $first = this.$searchResults.querySelector('li:first-child a'); 167 | 168 | if ($first) { 169 | document.location = $first.getAttribute('href'); 170 | } 171 | 172 | this.$searchBox.value = ''; 173 | this.$searchBox.blur(); 174 | this.hideSearch(); 175 | 176 | if (this._closeAfterSearch) { 177 | this.hide(); 178 | } 179 | } 180 | 181 | function init() { 182 | var menu = new Menu(); 183 | } 184 | 185 | document.addEventListener('DOMContentLoaded', init); 186 | 187 | function debounce(fn) { 188 | var timeout; 189 | return function() { 190 | var args = arguments; 191 | if (timeout) { 192 | clearTimeout(timeout); 193 | } 194 | timeout = setTimeout(function() { 195 | timeout = null; 196 | fn.apply(this, args); 197 | }.bind(this), 150); 198 | } 199 | } 200 | 201 | // The following license applies to the fuzzysearch function 202 | // The MIT License (MIT) 203 | // Copyright © 2015 Nicolas Bevacqua 204 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 205 | // this software and associated documentation files (the "Software"), to deal in 206 | // the Software without restriction, including without limitation the rights to 207 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 208 | // the Software, and to permit persons to whom the Software is furnished to do so, 209 | // subject to the following conditions: 210 | 211 | // The above copyright notice and this permission notice shall be included in all 212 | // copies or substantial portions of the Software. 213 | 214 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 215 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 216 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 217 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 218 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 219 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 220 | function fuzzysearch (needle, haystack) { 221 | var tlen = haystack.length; 222 | var qlen = needle.length; 223 | if (qlen > tlen) { 224 | return false; 225 | } 226 | if (qlen === tlen) { 227 | return needle === haystack; 228 | } 229 | outer: for (var i = 0, j = 0; i < qlen; i++) { 230 | var nch = needle.charCodeAt(i); 231 | while (j < tlen) { 232 | if (haystack.charCodeAt(j++) === nch) { 233 | continue outer; 234 | } 235 | } 236 | return false; 237 | } 238 | return true; 239 | } 240 | var CLAUSE_NODES = ['EMU-CLAUSE', 'EMU-INTRO', 'EMU-ANNEX']; 241 | function findLocalReferences ($elem) { 242 | var name = $elem.innerHTML; 243 | var references = []; 244 | 245 | var parentClause = $elem.parentNode; 246 | while (parentClause && CLAUSE_NODES.indexOf(parentClause.nodeName) === -1) { 247 | parentClause = parentClause.parentNode; 248 | } 249 | 250 | if(!parentClause) return; 251 | 252 | var vars = parentClause.querySelectorAll('var'); 253 | 254 | for (var i = 0; i < vars.length; i++) { 255 | var $var = vars[i]; 256 | 257 | if ($var.innerHTML === name) { 258 | references.push($var); 259 | } 260 | } 261 | 262 | return references; 263 | } 264 | 265 | function toggleFindLocalReferences($elem) { 266 | var references = findLocalReferences($elem); 267 | if ($elem.classList.contains('referenced')) { 268 | references.forEach(function ($reference) { 269 | $reference.classList.remove('referenced'); 270 | }); 271 | } else { 272 | references.forEach(function ($reference) { 273 | $reference.classList.add('referenced'); 274 | }); 275 | } 276 | } 277 | 278 | function installFindLocalReferences () { 279 | document.addEventListener('click', function (e) { 280 | if (e.target.nodeName === 'VAR') { 281 | toggleFindLocalReferences(e.target); 282 | } 283 | }); 284 | } 285 | 286 | document.addEventListener('DOMContentLoaded', installFindLocalReferences); 287 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Template Literal Revision

Stage 4 / September 12, 2017

Template Literal Revision

7 | 8 | 9 | 10 |

Introduction#

11 | 12 |

Template literals should allow the embedding of languages (DSLs etc.). But restrictions on escape sequences make this problematic.

13 | 14 |

For example, consider making a latex processor with templates:

15 | 16 |
function latex(strings) {
 17 |   // ...
 18 | }
 19 | 
 20 | let document = latex`
 21 | \newcommand{\fun}{\textbf{Fun!}}  // works just fine
 22 | \newcommand{\unicode}{\textbf{Unicode!}} // Illegal token!
 23 | \newcommand{\xerxes}{\textbf{King!}} // Illegal token!
 24 | 
 25 | Breve over the h goes \u{h}ere // Illegal token!
 26 | `
27 | 28 |

The problem here is that \u is the start of a unicode escape, but ES grammar forces it to be of the form \u00FF or \u{42} 29 | and considers the token \unicode illegal. 30 | Similarly \x is the start of a hex escape like \xFF but \xerxes is illegal. Octal literal escapes have the same problem; \0100 is illegal.

31 | 32 |

Proposal Overview

33 | 34 |

Remove the restriction on escape sequences.

35 | 36 |

Lifting the restriction raises the question of how to handle cooked template values that contain illegal escape sequences. Currently, cooked template values are supposed to replace escape sequences with the "Unicode code point represented by the escape sequence" but this can't happen if the escape sequence is not valid.

37 | 38 |

The proposed solution is to set the cooked value to undefined for template values that contain illegal escape sequences. The raw value is still accessible via .raw so embedded DSLs that might contain undefined cooked values can just use the raw string:

39 | 40 |
function tag(strs) {
 41 |   strs[0] === undefined
 42 |   strs.raw[0] === "\\unicode and \\u{55}";
 43 | }
 44 | tag`\unicode and \u{55}`
45 | 46 |

This loosening of the escape sequence restriction only applies to tagged template literals; untagged templates still throw an early error for invalid escape sequences:

47 | 48 |
let bad = `bad escape sequence: \unicode`; // throws early error
49 |
50 | 51 | 52 | 53 |

1(11.8.6) Template Literal Lexical Components#

54 |

Syntax

55 | 56 | 57 | Template::NoSubstitutionTemplate 58 | TemplateHead 59 | 60 | 61 | NoSubstitutionTemplate::`TemplateCharactersopt` 62 | 63 | 64 | TemplateHead::`TemplateCharactersopt${ 65 | 66 | 67 | TemplateSubstitutionTail::TemplateMiddle 68 | TemplateTail 69 | 70 | 71 | TemplateMiddle::}TemplateCharactersopt${ 72 | 73 | 74 | TemplateTail::}TemplateCharactersopt` 75 | 76 | 77 | TemplateCharacters::TemplateCharacterTemplateCharactersopt 78 | 79 | 80 | 81 | 82 | TemplateCharacter::$[lookahead ≠ {] 83 | \EscapeSequence 84 | LineContinuation 85 | LineTerminatorSequence 86 | SourceCharacterbut not one of ` or \ or $ or LineTerminator 87 | 88 | 89 | 90 | 91 | TemplateCharacter::$[lookahead ≠ {] 92 | \EscapeSequence 93 | \NotEscapeSequence 94 | LineContinuation 95 | LineTerminatorSequence 96 | SourceCharacterbut not one of ` or \ or $ or LineTerminator 97 | 98 | 99 | 100 | 101 | 102 | NotEscapeSequence::0DecimalDigit 103 | DecimalDigitbut not 0 104 | x[lookahead ∉ HexDigit] 105 | xHexDigit[lookahead ∉ HexDigit] 106 | u[lookahead ∉ HexDigit][lookahead ≠ {] 107 | uHexDigit[lookahead ∉ HexDigit] 108 | uHexDigitHexDigit[lookahead ∉ HexDigit] 109 | uHexDigitHexDigitHexDigit[lookahead ∉ HexDigit] 110 | u{[lookahead ∉ HexDigit] 111 | u{NotCodePoint 112 | u{CodePoint[lookahead ≠ }] 113 | 114 | 115 | NotCodePoint::HexDigitsbut not if MV of HexDigits ≤ 0x10FFFF 116 | 117 | 118 | CodePoint::HexDigitsbut not if MV of HexDigits > 0x10FFFF 119 | 120 | 121 | 122 |

A conforming implementation must not use the extended definition of EscapeSequence described in B.1.2 when parsing a TemplateCharacter.

123 | Note 124 |

TemplateSubstitutionTail is used by the InputElementTemplateTail alternative lexical goal.

125 |
126 |
127 | 128 | 129 | 130 |

2(11.8.6.1) Static Semantics: TV and TRV#

131 |

A template literal component is interpreted as a sequence of Unicode code points. The Template Value (TV) of a literal component is described in terms of code unit values (SV, 11.8.4) contributed by the various parts of the template literal component. As part of this process, some Unicode code points within the template component are interpreted as having a mathematical value (MV, 11.8.3). In determining a TV, escape sequences are replaced by the UTF-16 code unit(s) of the Unicode code point represented by the escape sequence. The Template Raw Value (TRV) is similar to a Template Value with the difference that in TRVs escape sequences are interpreted literally.

132 | 526 | Note 527 |

TV excludes the code units of LineContinuation while TRV includes them. <CR><LF> and <CR> LineTerminatorSequences are normalized to <LF> for both TV and TRV. An explicit EscapeSequence is needed to include a <CR> or <CR><LF> sequence.

528 |
529 |
530 | 531 | 532 | 533 | 534 |

3(12.2.9) Template Literals#

535 |

Syntax

536 | 537 | 538 | 539 | TemplateLiteral[Yield]:NoSubstitutionTemplate 540 | SubstitutionTemplate[?Yield] 541 | 542 | 543 | SubstitutionTemplate[Yield]:TemplateHeadExpression[In, ?Yield]TemplateSpans[?Yield] 544 | 545 | 546 | TemplateSpans[Yield]:TemplateTail 547 | TemplateMiddleList[?Yield]TemplateTail 548 | 549 | 550 | TemplateMiddleList[Yield]:TemplateMiddleExpression[In, ?Yield] 551 | TemplateMiddleList[?Yield]TemplateMiddleExpression[In, ?Yield] 552 | 553 | 554 | 555 | 556 | 557 | TemplateLiteral[Yield, Tagged]:NoSubstitutionTemplate 558 | SubstitutionTemplate[?Yield, ?Tagged] 559 | 560 | 561 | SubstitutionTemplate[Yield, Tagged]:TemplateHeadExpression[In, ?Yield]TemplateSpans[?Yield, ?Tagged] 562 | 563 | 564 | TemplateSpans[Yield, Tagged]:TemplateTail 565 | TemplateMiddleList[?Yield, ?Tagged]TemplateTail 566 | 567 | 568 | TemplateMiddleList[Yield, Tagged]:TemplateMiddleExpression[In, ?Yield] 569 | TemplateMiddleList[?Yield, ?Tagged]TemplateMiddleExpression[In, ?Yield] 570 | 571 | 572 |
573 | 574 | 575 | 576 | 577 |

4Static Semantics: Early Errors#

578 | 579 | TemplateLiteral[Yield, Tagged]:NoSubstitutionTemplate 580 | 581 | 587 | 588 | 589 | SubstitutionTemplate[Yield, Tagged]:TemplateHeadExpression[In, ?Yield]TemplateSpans[?Yield, ?Tagged] 590 | 591 | 597 | 598 | 599 | TemplateSpans[Yield, Tagged]:TemplateTail 600 | 601 | 607 | 608 | 609 | TemplateMiddleList[Yield, Tagged]:TemplateMiddleExpression[In, ?Yield] 610 | TemplateMiddleList[?Yield, ?Tagged]TemplateMiddleExpression[In, ?Yield] 611 | 612 | 618 |
619 |
620 | 621 | 622 | 691 | 692 | 693 | 694 |

5(12.3) Left-Hand-Side Expressions#

695 |

Syntax

696 | 697 | 698 | MemberExpression[Yield]:PrimaryExpression[?Yield] 699 | MemberExpression[?Yield][Expression[In, ?Yield]] 700 | MemberExpression[?Yield].IdentifierName 701 | 702 | MemberExpression[?Yield]TemplateLiteral[?Yield] 703 | MemberExpression[?Yield]TemplateLiteral[?Yield, Tagged] 704 | 705 | SuperProperty[?Yield] 706 | MetaProperty 707 | newMemberExpression[?Yield]Arguments[?Yield] 708 | 709 | 710 | 711 | CallExpression[Yield]:MemberExpression[?Yield]Arguments[?Yield] 712 | SuperCall[?Yield] 713 | CallExpression[?Yield]Arguments[?Yield] 714 | CallExpression[?Yield][Expression[?Yield]] 715 | CallExpression[?Yield].IdentifierName 716 | 717 | CallExpression[?Yield]TemplateLiteral[?Yield] 718 | CallExpression[?Yield]TemplateLiteral[?Yield, Tagged] 719 | 720 | 721 | 722 | 723 | 724 |
725 |

ACopyright & Software License#

726 | 727 |

Copyright Notice

728 |

© 2016 Tim Disney

729 | 730 |

Software License

731 |

All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT http://www.ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.

732 | 733 |

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

734 | 735 |
    736 |
  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. 737 |
  3. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  4. 738 |
  5. Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.
  6. 739 |
740 | 741 |

THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

742 | 743 |
744 | 745 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "name": "template-literal-revision", 4 | "version": "0.0.0", 5 | "description": "ECMA-262 proposal to update tempalte literals", 6 | "main": "index.js", 7 | "scripts": { 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+ssh://git@github.com/disnt/template-literal-revision.git" 13 | }, 14 | "keywords": [ 15 | "ECMA-262", 16 | "ECMAScript", 17 | "template literals", 18 | "proposal" 19 | ], 20 | "author": "Tim Disney", 21 | "license": "UNLICENSED", 22 | "devDependencies": { 23 | "ecmarkup": "^3.2.2" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /proposal.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  8 | title: Template Literal Revision
  9 | status: candidate
 10 | stage: 4
 11 | contributors: Tim Disney
 12 | toc: true
 13 | 
14 | 15 | 16 | 17 |

Introduction

18 | 19 |

Template literals should allow the embedding of languages (DSLs etc.). But restrictions on escape sequences make this problematic.

20 | 21 |

For example, consider making a latex processor with templates:

22 | 23 |

 24 |     function latex(strings) {
 25 |       // ...
 26 |     }
 27 | 
 28 |     let document = latex`
 29 |     \newcommand{\fun}{\textbf{Fun!}}  // works just fine
 30 |     \newcommand{\unicode}{\textbf{Unicode!}} // Illegal token!
 31 |     \newcommand{\xerxes}{\textbf{King!}} // Illegal token!
 32 | 
 33 |     Breve over the h goes \u{h}ere // Illegal token!
 34 |     `
 35 | 
 36 |   
37 | 38 |

The problem here is that `\u` is the start of a unicode escape, but ES grammar forces it to be of the form `\u00FF` or `\u{42}` 39 | and considers the token `\unicode` illegal. 40 | Similarly `\x` is the start of a hex escape like `\xFF` but `\xerxes` is illegal. Octal literal escapes have the same problem; `\0100` is illegal.

41 | 42 |

Proposal Overview

43 | 44 |

Remove the restriction on escape sequences.

45 | 46 |

Lifting the restriction raises the question of how to handle cooked template values that contain illegal escape sequences. Currently, cooked template values are supposed to replace escape sequences with the "Unicode code point represented by the escape sequence" but this can't happen if the escape sequence is not valid.

47 | 48 |

The proposed solution is to set the cooked value to `undefined` for template values that contain illegal escape sequences. The raw value is still accessible via `.raw` so embedded DSLs that might contain `undefined` cooked values can just use the raw string:

49 | 50 |

 51 |     function tag(strs) {
 52 |       strs[0] === undefined
 53 |       strs.raw[0] === "\\unicode and \\u{55}";
 54 |     }
 55 |     tag`\unicode and \u{55}`
 56 |   
57 | 58 |

This loosening of the escape sequence restriction only applies to tagged template literals; untagged templates still throw an early error for invalid escape sequences:

59 | 60 |

 61 |     let bad = `bad escape sequence: \unicode`; // throws early error
 62 |   
63 |
64 | 65 | 66 | 67 |

(11.8.6) Template Literal Lexical Components

68 |

Syntax

69 | 70 | 71 | Template :: 72 | NoSubstitutionTemplate 73 | TemplateHead 74 | 75 | NoSubstitutionTemplate :: 76 | ``` TemplateCharacters? ``` 77 | 78 | TemplateHead :: 79 | ``` TemplateCharacters? `${` 80 | 81 | TemplateSubstitutionTail :: 82 | TemplateMiddle 83 | TemplateTail 84 | 85 | TemplateMiddle :: 86 | `}` TemplateCharacters? `${` 87 | 88 | TemplateTail :: 89 | `}` TemplateCharacters? ``` 90 | 91 | TemplateCharacters :: 92 | TemplateCharacter TemplateCharacters? 93 | 94 | 95 | 96 | 97 | TemplateCharacter :: 98 | `$` [lookahead != `{` ] 99 | `\` EscapeSequence 100 | LineContinuation 101 | LineTerminatorSequence 102 | SourceCharacter but not one of ``` or `\` or `$` or LineTerminator 103 | 104 | 105 | 106 | 107 | TemplateCharacter :: 108 | `$` [lookahead != `{` ] 109 | `\` EscapeSequence 110 | `\` NotEscapeSequence 111 | LineContinuation 112 | LineTerminatorSequence 113 | SourceCharacter but not one of ``` or `\` or `$` or LineTerminator 114 | 115 | 116 | 117 | 118 | 119 | NotEscapeSequence :: 120 | `0` DecimalDigit 121 | DecimalDigit but not `0` 122 | `x` [lookahead <! HexDigit] 123 | `x` HexDigit [lookahead <! HexDigit] 124 | `u` [lookahead <! HexDigit] [lookahead != `{`] 125 | `u` HexDigit [lookahead <! HexDigit] 126 | `u` HexDigit HexDigit [lookahead <! HexDigit] 127 | `u` HexDigit HexDigit HexDigit [lookahead <! HexDigit] 128 | `u` `{` [lookahead <! HexDigit] 129 | `u` `{` NotCodePoint 130 | `u` `{` CodePoint [lookahead != `}`] 131 | 132 | NotCodePoint :: 133 | HexDigits [> but not if MV of HexDigits ≤ 0x10FFFF ] 134 | 135 | CodePoint :: 136 | HexDigits [> but not if MV of HexDigits > 0x10FFFF ] 137 | 138 | 139 | 140 |

A conforming implementation must not use the extended definition of |EscapeSequence| described in when parsing a |TemplateCharacter|.

141 | 142 |

|TemplateSubstitutionTail| is used by the |InputElementTemplateTail| alternative lexical goal.

143 |
144 |
145 | 146 | 147 | 148 |

(11.8.6.1) Static Semantics: TV and TRV

149 |

A template literal component is interpreted as a sequence of Unicode code points. The Template Value (TV) of a literal component is described in terms of code unit values (SV, ) contributed by the various parts of the template literal component. As part of this process, some Unicode code points within the template component are interpreted as having a mathematical value (MV, ). In determining a TV, escape sequences are replaced by the UTF-16 code unit(s) of the Unicode code point represented by the escape sequence. The Template Raw Value (TRV) is similar to a Template Value with the difference that in TRVs escape sequences are interpreted literally.

150 | 357 | 358 |

TV excludes the code units of |LineContinuation| while TRV includes them. <CR><LF> and <CR> |LineTerminatorSequence|s are normalized to <LF> for both TV and TRV. An explicit |EscapeSequence| is needed to include a <CR> or <CR><LF> sequence.

359 |
360 |
361 | 362 | 363 | 364 | 365 |

(12.2.9) Template Literals

366 |

Syntax

367 | 368 | 369 | 370 | TemplateLiteral[Yield] : 371 | NoSubstitutionTemplate 372 | SubstitutionTemplate[?Yield] 373 | 374 | SubstitutionTemplate[Yield] : 375 | TemplateHead Expression[In, ?Yield] TemplateSpans[?Yield] 376 | 377 | TemplateSpans[Yield] : 378 | TemplateTail 379 | TemplateMiddleList[?Yield] TemplateTail 380 | 381 | TemplateMiddleList[Yield] : 382 | TemplateMiddle Expression[In, ?Yield] 383 | TemplateMiddleList[?Yield] TemplateMiddle Expression[In, ?Yield] 384 | 385 | 386 | 387 | 388 | 389 | TemplateLiteral[Yield, Tagged] : 390 | NoSubstitutionTemplate 391 | SubstitutionTemplate[?Yield, ?Tagged] 392 | 393 | SubstitutionTemplate[Yield, Tagged] : 394 | TemplateHead Expression[In, ?Yield] TemplateSpans[?Yield, ?Tagged] 395 | 396 | TemplateSpans[Yield, Tagged] : 397 | TemplateTail 398 | TemplateMiddleList[?Yield, ?Tagged] TemplateTail 399 | 400 | TemplateMiddleList[Yield, Tagged] : 401 | TemplateMiddle Expression[In, ?Yield] 402 | TemplateMiddleList[?Yield, ?Tagged] TemplateMiddle Expression[In, ?Yield] 403 | 404 | 405 |
406 | 407 | 408 | 409 | 410 |

Static Semantics: Early Errors

411 | 412 | TemplateLiteral[Yield, Tagged] : NoSubstitutionTemplate 413 | 414 | 419 | 420 | 421 | SubstitutionTemplate[Yield, Tagged] : TemplateHead Expression[In, ?Yield] TemplateSpans[?Yield, ?Tagged] 422 | 423 | 428 | 429 | 430 | TemplateSpans[Yield, Tagged] : TemplateTail 431 | 432 | 437 | 438 | 439 | TemplateMiddleList[Yield, Tagged] : 440 | TemplateMiddle Expression[In, ?Yield] 441 | TemplateMiddleList[?Yield, ?Tagged] TemplateMiddle Expression[In, ?Yield] 442 | 443 | 448 |
449 |
450 | 451 | 452 | 521 | 522 | 523 | 524 |

(12.3) Left-Hand-Side Expressions

525 |

Syntax

526 | 527 | 528 | PrimaryExpression 529 | 530 | MemberExpression 531 | [ 532 | Expression 533 | ] 534 | 535 | 536 | MemberExpression 537 | . 538 | IdentifierName 539 | 540 | 541 | 542 | MemberExpression 543 | TemplateLiteral 544 | 545 | 546 | MemberExpression 547 | TemplateLiteral 548 | 549 | 550 | 551 | SuperProperty 552 | 553 | 554 | MetaProperty 555 | 556 | 557 | new 558 | MemberExpression 559 | Arguments 560 | 561 | 562 | 563 | 564 | 565 | MemberExpression 566 | Arguments 567 | 568 | 569 | SuperCall 570 | 571 | 572 | CallExpression 573 | Arguments 574 | 575 | 576 | CallExpression 577 | [ 578 | Expression 579 | ] 580 | 581 | 582 | CallExpression 583 | . 584 | IdentifierName 585 | 586 | 587 | 588 | CallExpression 589 | TemplateLiteral 590 | 591 | 592 | CallExpression 593 | TemplateLiteral 594 | 595 | 596 | 597 | 598 | 599 | 600 |
601 | --------------------------------------------------------------------------------