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

[question] How to pass pure html in toJSON() method? #23

Open
dariuszsikorski opened this issue Nov 22, 2016 · 2 comments
Open

[question] How to pass pure html in toJSON() method? #23

dariuszsikorski opened this issue Nov 22, 2016 · 2 comments

Comments

@dariuszsikorski
Copy link

dariuszsikorski commented Nov 22, 2016

I work on code editor and have following question:

DomJson Accepts html element as an argument for domJSON.toJSON(element)
Can i pass pure html string instead? domJSON.toJSON('<p>some code</p>') i tried that and it logs error length is undefined which may be because it expects html element, not a string.

@azaslavsky
Copy link
Owner

Sorry for the late reply. You are correct, it expects an HTML element. I'll look into adding support for strings, but under the hood all that would do is create new element, append the string as innerHTML, and then parse the resulting HTML element. In the meantime, you could probably just use the following code to achieve the same result:

var myHTMLString = '<p>some code</p><p>Uh-oh, a second node!</p>'; //Or whatever HTML
var div = document.createElement('div');
div.innerHTML = myHTMLString;
var frag = document.createDocumentFragment();

//Iterate over the nodes added to the div, and move them over to the fragment
Array.prototype.forEach.call(childNodes, function(node) {
  frag.appendChild(node);
});

//Pass the populated fragment to domJSON
domJSON.toJSON(frag);

@dariuszsikorski
Copy link
Author

Thank you for the response and code example, i think adding support for pure string along with htmlElement is a good idea, just for different use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants