YUI Editor: Getting rid of <html> tags in content

泄露秘密 提交于 2019-12-11 15:56:26

问题


I implemented the YUI rich text editor and I would like to get rid of the <html>, <body> and DOCTYPE tags as soon as I save the content from the editor. I know I could do this afterwards by parsing the HTML, but there must be a better solution.

Right now this is saved when I edit a text in the YUI editor:

<!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>

...but I would like to just save this:

<p>foo</p>

Any idea?

P.S.: I implemented the YUI editor using the yui_editor plugin for ruby on rails, but a YUI editor generic answer would be welcome too!


回答1:


In the meantime I solved the problem myself by parsing the html on submit. Yes I know, I wasn't looking for this solution at first, but finally I came to the conclusion that it is the easiest way to solve it. I used the Nokogiri RubyGem for Rails to do the parsing:

value = Nokogiri::HTML(yui_content).css('body').to_html 
value.gsub!(/<body>/,'') 
value.gsub!(/<\/body>/,'')



回答2:


One solution would be this, it uses regular expressions to catch everything between <body> and </body>. Example (modified from the YUI editor page):

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;
});


来源:https://stackoverflow.com/questions/611843/yui-editor-getting-rid-of-html-tags-in-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!