Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it compatible with htmlparser format? #47

Closed
ollie-o opened this issue Jan 18, 2018 · 3 comments
Closed

Is it compatible with htmlparser format? #47

ollie-o opened this issue Jan 18, 2018 · 3 comments

Comments

@ollie-o
Copy link

ollie-o commented Jan 18, 2018

Is it possible for us to call the following?

// htmlparser format, not htmlparser2
const htmlparserDoc = [ { type: 'tag'
  , name: 'a'
  , attribs: { href: 'test.html' }
  , children: [ { data: 'hello world', type: 'text' } ]
  }
];

domToReact(htmlparserDoc, options);

The htmlparser2 format contains circular references, which prevents us from calling JSON.stringify() on it, so we'd like to use htmlparser if we can.

@remarkablemark
Copy link
Owner

@oliverodaa Should be possible:

$ node
> const domToReact = require('html-react-parser/lib/dom-to-react');
> const htmlparserDoc = [{ type: 'tag', name: 'a', attribs: { href: 'test.html' }, children: [{ data: 'hello world', type: 'text' }] }];
> domToReact(htmlparserDoc);
{ '$$typeof': Symbol(react.element),
  type: 'a',
  key: null,
  ref: null,
  props: { href: 'test.html', children: 'hello world' },
  _owner: null,
  _store: {} }

Let me know if you experience any issues; otherwise, please close the issue if it works.

@ollie-o
Copy link
Author

ollie-o commented Jan 22, 2018

Thank you for the quick response! I am still in the process of working on it but will be sure to close the issue when I have confirmed that it either works or doesn't work.

@ollie-o
Copy link
Author

ollie-o commented Jan 22, 2018

I can confirm that it does in fact work!

I ended up using htmlparser2 but manually removed parent, next, and prev before sending from my server to my client.

For anyone stumbling upon this, here is the code I used to strip those circular references:

JSON.stringify(obj,
    function(key, value) { 
        return key === 'parent' || key === 'next' || key === 'prev'
            ? undefined
            : value;
  });

@ollie-o ollie-o closed this as completed Jan 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants