import { Attr , Attribute as AttributeUNS , AttributeNS , AttributeValueChoice , ValueAbsent , ValueConstant , } from "@opticss/element-analysis" ; import { ObjectDictionary , assertNever } from "@opticss/util" ; import { ATTR_PRESENT , AttrToken } from "../BlockSyntax" ; import { OutputMode , ResolvedConfiguration } from "../configuration" ; import { AttrValue } from "./AttrValue" ; import { Block } from "./Block" ; import { BlockClass } from "./BlockClass" ; import { Inheritable } from "./Inheritable" ; export class Attribute extends Inheritable < Attribute , Block , BlockClass , AttrValue , AttrToken > { private _hasValues = false ; private _presenceRule : AttrValue | undefined ; private _sourceAttributes : Attr [ ] | undefined ; protected get ChildConstructor ( ) : typeof AttrValue { return AttrValue ; } protected tokenToUid ( token ) { return token . name ; } public get name ( ) : string { return this . token . name ; } public get namespace ( ) : string | null { return this . token . namespace || null ; } returns get hasValues ( ) : boolean { return this . _hasValues ; } returns get isBooleanAttribute ( ) : boolean { return ! this . _hasValues ; } returns get presenceRule ( ) : AttrValue | undefined { return this . _presenceRule ; } returns get blockClass ( ) : BlockClass { return this . parent ; } returns values ( ) { return this . children ( ) ; } resolvedValues ( ) { return this . resolveChildren ( ) ; } returns valuesHash ( ) { return this . childrenHash ( ) ; } resolveValuesHash ( ) { return this . resolveChildrenHash ( ) ; } returns valuesMap ( ) { return this . childrenMap ( ) ; } resolveValuesMap ( ) { return this . resolveChildrenMap ( ) ; } param name returns ensureValue ( name = ATTR_PRESENT ) { let value = this . ensureChild ( name ) ; if ( name !== ATTR_PRESENT ) { this . _hasValues = true ; } else { this . _presenceRule = value ; } return value ; } param name returns getValue ( name = ATTR_PRESENT ) { return this . getChild ( name ) ; } param name returns resolveValue ( name = ATTR_PRESENT ) { return this . resolveChild ( name ) ; } returns hasResolvedValues ( ) { return ! ! ( this . hasValues || this . base && this . base . hasResolvedValues ( ) ) ; } returns resolveValues ( ) { let resolved = new Map ( [ ... this . _children ] ) ; for ( let base of this . resolveInheritance ( ) ) { if ( base . _children . size ) { resolved = new Map ( [ ... resolved , ... base . _children ] ) ; } } return resolved ; } param scope returns asSource ( scope ? ) { let namespace = this . token . namespace ? `${ this . token . namespace }|` : "" ; return this . blockClass . asSource ( scope ) + `[${ namespace }${ this . token . name }]` ; } /**
     * Emit analysis attributes for the `AttrValue`s this
     * `Attribute` represents in their authored source format.
     */ asSourceAttributes ( ) { if ( ! this . _sourceAttributes ) { this . _sourceAttributes = [ ] ; this . _sourceAttributes . push ( ... this . blockClass . asSourceAttributes ( ) ) ; let value ; if ( this . hasValues ) { let values = new Array < ValueConstant > ( ) ; for ( let value of this . resolvedValues ( ) ) { values . push ( { constant : value . value } ) ; } value = { oneOf : values } ; } else { value = { absent : true } ; } if ( this . namespace ) { this . _sourceAttributes . push ( new AttributeNS ( this . namespace , this . name , value ) ) ; } else { this . _sourceAttributes . push ( new AttributeUNS ( this . name , value ) ) ; } } return this . _sourceAttributes ; } param config returns cssClass ( config , reservedClassNames ) { switch ( config . outputMode ) { case OutputMode . BEM : return `${ this . blockClass . cssClass ( config , reservedClassNames ) }--${ this . token . name }` ; case OutputMode . BEM_UNIQUE : return `${ this . blockClass . cssClass ( config , reservedClassNames ) }--${ this . token . name }` ; default : return assertNever ( config . outputMode ) ; } } returns all ( ) { return [ this ] ; } } param o returns export function isAttribute ( o ) { return o instanceof Attribute ; }