Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions frontend/app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,21 @@ def setup_context(options)
end

def render_token(opts)
link_opts = {:class => "btn btn-mini"}
link_opts = {}
link_opts.merge!({:target => "_blank"}) if opts[:inside_token_editor] || opts[:inside_linker_browse]
popover_url = url_for :controller => :resolver, :action => :resolve_readonly
popover_url += "?uri=#{opts[:uri]}"
popover_content = link_to I18n.t("actions.view"), popover_url, link_opts
url = url_for :controller => :resolver, :action => :resolve_readonly
url += "?uri=#{opts[:uri]}"

html = "<div class='"
html += "token " if not opts[:inside_token_editor]
aria_label = CGI.escape_html(strip_tags(clean_mixed_content(opts[:label].to_s)))
html += "#{opts[:type]} has-popover' data-toggle='popover' data-trigger='#{opts[:trigger] || "custom"}' data-html='true' data-placement='#{opts[:placement] || "auto"}' data-content=\"#{CGI.escape_html(popover_content)}\" aria-label=\"#{aria_label}\" tabindex='0'>"
link_opts[:class] = "token " unless opts[:inside_token_editor]
link_opts[:class] = "#{link_opts[:class]}#{opts[:type]}"
Comment on lines +94 to +95

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the fence on whether this is better or not. It's slightly less confusing, but it does feel a bit uglier.

Suggested change
link_opts[:class] = "token " unless opts[:inside_token_editor]
link_opts[:class] = "#{link_opts[:class]}#{opts[:type]}"
link_opts[:class] = "#{'token ' unless opts[:inside_token_editor]}#{opts[:type]}"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually find the current way a little easier to read


if opts[:icon_class]
html += "<span class='icon-token #{opts[:icon_class]}'></span>"
html = "<span class='icon-token #{opts[:icon_class]}'></span>"
else
html += "<span class='icon-token'></span>"
html = "<span class='icon-token'></span>"
end
html += clean_mixed_content(opts[:label])
html += "</div>"
html.html_safe
link_to html.html_safe, url, link_opts
end

def link_to_help(opts = {})
Expand Down
27 changes: 16 additions & 11 deletions frontend/spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,31 @@

describe ApplicationHelper do
describe '#render_token' do
it 'adds an aria-label derived from the token label' do
html = helper.render_token(
let(:token_options) do
{
uri: '/repositories/1/resources/2',
type: 'resource',
label: +'Test Label'
)
}
end

expect(html).to include('aria-label="Test Label"')
expect(html).to include("tabindex='0'")
it 'renders the token as a direct link without popover behavior' do
html = helper.render_token(token_options)

expect(html).to include('href="/resolve/readonly?uri=/repositories/1/resources/2"')
expect(html).to include('class="token resource"')
expect(html).not_to include('data-toggle="popover"')
end

it 'strips HTML before setting the aria-label' do
it 'renders the token icon and opens the link in a new tab in linker contexts' do
html = helper.render_token(
uri: '/repositories/1/resources/2',
type: 'resource',
label: +'<em>Test</em> Label'
**token_options,
icon_class: 'icon-book',
inside_linker_browse: true
)

expect(html).to include('aria-label="Test Label"')
expect(html).not_to include('aria-label="<em>Test</em> Label"')
expect(html).to include("<span class='icon-token icon-book'></span>")
expect(html).to include('target="_blank"')
end
end
end