import { Iterable } from "@siteimprove/alfa-iterable" ; import { None , Option } from "@siteimprove/alfa-option" ; import { Predicate } from "@siteimprove/alfa-predicate" ; import { Refinement } from "@siteimprove/alfa-refinement" ; import * as dom from "@siteimprove/alfa-dom" ; import { Attribute } from "../attribute" ; import { Name } from "../name" ; import { Node } from "../node" ; import { Role } from "../role" ; public export class Element extends Node { public static of ( owner , role = None , name = None , attributes = [ ] , children = [ ] ) { return new Element ( owner , role , name , Array . from ( attributes ) , Array . from ( children ) ) ; } private readonly _role : Option < Role > ; private readonly _name : Option < Name > ; private readonly _attributes : Array < Attribute > ; private constructor ( owner , role , name , attributes , children ) { super ( owner , children ) ; this . _role = role ; this . _name = name ; this . _attributes = attributes ; } public get role ( ) : Option < Role > { return this . _role ; } public get name ( ) : Option < Name > { return this . _name ; } public get attributes ( ) : ReadonlyArray < Attribute > { return this . _attributes ; } public attribute < N extends Attribute . Name > ( refinement ) ; public attribute ( predicate ) ; public attribute < N extends Attribute . Name > ( predicate ) ; public attribute ( predicate ) { return Iterable . find ( this . _attributes , typeof predicate === "string" ? ( attribute ) => attribute . name === predicate : predicate ) ; } public clone ( ) { return new Element ( this . _node , this . _role , this . _name , this . _attributes , this . _children . map ( ( child ) => child . clone ( ) ) ) ; } public isIgnored ( ) { return false ; } public toJSON ( ) { return { type : "element" , node : this . _node . path ( ) , role : this . _role . map ( ( role ) => role . name ) . getOr ( null ) , name : this . _name . map ( ( name ) => name . value ) . getOr ( null ) , attributes : this . _attributes . map ( ( attribute ) => attribute . toJSON ( ) ) , children : this . _children . map ( ( child ) => child . toJSON ( ) ) , } ; } public toString ( ) { return [ [ this . _role . map ( ( role ) => role . name ) . getOr ( "element" ) , ... this . _name . map ( ( name ) => `"${ name }"` ) , ] . join ( " " ) , ... this . _children . map ( ( child ) => indent ( child . toString ( ) ) ) , ] . join ( "\n" ) ; } } public export namespace Element { export interface JSON extends Node . JSON { type : "element" ; role : string | null ; name : string | null ; attributes : Array < Attribute . JSON > ; } } function indent ( input ) { return input . replace ( /^/gm , "  " ) ; }