Search code examples
javascriptruby-on-railsyuirichtextboxyui-editor

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


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!


Solution

  • 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>/,'')