@hmcts/media-viewer 4.2.49 → 4.2.50-innerhtml-security-update

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
@@ -3364,26 +3364,55 @@ class TextHighlightDirective {
3364
3364
  highlightInputText(textToHighlight) {
3365
3365
  this.resetHighlight();
3366
3366
  this.textToHighlight = textToHighlight;
3367
- const searchPattern = new RegExp(textToHighlight, 'gi');
3367
+ const searchPattern = new RegExp(this.escapeRegExp(textToHighlight), 'gi');
3368
3368
  const hostElement = this.element.nativeElement;
3369
- if (hostElement.innerHTML.match(searchPattern)) {
3370
- hostElement.innerHTML = hostElement.innerHTML
3371
- .replace(searchPattern, this.highlightPattern('$&'));
3372
- }
3369
+ this.highlightTextNodes(hostElement, searchPattern);
3373
3370
  this.textToHighlight = undefined;
3374
3371
  }
3375
3372
  resetHighlight() {
3376
3373
  const hostElement = this.element.nativeElement;
3377
- const searchPattern = new RegExp(this.highlightPattern('(.*?)'), 'gi');
3378
- while (hostElement.innerHTML.match(searchPattern)) {
3379
- const matchGroups = searchPattern.exec(hostElement.innerHTML);
3380
- if (matchGroups) {
3381
- hostElement.innerHTML = hostElement.innerHTML.replace(matchGroups[0], matchGroups[1]);
3374
+ Array.from(hostElement.querySelectorAll('span.mvTextHighlight')).forEach((highlightElement) => {
3375
+ highlightElement.replaceWith(document.createTextNode(highlightElement.textContent || ''));
3376
+ });
3377
+ hostElement.normalize();
3378
+ }
3379
+ highlightTextNodes(hostElement, searchPattern) {
3380
+ const walker = document.createTreeWalker(hostElement, NodeFilter.SHOW_TEXT);
3381
+ const textNodes = [];
3382
+ let currentNode = walker.nextNode();
3383
+ while (currentNode) {
3384
+ textNodes.push(currentNode);
3385
+ currentNode = walker.nextNode();
3386
+ }
3387
+ textNodes.forEach((textNode) => this.highlightTextNode(textNode, searchPattern));
3388
+ }
3389
+ highlightTextNode(textNode, searchPattern) {
3390
+ const text = textNode.textContent || '';
3391
+ const matches = Array.from(text.matchAll(searchPattern));
3392
+ if (matches.length === 0) {
3393
+ return;
3394
+ }
3395
+ const fragment = document.createDocumentFragment();
3396
+ let currentIndex = 0;
3397
+ matches.forEach((match) => {
3398
+ const matchIndex = match.index || 0;
3399
+ const matchText = match[0];
3400
+ if (matchIndex > currentIndex) {
3401
+ fragment.appendChild(document.createTextNode(text.slice(currentIndex, matchIndex)));
3382
3402
  }
3403
+ const highlightElement = document.createElement('span');
3404
+ highlightElement.className = 'mvTextHighlight';
3405
+ highlightElement.textContent = matchText;
3406
+ fragment.appendChild(highlightElement);
3407
+ currentIndex = matchIndex + matchText.length;
3408
+ });
3409
+ if (currentIndex < text.length) {
3410
+ fragment.appendChild(document.createTextNode(text.slice(currentIndex)));
3383
3411
  }
3412
+ textNode.replaceWith(fragment);
3384
3413
  }
3385
- highlightPattern(dynamicText) {
3386
- return '<span class="mvTextHighlight">' + dynamicText + '</span>';
3414
+ escapeRegExp(value) {
3415
+ return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
3387
3416
  }
3388
3417
  /** @nocollapse */ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.35", ngImport: i0, type: TextHighlightDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive }); }
3389
3418
  /** @nocollapse */ static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "20.3.35", type: TextHighlightDirective, isStandalone: false, selector: "[mvTextHighlight]", inputs: { textToHighlight: "textToHighlight" }, ngImport: i0 }); }
@@ -7969,12 +7998,14 @@ class GovUkLabelComponent {
7969
7998
  /** @nocollapse */ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.35", ngImport: i0, type: GovUkLabelComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
7970
7999
  /** @nocollapse */ static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "20.3.35", type: GovUkLabelComponent, isStandalone: false, selector: "mv-gov-label", inputs: { config: "config" }, ngImport: i0, template: `<h1 *ngIf="config.isPageHeading else noHeading">
7971
8000
  <label *ngIf="config.label" [class]="config.classes + ' govuk-label'"
7972
- [for]="config.id" [innerHTML]="config.label">
8001
+ [for]="config.id">
8002
+ {{ config.label }}
7973
8003
  </label>
7974
8004
  </h1>
7975
8005
  <ng-template #noHeading>
7976
8006
  <label *ngIf="config.label" [class]="config.classes + ' govuk-label'"
7977
- [for]="config.id" [innerHTML]="config.label">
8007
+ [for]="config.id">
8008
+ {{ config.label }}
7978
8009
  </label>
7979
8010
  </ng-template>
7980
8011
  `, isInline: true, dependencies: [{ kind: "directive", type: i1.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }] }); }
@@ -7985,12 +8016,14 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.35", ngImpo
7985
8016
  selector: 'mv-gov-label',
7986
8017
  template: `<h1 *ngIf="config.isPageHeading else noHeading">
7987
8018
  <label *ngIf="config.label" [class]="config.classes + ' govuk-label'"
7988
- [for]="config.id" [innerHTML]="config.label">
8019
+ [for]="config.id">
8020
+ {{ config.label }}
7989
8021
  </label>
7990
8022
  </h1>
7991
8023
  <ng-template #noHeading>
7992
8024
  <label *ngIf="config.label" [class]="config.classes + ' govuk-label'"
7993
- [for]="config.id" [innerHTML]="config.label">
8025
+ [for]="config.id">
8026
+ {{ config.label }}
7994
8027
  </label>
7995
8028
  </ng-template>
7996
8029
  `,