Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use regex that can be slow #295

Merged
merged 1 commit into from
Dec 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions lib/inlines.js
Original file line number Diff line number Diff line change
Expand Up @@ -980,17 +980,38 @@ var parseInline = function(block) {
// Parse string content in block into inline children,
// using refmap to resolve references.
var parseInlines = function(block) {
// trim() removes non-ASCII whitespaces, vertical tab, form feed and so on.
// String.protoype.trim() removes non-ASCII whitespaces, vertical tab, form feed and so on.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim#return_value
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#white_space
// Removes only ASCII tab and space.
this.subject = block._string_content.replace(/^[\t \r\n]+|[\t \r\n]+$/g, "")
this.subject = trim(block._string_content)
this.pos = 0;
this.delimiters = null;
this.brackets = null;
while (this.parseInline(block)) {}
block._string_content = null; // allow raw string to be garbage collected
this.processEmphasis(null);

function trim(str) {
var start = 0;
for(; start < str.length; start++) {
if (!isSpace(str.charCodeAt(start))) {
break;
}
}
var end = str.length - 1;
for(; end >= start; end--) {
if (!isSpace(str.charCodeAt(end))) {
break;
}
}
return str.slice(start, end + 1);

function isSpace(c) {
// U+0020 = space, U+0009 = tab, U+000A = LF, U+000D = CR
return c === 0x20 || c === 9 || c === 0xa || c === 0xd;
}
}
};

// The InlineParser object.
Expand Down
Loading