# frozen_string_literal: true require 'spec_helper' RSpec.describe HtmlSanitizer do subject { described_class } context "when HTML has supported tags" do it "keeps supported regular tags" do supported_tags = %w[h1 h2 h3 h4 div p b i u a strong em del pre blockquote ul ol li figure] supported_tags.each do |tag| html = "<#{tag}>Content" sanitized_html = subject.sanitize(html) expect(sanitized_html).to eq(html), "Expected '#{tag}' to be preserved." end end it "keeps supported void tags" do supported_tags = %w[br hr] supported_tags.each do |tag| html = "<#{tag}>" sanitized_html = subject.sanitize(html) expect(sanitized_html).to eq(html), "Expected '#{tag}' to be preserved." end end it "handles nested tags" do html = '
' expect(subject.sanitize(html)).to eq(html) end end context "when HTML has dangerous tags" do it "removes script tags" do html = "Hello !" expect(subject.sanitize(html)).to eq "Hello alert!" end it "removes iframe tags" do html = "Content " expect(subject.sanitize(html)).to eq "Content " end it "removes object tags" do html = "" expect(subject.sanitize(html)).to eq "" end it "removes embed tags" do html = "" expect(subject.sanitize(html)).to eq "" end it "removes link tags" do html = "" expect(subject.sanitize(html)).to eq "" end it "removes base tags" do html = "" expect(subject.sanitize(html)).to eq "" end it "removes form tags" do html = "
...
" expect(subject.sanitize(html)).to eq "..." end it "removes combined dangerous tags" do html = "" expect(subject.sanitize(html)).to eq "alert" end end context "when HTML has supported attributes" do it "keeps supported attributes" do html = 'Hello alert!' expect(subject.sanitize(html)) .to eq 'Hello alert!' end end context "when HTML has dangerous attributes" do it "removes unsupported attributes" do html = 'Hello alert!' expect(subject.sanitize(html)) .to eq 'Hello alert!' end it "removes dangerous attribute values" do html = 'Hello you!' expect(subject.sanitize(html)) .to eq 'Hello you!' end it "keeps only Trix-specific data attributes" do html = '
...
' expect(subject.sanitize(html)).to eq('
...
') end end context "when HTML has links" do describe "#sanitize" do it "doesn't add target blank to links" do html = 'Link' expect(subject.sanitize(html)).to eq('Link') end end describe "#sanitize_and_enforece_link_target_blank" do it "adds target blank to links so they open in new windows" do html = 'Link' expect(subject.sanitize_and_enforce_link_target_blank(html)) .to eq('Link') end end end end