Skip to content

Commit

Permalink
fix: prevent reporting elements with empty textContent
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Jun 22, 2021
1 parent a3bf775 commit 9f840da
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/capture-announcements.ts
Expand Up @@ -33,7 +33,9 @@ export default function CaptureAnnouncements(options: Options): Restore {
) => {
const content = trimWhiteSpace(textContent);

options.onCapture(content, politenessSetting);
if (content) {
options.onCapture(content, politenessSetting);
}
};

const onIncorrectStatusMessage: NonNullable<
Expand All @@ -42,7 +44,9 @@ export default function CaptureAnnouncements(options: Options): Restore {
if (options.onIncorrectStatusMessage) {
const content = trimWhiteSpace(textContent);

options.onIncorrectStatusMessage(content);
if (content) {
options.onIncorrectStatusMessage(content);
}
}
};

Expand Down Expand Up @@ -208,6 +212,7 @@ function onRemoveAttribute(
}
}

function trimWhiteSpace(text: string) {
return text.trim().replace(WHITE_SPACE_REGEXP, ' ');
function trimWhiteSpace(text: string): string | null {
const trimmed = text.trim().replace(WHITE_SPACE_REGEXP, ' ');
return trimmed.length > 0 ? trimmed : null;
}
42 changes: 42 additions & 0 deletions test/capture-announcements.test.ts
Expand Up @@ -322,4 +322,46 @@ describe('common', () => {
'First message here Second message here'
);
});

test('onCapture should not report when textContent is empty', () => {
element.setAttribute('role', 'status');
appendToRoot(element);

element.textContent = ' ';
element.textContent = ' ';

// prettier-ignore
element.textContent = `
`;

expect(onCapture).not.toHaveBeenCalled();
});

test('onIncorrectStatusMessage should not report when textContent is empty', () => {
const element2 = document.createElement('div');
const element3 = document.createElement('div');

element.setAttribute('role', 'status');
element2.setAttribute('role', 'status');
element3.setAttribute('role', 'status');

element.textContent = ' ';
element2.textContent = ' ';

// prettier-ignore
element3.textContent = `
`;

appendToRoot(element);
appendToRoot(element2);
appendToRoot(element3);

expect(onIncorrectStatusMessage).not.toHaveBeenCalled();
});
});

0 comments on commit 9f840da

Please sign in to comment.