diff --git a/d.ts/homey.d.ts b/d.ts/homey.d.ts index 51933f4..6b1aaa0 100644 --- a/d.ts/homey.d.ts +++ b/d.ts/homey.d.ts @@ -57,6 +57,7 @@ export function queryAll(element: Element, selector: CSSSelector): Elements; export function closest(element: Element, condition: CSSSelector | Predicate): Element | null; export function getHtml(element: Element): string; export function getText(element: Node): string; +export function hasAttr(element: Element, attribute: string): boolean; export function matches(element: Element, selector: CSSSelector): boolean; export function parent(element: Element): Element | null; export function parentBy(element: Element, condition: CSSSelector | Predicate): Element | null; diff --git a/spec/traversing/has-attr.spec.js b/spec/traversing/has-attr.spec.js new file mode 100644 index 0000000..a6468db --- /dev/null +++ b/spec/traversing/has-attr.spec.js @@ -0,0 +1,21 @@ +import { hasAttr } from "../../src"; + +describe("hasAttr", () => { + afterEach(clearFixtures); + + it("returns true if element has a given attribute", () => { + useFixture(`
`); + + const subject = document.querySelector(".root"); + + expect(hasAttr(subject, "class")).toBe(true); + }); + + it("returns false if element hasn't a given attribute", () => { + useFixture(``); + + const subject = document.querySelector(".root"); + + expect(hasAttr(subject, "id")).toBe(false); + }); +}); diff --git a/src/index.js b/src/index.js index 4130569..c6c4a85 100644 --- a/src/index.js +++ b/src/index.js @@ -35,8 +35,10 @@ import setText from "./manipulation/set-text"; import query from "./quering/query"; import queryAll from "./quering/query-all"; import closest from "./traversing/closest"; +import getAttr from "./traversing/get-attr"; import getHtml from "./traversing/get-html"; import getText from "./traversing/get-text"; +import hasAttr from "./traversing/has-attr"; import matches from "./traversing/matches"; import parent from "./traversing/parent"; import parentBy from "./traversing/parent-by"; @@ -81,8 +83,10 @@ export { query, queryAll, closest, + getAttr, getHtml, getText, + hasAttr, matches, parent, parentBy, diff --git a/src/traversing/has-attr.js b/src/traversing/has-attr.js new file mode 100644 index 0000000..22cb259 --- /dev/null +++ b/src/traversing/has-attr.js @@ -0,0 +1,7 @@ +/* @flow */ + +function hasAttr(element: Element, attribute: string): boolean { + return element.hasAttribute(attribute); +} + +export default hasAttr;