From 2b93a403d3f4d226255c8ba15fb74a9eb3f03226 Mon Sep 17 00:00:00 2001 From: Steve Brudz Date: Wed, 25 Aug 2021 08:53:22 -0500 Subject: [PATCH] fix: fix issue with warnings not appearing for first line of email text We needed to use childNodes instead of children in order to pick up both the child elements with text and the DOM text nodes directly associated with the element. re #117 --- spec/JustNotSorrySpec.test.js | 19 +++++++++++++++++++ src/components/JustNotSorry.js | 4 ++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/spec/JustNotSorrySpec.test.js b/spec/JustNotSorrySpec.test.js index 7911891..ac03a1f 100644 --- a/spec/JustNotSorrySpec.test.js +++ b/spec/JustNotSorrySpec.test.js @@ -147,6 +147,25 @@ describe('JustNotSorry', () => { expect(wrapper.state('parentNode')).toBe(domNode.parentNode); }); + describe('when there are top-level and child nodes', () => { + it('catches the warnings for both', () => { + const elem = mount( +
+ test!!! +
Hello!!!
+
+ ); + + const domNode = elem.getDOMNode(); + instance.updateWarnings(domNode, [ + buildWarning('\\b!{3,}\\B', 'warning message'), + ]); + + expect(wrapper.state('warnings').length).toBe(2); + expect(wrapper.state('parentNode')).toBe(domNode.parentNode); + }); + }); + it('does not add warnings for tooltip matches', () => { const node = enterText('test justify test'); simulateEvent(node, 'focus'); diff --git a/src/components/JustNotSorry.js b/src/components/JustNotSorry.js index 4a33293..ecd87cf 100644 --- a/src/components/JustNotSorry.js +++ b/src/components/JustNotSorry.js @@ -43,8 +43,8 @@ class JustNotSorry extends Component { updateWarnings(email, patterns) { const newWarnings = - email.children.length > 0 - ? Array.from(email.children) + email.childNodes.length > 0 + ? Array.from(email.childNodes) .filter((node) => node.textContent !== '') .flatMap((text) => findRanges(text, patterns)) : findRanges(email, patterns);