Commit 90b68e06 authored by Lukas 'Eipi' Eipert's avatar Lukas 'Eipi' Eipert

Prevent empty classes in ansi2html conversion

Currently we write out empty CSS classes (`class=""`) every time we
create a new tag. This adds 9 unnecessary bytes per span element. In a
recent trace, I have counted 11950 span elements. So we transported 105
unnecessary kilobytes!
parent 004b72fe
......@@ -314,7 +314,12 @@ module Gitlab
css_classes += sections.map { |section| "js-s-#{section}" }
end
@out << %{<span class="#{css_classes.join(' ')}">}
@out << if css_classes.any?
%{<span class="#{css_classes.join(' ')}">}
else
%{<span>}
end
@n_open_tags += 1
end
......
......@@ -546,7 +546,7 @@ describe Projects::JobsController, :clean_gitlab_redis_shared_state do
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['id']).to eq job.id
expect(json_response['status']).to eq job.status
expect(json_response['html']).to eq('<span class="">BUILD TRACE</span>')
expect(json_response['html']).to eq('<span>BUILD TRACE</span>')
end
end
......
......@@ -6,11 +6,11 @@ describe Gitlab::Ci::Ansi2html do
subject { described_class }
it "prints non-ansi as-is" do
expect(convert_html("Hello")).to eq('<span class="">Hello</span>')
expect(convert_html("Hello")).to eq('<span>Hello</span>')
end
it "strips non-color-changing control sequences" do
expect(convert_html("Hello \e[2Kworld")).to eq('<span class="">Hello world</span>')
expect(convert_html("Hello \e[2Kworld")).to eq('<span>Hello world</span>')
end
it "prints simply red" do
......@@ -34,7 +34,7 @@ describe Gitlab::Ci::Ansi2html do
end
it "resets colors after red on blue" do
expect(convert_html("\e[31;44mHello\e[0m world")).to eq('<span class="term-fg-red term-bg-blue">Hello</span><span class=""> world</span>')
expect(convert_html("\e[31;44mHello\e[0m world")).to eq('<span class="term-fg-red term-bg-blue">Hello</span><span> world</span>')
end
it "performs color change from red/blue to yellow/blue" do
......@@ -46,11 +46,11 @@ describe Gitlab::Ci::Ansi2html do
end
it "performs color change from red/blue to reset to yellow/green" do
expect(convert_html("\e[31;44mHello\e[0m \e[33;42mworld")).to eq('<span class="term-fg-red term-bg-blue">Hello</span><span class=""> </span><span class="term-fg-yellow term-bg-green">world</span>')
expect(convert_html("\e[31;44mHello\e[0m \e[33;42mworld")).to eq('<span class="term-fg-red term-bg-blue">Hello</span><span> </span><span class="term-fg-yellow term-bg-green">world</span>')
end
it "ignores unsupported codes" do
expect(convert_html("\e[51mHello\e[0m")).to eq('<span class="">Hello</span>')
expect(convert_html("\e[51mHello\e[0m")).to eq('<span>Hello</span>')
end
it "prints light red" do
......@@ -74,8 +74,8 @@ describe Gitlab::Ci::Ansi2html do
end
it "resets bold text" do
expect(convert_html("\e[1mHello\e[21m world")).to eq('<span class="term-bold">Hello</span><span class=""> world</span>')
expect(convert_html("\e[1mHello\e[22m world")).to eq('<span class="term-bold">Hello</span><span class=""> world</span>')
expect(convert_html("\e[1mHello\e[21m world")).to eq('<span class="term-bold">Hello</span><span> world</span>')
expect(convert_html("\e[1mHello\e[22m world")).to eq('<span class="term-bold">Hello</span><span> world</span>')
end
it "prints italic text" do
......@@ -83,7 +83,7 @@ describe Gitlab::Ci::Ansi2html do
end
it "resets italic text" do
expect(convert_html("\e[3mHello\e[23m world")).to eq('<span class="term-italic">Hello</span><span class=""> world</span>')
expect(convert_html("\e[3mHello\e[23m world")).to eq('<span class="term-italic">Hello</span><span> world</span>')
end
it "prints underlined text" do
......@@ -91,7 +91,7 @@ describe Gitlab::Ci::Ansi2html do
end
it "resets underlined text" do
expect(convert_html("\e[4mHello\e[24m world")).to eq('<span class="term-underline">Hello</span><span class=""> world</span>')
expect(convert_html("\e[4mHello\e[24m world")).to eq('<span class="term-underline">Hello</span><span> world</span>')
end
it "prints concealed text" do
......@@ -99,7 +99,7 @@ describe Gitlab::Ci::Ansi2html do
end
it "resets concealed text" do
expect(convert_html("\e[8mHello\e[28m world")).to eq('<span class="term-conceal">Hello</span><span class=""> world</span>')
expect(convert_html("\e[8mHello\e[28m world")).to eq('<span class="term-conceal">Hello</span><span> world</span>')
end
it "prints crossed-out text" do
......@@ -107,7 +107,7 @@ describe Gitlab::Ci::Ansi2html do
end
it "resets crossed-out text" do
expect(convert_html("\e[9mHello\e[29m world")).to eq('<span class="term-cross">Hello</span><span class=""> world</span>')
expect(convert_html("\e[9mHello\e[29m world")).to eq('<span class="term-cross">Hello</span><span> world</span>')
end
it "can print 256 xterm fg colors" do
......@@ -139,15 +139,15 @@ describe Gitlab::Ci::Ansi2html do
end
it "prints &lt;" do
expect(convert_html("<")).to eq('<span class="">&lt;</span>')
expect(convert_html("<")).to eq('<span>&lt;</span>')
end
it "replaces newlines with line break tags" do
expect(convert_html("\n")).to eq('<span class=""><br/><span class=""></span></span>')
expect(convert_html("\n")).to eq('<span><br/><span></span></span>')
end
it "groups carriage returns with newlines" do
expect(convert_html("\r\n")).to eq('<span class=""><br/><span class=""></span></span>')
expect(convert_html("\r\n")).to eq('<span><br/><span></span></span>')
end
describe "incremental update" do
......@@ -184,7 +184,7 @@ describe Gitlab::Ci::Ansi2html do
context "with partial sequence" do
let(:pre_text) { "Hello\e" }
let(:pre_html) { "<span class=\"\">Hello</span>" }
let(:pre_html) { "<span>Hello</span>" }
let(:text) { "[1m World" }
let(:html) { "<span class=\"term-bold\"> World</span>" }
......@@ -193,9 +193,9 @@ describe Gitlab::Ci::Ansi2html do
context 'with new line' do
let(:pre_text) { "Hello\r" }
let(:pre_html) { "<span class=\"\">Hello\r</span>" }
let(:pre_html) { "<span>Hello\r</span>" }
let(:text) { "\nWorld" }
let(:html) { "<span class=\"\"><br/><span class=\"\">World</span></span>" }
let(:html) { "<span><br/><span>World</span></span>" }
it_behaves_like 'stateable converter'
end
......@@ -222,7 +222,7 @@ describe Gitlab::Ci::Ansi2html do
text = "#{section_start}Some text#{section_end}"
class_name_start = section_start.gsub("\033[0K", '').gsub('<', '&lt;')
class_name_end = section_end.gsub("\033[0K", '').gsub('<', '&lt;')
html = %{<span class="">#{class_name_start}Some text#{class_name_end}</span>}
html = %{<span>#{class_name_start}Some text#{class_name_end}</span>}
expect(convert_html(text)).to eq(html)
end
......
......@@ -65,9 +65,9 @@ describe Gitlab::Ci::Trace::Stream, :clean_gitlab_redis_cache do
result = stream.html
expect(result).to eq(
"<span class=\"\">ヾ(´༎ຶД༎ຶ`)ノ<br/><span class=\"\"></span></span>"\
"<span class=\"term-fg-green\">許功蓋</span><span class=\"\"><br/>"\
"<span class=\"\"></span></span>")
"<span>ヾ(´༎ຶД༎ຶ`)ノ<br/><span></span></span>"\
"<span class=\"term-fg-green\">許功蓋</span><span><br/>"\
"<span></span></span>")
expect(result.encoding).to eq(Encoding.default_external)
end
end
......@@ -253,7 +253,7 @@ describe Gitlab::Ci::Trace::Stream, :clean_gitlab_redis_cache do
it 'returns html content with state' do
result = stream.html_with_state
expect(result.html).to eq("<span class=\"\">1234</span>")
expect(result.html).to eq("<span>1234</span>")
end
context 'follow-up state' do
......@@ -269,7 +269,7 @@ describe Gitlab::Ci::Trace::Stream, :clean_gitlab_redis_cache do
result = stream.html_with_state(last_result.state)
expect(result.append).to be_truthy
expect(result.html).to eq("<span class=\"\">5678</span>")
expect(result.html).to eq("<span>5678</span>")
end
end
end
......@@ -306,12 +306,12 @@ describe Gitlab::Ci::Trace::Stream, :clean_gitlab_redis_cache do
shared_examples_for 'htmls' do
it "returns html" do
expect(stream.html).to eq(
"<span class=\"\">12<br/><span class=\"\">34<br/>"\
"<span class=\"\">56</span></span></span>")
"<span>12<br/><span>34<br/>"\
"<span>56</span></span></span>")
end
it "returns html for last line only" do
expect(stream.html(last_lines: 1)).to eq("<span class=\"\">56</span>")
expect(stream.html(last_lines: 1)).to eq("<span>56</span>")
end
end
......
......@@ -7,11 +7,11 @@ shared_examples_for 'common trace features' do
end
it "returns formatted html" do
expect(trace.html).to eq("<span class=\"\">12<br/><span class=\"\">34</span></span>")
expect(trace.html).to eq("<span>12<br/><span>34</span></span>")
end
it "returns last line of formatted html" do
expect(trace.html(last_lines: 1)).to eq("<span class=\"\">34</span>")
expect(trace.html(last_lines: 1)).to eq("<span>34</span>")
end
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment