Skip to content

Generated page_update javascript is incorrectly escaped. #18

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions lib/action_view/helpers/prototype_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ def render(*options)

def with_formats(*args)
return yield unless @context

lookup = @context.lookup_context
begin
old_formats, lookup.formats = lookup.formats, args
Expand Down Expand Up @@ -587,7 +587,7 @@ def method_missing(method, *arguments)
# page.hide 'spinner'
# end
def update_page(&block)
JavaScriptGenerator.new(self, &block).to_s.html_safe
JavaScriptGenerator.new(self, &block).to_s
end

# Works like update_page but wraps the generated JavaScript in a
Expand Down
4 changes: 2 additions & 2 deletions lib/prototype-rails/javascript_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def button_to_function(name, *args, &block)
function = block_given? ? update_page(&block) : args[0] || ''
onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function};"

tag(:input, html_options.merge(:type => 'button', :value => name, :onclick => onclick))
tag(:input, html_options.merge(:type => 'button', :value => name, :onclick => escape_once(onclick)), false, false)
end

# link_to_function("Show me more", nil, :id => "more_link") do |page|
Expand All @@ -60,6 +60,6 @@ def link_to_function(name, *args, &block)
onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function}; return false;"
href = html_options[:href] || '#'

content_tag(:a, name, html_options.merge(:href => href, :onclick => onclick))
content_tag(:a, name, html_options.merge(:href => href, :onclick => escape_once(onclick)), false)
end
end
12 changes: 12 additions & 0 deletions test/template/prototype_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ def test_update_page_tag_with_html_options
assert_equal javascript_tag(create_generator(&block).to_s, {:defer => 'true'}), update_page_tag({:defer => 'true'}, &block)
end

def test_update_page_html_tag
block = Proc.new { |page| page.hide('cancel-commit') }
input_tag = submit_tag('Save', :onclick => 'Element.hide("cancel-commit");')
assert_equal input_tag, submit_tag('Save', :onclick => update_page(&block))
end

def test_update_page_link_to_function
block = Proc.new { |page| page.hide('cancel-commit') }
link_tag = link_to_function('Save', 'Element.hide("cancel-commit");')
assert_equal link_tag, link_to_function('Save', update_page(&block))
end

def test_remote_function
res = remote_function(:url => authors_path, :with => "'author[name]='+$F('author_name')+'&author[dob]='+$F('author_dob')")
assert_equal "new Ajax.Request('/authors', {asynchronous:true, evalScripts:true, parameters:'author[name]='+$F('author_name')+'&author[dob]='+$F('author_dob')})", res
Expand Down
49 changes: 37 additions & 12 deletions vendor/assets/javascripts/prototype_ujs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
(function() {
Ajax.Responders.register({
onCreate: function(request) {
var token = $$('meta[name=csrf-token]')[0];
if (token) {
if (!request.options.requestHeaders) request.options.requestHeaders = {};
request.options.requestHeaders['X-CSRF-Token'] = token.readAttribute('content');
}
}
});

// Technique from Juriy Zaytsev
// http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
function isEventSupported(eventName) {
Expand Down Expand Up @@ -130,11 +140,24 @@

function disableFormElements(form) {
form.select('input[type=submit][data-disable-with]').each(function(input) {
if (input.name == form.retrieve('rails:submit-button')) {
if (window.hiddenCommit) {
window.hiddenCommit.setAttribute('name', input.name);
window.hiddenCommit.setAttribute('value', input.value);
} else {
hiddenCommit = document.createElement('input');
hiddenCommit.type = 'hidden';
hiddenCommit.value = input.value;
hiddenCommit.name = input.name;
form.appendChild(hiddenCommit);
}
}

input.store('rails:original-value', input.getValue());
input.setValue(input.readAttribute('data-disable-with')).disable();
});
}

function enableFormElements(form) {
form.select('input[type=submit][data-disable-with]').each(function(input) {
input.setValue(input.retrieve('rails:original-value')).enable();
Expand All @@ -147,17 +170,19 @@
}

document.on('click', 'a[data-confirm], a[data-remote], a[data-method]', function(event, link) {
if (!allowAction(link)) {
event.stop();
return false;
}
if (Event.isLeftClick(event)) {
if (!allowAction(link)) {
event.stop();
return false;
}

if (link.readAttribute('data-remote')) {
handleRemote(link);
event.stop();
} else if (link.readAttribute('data-method')) {
handleMethod(link);
event.stop();
if (link.readAttribute('data-remote')) {
handleRemote(link);
event.stop();
} else if (link.readAttribute('data-method')) {
handleMethod(link);
event.stop();
}
}
});

Expand Down Expand Up @@ -185,7 +210,7 @@
document.on('ajax:create', 'form', function(event, form) {
if (form == event.findElement()) disableFormElements(form);
});

document.on('ajax:complete', 'form', function(event, form) {
if (form == event.findElement()) enableFormElements(form);
});
Expand Down