Skip to content

Instantly share code, notes, and snippets.

@mironov
Created November 30, 2009 16:55
Show Gist options
  • Save mironov/245549 to your computer and use it in GitHub Desktop.
Save mironov/245549 to your computer and use it in GitHub Desktop.
span_trans = lambda do |el|
if el[:node].name == "span"
if el[:node].has_attribute?("style")
rules = el[:node].attribute("style").value.split(";")
new_rules = rules.map do |rule|
rule.match(/text-decoration:\s*(underline|overline|line-through)\b/) ||
rule.match(/font-weight:\s*bold\b/) ||
rule.match(/font-style:\s*italic\b/)
end
new_style = new_rules.compact.map{|r| r.to_s}.join("; ")
end
if new_style == "" || !el[:node].has_attribute?("style")
el[:node].children.each { |n| el[:node].add_previous_sibling(n) }
el[:node].unlink
end
end
{:node => el[:node]}
end
@kbighorse
Copy link

Curious, how did you test this? I'm trying to adapt this to whitelist all style attributes, plus text-align values, but it's not working calling through Sanitize.

@kbighorse
Copy link

Also, I'm not seeing where the new_style is being added back to the node, am I missing something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment