Class: Html2rss::Selectors::PostProcessors::SanitizeHtml

Inherits:
Base
  • Object
show all
Defined in:
lib/html2rss/selectors/post_processors/sanitize_html.rb

Overview

Constant Summary collapse

TAG_ATTRIBUTES =
{
  'a' => {
    'rel' => 'nofollow noopener noreferrer',
    'target' => '_blank'
  },

  'area' => {
    'rel' => 'nofollow noopener noreferrer',
    'target' => '_blank'
  },

  'img' => {
    'referrerpolicy' => 'no-referrer',
    'crossorigin' => 'anonymous',
    'loading' => 'lazy',
    'decoding' => 'async'
  },

  'iframe' => {
    'referrerpolicy' => 'no-referrer',
    'crossorigin' => 'anonymous',
    'loading' => 'lazy',
    'sandbox' => 'allow-same-origin',
    'src' => true,
    'width' => true,
    'height' => true
  },

  'video' => {
    'referrerpolicy' => 'no-referrer',
    'crossorigin' => 'anonymous',
    'preload' => 'none',
    'playsinline' => 'true',
    'controls' => 'true'
  },

  'audio' => {
    'referrerpolicy' => 'no-referrer',
    'crossorigin' => 'anonymous',
    'preload' => 'none'
  }
}.freeze

Instance Attribute Summary

Attributes inherited from Base

#context, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

assert_type, expect_options, #initialize

Constructor Details

This class inherits a constructor from Html2rss::Selectors::PostProcessors::Base

Class Method Details

.get(html, url) ⇒ String?

Shorthand method to get the sanitized HTML.

Parameters:

Returns:

  • (String, nil)


95
96
97
98
99
# File 'lib/html2rss/selectors/post_processors/sanitize_html.rb', line 95

def self.get(html, url)
  return nil if String(html).empty?

  new(html, config: { channel: { url: } }).get
end

.validate_args!(value, context) ⇒ Object



86
87
88
# File 'lib/html2rss/selectors/post_processors/sanitize_html.rb', line 86

def self.validate_args!(value, context)
  assert_type value, String, :value, context:
end

Instance Method Details

#getString?

Returns:

  • (String, nil)


103
104
105
106
107
108
# File 'lib/html2rss/selectors/post_processors/sanitize_html.rb', line 103

def get
  sanitized_html = Sanitize.fragment(value, sanitize_config).to_s
  sanitized_html.gsub!(/\s+/, ' ')
  sanitized_html.strip!
  sanitized_html.empty? ? nil : sanitized_html
end