Skip to content
This repository has been archived by the owner on Dec 22, 2023. It is now read-only.

Strip out white space text nodes #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/processing-instructions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var ProcessingInstructions = function(React) {

return {
defaultProcessingInstructions: [{
shouldProcessNode: ShouldProcessNodeDefinitions.shouldProcessEveryNode,
shouldProcessNode: ShouldProcessNodeDefinitions.shouldProcessEveryNodeExceptWhiteSpace,
processNode: processNodeDefinitions.processDefaultNode
}]
};
Expand Down
7 changes: 6 additions & 1 deletion lib/should-process-node-definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ function shouldProcessEveryNode(node) {
return true;
}

function shouldProcessEveryNodeExceptWhiteSpace(node) {
return node.type !== 'text' || node.data.trim();
}

module.exports = {
shouldProcessEveryNode: shouldProcessEveryNode
shouldProcessEveryNode: shouldProcessEveryNode,
shouldProcessEveryNodeExceptWhiteSpace: shouldProcessEveryNodeExceptWhiteSpace
};
41 changes: 41 additions & 0 deletions test/html-to-react-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,47 @@ describe('Html2React', function() {

assert.equal(reactHtml, htmlExpected);
});

it('should not insert spans into tables even if there is white space', function() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, this test doesn't add anything except verify that whitespace text nodes are stripped.

var htmlInput = '<table> <tbody> <tr>\n<td>x</td> <td>3</td> </tr> </tbody> </table>';
var htmlExpected = '<table><tbody><tr><td>x</td><td>3</td></tr></tbody></table>';

var reactComponent = parser.parse(htmlInput);
var reactHtml = ReactDOMServer.renderToStaticMarkup(reactComponent);

assert.equal(reactHtml, htmlExpected);
});

it('should preserve leading white space in a div', function() {
var htmlInput = '<div> hi</div>';

var reactComponent = parser.parse(htmlInput);
var reactHtml = ReactDOMServer.renderToStaticMarkup(reactComponent);

assert.equal(reactHtml, htmlInput);
});

// Should it strip out contents of a div containing only white space?
it('should strip out contents of a div containing only white space', function() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we actually remove text nodes that are whitespace only? The comment seems to indicate the PR author isn't sure either.

var htmlInput = '<div> </div>';
var htmlExpected = '<div></div>';

var reactComponent = parser.parse(htmlInput);
var reactHtml = ReactDOMServer.renderToStaticMarkup(reactComponent);

assert.equal(reactHtml, htmlExpected);
});

// Should it strip out white space that is a sibling to an element?
it('should strip out white space that is a sibling to an element', function() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this make any semantic difference compared to just stripping whitespace text nodes?

var htmlInput = '<div> <span>hi</span>bye</div>';
var htmlExpected = '<div><span>hi</span>bye</div>';

var reactComponent = parser.parse(htmlInput);
var reactHtml = ReactDOMServer.renderToStaticMarkup(reactComponent);

assert.equal(reactHtml, htmlExpected);
});
});

describe('parse invalid HTML', function() {
Expand Down