Skip to content
This repository was archived by the owner on Apr 10, 2024. It is now read-only.
This repository was archived by the owner on Apr 10, 2024. It is now read-only.

Wrong parsing style attribute #28

@milenkovyua

Description

@milenkovyua

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions