Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 9, 2025

Closes #

Additional information (check all that apply):

  • Bug fix
  • New functionality
  • Documentation
  • Demos
  • CI/CD

Checklist:

  • All relevant tags have been applied to this PR
  • This PR includes unit tests covering all the new code (test guidelines)
  • This PR includes API docs for newly added methods/properties (api docs guidelines)
  • This PR includes feature/README.MD updates for the feature docs
  • This PR includes general feature table updates in the root README.MD
  • This PR includes CHANGELOG.MD updates for newly added functionality
  • This PR contains breaking changes
  • This PR includes ng update migrations for the breaking changes (migrations guidelines)
  • This PR includes behavioral changes and the feature specification has been updated with them

Description

IgxTextHighlightDirective throws TypeError: Cannot set properties of undefined (setting 'searchText') when destroyed before ngAfterViewInit() completes. This occurs during rapid component lifecycles (e.g., *ngIf toggling, route changes).

Root cause: _lastSearchInfo is initialized in ngAfterViewInit(), but ngOnDestroy()clearHighlight() attempts to mutate it unconditionally.

Changes

text-highlight.directive.ts

  • Added null guard in clearHighlight() before accessing _lastSearchInfo properties

text-highlight.directive.spec.ts

  • Added test case verifying directive can be destroyed without initialization
// Before: TypeError if destroyed early
public clearHighlight(): void {
    this.clearChildElements(false);
    this._lastSearchInfo.searchText = '';  // Throws if undefined
    this._lastSearchInfo.matchCount = 0;
}

