Skip to content

Commit

Permalink
Introduce a trim: Boolean argument for the BaseComponent that trims…
Browse files Browse the repository at this point in the history
… the content with `strip` before rendering it
  • Loading branch information
HDinger committed Aug 28, 2024
1 parent 3238b19 commit 04c9c94
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 19 deletions.
8 changes: 7 additions & 1 deletion app/components/primer/base_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,16 @@ class BaseComponent < Primer::Component
# | :- | :- | :- |
# | classes | String | CSS class name value to be concatenated with generated Primer CSS classes. |
# | test_selector | String | Adds `data-test-selector='given value'` in non-Production environments for testing purposes. |
# | trim | Boolean | Calls `strip` on the content to remove trailing and leading white spaces. |
def initialize(tag:, classes: nil, **system_arguments)
@tag = tag

@system_arguments = validate_arguments(tag: tag, **system_arguments)

@result = Primer::Classify.call(**@system_arguments.merge(classes: classes))

@trim = !!@system_arguments[:trim]

@system_arguments[:"data-view-component"] = true
# Filter out Primer keys so they don't get assigned as HTML attributes
@content_tag_args = add_test_selector(@system_arguments).except(*Primer::Classify::Utilities::UTILITIES.keys)
Expand All @@ -167,7 +170,10 @@ def call
if SELF_CLOSING_TAGS.include?(@tag)
tag(@tag, @content_tag_args.merge(@result))
else
content_tag(@tag, content, @content_tag_args.merge(@result))
# strip unsets `html_safe`, so we have to set it back again to guarantee that HTML blocks won't break
parsed_content = (@trim && !content.blank?) ? content.strip.html_safe : content # rubocop:disable Rails/OutputSafety

content_tag(@tag, parsed_content, @content_tag_args.merge(@result))
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions app/components/primer/beta/link.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<%= render Primer::ConditionalWrapper.new(condition: tooltip?, tag: :span, position: :relative) do %>
<%= render(Primer::BaseComponent.new(**@system_arguments)) do -%>
<%= render Primer::ConditionalWrapper.new(condition: tooltip?, trim: true, tag: :span, position: :relative) do %>
<%= render(Primer::BaseComponent.new(trim: true, **@system_arguments)) do %>
<% if leading_visual %>
<span class="Link-visual Link-leadingVisual">
<%= leading_visual %>
</span>
<% end %>
<%= trimmed_content -%>
<%= content %>
<% if trailing_visual %>
<span class="Link-visual Link-trailingVisual">
<%= trailing_visual %>
</span>
<% end %>
<% end -%>
<%= tooltip %>
<% end %>
<%= tooltip if tooltip? %>
<% end %>
13 changes: 0 additions & 13 deletions app/components/primer/beta/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,6 @@ def initialize(href: nil, scheme: DEFAULT_SCHEME, muted: false, underline: false
def before_render
raise ArgumentError, "href is required" if @system_arguments[:href].nil? && !Rails.env.production?
end

private

def trimmed_content
return if content.blank?

trimmed_content = content.strip

return trimmed_content unless content.html_safe?

# strip unsets `html_safe`, so we have to set it back again to guarantee that HTML blocks won't break
trimmed_content.html_safe # rubocop:disable Rails/OutputSafety
end
end
end
end

0 comments on commit 04c9c94

Please sign in to comment.