<commit_before>require 'spec_helper'

describe Muddle::Filter::BoilerplateCSS do
  let(:f) { Muddle::Filter::BoilerplateCSS }

  it "can parse full documents" do
    output = f.filter(minimal_email_body)

    xpath(output, '//head').count.should eql(1)
    xpath(output, '//style').count.should eql(2)
    xpath(output, 'html/head/style[@type="text/css"]').first.content.should include('Boilerplate CSS for Inlining')
    output.should have_xpath('/html/body/preceding-sibling::head')
  end

  it "adds <head> and a <style> tag if needed" do
    email = "<html><body><h1>foo</h1><body></html>"

    output = f.filter(email)

    output.should have_xpath('html')
    output.should have_xpath('html/head')
    output.should have_xpath('html/head/style[@type="text/css"]')
    xpath(output, 'html/head/style[@type="text/css"]').first.content.should include('Boilerplate CSS for Inlining')
    output.should have_xpath('/html/body/preceding-sibling::head')
  end

  it "adds the <style> tag before existing one" do
    email = "<html><head><style></style></head> <h1>foo</h1></html>"

    output = f.filter(email)

    xpath(output, 'html/head/style').count.should eql(2)
    xpath(output, 'html/head/style[@type="text/css"]').should be_true
    xpath(output, 'html/head/style[@type="text/css"]').first.content.should include("Boilerplate CSS for Inlining")
  end

  it "adds the <style> tag if none are defined" do
    email = "<html><head></head> <h1>foo</h1></html>"

    output = f.filter(email)

    xpath(output, 'html/head/style[@type="text/css"]').should be_true
    xpath(output, 'html/head/style[@type="text/css"]').first.content.should include('Boilerplate CSS for Inlining')
  end
end<commit_msg>Reorg the boilerplate css specs.<commit_after>require 'spec_helper'

describe Muddle::Filter::BoilerplateCSS do
  context "with a minimal email" do
    subject { Muddle::Filter::BoilerplateCSS.filter(minimal_email_body) }

    it "has only one head" do
      xpath(subject, '//head').count.should eql(1)
    end

    it "inserts an extra style tag" do
      xpath(subject, '//style').count.should eql(2)
    end

    it "inserts the boilerplate css into the head" do
      xpath(subject, 'html/head/style[@type="text/css"]').first.content.should include('Boilerplate CSS for Inlining')
    end

    it "keeps the head before the body" do
      subject.should have_xpath('/html/body/preceding-sibling::head')
    end
  end

  context "with no head tag" do
    let(:email) { "<html><body><h1>foo</h1><body></html>" }

    subject { Muddle::Filter::BoilerplateCSS.filter(email) }

    it "retains the html tag" do
      subject.should have_xpath('html')
    end

    it "adds a head tag inside the html tag" do
      subject.should have_xpath('html/head')
    end

    it "adds a style tag into the head tag" do
      subject.should have_xpath('html/head/style[@type="text/css"]')
    end

    it "puts the boilerplate css into the style tag" do
      xpath(subject, 'html/head/style[@type="text/css"]').first.content.should include('Boilerplate CSS for Inlining')
    end

    it "puts the head tag before the body" do
      subject.should have_xpath('/html/body/preceding-sibling::head')
    end
  end

  context "with an existing style in a head tag" do
    let(:email) { "<html><head><style></style></head> <h1>foo</h1></html>" }

    subject { Muddle::Filter::BoilerplateCSS.filter(email) }

    it "adds a second style tag" do
      xpath(subject, 'html/head/style').count.should eql(2)
    end

    it "inserts a style with type text/css" do
      xpath(subject, 'html/head/style').first.attribute('type').value.should == 'text/css'
    end

    it "inserts the boilerplate css into the first style tag" do
      xpath(subject, 'html/head/style').first.content.should include("Boilerplate CSS for Inlining")
    end
  end

  context "with a head and no style tag" do
    let(:email) { "<html><head></head> <h1>foo</h1></html>" }

    subject { Muddle::Filter::BoilerplateCSS.filter(email) }

    it "inserts a style tag" do
      xpath(subject, 'html/head/style[@type="text/css"]').should be_true
    end

    it "inserts the boilerplate css into the style tag" do
      xpath(subject, 'html/head/style[@type="text/css"]').first.content.should include('Boilerplate CSS for Inlining')
    end
  end
end
{'fraction_non_alphanumeric': 0.15015084706428405, 'fraction_numerical': 0.004177303318635414, 'mean_word_length': 3.7729789590254708, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1669490', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1417, 'n_words': 352}