Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
open-source
logilab-mtconverter
Commits
185be7d0dfa0
Commit
788e9dd1
authored
Jul 06, 2009
by
Sylvain Thénault
Browse files
fix for xml_escape called with unicode characters, string.translate want an unicode map...
parent
7b93cad27804
Changes
2
Hide whitespace changes
Inline
Side-by-side
__init__.py
View file @
185be7d0
...
...
@@ -95,6 +95,7 @@ TR_CONTROL_CHARS[ord('\f')] = '\n'
TR_CONTROL_CHARS
[
ord
(
'
\v
'
)]
=
'
\n
'
ESC_CAR_TABLE
=
string
.
maketrans
(
''
.
join
(
CONTROL_CHARS
),
''
.
join
(
TR_CONTROL_CHARS
))
ESC_UCAR_TABLE
=
unicode
(
ESC_CAR_TABLE
,
'latin1'
)
# XXX deprecate at some point (once less used :)
#@obsolete('use xml_escape')
...
...
@@ -103,7 +104,10 @@ def html_escape(data):
def
xml_escape
(
data
):
"""escapes XML forbidden characters in attributes and PCDATA"""
data
=
data
.
translate
(
ESC_CAR_TABLE
)
if
isinstance
(
data
,
unicode
):
data
=
data
.
translate
(
ESC_UCAR_TABLE
)
else
:
data
=
data
.
translate
(
ESC_CAR_TABLE
)
return
(
data
.
replace
(
'&'
,
'&'
).
replace
(
'<'
,
'<'
).
replace
(
'>'
,
'>'
)
.
replace
(
'"'
,
'"'
).
replace
(
"'"
,
'''
))
...
...
test/unittest_utils.py
View file @
185be7d0
...
...
@@ -34,6 +34,16 @@ class HtmlEscapeTC(TestCase):
if
car
in
SPECIAL_CHARS
:
continue
yield
self
.
assertEquals
,
xml_escape
(
car
),
' '
yield
self
.
assertEquals
,
xml_escape
(
u
'é'
),
u
'é'
def
test_escape_special_chars_unicode
(
self
):
for
car
,
trcar
in
SPECIAL_CHARS
.
items
():
yield
self
.
assertEquals
,
xml_escape
(
unicode
(
car
)),
trcar
for
carnum
in
xrange
(
32
):
car
=
chr
(
carnum
)
if
car
in
SPECIAL_CHARS
:
continue
yield
self
.
assertEquals
,
xml_escape
(
unicode
(
car
)),
' '
def
test_html_unescape
(
self
):
for
data
,
expected
in
[(
'toto'
,
'toto'
),
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment