Skip to content

Commit

Permalink
Skip Empty DIV when traverse (microsoft#412)
Browse files Browse the repository at this point in the history
* Skip Empty DIV when traverse

* imporve
  • Loading branch information
JiuqingSong authored May 15, 2020
1 parent e25be11 commit 23c6060
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/roosterjs-editor-dom/lib/test/utils/shouldSkipNodeTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ describe('shouldSkipNode, shouldSkipNode()', () => {
expect(shouldSkip).toBe(true);
});

it('Empty DIV node', () => {
// Arrange
let node = DomTestHelper.createElementFromContent(testID, '<div></div>');

// Act
let shouldSkip = shouldSkipNode(node.firstChild);

// Assert
expect(shouldSkip).toBe(true);
});

it('Regular node', () => {
// Arrange
let node = document.createTextNode('abc');
Expand Down
7 changes: 6 additions & 1 deletion packages/roosterjs-editor-dom/lib/utils/shouldSkipNode.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import getTagOfNode from './getTagOfNode';
import { getComputedStyle } from './getComputedStyles';
import { NodeType } from 'roosterjs-editor-types';

Expand All @@ -9,12 +10,16 @@ const CRLF = /^[\r\n]+$/gm;
* - it is a text node but is empty
* - it is a text node but contains just CRLF (noisy text node that often comes in-between elements)
* - has a display:none
* - it is just <div></div>
*/
export default function shouldSkipNode(node: Node): boolean {
if (node.nodeType == NodeType.Text) {
return !node.nodeValue || node.textContent == '' || CRLF.test(node.nodeValue);
} else if (node.nodeType == NodeType.Element) {
return getComputedStyle(node, 'display') == 'none';
return (
(getTagOfNode(node) == 'DIV' && !node.firstChild) ||
getComputedStyle(node, 'display') == 'none'
);
} else {
return true;
}
Expand Down

0 comments on commit 23c6060

Please sign in to comment.