import { Attr , Attribute , SerializedAttribute } from "./Attribute" ; import { attrValues } from "./attrValues" ; import { SourceLocation } from "./SourceLocation" ; import { POSITION_UNKNOWN , SourcePosition } from "./SourceLocation" ; import { SerializedTagname , Tag , Tagname } from "./Tagname" ; export type Selectable = Element | Tag | Attr ; export interface ElementInfo < TagnameType = Tag , AttributeType = Attr > { sourceLocation ? : SourceLocation ; tagname : TagnameType ; attributes : Array < AttributeType > ; id ? : string ; } export type SerializedElementInfo = ElementInfo < SerializedTagname , SerializedAttribute > ; export class Element implements ElementInfo { sourceLocation : SourceLocation ; tagname : Tag ; attributes : Array < Attr > ; id : string | undefined ; constructor ( tagname , attributes , sourceLocation ? , id ? ) { this . tagname = tagname ; this . attributes = attributes ; this . sourceLocation = sourceLocation || { start : POSITION_UNKNOWN } ; this . id = id ; } static fromElementInfo ( info ) { return new Element ( info . tagname , info . attributes , info . sourceLocation , info . id ) ; } serialize ( ) { let e = { tagname : this . tagname . toJSON ( ) , attributes : this . attributes . map ( a => a . toJSON ( ) ) , } ; if ( this . sourceLocation && this . sourceLocation . start . line >= 0 ) { e . sourceLocation = this . sourceLocation ; } return e ; } toString ( ) { let parts = [ ] ; parts . push ( this . tagname ) ; for ( let attr of this . attributes ) { parts . push ( attr ) ; } return `<${ parts . join ( " " ) }>` ; } } param unknownAttrs param startPosition param endPosition export function unknownElement ( unknownAttrs = [ "class" , "id" ] , startPosition ? , endPosition ? ) { let tagname = new Tagname ( attrValues . unknown ( ) ) ; let attrs = new Array < Attribute > ( ) ; for ( let attrName of unknownAttrs ) { attrs . push ( new Attribute ( attrName , attrValues . unknown ( ) ) ) ; } let loc = undefined ; if ( startPosition ) { loc = { start : startPosition } ; if ( endPosition ) { loc . end = endPosition ; } } return new Element ( tagname , attrs , loc ) ; }