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!
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>/,'')
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;
});