alloy 3.0.1 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/cla.yaml +1 -1
- package/Alloy/alloy.js +7 -4
- package/Alloy/builtins/social.js +1 -1
- package/Alloy/commands/compile/Orphanage.js +1 -2
- package/Alloy/commands/compile/README.md +1 -1
- package/Alloy/commands/compile/ast/builtins-plugin.js +1 -1
- package/Alloy/commands/compile/compilerUtils.js +24 -11
- package/Alloy/commands/compile/index.js +211 -62
- package/Alloy/commands/compile/parsers/Alloy.Abstract._BackboneClass.js +3 -2
- package/Alloy/commands/compile/parsers/Alloy.Require.js +10 -4
- package/Alloy/commands/compile/sourceMapper.js +0 -16
- package/Alloy/commands/compile/styler.js +101 -58
- package/Alloy/commands/db/index.js +76 -0
- package/Alloy/commands/generate/targets/jmk.js +1 -1
- package/Alloy/commands/generate/targets/style.js +1 -1
- package/Alloy/commands/generate/targets/widget.js +2 -2
- package/Alloy/commands/purgetss/index.js +5 -0
- package/Alloy/lib/alloy/sync/sql.js +10 -7
- package/Alloy/template/lib/alloy.js +7 -1
- package/Alloy/tiapp.js +18 -0
- package/CHANGELOG.md +6 -0
- package/README.md +4 -0
- package/package.json +1 -1
- package/test/specs/purgetss.js +74 -0
- package/test/specs/require.js +94 -0
- package/test/specs/styler.js +271 -0
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
var path = require('path'),
|
|
2
|
+
_ = require('lodash'),
|
|
3
|
+
U = require('../../Alloy/utils'),
|
|
4
|
+
styler = require('../../Alloy/commands/compile/styler');
|
|
5
|
+
|
|
6
|
+
var fixtureTss = path.join(__dirname, '..', 'apps', 'testing', 'multi-classes', 'styles', 'index.tss');
|
|
7
|
+
|
|
8
|
+
// return only the entries whose key matches
|
|
9
|
+
function byKey(sorted, key) {
|
|
10
|
+
return _.filter(sorted, { key: key });
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
describe('styler.js', function() {
|
|
14
|
+
// U.die() calls process.exit(), which would abort the whole test run.
|
|
15
|
+
// Stub it so an invalid selector surfaces as a failed spec instead.
|
|
16
|
+
var originalDie, died;
|
|
17
|
+
|
|
18
|
+
beforeEach(function() {
|
|
19
|
+
died = [];
|
|
20
|
+
originalDie = U.die;
|
|
21
|
+
U.die = function(msg) {
|
|
22
|
+
died.push(msg);
|
|
23
|
+
throw new Error(msg);
|
|
24
|
+
};
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
afterEach(function() {
|
|
28
|
+
U.die = originalDie;
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('sortStyles() with comma-separated selectors', function() {
|
|
32
|
+
|
|
33
|
+
it('creates one entry per class in ".a, .b"', function() {
|
|
34
|
+
var sorted = styler.sortStyles({ '.a, .b': { color: 'red' } });
|
|
35
|
+
expect(sorted.length).toBe(2);
|
|
36
|
+
|
|
37
|
+
var a = byKey(sorted, 'a')[0];
|
|
38
|
+
var b = byKey(sorted, 'b')[0];
|
|
39
|
+
expect(a).toBeDefined();
|
|
40
|
+
expect(b).toBeDefined();
|
|
41
|
+
expect(a.isClass).toBe(true);
|
|
42
|
+
expect(b.isClass).toBe(true);
|
|
43
|
+
expect(a.style).toEqual({ color: 'red' });
|
|
44
|
+
expect(b.style).toEqual({ color: 'red' });
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('creates one entry per id in "#x, #y"', function() {
|
|
48
|
+
var sorted = styler.sortStyles({ '#x, #y': { top: 1 } });
|
|
49
|
+
expect(sorted.length).toBe(2);
|
|
50
|
+
expect(byKey(sorted, 'x')[0].isId).toBe(true);
|
|
51
|
+
expect(byKey(sorted, 'y')[0].isId).toBe(true);
|
|
52
|
+
expect(byKey(sorted, 'x')[0].style).toEqual({ top: 1 });
|
|
53
|
+
expect(byKey(sorted, 'y')[0].style).toEqual({ top: 1 });
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('keeps the selector type of each part in a mixed key', function() {
|
|
57
|
+
var sorted = styler.sortStyles({ 'Label, .btn, #main': { width: 100 } });
|
|
58
|
+
expect(sorted.length).toBe(3);
|
|
59
|
+
|
|
60
|
+
var label = byKey(sorted, 'Label')[0];
|
|
61
|
+
var btn = byKey(sorted, 'btn')[0];
|
|
62
|
+
var main = byKey(sorted, 'main')[0];
|
|
63
|
+
|
|
64
|
+
expect(label.isApi).toBe(true);
|
|
65
|
+
expect(label.isClass).toBeUndefined();
|
|
66
|
+
expect(label.isId).toBeUndefined();
|
|
67
|
+
|
|
68
|
+
expect(btn.isClass).toBe(true);
|
|
69
|
+
expect(btn.isApi).toBeUndefined();
|
|
70
|
+
expect(btn.isId).toBeUndefined();
|
|
71
|
+
|
|
72
|
+
expect(main.isId).toBe(true);
|
|
73
|
+
expect(main.isApi).toBeUndefined();
|
|
74
|
+
expect(main.isClass).toBeUndefined();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('gives the API entry a lower priority than the class and id entries', function() {
|
|
78
|
+
var sorted = styler.sortStyles({ '#main, .btn, Label': { width: 100 } });
|
|
79
|
+
expect(sorted[0].key).toBe('Label');
|
|
80
|
+
expect(sorted[1].key).toBe('btn');
|
|
81
|
+
expect(sorted[2].key).toBe('main');
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('preserves declaration order between selectors of the same key', function() {
|
|
85
|
+
var sorted = styler.sortStyles({ '.first, .second, .third': { top: 0 } });
|
|
86
|
+
expect(_.map(sorted, 'key')).toEqual(['first', 'second', 'third']);
|
|
87
|
+
expect(sorted[0].priority < sorted[1].priority).toBe(true);
|
|
88
|
+
expect(sorted[1].priority < sorted[2].priority).toBe(true);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('does not split on commas inside a query bracket', function() {
|
|
92
|
+
var sorted = styler.sortStyles({ '.c[platform=ios,android]': { z: 1 } });
|
|
93
|
+
expect(sorted.length).toBe(1);
|
|
94
|
+
expect(sorted[0].key).toBe('c');
|
|
95
|
+
expect(sorted[0].queries).toEqual({ platform: ['ios', 'android'] });
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('splits selectors with queries and keeps the query on its own selector only', function() {
|
|
99
|
+
var sorted = styler.sortStyles({ '.x[platform=ios], .y': { z: 1 } });
|
|
100
|
+
expect(sorted.length).toBe(2);
|
|
101
|
+
|
|
102
|
+
var x = byKey(sorted, 'x')[0];
|
|
103
|
+
var y = byKey(sorted, 'y')[0];
|
|
104
|
+
expect(x.queries).toEqual({ platform: ['ios'] });
|
|
105
|
+
expect(y.queries).toBeUndefined();
|
|
106
|
+
expect(x.style).toEqual({ z: 1 });
|
|
107
|
+
expect(y.style).toEqual({ z: 1 });
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('handles a bracketed query with a comma next to a plain selector', function() {
|
|
111
|
+
var sorted = styler.sortStyles({ '.a[platform=ios,android], .b[formFactor=handheld]': { z: 1 } });
|
|
112
|
+
expect(sorted.length).toBe(2);
|
|
113
|
+
expect(byKey(sorted, 'a')[0].queries).toEqual({ platform: ['ios', 'android'] });
|
|
114
|
+
expect(byKey(sorted, 'b')[0].queries).toEqual({ formFactor: 'handheld' });
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('ignores a trailing comma', function() {
|
|
118
|
+
var sorted = styler.sortStyles({ '.a,': { q: 1 } });
|
|
119
|
+
expect(sorted.length).toBe(1);
|
|
120
|
+
expect(sorted[0].key).toBe('a');
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('ignores a trailing comma followed by whitespace', function() {
|
|
124
|
+
var sorted = styler.sortStyles({ '.a, ': { q: 1 } });
|
|
125
|
+
expect(sorted.length).toBe(1);
|
|
126
|
+
expect(sorted[0].key).toBe('a');
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('ignores empty chunks between commas', function() {
|
|
130
|
+
var sorted = styler.sortStyles({ '.a,,.b, , .c': { q: 1 } });
|
|
131
|
+
expect(_.map(sorted, 'key')).toEqual(['a', 'b', 'c']);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('trims whitespace around each selector', function() {
|
|
135
|
+
var sorted = styler.sortStyles({ ' .a ,\t.b\n, #c ': { q: 1 } });
|
|
136
|
+
expect(_.map(sorted, 'key')).toEqual(['a', 'b', 'c']);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('gives each selector from one key its own style object', function() {
|
|
140
|
+
var sorted = styler.sortStyles({ '.a, .b': { color: 'red', font: { fontSize: 12 } } });
|
|
141
|
+
var a = byKey(sorted, 'a')[0];
|
|
142
|
+
var b = byKey(sorted, 'b')[0];
|
|
143
|
+
|
|
144
|
+
expect(a.style).not.toBe(b.style);
|
|
145
|
+
expect(a.style.font).not.toBe(b.style.font);
|
|
146
|
+
|
|
147
|
+
a.style.color = 'blue';
|
|
148
|
+
a.style.font.fontSize = 20;
|
|
149
|
+
expect(b.style.color).toBe('red');
|
|
150
|
+
expect(b.style.font.fontSize).toBe(12);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('does not mutate the original style object when a key is split', function() {
|
|
154
|
+
var original = { '.a, .b': { color: 'red' } };
|
|
155
|
+
var sorted = styler.sortStyles(original);
|
|
156
|
+
sorted[0].style.color = 'blue';
|
|
157
|
+
expect(original['.a, .b'].color).toBe('red');
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('still reuses the style object for a single selector', function() {
|
|
161
|
+
var style = { color: 'red' };
|
|
162
|
+
var sorted = styler.sortStyles({ '.a': style });
|
|
163
|
+
expect(sorted.length).toBe(1);
|
|
164
|
+
expect(sorted[0].style).toBe(style);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('leaves single selectors untouched next to comma-separated keys', function() {
|
|
168
|
+
var sorted = styler.sortStyles({
|
|
169
|
+
'.a, .b': { color: 'red' },
|
|
170
|
+
'#x': { w: 1 },
|
|
171
|
+
'Label': { h: 2 }
|
|
172
|
+
});
|
|
173
|
+
expect(sorted.length).toBe(4);
|
|
174
|
+
expect(_.map(sorted, 'key')).toEqual(['Label', 'a', 'b', 'x']);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it('appends split entries to an existing style', function() {
|
|
178
|
+
var existing = styler.sortStyles({ 'Label': { h: 2 } });
|
|
179
|
+
var sorted = styler.sortStyles({ '.a, .b': { color: 'red' } }, { existingStyle: existing });
|
|
180
|
+
expect(sorted.length).toBe(3);
|
|
181
|
+
expect(_.map(sorted, 'key')).toEqual(['Label', 'a', 'b']);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('applies platform and theme priority bumps to every split entry', function() {
|
|
185
|
+
var plain = styler.sortStyles({ '.a, .b': { q: 1 } });
|
|
186
|
+
var bumped = styler.sortStyles({ '.a, .b': { q: 1 } }, { platform: 'ios', theme: 'dark' });
|
|
187
|
+
_.each(bumped, function(entry, i) {
|
|
188
|
+
// ORDER increments are tiny (0.0001 per entry), the platform+theme
|
|
189
|
+
// bump is 100.9, so a > comparison is enough here
|
|
190
|
+
expect(entry.priority - plain[i].priority > 100).toBe(true);
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
describe('invalid selectors', function() {
|
|
195
|
+
it('dies on an invalid part of a comma-separated key', function() {
|
|
196
|
+
expect(function() {
|
|
197
|
+
styler.sortStyles({ '.a, [broken': { q: 1 } });
|
|
198
|
+
}).toThrow();
|
|
199
|
+
expect(died.length).toBe(1);
|
|
200
|
+
expect(died[0]).toBe('Invalid style specifier "[broken"');
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('does not die on a whitespace-only part after a trailing comma', function() {
|
|
204
|
+
expect(function() {
|
|
205
|
+
styler.sortStyles({ '.a, ': { q: 1 } });
|
|
206
|
+
}).not.toThrow();
|
|
207
|
+
expect(died.length).toBe(0);
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
describe('loadStyle() + sortStyles() from a .tss file', function() {
|
|
213
|
+
var sorted;
|
|
214
|
+
|
|
215
|
+
it('loads the multi-classes fixture', function() {
|
|
216
|
+
var loaded = styler.loadStyle(fixtureTss);
|
|
217
|
+
expect(_.isObject(loaded)).toBe(true);
|
|
218
|
+
expect(loaded['#header, #footer']).toBeDefined();
|
|
219
|
+
expect(loaded['.heading, Button']).toBeDefined();
|
|
220
|
+
sorted = styler.sortStyles(loaded);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it('expands "#header, #footer" into two id entries', function() {
|
|
224
|
+
var header = byKey(sorted, 'header');
|
|
225
|
+
var footer = byKey(sorted, 'footer');
|
|
226
|
+
expect(header.length).toBe(1);
|
|
227
|
+
expect(footer.length).toBe(1);
|
|
228
|
+
expect(header[0].isId).toBe(true);
|
|
229
|
+
expect(footer[0].isId).toBe(true);
|
|
230
|
+
expect(header[0].style).toEqual(footer[0].style);
|
|
231
|
+
expect(header[0].style).not.toBe(footer[0].style);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it('expands ".heading, Button" into a class and an API entry', function() {
|
|
235
|
+
var heading = byKey(sorted, 'heading');
|
|
236
|
+
var button = byKey(sorted, 'Button');
|
|
237
|
+
expect(heading.length).toBe(1);
|
|
238
|
+
expect(button.length).toBe(1);
|
|
239
|
+
expect(heading[0].isClass).toBe(true);
|
|
240
|
+
expect(button[0].isApi).toBe(true);
|
|
241
|
+
expect(heading[0].style.top).toBe(10);
|
|
242
|
+
expect(button[0].style.top).toBe(10);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it('keeps ".button[platform=ios,android]" as a single entry', function() {
|
|
246
|
+
var buttons = byKey(sorted, 'button');
|
|
247
|
+
var withQuery = _.filter(buttons, function(b) { return b.queries; });
|
|
248
|
+
expect(withQuery.length).toBe(1);
|
|
249
|
+
expect(withQuery[0].queries).toEqual({ platform: ['ios', 'android'] });
|
|
250
|
+
expect(withQuery[0].style).toEqual({ width: 200 });
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it('applies the query only to ".muted" in ".muted[platform=ios], #secondary"', function() {
|
|
254
|
+
var muted = byKey(sorted, 'muted')[0];
|
|
255
|
+
var secondary = byKey(sorted, 'secondary')[0];
|
|
256
|
+
expect(muted.queries).toEqual({ platform: ['ios'] });
|
|
257
|
+
expect(secondary.queries).toBeUndefined();
|
|
258
|
+
expect(muted.style).toEqual({ opacity: 0.5 });
|
|
259
|
+
expect(secondary.style).toEqual({ opacity: 0.5 });
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
it('ignores the trailing comma in ".button,"', function() {
|
|
263
|
+
var plain = _.filter(byKey(sorted, 'button'), function(b) { return !b.queries; });
|
|
264
|
+
expect(plain.length).toBe(1);
|
|
265
|
+
expect(plain[0].style).toEqual({ height: 44 });
|
|
266
|
+
// no entry was created for an empty selector
|
|
267
|
+
expect(byKey(sorted, '').length).toBe(0);
|
|
268
|
+
expect(byKey(sorted, 'undefined').length).toBe(0);
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
});
|