-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
util.ts
159 lines (144 loc) · 5.39 KB
/
util.ts
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import {
Constructable,
IRegistry,
PLATFORM
} from '@aurelia/kernel';
import {
Aurelia,
CustomElementResource,
ILifecycle
} from '@aurelia/runtime';
import { expect } from 'chai';
import {
_,
eachCartesianJoin,
eachCartesianJoinFactory,
ensureNotCalled,
htmlStringify,
jsonStringify,
massReset,
massRestore,
massSpy,
massStub,
padRight,
stringify,
verifyEqual
} from '../../../../scripts/test-lib';
import {
h
} from '../../../../scripts/test-lib-dom';
import { HTMLTestContext, TestContext } from '../util';
export function cleanup(ctx: HTMLTestContext): void {
const body = ctx.doc.body;
let current = body.firstElementChild;
while (current !== null) {
const next = current.nextElementSibling;
if (current.tagName === 'APP') {
body.removeChild(current);
}
current = next;
}
}
export function setup(ctx: HTMLTestContext, template: string | Node, $class: Constructable | null, ...registrations: any[]) {
const { container, lifecycle, observerLocator } = ctx;
container.register(...registrations);
const host = ctx.createElement('app');
const au = new Aurelia(container);
const App = CustomElementResource.define({ name: 'app', template }, $class);
const component = new App() as any;
return { container, lifecycle, host, au, component, observerLocator };
}
export function setupAndStart(ctx: HTMLTestContext, template: string | Node, $class: Constructable | null, ...registrations: any[]) {
const { container, lifecycle, host, au, component, observerLocator } = setup(ctx, template, $class, ...registrations);
au.app({ host, component });
au.start();
return { container, lifecycle, host, au, component, observerLocator };
}
export function setupWithDocument(ctx: HTMLTestContext, template: string | Node, $class: Constructable | null, ...registrations: any[]) {
const { container, lifecycle, host, au, component, observerLocator } = setup(ctx, template, $class, ...registrations);
ctx.doc.body.appendChild(host);
return { container, lifecycle, host, au, component, observerLocator };
}
export function setupWithDocumentAndStart(ctx: HTMLTestContext, template: string | Node, $class: Constructable | null, ...registrations: any[]) {
const { container, lifecycle, host, au, component, observerLocator } = setupWithDocument(ctx, template, $class, ...registrations);
au.app({ host, component });
au.start();
return { container, lifecycle, host, au, component, observerLocator };
}
export function tearDown(au: Aurelia, lifecycle: ILifecycle, host: HTMLElement) {
au.stop();
expect(lifecycle['flushCount']).to.equal(0);
host.remove();
}
const reg = /\s+/g;
export function trimFull(text: string) {
return text.replace(reg, '');
}
export { _, h, stringify, jsonStringify, htmlStringify, verifyEqual, padRight, massSpy, massStub, massReset, massRestore, ensureNotCalled, eachCartesianJoin, eachCartesianJoinFactory };
const eventCmds = { delegate: 1, capture: 1, call: 1 };
/**
* jsx with aurelia binding command friendly version of h
*/
export const hJsx = function(name: string, attrs: Record<string, string> | null, ...children: (Node | string)[]) {
const el = document.createElement(name === 'let$' ? 'let' : name);
if (attrs !== null) {
let value: string | string[];
let len: number;
for (const attr in attrs) {
value = attrs[attr];
// if attr is class or its alias
// split up by splace and add to element via classList API
if (attr === 'class' || attr === 'className' || attr === 'cls') {
value = value === undefined || value === null
? []
: Array.isArray(value)
? value
: ('' + value).split(' ');
el.classList.add(...value as string[]);
}
// for attributes with matching properties, simply assign
// other if special attribute like data, or ones start with _
// assign as well
else if (attr in el || attr === 'data' || attr[0] === '_') {
el[attr] = value;
}
// if it's an asElement attribute, camel case it
else if (attr === 'asElement') {
el.setAttribute('as-element', value);
}
// ortherwise do fallback check
else {
// is it an event handler?
if (attr[0] === 'o' && attr[1] === 'n' && attr[attr.length] !== '$') {
const decoded = PLATFORM.kebabCase(attr.slice(2));
const parts = decoded.split('-');
if (parts.length > 1) {
const lastPart = parts[parts.length - 1];
const cmd = eventCmds[lastPart] ? lastPart : 'trigger';
el.setAttribute(`${parts.slice(0, -1).join('-')}.${cmd}`, value);
} else {
el.setAttribute(`${parts[0]}.trigger`, value);
}
} else {
const len = attr.length;
const lastIdx = attr.lastIndexOf('$');
if (lastIdx === -1) {
el.setAttribute(PLATFORM.kebabCase(attr), value);
} else {
let cmd = attr.slice(lastIdx + 1);
cmd = cmd ? PLATFORM.kebabCase(cmd) : 'bind';
el.setAttribute(`${PLATFORM.kebabCase(attr.slice(0, lastIdx))}.${cmd}`, value);
}
}
}
}
}
const appender = (el instanceof HTMLTemplateElement) ? el.content : el;
for (const child of children) {
if (child === null || child === undefined) {
continue;
}
appender.appendChild(child instanceof Node ? child : document.createTextNode('' + child));
}
return el;
}