Skip to content

A platform for community discussion. Free, open, simple.

PERF: remove oga gem

Sam94b2c70c0d5f
diff --git a/Gemfile b/Gemfile
index ced4bbd2dc1..cf250465649 100644
--- a/Gemfile
+++ b/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'
diff --git a/Gemfile.lock b/Gemfile.lock
index 7029f5d7cee..09d9ff3bb30 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -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
diff --git a/lib/html_normalize.rb b/lib/html_normalize.rb
deleted file mode 100644
index 1243a4426a6..00000000000
--- a/lib/html_normalize.rb
+++ /dev/null
@@ -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
diff --git a/plugins/poll/spec/lib/pretty_text_spec.rb b/plugins/poll/spec/lib/pretty_text_spec.rb
index 85ee2c2a00e..fedc70eb986 100644
--- a/plugins/poll/spec/lib/pretty_text_spec.rb
+++ b/plugins/poll/spec/lib/pretty_text_spec.rb
@@ -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>
diff --git a/spec/components/html_normalize_spec.rb b/spec/components/html_normalize_spec.rb
deleted file mode 100644
index 7c75d8a15bd..00000000000
--- a/spec/components/html_normalize_spec.rb
+++ /dev/null
@@ -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
diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb
index 971d7dd6005..487ec569681 100644
--- a/spec/components/pretty_text_spec.rb
+++ b/spec/components/pretty_text_spec.rb
@@ -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