Skip to content

Commit

Permalink
reformat code to escape quotes (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
vergilet authored Jul 22, 2022
1 parent 5fb48e2 commit 6bb41f1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
26 changes: 13 additions & 13 deletions lib/repost/senpai.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def perform
:submit_text, :authenticity_token, :charset, :autosubmit_nonce

def form_head
"<form id='#{form_id}' action='#{url}' method='#{method}' accept-charset='#{charset}'>"
%Q(<form id="#{form_id}" action="#{url}" method="#{method}" accept-charset="#{charset}">)
end

def form_body
Expand All @@ -57,29 +57,29 @@ def form_input(key, value)
form_input("#{key}[]", inner_value)
end.join
else
"<input type='hidden' name='#{key}' value=#{process_value(value)}>"
%Q(<input type="hidden" name="#{key}" value=#{process_value(value)}>)
end
end

def form_footer
"</form>"
%Q(</form>)
end

def csrf_token
"<input name='authenticity_token' value='#{authenticity_token}' type='hidden'>"
%Q(<input name="authenticity_token" value="#{authenticity_token}" type="hidden">)
end

def no_script
"<noscript>
%Q(<noscript>
#{submit_section}
</noscript>"
</noscript>)
end

def submit_section
"<div class='#{section_classes}'>
%Q(<div class="#{section_classes}">
#{section_html}
<input class='#{submit_classes}' type='submit' value='#{submit_text}'></input>
</div>"
<input class="#{submit_classes}" type="submit" value="#{submit_text}"></input>
</div>)
end

def generated_form_id
Expand All @@ -88,14 +88,14 @@ def generated_form_id

def auto_submit_script
nonce_attr = %Q( nonce="#{autosubmit_nonce}") if autosubmit_nonce
"<script#{nonce_attr}>
document.getElementById('#{form_id}').submit();
</script>"
%Q(<script#{nonce_attr}>
document.getElementById("#{form_id}").submit();
</script>)
end

def process_value(value)
return value if value.is_a?(Integer)
'\'' + value.to_s + '\''
%Q("#{value.to_s.gsub("\"", '\'')}")
end
end
end
39 changes: 22 additions & 17 deletions spec/senpai_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
aggregate_failures do
expect(html).to include('form')
expect(html).to include(url)
expect(html).to include("type='submit'")
expect(html).to include("type=\"submit\"")
end
end

Expand All @@ -24,22 +24,27 @@
count: 696,
string_size: '234',
boolean: true,
string_boolean: 'false'
string_boolean: 'false',
quoted_string: "Tuan O'Keefe",
double_quoted_string: "Sam O\"Daren"
}
end

let(:html) { described_class.perform(url, params: params) }

it 'generates post form' do
aggregate_failures do
expect(html).to include("input type='hidden'")
expect(html).to include("value='#{params[:name]}'")
expect(html).to include("value='#{params[:description]}'")
expect(html).to include("value='#{params[:string_size]}'")
expect(html).to include("value='#{params[:string_boolean]}'")
puts html
expect(html).to include("input type=\"hidden\"")
expect(html).to include("value=\"#{params[:name]}\"")
expect(html).to include("value=\"#{params[:description]}\"")
expect(html).to include("value=\"#{params[:string_size]}\"")
expect(html).to include("value=\"#{params[:string_boolean]}\"")
expect(html).to include("value=\"#{params[:quoted_string]}\"")
expect(html).to include("value=\"#{params[:double_quoted_string].gsub("\"", "\'")}\"")

expect(html).to include("value=#{params[:count]}")
expect(html).to include("value='#{params[:boolean]}'")
expect(html).to include("value=\"#{params[:boolean]}\"")
end
end

Expand All @@ -58,13 +63,13 @@

it 'handles arbitrarily nested params' do
aggregate_failures do
expect(html).to include("name='top_level[top_level_item]' value='hello'")
expect(html).to include("name='top_level[second_level][third_level]' value='qwerty'")
expect(html).to include("name='top_level[second_level][array][]' value=1")
expect(html).to include("name='top_level[second_level][array][]' value=2")
expect(html).to include("name='top_level[second_level][array][]' value='3'")
expect(html).to include("name='top_level[second_level][array][][a]' value=4")
expect(html).to include("name='top_level[second_level][array][][b]' value='5'")
expect(html).to include("name=\"top_level[top_level_item]\" value=\"hello\"")
expect(html).to include("name=\"top_level[second_level][third_level]\" value=\"qwerty\"")
expect(html).to include("name=\"top_level[second_level][array][]\" value=1")
expect(html).to include("name=\"top_level[second_level][array][]\" value=2")
expect(html).to include("name=\"top_level[second_level][array][]\" value=\"3\"")
expect(html).to include("name=\"top_level[second_level][array][][a]\" value=4")
expect(html).to include("name=\"top_level[second_level][array][][b]\" value=\"5\"")
end
end
end
Expand All @@ -81,8 +86,8 @@

it 'handles enumerable params' do
aggregate_failures do
expect(html).to include("name='multi_item[]' value='hello'")
expect(html).to include("name='second_level[multi_item][]' value='qwerty'")
expect(html).to include("name=\"multi_item[]\" value=\"hello\"")
expect(html).to include("name=\"second_level[multi_item][]\" value=\"qwerty\"")
end
end
end
Expand Down

0 comments on commit 6bb41f1

Please sign in to comment.