PERF: remove oga gem
oga gem is automatically required by the aws gem the oga gem retains about 1mb of memory, aws now uses nokogiri This also removes the html normalize from the pretty text specs that was a fair bit buggy as the polls test shows.
This commit is contained in:
parent
28365f8ae5
commit
94b2c70c0d
3
Gemfile
3
Gemfile
|
@ -67,9 +67,6 @@ gem 'multi_json'
|
|||
gem 'mustache'
|
||||
gem 'nokogiri'
|
||||
|
||||
# this may end up deprecating nokogiri
|
||||
gem 'oga', require: false
|
||||
|
||||
gem 'omniauth'
|
||||
gem 'omniauth-openid'
|
||||
gem 'openid-redis-store'
|
||||
|
|
|
@ -41,7 +41,6 @@ GEM
|
|||
annotate (2.7.2)
|
||||
activerecord (>= 3.2, < 6.0)
|
||||
rake (>= 10.4, < 13.0)
|
||||
ansi (1.5.0)
|
||||
arel (8.0.0)
|
||||
ast (2.3.0)
|
||||
aws-partitions (1.24.0)
|
||||
|
@ -200,9 +199,6 @@ GEM
|
|||
multi_json (~> 1.3)
|
||||
multi_xml (~> 0.5)
|
||||
rack (>= 1.2, < 3)
|
||||
oga (2.10)
|
||||
ast
|
||||
ruby-ll (~> 2.1)
|
||||
oj (3.1.0)
|
||||
omniauth (1.6.1)
|
||||
hashie (>= 3.4.6, < 3.6.0)
|
||||
|
@ -334,9 +330,6 @@ GEM
|
|||
rainbow (>= 2.2.2, < 3.0)
|
||||
ruby-progressbar (~> 1.7)
|
||||
unicode-display_width (~> 1.0, >= 1.0.1)
|
||||
ruby-ll (2.1.2)
|
||||
ansi
|
||||
ast
|
||||
ruby-openid (2.7.0)
|
||||
ruby-prof (0.16.2)
|
||||
ruby-progressbar (1.9.0)
|
||||
|
@ -459,7 +452,6 @@ DEPENDENCIES
|
|||
multi_json
|
||||
mustache
|
||||
nokogiri
|
||||
oga
|
||||
oj
|
||||
omniauth
|
||||
omniauth-facebook
|
||||
|
|
|
@ -1,151 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
#
|
||||
# this class is used to normalize html output for internal comparisons in specs
|
||||
#
|
||||
require 'oga'
|
||||
|
||||
class HtmlNormalize
|
||||
|
||||
def self.normalize(html)
|
||||
parsed = Oga.parse_html(html.strip, strict: true)
|
||||
if parsed.children.length != 1
|
||||
puts parsed.children.count
|
||||
raise "expecting a single child"
|
||||
end
|
||||
new(parsed.children.first).format
|
||||
end
|
||||
|
||||
SELF_CLOSE = Set.new(%w{area base br col command embed hr img input keygen line meta param source track wbr})
|
||||
|
||||
BLOCK = Set.new(%w{
|
||||
html
|
||||
body
|
||||
aside
|
||||
p
|
||||
h1 h2 h3 h4 h5 h6
|
||||
ol ul
|
||||
address
|
||||
blockquote
|
||||
dl
|
||||
div
|
||||
fieldset
|
||||
form
|
||||
hr
|
||||
noscript
|
||||
table
|
||||
pre
|
||||
})
|
||||
|
||||
def initialize(doc)
|
||||
@doc = doc
|
||||
end
|
||||
|
||||
def format
|
||||
buffer = String.new
|
||||
dump_node(@doc, 0, buffer)
|
||||
buffer.strip!
|
||||
buffer
|
||||
end
|
||||
|
||||
def inline?(node)
|
||||
Oga::XML::Text === node || !BLOCK.include?(node.name.downcase)
|
||||
end
|
||||
|
||||
def dump_node(node, indent = 0, buffer)
|
||||
|
||||
if Oga::XML::Text === node
|
||||
if node.parent&.name
|
||||
buffer << node.text
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
name = node.name.downcase
|
||||
|
||||
block = BLOCK.include?(name)
|
||||
|
||||
buffer << " " * indent * 2 if block
|
||||
|
||||
buffer << "<" << name
|
||||
|
||||
attrs = node&.attributes
|
||||
if (attrs && attrs.length > 0)
|
||||
attrs.sort! { |x, y| x.name <=> y.name }
|
||||
attrs.each do |a|
|
||||
buffer << " "
|
||||
buffer << a.name
|
||||
if a.value
|
||||
buffer << "='"
|
||||
buffer << a.value
|
||||
buffer << "'"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
buffer << ">"
|
||||
|
||||
if block
|
||||
buffer << "\n"
|
||||
end
|
||||
|
||||
children = node.children
|
||||
children = trim(children) if block
|
||||
|
||||
inline_buffer = nil
|
||||
|
||||
children&.each do |child|
|
||||
if block && inline?(child)
|
||||
inline_buffer ||= String.new
|
||||
dump_node(child, indent + 1, inline_buffer)
|
||||
else
|
||||
if inline_buffer
|
||||
buffer << " " * (indent + 1) * 2
|
||||
buffer << inline_buffer.strip
|
||||
inline_buffer = nil
|
||||
else
|
||||
dump_node(child, indent + 1, buffer)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if inline_buffer
|
||||
buffer << " " * (indent + 1) * 2
|
||||
buffer << inline_buffer.strip
|
||||
inline_buffer = nil
|
||||
end
|
||||
|
||||
if block
|
||||
buffer << "\n" unless buffer[-1] == "\n"
|
||||
buffer << " " * indent * 2
|
||||
end
|
||||
|
||||
unless SELF_CLOSE.include?(name)
|
||||
buffer << "</" << name
|
||||
buffer << ">\n"
|
||||
end
|
||||
end
|
||||
|
||||
def trim(nodes)
|
||||
start = 0
|
||||
finish = nodes.length
|
||||
|
||||
nodes.each do |n|
|
||||
if Oga::XML::Text === n && n.text.blank?
|
||||
start += 1
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
nodes.reverse_each do |n|
|
||||
if Oga::XML::Text === n && n.text.blank?
|
||||
finish -= 1
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
nodes[start...finish]
|
||||
end
|
||||
|
||||
end
|
|
@ -1,10 +1,9 @@
|
|||
require 'rails_helper'
|
||||
require 'html_normalize'
|
||||
|
||||
describe PrettyText do
|
||||
|
||||
def n(html)
|
||||
HtmlNormalize.normalize(html)
|
||||
html.strip
|
||||
end
|
||||
|
||||
it 'supports multi choice polls' do
|
||||
|
@ -95,14 +94,13 @@ describe PrettyText do
|
|||
cooked = PrettyText.cook md
|
||||
|
||||
expected = <<~MD
|
||||
<div class="poll" data-poll-status="open" data-poll-name="poll" data-poll-type="multiple">
|
||||
<div class="poll" data-poll-status="open" data-poll-type="multiple" data-poll-name="poll">
|
||||
<div>
|
||||
<div class="poll-container">
|
||||
<ol>
|
||||
<li data-poll-option-id='b6475cbf6acb8676b20c60582cfc487a'>test 1 <img alt=':slight_smile:' class='emoji' src='/images/emoji/twitter/slight_smile.png?v=5' title=':slight_smile:'> <b>test</b>
|
||||
</li>
|
||||
<li data-poll-option-id='7158af352698eb1443d709818df097d4'>test 2</li>
|
||||
<li data-poll-option-id="b6475cbf6acb8676b20c60582cfc487a">test 1 <img src="/images/emoji/twitter/slight_smile.png?v=5" title=":slight_smile:" class="emoji" alt=":slight_smile:"> <b>test</b>
|
||||
</li>
|
||||
<li data-poll-option-id="7158af352698eb1443d709818df097d4">test 2</li>
|
||||
</ol>
|
||||
</div>
|
||||
<div class="poll-info">
|
||||
|
@ -110,12 +108,9 @@ describe PrettyText do
|
|||
<span class="info-number">0</span>
|
||||
<span class="info-text">voters</span>
|
||||
</p>
|
||||
<p>
|
||||
Choose up to <strong>2</strong> options</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="poll-buttons">
|
||||
<a title="Cast your votes">Vote now!</a>
|
||||
<a title="Display the poll results">Show results</a>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -1,73 +0,0 @@
|
|||
require 'rails_helper'
|
||||
require 'html_normalize'
|
||||
|
||||
describe HtmlNormalize do
|
||||
|
||||
def n(html)
|
||||
HtmlNormalize.normalize(html)
|
||||
end
|
||||
|
||||
it "handles attributes without values" do
|
||||
expect(n "<img alt>").to eq("<img alt>")
|
||||
end
|
||||
|
||||
it "handles self closing tags" do
|
||||
|
||||
source = <<-HTML
|
||||
<div>
|
||||
<span><img src='testing'>
|
||||
boo</span>
|
||||
</div>
|
||||
HTML
|
||||
expect(n source).to eq(source.strip)
|
||||
end
|
||||
|
||||
it "Can handle aside" do
|
||||
|
||||
source = <<~HTML
|
||||
<aside class="quote" data-topic="2" data-post="1">
|
||||
<div class="title">
|
||||
<div class="quote-controls"></div>
|
||||
<a href="http://test.localhost/t/this-is-a-test-topic-slight-smile/x/2">This is a test topic <img src="/images/emoji/emoji_one/slight_smile.png?v=5" title="slight_smile" alt="slight_smile" class="emoji"></a></div>
|
||||
<blockquote>
|
||||
<p>ddd</p>
|
||||
</blockquote></aside>
|
||||
HTML
|
||||
expected = <<~HTML
|
||||
<aside class="quote" data-post="1" data-topic="2">
|
||||
<div class="title">
|
||||
<div class="quote-controls"></div>
|
||||
<a href="http://test.localhost/t/this-is-a-test-topic-slight-smile/x/2">This is a test topic <img src="/images/emoji/emoji_one/slight_smile.png?v=5" title="slight_smile" alt="slight_smile" class="emoji"></a>
|
||||
</div>
|
||||
<blockquote>
|
||||
<p>ddd</p>
|
||||
</blockquote>
|
||||
</aside>
|
||||
HTML
|
||||
|
||||
expect(n expected).to eq(n source)
|
||||
end
|
||||
|
||||
it "Can normalize attributes" do
|
||||
|
||||
source = "<a class='a b' name='sam'>b</a>"
|
||||
same = "<a name='sam' class='a b' >b</a>"
|
||||
|
||||
expect(n source).to eq(n same)
|
||||
end
|
||||
|
||||
it "Can indent divs nicely" do
|
||||
source = "<div> <div><div>hello world</div> </div> </div>"
|
||||
expected = <<~HTML
|
||||
<div>
|
||||
<div>
|
||||
<div>
|
||||
hello world
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
HTML
|
||||
|
||||
expect(n source).to eq(expected.strip)
|
||||
end
|
||||
end
|
|
@ -1,6 +1,5 @@
|
|||
require 'rails_helper'
|
||||
require 'pretty_text'
|
||||
require 'html_normalize'
|
||||
|
||||
describe PrettyText do
|
||||
|
||||
|
@ -9,11 +8,11 @@ describe PrettyText do
|
|||
end
|
||||
|
||||
def n(html)
|
||||
HtmlNormalize.normalize(html)
|
||||
html.strip
|
||||
end
|
||||
|
||||
def cook(*args)
|
||||
n(PrettyText.cook(*args))
|
||||
PrettyText.cook(*args)
|
||||
end
|
||||
|
||||
let(:wrapped_image) { "<div class=\"lightbox-wrapper\"><a href=\"//localhost:3000/uploads/default/4399/33691397e78b4d75.png\" class=\"lightbox\" title=\"Screen Shot 2014-04-14 at 9.47.10 PM.png\"><img src=\"//localhost:3000/uploads/default/_optimized/bd9/b20/bbbcd6a0c0_655x500.png\" width=\"655\" height=\"500\"><div class=\"meta\">\n<span class=\"filename\">Screen Shot 2014-04-14 at 9.47.10 PM.png</span><span class=\"informations\">966x737 1.47 MB</span><span class=\"expand\"></span>\n</div></a></div>" }
|
||||
|
@ -33,13 +32,13 @@ describe PrettyText do
|
|||
|
||||
topic = Fabricate(:topic, title: "this is a test topic :slight_smile:")
|
||||
expected = <<~HTML
|
||||
<aside class="quote" data-topic="#{topic.id}" data-post="2">
|
||||
<aside class="quote" data-post="2" data-topic="#{topic.id}">
|
||||
<div class="title">
|
||||
<div class="quote-controls"></div>
|
||||
<a href="http://test.localhost/t/this-is-a-test-topic-slight-smile/#{topic.id}/2">This is a test topic <img src="/images/emoji/twitter/slight_smile.png?v=5" title="slight_smile" alt="slight_smile" class="emoji"></a>
|
||||
<div class="quote-controls"></div>
|
||||
<a href="http://test.localhost/t/this-is-a-test-topic-slight-smile/#{topic.id}/2">This is a test topic <img src="/images/emoji/twitter/slight_smile.png?v=5" title="slight_smile" alt="slight_smile" class="emoji"></a>
|
||||
</div>
|
||||
<blockquote>
|
||||
<p>ddd</p>
|
||||
<p>ddd</p>
|
||||
</blockquote>
|
||||
</aside>
|
||||
HTML
|
||||
|
@ -126,13 +125,13 @@ describe PrettyText do
|
|||
|
||||
topic = Fabricate(:topic, title: "this is a test topic")
|
||||
expected = <<~HTML
|
||||
<aside class="quote group-#{group.name}" data-topic="#{topic.id}" data-post="2">
|
||||
<aside class="quote group-#{group.name}" data-post="2" data-topic="#{topic.id}">
|
||||
<div class="title">
|
||||
<div class="quote-controls"></div>
|
||||
<img alt class='avatar' height='20' src='//test.localhost/uploads/default/avatars/42d/57c/46ce7ee487/40.png' width='20'><a href='http://test.localhost/t/this-is-a-test-topic/#{topic.id}/2'>This is a test topic</a>
|
||||
<div class="quote-controls"></div>
|
||||
<img alt width="20" height="20" src="//test.localhost/uploads/default/avatars/42d/57c/46ce7ee487/40.png" class="avatar"><a href="http://test.localhost/t/this-is-a-test-topic/#{topic.id}/2">This is a test topic</a>
|
||||
</div>
|
||||
<blockquote>
|
||||
<p>ddd</p>
|
||||
<p>ddd</p>
|
||||
</blockquote>
|
||||
</aside>
|
||||
HTML
|
||||
|
|
Loading…
Reference in New Issue