From 0a308a7f0ed8fab77d7093383b76abaaae6e46f5 Mon Sep 17 00:00:00 2001 From: Charlotte Gore Date: Sun, 20 Oct 2013 13:20:59 +0100 Subject: [PATCH 1/4] Added support for setting attributes with an object --- component.json | 2 +- index.js | 15 ++++++++++++++- test/dom.js | 19 +++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/component.json b/component.json index 7a9c97f..4ce20cc 100644 --- a/component.json +++ b/component.json @@ -1,6 +1,6 @@ { "name": "dom", - "version": "0.9.0", + "version": "0.10.0", "description": "DOM traversal, manipulation and events aggregate library", "keywords": [ "html", diff --git a/index.js b/index.js index 6b38641..14da1a0 100644 --- a/index.js +++ b/index.js @@ -150,11 +150,24 @@ List.prototype.remove = function(){ */ List.prototype.attr = function(name, val){ + + var obj; // get - if (1 == arguments.length) { + if (1 == arguments.length && "string" === typeof name) { return this.els[0] && this.els[0].getAttribute(name); } + // set via object + if(1 == arguments.length && "object" === typeof name){ + obj = name; + for(var attr in obj){ + if(obj.hasOwnProperty(attr)){ + this.attr(attr, obj[attr]); + } + } + return this; + } + // remove if (null == val) { return this.removeAttr(name); diff --git a/test/dom.js b/test/dom.js index f1a6338..fc29f20 100644 --- a/test/dom.js +++ b/test/dom.js @@ -421,6 +421,25 @@ describe('.attr()', function(){ assert('logo' == list.attr('id')); }) }) + + describe('with an object of key value pairs', function(){ + it('should set the attributes', function(){ + var list = dom('
').find('a'); + var ret = list.attr({ href : '#', rel : 'rels/attr-test'}); + assert(ret == list); + assert('#' == list.get(0).getAttribute('href')); + assert('#' == list.get(1).getAttribute('href')); + assert('rels/attr-test' == list.get(0).getAttribute('rel')); + assert('rels/attr-test' == list.get(1).getAttribute('rel')); + }) + + it('should unset an attribute', function(){ + var list = dom('
').find('a'); + list.attr({ href : null }); + assert(null == list.get(0).getAttribute('href')); + }) + + }) }) describe('.prop()', function(){ From ff61f25f65386468312b123f326a1c9bcf55a888 Mon Sep 17 00:00:00 2001 From: Charlotte Gore Date: Sun, 20 Oct 2013 13:24:57 +0100 Subject: [PATCH 2/4] Updated docs --- Readme.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Readme.md b/Readme.md index 32ac9c3..07d01d4 100644 --- a/Readme.md +++ b/Readme.md @@ -160,6 +160,17 @@ var url = dom('img').attr('src'); dom('img').attr('src', 'image/of/maru.jpg'); ``` +### .attr(obj) + + Set attributes with an object + +```js +dom('a').attr({ + href : 'http://github.com', + rel : 'source' +}); +``` + ### .ATTR() Attribute getters. These are functionally equivalent From cc5f814ab41ae2b828d1e6239c0205ac2d5ca890 Mon Sep 17 00:00:00 2001 From: Charlotte Gore Date: Sun, 20 Oct 2013 14:01:42 +0100 Subject: [PATCH 3/4] reverted version number increment and applied code style guide --- component.json | 2 +- index.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/component.json b/component.json index 4ce20cc..7a9c97f 100644 --- a/component.json +++ b/component.json @@ -1,6 +1,6 @@ { "name": "dom", - "version": "0.10.0", + "version": "0.9.0", "description": "DOM traversal, manipulation and events aggregate library", "keywords": [ "html", diff --git a/index.js b/index.js index 14da1a0..e76ba79 100644 --- a/index.js +++ b/index.js @@ -158,10 +158,10 @@ List.prototype.attr = function(name, val){ } // set via object - if(1 == arguments.length && "object" === typeof name){ + if (1 == arguments.length && "object" === typeof name){ obj = name; - for(var attr in obj){ - if(obj.hasOwnProperty(attr)){ + for (var attr in obj){ + if (obj.hasOwnProperty(attr)){ this.attr(attr, obj[attr]); } } From 652c3d008d379f663a01e5a5bb0179709de344c5 Mon Sep 17 00:00:00 2001 From: Charlotte Gore Date: Sun, 20 Oct 2013 19:16:37 +0100 Subject: [PATCH 4/4] More "house style" stuff --- index.js | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index e76ba79..dc4dced 100644 --- a/index.js +++ b/index.js @@ -151,23 +151,19 @@ List.prototype.remove = function(){ List.prototype.attr = function(name, val){ - var obj; - // get - if (1 == arguments.length && "string" === typeof name) { - return this.els[0] && this.els[0].getAttribute(name); - } - // set via object - if (1 == arguments.length && "object" === typeof name){ - obj = name; - for (var attr in obj){ - if (obj.hasOwnProperty(attr)){ - this.attr(attr, obj[attr]); - } + if ("object" == typeof name){ + for (var attr in name) { + this.attr(attr, name[attr]); } return this; } + // get + if (1 == arguments.length) { + return this.els[0] && this.els[0].getAttribute(name); + } + // remove if (null == val) { return this.removeAttr(name);