// After: Safe destruction at any lifecycle point
public clearHighlight(): void {
    this.clearChildElements(false);
    if (this._lastSearchInfo) {
        this._lastSearchInfo.searchText = '';
        this._lastSearchInfo.matchCount = 0;
    }
}

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • accounts.google.com
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --disable-dev-shm-usage --use-angle=swiftshader-webgl --crashpad-handler-pid=3743 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/karma-47021508 --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,10695444732852139445,17746843444129488011,262144 --disable-features=PaintHolding --variations-seed-version --trace-process-track-uuid=3190708989122997041 (dns block)
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --disable-dev-shm-usage --use-angle=swiftshader-webgl --crashpad-handler-pid=3744 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/karma-71043107 --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,15790000479656197725,8015250652308679749,262144 --disable-features=PaintHolding --variations-seed-version --trace-process-track-uuid=3190708989122997041 (dns block)
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --disable-dev-shm-usage --use-angle=swiftshader-webgl --crashpad-handler-pid=3745 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/karma-55166021 --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,1316495075333674403,18310962144212124140,262144 --disable-features=PaintHolding --variations-seed-version --trace-process-track-uuid=3190708989122997041 (dns block)
  • clients2.google.com
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --disable-dev-shm-usage --use-angle=swiftshader-webgl --crashpad-handler-pid=3743 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/karma-47021508 --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,10695444732852139445,17746843444129488011,262144 --disable-features=PaintHolding --variations-seed-version --trace-process-track-uuid=3190708989122997041 (dns block)
    • Triggering command: /home/REDACTED/.cache/puppeteer/chrome/linux-142.0.7444.175/chrome-linux64/chrome /home/REDACTED/.cache/puppeteer/chrome/linux-142.0.7444.175/chrome-linux64/chrome --user-data-dir=/tmp/karma-47021508 --enable-automation --no-default-browser-check --no-first-run --disable-default-apps --disable-popup-blocking --disable-translate --disable-REDACTED-timer-throttling --disable-renderer-REDACTEDing --disable-device-discovery-notifications --no-sandbox --disable-gpu --window-size=820,800 --js-flags=--expose-gc http://localhost:9876/?id=47021508 --headless --disable-gpu --disable-dev-shm-usage --remote-debugging-port=9222 (dns block)
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --disable-dev-shm-usage --use-angle=swiftshader-webgl --crashpad-handler-pid=3744 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/karma-71043107 --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,15790000479656197725,8015250652308679749,262144 --disable-features=PaintHolding --variations-seed-version --trace-process-track-uuid=3190708989122997041 (dns block)
  • redirector.gvt1.com
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --disable-dev-shm-usage --use-angle=swiftshader-webgl --crashpad-handler-pid=3743 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/karma-47021508 --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,10695444732852139445,17746843444129488011,262144 --disable-features=PaintHolding --variations-seed-version --trace-process-track-uuid=3190708989122997041 (dns block)
    • Triggering command: /home/REDACTED/.cache/puppeteer/chrome/linux-142.0.7444.175/chrome-linux64/chrome /home/REDACTED/.cache/puppeteer/chrome/linux-142.0.7444.175/chrome-linux64/chrome --user-data-dir=/tmp/karma-47021508 --enable-automation --no-default-browser-check --no-first-run --disable-default-apps --disable-popup-blocking --disable-translate --disable-REDACTED-timer-throttling --disable-renderer-REDACTEDing --disable-device-discovery-notifications --no-sandbox --disable-gpu --window-size=820,800 --js-flags=--expose-gc http://localhost:9876/?id=47021508 --headless --disable-gpu --disable-dev-shm-usage --remote-debugging-port=9222 (dns block)
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --disable-dev-shm-usage --use-angle=swiftshader-webgl --crashpad-handler-pid=3745 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/karma-55166021 --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,1316495075333674403,18310962144212124140,262144 --disable-features=PaintHolding --variations-seed-version --trace-process-track-uuid=3190708989122997041 (dns block)
  • www.google.com
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --disable-dev-shm-usage --use-angle=swiftshader-webgl --crashpad-handler-pid=3743 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/karma-47021508 --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,10695444732852139445,17746843444129488011,262144 --disable-features=PaintHolding --variations-seed-version --trace-process-track-uuid=3190708989122997041 (dns block)
    • Triggering command: /home/REDACTED/.cache/puppeteer/chrome/linux-142.0.7444.175/chrome-linux64/chrome /home/REDACTED/.cache/puppeteer/chrome/linux-142.0.7444.175/chrome-linux64/chrome --user-data-dir=/tmp/karma-47021508 --enable-automation --no-default-browser-check --no-first-run --disable-default-apps --disable-popup-blocking --disable-translate --disable-REDACTED-timer-throttling --disable-renderer-REDACTEDing --disable-device-discovery-notifications --no-sandbox --disable-gpu --window-size=820,800 --js-flags=--expose-gc http://localhost:9876/?id=47021508 --headless --disable-gpu --disable-dev-shm-usage --remote-debugging-port=9222 (dns block)
    • Triggering command: /proc/self/exe /proc/self/exe --type=utility --utility-sub-type=network.mojom.NetworkService --lang=en-US --service-sandbox-type=network --no-sandbox --disable-dev-shm-usage --use-angle=swiftshader-webgl --crashpad-handler-pid=3745 --enable-crash-reporter=, --noerrdialogs --user-data-dir=/tmp/karma-55166021 --change-stack-guard-on-fork=enable --shared-files=network_parent_dirs_pipe:100,v8_context_snapshot_data:101 --field-trial-handle=3,i,1316495075333674403,18310962144212124140,262144 --disable-features=PaintHolding --variations-seed-version --trace-process-track-uuid=3190708989122997041 (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>IgxTextHighlightDirective throws TypeError when destroyed before ngAfterViewInit completes</issue_title>
<issue_description>## Description
The IgxTextHighlightDirective throws a TypeError: Cannot set properties of undefined (setting 'searchText') when the directive is destroyed before its view initialization completes. This occurs when components containing the directive are destroyed
during Angular's change detection cycle before ngAfterViewInit() has been called.

The error occurs in the clearHighlight() method which is called from ngOnDestroy(). The method attempts to access this._lastSearchInfo.searchText and this._lastSearchInfo.matchCount, but _lastSearchInfo is only initialized in
ngAfterViewInit(). If ngOnDestroy() is called before ngAfterViewInit() completes, _lastSearchInfo remains undefined, causing the error.

Error Stack Trace:
TypeError: Cannot set properties of undefined (setting 'searchText')
at _IgxTextHighlightDirective.clearHighlight (infragistics-igniteui-angular.mjs:38551:26)
at _IgxTextHighlightDirective.ngOnDestroy (infragistics-igniteui-angular.mjs:38472:10)
at executeOnDestroys (debug_node.mjs:7661:32)
at cleanUpView (debug_node.mjs:7526:9)
at destroyViewTree (debug_node.mjs:7463:21)
at destroyLView (debug_node.mjs:7500:5)

Problematic Code (from infragistics-igniteui-angular.mjs):

ngOnDestroy() {
    this.clearHighlight();  // Calls clearHighlight before checking initialization
    // ...
}

clearHighlight() {
    this.clearChildElements(false);
    this._lastSearchInfo.searchText = '';  // ERROR: No null check
    this._lastSearchInfo.matchCount = 0;   // ERROR: No null check
}

ngAfterViewInit() {
    // ...
    this._lastSearchInfo = {  // Only initialized here
        searchText: '',
        content: this.value,
        matchCount: 0,
        caseSensitive: false,
        exactMatch: false
    };
}

Steps to reproduce

  1. Create an Angular application with components that use IgxTextHighlightDirective (e.g., tree nodes, list items with search highlighting)
  2. Dynamically create and destroy these components rapidly (e.g., routing between views, conditionally rendering with *ngIf)
  3. Ensure the components are destroyed during Angular's change detection before their ngAfterViewInit() lifecycle hook completes
  4. Observe the console error: TypeError: Cannot set properties of undefined (setting 'searchText')

Result

The application throws a TypeError when IgxTextHighlightDirective is destroyed before initialization completes. The error propagates to the global error handler, potentially causing error loops or degraded user experience.

Expected result

The IgxTextHighlightDirective should safely handle being destroyed at any point in its lifecycle, including before ngAfterViewInit() completes. The clearHighlight() method should check if _lastSearchInfo exists before accessing its properties.

Proposed Fix:

 clearHighlight() {
     this.clearChildElements(false);
     if (this._lastSearchInfo) {  // Add null check
         this._lastSearchInfo.searchText = '';
         this._lastSearchInfo.matchCount = 0;
     }
 }
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
<comment_new><author>@gedinakova</author><body>
@Copilot Please, review the issue & implement the fix.</body></comment_new>
<comment_new><author>@gedinakova</author><body>
@copilot Please, create a new PR as the previous one has too many conflicts.</body></comment_new>
</comments>

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: gedinakova <16817847+gedinakova@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix TypeError in IgxTextHighlightDirective on destroy Fix TypeError in IgxTextHighlightDirective when destroyed before ngAfterViewInit Dec 9, 2025
Copilot AI requested a review from gedinakova December 9, 2025 11:08
@gedinakova
Copy link
Contributor

@copilot Please, make PRs to branches 21.0.x and 20.1.x as well.

Copy link
Contributor Author

Copilot AI commented Dec 10, 2025

@copilot Please, make PRs to branches 21.0.x and 20.1.x as well.

I cannot create PRs to different branches or push changes directly using git commands. However, the fix is identical for all branches - it only requires adding a null check in the clearHighlight() method.

The changes needed are:

  1. In text-highlight.directive.ts lines 305-306: Add if (this._lastSearchInfo) guard
  2. In text-highlight.directive.spec.ts: Add the test case for early destruction

You can cherry-pick commit 259f74b to both branches, or I can help apply the changes if you create new branch contexts for 21.0.x and 20.1.x.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IgxTextHighlightDirective throws TypeError when destroyed before ngAfterViewInit completes

2 participants