import { Hash } from "@siteimprove/alfa-hash" ; import { Parser } from "@siteimprove/alfa-parser" ; import { Slice } from "@siteimprove/alfa-slice" ; import { Token } from "../syntax/token" ; import { Converter , Convertible } from "./converter" ; import { Unit } from "./unit" ; import { Dimension } from "./dimension" ; const { map , either } = Parser ; /**
 *  https public export class Length < U extends Unit . Length = Unit . Length > extends Dimension < "length" , Unit . Length , U > implements Convertible < Unit . Length . Absolute > { public static of < U extends Unit . Length > ( value , unit ) { return new Length ( value , unit ) ; } private constructor ( value , unit ) { super ( value , unit ) ; } public get type ( ) : "length" { return "length" ; } public get canonicalUnit ( ) : "px" { return "px" ; } public hasUnit < U extends Unit . Length > ( unit ) { return ( this . _unit as Unit . Length ) === unit ; } public withUnit < U extends Unit . Length > ( unit ) { if ( this . hasUnit ( unit ) ) { return this ; } if ( Unit . isAbsoluteLength ( unit ) && Unit . isAbsoluteLength ( this . _unit ) ) { return Length . of ( Converter . length ( this . _value , this . _unit , unit ) , unit ) ; } throw new Error ( `Cannot convert ${ this . _unit } to ${ unit }` ) ; } public isRelative ( ) { return Unit . isRelativeLength ( this . _unit ) ; } public isFontRelative ( ) { return Unit . isFontRelativeLength ( this . _unit ) ; } public isViewportRelative ( ) { return Unit . isViewportRelativeLength ( this . _unit ) ; } public isAbsolute ( ) { return Unit . isAbsoluteLength ( this . _unit ) ; } public equals ( value ) { return value instanceof Length && super . equals ( value ) ; } public hash ( hash ) { super . hash ( hash ) ; hash . writeString ( this . _unit ) ; } public toJSON ( ) { return { type : "length" , value : this . _value , unit : this . _unit , } ; } public toString ( ) { return `${ this . _value }${ this . _unit }` ; } } public export namespace Length { export interface JSON < U extends Unit . Length = Unit . Length > extends Dimension . JSON < "length" > { unit : U ; } export function isLength ( value ) { return value instanceof Length ; } export const parse = either ( map ( Token . parseDimension ( ( dimension ) => Unit . isLength ( dimension . unit ) ) , ( dimension ) => Length . of ( dimension . value , dimension . unit as Unit . Length ) ) , map ( Token . parseNumber ( ( number ) => number . isInteger && number . value === 0 ) , ( ) => Length . of ( 0 , "px" ) ) ) ; export function isZero ( length ) { return length . value === 0 ; } }