Skip to content

Instantly share code, notes, and snippets.

@caius
Forked from ashmoran/escape_xpath_string.rb
Created June 23, 2011 12:49
Show Gist options
  • Save caius/1042474 to your computer and use it in GitHub Desktop.
Save caius/1042474 to your computer and use it in GitHub Desktop.
escape_xpath_string
# escape_xpath_string("'Allo 'Allo is the dogs'")
# #=> %Q{concat('',"'",'Allo ',"'",'Allo is the dogs',"'",'')}
#
# ... which is fine, but I'd prefer it if it returned ...
#
# #=> %Q{concat("'",'Allo ',"'",'Allo is the dogs',"'")}
def escape_xpath_string(string)
return string unless string.include?("'")
components = string.split("'", -1).map do |component|
"'#{component}'" unless component == ""
end.compact
%{concat("'",#{components.join(%Q{,"'",})},"'")}
end
escape_xpath_string("'Allo 'Allo is the dogs'") # => "concat(\"'\",'Allo ',\"'\",'Allo is the dogs',\"'\")"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment