From cb219a0d3a2b056bf34ca9cd522657f4108e00dc Mon Sep 17 00:00:00 2001 From: Minas Keshishyan Date: Mon, 3 Feb 2020 15:17:49 +0400 Subject: [PATCH] add getAttribute --- src/nodes/html.ts | 8 ++++++++ test/html.js | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/nodes/html.ts b/src/nodes/html.ts index 3d08e44..9374a4f 100644 --- a/src/nodes/html.ts +++ b/src/nodes/html.ts @@ -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 diff --git a/test/html.js b/test/html.js index dd34cf4..b5b0c7c 100644 --- a/test/html.js +++ b/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('

'); + root.firstChild.getAttribute('a').should.eql('a1b'); + }); + + it('should return null when there is no such attribute', function () { + var root = parseHTML('

'); + should.equal(root.firstChild.getAttribute('b'), null); + }); + }); + describe('#setAttribute', function () { it('should edit the attributes of the element', function () { var root = parseHTML('

');