Class: Seo::HtmlContentSanitizer

Inherits:
BaseService show all
Defined in:
app/services/seo/html_content_sanitizer.rb

Overview

Sanitizes HTML content by removing empty elements and cleaning up inline styles.
Used primarily for blog posts and article content rendered from WYSIWYG editors.

Cleans up common issues:

  • Empty and list elements that create unwanted whitespace
  • Inline styles that conflict with CSS layout (clear, margins, padding, float)
  • Unnecessary tags inside table cells
  • Adds Bootstrap table classes (table w-auto) so tables fit beside floated sidebar

Defined Under Namespace

Classes: Result

Constant Summary collapse

PROBLEMATIC_STYLE_PROPERTIES =

Inline style properties that should be removed (they conflict with CSS layout)

%w[clear margin padding float].freeze

Instance Method Summary collapse

Methods inherited from BaseService

#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#process(html_fragment) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/services/seo/html_content_sanitizer.rb', line 19

def process(html_fragment)
  return Result.new(html_out: html_fragment) if html_fragment.blank?

  html_doc = Nokogiri::HTML(html_fragment)

  clean_inline_styles(html_doc)
  clean_table_cells(html_doc)
  add_bootstrap_table_classes(html_doc)
  remove_empty_paragraphs(html_doc)
  remove_empty_lists(html_doc)

  html_out = html_doc.at('body')&.inner_html.presence&.html_safe
  Result.new(html_out: html_out)
end