add getAttribute

This commit is contained in:
Minas Keshishyan 2020-02-03 15:17:49 +04:00
parent 9257f300b9
commit cb219a0d3a
2 changed files with 20 additions and 0 deletions

View file

@ -431,6 +431,14 @@ export default class HTMLElement extends Node {
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
* @param {string} key The attribute name

View file

@ -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 () {
it('should edit the attributes of the element', function () {
var root = parseHTML('<p a=12></p>');