Browse files

rebuild parser and add escape pragma example

  • Loading branch information...
1 parent bb309c2 commit 42840ae90e18a9f9cf763612ec812db0c2163b1d akdubya committed Jan 26, 2011
Showing with 42 additions and 12 deletions.
  1. +36 −12 lib/parser.js
  2. +6 −0 test/examples.js
View
48 lib/parser.js
@@ -17,6 +17,31 @@ var parser = (function(){
var rightmostMatchFailuresExpected = [];
var cache = {};
+ function padLeft(input, padding, length) {
+ var result = input;
+
+ var padLength = length - input.length;
+ for (var i = 0; i < padLength; i++) {
+ result = padding + result;
+ }
+
+ return result;
+ }
+
+ function escape(ch) {
+ var charCode = ch.charCodeAt(0);
+
+ if (charCode < 0xFF) {
+ var escapeChar = 'x';
+ var length = 2;
+ } else {
+ var escapeChar = 'u';
+ var length = 4;
+ }
+
+ return '\\' + escapeChar + padLeft(charCode.toString(16).toUpperCase(), '0', length);
+ }
+
function quote(s) {
/*
* ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a
@@ -25,12 +50,11 @@ var parser = (function(){
* Any character may appear in the form of an escape sequence.
*/
return '"' + s
- .replace(/\\/g, '\\\\') // backslash
- .replace(/"/g, '\\"') // closing quote character
- .replace(/\r/g, '\\r') // carriage return
- .replace(/\u2028/g, '\\u2028') // line separator
- .replace(/\u2029/g, '\\u2029') // paragraph separator
- .replace(/\n/g, '\\n') // line feed
+ .replace(/\\/g, '\\\\') // backslash
+ .replace(/"/g, '\\"') // closing quote character
+ .replace(/\r/g, '\\r') // carriage return
+ .replace(/\n/g, '\\n') // line feed
+ .replace(/[\x80-\uFFFF]/g, escape) // non-ASCII characters
+ '"';
}
@@ -240,13 +264,13 @@ var parser = (function(){
var savedPos0 = pos;
var result2 = parse_ld();
if (result2 !== null) {
- if (input.substr(pos).match(/^[#?^<+@]/) !== null) {
+ if (input.substr(pos).match(/^[#?^<+@%]/) !== null) {
var result3 = input.charAt(pos);
pos++;
} else {
var result3 = null;
if (reportMatchFailures) {
- matchFailed("[#?^<+@]");
+ matchFailed("[#?^<+@%]");
}
}
if (result3 !== null) {
@@ -1883,13 +1907,13 @@ var parser = (function(){
var savedPos0 = pos;
var result3 = parse_ld();
if (result3 !== null) {
- if (input.substr(pos).match(/^[#?^><+%:@\/~]/) !== null) {
+ if (input.substr(pos).match(/^[#?^><+%:@\/~%]/) !== null) {
var result4 = input.charAt(pos);
pos++;
} else {
var result4 = null;
if (reportMatchFailures) {
- matchFailed("[#?^><+%:@\\/~]");
+ matchFailed("[#?^><+%:@\\/~%]");
}
}
if (result4 !== null) {
@@ -2187,13 +2211,13 @@ var parser = (function(){
}
- if (input.substr(pos).match(/^[  ]/) !== null) {
+ if (input.substr(pos).match(/^[ \xA0\uFEFF]/) !== null) {
var result0 = input.charAt(pos);
pos++;
} else {
var result0 = null;
if (reportMatchFailures) {
- matchFailed("[  ]");
+ matchFailed("[ \\xA0\\uFEFF]");
}
}
View
6 test/examples.js
@@ -87,6 +87,12 @@ exports.dustExamples = [
expected: "<script>alert('Hello!')</script>\n&lt;script&gt;alert('Goodbye!')&lt;/script&gt;"
},
{
+ name: "escape_pragma",
+ source: "{%esc:s}\n {unsafe}{~n}\n {%esc:h}\n {unsafe}\n {/esc}\n{/esc}",
+ context: { unsafe: "<script>alert('Goodbye!')</script>" },
+ expected: "<script>alert('Goodbye!')</script>\n&lt;script&gt;alert('Goodbye!')&lt;/script&gt;"
+ },
+ {
name: "else_block",
source: "{#foo}\n" +
" foo,{~s}\n" +

0 comments on commit 42840ae

Please sign in to comment.