Created
February 8, 2013 04:44
-
-
Save rodw/4736648 to your computer and use it in GitHub Desktop.
Demonstrates a minor issue with node-html-encoder's `htmlEncode` and `htmlDecode` functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Encoder = require('./lib/encoder.js').Encoder; | |
var encoder = new Encoder('entity'); | |
console.log(''); | |
console.log('Generally, when no characters need to be encoded,'); | |
console.log('htmlEncode() returns input string.'); | |
console.log(''); | |
console.log('For example,'); | |
console.log(' encoder.htmlEncode("Foo")'); | |
console.log('yields'); | |
console.log(' "'+encoder.htmlEncode("Foo")+'".'); // we expect to see "Foo" here. | |
console.log(''); | |
console.log('This holds true even when the input string has leading'); | |
console.log('or trailing whitespace. For example,'); | |
console.log(' encoder.htmlEncode(" Foo ")'); | |
console.log('yields'); | |
console.log(' "'+encoder.htmlEncode(" Foo ")+'".'); // we expect to see " Foo " here. | |
console.log(''); | |
console.log('But when the input string is comprised entirely of'); | |
console.log('whitespace, htmlEncode() always returns a blank'); | |
console.log('string, rather than the original input string.'); | |
console.log(''); | |
console.log('For example,'); | |
console.log(' encoder.htmlEncode(" ")'); | |
console.log('yields'); | |
console.log(' "'+encoder.htmlEncode(" ")+'".'); // we expect to see "" here, but we'd like to see " " | |
console.log('or'); | |
console.log(' encoder.htmlEncode(" ")'); | |
console.log('yields'); | |
console.log(' "'+encoder.htmlEncode(" ")+'".'); // we expect to see "" here, but we'd like to see " " | |
console.log(''); | |
console.log(''); | |
console.log('htmlDecode() has the same problem.'); | |
console.log(' encoder.htmlDecode("Foo")'); | |
console.log('yields'); | |
console.log(' "'+encoder.htmlDecode("Foo")+'".'); // we expect to see "Foo" here. | |
console.log(''); | |
console.log('And'); | |
console.log(' encoder.htmlDecode(" Foo ")'); | |
console.log('yields'); | |
console.log(' "'+encoder.htmlDecode(" Foo ")+'".'); // we expect to see " Foo " here. | |
console.log(''); | |
console.log('But'); | |
console.log(' encoder.htmlDecode(" ")'); | |
console.log('yields'); | |
console.log(' "'+encoder.htmlDecode(" ")+'".'); // we expect to see "" here, but we'd like to see " " | |
console.log(''); | |
console.log('and'); | |
console.log(' encoder.htmlDecode(" ")'); | |
console.log('yields'); | |
console.log(' "'+encoder.htmlDecode(" ")+'".'); // we expect to see "" here, but we'd like to see " " | |
console.log(''); | |
console.log(''); | |
console.log('This is easy to fix. Rather than something like'); | |
console.log(' if(this.isBlank(str)) return \'\';'); | |
console.log('we should use'); | |
console.log(' if(this.isBlank(str)) return str;'); | |
console.log(''); | |
console.log('When the input string was truly empty, the'); | |
console.log('behavior doesn\'t change, but when given a'); | |
console.log('non-empty but blank string (one comprised'); | |
console.log('entirely of whitespace), now we get back the'); | |
console.log('orginal string, unchanged.'); | |
console.log(''); | |
console.log('It doesn\'t look like any of the code depends'); | |
console.log('on htmlEncode and htmlDecode reducing blank'); | |
console.log('strings to empty ones, so I believe this is'); | |
console.log('safe change to make.'); | |
console.log(''); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Save this in the project root directory (the one containing
package.json
) and run it with something like