link_header (~> 0.0, >= 0.0.8)
rdf-normalize (0.4.0)
rdf (~> 3.1)
+ redcarpet (3.5.1)
redis (4.2.5)
- redis-actionpack (5.2.0)
- actionpack (>= 5, < 7)
- redis-rack (>= 2.1.0, < 3)
- redis-store (>= 1.1.0, < 2)
- redis-activesupport (5.2.0)
- activesupport (>= 3, < 7)
- redis-store (>= 1.3, < 2)
redis-namespace (1.8.1)
redis (>= 3.0.4)
- redis-rack (2.1.3)
- rack (>= 2.0.8, < 3)
- redis-store (>= 1.2, < 2)
- redis-rails (5.0.2)
- redis-actionpack (>= 5.0, < 6)
- redis-activesupport (>= 5.0, < 6)
- redis-store (>= 1.2, < 2)
- redis-store (1.9.0)
- redis (>= 4, < 5)
regexp_parser (2.1.1)
request_store (1.5.0)
rack (>= 1.4)
rails-i18n (~> 5.1)
rails-settings-cached (~> 0.6)
rdf-normalize (~> 0.4)
+ redcarpet (~> 3.5)
redis (~> 4.2)
redis-namespace (~> 1.8)
- redis-rails (~> 5.0)
rqrcode (~> 1.2)
- rspec-rails (~> 4.1)
+ rspec-rails (~> 5.0)
rspec-sidekiq (~> 3.1)
rspec_junit_formatter (~> 0.4)
rubocop (~> 1.11)
# frozen_string_literal: true
require 'singleton'
- require_relative './sanitize_config'
+class HTMLRenderer < Redcarpet::Render::HTML
+ def block_code(code, language)
+ "<pre><code>#{encode(code).gsub("\n", "<br/>")}</code></pre>"
+ end
+
+ def autolink(link, link_type)
+ return link if link_type == :email
+ Formatter.instance.link_url(link)
+ rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
+ encode(link)
+ end
+
+ private
+
+ def html_entities
+ @html_entities ||= HTMLEntities.new
+ end
+
+ def encode(html)
+ html_entities.encode(html)
+ end
+end
+
class Formatter
include Singleton
include RoutingHelper
# frozen_string_literal: true
require 'rails_helper'
- require Rails.root.join('app', 'lib', 'sanitize_config.rb')
describe Sanitize::Config do
- describe '::MASTODON_STRICT' do
- subject { Sanitize::Config::MASTODON_STRICT }
-
- it 'converts h1 to p' do
- expect(Sanitize.fragment('<h1>Foo</h1>', subject)).to eq '<p>Foo</p>'
- end
-
- it 'converts ul to p' do
- expect(Sanitize.fragment('<p>Check out:</p><ul><li>Foo</li><li>Bar</li></ul>', subject)).to eq '<p>Check out:</p><p>Foo<br>Bar</p>'
- end
-
- it 'converts p inside ul' do
- expect(Sanitize.fragment('<ul><li><p>Foo</p><p>Bar</p></li><li>Baz</li></ul>', subject)).to eq '<p>Foo<br>Bar<br>Baz</p>'
+ shared_examples 'common HTML sanitization' do
+ it 'keeps h1' do
+ expect(Sanitize.fragment('<h1>Foo</h1>', subject)).to eq '<h1>Foo</h1>'
end
- it 'converts ul inside ul' do
- expect(Sanitize.fragment('<ul><li>Foo</li><li><ul><li>Bar</li><li>Baz</li></ul></li></ul>', subject)).to eq '<p>Foo<br>Bar<br>Baz</p>'
+ it 'keeps ul' do
+ expect(Sanitize.fragment('<p>Check out:</p><ul><li>Foo</li><li>Bar</li></ul>', subject)).to eq '<p>Check out:</p><ul><li>Foo</li><li>Bar</li></ul>'
end
- it 'keep links in lists' do
- expect(Sanitize.fragment('<p>Check out:</p><ul><li><a href="https://joinmastodon.org" rel="nofollow noopener noreferrer" target="_blank">joinmastodon.org</a></li><li>Bar</li></ul>', subject)).to eq '<p>Check out:</p><p><a href="https://joinmastodon.org" rel="nofollow noopener noreferrer" target="_blank">joinmastodon.org</a><br>Bar</p>'
+ it 'keeps start and reversed attributes of ol' do
+ expect(Sanitize.fragment('<p>Check out:</p><ol start="3" reversed=""><li>Foo</li><li>Bar</li></ol>', subject)).to eq '<p>Check out:</p><ol start="3" reversed=""><li>Foo</li><li>Bar</li></ol>'
end
it 'removes a without href' do