From 487d9ffc21c782469690e9d533a9db978cb22a56 Mon Sep 17 00:00:00 2001 From: Jordon Bedwell Date: Thu, 19 Nov 2015 18:46:03 -0600 Subject: [PATCH] Slightly speed up url sanitization and handle multiples of ///. --- lib/jekyll/url.rb | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/lib/jekyll/url.rb b/lib/jekyll/url.rb index 14b70631..6e8042b0 100644 --- a/lib/jekyll/url.rb +++ b/lib/jekyll/url.rb @@ -70,20 +70,11 @@ module Jekyll end end - # Returns a sanitized String URL - def sanitize_url(in_url) - url = in_url \ - # Remove all double slashes - .gsub(/\/\//, '/') \ - # Remove every URL segment that consists solely of dots - .split('/').reject{ |part| part =~ /^\.+$/ }.join('/') \ - # Always add a leading slash - .gsub(/\A([^\/])/, '/\1') + # Returns a sanitized String URL, stripping "../../" and multiples of "/", + # as well as the beginning "/" so we can enforce and ensure it. - # Append a trailing slash to the URL if the unsanitized URL had one - url << "/" if in_url.end_with?("/") - - url + def sanitize_url(str) + "/" + str.gsub(/\/{2,}/, "/").gsub(%r!\.+\/|\A/+!, "") end # Escapes a path to be a valid URL path segment