-
Notifications
You must be signed in to change notification settings - Fork 0
/
i-bem__html.bemhtml
619 lines (489 loc) · 17.5 KB
/
i-bem__html.bemhtml
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
!function oninit() {
var BEM_ = {},
toString = Object.prototype.toString,
SHORT_TAGS = { // хэш для быстрого определения, является ли тэг коротким
area : 1, base : 1, br : 1, col : 1, command : 1, embed : 1, hr : 1, img : 1,
input : 1, keygen : 1, link : 1, meta : 1, param : 1, source : 1, wbr : 1 };
/** @fileOverview - module for internal BEM helpers */
/** @requires BEM */
(function(BEM, undefined) {
/**
* Separator for modifiers and their values
* @const
* @type String
*/
var MOD_DELIM = '_',
/**
* Separator between block names and a nested element
* @const
* @type String
*/
ELEM_DELIM = '__',
/**
* Pattern for acceptable names of elements and modifiers
* @const
* @type String
*/
NAME_PATTERN = '[a-zA-Z0-9-]+';
function buildModPostfix(modName, modVal, buffer) {
buffer.push(MOD_DELIM, modName, MOD_DELIM, modVal);
}
function buildBlockClass(name, modName, modVal, buffer) {
buffer.push(name);
modVal && buildModPostfix(modName, modVal, buffer);
}
function buildElemClass(block, name, modName, modVal, buffer) {
buildBlockClass(block, undefined, undefined, buffer);
buffer.push(ELEM_DELIM, name);
modVal && buildModPostfix(modName, modVal, buffer);
}
BEM.INTERNAL = {
NAME_PATTERN : NAME_PATTERN,
MOD_DELIM : MOD_DELIM,
ELEM_DELIM : ELEM_DELIM,
buildModPostfix : function(modName, modVal, buffer) {
var res = buffer || [];
buildModPostfix(modName, modVal, res);
return buffer? res : res.join('');
},
/**
* Builds the class for a block or element with a modifier
* @private
* @param {String} block Block name
* @param {String} [elem] Element name
* @param {String} [modName] Modifier name
* @param {String} [modVal] Element name
* @param {Array} [buffer] Buffer
* @returns {String|Array} Class or buffer string (depending on whether the buffer parameter is present)
*/
buildClass : function(block, elem, modName, modVal, buffer) {
var typeOf = typeof modName;
if(typeOf == 'string') {
if(typeof modVal != 'string') {
buffer = modVal;
modVal = modName;
modName = elem;
elem = undefined;
}
} else if(typeOf != 'undefined') {
buffer = modName;
modName = undefined;
} else if(elem && typeof elem != 'string') {
buffer = elem;
elem = undefined;
}
if(!(elem || modName || buffer)) { // оптимизация для самого простого случая
return block;
}
var res = buffer || [];
elem?
buildElemClass(block, elem, modName, modVal, res) :
buildBlockClass(block, modName, modVal, res);
return buffer? res : res.join('');
},
/**
* Builds modifier classes
* @private
* @param {String} block Block name
* @param {String} [elem] Element name
* @param {Object} [mods] Modifier name
* @param {Array} [buffer] Buffer
* @returns {String|Array} Class or buffer string (depending on whether the buffer parameter is present)
*/
buildModsClasses : function(block, elem, mods, buffer) {
var res = buffer || [];
if(mods) {
var modName; // TODO: разобраться с OmetaJS и YUI Compressor
for(modName in mods) {
if(!mods.hasOwnProperty(modName)) continue;
var modVal = mods[modName];
if (modVal == null) continue;
modVal = mods[modName] + '';
if (!modVal) continue;
res.push(' ');
if (elem) {
buildElemClass(block, elem, modName, modVal, res)
} else {
buildBlockClass(block, modName, modVal, res);
}
}
}
return buffer? res : res.join('');
},
/**
* Builds full classes for a block or element with modifiers
* @private
* @param {String} block Block name
* @param {String} [elem] Element name
* @param {Object} [mods] Modifier name
* @param {Array} [buffer] Buffer
* @returns {String|Array} Class or buffer string (depending on whether the buffer parameter is present)
*/
buildClasses : function(block, elem, mods, buffer) {
var res = buffer || [];
elem?
buildElemClass(block, elem, undefined, undefined, res) :
buildBlockClass(block, undefined, undefined, res);
this.buildModsClasses(block, elem, mods, buffer);
return buffer? res : res.join('');
}
};
})(BEM_);
function BEMContext(apply_) {
this.apply = apply_;
this._ = this;
this.ctx = null;
this._buf = null;
// Stub out fields that will be used later
this._start = true;
this._listLength = 0;
this._notNewList = false;
this._inside = false;
this.position = 0;
};
BEMContext.prototype.reinit = function reinit(context) {
this.ctx = typeof context === null ? '' : context;
this._buf = [];
// Stub out fields that will be used later
this._start = true;
this._listLength = 0;
this._notNewList = false;
this.position = 0;
};
if (Array.isArray) {
BEMContext.prototype.isArray = function isArray(obj) {
return Array.isArray(obj);
};
} else {
BEMContext.prototype.isArray = function isArray(obj) {
return toString.call(obj) === "[object Array]";
};
}
BEMContext.prototype.isSimple = function isSimple(obj) {
var t = typeof obj;
return t === 'string' || t === 'number' || t === 'boolean';
};
BEMContext.prototype.isShortTag = function isShortTag(t) {
return SHORT_TAGS.hasOwnProperty(t);
};
BEMContext.prototype.extend = function extend(o1, o2) {
if(!o1 || !o2) return o1 || o2;
var res = {}, n;
for(n in o1) o1.hasOwnProperty(n) && (res[n] = o1[n]);
for(n in o2) o2.hasOwnProperty(n) && (res[n] = o2[n]);
return res;
};
BEMContext.prototype.identify = (function() {
var cnt = 0,
id = BEM_.__id = (+new Date()),
expando = '__' + id,
get = function() { return 'uniq' + id + ++cnt; };
return function(obj, onlyGet) {
if(!obj) return get();
if(onlyGet || obj[expando]) return obj[expando];
else return (obj[expando] = get());
};
})();
BEMContext.prototype.xmlEscape = function xmlEscape(str) {
return (str + '').replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
};
BEMContext.prototype.attrEscape = function attrEscape(str) {
return (str + '').replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
};
BEMContext.prototype.BEM = BEM_;
BEMContext.prototype.isFirst = function isFirst() {
return this.position === 1;
};
BEMContext.prototype.isLast = function isLast() {
return this.position === this._listLength;
};
BEMContext.prototype.generateId = function generateId() {
return this.identify(this.ctx);
};
// Wrap xjst's apply and export our own
exports.apply = BEMContext.apply = function _apply() {
var ctx = new BEMContext(apply);
ctx.reinit(this);
ctx.apply();
return ctx._buf.join('');
};
}();
return exports;
this._mode === '' {
true: {
var vBlock = this.ctx.block,
vElem = this.ctx.elem,
block = this._currBlock || this.block;
this.ctx || (this.ctx = {});
local(
this._mode = 'default',
this._links = this.ctx.links || this._links,
this.block = vBlock || (vElem ? block : undefined),
this._currBlock = vBlock || vElem ? undefined : block,
this.elem = this.ctx.elem,
this.mods = (vBlock ? this.ctx.mods : this.mods) || {},
this.elemMods = this.ctx.elemMods || {}
) {
(this.block || this.elem) ?
(this.position = (this.position || 0) + 1) :
this._listLength--;
apply();
}
}
this._.isArray(this.ctx): {
var v = this.ctx,
l = v.length,
i = 0,
prevPos = this.position,
prevNotNewList = this._notNewList;
if(prevNotNewList) {
this._listLength += l - 1;
} else {
this.position = 0;
this._listLength = l;
}
this._notNewList = true;
while(i < l) {
var newCtx = v[i++];
apply(this.ctx = newCtx == null ? '' : newCtx);
}
prevNotNewList || (this.position = prevPos);
}
!this.ctx: {
this._listLength--;
}
this._.isSimple(this.ctx): {
this._listLength--;
var ctx = this.ctx;
(ctx && ctx !== true || ctx === 0) && this._buf.push(ctx);
}
}
default: {
var BEM_ = this.BEM,
v = this.ctx,
buf = this._buf,
tag;
tag = apply(this._mode = 'tag');
typeof tag != 'undefined' || (tag = v.tag);
typeof tag != 'undefined' || (tag = 'div');
if(tag) {
var jsParams, js;
if(this.block && v.js !== false) {
js = apply(this._mode = 'js');
js = js? this._.extend(v.js, js === true? {} : js) : v.js === true? {} : v.js;
js && ((jsParams = {})[BEM_.INTERNAL.buildClass(this.block, v.elem)] = js);
}
buf.push('<', tag);
var isBEM = apply(this._mode = 'bem');
typeof isBEM != 'undefined' || (isBEM = typeof v.bem != 'undefined' ? v.bem : v.block || v.elem);
var cls = apply(this._mode = 'cls');
cls || (cls = v.cls);
var addJSInitClass = v.block && jsParams;
if(isBEM || cls) {
buf.push(' class="');
if(isBEM) {
BEM_.INTERNAL.buildClasses(this.block, v.elem, v.elemMods || v.mods, buf);
var mix = apply(this._mode = 'mix');
v.mix && (mix = mix? mix.concat(v.mix) : v.mix);
if(mix) {
var visited = {};
function visitedKey(block, elem) {
return (block || '') + '__' + (elem || '');
}
visited[visitedKey(this.block, this.elem)] = true;
// Transform mix to the single-item array if it's not array
if (!this._.isArray(mix)) mix = [mix];
for (var i = 0; i < mix.length; i++) {
var mixItem = mix[i];
if (!mixItem) continue;
var hasItem = mixItem.block || mixItem.elem,
block = mixItem.block || mixItem._block || this.block,
elem = mixItem.elem || mixItem._elem || this.elem,
mods = mixItem.mods || this.mods,
elemMods = mixItem.elemMods || {};
hasItem && buf.push(' ');
BEM_.INTERNAL[hasItem? 'buildClasses' : 'buildModsClasses'](
block,
mixItem.elem || mixItem._elem ||
(mixItem.block ? undefined : this.elem),
mixItem.elemMods || mixItem.mods,
buf);
if(mixItem.js) {
(jsParams || (jsParams = {}))[BEM_.INTERNAL.buildClass(block, mixItem.elem)] = mixItem.js === true? {} : mixItem.js;
addJSInitClass || (addJSInitClass = block && !mixItem.elem);
}
// Process nested mixes
if (hasItem && !visited[visitedKey(block, elem)]) {
visited[visitedKey(block, elem)] = true;
var nestedMix = apply(
this.block = block,
this.elem = elem,
this.mods = mods,
this.elemMods = elemMods,
'mix'
);
if (nestedMix) {
for (var j = 0; j < nestedMix.length; j++) {
var nestedItem = nestedMix[j];
if (!nestedItem.block &&
!nestedItem.elem ||
!visited[visitedKey(
nestedItem.block,
nestedItem.elem
)]) {
nestedItem._block = block;
nestedItem._elem = elem;
mix.splice(i + 1, 0, nestedItem);
}
}
}
}
}
}
}
cls && buf.push(isBEM? ' ' : '', cls);
addJSInitClass && buf.push(' i-bem');
buf.push('"');
}
if(jsParams) {
var jsAttr = apply(this._mode = 'jsAttr');
buf.push(
' ', jsAttr || 'data-bem', '="',
this._.attrEscape(JSON.stringify(jsParams)),
'"');
}
var attrs = apply(this._mode = 'attrs');
attrs = this._.extend(attrs, v.attrs); // NOTE: возможно стоит делать массив, чтобы потом быстрее сериализовывать
if(attrs) {
var name; // TODO: разобраться с OmetaJS и YUI Compressor
for(name in attrs) {
if (attrs[name] === undefined) continue;
buf.push(' ', name, '="', this._.attrEscape(attrs[name]), '"');
}
}
}
if(this._.isShortTag(tag)) {
buf.push('/>');
} else {
tag && buf.push('>');
var content = apply(this._mode = 'content');
if(content || content === 0) {
var isBEM = this.block || this.elem;
apply(
this._notNewList = false,
this.position = isBEM ? 1 : this.position,
this._listLength = isBEM ? 1 : this._listLength,
this.ctx = content,
this._mode = '');
}
tag && buf.push('</', tag, '>');
}
}
// Cache start
cache && this.ctx && this.ctx.cache: {
var cached;
// setProperty(obj, ['nested', 'key'], 'value')
function setProperty(obj, key, value) {
if (key.length === 0) return;
if (Array.isArray(value)) {
var target = obj;
for (var i = 0; i < value.length - 1; i++) {
target = target[value[i]];
}
value = target[value[i]];
}
var host = obj,
previous;
for (var i = 0; i < key.length - 1; i++) {
host = host[key[i]];
}
previous = host[key[i]];
host[key[i]] = value;
return previous;
}
if (cached = cache.get(this.ctx.cache)) {
// get new links from context
var oldLinks = this._links;
if (this.ctx.links) this._links = this.ctx.links;
// Cache hit
for (var i = 0; i < cached.log.length; i++) {
if (typeof cached.log[i] === 'string') {
this._buf.push(cached.log[i]);
continue;
}
// Apply log and create reverse movement
var log = cached.log[i],
reverseLog;
reverseLog = log.log.map(function(entry) {
return {
key: entry[0],
value: setProperty(this, entry[0], entry[1])
};
}, this).reverse();
// Generate content
apply(this.ctx.cache = null,
this._cacheLog = null,
this.ctx.link = log.link);
// Revert changes
reverseLog.forEach(function(entry) {
setProperty(this, entry.key, entry.value)
}, this);
}
// restore links
this._links = oldLinks;
return cached.res;
}
// Record log
var cacheLog = [],
res;
local(this.ctx.cache = null,
this._cachePos = this._buf.length,
this._cacheLog = cacheLog,
this._localLog = []) {
res = apply();
var tail = this._buf.slice(this._cachePos).join('');
if (tail) cacheLog.push(tail);
}
// And set cache
cache.set(this.ctx.cache, { log: cacheLog, res: res });
return res;
}
// Data redirection
this.ctx && !this._.isSimple(this.ctx) && this.ctx.link: {
function follow() {
if (this.ctx.link === 'no-follow') return;
var data = this._links[this.ctx.link];
return apply(this.ctx = data);
}
if (!cache || !this._cacheLog) return follow.call(this);
// Get contents that should be cached
var contents = this._buf.slice(this._cachePos).join('');
// Move buffer position forward
this._cachePos = this._buf.length;
this._cacheLog.push(
// static cache entry
contents,
// dynamic entry
{
log: this._localLog.slice(),
link: this.ctx.link
}
);
var res = follow.call(this);
// Skip non-cacheable contents
this._cachePos = this._buf.length;
return res;
}
tag: undefined
attrs: undefined
cls: undefined
js: undefined
jsAttr: undefined
bem: undefined
mix: undefined
content: this.ctx.content