Skip to content

Commit

Permalink
Merge pull request #5 from PolymerLabs/hasattr
Browse files Browse the repository at this point in the history
Add hasAttribute()
  • Loading branch information
dfreedm committed Mar 16, 2015
2 parents c3630ba + 84a02e4 commit 6244c98
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
9 changes: 9 additions & 0 deletions dom5.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ function getAttributeIndex(element, name) {
return -1;
}

/**
* @returns {boolean} `true` iff [element] has the attribute [name], `false`
* otherwise.
*/
function hasAttribute(element, name) {
return getAttributeIndex(element, name) !== -1;
}

function getAttribute(element, name) {
var i = getAttributeIndex(element, name);
if (i > -1) {
Expand Down Expand Up @@ -149,6 +157,7 @@ function queryAll(node, predicate, matches) {

module.exports = {
getAttribute: getAttribute,
hasAttribute: hasAttribute,
setAttribute: setAttribute,
isElement: isElement,
query: query,
Expand Down
43 changes: 42 additions & 1 deletion test/dom5_test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/**
* @license
* Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/

var chai = require('chai');
var Parse5 = require('parse5');
var dom5 = require('../dom5');
Expand All @@ -7,7 +17,7 @@ var parser = new Parse5.Parser();

suite('dom5', function() {

var docText = "<!DOCTYPE html><div id='A'>a1<div bar='b1' bar='b2'>b1</div>a2</div>";
var docText = "<!DOCTYPE html><div id='A' qux>a1<div bar='b1' bar='b2'>b1</div>a2</div>";
var doc = null;

setup(function () {
Expand Down Expand Up @@ -40,6 +50,37 @@ suite('dom5', function() {

});

suite('hasAttribute', function() {

test('returns false for a non-set attribute', function() {
var divA = doc.childNodes[1].childNodes[1].childNodes[0];
assert.equal(dom5.hasAttribute(divA, 'foo'), false);
});

test('returns true for a set attribute', function() {
var divA = doc.childNodes[1].childNodes[1].childNodes[0];
assert.equal(dom5.hasAttribute(divA, 'id'), true);
});

test('returns true for a doubly set attribute', function() {
var divB = doc.childNodes[1].childNodes[1].childNodes[0].childNodes[1];
assert.equal(dom5.hasAttribute(divB, 'bar'), true);
});

test('returns true for attribute with no value', function() {
var divA = doc.childNodes[1].childNodes[1].childNodes[0];
assert.equal(dom5.hasAttribute(divA, 'qux'), true);
});

test('throws when called on a text node', function() {
var text = doc.childNodes[1].childNodes[1].childNodes[0].childNodes[0];
assert.throws(function () {
dom5.hasAttribute(text, 'bar');
});
});

});

suite('setAttribute', function() {

test('sets a non-set attribute', function() {
Expand Down

0 comments on commit 6244c98

Please sign in to comment.