From be769dcf00874cb03da668a937a8967e1a49379d Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Thu, 3 Apr 2014 14:13:35 -0400 Subject: [PATCH] SANITIZE THE collection name plz. --- lib/jekyll/collection.rb | 6 +++++- test/test_collections.rb | 40 ++++++++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/lib/jekyll/collection.rb b/lib/jekyll/collection.rb index e7878962..b74af552 100644 --- a/lib/jekyll/collection.rb +++ b/lib/jekyll/collection.rb @@ -4,7 +4,7 @@ module Jekyll def initialize(site, label) @site = site - @label = label + @label = sanitize_label(label) end def docs @@ -38,6 +38,10 @@ module Jekyll "#" end + def sanitize_label(label) + label.gsub(/[^a-z0-9_\-]/i, '') + end + def to_liquid { "label" => label, diff --git a/test/test_collections.rb b/test/test_collections.rb index 06120790..d550536a 100644 --- a/test/test_collections.rb +++ b/test/test_collections.rb @@ -2,12 +2,28 @@ require 'helper' class TestCollections < Test::Unit::TestCase - context "with no collections specified" do - setup do - @site = Site.new(Jekyll.configuration({ + def fixture_site(overrides = {}) + Jekyll::Site.new(Jekyll.configuration( + overrides.merge({ "source" => source_dir, "destination" => dest_dir - })) + }) + )) + end + + context "a simple collection" do + setup do + @collection = Jekyll::Collection.new(fixture_site, "../../etc/password") + end + + should "sanitize the label name" do + assert_equal @collection.label, "etcpassword" + end + end + + context "with no collections specified" do + setup do + @site = fixture_site @site.process end @@ -18,11 +34,9 @@ class TestCollections < Test::Unit::TestCase context "with a collection" do setup do - @site = Site.new(Jekyll.configuration({ - "collections" => ["methods"], - "source" => source_dir, - "destination" => dest_dir - })) + @site = fixture_site({ + "collections" => ["methods"] + }) @site.process end @@ -49,12 +63,10 @@ class TestCollections < Test::Unit::TestCase context "in safe mode" do setup do - @site = Site.new(Jekyll.configuration({ + @site = fixture_site({ "collections" => ["methods"], - "safe" => true, - "source" => source_dir, - "destination" => dest_dir - })) + "safe" => true + }) @site.process @collection = @site.collections["methods"] end