Module: Html2rss::Html::ArticleRules::Image

Defined in:
lib/html2rss/html/article_rules/image.rb

Overview

DOM-agnostic image URL selection (srcset sizing, CSS background urls).

Uses string scans instead of Regexp: CodeQL rb/polynomial-redos flags the prior patterns on uncontrolled HTML attributes, and length guards alone do not clear the query.

Constant Summary collapse

MAX_ATTR_CHARS =

Skip absurdly large attributes before scanning.

4_096
QUOTES =

Optional quoting around a CSS url(...) value.

['"', "'"].freeze
UNITS =

Srcset width/height descriptor suffixes.

%w[w h].freeze
WS =

HTML/CSS whitespace treated as srcset token separators.

[' ', "\t", "\n", "\r", "\f"].freeze

Class Method Summary collapse

Class Method Details

.best_from_styles(style_strings) ⇒ String?

Returns longest non-data background url.

Parameters:

  • style_strings (Array<String>)

    CSS style attribute values

Returns:

  • (String, nil)

    longest non-data background url



35
36
37
38
39
40
# File 'lib/html2rss/html/article_rules/image.rb', line 35

def best_from_styles(style_strings)
  Array(style_strings)
    .filter_map { |style| style_url(style) }
    .reject { |src| src.start_with?('data:') }
    .max_by(&:size)
end

.largest_from_srcsets(srcset_strings) ⇒ String?

Returns URL of the largest width/height candidate.

Parameters:

  • srcset_strings (Array<String>)

    raw srcset attribute values

Returns:

  • (String, nil)

    URL of the largest width/height candidate



26
27
28
29
30
# File 'lib/html2rss/html/article_rules/image.rb', line 26

def largest_from_srcsets(srcset_strings)
  by_size = {}
  Array(srcset_strings).each { |srcset| merge_srcset!(by_size, srcset) }
  by_size[by_size.keys.max]
end