Comparing version 4.0.0 to 4.1.0
# Changelog | ||
## 4.1.0 | ||
- Ability to replace `toHtml` method for only node (#144). | ||
- Added `jsElem` option (#148). | ||
- Boolean attributes supported (#145). | ||
- Trim `cls` (#143). | ||
## 4.0.0 | ||
@@ -4,0 +10,0 @@ - Added global matcher support: `beforeEach` and `afterEach` (#121). |
# История изменений | ||
## 4.1.0 | ||
- Добавлена возможность переопределять метод `toHtml` для любого узла (#144). | ||
- Добавлена опция `jsElem` (#148). | ||
- Поддержка булевых атрибутов (#145). | ||
- Обрезать пробелы в поле `cls` (#143). | ||
## 4.0.0 | ||
@@ -4,0 +10,0 @@ - Добавлена поддержка глобальных шаблонов: `beforeEach` and `afterEach` (#121). |
@@ -51,2 +51,3 @@ var BH = (function() { | ||
this._optJsCls = 'i-bem'; | ||
this._optJsElem = true; | ||
this._optEscapeContent = false; | ||
@@ -568,2 +569,5 @@ this._optNobaseMods = false; | ||
} | ||
if (options.hasOwnProperty('jsElem')) { | ||
this._optJsElem = options.jsElem; | ||
} | ||
if (options.clsNobaseMods) { | ||
@@ -969,19 +973,40 @@ this._optNobaseMods = true; | ||
toHtml: function(json) { | ||
var res, i, l, item; | ||
if (json === false || json == null) return ''; | ||
this._buf = ''; | ||
this._html(json); | ||
var buf = this._buf; | ||
delete this._buf; | ||
return buf; | ||
}, | ||
/** | ||
* Наполняет HTML-строку. | ||
* @param {BemJson} json | ||
* @returns {undefined} | ||
*/ | ||
_html: function(json) { | ||
var i, l, item; | ||
if (json === false || json == null) return; | ||
if (typeof json !== 'object') { | ||
return this._optEscapeContent ? xmlEscape(json) : json; | ||
this._buf += this._optEscapeContent ? xmlEscape(json) : json; | ||
} else if (Array.isArray(json)) { | ||
res = ''; | ||
for (i = 0, l = json.length; i < l; i++) { | ||
item = json[i]; | ||
if (item !== false && item != null) { | ||
res += this.toHtml(item); | ||
this._html(item); | ||
} | ||
} | ||
return res; | ||
} else { | ||
if (json.toHtml) { | ||
var html = json.toHtml.call(this, json) || ''; | ||
this._buf += html; | ||
return; | ||
} | ||
var isBEM = json.bem !== false; | ||
if (typeof json.tag !== 'undefined' && !json.tag) { | ||
return json.html || json.content ? this.toHtml(json.content) : ''; | ||
if (json.html) { | ||
this._buf += json.html; | ||
} else { | ||
this._html(json.content); | ||
} | ||
return; | ||
} | ||
@@ -997,3 +1022,5 @@ if (json.mix && !Array.isArray(json.mix)) { | ||
jval = jattr[i]; | ||
if (jval !== null && jval !== undefined) { | ||
if (jval === true) { | ||
attrs += ' ' + i; | ||
} else if (jval !== false && jval !== null && jval !== undefined) { | ||
attrs += ' ' + i + '="' + attrEscape(jval) + '"'; | ||
@@ -1014,2 +1041,4 @@ } | ||
var addJSInitClass = this._optJsCls && (this._optJsElem || !json.elem); | ||
var mixes = json.mix; | ||
@@ -1029,2 +1058,5 @@ if (mixes && mixes.length) { | ||
hasMixJsParams = true; | ||
if (!addJSInitClass) { | ||
addJSInitClass = mixBlock && (this._optJsCls && (this._optJsElem || !mixElem)); | ||
} | ||
} | ||
@@ -1037,3 +1069,3 @@ } | ||
if (jsParams) { | ||
if (this._optJsCls) cls += ' ' + this._optJsCls; | ||
if (addJSInitClass) cls += ' ' + this._optJsCls; | ||
var jsData = (!hasMixJsParams && json.js === true ? | ||
@@ -1048,29 +1080,19 @@ '{"' + base + '":{}}' : | ||
if (json.cls) { | ||
cls = (cls ? cls + ' ' : '') + attrEscape(json.cls); | ||
cls = (cls ? cls + ' ' : '') + attrEscape(json.cls).trim(); | ||
} | ||
var content, tag = (json.tag || 'div'); | ||
res = '<' + tag + (cls ? ' class="' + cls + '"' : '') + (attrs ? attrs : ''); | ||
var tag = (json.tag || 'div'); | ||
this._buf += '<' + tag + (cls ? ' class="' + cls + '"' : '') + (attrs ? attrs : ''); | ||
if (selfCloseHtmlTags[tag]) { | ||
res += '/>'; | ||
this._buf += '/>'; | ||
} else { | ||
res += '>'; | ||
this._buf += '>'; | ||
if (json.html) { | ||
res += json.html; | ||
} else if ((content = json.content) != null) { | ||
if (Array.isArray(content)) { | ||
for (i = 0, l = content.length; i < l; i++) { | ||
item = content[i]; | ||
if (item !== false && item != null) { | ||
res += this.toHtml(item); | ||
} | ||
} | ||
} else { | ||
res += this.toHtml(content); | ||
} | ||
this._buf += json.html; | ||
} else { | ||
this._html(json.content); | ||
} | ||
res += '</' + tag + '>'; | ||
this._buf += '</' + tag + '>'; | ||
} | ||
return res; | ||
} | ||
@@ -1077,0 +1099,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"name": "bh", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"repository": "https://github.com/bem/bh", | ||
@@ -8,0 +8,0 @@ "licence": "MIT", |
@@ -194,6 +194,4 @@ BH [![NPM version](https://badge.fury.io/js/bh.svg)](http://badge.fury.io/js/bh) [![Build Status](https://travis-ci.org/bem/bh.svg?branch=master)](https://travis-ci.org/bem/bh) [![Dependency Status](https://gemnasium.com/bem/bh.svg)](https://gemnasium.com/bem/bh) [![Coverage Status](https://img.shields.io/coveralls/bem/bh.svg?branch=master)](https://coveralls.io/r/bem/bh) | ||
bh.setOptions({ escapeContent: true }); | ||
bh.apply({ content: '<script>' }); | ||
bh.apply({ content: '<script>' }); | ||
``` | ||
```html | ||
@@ -379,2 +377,10 @@ <div><script></div> | ||
*N.B.* Pass `true` as a second parameter to set a boolean attribute: | ||
```javascript | ||
bh.match('link_hidden_yes', function(ctx) { | ||
ctx.attr('hidden', true); | ||
}); | ||
``` | ||
## ctx.attrs([values[, force]]) | ||
@@ -381,0 +387,0 @@ |
@@ -52,3 +52,3 @@ BH [![NPM version](https://badge.fury.io/js/bh.svg)](http://badge.fury.io/js/bh) [![Build Status](https://travis-ci.org/bem/bh.svg?branch=master)](https://travis-ci.org/bem/bh) [![Dependency Status](https://gemnasium.com/bem/bh.svg)](https://gemnasium.com/bem/bh) [![Coverage Status](https://img.shields.io/coveralls/bem/bh.svg?branch=master)](https://coveralls.io/r/bem/bh) | ||
Функции для работы с BEMJSON — **шаблоны** — объявляются через метод `match`. В теле функций описывается логика преобразования BEMJSON. | ||
В функцию-шаблон передаются два аргумента: `ctx` — инстанция класса `Ctx` и `json` — ссылка на текущий узел BEMJSON-дерева. | ||
В функцию-шаблон передаются два аргумента: `ctx` — экземпляр класса `Ctx` и `json` — ссылка на текущий узел BEMJSON-дерева. | ||
@@ -370,2 +370,10 @@ *Замечание:* Категорически не рекомендуется вносить изменения напрямую в объект `json`. Вместо этого следует использовать методы объекта `ctx`. Объект `json` рекомендуется использовать только для «чтения» (см. также метод `ctx.json()`). | ||
*Замечание:* Чтобы задать булевый атрибут, следует передать вторым параметром `true`: | ||
```javascript | ||
bh.match('link_hidden_yes', function(ctx) { | ||
ctx.attr('hidden', true); | ||
}); | ||
``` | ||
## ctx.attrs([values[, force]]) | ||
@@ -372,0 +380,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
332062
1172
587