Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Strip UTF-8 BOMs off of strings passed to jade.compile. #702

Merged
merged 1 commit into from

2 participants

@domenic

Sometimes, people are dumb, and files get generated with UTF-8 BOMs at the end. Jade can help those dumb people.

If you don't want to accept this that's fine, and we can just tell people to strip it off themselves. It's not as big a deal here as it is elsewhere since you pass Jade a string, not a filename, so if you leave the BOM in it's your fault, not Jade's.

@domenic domenic Strip UTF-8 BOMs off of strings passed to jade.compile.
Sometimes, people are dumb, and files get generated with UTF-8 BOMs at the end. Jade can help those dumb people.
87d9f4d
@tj tj merged commit 4efd475 into jadejs:master
@tj
Owner
tj commented

i do think something like this belongs higher up in a node API or similar but meh! fine for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 9, 2012
  1. @domenic

    Strip UTF-8 BOMs off of strings passed to jade.compile.

    domenic authored
    Sometimes, people are dumb, and files get generated with UTF-8 BOMs at the end. Jade can help those dumb people.
This page is out of date. Refresh to see the latest.
Showing with 17 additions and 2 deletions.
  1. +15 −2 lib/jade.js
  2. +1 −0  test/cases/utf8bom.html
  3. +1 −0  test/cases/utf8bom.jade
View
17 lib/jade.js
@@ -118,6 +118,17 @@ function parse(str, options){
}
/**
+ * Strip any UTF-8 BOM off of the start of `str`, if it exists.
+ *
+ * @param {String} str
+ * @return {String}
+ * @api private
+ */
+function stripBOM(str){
+ return str.charCodeAt(0) === 0xFEFF ? str.substring(1) : str;
+}
+
+/**
* Compile a `Function` representation of the given jade `str`.
*
* Options:
@@ -140,17 +151,19 @@ exports.compile = function(str, options){
: 'undefined'
, fn;
+ str = stripBOM(String(str));
+
if (options.compileDebug !== false) {
fn = [
'var __jade = [{ lineno: 1, filename: ' + filename + ' }];'
, 'try {'
- , parse(String(str), options)
+ , parse(str, options)
, '} catch (err) {'
, ' rethrow(err, __jade[0].filename, __jade[0].lineno);'
, '}'
].join('\n');
} else {
- fn = parse(String(str), options);
+ fn = parse(str, options);
}
if (client) {
View
1  test/cases/utf8bom.html
@@ -0,0 +1 @@
+<p>"foo"</p>
View
1  test/cases/utf8bom.jade
@@ -0,0 +1 @@
+p "foo"
Something went wrong with that request. Please try again.