复制节点集合、处理它们然后序列化的有效方法?

发布于 2024-10-16 02:46:08 字数 920 浏览 8 评论 0原文

我最初认为使用正则表达式从序列化 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2024-10-23 02:46:08

“我不太确定需要做什么来‘复制’节点集合” 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文