condensation 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .DS_Store
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in condensation.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Derrick Reimer
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Condensation
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'condensation'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install condensation
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "lib"
7
+ t.pattern = "spec/**/*_spec.rb"
8
+ t.verbose = true
9
+ end
10
+
11
+ desc "Run tests"
12
+ task :default => :test
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'condensation/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "condensation"
8
+ gem.version = Condensation::VERSION
9
+ gem.authors = ["Derrick Reimer"]
10
+ gem.email = ["derrickreimer@gmail.com"]
11
+ gem.description = %q{A collection of handy extensions to the Liquid templating language}
12
+ gem.summary = %q{Condensation is a collection of handy extensions to the Liquid templating language}
13
+ gem.homepage = "https://github.com/djreimer/condensation"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "liquid", "~> 2.0"
21
+ gem.add_development_dependency "shoulda-context", "~> 1.0"
22
+ end
@@ -0,0 +1,10 @@
1
+ module Condensation
2
+ module Filters
3
+ module Default
4
+ def default(input, default_value = "")
5
+ is_blank = input.respond_to?(:empty?) ? input.empty? : !input
6
+ is_blank ? default_value : input
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ module Condensation
2
+ module Filters
3
+ module Hyperlink
4
+ def hyperlink(input, text = nil)
5
+ return if input.nil?
6
+ input = Sanitizer.new(input.to_s).escape_html
7
+ text = text ? Sanitizer.new(text).escape_html : input
8
+ "<a href='#{input}'>#{text}</a>"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Condensation
2
+ module Filters
3
+ module ReplaceInnerHTML
4
+ def replace_inner_html(input, inner_html = "")
5
+ return if input.nil?
6
+ input.to_s.gsub(/\A(<\S.*>)(.*)(<\/\S*>)\z/) do
7
+ "#{$1}#{inner_html}#{$3}"
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ require "condensation/filters/default"
2
+ require "condensation/filters/hyperlink"
3
+ require "condensation/filters/replace_inner_html"
@@ -0,0 +1,13 @@
1
+ module Condensation
2
+ class Sanitizer
3
+ attr_reader :input
4
+
5
+ def initialize(input)
6
+ @input = input
7
+ end
8
+
9
+ def escape_html
10
+ CGI.escapeHTML(input) rescue input
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Condensation
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,17 @@
1
+ require "liquid"
2
+ require "condensation/sanitizer"
3
+ require "condensation/filters"
4
+ require "condensation/version"
5
+
6
+ module Condensation
7
+ FILTERS = [
8
+ Filters::Default,
9
+ Filters::ReplaceInnerHTML
10
+ ]
11
+
12
+ def register_filters
13
+ FILTERS.each do |filter|
14
+ Liquid::Template.register_filter(filter)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,45 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ describe Condensation::Filters::Default do
4
+ def render_with_filter(template, context)
5
+ template.render(context, :filters => [Condensation::Filters::Default])
6
+ end
7
+
8
+ describe "#default" do
9
+ it "should return input if it is not blank" do
10
+ template = Liquid::Template.parse("{{ name | default: 'Bar' }}")
11
+ result = render_with_filter(template, { "name" => "Foo" })
12
+ result.must_equal "Foo"
13
+ end
14
+
15
+ it "should return default value if input is nil" do
16
+ template = Liquid::Template.parse("{{ name | default: 'Bar' }}")
17
+ result = render_with_filter(template, { "name" => nil })
18
+ result.must_equal "Bar"
19
+ end
20
+
21
+ it "should return default value if input is an empty string" do
22
+ template = Liquid::Template.parse("{{ name | default: 'Bar' }}")
23
+ result = render_with_filter(template, { "name" => "" })
24
+ result.must_equal "Bar"
25
+ end
26
+
27
+ it "should return default value if input is an empty hash" do
28
+ template = Liquid::Template.parse("{{ name | default: 'Bar' }}")
29
+ result = render_with_filter(template, { "name" => {} })
30
+ result.must_equal "Bar"
31
+ end
32
+
33
+ it "should return default value if input is an empty array" do
34
+ template = Liquid::Template.parse("{{ name | default: 'Bar' }}")
35
+ result = render_with_filter(template, { "name" => [] })
36
+ result.must_equal "Bar"
37
+ end
38
+
39
+ it "should return default value if input is false" do
40
+ template = Liquid::Template.parse("{{ name | default: 'Bar' }}")
41
+ result = render_with_filter(template, { "name" => false })
42
+ result.must_equal "Bar"
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,52 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ describe Condensation::Filters::Hyperlink do
4
+ def render_with_filter(template, context)
5
+ template.render(context, :filters => [Condensation::Filters::Hyperlink])
6
+ end
7
+
8
+ describe "#hyperlink" do
9
+ it "should use the URL as the anchor text if none provided" do
10
+ url = "http://www.example.com"
11
+ template = Liquid::Template.parse("{{ url | hyperlink }}")
12
+ result = render_with_filter(template, { "url" => url })
13
+ result.must_equal "<a href='#{url}'>#{url}</a>"
14
+ end
15
+
16
+ it "should use the provided anchor text" do
17
+ url = "http://www.example.com"
18
+ template = Liquid::Template.parse("{{ url | hyperlink: 'Click here!' }}")
19
+ result = render_with_filter(template, { "url" => url })
20
+ result.must_equal "<a href='#{url}'>Click here!</a>"
21
+ end
22
+
23
+ it "should escape HTML in the URL" do
24
+ url = "<script>attack.me()</script>"
25
+ escaped = "&lt;script&gt;attack.me()&lt;/script&gt;"
26
+ template = Liquid::Template.parse("{{ url | hyperlink }}")
27
+ result = render_with_filter(template, { "url" => url })
28
+ result.must_equal "<a href='#{escaped}'>#{escaped}</a>"
29
+ end
30
+
31
+ it "should escape HTML in the anchor text" do
32
+ url = "http://www.example.com"
33
+ text = "<script>attack.me()</script>"
34
+ escaped = "&lt;script&gt;attack.me()&lt;/script&gt;"
35
+ template = Liquid::Template.parse("{{ url | hyperlink: '#{text}' }}")
36
+ result = render_with_filter(template, { "url" => url })
37
+ result.must_equal "<a href='#{url}'>#{escaped}</a>"
38
+ end
39
+
40
+ it "should return empty string if input is nil" do
41
+ template = Liquid::Template.parse("{{ url | hyperlink }}")
42
+ result = render_with_filter(template, {})
43
+ result.must_equal ""
44
+ end
45
+
46
+ it "should handle numeric input" do
47
+ template = Liquid::Template.parse("{{ url | hyperlink: 'Bar' }}")
48
+ result = render_with_filter(template, { "url" => 1 })
49
+ result.must_equal "<a href='1'>Bar</a>"
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ describe Condensation::Filters::ReplaceInnerHTML do
4
+ def render_with_filter(template, context)
5
+ template.render(context, :filters => [Condensation::Filters::ReplaceInnerHTML])
6
+ end
7
+
8
+ describe "#replace_inner_html" do
9
+ it "should replace the inner content of a given HTML tag" do
10
+ tag = "<a href='http://www.example.com'>Foo</a>"
11
+ template = Liquid::Template.parse("{{ example_link | replace_inner_html: 'Bar' }}")
12
+ result = render_with_filter(template, { "example_link" => tag })
13
+ result.must_equal "<a href='http://www.example.com'>Bar</a>"
14
+ end
15
+
16
+ it "should not alter malformed input" do
17
+ tags = [
18
+ "< a href='http://www.example.com'>Foo</a>",
19
+ "<a href='http://www.example.com'>Foo< /a>",
20
+ "<a href='http://www.example.com'>Foo",
21
+ " <a href='http://www.example.com'>Foo</a> "
22
+ ]
23
+
24
+ tags.each do |tag|
25
+ template = Liquid::Template.parse("{{ example_link | replace_inner_html: 'Bar' }}")
26
+ result = render_with_filter(template, { "example_link" => tag })
27
+ result.must_equal tag
28
+ end
29
+ end
30
+
31
+ it "should return empty string if input is nil" do
32
+ template = Liquid::Template.parse("{{ example_link | replace_inner_html: 'Bar' }}")
33
+ result = render_with_filter(template, {})
34
+ result.must_equal ""
35
+ end
36
+
37
+ it "should handle numeric input" do
38
+ template = Liquid::Template.parse("{{ example_link | replace_inner_html: 'Bar' }}")
39
+ result = render_with_filter(template, { "example_link" => 1 })
40
+ result.must_equal "1"
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe Condensation::Sanitizer do
4
+ describe "#escape_html" do
5
+ it "should escape HTML entities" do
6
+ html = "<div><a href='http://foo.bar/?x=r%20b&y=z'>Baz</a></div>"
7
+ escaped = "&lt;div&gt;&lt;a href='http://foo.bar/?x=r%20b&amp;y=z'&gt;Baz&lt;/a&gt;&lt;/div&gt;"
8
+ Condensation::Sanitizer.new(html).escape_html.must_equal(escaped)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe Condensation do
4
+ describe "#register_filters" do
5
+ # TODO
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ $:.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'condensation'
4
+ require 'minitest/spec'
5
+ require 'minitest/autorun'
6
+ require 'minitest/pride'
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: condensation
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Derrick Reimer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: liquid
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: shoulda-context
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.0'
46
+ description: A collection of handy extensions to the Liquid templating language
47
+ email:
48
+ - derrickreimer@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - condensation.gemspec
59
+ - lib/condensation.rb
60
+ - lib/condensation/filters.rb
61
+ - lib/condensation/filters/default.rb
62
+ - lib/condensation/filters/hyperlink.rb
63
+ - lib/condensation/filters/replace_inner_html.rb
64
+ - lib/condensation/sanitizer.rb
65
+ - lib/condensation/version.rb
66
+ - spec/condensation/filters/default_spec.rb
67
+ - spec/condensation/filters/hyperlink_spec.rb
68
+ - spec/condensation/filters/replace_inner_html_spec.rb
69
+ - spec/condensation/sanitizer_spec.rb
70
+ - spec/condensation_spec.rb
71
+ - spec/spec_helper.rb
72
+ homepage: https://github.com/djreimer/condensation
73
+ licenses: []
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 1.8.23
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Condensation is a collection of handy extensions to the Liquid templating
96
+ language
97
+ test_files:
98
+ - spec/condensation/filters/default_spec.rb
99
+ - spec/condensation/filters/hyperlink_spec.rb
100
+ - spec/condensation/filters/replace_inner_html_spec.rb
101
+ - spec/condensation/sanitizer_spec.rb
102
+ - spec/condensation_spec.rb
103
+ - spec/spec_helper.rb