Skip to content

Commit

Permalink
Задание атрибута вида <input required>
Browse files Browse the repository at this point in the history
  • Loading branch information
sameoldmadness committed May 9, 2015
1 parent 8f49e4a commit cfbd32e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,14 @@ bh.match('link', function(ctx) {
});
```
*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]])
This class method returns/sets attributes depending on arguments. Use **force** to set the attributes even if they were specified earlier.
Expand Down
8 changes: 8 additions & 0 deletions README.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,14 @@ bh.match('link', function(ctx) {
});
```
*Замечание:* Чтобы задать булевый атрибут, следует передать вторым параметром `true`:
```javascript
bh.match('link_hidden_yes', function(ctx) {
ctx.attr('hidden', true);
});
```
## ctx.attrs([values[, force]])
Возвращает/устанавливает атрибуты в зависимости от аргументов. **force** — задать атрибуты, даже если они были заданы ранее.
Expand Down
4 changes: 3 additions & 1 deletion lib/bh.js
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,9 @@ BH.prototype = {
if (jattr = json.attrs) {
for (i in jattr) {
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) + '"';
}
}
Expand Down
3 changes: 2 additions & 1 deletion test/test.attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ describe('ctx.attr()', function() {
ctx.attr('name', undefined);
ctx.attr('type', 'button');
ctx.attr('disabled', false);
ctx.attr('hidden', true);
ctx.attr('value', null);
});
bh.apply({ block: 'checkbox' }).should.equal('<div class="checkbox" type="button" disabled="false"></div>');
bh.apply({ block: 'checkbox' }).should.equal('<div class="checkbox" type="button" hidden></div>');
});

it('should not override user attr', function() {
Expand Down
6 changes: 4 additions & 2 deletions test/test.attrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ describe('ctx.attrs()', function() {
it('should set attrs', function() {
bh.match('checkbox', function(ctx) {
ctx.attrs({
name: undefined,
type: 'button',
disabled: false,
name: undefined
hidden: true,
value: null
});
});
bh.apply({ block: 'checkbox' }).should.equal('<div class="checkbox" type="button" disabled="false"></div>');
bh.apply({ block: 'checkbox' }).should.equal('<div class="checkbox" type="button" hidden></div>');
});

it('should not override user attrs', function() {
Expand Down

0 comments on commit cfbd32e

Please sign in to comment.