import HTMLElement from '../html-element/HTMLElement'; import IHTMLElement from '../html-element/IHTMLElement'; import IHTMLFormElement from '../html-form-element/IHTMLFormElement'; import IHTMLLabelElement from './IHTMLLabelElement'; /** * HTML Label Element. * * Reference: * https://developer.mozilla.org/en-US/docs/Web/API/HTMLLabelElement. */ export default class HTMLLabelElement extends HTMLElement implements IHTMLLabelElement { /** * Returns a string containing the ID of the labeled control. This reflects the "for" attribute. * * @returns ID of the labeled control. */ public get htmlFor(): string { const htmlFor = this.getAttributeNS(null, 'for'); if (htmlFor !== null) { return htmlFor; } return htmlFor !== null ? htmlFor : ''; } /** * Sets a string containing the ID of the labeled control. This reflects the "for" attribute. * * @param htmlFor ID of the labeled control. */ public set htmlFor(htmlFor: string) { this.setAttributeNS(null, 'for', htmlFor); } /** * Returns an HTML element representing the control with which the label is associated. * * @returns Control element. */ public get control(): IHTMLElement { const htmlFor = this.htmlFor; if (htmlFor) { return this.ownerDocument.getElementById(htmlFor); } for (const child of this.children) { if (child.tagName === 'INPUT') { return child; } } return null; } /** * Returns the parent form element. * * @returns Form. */ public get form(): IHTMLFormElement { let parent = this.parentNode; while (parent && parent.tagName !== 'FORM') { parent = parent.parentNode; } return parent; } /** * Clones a node. * * @override * @param [deep=false] "true" to clone deep. * @returns Cloned node. */ public cloneNode(deep = false): IHTMLLabelElement { return super.cloneNode(deep); } }