Skip to content

Commit 420dce9

Browse files
authored
Merge pull request #41 from jonschlinkert/fix/CVE-2023-26115-2
🔒fix: CVE 2023 26115 (2)
2 parents 786ebf1 + ace0b3c commit 420dce9

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

index.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
/*!
22
* word-wrap <https://github.com/jonschlinkert/word-wrap>
33
*
4-
* Copyright (c) 2014-2017, Jon Schlinkert.
4+
* Copyright (c) 2014-2023, Jon Schlinkert.
55
* Released under the MIT License.
66
*/
77

8+
function trimEnd(str) {
9+
let lastCharPos = str.length - 1;
10+
let lastChar = str[lastCharPos];
11+
while(lastChar === ' ' || lastChar === '\t') {
12+
lastChar = str[--lastCharPos];
13+
}
14+
return str.substring(0, lastCharPos + 1);
15+
}
16+
17+
function trimTabAndSpaces(str) {
18+
const lines = str.split('\n');
19+
const trimmedLines = lines.map((line) => trimEnd(line));
20+
return trimmedLines.join('\n');
21+
}
22+
823
module.exports = function(str, options) {
924
options = options || {};
1025
if (str == null) {
@@ -36,7 +51,7 @@ module.exports = function(str, options) {
3651
}).join(newline);
3752

3853
if (options.trim === true) {
39-
result = result.replace(/[ \t]*$/gm, '');
54+
result = trimTabAndSpaces(result);
4055
}
4156
return result;
4257
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "word-wrap",
33
"description": "Wrap words to a specified length.",
4-
"version": "1.2.3",
4+
"version": "1.2.4",
55
"homepage": "https://github.com/jonschlinkert/word-wrap",
66
"author": "Jon Schlinkert (https://github.com/jonschlinkert)",
77
"contributors": [

test.js

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ describe('wrap', function () {
3434
assert.equal(wrap(str, {trim: true}), 'A project without documentation is like a project\nthat doesn\'t exist. Verb solves this by making it\ndead simple to generate project documentation,\nusing simple markdown templates, with zero\nconfiguration required.');
3535
});
3636

37+
it('should trim trailing whitespace (even for empty lines):', function () {
38+
assert.equal(wrap("a \n\nb \n \nc\t", {trim: true}), 'a\n\nb\n\nc');
39+
});
40+
3741
it('should handle strings with just newlines', function () {
3842
assert.equal(wrap('\r\n', {indent: '\r\n', width: 18}), '\r\n');
3943
});

0 commit comments

Comments
 (0)