Skip to content

Commit 0bc51e0

Browse files
Merge pull request #24 from remarkablemark/bug-client-parser
Fix regex bug on client parser
2 parents 646b4a8 + 1bd17dd commit 0bc51e0

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

lib/html-to-dom-client.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,14 @@ function formatDOM(nodes, parentNode) {
102102
* @return {Object} - The DOM nodes.
103103
*/
104104
function htmlToDOMClient(html) {
105-
var match = typeof html === 'string' ? html.match(/<(.+?)>/) : null;
105+
// from `<p>` or `<p style="">` get `p`
106+
var match = typeof html === 'string' ? html.match(/<(.+?)[>\s]/) : null;
106107
var tagName;
107108
var parentNode;
108109
var nodes;
109110

110111
if (match && typeof match[1] === 'string') {
111-
tagName = match[1].toLowerCase();
112+
tagName = match[1].trim().toLowerCase();
112113
}
113114

114115
// `DOMParser` can parse full HTML

test/html-to-dom.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,35 @@ describe('html-to-dom', function() {
5555
data.html.comment +
5656
data.html.script
5757
);
58-
helpers.deepEqualCircular(htmlToDOMClient(html), htmlToDOMServer(html));
58+
59+
helpers.deepEqualCircular(
60+
htmlToDOMClient(html),
61+
htmlToDOMServer(html)
62+
);
5963
});
6064

65+
it('works with `window.DOMParser`', function() {
66+
var html = (
67+
data.html.attributes +
68+
data.html.nested +
69+
data.html.comment +
70+
data.html.script
71+
);
72+
73+
// mock `window.DOMParser`
74+
// https://developer.mozilla.org/en-US/docs/Web/API/DOMParser
75+
window.DOMParser = function() {
76+
this.parseFromString = function(html) {
77+
jsdomify.create(html);
78+
return document;
79+
};
80+
};
81+
82+
helpers.deepEqualCircular(
83+
htmlToDOMClient(html),
84+
htmlToDOMServer(html)
85+
);
86+
});
6187
});
6288

6389
});

0 commit comments

Comments
 (0)