Skip to content

Commit

Permalink
[Refactor] mount: use getHTMLFromHostNodes
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Mar 15, 2019
1 parent 8d4262b commit e695eb4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
12 changes: 8 additions & 4 deletions packages/enzyme/src/RSTTraversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,21 @@ export function getTextFromHostNodes(node, adapter) {
});
}

function getHTMLFromHostNode(hostNode) {
if (hostNode == null) {
return null;
}
return hostNode.outerHTML.replace(/\sdata-(reactid|reactroot)+="([^"]*)+"/g, '');
}

export function getHTMLFromHostNodes(node, adapter) {
if (node === null) return null;

const hostNode = adapter.nodeToHostNode(node, true);
if (hostNode === null) return null;

const nodeArray = Array.isArray(hostNode) ? hostNode : [hostNode];
const nodesHTML = nodeArray.map(item => (item === null
? null
: item.outerHTML.replace(/\sdata-(reactid|reactroot)+="([^"]*)+"/g, '')
));
const nodesHTML = nodeArray.map(item => getHTMLFromHostNode(item));

return nodesHTML.join('');
}
18 changes: 3 additions & 15 deletions packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
parentsOfNode,
treeFilter,
getTextFromHostNodes,
getHTMLFromHostNodes,
} from './RSTTraversal';

import { buildPredicate, reduceTreesBySelector } from './selectors';
Expand Down Expand Up @@ -586,21 +587,8 @@ class ReactWrapper {
* @returns {String}
*/
html() {
return this.single('html', (n) => {
if (n === null) return null;
const adapter = getAdapter(this[OPTIONS]);
const node = adapter.nodeToHostNode(n, true);

if (node === null) return null;

const nodeArray = Array.isArray(node) ? node : [node];
const nodesHTML = nodeArray.map(item => (item === null
? null
: item.outerHTML.replace(/\sdata-(reactid|reactroot)+="([^"]*)+"/g, '')
));

return nodesHTML.join('');
});
const adapter = getAdapter(this[OPTIONS]);
return this.single('html', n => getHTMLFromHostNodes(n, adapter));
}

/**
Expand Down

0 comments on commit e695eb4

Please sign in to comment.