Created
June 28, 2013 04:57
-
-
Save issa-tseng/5882546 to your computer and use it in GitHub Desktop.
goal html sanitizer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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