forked from floatdrop/bemjson-to-html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
173 lines (141 loc) · 5.38 KB
/
index.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
var escape = require('./escape.js');
var DEFAULT_MOD_SEPARATOR = '_';
var _jsAttrName = 'onclick';
var _defaultTag = 'div';
function BEMJSON (options) {
options = options || {};
options.jsAttrScheme = options.jsAttrScheme || 'js';
options.jsAttrName = options.jsAttrName || _jsAttrName;
options.jsAttrIsJs = options.jsAttrScheme === 'js';
options.defaultTag = options.defaultTag || _defaultTag;
options.modificatorSeparator = options.modificatorSeparator || DEFAULT_MOD_SEPARATOR;
options.addDefautTagAttributes = options.addDefautTagAttributes || false;
this._options = options;
}
BEMJSON.prototype.bemClasses = function bemClasses(bemjson, argBlock) {
var block = bemjson.block || argBlock;
if (bemjson.bem === false || !block) { return ''; }
var base = block + (bemjson.elem ? '__' + bemjson.elem : '');
var res = (base === argBlock) ? '' : base;
var mods = bemjson.elem ? bemjson.elemMods : bemjson.mods;
var modSep = this._options && this._options.modificatorSeparator ? this._options.modificatorSeparator : DEFAULT_MOD_SEPARATOR;
if (mods) {
for (var i in mods) {
if (typeof mods[i] === 'number') {
mods[i] += '';
}
if (mods[i]) {
res += ' ' + base + modSep + i + (mods[i] === true ? '' : modSep + mods[i]);
}
}
}
if (bemjson.mix) {
if (!Array.isArray(bemjson.mix)) { bemjson.mix = [bemjson.mix]; }
for (var i = 0; i < bemjson.mix.length; i++) {
var mix = bemjson.mix[i];
if (!mix) { continue; }
res += ' ' + bemClasses(mix, block);
}
}
return res;
};
BEMJSON.prototype.classes = function classes(bemjson, ctxBlock) {
if (ctxBlock && bemjson.elem && !bemjson.block) {
bemjson.block = ctxBlock;
}
var cls = (bemjson.bem !== false && (bemjson.block || ctxBlock)) ? this.bemClasses(bemjson) : '';
if (bemjson.cls) { cls += ' ' + bemjson.cls; }
if (bemjson.jsParams && Object.keys(bemjson.jsParams).length !== 0) { cls += ' i-bem'; }
if (cls === '') { return ''; }
return ' class="' + escape(cls) + '"';
};
BEMJSON.prototype.attributes = function attributes(json) {
if (!json.attrs) { return ''; }
var attrs = '';
for (var key in json.attrs) {
var attr = json.attrs[key];
if (attr !== null && attr !== undefined) {
attrs = attrs + ' ' + key + '="' + escape(attr) + '"';
}
}
return attrs;
};
BEMJSON.prototype.fillJsParamsFromMixins = function fillJsParamsFromMixins(json) {
if (!json.mix) { return; }
if (!Array.isArray(json.mix)) { json.mix = [json.mix]; }
var mixes = json.mix;
for (var i = 0; i < mixes.length; i++) {
var mix = mixes[i];
if (mix && mix.js) {
json.jsParams = json.jsParams || {};
mix.js = mix.js === true ? {} : mix.js;
json.jsParams[(mix.block || json.block) + (mix.elem ? '__' + mix.elem : '')] = mix.js;
}
}
};
BEMJSON.prototype.concatinateArray = function concatinateArray(array, ctxBlock) {
var res = '';
for (var i = 0; i < array.length; i++) {
if (array[i] !== undefined && array[i] !== false && array[i] !== null) {
res += this.toHtml(array[i], ctxBlock);
}
}
return res;
};
BEMJSON.prototype.toHtml = function toHtml(bemjson, ctxBlock) {
if (bemjson === undefined || bemjson === false || bemjson === null) { return ''; }
if (typeof bemjson !== 'object') {
return bemjson;
}
if (bemjson.block) {
ctxBlock = bemjson.block;
}
if (Array.isArray(bemjson)) {
return this.concatinateArray(bemjson, ctxBlock);
}
if (!bemjson) { return bemjson + ''; }
if (bemjson.tag === false) { return this.toHtml(bemjson.content || '', ctxBlock); }
if (bemjson.js) {
bemjson.jsParams = bemjson.jsParams || {};
bemjson.jsParams[bemjson.block + (bemjson.elem ? '__' + bemjson.elem : '')] = bemjson.js === true ? {} : bemjson.js;
}
this.fillJsParamsFromMixins(bemjson);
bemjson.tag = bemjson.tag || this._options.defaultTag;
var res = '<' + bemjson.tag + this.classes(bemjson, ctxBlock) + this.attributes(bemjson);
if (bemjson.jsParams || bemjson.hasMixJsParams) {
var jsData = JSON.stringify(bemjson.jsParams).replace(/"/g, '"');
bemjson.jsAttr = bemjson.jsAttr || this._options.jsAttrName;
res += ' ' + bemjson.jsAttr + '="' + (this._options.jsAttrIsJs ? 'return ' + jsData + ';' : jsData) + '"';
}
var tag = bemjson.tag;
if (this._options.addDefautTagAttributes) {
if (tag === 'img') {
res += ' src="#" alt="" ';
}
if (tag === 'a') {
res += ' href="#" ';
}
if (tag === 'source') {
res += ' src="#" ';
}
}
if (tag === 'area' ||
tag === 'base' ||
tag === 'br' ||
tag === 'col' ||
tag === 'command' ||
tag === 'embed' ||
tag === 'hr' ||
tag === 'img' ||
tag === 'input' ||
tag === 'keygen' ||
tag === 'link' ||
tag === 'menuitem' ||
tag === 'meta' ||
tag === 'param' ||
tag === 'source' ||
tag === 'track' ||
tag === 'wbr') { return res + '/>'; }
return res + '>' + this.toHtml(bemjson.content, ctxBlock) + '</' + bemjson.tag + '>';
};
module.exports = BEMJSON;