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

Add: Blob.text() method #3705

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/jsdom/living/file-api/Blob-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,17 @@ exports.implementation = class BlobImpl {
blob._buffer = slicedBuffer;
return blob;
}

text() {
const decoder = new TextDecoder("utf-8");
const buffer = this._buffer;

return new Promise((resolve, reject) => {
try {
resolve(decoder.decode(buffer));
} catch (e) {
reject(e);
}
});
}
};
2 changes: 2 additions & 0 deletions lib/jsdom/living/file-api/Blob.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ interface Blob {
Blob slice([Clamp] optional long long start,
[Clamp] optional long long end,
optional DOMString contentType);

Promise<DOMString> text();
};

enum EndingType { "transparent", "native" };
Expand Down
25 changes: 25 additions & 0 deletions test/blob-impl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use strict";
const assert = require("node:assert/strict");
const { describe, it } = require("mocha-sugar-free");

const BlobImpl =
require("../lib/jsdom/living/file-api/Blob-impl.js").implementation;
const { TextDecoder } = require("util");
global.TextDecoder = TextDecoder;

describe("File-API: Blob-impl.js", () => {
it("should return correct test from the blob", done => {
const globalObject = {};
const blobData = "Hello World";
const blobPros = { type: "text/plain" };
const blob = new BlobImpl(globalObject, [[blobData], blobPros], {});

blob
.text()
.then(text => {
assert.strictEqual(text, blobData, 'Blob text should be "Hello World"');
done();
})
.catch(done);
});
});
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";
// This includes all tests that are run using Mocha, including web-platform-tests.

require("./blob-impl.js");
require("./api/basics.js");
require("./api/cookies.js");
require("./api/encoding.js");
Expand Down