Skip to content

Instantly share code, notes, and snippets.

@issa-tseng
Created June 28, 2013 04:57
Show Gist options
  • Save issa-tseng/5882546 to your computer and use it in GitHub Desktop.
Save issa-tseng/5882546 to your computer and use it in GitHub Desktop.
goal html sanitizer.
goals.each(function(model)
{
var markup = model.get('description')
if (markup)
{
markup = markup.replace(/\n/g, '').replace(/ /g, ' ').replace(/ +/g, ' ');
var $markup = $(markup);
$wrapper = $('<div/>').append($markup)
$wrapper.find('div').each(function()
{
var $this = $(this);
if ($this.find('p').length > 0)
$this.replaceWith($this.contents());
else
$this.replaceWith($('<p/>').append($this.contents()));
});
$wrapper.find('> span').each(function()
{
var $this = $(this);
if ($this.find('p').length > 0)
$this.replaceWith($this.contents());
else
$this.replaceWith($('<p/>').append($this.contents()));
});
$wrapper.find('span').each(function()
{
var $this = $(this);
if ($this.children().length === 0 && $this.text().replace(/ |\u00a0/g, '') === '')
$this.remove();
else
$this.replaceWith($this.contents());
});
$wrapper.find('p').each(function()
{
var $this = $(this);
if ($this.children().length === 0 && $this.text().replace(/ |\u00a0/g, '') === '')
$this.remove();
});
$wrapper.find('br').remove();
$wrapper.find('*').attr('style', null);
goal.set('description', $wrapper.html());
goal.save();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment