-
Notifications
You must be signed in to change notification settings - Fork 177
Wrong parsing style attribute #28
Description
When parsing from HTML to JSON, the style attribute is not parsed correctly. If there are more css values in "style", they are turned into array incorrectly. The text inside it uses spaces to turn the css into array. According to this the style:
background-color: #556677;padding-top: 10px
is turned into:
['background-color:','#556677;padding-top:','10px']
I fixed this by changing the following code:
`HTMLParser(html, {
start: function(tag, attrs, unary) {
debug(tag, attrs, unary);
// node for this element
var node = {
node: 'element',
tag: tag,
};
if (attrs.length !== 0) {
node.attr = attrs.reduce(function(pre, attr) {
var name = attr.name;
var value = attr.value;
// has multi attibutes
// make it array of attribute
if (value.match(/ /)) {
value = value.split(' ');
}`
with :
`HTMLParser(html, {
start: function(tag, attrs, unary) {
debug(tag, attrs, unary);
// node for this element
var node = {
node: 'element',
tag: tag,
};
if (attrs.length !== 0) {
node.attr = attrs.reduce(function(pre, attr) {
var name = attr.name;
var value = attr.value;
// has multi attibutes
// make it array of attribute
if (value.match(/;/)) {
value = value.split(';');
} else if (value.match(/ /)) {
value = value.split(' ');
}`
And it worked great. It also solves the one line css issue where css without any whitespaces will get parsed as single string, instead of as array. With a litle bit more code issue number #20 could be solved too, by doing second split with ':' and putting the result into object instead of array, without need for additional css library.