-
-
Save caius/1042474 to your computer and use it in GitHub Desktop.
escape_xpath_string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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