Skip to content

Commit 8602729

Browse files
sheikalthafpkozlowski-opensource
authored andcommitted
fix(devtools): remove existing highlight before highlighting another element (angular#57746)
In this PR we will remove the exiting highlight before highlighting the another element so that the highlight always be one PR Close angular#57746
1 parent 7b2fda1 commit 8602729

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

devtools/projects/ng-devtools-backend/src/lib/highlighter.spec.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,17 @@ describe('highlighter', () => {
183183
});
184184

185185
describe('highlightSelectedElement', () => {
186+
function createElement(name: string) {
187+
const element = document.createElement(name);
188+
element.style.width = '25px';
189+
element.style.height = '20px';
190+
element.style.display = 'block';
191+
document.body.appendChild(element);
192+
return element;
193+
}
194+
186195
it('should show overlay', () => {
187-
const appNode = document.createElement('app');
188-
appNode.style.width = '25px';
189-
appNode.style.height = '20px';
190-
appNode.style.display = 'block';
191-
document.body.appendChild(appNode);
196+
const appNode = createElement('app');
192197
(window as any).ng = {
193198
getComponent: (el: any) => new (class FakeComponent {})(),
194199
};
@@ -202,5 +207,19 @@ describe('highlighter', () => {
202207
highlighter.unHighlight();
203208
expect(document.body.querySelectorAll('.ng-devtools-overlay').length).toBe(0);
204209
});
210+
211+
it('should remove the previous overlay when calling highlightSelectedElement again', () => {
212+
const appNode = createElement('app');
213+
const appNode2 = createElement('app-two');
214+
215+
(window as any).ng = {
216+
getComponent: (el: any) => new (class FakeComponent {})(),
217+
};
218+
219+
highlighter.highlightSelectedElement(appNode);
220+
highlighter.highlightSelectedElement(appNode2);
221+
const overlay = document.body.querySelectorAll('.ng-devtools-overlay');
222+
expect(overlay.length).toBe(1);
223+
});
205224
});
206225
});

devtools/projects/ng-devtools-backend/src/lib/highlighter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export function getDirectiveName(dir: Type<unknown> | undefined | null): string
8383
}
8484

8585
export function highlightSelectedElement(el: Node): void {
86+
unHighlight();
8687
selectedElementOverlay = addHighlightForElement(el);
8788
}
8889

0 commit comments

Comments
 (0)