import { Rule , Diagnostic } from "@siteimprove/alfa-act" ; import { Node } from "@siteimprove/alfa-aria" ; import { Element , Namespace } from "@siteimprove/alfa-dom" ; import { Predicate } from "@siteimprove/alfa-predicate" ; import { Err , Ok } from "@siteimprove/alfa-result" ; import { Criterion } from "@siteimprove/alfa-wcag" ; import { Page } from "@siteimprove/alfa-web" ; import { expectation } from "../common/expectation" ; import { hasNonEmptyAccessibleName , isIgnored , referenceSameResource , } from "../common/predicate" ; import { Scope } from "../tags" ; import { Group } from "../common/group" ; import { normalize } from "../common/normalize" ; import { Question } from "../common/question" ; const { isElement , hasName , hasNamespace } = Element ; const { and , not } = Predicate ; export default Rule . Atomic . of < Page , Group < Element > , Question . Metadata > ( { uri : "https://alfa.siteimprove.com/rules/sia-r15" , requirements : [ Criterion . of ( "4.1.2" ) ] , tags : [ Scope . Component ] , evaluate ( { device , document , response } ) { return { applicability ( ) { return document . descendants ( { flattened : true , nested : true } ) . filter ( isElement ) . filter ( and ( hasName ( "iframe" ) , hasNamespace ( Namespace . HTML ) , not ( isIgnored ( device ) ) , hasNonEmptyAccessibleName ( device ) ) ) . groupBy ( ( iframe ) => Node . from ( iframe , device ) . name . map ( ( name ) => normalize ( name . value ) ) ) . filter ( ( elements ) => elements . size > 1 ) . map ( Group . of ) . values ( ) ; } , expectations ( target ) { const embedSameResource = [ ... target ] . every ( ( element , i , elements ) => i === 0 || referenceSameResource ( response . url ) ( element , elements [ i - 1 ] ) ) ; return { 1 : expectation ( embedSameResource , ( ) => Outcomes . EmbedSameResources , ( ) => Question . of ( "reference-equivalent-resources" , target , Diagnostic . of ( "Do the <iframe> elements embed equivalent resources?" ) ) . map ( ( embedEquivalentResources ) => expectation ( embedEquivalentResources , ( ) => Outcomes . EmbedEquivalentResources , ( ) => Outcomes . EmbedDifferentResources ) ) ) , } ; } , } ; } , } ) ; export namespace Outcomes { export const EmbedSameResources = Ok . of ( Diagnostic . of ( `The \`<iframe>\` elements embed the same resource` ) ) ; export const EmbedEquivalentResources = Ok . of ( Diagnostic . of ( `The \`<iframe>\` elements embed equivalent resources` ) ) ; export const EmbedDifferentResources = Err . of ( Diagnostic . of ( `The \`<iframe>\` elements do not embed the same or equivalent resources` ) ) ; }