Skip to content
Snippets Groups Projects
Commit 5ab0bb6f authored by Henrik tom Wörden's avatar Henrik tom Wörden
Browse files

FIX: add quote character to escaped characters

parent 924c4d29
Branches
Tags
1 merge request!130Release v0.14.0
Pipeline #47103 passed
......@@ -19,13 +19,15 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
import warnings
def escape_quoted_text(text: str) -> str:
"""The characters ``\\`` and ``*`` need to be escaped if used in quoted expressions in the query
language.
This function return the given string where the characters ``\\`` and ``*`` are escaped by
a ``\\``.
def escape_squoted_text(text: str) -> str:
"""The characters ``\\``, ``*`` and ``'`` need to be escaped if used in single quoted
expressions in the query language.
This function returns the given string where the characters ``\\``, ``'`` and ``*`` are
escaped by a ``\\``.
Parameters
----------
......@@ -37,4 +39,32 @@ def escape_quoted_text(text: str) -> str:
str
The escaped text
"""
return text.replace('\\', r'\\').replace('*', r'\*')
return text.replace('\\', r'\\').replace("'", "\\'").replace('*', r'\*')
def escape_dquoted_text(text: str) -> str:
"""The characters ``\\``, ``*`` and ``"`` need to be escaped if used in double quoted
expressions in the query language.
This function returns the given string where the characters ``\\``, ``"`` and ``*`` are
escaped by a ``\\``.
Parameters
----------
text : str
The text to be escaped
Returns
-------
str
The escaped text
"""
return text.replace('\\', r'\\').replace('"', '\\"').replace('*', r'\*')
def escape_quoted_text(text: str) -> str:
"""
Please use escape_squoted_text or escape_dquoted_text
"""
warnings.warn("Please use escape_squoted_text or escape_dquoted_text", DeprecationWarning)
return escape_squoted_text(text)
......@@ -25,7 +25,8 @@
from __future__ import unicode_literals
from linkahead.common.utils import xml2str
from linkahead.utils.escape import escape_quoted_text
from linkahead.utils.escape import (escape_dquoted_text, escape_quoted_text,
escape_squoted_text)
from lxml.etree import Element
......@@ -41,3 +42,5 @@ def test_escape_quoted_text():
assert escape_quoted_text("bl\\a") == "bl\\\\a"
assert escape_quoted_text("bl*a") == "bl\\*a"
assert escape_quoted_text("bl*ab\\\\lab\\*labla") == "bl\\*ab\\\\\\\\lab\\\\\\*labla"
assert escape_squoted_text("bl'a") == "bl\\'a"
assert escape_dquoted_text('bl"a') == 'bl\\"a'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment