Skip to content

Commit

Permalink
Merge pull request #24 from remarkablemark/bug-client-parser
Browse files Browse the repository at this point in the history
Fix regex bug on client parser
  • Loading branch information
remarkablemark committed Sep 27, 2016
2 parents 646b4a8 + 1bd17dd commit 0bc51e0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/html-to-dom-client.js
Expand Up @@ -102,13 +102,14 @@ function formatDOM(nodes, parentNode) {
* @return {Object} - The DOM nodes.
*/
function htmlToDOMClient(html) {
var match = typeof html === 'string' ? html.match(/<(.+?)>/) : null;
// from `<p>` or `<p style="">` get `p`
var match = typeof html === 'string' ? html.match(/<(.+?)[>\s]/) : null;
var tagName;
var parentNode;
var nodes;

if (match && typeof match[1] === 'string') {
tagName = match[1].toLowerCase();
tagName = match[1].trim().toLowerCase();
}

// `DOMParser` can parse full HTML
Expand Down
28 changes: 27 additions & 1 deletion test/html-to-dom.js
Expand Up @@ -55,9 +55,35 @@ describe('html-to-dom', function() {
data.html.comment +
data.html.script
);
helpers.deepEqualCircular(htmlToDOMClient(html), htmlToDOMServer(html));

helpers.deepEqualCircular(
htmlToDOMClient(html),
htmlToDOMServer(html)
);
});

it('works with `window.DOMParser`', function() {
var html = (
data.html.attributes +
data.html.nested +
data.html.comment +
data.html.script
);

// mock `window.DOMParser`
// https://developer.mozilla.org/en-US/docs/Web/API/DOMParser
window.DOMParser = function() {
this.parseFromString = function(html) {
jsdomify.create(html);
return document;
};
};

helpers.deepEqualCircular(
htmlToDOMClient(html),
htmlToDOMServer(html)
);
});
});

});

0 comments on commit 0bc51e0

Please sign in to comment.