Hi!
I wanted to report an issue:
JSON values of HTML attributes are rewritten to an escaped value which breaks the HTML:
<div data-json='{
"json": "value"
}'></div>
Result of .toString():
<div data-json="{\"json\":\"value\"}"></div>
Edit
Since the goal of the HTML parser is speed, it may be best to replace JSON.stringify for HTML attributes with a simple string based value verification and leave the original value, even if it would be a mere space or empty string, intact. It could save 50,000+ JSON.stringify calls for some HTML documents.
For some attributes or Javascript functionality it does matter if the attribute contains ="". Stripping it would cost parsing resources while it seems to provide no other advantage than HTML compression, which does not seem to be a goal of the HTML parser.
The following example may provide a hint for a solution:
// Update rawString
const quoteRegex = /"/g; // re-use
this.rawAttrs = Object.keys(attrs).map(function(name) {
var val = attrs[name];
if (val === undefined) { // not a string
return name;
} else {
return name + '="' + val.replace(quoteRegex, '"') + '"';
}
}).join(' ');
Hi!
I wanted to report an issue:
JSON values of HTML attributes are rewritten to an escaped value which breaks the HTML:
Result of
.toString():Edit
Since the goal of the HTML parser is speed, it may be best to replace
JSON.stringifyfor HTML attributes with a simple string based value verification and leave the original value, even if it would be a mere space or empty string, intact. It could save 50,000+JSON.stringifycalls for some HTML documents.For some attributes or Javascript functionality it does matter if the attribute contains
="". Stripping it would cost parsing resources while it seems to provide no other advantage than HTML compression, which does not seem to be a goal of the HTML parser.The following example may provide a hint for a solution: