Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: (CXSPA-1128) - Select a11y directive sanitized innerText #19408

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import {
Optional,
PLATFORM_ID,
Renderer2,
SecurityContext,
} from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { FeatureConfigService, TranslationService } from '@spartacus/core';
import { filter, take } from 'rxjs';
import { BREAKPOINT, BreakpointService } from '../../../layout';
Expand All @@ -35,6 +37,7 @@ export class NgSelectA11yDirective implements AfterViewInit {
@Input() cxNgSelectA11y: { ariaLabel?: string; ariaControls?: string };

protected translationService = inject(TranslationService);
protected domSanitizer = inject(DomSanitizer);
private featureConfigService = inject(FeatureConfigService);

@HostListener('open')
Expand Down Expand Up @@ -107,7 +110,11 @@ export class NgSelectA11yDirective implements AfterViewInit {
.subscribe((translation) => {
options.forEach(
(option: HTMLOptionElement, index: string | number) => {
const ariaLabel = `${option.innerText}, ${+index + 1} ${translation} ${options.length}`;
const sanitizedOptionText = this.domSanitizer.sanitize(
SecurityContext.HTML,
option.innerText
);
const ariaLabel = `${sanitizedOptionText}, ${+index + 1} ${translation} ${options.length}`;
this.renderer.setAttribute(option, ARIA_LABEL, ariaLabel);
}
);
Expand All @@ -125,17 +132,19 @@ export class NgSelectA11yDirective implements AfterViewInit {
observer: MutationObserver,
divCombobox: HTMLElement
) {
const valueLabel =
this.elementRef.nativeElement.querySelector('.ng-value-label')?.innerText;
if (valueLabel) {
const sanitizedValueLabel = this.domSanitizer.sanitize(
SecurityContext.HTML,
this.elementRef.nativeElement.querySelector('.ng-value-label')?.innerText
);
if (sanitizedValueLabel) {
const comboboxAriaLabel = divCombobox?.getAttribute(ARIA_LABEL) || '';
const valueElement =
this.elementRef.nativeElement.querySelector('.ng-value');
this.renderer.setAttribute(valueElement, 'aria-hidden', 'true');
this.renderer.setAttribute(
divCombobox,
ARIA_LABEL,
comboboxAriaLabel + ', ' + valueLabel
comboboxAriaLabel + ', ' + sanitizedValueLabel
);
}
observer.disconnect();
Expand Down
Loading