From de4083afee26b23abc7ea1fcba164025b58571ac Mon Sep 17 00:00:00 2001 From: Anton Shabatin <131185633+sha4be@users.noreply.github.com> Date: Thu, 18 Jul 2024 09:54:34 +0200 Subject: [PATCH] Fix clearing hoghligh functionality when a user turn the feature off (#108) --- .../__name@dasherize__.directive.ts.template | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/ng-generate/components/table/generators/directives/highlight/files/__name@dasherize__.directive.ts.template b/src/ng-generate/components/table/generators/directives/highlight/files/__name@dasherize__.directive.ts.template index a68adfd7..2f602569 100644 --- a/src/ng-generate/components/table/generators/directives/highlight/files/__name@dasherize__.directive.ts.template +++ b/src/ng-generate/components/table/generators/directives/highlight/files/__name@dasherize__.directive.ts.template @@ -44,15 +44,19 @@ export class <%= classify(name) %>Directive implements OnChanges, OnInit { ngOnChanges(changes: HighlightSimpleChanges) { if ((changes.highlight && !changes.highlight.firstChange) || (changes.caseSensitive && !changes.caseSensitive.firstChange)) { - if (this._selected) { - this.transformText(); - } + this.handleHighlightText(); } } ngOnInit(): void { + this.handleHighlightText(); + } + + private handleHighlightText(): void { if (this._selected) { - this.transformText(); + this.transformText(); + } else if (this.isStringHighlighted) { + this.clearHighlights(); } } @@ -149,4 +153,13 @@ export class <%= classify(name) %>Directive implements OnChanges, OnInit { result += this.highlightSource?.substring(lastIndex); return result; } + + private get isStringHighlighted(): boolean { + return !!this.el.nativeElement.querySelector('mark'); + } + + private clearHighlights(): void { + const content = this.sanitizer.sanitize(SecurityContext.STYLE, this.el.nativeElement.innerText); + (this.el.nativeElement as HTMLElement).innerHTML = content; + } }