import { Diagnostic } from "@siteimprove/alfa-act" ; import { Array } from "@siteimprove/alfa-array" ; import { Device } from "@siteimprove/alfa-device" ; import { Element } from "@siteimprove/alfa-dom" ; import { Equatable } from "@siteimprove/alfa-equatable" ; import { Hash , Hashable } from "@siteimprove/alfa-hash" ; import { Serializable } from "@siteimprove/alfa-json" ; import { Map } from "@siteimprove/alfa-map" ; import { Result } from "@siteimprove/alfa-result" ; import { Context } from "@siteimprove/alfa-selector" ; import { Sequence } from "@siteimprove/alfa-sequence" ; import { Property , Style } from "@siteimprove/alfa-style" ; import * as json from "@siteimprove/alfa-json" ; import { Contrast } from "../../src/common/diagnostic/contrast" ; import { Serialise } from "./serialise" ; type Name = Property . Name | Property . Shorthand . Name ; export class ElementDistinguishable implements Equatable , Hashable , Serializable { public static of ( style = [ ] , pairings = [ ] ) { return new ElementDistinguishable ( Map . from ( style ) , Array . from ( pairings ) ) ; } private readonly _style : Map < Name , string > ; private readonly _pairings : ReadonlyArray < Contrast . Pairing > ; private constructor ( style , pairings ) { this . _style = style ; this . _pairings = pairings ; } public get style ( ) : Map < Name , string > { return this . _style ; } public get pairings ( ) : ReadonlyArray < Contrast . Pairing > { return this . _pairings ; } public withStyle ( ... styles ) { return ElementDistinguishable . of ( [ ... this . _style , ... styles ] , this . _pairings ) ; } public equals ( value ) ; public equals ( value ) ; public equals ( value ) { return ( value instanceof ElementDistinguishable && value . _style . equals ( this . _style ) && Array . equals ( value . _pairings , this . _pairings ) ) ; } public hash ( hash ) { this . _style . hash ( hash ) ; Array . hash ( this . _pairings , hash ) ; } public toJSON ( ) { return { style : this . _style . toJSON ( ) , pairings : Array . toJSON ( this . _pairings ) , } ; } } export namespace ElementDistinguishable { export interface JSON { [ key ] : json . JSON ; style : Map . JSON < Name , string > ; pairings : Array < Contrast . Pairing . JSON > ; } export function from ( element , device , target , context = Context . empty ( ) , pairings ) { const style = Style . from ( element , device , context ) ; const border = ( [ "color" , "style" , "width" ] as const ) . map ( ( property ) => Serialise . borderShorthand ( style , property ) ) ; const cursor = context . isHovered ( target ) ? [ [ "cursor" , Serialise . getLonghand ( style , "cursor" ) ] as const ] : [ ] ; return ElementDistinguishable . of ( [ ... border , ... cursor , [ "color" , Serialise . getLonghand ( style , "color" ) ] as const , [ "font" , Serialise . font ( style ) ] as const , [ "vertical-align" , Serialise . getLonghand ( style , "vertical-align" ) , ] as const , [ "background" , Serialise . background ( style ) ] as const , [ "outline" , Serialise . outline ( style ) ] as const , [ "text-decoration" , Serialise . textDecoration ( style ) ] as const , ] . filter ( ( [ _ , value ] ) => value !== "" ) , pairings ) ; } } export class DistinguishingStyles extends Diagnostic { public static of ( message , defaultStyles = Sequence . empty ( ) , hoverStyles = Sequence . empty ( ) , focusStyles = Sequence . empty ( ) ) { return new DistinguishingStyles ( message , Sequence . from ( defaultStyles ) , Sequence . from ( hoverStyles ) , Sequence . from ( focusStyles ) ) ; } private readonly _defaultStyles : Sequence < Result < ElementDistinguishable > > ; private readonly _hoverStyles : Sequence < Result < ElementDistinguishable > > ; private readonly _focusStyles : Sequence < Result < ElementDistinguishable > > ; private constructor ( message , defaultStyles , hoverStyles , focusStyles ) { super ( message ) ; this . _defaultStyles = defaultStyles ; this . _hoverStyles = hoverStyles ; this . _focusStyles = focusStyles ; } public get defaultStyles ( ) : Iterable < Result < ElementDistinguishable > > { return this . _defaultStyles ; } public get hoverStyles ( ) : Iterable < Result < ElementDistinguishable > > { return this . _hoverStyles ; } public get focusStyles ( ) : Iterable < Result < ElementDistinguishable > > { return this . _focusStyles ; } public equals ( value ) ; public equals ( value ) ; public equals ( value ) { return ( value instanceof DistinguishingStyles && value . _defaultStyles . equals ( this . _defaultStyles ) && value . _hoverStyles . equals ( this . _hoverStyles ) && value . _focusStyles . equals ( this . _focusStyles ) ) ; } public toJSON ( ) { return { ... super . toJSON ( ) , defaultStyle : this . _defaultStyles . toJSON ( ) , hoverStyle : this . _hoverStyles . toJSON ( ) , focusStyle : this . _focusStyles . toJSON ( ) , } ; } } export namespace DistinguishingStyles { export interface JSON extends Diagnostic . JSON { defaultStyle : Sequence . JSON < Result < ElementDistinguishable > > ; hoverStyle : Sequence . JSON < Result < ElementDistinguishable > > ; focusStyle : Sequence . JSON < Result < ElementDistinguishable > > ; } export function isDistinguishingStyles ( value ) { return value instanceof DistinguishingStyles ; } }