防止用户生成的内容破坏布局?
我有一个包含一些用户生成的内容的网站,我希望能够将布局的标记以及用户生成的内容的标记分开,这样 ug 内容就不会破坏网站布局。
用户生成的内容是可信的,因为它来自我网络上的已知用户组,但尽管如此,只允许一小部分 html 标签(p、ul/ol/li、em、strong 等) )。然而,用户生成的内容不能保证格式良好,并且我们遇到过一些格式错误的用户生成内容破坏网站布局的实例。
我们正在与用户合作,以保持内容格式良好,但与此同时,我正在尝试找到一种将内容与布局分开的好方法。我一直在研究命名空间,但无法找到有关嵌入式命名空间的 CSS 支持的良好文档。
有人有什么好主意吗?
编辑
我在这里看到了一些非常好的建议,但我可能应该澄清一下,我完全没有控制用户使用的输入机制。他们将内容输入一个系统,而我的页面使用该系统的 API 从中提取内容。该系统正在使用 TinyMCE,但正如我所说,我们仍然收到一些格式错误的内容。
I have a site that wraps some user-generated content, and I want to be able to separate the markup for the layout, and the markup from the user-generated content, so the u-g content can't break the site layout.
The user-generated content is trusted, as it is coming from a known group of users on my network, but nonetheless only a small subset of html tags are allowed (p, ul/ol/li, em, strong, and a couple more). However, the user-generated content is not guaranteed to be well-formed, and we have had some instances of malformed user-generated content breaking the layout of the site.
We are working with our users to keep the content well-formed, but in the meantime I am trying to find a good way to separate the content from the layout. I have been looking into namespaces, but have been unable to find good documentation about CSS support for embedded namespaces.
Anyone have any good ideas?
EDIT
I have seen some really good suggestions here, but I should probably clarify that I have absolutely no control over the input mechanism that the users use. They are entering content into one system, and my page uses that system's API to pull content out of it. That system is using TinyMCE, but like I said, we are still getting some malformed content.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不使用 markdown
如果您的用户懂得 HTML 或能够掌握 markdown 语法概念的人,我建议您选择使用 markdown。 Stackoverflow 与它配合得很好。我无法想象 Stackoverflow 上有一个普通的富编辑器。 Markdown 编辑器使用起来更加简单、快捷,并且为大多数情况提供了足够的格式化功能。如果您需要一些特殊的附加功能,您可以随时添加这些功能,但对于初学者来说,开箱即用的功能就足够了。
用于自我验证的实时视图
但不要忘记包含用户正在编写的内容的实时视图。自我验证创造奇迹,因此他们在发布数据之前纠正自己的错误。
Why not use markdown
If your users are HTML literate or people that can grasp the concept of markdown syntax I suggest you rather go with that. Stackoverflow works great with it. I can't imagine having a usual rich editor on Stackoverflow. Markdown editors are much simpler and faster to use and provide enough formatting capabilities for most situations. If you need some special additional features you can always add those in but for starters oute of the box capabilities will suffice.
Real-time view for self validation
But don't forget to include a real time view of what users are writing. Self validation makes miracles so they correct their own mistakes before posting data.
无需解析结果或强制用户使用结构化格式,只需在 iframe 中显示内容即可:
Instead of parsing the result or forcing the user to use a structured format, just display the content within an iframe:
我专门构建了定制 CMS 系统多年,并且总是很幸运地结合了高质量的所见即所得、强大的前端验证和不懈的后端验证。
我总是倾向于 CKEditor,因为它是唯一可以在前端处理 Microsoft Word 输出的前端编辑器……这是我书中的必备工具。当然,其他人有来自 word 解决方案的粘贴,但祝用户使用它好运。实际上,由于 Microsoft Word 没有在 Tiny 中进行清理,我确实遇到过客户端过载数据库插入的情况。 HTML tidy 是一个很好的解决方案,可以在后端验证之前清理内容。
CK 有内置的模板和类,所以我使用它们来帮助我的用户格式化而不过度。在后端,我检查以确保他们没有尝试使用 CSS 进行任何有趣的业务,但这从来不是那群用户所关心的问题。给他们足够的(安全)功能,他们将永远不必变得流氓。
I built custom CMS systems exclusively for several years and always had great luck with a combination of a quality WYSIWYG, strong front-end validation, and relentless back-end validation.
I always gravitate toward CKEditor because it's the only front-end editor that can deal with Microsoft Word output on the front end...that's a must-have in my books. Sure, others have a paste from word solution, but good luck getting users to use it. I've actually had a client overload a db insert thanks to Microsoft Word that didn't get scrubbed in Tiny. HTML tidy is a great solution to clean things up prior to validation on the back end.
CK has built-in templates and classes, so I used those to help my users format without going overboard. On the back-end I checked to ensure they hadn't tried any funny business with CSS, but it was never a concern with that group of users. Give them enough (safe) features and they'll never HAVE to go rogue.
也许有点过头了,但是 HTML
整洁
如果你能使用它,可能会有所帮助。
使用所见即所得的方式,例如
TinyMCE
或 CKEditor 已内置清理方法。
Robert Koritnik 使用 Markdown 的建议似乎很棒,尤其是考虑到您只允许一些无害的格式标记。
我认为 CSS 无法阻止布局因开放 HTML 标签而被破坏,所以我可能会忘记这个想法。
Maybe overkill, but HTML
Tidy
could help if you can use it.
Use a WYSIWYG like
TinyMCE
or CKEditor that has built in cleanup methods.
Robert Koritnik's suggestion to use markdown seems brilliant, especially considering that you only allow a few harmless formatting tags.
I don't think there's anything you can do with CSS to stop layouts from breaking due to open HTML tags, so I would probably forget that idea.