Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #626 from jledentu/namednodemap
#308@minor: Implement NamedNodeMap and use it for Element.attributes
- Loading branch information
Showing
10 changed files
with
484 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import IAttr from '../nodes/attr/IAttr'; | ||
|
||
/** | ||
* NamedNodeMap. | ||
* | ||
* Reference: | ||
* https://developer.mozilla.org/en-US/docs/Web/API/NamedNodeMap. | ||
*/ | ||
export default interface INamedNodeMap extends Iterable<IAttr> { | ||
[index: number]: IAttr; | ||
[Symbol.toStringTag]: string; | ||
readonly length: number; | ||
|
||
/** | ||
* Returns attribute by index. | ||
* | ||
* @param index Index. | ||
*/ | ||
item: (index: number) => IAttr; | ||
|
||
/** | ||
* Returns attribute by name. | ||
* | ||
* @param qualifiedName Name. | ||
* @returns Attribute. | ||
*/ | ||
getNamedItem: (qualifiedName: string) => IAttr; | ||
|
||
/** | ||
* Returns attribute by name and namespace. | ||
* | ||
* @param namespace Namespace. | ||
* @param localName Local name of the attribute. | ||
* @returns Attribute. | ||
*/ | ||
getNamedItemNS: (namespace: string, localName: string) => IAttr; | ||
|
||
/** | ||
* Adds a new attribute node. | ||
* | ||
* @param attr Attribute. | ||
* @returns Replaced attribute. | ||
*/ | ||
setNamedItem: (attr: IAttr) => IAttr; | ||
|
||
/** | ||
* Adds a new namespaced attribute node. | ||
* | ||
* @param attr Attribute. | ||
* @returns Replaced attribute. | ||
*/ | ||
setNamedItemNS: (attr: IAttr) => IAttr; | ||
|
||
/** | ||
* Removes an attribute. | ||
* | ||
* @param qualifiedName Name of the attribute. | ||
* @returns Removed attribute. | ||
*/ | ||
removeNamedItem: (qualifiedName: string) => IAttr; | ||
|
||
/** | ||
* Removes a namespaced attribute. | ||
* | ||
* @param namespace Namespace. | ||
* @param localName Local name of the attribute. | ||
* @returns Removed attribute. | ||
*/ | ||
removeNamedItemNS: (namespace: string, localName: string) => IAttr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import type Element from '../nodes/element/Element'; | ||
import IAttr from '../nodes/attr/IAttr'; | ||
import INamedNodeMap from './INamedNodeMap'; | ||
|
||
/** | ||
* NamedNodeMap. | ||
* | ||
* Reference: | ||
* https://developer.mozilla.org/en-US/docs/Web/API/NamedNodeMap. | ||
*/ | ||
export default class NamedNodeMap implements INamedNodeMap { | ||
[index: number]: IAttr; | ||
|
||
/** | ||
* Reference to the element. | ||
*/ | ||
#ownerElement: Element; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param element Associated element. | ||
*/ | ||
constructor(element: Element) { | ||
this.#ownerElement = element; | ||
} | ||
|
||
/** | ||
* Returns string. | ||
* | ||
* @returns string. | ||
*/ | ||
public get [Symbol.toStringTag](): string { | ||
return this.constructor.name; | ||
} | ||
|
||
/** | ||
* Length. | ||
* | ||
* @returns Length. | ||
*/ | ||
public get length(): number { | ||
return Object.keys(this.#ownerElement._attributes).length; | ||
} | ||
|
||
/** | ||
* Returns attribute by index. | ||
* | ||
* @param index Index. | ||
*/ | ||
public item(index: number): IAttr | null { | ||
if (index < 0) { | ||
return null; | ||
} | ||
const attr = Object.values(this.#ownerElement._attributes)[index]; | ||
return attr ? attr : null; | ||
} | ||
|
||
/** | ||
* Returns attribute by name. | ||
* | ||
* @param qualifiedName Name. | ||
* @returns Attribute. | ||
*/ | ||
public getNamedItem(qualifiedName: string): IAttr | null { | ||
return this.#ownerElement.getAttributeNode(qualifiedName); | ||
} | ||
|
||
/** | ||
* Returns attribute by name and namespace. | ||
* | ||
* @param namespace Namespace. | ||
* @param localName Local name of the attribute. | ||
* @returns Attribute. | ||
*/ | ||
public getNamedItemNS(namespace: string, localName: string): IAttr | null { | ||
return this.#ownerElement.getAttributeNodeNS(namespace, localName); | ||
} | ||
|
||
/** | ||
* Adds a new attribute node. | ||
* | ||
* @param attr Attribute. | ||
* @returns Replaced attribute. | ||
*/ | ||
public setNamedItem(attr: IAttr): IAttr { | ||
return this.#ownerElement.setAttributeNode(attr); | ||
} | ||
|
||
/** | ||
* Adds a new namespaced attribute node. | ||
* | ||
* @param attr Attribute. | ||
* @returns Replaced attribute. | ||
*/ | ||
public setNamedItemNS(attr: IAttr): IAttr { | ||
return this.#ownerElement.setAttributeNodeNS(attr); | ||
} | ||
|
||
/** | ||
* Removes an attribute. | ||
* | ||
* @param qualifiedName Name of the attribute. | ||
* @returns Removed attribute. | ||
*/ | ||
public removeNamedItem(qualifiedName: string): IAttr | null { | ||
const attr = this.getNamedItem(qualifiedName); | ||
|
||
if (attr) { | ||
this.#ownerElement.removeAttributeNode(attr); | ||
} | ||
return attr; | ||
} | ||
|
||
/** | ||
* Removes a namespaced attribute. | ||
* | ||
* @param namespace Namespace. | ||
* @param localName Local name of the attribute. | ||
* @returns Removed attribute. | ||
*/ | ||
public removeNamedItemNS(namespace: string, localName: string): IAttr | null { | ||
const attr = this.getNamedItemNS(namespace, localName); | ||
|
||
if (attr) { | ||
this.#ownerElement.removeAttributeNode(attr); | ||
} | ||
return attr; | ||
} | ||
|
||
/** | ||
* Iterator. | ||
* | ||
* @returns Iterator. | ||
*/ | ||
public [Symbol.iterator](): Iterator<IAttr> { | ||
let index = -1; | ||
return { | ||
next: () => { | ||
index++; | ||
return { value: this.item(index), done: index >= this.length }; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.