复制节点集合、处理它们然后序列化的有效方法?
我最初认为使用正则表达式从序列化 DOM 字符串中删除 YUI3 classNames(或整个类属性)和 id 属性是一种足够合理的方法 - 但现在我不确定,因为有关于在 HTML 上使用正则表达式的各种警告。
我正在考虑复制有问题的 DOM 结构,执行:
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');
然后:
// I believe this can be done on a NodeList collection...
nodeStructure.removeAttribute('id');
我不太确定我需要做什么来“复制”节点集合,因为我不知道实际上不想对我的实时标记执行上述操作,因为它只是被保存 - 不是“关闭”或“退出”,用户可以继续更改标记,然后再次保存。我知道上面的内容不会复制。
这有效吗?在将其保存为字符串之前,是否有更好的方法来“清理”添加到 DOM 的框架的实时标记(也许稍后还会清理其他内容)?如果这是一个好方法,那么复制我的节点集合以进行安全清理的安全方法是什么?
谢谢! d
(这是关于我的第一个想法的与正则表达式相关的帖子,以清理序列化的整个 DOM 结构:安全正则表达式来清理序列化 DOM?,该问题包含一些示例 HTML,如果您需要的话)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“我不太确定需要做什么来‘复制’节点集合” Yi Jiang,你再也没有回来:( 为了完整起见,只是回答这个问题。
"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.