| 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 | 1
1
1
1
1
1
1
1464
1
122
122
122
122
122
122
122
122
4
4
4
122
10
5
5
5
122
5
122
122
122
122
122
122
122
122
8
8
8
122
122
122
34
13
21
122
11
1
10
1
152
151
151
151
151
151
151
1
179
1
186
6
180
11
169
167
167
167
167
167
167
167
2
1
167
167
167
161
167
167
34
167
1
165
165
36
36
165
165
209
162
162
162
162
165
1
11
11
11
30
30
30
11
11
11
1
151
151
132
151
151
1
4
4
1
11
1
11
1
2
1
4
1
7
1
7
4
4
1
4
4
1
1
1
| var Utils = require('./utils.js');
var lastGenId = 0;
var BEMJSON = require('bemjson-to-html');
var Context = require('snap-context');
var matcher = require('object-match-statement');
var flatten = require('./flatten.js');
function defineContextMethod(object, method, func) {
Object.defineProperty(object, method, {
value: func,
configurable : false,
enumerable : false,
writable : false
});
}
function JSOTBH() {
this._options = {
jsAttrName: 'onclick',
};
this.bemjson = new BEMJSON(this._options);
this._matchers = {};
this._patterns = {};
this._current = { length: -1, position: -1, matcherIdx: -1 };
this._context = new Context();
this._milliseconds = new Date().getTime().toString();
this.applyBase = function () {
var block = this._context.get('block');
this.applyMatchers(
this._matchers[block],
this._patterns[block],
this._current.matcherIdx - 1
);
this.stop();
};
this.tParam = function (key, value) {
if (value) {
this._context.set(key, value);
return value;
}
return this._context.get(key);
};
this.stop = function () {
this._stopFlag = true;
};
defineContextMethod(this, 'attr', Utils.setPropertyKeyValue.bind(this)('attrs'));
defineContextMethod(this, 'attrs', Utils.setPropertyKeyValueObject.bind(this)('attrs'));
defineContextMethod(this, 'bem', Utils.setPropertyValue.bind(this)('bem'));
defineContextMethod(this, 'cls', Utils.setPropertyValue.bind(this)('cls'));
defineContextMethod(this, 'content', Utils.setPropertyValue.bind(this)('content'));
defineContextMethod(this, 'extend', Utils.extend);
defineContextMethod(this, 'js', Utils.setPropertyValue.bind(this).call(this, 'js'));
defineContextMethod(this, 'param', function () {
var args = Array.prototype.slice.call(arguments);
var name = args.shift();
return Utils.setPropertyValue.bind(this)(name).apply(this, args);
});
defineContextMethod(this, 'tag', Utils.setPropertyValue.bind(this)('tag'));
defineContextMethod(this, 'mix', Utils.setPropertyArray.bind(this)('mix'));
defineContextMethod(this, 'mod', function () {
if (this._context.get('object').elem) {
return Utils.setPropertyKeyValue.bind(this)('elemMods').apply(this, arguments);
} else {
return Utils.setPropertyKeyValue.bind(this)('mods').apply(this, arguments);
}
});
defineContextMethod(this, 'mods', function () {
if (this._context.get('object').elem) {
return Utils.setPropertyKeyValueObject.bind(this)('elemMods').apply(this, arguments);
} else {
return Utils.setPropertyKeyValueObject.bind(this)('mods').apply(this, arguments);
}
});
}
JSOTBH.prototype.match = function match(pattern, callback) {
if (typeof pattern !== 'string') { throw new Error('Pattern should be a string, not a ' + pattern); }
var parsedPattern = Utils.parseBhIdentifier(pattern);
this._matchers[parsedPattern.block] = this._matchers[parsedPattern.block] || [];
this._matchers[parsedPattern.block].push(callback.bind(this, this));
this._patterns[parsedPattern.block] = this._patterns[parsedPattern.block] || [];
this._patterns[parsedPattern.block].push(this.compilePattern(parsedPattern));
return this;
};
JSOTBH.prototype.apply = function apply(json) {
return this.bemjson.toHtml(this.process(json), this._options);
};
JSOTBH.prototype.process = function process(json) {
if (typeof json === 'string') {
return json;
}
if (Array.isArray(json)) {
return this.processArray(json);
}
if (typeof json === 'object' && json) {
if (json.block) { this._context.set('block', json.block); }
if (json.mods) { this._context.set('blockMods', json.mods); }
this._context.set('object', json);
this._context.snapshot();
var result = this.processObject(json);
this._context.restore();
return result;
} else {
return '' + json;
}
};
JSOTBH.prototype.processObject = function processObject() {
var block = this._context.get('block');
var matchersForBlock = this._matchers[block];
if (matchersForBlock) {
this.applyMatchers(matchersForBlock, this._patterns[block]);
}
var object = this._context.get('object');
if (object.content !== undefined) {
object.content = this.apply(object.content);
}
return object;
};
JSOTBH.prototype.applyMatchers = function applyMatchers(matchers, patterns, startFrom) {
var object = this._context.get('object');
if (!object.block) {
object.block = this._context.get('block');
object.mods = this._context.get('blockMods');
}
if (startFrom === undefined) { startFrom = matchers.length - 1; }
for (var m = startFrom; m >= 0 ; m--) {
if (patterns[m](object)) {
this._current.matcherIdx = m;
var result = matchers[m](this._context.get('object'));
if (result) { this._context.set('object', result); }
if (this._stopFlag) { break; }
}
}
this._stopFlag = false;
};
JSOTBH.prototype.processArray = function processArray(array) {
var result = '';
array = flatten(array);
for (var i = array.length - 1; i >= 0; i--) {
this._current.length = array.length;
this._current.position = i;
result = this.apply(array[i]) + result;
}
this._current.length = -1;
this._current.position = 0;
return result;
};
JSOTBH.prototype.compilePattern = function compilePatern(pattern) {
var statement = matcher.build('object', pattern);
if (pattern.elem === undefined) {
statement = 'object.elem === undefined && ' + statement;
}
var composedFunction = 'return ' + statement + ';';
/*jshint -W054*/ /* Yes, this is eval */
return new Function('object', composedFunction);
};
JSOTBH.prototype.generateId = function generateId() {
lastGenId += 1;
return 'uniq' + this._milliseconds + lastGenId;
};
JSOTBH.prototype.isFirst = function isFirts() {
return this._current.position === 0;
};
JSOTBH.prototype.isLast = function isLast() {
return this._current.position === this._current.length - 1;
};
JSOTBH.prototype.json = function json() {
return this._context.get('object');
};
JSOTBH.prototype.length = function length() {
return this._current.length;
};
JSOTBH.prototype.position = function position() {
return this._current.position;
};
JSOTBH.prototype.isSimple = function isSimple(obj) {
if (!obj || obj === true) { return true; }
var t = typeof obj;
return t === 'string' || t === 'number';
};
JSOTBH.prototype.setOptions = function setOptions(_options) {
this._options = Utils.extend(this._options, _options);
this.bemjson = new BEMJSON(this._options);
};
JSOTBH.prototype.getOptions = function getOptions() {
return this._options;
};
module.exports = JSOTBH;
|