From 27070705d3b58d3d4c78a35b89e4532d2e64c37b Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Wed, 21 Oct 2020 09:56:32 -0500 Subject: [PATCH] fix: remove string.repeat for ie11 (#1772) --- src/Lexer.js | 5 +++-- src/helpers.js | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Lexer.js b/src/Lexer.js index dc551458..27a740ce 100644 --- a/src/Lexer.js +++ b/src/Lexer.js @@ -1,6 +1,7 @@ const Tokenizer = require('./Tokenizer.js'); const { defaults } = require('./defaults.js'); const { block, inline } = require('./rules.js'); +const { repeatString } = require('./helpers.js'); /** * smartypants text replacement @@ -340,14 +341,14 @@ module.exports = class Lexer { if (links.length > 0) { while ((match = this.tokenizer.rules.inline.reflinkSearch.exec(maskedSrc)) != null) { if (links.includes(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) { - maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex); + maskedSrc = maskedSrc.slice(0, match.index) + '[' + repeatString('a', match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex); } } } } // Mask out other blocks while ((match = this.tokenizer.rules.inline.blockSkip.exec(maskedSrc)) != null) { - maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex); + maskedSrc = maskedSrc.slice(0, match.index) + '[' + repeatString('a', match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex); } while (src) { diff --git a/src/helpers.js b/src/helpers.js index e5b88932..93c75a1b 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -228,6 +228,22 @@ function checkSanitizeDeprecation(opt) { } } +// copied from https://stackoverflow.com/a/5450113/806777 +function repeatString(pattern, count) { + if (count < 1) { + return ''; + } + let result = ''; + while (count > 1) { + if (count & 1) { + result += pattern; + } + count >>= 1; + pattern += pattern; + } + return result + pattern; +} + module.exports = { escape, unescape, @@ -239,5 +255,6 @@ module.exports = { splitCells, rtrim, findClosingBracket, - checkSanitizeDeprecation + checkSanitizeDeprecation, + repeatString };