Add polyfills for String.trimLeft/Right for browsers without support.

This commit is contained in:
David Anson 2015-05-03 22:40:34 -07:00
parent dce6024e16
commit 85531ff21e
4 changed files with 57 additions and 1 deletions

26
demo/browser-polyfills.js Normal file
View file

@ -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
};
}

View file

@ -27,6 +27,7 @@
<footer>Copyright &copy; 2015 by <a href="//dlaa.me/">David Anson</a></footer>
</div>
</div>
<script src="browser-polyfills.js"></script>
<script src="../node_modules/markdown-it/dist/markdown-it.min.js"></script>
<script src="require-stub.js"></script>
<script src="markdownlint-browser.js"></script>

View file

@ -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"
},

View file

@ -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();
};