Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion lib/internal/blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function getSource(source, endings) {
source = new Uint8Array(source);
} else if (!isArrayBufferView(source)) {
if (endings === 'native')
source = RegExpPrototypeSymbolReplace(/\n|\r\n/g, source, EOL);
source = RegExpPrototypeSymbolReplace(/\r\n|\r|\n/g, source, EOL);
source = enc.encode(source);
}

Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,24 @@ assert.throws(() => new Blob({}), {
const b = new Blob(['hello\n'], { endings: 'native' });
assert.strictEqual(b.size, EOL.length + 5);

// The WHATWG "convert line endings to native" algorithm normalizes every
// standalone "\r", "\n", and "\r\n" sequence to the native line ending.
// Refs: https://w3c.github.io/FileAPI/#convert-line-endings-to-native
(async () => {
const cases = [
['a\rb', `a${EOL}b`],
['a\nb', `a${EOL}b`],
['a\r\nb', `a${EOL}b`],
['a\r\rb', `a${EOL}${EOL}b`],
['a\n\rb', `a${EOL}${EOL}b`],
['\r\n\r', `${EOL}${EOL}`],
];
for (const [input, expected] of cases) {
const blob = new Blob([input], { endings: 'native' });
assert.strictEqual(await blob.text(), expected);
}
})().then(common.mustCall());

[1, {}, 'foo'].forEach((endings) => {
assert.throws(() => new Blob([], { endings }), {
code: 'ERR_INVALID_ARG_VALUE',
Expand Down
Loading