/
replaceAsync.js
47 lines (45 loc) · 1.86 KB
/
replaceAsync.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const htmlFindSrc = require("../src");
const chai = require("chai");
const assert = chai.assert;
describe("replaceAsync", function () {
it("should do replacement correctly", async function () {
assert.deepEqual(
await htmlFindSrc.replaceAsync('...<img src="./abc.jpg">abc</img>...<img width=100 src="./hello.jpg">abc</img>...', "hi.jpg", {
tag: "img",
attr: "src",
})
, `...<img src="hi.jpg">abc</img>...<img width=100 src="hi.jpg">abc</img>...`,
)
});
it("should do async replacement correctly", async function () {
assert.deepEqual(
await htmlFindSrc.replaceAsync('...<img src="./abc.jpg">abc</img>...<img width=100 src="./hello.jpg">abc</img>...',
_ => _.value.toUpperCase(), {
tag: "img",
attr: "src",
},
), `...<img src="./ABC.JPG">abc</img>...<img width=100 src="./HELLO.JPG">abc</img>...`)
assert.deepEqual(
await htmlFindSrc.replaceAsync('...<img src="./abc.jpg">abc</img>...<img width=100 src="./hello.jpg">abc</img>...',
_ => new Promise(resolve => {
setTimeout(() => resolve(_.value.toUpperCase()), 1000)
}), {
tag: "img",
attr: "src",
},
), `...<img src="./ABC.JPG">abc</img>...<img width=100 src="./HELLO.JPG">abc</img>...`)
});
it("should be able to work with parseUrl", async function () {
assert.deepEqual(
await htmlFindSrc.replaceAsync('...<img src="./abc.jpg">abc</img>...<img width=100 src="./hello.jpg">abc</img>...',
_ => new Promise(resolve => {
setTimeout(() => resolve(_.parsedUrl), 1000)
}), {
parseAttrValueAsUrl: true,
baseUrl: "http://example.com",
tag: "img",
attr: "src",
},
), `...<img src="http://example.com/abc.jpg">abc</img>...<img width=100 src="http://example.com/hello.jpg">abc</img>...`)
});
});