This repository was archived by the owner on Oct 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathinline-style-resource-inline-visitor.spec.ts
More file actions
119 lines (110 loc) · 3.66 KB
/
inline-style-resource-inline-visitor.spec.ts
File metadata and controls
119 lines (110 loc) · 3.66 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import {ASTNode} from '../../ast';
import {MockWorkerScope, MockResponse} from '../../testing';
import {InlineStyleResourceInlineVisitor} from './';
const createResponseHelper = (body: string, contentType: string) => {
const response = new MockResponse(body);
response.headers = {
get(name: string) {
if (name === 'content-type') {
return contentType;
}
}
};
return response;
};
describe('ResourceInlineVisitor', () => {
let simpleNode: ASTNode;
let nestedAst: ASTNode;
let multiStyles: ASTNode;
let jpgInlineVisitor: InlineStyleResourceInlineVisitor;
let pngJpgInlineVisitor: InlineStyleResourceInlineVisitor;
let scope: MockWorkerScope;
beforeEach(() => {
scope = new MockWorkerScope();
jpgInlineVisitor = new InlineStyleResourceInlineVisitor(scope, ['jpg']);
pngJpgInlineVisitor = new InlineStyleResourceInlineVisitor(scope, ['png', 'jpg']);
simpleNode = {
attrs: [{
name: 'style',
value: 'color: #fff; background-image: url(bar.jpg);'
}],
nodeName: 'div',
};
multiStyles = {
attrs: [{
name: 'style',
value: 'color: #fff; background-image: url(bar.jpg);'
}, {
name: 'style',
value: 'color: #fff; background-image: url(baz.jpg);'
}],
nodeName: 'div',
};
nestedAst = {
nodeName: 'body',
attrs: null,
childNodes: [
{
nodeName: 'div',
attrs: null,
childNodes: [
{
nodeName: 'p',
attrs: null,
childNodes: [
{
nodeName: 'span',
attrs: [{
name: 'style',
value: 'color: #fff; background-image: url(bar.jpg);'
}],
}
]
}
]
},
{
nodeName: 'section',
attrs: null,
childNodes: [
{
nodeName: 'span',
attrs: [{
name: 'style',
value: 'font-size: 42px; background-image: url(bar.png);'
}]
}
]
}
]
};
});
it('should inline assets in style attribute', (done: any) => {
scope.mockResponses['bar.jpg'] = createResponseHelper('bar', 'image/jpg');
jpgInlineVisitor.visit(simpleNode)
.then(() => {
expect(simpleNode.attrs[0].value).toBe('color: #fff; background-image: url(data:image/jpg;base64,YgBhAHIA);');
done();
});
});
it('should work with nested elements', (done: any) => {
scope.mockResponses['bar.jpg'] = createResponseHelper('bar', 'image/jpg');
scope.mockResponses['bar.png'] = createResponseHelper('bar', 'image/png');
pngJpgInlineVisitor.visit(nestedAst)
.then(() => {
expect(nestedAst.childNodes[0].childNodes[0].childNodes[0].attrs[0].value).toBe('color: #fff; background-image: url(data:image/jpg;base64,YgBhAHIA);');
expect(nestedAst.childNodes[1].childNodes[0].attrs[0].value).toBe('font-size: 42px; background-image: url(data:image/png;base64,YgBhAHIA);');
done();
});
});
it('should always pick the last occurence of the style attribute', (done: any) => {
scope.mockResponses['bar.jpg'] = createResponseHelper('bar', 'image/jpg');
scope.mockResponses['baz.jpg'] = createResponseHelper('baz', 'image/jpg');
jpgInlineVisitor.visit(multiStyles)
.then(() => {
expect(multiStyles.attrs[0].value).toBe('color: #fff; background-image: url(bar.jpg);');
expect(multiStyles.attrs[1].value).toBe('color: #fff; background-image: url(data:image/jpg;base64,YgBhAHoA);');
done();
});
});
});