Class: Loofah::Scrubbers::DoubleBreakpoint

Inherits:
Loofah::Scrubber show all
Defined in:
lib/loofah/scrubbers.rb

Overview

scrub!(:double_breakpoint)

+:double_breakpoint+ replaces double-break tags with closing/opening paragraph tags.

   markup = "<p>Some text here in a logical paragraph.<br><br>Some more text, apparently a second paragraph.</p>"
   Loofah.html5_fragment(markup).scrub!(:double_breakpoint)
   => "<p>Some text here in a logical paragraph.</p><p>Some more text, apparently a second paragraph.</p>"

Constant Summary

Constants inherited from Loofah::Scrubber

Loofah::Scrubber::CONTINUE, Loofah::Scrubber::STOP

Instance Attribute Summary

Attributes inherited from Loofah::Scrubber

#block, #direction

Instance Method Summary collapse

Methods inherited from Loofah::Scrubber

#append_attribute, #traverse

Constructor Details

#initializeDoubleBreakpoint

rubocop:disable Lint/MissingSuper



363
364
365
# File 'lib/loofah/scrubbers.rb', line 363

def initialize # rubocop:disable Lint/MissingSuper
  @direction = :top_down
end

Instance Method Details

#scrub(node) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/loofah/scrubbers.rb', line 367

def scrub(node)
  return CONTINUE unless (node.type == Nokogiri::XML::Node::ELEMENT_NODE) && (node.name == "p")

  paragraph_with_break_point_nodes = node.xpath("//p[br[following-sibling::br]]")

  paragraph_with_break_point_nodes.each do |paragraph_node|
    new_paragraph = paragraph_node.add_previous_sibling("<p>").first

    paragraph_node.children.each do |child|
      remove_blank_text_nodes(child)
    end

    paragraph_node.children.each do |child|
      # already unlinked
      next if child.parent.nil?

      if child.name == "br" && child.next_sibling.name == "br"
        new_paragraph = paragraph_node.add_previous_sibling("<p>").first
        child.next_sibling.unlink
        child.unlink
      else
        child.parent = new_paragraph
      end
    end

    paragraph_node.unlink
  end

  CONTINUE
end