Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing whitespace in rendered LinkComponent #1274

Merged
merged 2 commits into from
Aug 4, 2022
Merged
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
5 changes: 5 additions & 0 deletions .changeset/brave-planets-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/view-components": patch
---

Fixing whitespace in rendered LinkComponent
12 changes: 0 additions & 12 deletions app/components/primer/link_component.html.erb

This file was deleted.

14 changes: 14 additions & 0 deletions app/components/primer/link_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,19 @@ def initialize(href: nil, tag: DEFAULT_TAG, scheme: DEFAULT_SCHEME, muted: false
def before_render
raise ArgumentError, "href is required when using <a> tag" if @system_arguments[:tag] == :a && @system_arguments[:href].nil? && !Rails.env.production?
end

def call
if tooltip.present?
render Primer::BaseComponent.new(tag: :span, position: :relative) do
render(Primer::BaseComponent.new(**@system_arguments)) do
content
end.to_s + tooltip.to_s
end
else
render(Primer::BaseComponent.new(**@system_arguments)) do
content
end
end
end
end
end
13 changes: 11 additions & 2 deletions test/components/link_component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ def test_renders_content_and_not_muted_link
end

def test_renders_no_additional_whitespace
render_inline(Primer::LinkComponent.new(href: "http://joe-jonas-shirtless.com")) { "content" }
result = render_inline(Primer::LinkComponent.new(href: "http://joe-jonas-shirtless.com")) { "content" }

assert_text(/^content$/)
assert_match(%r{^<a[^>]+>content</a>$}, result.to_s)
end

def test_renders_without_trailing_newline
Expand Down Expand Up @@ -83,4 +83,13 @@ def test_raises_if_a_tag_and_href_nil
def test_status
assert_component_state(Primer::LinkComponent, :beta)
end

def test_renders_with_tooltip_sibling
render_inline(Primer::LinkComponent.new(id: "1", href: "http://google.com")) do |c|
c.tooltip(text: "Tooltip text")
"content"
end

assert_selector("a[href='http://google.com'] + tool-tip", text: "Tooltip text", visible: false)
end
end