import { AttributeValueParser } from "@opticss/attr-analysis-dsl" ; import { Attribute , AttributeNS , POSITION_UNKNOWN , Tagname } from "@opticss/element-analysis" ; import { TemplateAnalysis } from "@opticss/template-api" ; import * as parse5 from "parse5-sax-parser" ; import { TestTemplate } from "./TestTemplate" ; export class SimpleAnalyzer { template : TestTemplate ; includeSourceInformation : boolean ; private valueParser : AttributeValueParser ; param template param includeSourceInformation constructor ( template , includeSourceInformation = false ) { this . template = template ; this . includeSourceInformation = includeSourceInformation ; this . valueParser = new AttributeValueParser ( template . plainHtml ) ; } returns analyze ( ) { let analysis = new TemplateAnalysis < "TestTemplate" > ( this . template ) ; const parser = new parse5 ( { sourceCodeLocationInfo : this . includeSourceInformation } ) ; parser . on ( "startTag" , ( token ) => { let { tagName , attrs , sourceCodeLocation : location } = token ; let startLocation = location ? { line : location . startLine , column : location . startCol } : POSITION_UNKNOWN ; let endLocation = location ? { line : location . startLine , column : location . startCol + location . endOffset } : POSITION_UNKNOWN ; analysis . startElement ( new Tagname ( { constant : tagName } ) , startLocation ) ; attrs . forEach ( attr => { let attrValue = this . valueParser . parse ( attr . namespace , attr . name , attr . value ) ; if ( attr . namespace ) { analysis . addAttribute ( new AttributeNS ( attr . namespace , attr . name , attrValue ) ) ; } else { analysis . addAttribute ( new Attribute ( attr . name , attrValue ) ) ; } } ) ; analysis . endElement ( endLocation ) ; } ) ; return new Promise ( ( resolve , reject ) => { parser . write ( this . template . contents , ( err ) => { if ( err ) { reject ( err ) ; } else { resolve ( analysis ) ; } } ) ; } ) ; } }