Skip to content

Commit

Permalink
fix: correctly trigger warnings when matched text is on separate lines
Browse files Browse the repository at this point in the history
Match was occurring on separate lines because we were searching across the entire container div
rather than the individual divs that each line in the mssage is made up of. This fix iterates
through each childNode (div) of text and identifies a match that way. Note, this doesn't fix the
highlighting though of phrases that span multiple lines.
  • Loading branch information
James Duffy committed Jan 29, 2021
1 parent 836f1f1 commit 857847b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
12 changes: 6 additions & 6 deletions spec/JustNotSorrySpec.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('JustNotSorry', () => {
});
node.simulate('focus');

expect(spy).toHaveBeenCalledWith(node.getDOMNode().parentNode);
expect(spy).toHaveBeenCalledWith(node.getDOMNode());
});
});

Expand Down Expand Up @@ -184,7 +184,7 @@ describe('JustNotSorry', () => {
expect.objectContaining({
pattern: '\\b!{3,}\\B',
message: 'warning message',
parentNode: node,
parentNode: node.parentNode,
})
);
});
Expand All @@ -202,7 +202,7 @@ describe('JustNotSorry', () => {
expect.objectContaining({
pattern: 'just',
message: 'warning message',
parentNode: node,
parentNode: node.parentNode,
})
);
});
Expand Down Expand Up @@ -233,7 +233,7 @@ describe('JustNotSorry', () => {
expect.objectContaining({
pattern: 'just',
message: 'warning message',
parentNode: node,
parentNode: node.parentNode,
})
);
});
Expand All @@ -250,7 +250,7 @@ describe('JustNotSorry', () => {
expect.objectContaining({
pattern: 'just',
message: 'warning message',
parentNode: node,
parentNode: node.parentNode,
})
);
});
Expand All @@ -267,7 +267,7 @@ describe('JustNotSorry', () => {
expect.objectContaining({
pattern: 'so sorry',
message: 'warning message',
parentNode: node,
parentNode: node.parentNode,
})
);
});
Expand Down
39 changes: 20 additions & 19 deletions src/components/JustNotSorry.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,22 @@ class JustNotSorry extends Component {
}

updateWarnings = (node, pattern, message) => {
domRegexpMatch(node, pattern, (match, range) => {
const newWarning = {
pattern: pattern.source,
message: message,
parentNode: node,
rangeToHighlight: range,
};

this.setState((state) => ({
warnings: state.warnings.concat(newWarning),
}));
});
if (node.childNodes) {
node.childNodes.forEach((childNode) => {
domRegexpMatch(childNode, pattern, (match, range) => {
const newWarning = {
pattern: pattern.source,
message: message,
parentNode: node.parentNode,
rangeToHighlight: range,
};

this.setState((state) => ({
warnings: state.warnings.concat(newWarning),
}));
});
});
}
};

addWarning = (node, warning) =>
Expand All @@ -82,13 +86,10 @@ class JustNotSorry extends Component {
};

addObserver = (event) => {
const element = event.currentTarget;
element.addEventListener(
'input',
this.checkForWarnings(element.parentNode)
);
this.addWarnings(element.parentNode);
this.observer.observe(element, OPTIONS);
const node = event.currentTarget;
this.observer.observe(node, OPTIONS);
node.addEventListener('input', this.checkForWarnings(node));
this.addWarnings(node);
};

removeObserver = (event) => {
Expand Down

0 comments on commit 857847b

Please sign in to comment.