question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problems with the new Html deserializer

See original GitHub issue

Do you want to request a feature or report a bug?

A bug.

What’s the current behavior?

The new Html deserializer doesn’t work with the default parseHtml function. You can see the errors trying to paste some html in the paste html example.

First you have this error: Uncaught TypeError: _this.parseHtml is not a function

That could be resolved by passing explicitily null to Html constructor like this, but I think it should be fixed to also use the default parser if the options is undefined:

const serializer = new Html({
    rules: RULES,
    parseHtml: null
});

Then I created this fiddle to show the next error: https://jsfiddle.net/6u1e543z/

Uncaught TypeError: elements.filter is not a function

This happen because elements is a NodeList, not an Array so filter is not defined there.

I don’t know if there are more, couldn’t get past this one.

What’s the expected behavior?

The Html should work as before after updating the deserialize methods.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
lucadegaspericommented, Jul 21, 2017

Here’s my html serializer rules

const BLOCK_TAGS = {
    p: 'paragraph',
    ul: 'bulleted-list',
    ol: 'numbered-list',
    li: 'list-item',
    h3: 'heading-three',

};

const INLINE_TAGS = {
    a: 'link'
};

// Add a dictionary of mark tags.
const MARK_TAGS = {
    em: 'italic',
    strong: 'bold',
    u: 'underlined',
};

const rules = [
    {
        deserialize: function(el, next) {
            const type = BLOCK_TAGS[el.tagName];
            console.log(el);
            if (!type) { return; }
            return {
                kind: 'block',
                type: type,
                nodes: next(el.childNodes)
            };
        },
        serialize: function(object, children) {
            if (object.kind != 'block') { return; }
            console.log(object);
            switch (object.type) {
                case 'numbered-list':
                    return <ol>{children}</ol>;
                case 'bulleted-list':
                    return <ul>{children}</ul>;
                case 'list-item':
                    return <li>{children}</li>;
                case 'paragraph':
                    return <p>{children}</p>;
                case 'heading-three':
                    return <h3>{children}</h3>;
                case 'link':
                    return <a>{children}</a>;
            }
        }
    },
    // Add a new rule that handles marks...
    {
        deserialize: function(el, next) {
            const type = MARK_TAGS[el.tagName];
            if (!type) { return; }
            return {
                kind: 'mark',
                type: type,
                nodes: next(el.childNodes)
            };
        },
        serialize: function(object, children) {
            if (object.kind != 'mark') { return; }
            switch (object.type) {
                case 'bold':
                    return <strong>{children}</strong>;
                case 'italic':
                    return <em>{children}</em>;
                case 'underline':
                    return <u>{children}</u>;
            }
        }
    },
    {
        deserialize: function (el, next) {
            if (el.tagName != 'a') { return; }
            const type = INLINE_TAGS[el.tagName];

            if (!type) {
                return;
            }
            return {
                kind: 'inline',
                type: type,
                nodes: next(el.childNodes),
                data: {
                    href: el.attrs.find(({name}) => name == 'href').value
                }
            };
        },
        serialize: function (object, children) {

            if (object.kind != 'inline') {
                return;
            }

            switch (object.type) {
                case 'link':
                    return <a href={object.data.get('href')}>{children}</a>;
            }
        }
    },
];

const html = new Html({rules, parseHtml: null});

and this is the example I’m using

this.state = {
            state: html.deserialize('<p>In addition to block nodes</p>')
        };

initializing the state with the Raw serializer works fine

0reactions
enzofereycommented, May 31, 2018

For people arriving here one year later. I was running into the same issue in a tests environment with Docker + Karma + PhantomJS. PhantomJS didn’t have access to the DOMParser API so it would fail. I tried adding JSDOM.fragment as suggested in the docs but it wasn’t working. The workaround was to move away from PhantomJS to a jsdom environment throught https://github.com/badeball/karma-jsdom-launcher. No longer needed to specify a parseHtml prop and bye-bye to the deprecated PhantomJS !

Read more comments on GitHub >

github_iconTop Results From Across the Web

Serialization/deserialization problems
If you face a serialization/deserialization error, it is likely that you need to provide a custom serializer/deserializer.
Read more >
c# - Problem with Json Deserialization of html string using ...
At the moment I'm stuck thinking what I'm missing in order to correctly deserialize the HTML string within the Json from the Config...
Read more >
Deserialization - OWASP Cheat Sheet Series
Serialization is the process of turning some object into a data format that can be restored later. People often serialize objects in order...
Read more >
There was an error deserializing the object of type .. ...
I have just started making an app (following someone) using a movie API from tmdb. There appears to be an issue with the...
Read more >
Install error 0x80131500 Failed to deserialize packages
statejson.txtI'm trying to install the new Visual Studio 2017 RC on one on my machines. The first attempt failed during download because of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found