Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

fix(blur): collapse the text node guard to an optional chain

Luis Ortiz2e90119134d6
diff --git a/src/core/blur/__tests__/scanner.test.ts b/src/core/blur/__tests__/scanner.test.ts
new file mode 100644
index 0000000..1aaa973
--- /dev/null
+++ b/src/core/blur/__tests__/scanner.test.ts
@@ -0,0 +1,55 @@
+// @vitest-environment jsdom
+import { afterEach, describe, expect, it } from 'vitest';
+import { BlurScanner } from '../scanner';
+
+const BLURRED = '.mimik-blur';
+
+function scanWith(html: string): Document {
+ document.body.innerHTML = html;
+ const scanner = new BlurScanner();
+ scanner.start(['email']);
+ scanner.detach();
+ return document;
+}
+
+afterEach(() => {
+ document.body.innerHTML = '';
+});
+
+describe('text node filtering', () => {
+ it('blurs a match in real text', () => {
+ scanWith('<p>Write to [email protected] today</p>');
+ expect(document.querySelectorAll(BLURRED)).toHaveLength(1);
+ expect(document.querySelector(BLURRED)?.textContent).toBe('[email protected]');
+ });
+
+ it('leaves text with no match alone', () => {
+ scanWith('<p>Nothing sensitive here</p>');
+ expect(document.querySelectorAll(BLURRED)).toHaveLength(0);
+ });
+
+ it('skips empty and whitespace-only nodes without throwing', () => {
+ expect(() => scanWith('<p></p><p> </p><p>\n\t </p><span></span>')).not.toThrow();
+ expect(document.querySelectorAll(BLURRED)).toHaveLength(0);
+ });
+
+ it('still finds a match among empty and blank siblings', () => {
+ scanWith('<p></p><p> </p><p>[email protected]</p><p> </p>');
+ expect(document.querySelectorAll(BLURRED)).toHaveLength(1);
+ });
+
+ it('never walks into an excluded tag', () => {
+ scanWith('<script>[email protected]</script><style>[email protected]</style><p>[email protected]</p>');
+ expect(document.querySelectorAll(BLURRED)).toHaveLength(1);
+ expect(document.querySelector(BLURRED)?.textContent).toBe('[email protected]');
+ });
+
+ it('does not blur inside something already blurred', () => {
+ scanWith('<p>[email protected] and [email protected]</p>');
+ const first = document.querySelectorAll(BLURRED).length;
+ const scanner = new BlurScanner();
+ scanner.start(['email']);
+ scanner.detach();
+ expect(document.querySelectorAll(BLURRED)).toHaveLength(first);
+ });
+});
diff --git a/src/core/blur/scanner.ts b/src/core/blur/scanner.ts
index 3aee772..4395faa 100644
--- a/src/core/blur/scanner.ts
+++ b/src/core/blur/scanner.ts
@@ -61,7 +61,7 @@ export class BlurScanner {
if (!node.parentElement) return NodeFilter.FILTER_REJECT;
if (EXCLUDED_TAGS.has(node.parentElement.tagName)) return NodeFilter.FILTER_REJECT;
if (node.parentElement.closest(`[${BLUR_ATTR}]`)) return NodeFilter.FILTER_REJECT;
- if (!node.textContent || !node.textContent.trim()) return NodeFilter.FILTER_REJECT;
+ if (!node.textContent?.trim()) return NodeFilter.FILTER_REJECT;
return NodeFilter.FILTER_ACCEPT;
},
});