Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regex bug on client parser #24

Merged
merged 2 commits into from Sep 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
);
});
});

});