diff --git a/demo/browser-polyfills.js b/demo/browser-polyfills.js new file mode 100644 index 00000000..d62d41a6 --- /dev/null +++ b/demo/browser-polyfills.js @@ -0,0 +1,26 @@ +"use strict"; + +// Polyfills for browsers that do not support String.trimLeft/Right +function trimLeftPolyfill() { + return this.replace(/^\s*/, ""); +} +/* istanbul ignore if */ +if (!String.prototype.trimLeft) { + String.prototype.trimLeft = trimLeftPolyfill; +} +function trimRightPolyfill() { + return this.replace(/\s*$/, ""); +} +/* istanbul ignore if */ +if (!String.prototype.trimRight) { + String.prototype.trimRight = trimRightPolyfill; +} + +// Export for testing +/* istanbul ignore else */ +if ((typeof module !== "undefined") && module.exports) { + module.exports = { + "trimLeftPolyfill": trimLeftPolyfill, + "trimRightPolyfill": trimRightPolyfill + }; +} diff --git a/demo/default.htm b/demo/default.htm index 3f7b532a..e7cb66bb 100644 --- a/demo/default.htm +++ b/demo/default.htm @@ -27,6 +27,7 @@ + diff --git a/package.json b/package.json index bd78d54c..e2dce25c 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "test": "nodeunit", "test-cover": "istanbul cover node_modules/nodeunit/bin/nodeunit", "debug": "node debug node_modules/nodeunit/bin/nodeunit", - "lint": "eslint lib test & eslint --env browser --global markdownit --global markdownlint --rule \"no-unused-vars: 0\" demo & eslint --rule \"no-console: 0, no-shadow: 0\" example", + "lint": "eslint lib test & eslint --env browser --global markdownit --global markdownlint --rule \"no-unused-vars: 0, no-extend-native: 0\" demo & eslint --rule \"no-console: 0, no-shadow: 0\" example", "build-demo": "browserify lib/markdownlint.js --standalone markdownlint --exclude markdown-it --outfile demo/markdownlint-browser.js", "example": "npm install through2 & cd example & node standalone.js & grunt markdownlint & gulp markdownlint" }, diff --git a/test/markdownlint-test.js b/test/markdownlint-test.js index 897cbc12..74b03bc5 100644 --- a/test/markdownlint-test.js +++ b/test/markdownlint-test.js @@ -7,6 +7,7 @@ var Q = require("q"); var markdownlint = require("../lib/markdownlint"); var shared = require("../lib/shared"); var rules = require("../lib/rules"); +var polyfills = require("../demo/browser-polyfills"); function createTestForFile(file) { return function testForFile(test) { @@ -667,3 +668,31 @@ module.exports.typeAllFiles = function typeAllFiles(test) { }); test.done(); }; + +module.exports.trimPolyfills = function trimPolyfills(test) { + var inputs = [ + "text text", + " text text ", + " text text ", + // ECMAScript Whitespace + "\u0009 text text \u0009", + "\u000b text text \u000b", + "\u000c text text \u000c", + "\u0020 text text \u0020", + "\u00a0 text text \u00a0", + "\ufeff text text \ufeff", + // ECMAScript LineTerminator + "\u000a text text \u000a", + "\u000d text text \u000d", + "\u2028 text text \u2028", + "\u2029 text text \u2029" + ]; + test.expect(inputs.length * 2); + inputs.forEach(function forInput(input) { + test.equal(polyfills.trimLeftPolyfill.call(input), input.trimLeft(), + "trimLeft incorrect for '" + input + "'"); + test.equal(polyfills.trimRightPolyfill.call(input), input.trimRight(), + "trimRight incorrect for '" + input + "'"); + }); + test.done(); +};