1

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

(This is the regex-related post on my first idea to clean up the serialized entire DOM structure: Safe regexs to clean serialized DOM?, that question contains some sample HTML, if you want it)

Community
  • 1
  • 1
danjah
  • 2,939
  • 2
  • 30
  • 47
  • 1
    "I'm not quite sure about what I'd need to do to 'copy' a collection of Nodes" - [`cloneNode`](https://developer.mozilla.org/En/DOM/Node.cloneNode)? – Yi Jiang Feb 06 '11 at 07:37
  • Eh, sorry, to clarify: I'm relying on YUI heavily to shield me from the pain of browsers as I put together my first web app beyond basic widgets - and I ask in this way because preferably I'd like to continue using YUI3 (or YUI2in3) to achieve this. Is cloneNode all consistent etc? I'll start reading... and is my approach around cleaning the NodeList sound? – danjah Feb 06 '11 at 07:54
  • I'm thinking nodeStructure.removeAttribute('id'); is not going to work, it's obviously undescriminating with ALL id attributes. I think I need to run a filter on the NodeList of some kind - remove the attribute mid-way through the filter and return the full NodeList - just without undesired id's? *scratches his head* That sounds way too complicated... – danjah Feb 06 '11 at 08:10

1 Answers1

0

"I'm not quite sure about what I'd need to do to 'copy' a collection of Nodes" Yi Jiang, you never came back :( Just answering this Q for completeness.

danjah
  • 2,939
  • 2
  • 30
  • 47