Skip to content

Commit

Permalink
Merge pull request #668 from rwaldin/prop-method
Browse files Browse the repository at this point in the history
Prop method PR updated
  • Loading branch information
fb55 committed Feb 1, 2016
2 parents b5531bb + 1f6ad79 commit c04ead1
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ $('.apple').attr('id', 'favorite').html()

> See http://api.jquery.com/attr/ for more information
#### .prop( name, value )
Method for getting and setting properties. Gets the property value for only the first element in the matched set.

```js
$('input[type="checkbox"]').prop('checked')
//=> false

$('input[type="checkbox"]').prop('checked', true).val()
//=> ok
```

> See http://api.jquery.com/prop/ for more information
#### .data( name, value )
Method for getting and setting data attributes. Gets or sets the data attribute value for only the first element in the matched set.

Expand Down
65 changes: 65 additions & 0 deletions lib/api/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,71 @@ exports.attr = function(name, value) {
return getAttr(this[0], name);
};

var getProp = function (el, name) {
return el.hasOwnProperty(name)
? el[name]
: rboolean.test(name)
? getAttr(el, name) !== undefined
: getAttr(el, name);
};

var setProp = function (el, name, value) {
el[name] = rboolean.test(name) ? !!value : value;
};

exports.prop = function (name, value) {
var i = 0,
property;

if (typeof name === 'string' && value === undefined) {

switch (name) {
case 'style':
property = this.css();

_.each(property, function (v, p) {
property[i++] = p;
});

property.length = i;

break;
case 'tagName':
case 'nodeName':
property = this[0].name.toUpperCase();
break;
default:
property = getProp(this[0], name);
}

return property;
}

if (typeof name === 'object' || value !== undefined) {

if (typeof value === 'function') {
return domEach(this, function(i, el) {
setProp(el, name, value.call(el, i, getProp(el, name)));
});
}

return domEach(this, function(i, el) {
if (!isTag(el)) return;

if (typeof name === 'object') {

_.each(name, function(val, name) {
setProp(el, name, val);
});

} else {
setProp(el, name, value);
}
});

}
};

var setData = function(el, name, value) {
if (!el.data) {
el.data = {};
Expand Down
55 changes: 55 additions & 0 deletions test/api/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,61 @@ describe('$(...)', function() {
});
});

describe('.prop', function () {
var $,
checkbox;

beforeEach(function () {
$ = cheerio.load(inputs);
checkbox = $('input[name=checkbox_on]');
});

it('(valid key) : valid prop should get value', function() {
expect(checkbox.prop('checked')).to.equal(true);
checkbox.css('display', 'none');
expect(checkbox.prop('style').display).to.equal('none');
expect(checkbox.prop('style')).to.have.length(1);
expect(checkbox.prop('style')).to.contain('display');
expect(checkbox.prop('tagName')).to.equal('INPUT');
expect(checkbox.prop('nodeName')).to.equal('INPUT');
});

it('(invalid key) : invalid prop should get undefined', function() {
var attr = checkbox.prop('lol');
expect(attr).to.be(undefined);
});

it('(key, value) : should set prop', function() {
expect(checkbox.prop('checked')).to.equal(true);
checkbox.prop('checked', false);
expect(checkbox.prop('checked')).to.equal(false);
checkbox.prop('checked', true);
expect(checkbox.prop('checked')).to.equal(true);
});

it('(map) : object map should set multiple props', function() {
checkbox.prop({
id: 'check',
checked: false
});
expect(checkbox.prop('id')).to.equal('check');
expect(checkbox.prop('checked')).to.equal(false);
});

it('(key, function) : should call the function and update the prop with the return value', function() {
checkbox.prop('checked', function(index, value) {
expect(index).to.equal(0);
expect(value).to.equal(true);
return false;
});
expect(checkbox.prop('checked')).to.equal(false);
});

it('(key, value) : should support chaining after setting props', function() {
expect(checkbox.prop('checked', false)).to.equal(checkbox);
});
});

describe('.data', function() {

beforeEach(function() {
Expand Down

0 comments on commit c04ead1

Please sign in to comment.