From c8e08d2717b98a2268da89f8196fdb1eb93725bf Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Thu, 20 Feb 2020 16:12:42 +1300 Subject: [PATCH] `Hash#slice!` was not implemented as I expected. --- .travis.yml | 1 + lib/trenni/sanitize/filter.rb | 4 ++++ lib/trenni/sanitize/fragment.rb | 6 ++++-- trenni-sanitize.gemspec | 2 +- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index cf0513e..ec78a16 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ cache: bundler matrix: include: + - rvm: 2.4 - rvm: 2.5 - rvm: 2.6 - rvm: 2.6 diff --git a/lib/trenni/sanitize/filter.rb b/lib/trenni/sanitize/filter.rb index 3f93b7b..1a2c5c9 100644 --- a/lib/trenni/sanitize/filter.rb +++ b/lib/trenni/sanitize/filter.rb @@ -66,6 +66,10 @@ def accept!(mode = ALL) def [] key self.tag&.attributes[key] end + + def limit_attributes(keys) + self.tag&.attributes&.select!{|key, value| keys.include?(key)} + end end def initialize(output, entities) diff --git a/lib/trenni/sanitize/fragment.rb b/lib/trenni/sanitize/fragment.rb index cafc02a..2830a72 100644 --- a/lib/trenni/sanitize/fragment.rb +++ b/lib/trenni/sanitize/fragment.rb @@ -20,10 +20,12 @@ require_relative 'filter' +require 'set' + module Trenni module Sanitize class Fragment < Filter - STANDARD_ATTRIBUTES = ['class', 'style'].freeze + STANDARD_ATTRIBUTES = Set.new(['class', 'style']).freeze ALLOWED_TAGS = { 'div' => STANDARD_ATTRIBUTES, @@ -49,7 +51,7 @@ class Fragment < Filter def filter(node) if attributes = ALLOWED_TAGS[node.name] - node.tag.attributes.slice!(*attributes) + node.limit_attributes(attributes) node.accept! else diff --git a/trenni-sanitize.gemspec b/trenni-sanitize.gemspec index c06fe9f..3a07197 100644 --- a/trenni-sanitize.gemspec +++ b/trenni-sanitize.gemspec @@ -15,7 +15,7 @@ Gem::Specification.new do |spec| spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"] - spec.required_ruby_version = '~> 2.5' + spec.required_ruby_version = '~> 2.4' spec.add_dependency "trenni", '~> 3.5'