walters 0.1.0-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +14 -0
- data/LICENSE-EscapeUtils.txt +20 -0
- data/LICENSE-houdini.txt +14 -0
- data/README.md +52 -0
- data/Rakefile +71 -0
- data/benchmark/html_clean_escape.rb +81 -0
- data/benchmark/html_escape.rb +78 -0
- data/lib/walters.rb +2 -0
- data/lib/walters/html/cgi.rb +12 -0
- data/lib/walters/html/erb.rb +13 -0
- data/lib/walters/html/haml.rb +10 -0
- data/lib/walters/html/html_safety.rb +19 -0
- data/lib/walters/html/rack.rb +11 -0
- data/lib/walters/javascript/action_view.rb +11 -0
- data/lib/walters/version.rb +3 -0
- data/lib/walters/walters.rb +23 -0
- data/lib/walters/walters_ext.jar +0 -0
- data/lib/walters/walters_xni.rb +109 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/walters/html_escape_spec.rb +58 -0
- data/spec/walters/javascript_escape_spec.rb +49 -0
- metadata +66 -0
data/LICENSE
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
2
|
+
you may not use this file except in compliance with the License.
|
3
|
+
You may obtain a copy of the License at
|
4
|
+
|
5
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
6
|
+
|
7
|
+
Unless required by applicable law or agreed to in writing, software
|
8
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
9
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
10
|
+
See the License for the specific language governing permissions and
|
11
|
+
limitations under the License.
|
12
|
+
|
13
|
+
|
14
|
+
This software also contains code licensed under the GNU General Public License version 2 with a linking exception.
|
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010-2013 Brian Lopez - http://github.com/brianmario
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/LICENSE-houdini.txt
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
Copyright (C) 2012 Vicent Martí
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
4
|
+
documentation files (the "Software"), to deal in the Software without restriction, including without
|
5
|
+
limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
6
|
+
of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
7
|
+
|
8
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions
|
9
|
+
of the Software.
|
10
|
+
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
12
|
+
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
13
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
14
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
walters [](https://travis-ci.org/wmeissner/walters)
|
2
|
+
======
|
3
|
+
|
4
|
+
[Walters](https://github.com/wmeissner/walters) is a fast HTML (and href, uri, xml, javascript) escaping library for JRuby.
|
5
|
+
|
6
|
+
It is optimised for the non-escaping case - i.e. where the input string contains no characters requiring escaping.
|
7
|
+
|
8
|
+
### Example usage
|
9
|
+
|
10
|
+
jruby-1.7.4.dev :001 > require 'walters'
|
11
|
+
=> true
|
12
|
+
jruby-1.7.4.dev :002 > Walters.escape_html('<html>')
|
13
|
+
=> "<html>"
|
14
|
+
|
15
|
+
|
16
|
+
The same extension can also be used from the legacy CRuby VM for compatibility.
|
17
|
+
|
18
|
+
2.0.0p0 :001 > require 'walters'
|
19
|
+
=> true
|
20
|
+
2.0.0p0 :002 > Walters.escape_html('<html>')
|
21
|
+
=> "<html>"
|
22
|
+
|
23
|
+
### Benchmarks
|
24
|
+
Escaping 1000 bytes of text requiring escaping 1000000 times under jruby-1.7.4:
|
25
|
+
|
26
|
+
user system total real
|
27
|
+
Rack::Utils.escape_html 89.980000 0.230000 90.210000 ( 90.272000)
|
28
|
+
Haml::Helpers.html_escape 50.420000 0.170000 50.590000 ( 51.147000)
|
29
|
+
ERB::Util.html_escape 44.650000 0.130000 44.780000 ( 45.518000)
|
30
|
+
CGI.escapeHTML 36.230000 0.090000 36.320000 ( 36.358000)
|
31
|
+
String#gsub 35.490000 0.090000 35.580000 ( 35.587000)
|
32
|
+
Walters.escape_html 10.090000 0.030000 10.120000 ( 10.126000)
|
33
|
+
|
34
|
+
Escaping 1000 bytes of html-free text 1000000 times under jruby-1.7.4:
|
35
|
+
|
36
|
+
user system total real
|
37
|
+
Rack::Utils.escape_html 3.530000 0.010000 3.540000 ( 3.601000)
|
38
|
+
Haml::Helpers.html_escape 4.140000 0.010000 4.150000 ( 4.196000)
|
39
|
+
ERB::Util.html_escape 11.710000 0.030000 11.740000 ( 11.876000)
|
40
|
+
CGI.escapeHTML 3.440000 0.010000 3.450000 ( 3.548000)
|
41
|
+
String#gsub 3.470000 0.000000 3.470000 ( 3.531000)
|
42
|
+
Walters.escape_html 0.660000 0.010000 0.670000 ( 0.663000)
|
43
|
+
|
44
|
+
### Punching ducks
|
45
|
+
There are monkey-patching shims supplied that replace common html escaping functions with calls to the Walters escape_html function.
|
46
|
+
|
47
|
+
e.g. to patch Rack::Utils.escape_html to use Walters.escape_html:
|
48
|
+
|
49
|
+
require 'walters/html/rack'
|
50
|
+
|
51
|
+
### Credits
|
52
|
+
The escaping routines are based on C code from the [Houdini](https://github.com/vmg/houdini) project, and the specs, benchmarks and monkey patches come from the [escape_utils](https://github.com/brianmario/escape_utils) project
|
data/Rakefile
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'rake/clean'
|
2
|
+
require 'rubygems/package_task'
|
3
|
+
require 'rubygems/tasks'
|
4
|
+
|
5
|
+
|
6
|
+
def gem_spec
|
7
|
+
@gem_spec ||= Gem::Specification.load('walters.gemspec')
|
8
|
+
end
|
9
|
+
|
10
|
+
TEST_DEPS = []
|
11
|
+
if defined?(JRUBY_VERSION)
|
12
|
+
require 'ant'
|
13
|
+
jar_file = 'lib/walters/walters_ext.jar'
|
14
|
+
CLEAN.include jar_file
|
15
|
+
TEST_DEPS << jar_file
|
16
|
+
|
17
|
+
gem_spec.files << jar_file
|
18
|
+
|
19
|
+
directory 'pkg/classes'
|
20
|
+
CLEAN.include 'pkg/classes'
|
21
|
+
|
22
|
+
desc 'Compile the JRuby extension'
|
23
|
+
task :compile => FileList['pkg/classes', 'jruby-ext/src/**/*.java'] do |t|
|
24
|
+
ant.javac :srcdir => 'jruby-ext/src', :destdir => t.prerequisites.first,
|
25
|
+
:source => '1.6', :target => '1.6', :debug => true, :includeantruntime => false,
|
26
|
+
:classpath => '${java.class.path}:${sun.boot.class.path}'
|
27
|
+
end
|
28
|
+
|
29
|
+
desc 'Build the jar'
|
30
|
+
file jar_file => :compile do |t|
|
31
|
+
ant.jar :basedir => 'pkg/classes', :destfile => t.name, :includes => '**/*.class'
|
32
|
+
end
|
33
|
+
|
34
|
+
task :jar => jar_file
|
35
|
+
end
|
36
|
+
|
37
|
+
Gem::Tasks.new do |t|
|
38
|
+
t.scm.tag.format = '%s'
|
39
|
+
end
|
40
|
+
|
41
|
+
task :specs do
|
42
|
+
sh %{#{Gem.ruby} -w -Ilib -S rspec spec}
|
43
|
+
end
|
44
|
+
|
45
|
+
namespace 'java' do
|
46
|
+
java_gem_spec = Gem::Specification.new do |s|
|
47
|
+
s.name = gem_spec.name
|
48
|
+
s.version = gem_spec.version
|
49
|
+
s.author = gem_spec.author
|
50
|
+
s.email = gem_spec.email
|
51
|
+
s.homepage = gem_spec.homepage
|
52
|
+
s.summary = gem_spec.summary
|
53
|
+
s.description = gem_spec.description
|
54
|
+
s.files = %w(LICENSE LICENSE-EscapeUtils.txt LICENSE-houdini.txt README.md Rakefile)
|
55
|
+
s.files << Dir['{lib,spec,benchmark}/**/*.rb']
|
56
|
+
s.files << 'lib/walters/walters_ext.jar'
|
57
|
+
s.has_rdoc = false
|
58
|
+
s.license = gem_spec.license
|
59
|
+
s.platform = 'java'
|
60
|
+
end
|
61
|
+
|
62
|
+
Gem::PackageTask.new(java_gem_spec) do |pkg|
|
63
|
+
pkg.need_zip = true
|
64
|
+
pkg.need_tar = true
|
65
|
+
pkg.package_dir = 'pkg'
|
66
|
+
end
|
67
|
+
|
68
|
+
task :gem => 'lib/walters/walters_ext.jar'
|
69
|
+
end
|
70
|
+
|
71
|
+
task 'gem:java' => 'java:gem'
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# This file contains code based on the EscapeUtils project. See the file LICENSE-EscapeUtils.txt for details.
|
3
|
+
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/..')
|
4
|
+
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
|
5
|
+
|
6
|
+
require 'rubygems'
|
7
|
+
require 'benchmark'
|
8
|
+
|
9
|
+
require 'rack'
|
10
|
+
require 'erb'
|
11
|
+
require 'cgi'
|
12
|
+
require 'haml'
|
13
|
+
require 'walters'
|
14
|
+
unless defined?(JRUBY_VERSION)
|
15
|
+
require 'fast_xs'
|
16
|
+
require 'escape_utils'
|
17
|
+
end
|
18
|
+
|
19
|
+
module HamlBench
|
20
|
+
extend Haml::Helpers
|
21
|
+
end
|
22
|
+
|
23
|
+
times = 1000_000
|
24
|
+
url = "http://en.wikipedia.org/wiki/Line_of_succession_to_the_British_throne"
|
25
|
+
html = ((('a'..'z').to_a.join('') + ('A'..'Z').to_a.join('')) * 100)[0...1000]
|
26
|
+
html = html.force_encoding('utf-8') if html.respond_to?(:force_encoding)
|
27
|
+
|
28
|
+
puts "Escaping #{html.bytesize} bytes of html #{times} times, from #{url}"
|
29
|
+
|
30
|
+
Benchmark.bmbm do |x|
|
31
|
+
x.report "Rack::Utils.escape_html" do
|
32
|
+
times.times do
|
33
|
+
Rack::Utils.escape_html(html)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
x.report "Haml::Helpers.html_escape" do
|
38
|
+
times.times do
|
39
|
+
HamlBench.html_escape(html)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
x.report "ERB::Util.html_escape" do
|
44
|
+
times.times do
|
45
|
+
ERB::Util.html_escape(html)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
x.report "CGI.escapeHTML" do
|
50
|
+
times.times do
|
51
|
+
CGI.escapeHTML(html)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
x.report "String#gsub" do
|
56
|
+
html_escape = { '&' => '&', '>' => '>', '<' => '<', '"' => '"', "'" => ''' }
|
57
|
+
times.times do
|
58
|
+
html.gsub(/[&"'><]/, html_escape)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
x.report "Walters.escape_html" do
|
63
|
+
times.times do
|
64
|
+
Walters.escape_html(html)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
unless defined?(JRUBY_VERSION)
|
69
|
+
#x.report "fast_xs_extra#fast_xs_html" do
|
70
|
+
# times.times do
|
71
|
+
# html.fast_xs_html
|
72
|
+
# end
|
73
|
+
#end
|
74
|
+
#
|
75
|
+
x.report "EscapeUtils.escape_html" do
|
76
|
+
times.times do
|
77
|
+
EscapeUtils.escape_html(html)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# This file contains code based on the EscapeUtils project. See the file LICENSE-EscapeUtils.txt for details.
|
3
|
+
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/..')
|
4
|
+
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
|
5
|
+
|
6
|
+
require 'rubygems'
|
7
|
+
require 'benchmark'
|
8
|
+
|
9
|
+
require 'rack'
|
10
|
+
require 'erb'
|
11
|
+
require 'cgi'
|
12
|
+
require 'haml'
|
13
|
+
require 'walters'
|
14
|
+
unless defined?(JRUBY_VERSION)
|
15
|
+
require 'fast_xs'
|
16
|
+
require 'escape_utils'
|
17
|
+
end
|
18
|
+
|
19
|
+
module HamlBench
|
20
|
+
extend Haml::Helpers
|
21
|
+
end
|
22
|
+
|
23
|
+
times = 1000_000
|
24
|
+
url = "http://en.wikipedia.org/wiki/Line_of_succession_to_the_British_throne"
|
25
|
+
html = `curl -s #{url}`[0...1000]
|
26
|
+
html = html.force_encoding('utf-8') if html.respond_to?(:force_encoding)
|
27
|
+
|
28
|
+
# Warm up java code
|
29
|
+
10_000_000.times { Walters.escape_html('fubar foo<bar>\'/&""') }
|
30
|
+
|
31
|
+
puts "Escaping #{html.bytesize} bytes of html #{times} times, from #{url}"
|
32
|
+
|
33
|
+
Benchmark.bmbm do |x|
|
34
|
+
x.report "Rack::Utils.escape_html" do
|
35
|
+
times.times do
|
36
|
+
Rack::Utils.escape_html(html)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
x.report "Haml::Helpers.html_escape" do
|
41
|
+
times.times do
|
42
|
+
HamlBench.html_escape(html)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
x.report "ERB::Util.html_escape" do
|
47
|
+
times.times do
|
48
|
+
ERB::Util.html_escape(html)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
x.report "CGI.escapeHTML" do
|
53
|
+
times.times do
|
54
|
+
CGI.escapeHTML(html)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
x.report "String#gsub" do
|
59
|
+
html_escape = { '&' => '&', '>' => '>', '<' => '<', '"' => '"', "'" => ''' }
|
60
|
+
times.times do
|
61
|
+
html.gsub(/[&"'><]/, html_escape)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
x.report "Walters.escape_html" do
|
66
|
+
times.times do
|
67
|
+
Walters.escape_html(html)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
unless defined?(JRUBY_VERSION)
|
72
|
+
x.report "EscapeUtils.escape_html" do
|
73
|
+
times.times do
|
74
|
+
EscapeUtils.escape_html(html)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/lib/walters.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# This file contains code based on the EscapeUtils project. See the file LICENSE-EscapeUtils.txt for details.
|
2
|
+
require_relative 'html_safety'
|
3
|
+
|
4
|
+
module CGI
|
5
|
+
extend ::Walters::HtmlSafety
|
6
|
+
extend ::Walters::HtmlUnescape
|
7
|
+
|
8
|
+
class << self
|
9
|
+
alias escapeHTML walters_escape_html
|
10
|
+
alias unescapeHTML walters_unescape_html
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# This file contains code based on the EscapeUtils project. See the file LICENSE-EscapeUtils.txt for details.
|
2
|
+
require_relative 'html_safety'
|
3
|
+
|
4
|
+
class ERB
|
5
|
+
module Util
|
6
|
+
include ::Walters::HtmlSafety
|
7
|
+
|
8
|
+
alias html_escape walters_escape_html
|
9
|
+
alias h html_escape
|
10
|
+
module_function :h
|
11
|
+
module_function :html_escape
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# This file contains code based on the EscapeUtils project. See the file LICENSE-EscapeUtils.txt for details.
|
2
|
+
require_relative 'html_safety'
|
3
|
+
|
4
|
+
module Haml
|
5
|
+
module Helpers
|
6
|
+
include ::Walters::HtmlSafety
|
7
|
+
|
8
|
+
alias html_escape walters_escape_html
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# This file contains code based on the EscapeUtils project. See the file LICENSE-EscapeUtils.txt for details.
|
2
|
+
require 'walters'
|
3
|
+
|
4
|
+
module Walters
|
5
|
+
module HtmlSafety
|
6
|
+
if ''.respond_to? :html_safe?
|
7
|
+
def walters_escape_html(s)
|
8
|
+
if s.html_safe?
|
9
|
+
s.to_s.html_safe
|
10
|
+
else
|
11
|
+
Walters.escape_html(s).html_safe
|
12
|
+
end
|
13
|
+
end
|
14
|
+
else
|
15
|
+
include ::Walters::HtmlEscape
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# This file contains code based on the EscapeUtils project. See the file LICENSE-EscapeUtils.txt for details.
|
2
|
+
require_relative 'html_safety'
|
3
|
+
|
4
|
+
module Rack
|
5
|
+
module Utils
|
6
|
+
include ::Walters::HtmlSafety
|
7
|
+
|
8
|
+
alias escape_html walters_escape_html
|
9
|
+
module_function :escape_html
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# This file contains code based on the EscapeUtils project. See the file LICENSE-EscapeUtils.txt for details.
|
2
|
+
require 'walters'
|
3
|
+
|
4
|
+
module ActionView
|
5
|
+
module Helpers
|
6
|
+
module JavaScriptHelper
|
7
|
+
include ::Walters::JavaScriptEscape
|
8
|
+
alias escape_javascript walters_escape_javascript
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2013 Wayne Meissner
|
3
|
+
#
|
4
|
+
# This file is part of the Walters project (http://github.com/wmeissner/walters).
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
|
18
|
+
|
19
|
+
if defined?(JRUBY_VERSION)
|
20
|
+
require 'walters/walters_ext'
|
21
|
+
else
|
22
|
+
require_relative 'walters_xni'
|
23
|
+
end
|
Binary file
|
@@ -0,0 +1,109 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2013 Wayne Meissner
|
3
|
+
#
|
4
|
+
# This file is part of the Walters project (http://github.com/wmeissner/walters).
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
|
18
|
+
require 'xni'
|
19
|
+
|
20
|
+
module Walters
|
21
|
+
extend XNI::Extension
|
22
|
+
extension 'walters'
|
23
|
+
|
24
|
+
class Buffer < XNI::DataObject
|
25
|
+
custom_finalizer
|
26
|
+
|
27
|
+
native :initialize, [ :uint ], :void
|
28
|
+
native :cstring, [], :cstring
|
29
|
+
|
30
|
+
native :escape_html, [ :cstring, :uint], :bool
|
31
|
+
native :escape_html0, [ :cstring, :uint, :bool ], :bool
|
32
|
+
native :unescape_html, [ :cstring, :uint], :bool
|
33
|
+
native :escape_xml, [ :cstring, :uint], :bool
|
34
|
+
native :escape_uri, [ :cstring, :uint], :bool
|
35
|
+
native :escape_url, [ :cstring, :uint], :bool
|
36
|
+
native :escape_href, [ :cstring, :uint], :bool
|
37
|
+
native :unescape_uri, [ :cstring, :uint], :bool
|
38
|
+
native :unescape_url, [ :cstring, :uint], :bool
|
39
|
+
native :escape_js, [ :cstring, :uint], :bool
|
40
|
+
native :unescape_js, [ :cstring, :uint], :bool
|
41
|
+
end
|
42
|
+
|
43
|
+
native :free_cstring, [ :pointer ], :void
|
44
|
+
native :read_cstring, [ :pointer ], :cstring
|
45
|
+
native :_escape_html, [ :cstring, :uint, :bool], :pointer
|
46
|
+
native :_unescape_html, [ :cstring, :uint], :pointer
|
47
|
+
native :_escape_xml, [ :cstring, :uint], :pointer
|
48
|
+
native :_escape_uri, [ :cstring, :uint], :pointer
|
49
|
+
native :_escape_url, [ :cstring, :uint], :pointer
|
50
|
+
native :_escape_href, [ :cstring, :uint], :pointer
|
51
|
+
native :_unescape_uri, [ :cstring, :uint], :pointer
|
52
|
+
native :_unescape_url, [ :cstring, :uint], :pointer
|
53
|
+
native :_escape_js, [ :cstring, :uint], :pointer
|
54
|
+
native :_unescape_js, [ :cstring, :uint], :pointer
|
55
|
+
|
56
|
+
class << self
|
57
|
+
private
|
58
|
+
def cstring(ptr, str)
|
59
|
+
if ptr
|
60
|
+
begin
|
61
|
+
str = read_cstring(ptr)
|
62
|
+
ensure
|
63
|
+
free_cstring(ptr)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
str
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.escape_html(src, secure = true)
|
71
|
+
cstring _escape_html(src, src.length, secure), src
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.unescape_html(src)
|
75
|
+
cstring _unescape_html(src, src.length), src
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.escape_xml(src)
|
79
|
+
cstring _escape_xml(src, src.length), src
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.escape_uri(src)
|
83
|
+
cstring _escape_uri(src, src.length), src
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.escape_url(src)
|
87
|
+
cstring _escape_url(src, src.length), src
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.escape_href(src)
|
91
|
+
cstring _escape_href(src, src.length), src
|
92
|
+
end
|
93
|
+
|
94
|
+
def self.unescape_uri(src)
|
95
|
+
cstring _unescape_uri(src, src.length), src
|
96
|
+
end
|
97
|
+
|
98
|
+
def self.unescape_url(src)
|
99
|
+
cstring _unescape_url(src, src.length), src
|
100
|
+
end
|
101
|
+
|
102
|
+
def self.escape_js(src)
|
103
|
+
cstring _escape_js(src, src.length), src
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.unescape_js(src)
|
107
|
+
cstring _unescape_js(src, src.length), src
|
108
|
+
end
|
109
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright (C) 2013 Wayne Meissner
|
4
|
+
#
|
5
|
+
# This file is part of the Walters project (http://github.com/wmeissner/walters).
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
#
|
19
|
+
# This file contains code based on the EscapeUtils project. See the file LICENSE-EscapeUtils.txt for details.
|
20
|
+
#
|
21
|
+
|
22
|
+
require File.expand_path('../../spec_helper.rb', __FILE__)
|
23
|
+
|
24
|
+
describe 'html escape' do
|
25
|
+
it 'double quotes' do
|
26
|
+
Walters.escape_html("<some_tag some_attr=\"some value\"/>").should == '<some_tag some_attr="some value"/>'
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'single quotes' do
|
30
|
+
Walters.escape_html("<some_tag some_attr='some value'/>").should == '<some_tag some_attr='some value'/>'
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'ampersand' do
|
34
|
+
Walters.escape_html('<b>Bourbon & Branch</b>').should == '<b>Bourbon & Branch</b>'
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'original returned if no escaping required' do
|
38
|
+
str = 'foobar'
|
39
|
+
Walters.escape_html(str).should equal str
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'all tags escaped' do
|
43
|
+
Walters.escape_html('&<>"\'/').should == '&<>"'/'
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'plain text followed by tag' do
|
47
|
+
Walters.escape_html('foobar<1>').should == 'foobar<1>'
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'non-utf8 input' do
|
51
|
+
lambda { Walters.escape_html('<b>Bourbon & Branch</b>'.encode('ISO-8859-1')) }.should raise_error Encoding::CompatibilityError
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'returned value has same encoding as original' do
|
55
|
+
str = '<b>Bourbon & Branch</b>'.encode('utf-8')
|
56
|
+
Walters.escape_html(str).encoding.should == str.encoding
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright (C) 2013 Wayne Meissner
|
4
|
+
#
|
5
|
+
# This file is part of the Walters project (http://github.com/wmeissner/walters).
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
#
|
19
|
+
# This file contains code based on the EscapeUtils project. See the file LICENSE-EscapeUtils.txt for details.
|
20
|
+
#
|
21
|
+
|
22
|
+
describe 'javascript' do
|
23
|
+
it 'quotes and newlines' do
|
24
|
+
Walters.escape_javascript(%(This "thing" is really\n netos\r\n\n')).should == %(This \\"thing\\" is really\\n netos\\n\\n\\')
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'backslash' do
|
28
|
+
Walters.escape_javascript(%(backslash\\test)).should == %(backslash\\\\test)
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'closed html tags' do
|
33
|
+
Walters.escape_javascript(%(keep <open>, but dont </close> tags)).should == %(keep <open>, but dont <\\/close> tags)
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'original returned if no escaping required' do
|
37
|
+
str = 'foobar'
|
38
|
+
Walters.escape_javascript(str).should equal str
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'input must be utf8' do
|
42
|
+
lambda { Walters.escape_javascript('dont </close> tags'.encode('ISO-8859-1')) }.should raise_error(Encoding::CompatibilityError)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'return value has same encoding as input' do
|
46
|
+
str = "dont </close> tags"
|
47
|
+
Walters.escape_javascript(str).encoding.should == Encoding.find('UTF-8')
|
48
|
+
end
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: walters
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: java
|
7
|
+
authors:
|
8
|
+
- Wayne Meissner
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-02 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: JRuby wrapper for Houdini html escaping library
|
15
|
+
email: wmeissner@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- LICENSE
|
21
|
+
- LICENSE-EscapeUtils.txt
|
22
|
+
- LICENSE-houdini.txt
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- lib/walters.rb
|
26
|
+
- lib/walters/version.rb
|
27
|
+
- lib/walters/walters.rb
|
28
|
+
- lib/walters/walters_xni.rb
|
29
|
+
- lib/walters/html/cgi.rb
|
30
|
+
- lib/walters/html/erb.rb
|
31
|
+
- lib/walters/html/haml.rb
|
32
|
+
- lib/walters/html/html_safety.rb
|
33
|
+
- lib/walters/html/rack.rb
|
34
|
+
- lib/walters/javascript/action_view.rb
|
35
|
+
- spec/spec_helper.rb
|
36
|
+
- spec/walters/html_escape_spec.rb
|
37
|
+
- spec/walters/javascript_escape_spec.rb
|
38
|
+
- benchmark/html_clean_escape.rb
|
39
|
+
- benchmark/html_escape.rb
|
40
|
+
- lib/walters/walters_ext.jar
|
41
|
+
homepage: http://wiki.github.com/wmeissner/walters
|
42
|
+
licenses:
|
43
|
+
- Apache 2.0
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
none: false
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
none: false
|
60
|
+
requirements: []
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.8.24
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: JRuby wrapper for Houdini
|
66
|
+
test_files: []
|