import { Hash } from "@siteimprove/alfa-hash" ; import { Parser } from "@siteimprove/alfa-parser" ; import { Err , Result } from "@siteimprove/alfa-result" ; import { Slice } from "@siteimprove/alfa-slice" ; import { Value } from "../value" ; import { Token } from "../syntax/token" ; import { Box } from "./box" ; import { Keyword } from "./keyword" ; import { Circle } from "./shape/circle" ; import { Inset } from "./shape/inset" ; import { Rectangle } from "./shape/rectangle" ; import { Ellipse } from "./shape/ellipse" ; import { Polygon } from "./shape/polygon" ; const { either } = Parser ; public export class Shape < S extends Shape . Basic = Shape . Basic , B extends Box . Geometry = Box . Geometry > extends Value < "shape" > { public static of < S extends Shape . Basic = Shape . Basic , B extends Box . Geometry = Box . Geometry > ( shape , box ) { return new Shape ( shape , box ) ; } private readonly _shape : S ; private readonly _box : B ; private constructor ( shape , box ) { super ( ) ; this . _shape = shape ; this . _box = box ; } public get type ( ) : "shape" { return "shape" ; } public get shape ( ) : S { return this . _shape ; } public get box ( ) : B { return this . _box ; } public equals ( value ) ; public equals ( value ) ; public equals ( value ) { return ( value instanceof Shape && value . _shape . equals ( this . _shape ) && value . _box . equals ( this . _box ) ) ; } public hash ( hash ) { hash . writeHashable ( this . _shape ) . writeHashable ( this . _box ) ; } public toJSON ( ) { return { type : "shape" , shape : this . _shape . toJSON ( ) , box : this . _box . toJSON ( ) , } ; } public toString ( ) { return `${ this . _shape . toString ( ) } ${ this . _box . toString ( ) }` ; } } public export namespace Shape { /**
     *  https export type Basic = Circle | Ellipse | Inset | Polygon | Rectangle ; export interface JSON extends Value . JSON < "shape" > { shape : Circle . JSON | Ellipse . JSON | Inset . JSON | Polygon . JSON | Rectangle . JSON ; box : Box . Geometry . JSON ; } remarks const parseBasicShape = either < Slice < Token > , Circle | Ellipse | Inset | Polygon , string > ( Circle . parse , Ellipse . parse , Inset . parse , Polygon . parse ) ; remarks export const parse = ( input ) => { let shape ; let box ; const skipWhitespace = ( ) => { for ( const [ remainder ] of Token . parseWhitespace ( input ) ) { input = remainder ; } } ; while ( true ) { skipWhitespace ( ) ; if ( shape === undefined ) { const result = parseBasicShape ( input ) ; if ( result . isOk ( ) ) { [ input , shape ] = result . get ( ) ; continue ; } } if ( box === undefined ) { const result = Box . parseGeometry ( input ) ; if ( result . isOk ( ) ) { [ input , box ] = result . get ( ) ; continue ; } } break ; } if ( shape === undefined ) { return Err . of ( "Expected a shape" ) ; } return Result . of ( [ input , Shape . of ( shape , box ?? Keyword . of ( "border-box" ) ) ] ) ; } ; }