Skip to content

Commit

Permalink
Merge pull request #383 from openstax/allow_data_attributes
Browse files Browse the repository at this point in the history
Allow style data attributes everywhere
  • Loading branch information
Dantemss authored Oct 13, 2022
2 parents c5c7b6b + e241d8a commit 12694ca
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
9 changes: 7 additions & 2 deletions config/initializers/user_html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,20 @@
{ node_whitelist: [node] }
end

STYLE_DATA_ATTRIBUTES = %w(bullet-style type orient valign align media)
STYLE_ATTRIBUTES = STYLE_DATA_ATTRIBUTES.map { |attr| "data-#{attr}" }

UserHtml.sanitize_config = Sanitize::Config.merge(
Sanitize::Config::RELAXED,
add_attributes: {
'a' => {'rel' => 'nofollow', 'target' => '_blank'}
},
attributes: Sanitize::Config::RELAXED[:attributes].merge({
attributes: Sanitize::Config::RELAXED[:attributes].merge(
# :all has to be a symbol, not a string
all: Sanitize::Config::RELAXED[:attributes][:all] + STYLE_ATTRIBUTES,
'span' => ['data-math'],
'div' => ['data-math', 'align'],
'p' => ['align'],
}),
),
transformers: [embed_transformer]
)
13 changes: 11 additions & 2 deletions spec/lib/user_html_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
expect(described_class.sanitize(content)).to eq 'Funny cat videos: '
end

describe 'data-math attribute' do
context 'data-math attribute' do
let (:formula){ %-\lim_{x\to\infty}f(x)=0- }

it 'is allowed on divs' do
Expand All @@ -93,7 +93,16 @@
content = "also: <p data-math='#{formula}'/>"
expect(described_class.sanitize(content)).to eq 'also: <p></p>'
end

end

context 'style attributes' do
STYLE_ATTRIBUTES.each do |attr|
context "#{attr} attribute" do
it 'is allowed on any element' do
content = "<table><tbody><tr><td #{attr}=\"test\">Hi</td></tr></tbody></table>"
expect(described_class.sanitize(content)).to eq content
end
end
end
end
end

0 comments on commit 12694ca

Please sign in to comment.