Class: Loofah::Scrubbers::TargetBlank

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

Overview

scrub!(:targetblank)

+:targetblank+ adds a target="_blank" attribute to all links.
If there is a target already set, replaces it with target="_blank".

   link_farmers_markup = "ohai! <a href='http://www.myswarmysite.com/'>I like your blog post</a>"
   Loofah.html5_fragment(link_farmers_markup).scrub!(:targetblank)
   => "ohai! <a href='http://www.myswarmysite.com/' target="_blank">I like your blog post</a>"

On modern browsers, setting target="_blank" on anchor elements implicitly provides the same
behavior as setting rel="noopener".

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

#initializeTargetBlank

rubocop:disable Lint/MissingSuper



247
248
249
# File 'lib/loofah/scrubbers.rb', line 247

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

Instance Method Details

#scrub(node) ⇒ Object



251
252
253
254
255
256
257
# File 'lib/loofah/scrubbers.rb', line 251

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

  node.set_attribute("target", "_blank")

  STOP
end