-
-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathtest-setup.ts
More file actions
73 lines (71 loc) · 1.77 KB
/
Copy pathtest-setup.ts
File metadata and controls
73 lines (71 loc) · 1.77 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
import '@angular/compiler';
import { setupTestBed } from '@analogjs/vitest-angular/setup-testbed';
setupTestBed();
if (typeof HTMLElement !== 'undefined') {
const BLK = '\x01';
const blockTags = new Set([
'DIV',
'P',
'H1',
'H2',
'H3',
'H4',
'H5',
'H6',
'LI',
'TR',
'BLOCKQUOTE',
'SECTION',
'ARTICLE',
'HEADER',
'FOOTER',
'NAV',
'UL',
'OL',
'DL',
'TABLE',
'HR',
'FIGCAPTION',
'FIGURE',
'DETAILS',
'SUMMARY',
]);
function getInnerTextPoly(node: Node, insidePre: boolean): string {
if (node.nodeType === 3) return node.nodeValue || '';
if (node.nodeType !== 1) return '';
const el = node as Element;
const tag = el.tagName;
if (tag === 'SCRIPT' || tag === 'STYLE') return '';
if (tag === 'BR') return '\n';
const isPre = tag === 'PRE';
const isBlock = blockTags.has(tag) || isPre;
const childPre = insidePre || isPre;
let text = '';
for (const child of Array.from(node.childNodes)) {
text += getInnerTextPoly(child, childPre);
}
if (!insidePre && !isPre) {
text = text.replace(/[ \t]+/g, ' ');
}
if (isBlock) {
text = BLK + text + BLK;
}
return text;
}
Object.defineProperty(HTMLElement.prototype, 'innerText', {
get(this: HTMLElement) {
let text = '';
for (const child of Array.from(this.childNodes)) {
text += getInnerTextPoly(child, false);
}
text = text.replace(new RegExp(BLK + '+', 'g'), '\n');
const lines = text.split('\n');
const trimmed = lines.map((l) => l.trim()).join('\n');
return trimmed.replace(/^\n+/, '').replace(/\n$/, '');
},
set(this: HTMLElement, value: string) {
this.textContent = value;
},
configurable: true,
});
}