Skip to content

Commit 293903d

Browse files
committed
style(rubocop): correct all warnings
1 parent 45fa826 commit 293903d

9 files changed

+176
-164
lines changed

Diff for: Gemfile

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
source 'https://rubygems.org'
1+
# frozen_string_literal: true
2+
3+
source "https://rubygems.org"
24

35
gemspec
46

Diff for: Rakefile

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
# frozen_string_literal: true
2+
13
require "bundler/gem_tasks"
24
require "rake/testtask"
35

46
task default: :test
57
Rake::TestTask.new do |t|
6-
t.pattern = 'test/**/*_test.rb'
8+
t.pattern = "test/**/*_test.rb"
79
t.warning = true
810
t.verbose = true
911
end

Diff for: lib/rails-html-sanitizer.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require "rails/html/sanitizer/version"
24
require "loofah"
35
require "rails/html/scrubbers"

Diff for: lib/rails/html/sanitizer.rb

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Rails
24
module Html
35
XPATHS_TO_REMOVE = %w{.//script .//form comment()}
@@ -8,15 +10,14 @@ def sanitize(html, options = {})
810
end
911

1012
private
13+
def remove_xpaths(node, xpaths)
14+
node.xpath(*xpaths).remove
15+
node
16+
end
1117

12-
def remove_xpaths(node, xpaths)
13-
node.xpath(*xpaths).remove
14-
node
15-
end
16-
17-
def properly_encode(fragment, options)
18-
fragment.xml? ? fragment.to_xml(options) : fragment.to_html(options)
19-
end
18+
def properly_encode(fragment, options)
19+
fragment.xml? ? fragment.to_xml(options) : fragment.to_html(options)
20+
end
2021
end
2122

2223
# === Rails::Html::FullSanitizer
@@ -35,7 +36,7 @@ def sanitize(html, options = {})
3536
remove_xpaths(loofah_fragment, XPATHS_TO_REMOVE)
3637
loofah_fragment.scrub!(TextOnlyScrubber.new)
3738

38-
properly_encode(loofah_fragment, encoding: 'UTF-8')
39+
properly_encode(loofah_fragment, encoding: "UTF-8")
3940
end
4041
end
4142

@@ -132,22 +133,21 @@ def sanitize(html, options = {})
132133
loofah_fragment.scrub!(:strip)
133134
end
134135

135-
properly_encode(loofah_fragment, encoding: 'UTF-8')
136+
properly_encode(loofah_fragment, encoding: "UTF-8")
136137
end
137138

138139
def sanitize_css(style_string)
139140
Loofah::HTML5::Scrub.scrub_css(style_string)
140141
end
141142

142143
private
144+
def allowed_tags(options)
145+
options[:tags] || self.class.allowed_tags
146+
end
143147

144-
def allowed_tags(options)
145-
options[:tags] || self.class.allowed_tags
146-
end
147-
148-
def allowed_attributes(options)
149-
options[:attributes] || self.class.allowed_attributes
150-
end
148+
def allowed_attributes(options)
149+
options[:attributes] || self.class.allowed_attributes
150+
end
151151
end
152152

153153
WhiteListSanitizer = SafeListSanitizer

Diff for: lib/rails/html/sanitizer/version.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Rails
24
module Html
35
class Sanitizer

Diff for: lib/rails/html/scrubbers.rb

+60-59
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Rails
24
module Html
35
# === Rails::Html::PermitScrubber
@@ -77,84 +79,83 @@ def scrub(node)
7779
end
7880

7981
protected
82+
def allowed_node?(node)
83+
@tags.include?(node.name)
84+
end
8085

81-
def allowed_node?(node)
82-
@tags.include?(node.name)
83-
end
86+
def skip_node?(node)
87+
node.text?
88+
end
8489

85-
def skip_node?(node)
86-
node.text?
87-
end
90+
def scrub_attribute?(name)
91+
!@attributes.include?(name)
92+
end
8893

89-
def scrub_attribute?(name)
90-
!@attributes.include?(name)
91-
end
94+
def keep_node?(node)
95+
if @tags
96+
allowed_node?(node)
97+
else
98+
Loofah::HTML5::Scrub.allowed_element?(node.name)
99+
end
100+
end
92101

93-
def keep_node?(node)
94-
if @tags
95-
allowed_node?(node)
96-
else
97-
Loofah::HTML5::Scrub.allowed_element?(node.name)
102+
def scrub_node(node)
103+
node.before(node.children) unless prune # strip
104+
node.remove
98105
end
99-
end
100106

101-
def scrub_node(node)
102-
node.before(node.children) unless prune # strip
103-
node.remove
104-
end
107+
def scrub_attributes(node)
108+
if @attributes
109+
node.attribute_nodes.each do |attr|
110+
attr.remove if scrub_attribute?(attr.name)
111+
scrub_attribute(node, attr)
112+
end
105113

106-
def scrub_attributes(node)
107-
if @attributes
108-
node.attribute_nodes.each do |attr|
109-
attr.remove if scrub_attribute?(attr.name)
110-
scrub_attribute(node, attr)
114+
scrub_css_attribute(node)
115+
else
116+
Loofah::HTML5::Scrub.scrub_attributes(node)
111117
end
112-
113-
scrub_css_attribute(node)
114-
else
115-
Loofah::HTML5::Scrub.scrub_attributes(node)
116118
end
117-
end
118119

119-
def scrub_css_attribute(node)
120-
if Loofah::HTML5::Scrub.respond_to?(:scrub_css_attribute)
121-
Loofah::HTML5::Scrub.scrub_css_attribute(node)
122-
else
123-
style = node.attributes['style']
124-
style.value = Loofah::HTML5::Scrub.scrub_css(style.value) if style
120+
def scrub_css_attribute(node)
121+
if Loofah::HTML5::Scrub.respond_to?(:scrub_css_attribute)
122+
Loofah::HTML5::Scrub.scrub_css_attribute(node)
123+
else
124+
style = node.attributes["style"]
125+
style.value = Loofah::HTML5::Scrub.scrub_css(style.value) if style
126+
end
125127
end
126-
end
127128

128-
def validate!(var, name)
129-
if var && !var.is_a?(Enumerable)
130-
raise ArgumentError, "You should pass :#{name} as an Enumerable"
129+
def validate!(var, name)
130+
if var && !var.is_a?(Enumerable)
131+
raise ArgumentError, "You should pass :#{name} as an Enumerable"
132+
end
133+
var
131134
end
132-
var
133-
end
134135

135-
def scrub_attribute(node, attr_node)
136-
attr_name = if attr_node.namespace
137-
"#{attr_node.namespace.prefix}:#{attr_node.node_name}"
138-
else
139-
attr_node.node_name
140-
end
136+
def scrub_attribute(node, attr_node)
137+
attr_name = if attr_node.namespace
138+
"#{attr_node.namespace.prefix}:#{attr_node.node_name}"
139+
else
140+
attr_node.node_name
141+
end
141142

142-
if Loofah::HTML5::SafeList::ATTR_VAL_IS_URI.include?(attr_name)
143-
return if Loofah::HTML5::Scrub.scrub_uri_attribute(attr_node)
144-
end
143+
if Loofah::HTML5::SafeList::ATTR_VAL_IS_URI.include?(attr_name)
144+
return if Loofah::HTML5::Scrub.scrub_uri_attribute(attr_node)
145+
end
145146

146-
if Loofah::HTML5::SafeList::SVG_ATTR_VAL_ALLOWS_REF.include?(attr_name)
147-
Loofah::HTML5::Scrub.scrub_attribute_that_allows_local_ref(attr_node)
148-
end
147+
if Loofah::HTML5::SafeList::SVG_ATTR_VAL_ALLOWS_REF.include?(attr_name)
148+
Loofah::HTML5::Scrub.scrub_attribute_that_allows_local_ref(attr_node)
149+
end
149150

150-
if Loofah::HTML5::SafeList::SVG_ALLOW_LOCAL_HREF.include?(node.name) && attr_name == 'xlink:href' && attr_node.value =~ /^\s*[^#\s].*/m
151-
attr_node.remove
152-
end
151+
if Loofah::HTML5::SafeList::SVG_ALLOW_LOCAL_HREF.include?(node.name) && attr_name == "xlink:href" && attr_node.value =~ /^\s*[^#\s].*/m
152+
attr_node.remove
153+
end
153154

154-
node.remove_attribute(attr_node.name) if attr_name == 'src' && attr_node.value !~ /[^[:space:]]/
155+
node.remove_attribute(attr_node.name) if attr_name == "src" && attr_node.value !~ /[^[:space:]]/
155156

156-
Loofah::HTML5::Scrub.force_correct_attribute_escaping! node
157-
end
157+
Loofah::HTML5::Scrub.force_correct_attribute_escaping! node
158+
end
158159
end
159160

160161
# === Rails::Html::TargetScrubber

Diff for: rails-html-sanitizer.gemspec

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
# coding: utf-8
2-
lib = File.expand_path('../lib', __FILE__)
2+
# frozen_string_literal: true
3+
4+
lib = File.expand_path("../lib", __FILE__)
35
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4-
require 'rails/html/sanitizer/version'
6+
require "rails/html/sanitizer/version"
57

68
Gem::Specification.new do |spec|
79
spec.name = "rails-html-sanitizer"
810
spec.version = Rails::Html::Sanitizer::VERSION
911
spec.authors = ["Rafael Mendonça França", "Kasper Timm Hansen"]
1012
11-
spec.description = %q{HTML sanitization for Rails applications}
12-
spec.summary = %q{This gem is responsible to sanitize HTML fragments in Rails applications.}
13+
spec.description = "HTML sanitization for Rails applications"
14+
spec.summary = "This gem is responsible to sanitize HTML fragments in Rails applications."
1315
spec.homepage = "https://github.com/rails/rails-html-sanitizer"
1416
spec.license = "MIT"
1517

1618
spec.required_ruby_version = ">= 2.5.0"
1719

18-
spec.metadata = {
20+
spec.metadata = {
1921
"bug_tracker_uri" => "https://github.com/rails/rails-html-sanitizer/issues",
2022
"changelog_uri" => "https://github.com/rails/rails-html-sanitizer/blob/v#{spec.version}/CHANGELOG.md",
2123
"documentation_uri" => "https://www.rubydoc.info/gems/rails-html-sanitizer/#{spec.version}",

0 commit comments

Comments
 (0)