mirror of
https://github.com/danbulant/node-html-parser
synced 2026-06-17 13:41:17 +00:00
add getAttribute
This commit is contained in:
parent
9257f300b9
commit
cb219a0d3a
2 changed files with 20 additions and 0 deletions
|
|
@ -431,6 +431,14 @@ export default class HTMLElement extends Node {
|
||||||
return attrs;
|
return attrs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get an attribute
|
||||||
|
* @return {string} value of the attribute
|
||||||
|
*/
|
||||||
|
getAttribute(key: string) {
|
||||||
|
return this.attributes[key] || null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set an attribute value to the HTMLElement
|
* Set an attribute value to the HTMLElement
|
||||||
* @param {string} key The attribute name
|
* @param {string} key The attribute name
|
||||||
|
|
|
||||||
12
test/html.js
12
test/html.js
|
|
@ -319,6 +319,18 @@ describe('HTML Parser', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('#getAttribute', function () {
|
||||||
|
it('should return value of the attribute', function () {
|
||||||
|
var root = parseHTML('<p a="a1b"></p>');
|
||||||
|
root.firstChild.getAttribute('a').should.eql('a1b');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return null when there is no such attribute', function () {
|
||||||
|
var root = parseHTML('<p></p>');
|
||||||
|
should.equal(root.firstChild.getAttribute('b'), null);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('#setAttribute', function () {
|
describe('#setAttribute', function () {
|
||||||
it('should edit the attributes of the element', function () {
|
it('should edit the attributes of the element', function () {
|
||||||
var root = parseHTML('<p a=12></p>');
|
var root = parseHTML('<p a=12></p>');
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue