class Loofah::Scrubbers::TargetBlank

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”.

Public Class Methods

new() click to toggle source
# File lib/loofah/scrubbers.rb, line 247
def initialize # rubocop:disable Lint/MissingSuper
  @direction = :top_down
end

Public Instance Methods

scrub(node) click to toggle source
# 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