Efficient way to copy a collection of Nodes, treat them, and then serialize?

Posted by Danjah on Stack Overflow See other posts from Stack Overflow or by Danjah
Published on 2011-02-06T07:17:57Z Indexed on 2011/02/06 7:26 UTC
Read the original article Hit count: 249

Hi all,

I initially thought a regex to remove YUI3 classNames (or whole class attributes) and id attributes from a serialized DOM string was a sound enough approach - but now I'm not sure, given various warnings about using regex on HTML.

I'm toying with the idea of making a copy of the DOM structure in question, performing:

var nodeStructure = Y.one('#wrap').all('*'); // A YUI3 NodeList

// Remove unwanted classNames.. I'd need to maintain a list of them to remove :/
nodeStructure.removeClass('unwantedClassName');

and then:

// I believe this can be done on a NodeList collection...
nodeStructure.removeAttribute('id');

I'm not quite sure about what I'd need to do to 'copy' a collection of Nodes anyway, as I don't actually want to do the above to my living markup, as its only being saved - not 'closed' or 'exited', a user could continue to change the markup, and then save again. The above doesn't make a copy, I know.

Is this efficient? Is there a better way to 'sanitize' my live markup of framework additions to the DOM (and maybe other things too at a later point), before saving it as a string? If it is a good approach, what's a safe way to go about copying my collection of Nodes for safe cleaning?

Thanks! d

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about dom