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

lobsters: Avoid continiously instantiating HTMLEntities #337

Merged
merged 2 commits into from
Oct 1, 2024
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
2 changes: 1 addition & 1 deletion benchmarks/lobsters/app/models/story.rb
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ def description_or_story_text(chars = 0)
s = s.to_s[0, chars].gsub(/ [^ ]*\z/, "")
end

HTMLEntities.new.decode(s.to_s)
HtmlEncoder.decode(s.to_s)
end

def domain_search_url
Expand Down
6 changes: 2 additions & 4 deletions benchmarks/lobsters/app/views/comments/index.rss.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="UTF-8" ?>
<% coder = HTMLEntities.new %>
<rss version="2.0">
<channel>
<title><%= Rails.application.name %><%= @title.present? ?
Expand All @@ -9,14 +8,13 @@

<% @comments.each do |comment| %>
<item>
<title>on <%= raw coder.encode(comment.story.title, :decimal) %></title>
<title>on <%= raw HtmlEncoder.encode(comment.story.title) %></title>
<link><%= comment.url %></link>
<guid><%= comment.short_id_url %></guid>
<author><%= comment.user.username %>@users.<%= Rails.application.domain %> (<%= comment.user.username %>)</author>
<pubDate><%= comment.created_at.rfc2822 %></pubDate>
<comments><%= comment.url %></comments>
<description><%= raw coder.encode(comment.markeddown_comment,
:decimal) %></description>
<description><%= raw HtmlEncoder.encode(comment.markeddown_comment) %></description>
</item>
<% end %>
</channel>
Expand Down
9 changes: 4 additions & 5 deletions benchmarks/lobsters/app/views/home/rss.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<% coder = HTMLEntities.new %>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title><%= Rails.application.name %><%= @title.present? ?
Expand All @@ -10,17 +9,17 @@

<% @stories.each do |story| %>
<item>
<title><%= raw coder.encode(story.title, :decimal) %></title>
<title><%= raw HtmlEncoder.encode(story.title) %></title>
<link><%= story.url_or_comments_url %></link>
<guid isPermaLink="false"><%= story.short_id_url %></guid>
<author><%= story.user.username %>@users.<%= Rails.application.domain %> (<%= story.user.username %>)</author>
<pubDate><%= story.created_at.rfc2822 %></pubDate>
<comments><%= story.comments_url %></comments>
<description>
<%= raw coder.encode(story.markeddown_description, :decimal) %>
<%= raw HtmlEncoder.encode(story.markeddown_description) %>
<% if story.url.present? %>
<%= raw coder.encode("<p>" +
link_to("Comments", story.comments_url) + "</p>", :decimal) %>
<%= raw HtmlEncoder.encode("<p>" +
link_to("Comments", story.comments_url) + "</p>") %>
<% end %>
</description>
<% story.taggings.each do |tagging| %>
Expand Down
17 changes: 17 additions & 0 deletions benchmarks/lobsters/extras/html_encoder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# typed: false

require "cgi"

module HtmlEncoder
HTML_ENTITIES = HTMLEntities.new

class << self
def encode(string, type = :decimal)
HTML_ENTITIES.encode(string, type)
end

def decode(encoded_string)
CGI.unescape_html(encoded_string)
end
end
end
Loading