Commit 5e70aee1 authored by David Sveningsson's avatar David Sveningsson
Browse files

feat(dom): implement {first,last}ElementChild accessors

parent 0ed11ed2
......@@ -169,6 +169,40 @@ describe("HtmlElement", () => {
expect(el.id).toBeNull();
});
describe("{first,last}ChildElement", () => {
let root: HtmlElement;
let a: HtmlElement;
let b: HtmlElement;
let c: HtmlElement;
beforeAll(() => {
root = new HtmlElement("root", null, NodeClosed.EndTag, null, location);
a = new HtmlElement("a", root, NodeClosed.EndTag, null, location);
b = new HtmlElement("b", root, NodeClosed.EndTag, null, location);
c = new HtmlElement("c", root, NodeClosed.EndTag, null, location);
});
it("firstElementChild should return first child element", () => {
expect.assertions(1);
expect(root.firstElementChild).toEqual(a);
});
it("firstElementChild should return null if there are no child elements", () => {
expect.assertions(1);
expect(b.firstElementChild).toBeNull();
});
it("lastElementChild should return last child element", () => {
expect.assertions(1);
expect(root.lastElementChild).toEqual(c);
});
it("lastElementChild should return null if there are no child elements", () => {
expect.assertions(1);
expect(b.lastElementChild).toBeNull();
});
});
it("previousSibling should return node before this node", () => {
expect.assertions(3);
const root = new HtmlElement("root", null, NodeClosed.EndTag, null, location);
......
......@@ -381,6 +381,22 @@ export class HtmlElement extends DOMNode {
return this.getAttributeValue("id");
}
/**
* Returns the first child element or null if there are no child elements.
*/
public get firstElementChild(): HtmlElement | null {
const children = this.childElements;
return children.length > 0 ? children[0] : null;
}
/**
* Returns the last child element or null if there are no child elements.
*/
public get lastElementChild(): HtmlElement | null {
const children = this.childElements;
return children.length > 0 ? children[children.length - 1] : null;
}
public get siblings(): HtmlElement[] {
return this.parent ? this.parent.childElements : [this];
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment