-
Notifications
You must be signed in to change notification settings - Fork 0
/
basis-templates.js
9306 lines (9305 loc) · 329 KB
/
basis-templates.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// resources(30):
// [FunctionExpression] ../../src/basis/template/declaration/element/b-isolate.js -> o.js
// [FunctionExpression] ../../src/basis/devpanel.js -> 0.js
// [FunctionExpression] ../../src/basis/template.js -> 2.js
// [FunctionExpression] ../../src/basis/template/const.js -> 3.js
// [FunctionExpression] ../../src/basis/template/declaration.js -> 4.js
// [FunctionExpression] ../../src/basis/template/tokenize.js -> e.js
// [FunctionExpression] ../../src/basis/template/isolateCss.js -> f.js
// [FunctionExpression] ../../src/basis/template/declaration/utils.js -> g.js
// [FunctionExpression] ../../src/basis/template/declaration/refs.js -> h.js
// [FunctionExpression] ../../src/basis/template/declaration/ast.js -> i.js
// [FunctionExpression] ../../src/basis/template/declaration/style.js -> j.js
// [FunctionExpression] ../../src/basis/template/declaration/attr.js -> k.js
// [FunctionExpression] ../../src/basis/template/declaration/element/b-content.js -> l.js
// [FunctionExpression] ../../src/basis/template/declaration/element/b-define.js -> m.js
// [FunctionExpression] ../../src/basis/template/declaration/element/b-include.js -> n.js
// [FunctionExpression] templates.js -> 1.js
// [FunctionExpression] ../../src/basis/template/declaration/element/b-l10n.js -> p.js
// [FunctionExpression] ../../src/basis/template/declaration/element/b-style.js -> q.js
// [FunctionExpression] ../../src/basis/template/declaration/element/b-svg.js -> r.js
// [FunctionExpression] ../../src/basis/template/declaration/element/b-text.js -> s.js
// [FunctionExpression] ../../src/basis/template/store.js -> 5.js
// [FunctionExpression] ../../src/basis/template/theme.js -> 6.js
// [FunctionExpression] ../../src/basis/template/html.js -> 7.js
// [FunctionExpression] ../../src/basis/l10n.js -> 8.js
// [FunctionExpression] ../../src/basis/event.js -> 9.js
// [FunctionExpression] ../../src/basis/utils/json-parser.js -> t.js
// [FunctionExpression] ../../src/basis/template/htmlfgen.js -> a.js
// [FunctionExpression] ../../src/basis/template/namespace.js -> b.js
// [FunctionExpression] ../../src/basis/template/buildDom.js -> c.js
// [FunctionExpression] ../../src/basis/dom/event.js -> d.js
//
// filelist(1):
// /scripts/release-configs/templates.js
//
(function(){
"use strict";
var __namespace_map__ = {"0.js":"basis.devpanel","1.js":"templates","2.js":"basis.template","3.js":"basis.template.const","4.js":"basis.template.declaration","5.js":"basis.template.store","6.js":"basis.template.theme","7.js":"basis.template.html","8.js":"basis.l10n","9.js":"basis.event","a.js":"basis.template.htmlfgen","b.js":"basis.template.namespace","c.js":"basis.template.buildDom","d.js":"basis.dom.event"};
var templates;
var __resources__ = {
"o.js": function (exports, module, basis, global, __filename, __dirname, require, resource, asset) {
var utils = basis.require("./g.js");
var getTokenAttrValues = utils.getTokenAttrValues;
module.exports = function (template, options, token) {
if (template.isolate) {
utils.addTemplateWarn(template, options, "<b:isolate> is already set to `" + template.isolate + "`", token.loc);
return;
}
template.isolate = getTokenAttrValues(token).prefix || options.isolate || options.genIsolateMarker();
};
},
"0.js": function (exports, module, basis, global, __filename, __dirname, require, resource, asset) {
if (basis.filename_) {
basis.createSandbox({
inspect: basis,
devInfoResolver: basis.config.devInfoResolver,
modules: {
api: { path: basis.path.resolve(__dirname, "../devpanel/api/index.js") },
type: {
path: basis.path.resolve(__dirname, "../devpanel/type/"),
filename: "index.js"
},
devpanel: {
autoload: true,
path: basis.path.resolve(__dirname, "../devpanel/"),
filename: "index.js"
}
}
});
}
},
"2.js": function (exports, module, basis, global, __filename, __dirname, require, resource, asset) {
var namespace = "basis.template";
var document = global.document;
var Class = basis.Class;
var cleaner = basis.cleaner;
var path = basis.path;
var consts = basis.require("./3.js");
var DECLARATION_VERSION = basis.require("./4.js").VERSION;
var getDeclFromSource = basis.require("./4.js").getDeclFromSource;
var makeDeclaration = basis.require("./4.js").makeDeclaration;
var setIsolatePrefixGenerator = basis.require("./4.js").setIsolatePrefixGenerator;
var store = basis.require("./5.js");
var theme = basis.require("./6.js");
var getSourceByPath = theme.get;
var templateList = [];
var sourceByDocumentId = {};
function resolveSourceByDocumentId(sourceId) {
var resource = sourceByDocumentId[sourceId];
if (!resource) {
var host = document.getElementById(sourceId);
var source = "";
if (host && host.tagName == "SCRIPT" && host.type == "text/basis-template")
source = host.textContent || host.text;
else if (!host)
basis.dev.warn("Template script element with id `" + sourceId + "` not found");
else
basis.dev.warn("Template should be declared in <script type=\"text/basis-template\"> element (id `" + sourceId + "`)");
resource = sourceByDocumentId[sourceId] = basis.resource.virtual("tmpl", source || "");
resource.id = sourceId;
resource.url = "<script id=\"" + sourceId + "\"/>";
}
return resource;
}
function resolveResource(ref, baseURI) {
if (/^#\d+$/.test(ref))
return templateList[ref.substr(1)];
if (/^id:/.test(ref))
return resolveSourceByDocumentId(ref.substr(3));
if (/^[a-z0-9\.]+$/i.test(ref) && !/\.tmpl$/.test(ref))
return getSourceByPath(ref);
return basis.resource(basis.resource.resolveURI(ref, baseURI, "<b:include src=\"{url}\"/>"));
}
function templateSourceUpdate() {
if (this.destroyBuilder)
buildTemplate.call(this);
var cursor = this;
while (cursor = cursor.attaches_)
cursor.handler.call(cursor.context);
}
function buildTemplate() {
var declaration = getDeclFromSource(this.source, this.baseURI, false, { isolate: this.getIsolatePrefix() });
var destroyBuilder = this.destroyBuilder;
var instances = {};
var funcs = this.builder(declaration.tokens, instances);
this.createInstance = funcs.createInstance;
this.clearInstance = funcs.destroyInstance;
this.destroyBuilder = funcs.destroy;
store.add(this.templateId, this, instances);
this.instances_ = instances;
this.decl_ = declaration;
var newDeps = declaration.deps;
var oldDeps = this.deps_;
this.deps_ = newDeps;
if (oldDeps)
for (var i = 0, dep; dep = oldDeps[i]; i++)
dep.bindingBridge.detach(dep, templateSourceUpdate, this);
if (newDeps)
for (var i = 0, dep; dep = newDeps[i]; i++)
dep.bindingBridge.attach(dep, templateSourceUpdate, this);
var newResources = declaration.resources;
var oldResources = this.resources;
this.resources = newResources;
if (newResources)
for (var i = 0, item; item = newResources[i]; i++) {
var resource = basis.resource(item.url).fetch();
if (typeof resource.startUse == "function")
resource.startUse();
}
if (oldResources)
for (var i = 0, item; item = oldResources[i]; i++) {
var resource = basis.resource(item.url).fetch();
if (typeof resource.stopUse == "function")
resource.stopUse();
}
if (destroyBuilder)
destroyBuilder(true);
}
var Template = Class(null, {
className: namespace + ".Template",
__extend__: function (value) {
if (value instanceof Template)
return value;
if (value instanceof TemplateSwitchConfig)
return new TemplateSwitcher(value);
return new Template(value);
},
source: "",
baseURI: "",
url: "",
attaches_: null,
init: function (source) {
if (templateList.length == 4096)
throw "Too many templates (maximum 4096)";
this.setSource(source || "");
this.templateId = templateList.push(this) - 1;
},
bindingBridge: {
attach: function (template, handler, context) {
var cursor = template;
while (cursor = cursor.attaches_)
if (cursor.handler === handler && cursor.context === context)
basis.dev.warn("basis.template.Template#bindingBridge.attach: duplicate handler & context pair");
template.attaches_ = {
handler: handler,
context: context,
attaches_: template.attaches_
};
},
detach: function (template, handler, context) {
var cursor = template;
var prev;
while (prev = cursor, cursor = cursor.attaches_)
if (cursor.handler === handler && cursor.context === context) {
prev.attaches_ = cursor.attaches_;
return;
}
basis.dev.warn("basis.template.Template#bindingBridge.detach: handler & context pair not found, nothing was removed");
},
get: function (template) {
var source = template.source;
return source && source.bindingBridge ? source.bindingBridge.get(source) : source;
}
},
createInstance: function (object, actionCallback, updateCallback, bindings, bindingInterface) {
buildTemplate.call(this);
return this.createInstance(object, actionCallback, updateCallback, bindings, bindingInterface);
},
clearInstance: function () {
},
getIsolatePrefix: function () {
return "i" + this.templateId + "__";
},
setSource: function (source) {
var oldSource = this.source;
if (oldSource != source) {
if (typeof source == "string") {
var m = source.match(/^([a-z]+):/);
if (m) {
source = source.substr(m[0].length);
switch (m[1]) {
case "id":
source = resolveSourceByDocumentId(source);
break;
case "path":
source = getSourceByPath(source);
break;
default:
basis.dev.warn(namespace + ".Template.setSource: Unknown prefix " + m[1] + " for template source was ingnored.");
}
}
}
if (oldSource && oldSource.bindingBridge) {
this.url = "";
this.baseURI = "";
oldSource.bindingBridge.detach(oldSource, templateSourceUpdate, this);
}
if (source && source.bindingBridge) {
if (source.url) {
this.url = source.url;
this.baseURI = path.dirname(source.url) + "/";
}
source.bindingBridge.attach(source, templateSourceUpdate, this);
}
this.source = source;
templateSourceUpdate.call(this);
}
},
destroy: function () {
if (this.destroyBuilder) {
store.remove(this.templateId);
this.destroyBuilder();
}
this.attaches_ = null;
this.createInstance = null;
this.resources = null;
this.source = null;
this.instances_ = null;
this.decl_ = null;
}
});
var TemplateSwitchConfig = function (config) {
basis.object.extend(this, config);
};
var TemplateSwitcher = basis.Class(null, {
className: namespace + ".TemplateSwitcher",
ruleRet_: null,
templates_: null,
templateClass: Template,
ruleEvents: null,
rule: String,
init: function (config) {
this.ruleRet_ = [];
this.templates_ = [];
this.rule = config.rule;
var events = config.events;
if (events && events.length) {
this.ruleEvents = {};
for (var i = 0, eventName; eventName = events[i]; i++)
this.ruleEvents[eventName] = true;
}
cleaner.add(this);
},
resolve: function (object) {
var ret = this.rule(object);
var idx = this.ruleRet_.indexOf(ret);
if (idx == -1) {
this.ruleRet_.push(ret);
idx = this.templates_.push(new this.templateClass(ret)) - 1;
}
return this.templates_[idx];
},
destroy: function () {
this.rule = null;
this.templates_ = null;
this.ruleRet_ = null;
}
});
function switcher(events, rule) {
if (!rule) {
rule = events;
events = null;
}
if (typeof events == "string")
events = events.split(/\s+/);
return new TemplateSwitchConfig({
rule: rule,
events: events
});
}
cleaner.add({
destroy: function () {
for (var i = 0, template; template = templateList[i]; i++)
template.destroy();
templateList = null;
}
});
module.exports = {
DECLARATION_VERSION: DECLARATION_VERSION,
TYPE_ELEMENT: consts.TYPE_ELEMENT,
TYPE_ATTRIBUTE: consts.TYPE_ATTRIBUTE,
TYPE_ATTRIBUTE_CLASS: consts.TYPE_ATTRIBUTE_CLASS,
TYPE_ATTRIBUTE_STYLE: consts.TYPE_ATTRIBUTE_STYLE,
TYPE_ATTRIBUTE_EVENT: consts.TYPE_ATTRIBUTE_EVENT,
TYPE_TEXT: consts.TYPE_TEXT,
TYPE_COMMENT: consts.TYPE_COMMENT,
TYPE_CONTENT: consts.TYPE_CONTENT,
TOKEN_TYPE: consts.TOKEN_TYPE,
TOKEN_BINDINGS: consts.TOKEN_BINDINGS,
TOKEN_REFS: consts.TOKEN_REFS,
ATTR_NAME: consts.ATTR_NAME,
ATTR_VALUE: consts.ATTR_VALUE,
ATTR_NAME_BY_TYPE: consts.ATTR_NAME_BY_TYPE,
CLASS_BINDING_ENUM: consts.CLASS_BINDING_ENUM,
CLASS_BINDING_BOOL: consts.CLASS_BINDING_BOOL,
CLASS_BINDING_INVERT: consts.CLASS_BINDING_INVERT,
ELEMENT_NAME: consts.ELEMENT_NAME,
ELEMENT_ATTRS: consts.ELEMENT_ATTRIBUTES_AND_CHILDREN,
ELEMENT_ATTRIBUTES_AND_CHILDREN: consts.ELEMENT_ATTRIBUTES_AND_CHILDREN,
TEXT_VALUE: consts.TEXT_VALUE,
COMMENT_VALUE: consts.COMMENT_VALUE,
CONTENT_CHILDREN: consts.CONTENT_CHILDREN,
CONTENT_PRIORITY: consts.CONTENT_PRIORITY,
TemplateSwitchConfig: TemplateSwitchConfig,
TemplateSwitcher: TemplateSwitcher,
Template: Template,
switcher: switcher,
getDeclFromSource: getDeclFromSource,
makeDeclaration: makeDeclaration,
resolveResource: resolveResource,
setIsolatePrefixGenerator: setIsolatePrefixGenerator,
getDebugInfoById: store.getDebugInfoById,
getTemplateCount: function () {
return templateList.length;
},
resolveTemplateInfoByNode: function (node) {
while (node) {
if (node[consts.MARKER])
return store.resolveInfoById(node[consts.MARKER]);
node = node.parentNode;
}
return null;
},
resolveInfoById: store.resolveInfoById,
resolveTemplateById: store.resolveTemplateById,
resolveObjectById: store.resolveObjectById,
resolveTmplById: store.resolveTmplById,
SourceWrapper: theme.SourceWrapper,
Theme: theme.Theme,
theme: theme.theme,
getThemeList: theme.getThemeList,
currentTheme: theme.currentTheme,
setTheme: theme.setTheme,
onThemeChange: theme.onThemeChange,
define: theme.define,
get: theme.get,
getPathList: theme.getPathList
};
},
"3.js": function (exports, module, basis, global, __filename, __dirname, require, resource, asset) {
var MARKER = "basisTemplateId_" + basis.genUID();
var TYPE_ELEMENT = 1;
var TYPE_ATTRIBUTE = 2;
var TYPE_ATTRIBUTE_CLASS = 4;
var TYPE_ATTRIBUTE_STYLE = 5;
var TYPE_ATTRIBUTE_EVENT = 6;
var TYPE_TEXT = 3;
var TYPE_COMMENT = 8;
var TYPE_CONTENT = 9;
var TOKEN_TYPE = 0;
var TOKEN_BINDINGS = 1;
var TOKEN_REFS = 2;
var ATTR_NAME = 3;
var ATTR_VALUE = 4;
var ATTR_NAME_BY_TYPE = {
4: "class",
5: "style"
};
var ATTR_TYPE_BY_NAME = {
"class": TYPE_ATTRIBUTE_CLASS,
"style": TYPE_ATTRIBUTE_STYLE
};
var ATTR_VALUE_INDEX = {
2: ATTR_VALUE,
4: ATTR_VALUE - 1,
5: ATTR_VALUE - 1,
6: 2
};
var CLASS_BINDING_ENUM = 1;
var CLASS_BINDING_BOOL = 2;
var CLASS_BINDING_INVERT = 3;
var CLASS_BINDING_EQUAL = 4;
var CLASS_BINDING_NOTEQUAL = 5;
var ELEMENT_NAME = 3;
var ELEMENT_ATTRIBUTES_AND_CHILDREN = 4;
var TEXT_VALUE = 3;
var COMMENT_VALUE = 3;
var CONTENT_CHILDREN = 2;
var CONTENT_PRIORITY = 1;
var document = global.document;
var CLONE_NORMALIZATION_TEXT_BUG = !document ? true : function () {
var element = document.createElement("div");
element.appendChild(document.createTextNode("a"));
element.appendChild(document.createTextNode("a"));
return element.cloneNode(true).childNodes.length == 1;
}();
module.exports = {
MARKER: MARKER,
TYPE_ELEMENT: TYPE_ELEMENT,
TYPE_ATTRIBUTE: TYPE_ATTRIBUTE,
TYPE_ATTRIBUTE_CLASS: TYPE_ATTRIBUTE_CLASS,
TYPE_ATTRIBUTE_STYLE: TYPE_ATTRIBUTE_STYLE,
TYPE_ATTRIBUTE_EVENT: TYPE_ATTRIBUTE_EVENT,
TYPE_TEXT: TYPE_TEXT,
TYPE_COMMENT: TYPE_COMMENT,
TYPE_CONTENT: TYPE_CONTENT,
TOKEN_TYPE: TOKEN_TYPE,
TOKEN_BINDINGS: TOKEN_BINDINGS,
TOKEN_REFS: TOKEN_REFS,
ATTR_NAME: ATTR_NAME,
ATTR_VALUE: ATTR_VALUE,
ATTR_NAME_BY_TYPE: ATTR_NAME_BY_TYPE,
ATTR_TYPE_BY_NAME: ATTR_TYPE_BY_NAME,
ATTR_VALUE_INDEX: ATTR_VALUE_INDEX,
ELEMENT_NAME: ELEMENT_NAME,
ELEMENT_ATTRIBUTES_AND_CHILDREN: ELEMENT_ATTRIBUTES_AND_CHILDREN,
TEXT_VALUE: TEXT_VALUE,
COMMENT_VALUE: COMMENT_VALUE,
CONTENT_CHILDREN: CONTENT_CHILDREN,
CONTENT_PRIORITY: CONTENT_PRIORITY,
CLASS_BINDING_ENUM: CLASS_BINDING_ENUM,
CLASS_BINDING_BOOL: CLASS_BINDING_BOOL,
CLASS_BINDING_INVERT: CLASS_BINDING_INVERT,
CLASS_BINDING_EQUAL: CLASS_BINDING_EQUAL,
CLASS_BINDING_NOTEQUAL: CLASS_BINDING_NOTEQUAL,
CLONE_NORMALIZATION_TEXT_BUG: CLONE_NORMALIZATION_TEXT_BUG
};
},
"4.js": function (exports, module, basis, global, __filename, __dirname, require, resource, asset) {
var hasOwnProperty = Object.prototype.hasOwnProperty;
var arraySearch = basis.array.search;
var arrayAdd = basis.array.add;
var tokenize = basis.require("./e.js");
var isolateCss = basis.require("./f.js");
var consts = basis.require("./3.js");
var utils = basis.require("./g.js");
var refUtils = basis.require("./h.js");
var styleUtils = basis.require("./j.js");
var attrUtils = basis.require("./k.js");
var walk = basis.require("./i.js").walk;
var TYPE_ELEMENT = consts.TYPE_ELEMENT;
var TYPE_ATTRIBUTE = consts.TYPE_ATTRIBUTE;
var TYPE_ATTRIBUTE_CLASS = consts.TYPE_ATTRIBUTE_CLASS;
var TYPE_TEXT = consts.TYPE_TEXT;
var TYPE_COMMENT = consts.TYPE_COMMENT;
var TYPE_CONTENT = consts.TYPE_CONTENT;
var TOKEN_TYPE = consts.TOKEN_TYPE;
var TOKEN_BINDINGS = consts.TOKEN_BINDINGS;
var TOKEN_REFS = consts.TOKEN_REFS;
var ATTR_VALUE_INDEX = consts.ATTR_VALUE_INDEX;
var ELEMENT_ATTRIBUTES_AND_CHILDREN = consts.ELEMENT_ATTRIBUTES_AND_CHILDREN;
var TEXT_VALUE = consts.TEXT_VALUE;
var CONTENT_PRIORITY = consts.CONTENT_PRIORITY;
var CONTENT_CHILDREN = consts.CONTENT_CHILDREN;
var resourceHash = utils.resourceHash;
var getTokenName = utils.getTokenName;
var bindingList = utils.bindingList;
var refList = refUtils.refList;
var addTokenRef = refUtils.addTokenRef;
var normalizeRefs = refUtils.normalizeRefs;
var applyAttrs = attrUtils.applyAttrs;
var styleNamespaceIsolate = styleUtils.styleNamespaceIsolate;
var isolateTokens = styleUtils.isolateTokens;
var topLevelInstructions = [
"define",
"isolate",
"l10n",
"style"
];
var humanTopLevelInstrcutionList = topLevelInstructions.map(function (name) {
return "<b:" + name + ">";
}).join(", ").replace(/, (<b:[a-z]+>)$/, " and $1");
var elementHandlers = {
content: basis.require("./l.js"),
define: basis.require("./m.js"),
include: basis.require("./n.js"),
isolate: basis.require("./o.js"),
l10n: basis.require("./p.js"),
style: basis.require("./q.js"),
svg: basis.require("./r.js"),
text: basis.require("./s.js")
};
var Template = function () {
};
var resolveResource = function () {
};
var genIsolateMarker = function () {
return basis.genUID() + "__";
};
var makeDeclaration = function () {
var includeStack = [];
var styleNamespaceResource = {};
function process(tokens, template, options) {
var result = [];
for (var i = 0, token, item; token = tokens[i]; i++) {
var refs = refList(token);
var bindings = bindingList(token);
switch (token.type) {
case TYPE_ELEMENT:
if (token.prefix == "b") {
if (topLevelInstructions.indexOf(token.name) === -1)
options.allowTopInstruction = false;
else if (!options.allowTopInstruction)
utils.addTemplateWarn(template, options, "Instruction tag <b:" + token.name + "> should be placed in the beginning of template before any markup or instructions other than " + humanTopLevelInstrcutionList + ". Currently it may works but will be ignored in future.", token.loc);
if (!elementHandlers.hasOwnProperty(token.name)) {
utils.addTemplateWarn(template, options, "Unknown instruction tag: <b:" + token.name + ">", token.loc);
continue;
}
elementHandlers[token.name](template, options, token, result);
continue;
}
options.allowTopInstruction = false;
item = [
1,
bindings,
refs,
getTokenName(token)
];
applyAttrs(template, options, item, token.attrs);
item.push.apply(item, process(token.children, template, options));
break;
case TYPE_TEXT:
if (refs && refs.length == 2 && arraySearch(refs, "element"))
bindings = refs[+!refs.lastSearchIndex];
item = [
3,
bindings,
refs
];
if (!refs || token.value != "{" + refs.join("|") + "}")
item.push(token.value);
break;
case TYPE_COMMENT:
if (options.optimizeSize && !bindings && !refs)
continue;
item = [
8,
bindings,
refs
];
if (!options.optimizeSize)
if (!refs || token.value != "{" + refs.join("|") + "}")
item.push(token.value);
break;
default:
utils.addTemplateWarn(template, options, "Unknown token type: " + token.type, token);
continue;
}
options.allowTopInstruction = false;
utils.addTokenLocation(template, options, item, token);
item.sourceToken = token;
result.push(item);
}
return result;
}
function absl10n(value, dictURI, l10nMap) {
if (typeof value == "string") {
var parts = value.split(":");
var key = parts[1];
if (key && parts[0] == "l10n") {
if (parts.length == 2 && key.indexOf("@") == -1) {
if (!dictURI)
return false;
key = key + "@" + dictURI;
value = "l10n:" + key;
}
arrayAdd(l10nMap, key);
}
}
return value;
}
function applyDefines(ast, template, options) {
walk(ast, function (nodeType, node) {
var bindings = node[TOKEN_BINDINGS];
switch (nodeType) {
case TYPE_ELEMENT:
applyDefines(node, template, options, ELEMENT_ATTRIBUTES_AND_CHILDREN);
break;
case TYPE_TEXT:
if (bindings) {
var binding = absl10n(bindings, options.dictURI, template.l10n);
node[TOKEN_BINDINGS] = binding || 0;
if (binding === false) {
utils.addTemplateWarn(template, options, "Dictionary for l10n binding on text node can't be resolved: {" + bindings + "}", node.loc);
node[TEXT_VALUE] = "{" + bindings + "}";
}
}
break;
case TYPE_ATTRIBUTE:
if (bindings) {
var array = bindings[0];
for (var j = array.length - 1; j >= 0; j--) {
var binding = absl10n(array[j], options.dictURI, template.l10n);
if (binding === false) {
utils.addTemplateWarn(template, options, "Dictionary for l10n binding on attribute can't be resolved: {" + array[j] + "}", node.loc);
var expr = bindings[1];
for (var k = 0; k < expr.length; k++)
if (typeof expr[k] == "number") {
if (expr[k] == j)
expr[k] = "{" + array[j] + "}";
else if (expr[k] > j)
expr[k] = expr[k] - 1;
}
array.splice(j, 1);
if (!array.length)
node[TOKEN_BINDINGS] = 0;
} else
array[j] = binding;
}
}
break;
case TYPE_ATTRIBUTE_CLASS:
if (bindings) {
for (var k = 0, bind; bind = bindings[k]; k++) {
if (bind.length > 2)
continue;
utils.addTokenLocation(template, options, bind, bind.info_);
var bindNameParts = bind[1].split(":");
var bindName = bindNameParts.pop();
var bindPrefix = bindNameParts.pop() || "";
if (hasOwnProperty.call(options.defines, bindName)) {
var define = options.defines[bindName];
bind[1] = (bindPrefix ? bindPrefix + ":" : "") + define[0];
bind.push.apply(bind, define.slice(1));
define.used = true;
} else {
bind.push(0);
utils.addTemplateWarn(template, options, "Unpredictable class binding: " + bind[0] + "{" + bind[1] + "}", bind.loc);
}
}
if (options.optimizeSize) {
var valueIdx = ATTR_VALUE_INDEX[nodeType];
if (!node[valueIdx])
node.length = valueIdx;
}
}
break;
}
});
}
function findElementCandidateNode(ast) {
function find(node, offset) {
for (var i = offset; i < node.length; i++) {
var child = node[i];
var type = child[TOKEN_TYPE];
var result;
if (type == TYPE_ELEMENT || type == TYPE_TEXT)
return child;
if (type == TYPE_COMMENT)
if (child[TOKEN_REFS] || child[TOKEN_BINDINGS])
return child;
if (type == TYPE_CONTENT) {
result = find(child, CONTENT_CHILDREN);
if (result)
return result;
}
}
return null;
}
return find(ast, 0);
}
return function makeDeclaration(source, baseURI, options, sourceUrl, sourceOrigin) {
var source_ = source;
options = basis.object.slice(options);
options.Template = Template;
options.genIsolateMarker = genIsolateMarker;
options.resolveResource = resolveResource;
options.getDeclFromSource = getDeclFromSource;
options.makeDeclaration = makeDeclaration;
options.process = process;
options.includeStack = includeStack;
options.includeOptions = options.includeOptions || {};
options.templates = {};
options.defines = {};
options.dictURI = sourceUrl ? basis.path.resolve(sourceUrl) : baseURI || "";
options.allowTopInstruction = true;
options.styleNSIsolateMap = {};
options.loc = true;
options.range = true;
if (options.dictURI) {
var extname = basis.path.extname(options.dictURI);
if (extname && extname != ".l10n")
options.dictURI = options.dictURI.substr(0, options.dictURI.length - extname.length) + ".l10n";
}
var result = {
sourceUrl: sourceUrl,
baseURI: baseURI || "",
tokens: null,
includes: [],
deps: [],
templates: {},
isolate: false,
styleNSPrefix: {},
resources: [],
l10n: [],
warns: []
};
result.removals = [];
result.states = {};
if (!source)
source = "";
if (!Array.isArray(source))
source = tokenize(String(source || ""), {
loc: !!options.loc,
range: !!options.range
});
if (source.warns)
source.warns.forEach(function (warn) {
utils.addTemplateWarn(result, options, warn[0], warn[1].loc);
});
includeStack.push(sourceOrigin !== true && sourceOrigin || {});
result.tokens = process(source, result, options);
includeStack.pop();
result.tokens.source_ = source_ !== undefined ? source_ : source && source.source_;
result.tokens.push([
TYPE_CONTENT,
0
]);
var tokenRefMap = normalizeRefs(result.tokens);
var elementCandidateNode = findElementCandidateNode(result.tokens);
var contentNodeRef = tokenRefMap[":content"];
if (contentNodeRef.node[CONTENT_PRIORITY] > 1)
contentNodeRef.node[CONTENT_PRIORITY] = 1;
contentNodeRef.overrided.forEach(function (overridedContentNodeRef) {
var nodeIndex = overridedContentNodeRef.parent.indexOf(overridedContentNodeRef.node);
if (nodeIndex != -1)
overridedContentNodeRef.parent.splice.apply(overridedContentNodeRef.parent, [
nodeIndex,
1
].concat(overridedContentNodeRef.node.slice(CONTENT_CHILDREN)));
if (overridedContentNodeRef.node[consts.CONTENT_PRIORITY] > 0)
result.removals.push({
reason: "<b:content/>",
removeToken: contentNodeRef.node,
includeToken: overridedContentNodeRef.node.includeToken,
token: overridedContentNodeRef.node,
node: overridedContentNodeRef.node
});
});
if (!elementCandidateNode) {
elementCandidateNode = [
TYPE_TEXT,
0,
0
];
result.tokens.unshift(elementCandidateNode);
}
if (!tokenRefMap.element)
addTokenRef(elementCandidateNode, "element");
applyDefines(result.tokens, result, options);
if (/^[^a-z_-]/i.test(result.isolate))
basis.dev.error("basis.template: isolation prefix `" + result.isolate + "` should not starts with symbol other than letter, underscore or dash, otherwise it leads to incorrect css class names and broken styles");
if (includeStack.length == 0) {
isolateTokens(result.tokens, result.isolate || "", result, options);
if (result.removals) {
var warns = result.warns;
result.warns = [];
result.removals.forEach(function (item) {
isolateTokens([item.token], result.isolate || "", result, options);
});
result.warns = warns;
}
for (var key in result.styleNSPrefix) {
var styleNSPrefix = result.styleNSPrefix[key];
if (!styleNSPrefix.used)
utils.addTemplateWarn(result, options, "Unused namespace: " + styleNSPrefix.name, styleNSPrefix.loc);
}
if (result.isolate)
for (var i = 0, item; item = result.resources[i]; i++)
if (item.type == "style" && item.isolate !== styleNamespaceIsolate)
item.isolate = result.isolate + item.isolate;
var originalResources = result.resources;
result.resources = result.resources.filter(function (item, idx, array) {
return item.url && !basis.array.search(array, resourceHash(item), resourceHash, idx + 1);
}).map(function (item) {
if (item.type != "style") {
return {
type: item.type,
url: item.url
};
}
var url = item.url;
var isolate = item.isolate;
var namespaceIsolate = isolate === styleNamespaceIsolate;
var cssMap;
if (namespaceIsolate) {
isolate = styleNamespaceIsolate[url];
if (url in styleNamespaceResource) {
item.url = styleNamespaceResource[url].url;
return {
type: "style",
url: styleNamespaceResource[url].url
};
}
}
if (!isolate) {
item.url = url;
return {
type: "style",
url: url
};
}
var resource = basis.resource.virtual("css", "").ready(function (cssResource) {
cssResource.url = url + "?isolate-prefix=" + isolate;
cssResource.baseURI = basis.path.dirname(url) + "/";
cssResource.map = cssMap;
sourceResource();
});
var sourceResource = basis.resource(url).ready(function (cssResource) {
var isolated = isolateCss(cssResource && cssResource.cssText || "", isolate, true);
if (typeof global.btoa == "function")
isolated.css += "\n/*# sourceMappingURL=data:application/json;base64," + global.btoa("{\"version\":3,\"sources\":[\"" + basis.path.origin + url + "\"]," + "\"mappings\":\"AAAA" + basis.string.repeat(";AACA", isolated.css.split("\n").length) + "\"}") + " */";
cssMap = isolated.map;
resource.update(isolated.css);
});
if (namespaceIsolate)
styleNamespaceResource[url] = resource;
item.url = resource.url;
return {
type: "style",
url: resource.url
};
});
result.styles = originalResources.map(function (item) {
var sourceUrl = item.url || utils.getTokenAttrValues(item.token).src;
return {
resource: item.url || false,
sourceUrl: sourceUrl ? basis.resource.resolveURI(sourceUrl, baseURI) : null,
isolate: item.isolate === styleNamespaceIsolate ? styleNamespaceIsolate[item.url] : item.isolate || false,
namespace: item.namespace || false,
inline: item.inline,
styleToken: item.token,
includeToken: item.includeToken
};
});
}
for (var key in options.defines) {
var define = options.defines[key];
if (!define.used)
utils.addTemplateWarn(result, options, "Unused define: " + key, define.loc);
}
if (!result.warns.length)
result.warns = false;
return result;
};
}();
function cloneDecl(array) {
var result = [];
if (array.source_)
result.source_ = array.source_;
for (var i = 0; i < array.length; i++)
result.push(Array.isArray(array[i]) ? cloneDecl(array[i]) : array[i]);
return result;
}
function getDeclFromSource(source, baseURI, clone, options) {
var result = source;
var sourceUrl;
if (source.bindingBridge) {
baseURI = "baseURI" in source ? source.baseURI : "url" in source ? basis.path.dirname(source.url) : baseURI;
sourceUrl = "url" in source ? source.url : sourceUrl;
result = source.bindingBridge.get(source);
}
if (Array.isArray(result)) {
if (clone)
result = cloneDecl(result);
result = { tokens: result };
} else {
if (result && (typeof result != "object" || !Array.isArray(result.tokens)))
result = String(result);
}
if (!result || typeof result == "string")
result = makeDeclaration(result, baseURI, options, sourceUrl, source);
return result;
}
basis.resource("./2.js").ready(function (exports) {
resolveResource = exports.resolveResource;
Template = exports.Template;
});
module.exports = {
VERSION: 3,
makeDeclaration: makeDeclaration,
walk: walk,
getDeclFromSource: getDeclFromSource,
setIsolatePrefixGenerator: function (fn) {
genIsolateMarker = fn;
}
};
},
"e.js": function (exports, module, basis, global, __filename, __dirname, require, resource, asset) {
var consts = basis.require("./3.js");
var TYPE_ELEMENT = consts.TYPE_ELEMENT;
var TYPE_ATTRIBUTE = consts.TYPE_ATTRIBUTE;
var TYPE_TEXT = consts.TYPE_TEXT;
var TYPE_COMMENT = consts.TYPE_COMMENT;
var ATTR_TYPE_BY_NAME = consts.ATTR_TYPE_BY_NAME;
var SYNTAX_ERROR = "Invalid or unsupported syntax";
var TEXT = /((?:.|[\r\n])*?)(\{(?:l10n:([a-zA-Z_][a-zA-Z0-9_\-]*(?:\.[a-zA-Z_][a-zA-Z0-9_\-]*)*(?:\.\{[a-zA-Z_][a-zA-Z0-9_\-]*\})?)\})?|<(\/|!--(\s*\{)?)?|$)/g;
var TAG_NAME = /([a-z_][a-z0-9\-_]*)(:|\{|\s*(\/?>)?)/ig;
var ATTRIBUTE_NAME_OR_END = /([a-z_][a-z0-9_\-]*)(:|\{|=|\s*)|(\/?>)/ig;
var COMMENT = /(.|[\r\n])*?-->/g;
var CLOSE_TAG = /([a-z_][a-z0-9_\-]*(?::[a-z_][a-z0-9_\-]*)?)>/ig;
var REFERENCE = /([a-z_][a-z0-9_]*)(\||\}\s*)/ig;
var ATTRIBUTE_VALUE = /"((?:(\\")|[^"])*?)"\s*/g;
var QUOTE_UNESCAPE = /\\"/g;
var BREAK_TAG_PARSE = /^/g;
var SINGLETON_TAG = /^(area|base|br|col|command|embed|hr|img|input|link|meta|param|source)$/i;
var TAG_IGNORE_CONTENT = {
text: /((?:.|[\r\n])*?)(?:<\/b:text>|$)/g,
style: /((?:.|[\r\n])*?)(?:<\/b:style>|$)/g
};
var ATTR_BINDING = /\{([a-z_][a-z0-9_]*|l10n:[a-z_][a-z0-9_]*(?:\.[a-z_][a-z0-9_]*)*(?:\.\{[a-z_][a-z0-9_]*\})?)\}/i;
var CLASS_ATTR_BINDING = /^((?:[a-z_][a-z0-9_\-]*)?(?::(?:[a-z_][a-z0-9_\-]*)?)?)\{((anim:)?[a-z_][a-z0-9_\-]*)\}$/i;
var STYLE_ATTR_PARTS = /\s*[^:]+?\s*:(?:\(.*?\)|".*?"|'.*?'|[^;]+?)+(?:;|$)/gi;
var STYLE_PROPERTY = /\s*([^:]+?)\s*:((?:\(.*?\)|".*?"|'.*?'|[^;]+?)+);?$/i;
var STYLE_ATTR_BINDING = /\{([a-z_][a-z0-9_]*)\}/i;
var ATTRIBUTE_MODE = /^(?:|append-|set-|remove-)(class|attr)$/;
var decodeHTMLTokens = function () {
var tokenMap = {};
var tokenElement = !basis.NODE_ENV ? global.document.createElement("div") : null;
var NAMED_CHARACTER_REF = /&([a-z]+\d*|#\d+|#x[0-9a-f]{1,4});?/gi;
if (basis.NODE_ENV)
tokenMap = __nodejsRequire("./htmlentity.json");
function namedCharReplace(m, token) {
if (!tokenMap[token]) {
if (token.charAt(0) == "#") {
tokenMap[token] = String.fromCharCode(token.charAt(1) == "x" || token.charAt(1) == "X" ? parseInt(token.substr(2), 16) : token.substr(1));
} else {
if (tokenElement) {
tokenElement.innerHTML = m;
tokenMap[token] = tokenElement.firstChild ? tokenElement.firstChild.nodeValue : m;
}
}
}
return tokenMap[token] || m;
}
return function decodeHTMLTokens(string) {
return String(string).replace(NAMED_CHARACTER_REF, namedCharReplace);
};
}();
function buildAttrExpression(parts) {
var bindName;
var names = [];
var expression = [];
var map = {};
for (var j = 0; j < parts.length; j++)
if (j % 2) {
bindName = parts[j];
if (!map[bindName]) {
map[bindName] = names.length;
names.push(bindName);
}
expression.push(map[bindName]);
} else {
if (parts[j])
expression.push(decodeHTMLTokens(parts[j]));
}
return [
names,
expression
];
}
function processAttr(token, mode, convertRange) {
var value = token.value;
var bindings = 0;
var parts;
var m;
if (value) {
switch (mode) {
case "class":