trix icon indicating copy to clipboard operation
trix copied to clipboard

Open in New Window option for links does not exist

Open Quirksmode opened this issue 11 years ago • 39 comments

Quirksmode avatar Oct 22 '15 10:10 Quirksmode

+1

jmz-b avatar Oct 22 '15 10:10 jmz-b

In the context menu? Shows up for me in Safari 8.0.8.

Edit: I'm not a smart person.

codeclown avatar Oct 23 '15 10:10 codeclown

@codeclown I’m guessing @Quirksmode means an option in the editor to add target="_blank" to a link.

adamschwartz avatar Oct 23 '15 23:10 adamschwartz

That's correct @adamschwartz, whilst I don't agree it's best practice, I am yet to meet a client who doesn't want the ability to open their links in a new window. A simple checkbox below the input where you add the url would suffice :-)

Quirksmode avatar Oct 28 '15 11:10 Quirksmode

@Quirksmode Absolutely I think it’s a very reasonable suggestion. :+1:

adamschwartz avatar Oct 28 '15 16:10 adamschwartz

Yes please!

hrishimittal avatar Nov 10 '15 11:11 hrishimittal

Thanks for the feature request, but I don’t think we’ll be adding this to Trix. If you need to add a target attribute to links, I’d suggest post-processing the HTML when you render it in your application.

sstephenson avatar Dec 18 '15 18:12 sstephenson

This would be a great, and trivial feature to add, while adding post-processing to any application that would like this feature is definitely more complex.

Would you reconsider?

ryan-kimber avatar Jul 03 '16 15:07 ryan-kimber

You could also handle this client side instead of post-processing your HTML:

// Open all external links in a new window
addEventListener("click", function(event) {
  var el = event.target

  if (el.tagName === "A" && !el.isContentEditable && el.host !== window.location.host) {
    el.setAttribute("target", "_blank")
  }
}, true)

javan avatar Aug 10 '16 18:08 javan

IMO, Workaround suggest to post-process is poor. I might want to create links to external content and internal content.

fdattein avatar Nov 02 '16 23:11 fdattein

+1 yeah - i'll need this one too

bostondevin avatar Nov 13 '16 21:11 bostondevin

If anyone is looking for a quick way to fix this in rails. Here's what you can do: (needs nokogiri)

module TrixHelper
  # found at stack overflow: https://stackoverflow.com/a/41557234/244089
  def change_a_target(body)
    doc = Nokogiri::HTML(body)
    doc.css('a').each do |link|
      link['target'] = '_blank'
      # To avoid window.opener attack when target blank is used
      # https://mathiasbynens.github.io/rel-noopener/
      link['rel'] = 'noopener'
    end
    doc.to_s
  end

  def sanitize_trix_html(html)
    sanitize(change_a_target(html), tags: %w(strong a), attributes: %w(href target rel))
  end
end

Then in your view you can: <%= sanitize_trix_html(@post.body_html) %>

ksarna avatar Oct 11 '17 12:10 ksarna

as the usage is growing, I am facing limitations with trix and this is one of them. The other one is inability to set constraints like required for the input boxes.

goravbhootra avatar Dec 28 '19 09:12 goravbhootra

+1 on this one... it's a very common use-case, and doing this outside the editor (post-processing) seems like a hack. It seems a a strange decision to reject it outright like this.

elsurudo avatar Apr 16 '20 15:04 elsurudo

Adding my +1 to make this feature part of Trix.

crimson-knight avatar May 15 '20 14:05 crimson-knight

+1

Is there a philosophical reason this is not to be developed? As in, if a PR were to come in for this, would it be rejected?

lewaabahmad avatar Jun 28 '20 23:06 lewaabahmad

+1

Post process only handle trix html after, we also need it to target blank when editing.

RicottaZhang avatar Jul 07 '20 10:07 RicottaZhang

FYI, at last I add it works for editor and show trix content.

$(document).ready(function() {
    $(".trix-content a").click(function(e) {
        $(this).attr("target","_blank");
    });

  // For those content inject by javascript, use an event handler for your trix container may be better.
  $(YourTrixContainerSelector).on('click', '.trix-content a', function(e) {
        $(this).attr("target","_blank");
   });
});

P.S. I prefer stick the solution to Javascript. So it can turn on case by case for each view.

RicottaZhang avatar Jul 08 '20 08:07 RicottaZhang

@RicottaZhang this workaround works well. Thank you. All Trix links are opened in a new tab.

yshmarov avatar Nov 24 '20 13:11 yshmarov

@RicottaZhang's solution didn't work for me b/c in some places, our content gets put on the page by JavaScript, so it isn't there when the page loads.

This is the best I've come up with:

# file: app/helpers/trix_helper.rb
module TrixHelper
  SCRUBBER = Loofah::Scrubber.new do |node|
    node[:target] = '_blank' if node.name == 'a'
  end

  # Overriding https://github.com/rails/rails/blob/da795a678b341adc8031c4d9cf0b5ef2176e2a5a/actiontext/app/helpers/action_text/content_helper.rb#L17
  def sanitize_action_text_content(content)
    original_scrubber, self.scrubber = self.scrubber, SCRUBBER
    super
  ensure
    self.scrubber = original_scrubber
  end
end

JoshCheek avatar Apr 06 '21 21:04 JoshCheek