【问题标题】:YUI Editor: Getting rid of <html> tags in contentYUI 编辑器:去除内容中的 <html> 标签
【发布时间】:2009-03-04 18:27:40
【问题描述】:
我实现了YUI rich text editor,一旦我从编辑器保存内容,我想摆脱<html>、<body> 和DOCTYPE 标签。我知道我可以通过解析 HTML 来做到这一点,但必须有更好的解决方案。
现在,当我在 YUI 编辑器中编辑文本时,它会保存:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<body>
<p>foo</p>
</body>
</html>
...但我想保存这个:
<p>foo</p>
有什么想法吗?
P.S.:我使用 yui_editor plugin for ruby on rails 实现了 YUI 编辑器,但也欢迎 YUI 编辑器通用答案!
【问题讨论】:
标签:
javascript
ruby-on-rails
yui
richtextbox
yui-editor
【解决方案1】:
与此同时,我通过在提交时解析 html 自己解决了这个问题。是的,我知道,起初我并没有在寻找这个解决方案,但最后我得出的结论是,这是解决它的最简单方法。我使用Nokogiri RubyGem for Rails 进行解析:
value = Nokogiri::HTML(yui_content).css('body').to_html
value.gsub!(/<body>/,'')
value.gsub!(/<\/body>/,'')
【解决方案2】:
一个解决方案是这样,它使用正则表达式来捕获<body> 和</body> 之间的所有内容。示例(从 YUI 编辑器页面修改):
var myEditor = new YAHOO.widget.Editor('msgpost');
myEditor.render();
YAHOO.util.Event.on('somebutton', 'click', function() {
myEditor.saveHTML();
//The var html will now have the contents of the textarea
var html = myEditor.get('element').value, match;
match = html.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
html = match ? match[1] : html;
});