Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Z
Zope
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
Zope
Commits
020a6d18
Commit
020a6d18
authored
Jan 09, 2007
by
Andreas Jung
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
merge from trunk (PreferredResolver, more tests), fixes Collector #2180
parent
c3a84f48
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
96 additions
and
5 deletions
+96
-5
lib/python/Products/PageTemplates/configure.zcml
lib/python/Products/PageTemplates/configure.zcml
+1
-1
lib/python/Products/PageTemplates/tests/testZopePageTemplate.py
...thon/Products/PageTemplates/tests/testZopePageTemplate.py
+54
-3
lib/python/Products/PageTemplates/unicodeconflictresolver.py
lib/python/Products/PageTemplates/unicodeconflictresolver.py
+41
-1
No files found.
lib/python/Products/PageTemplates/configure.zcml
View file @
020a6d18
...
...
@@ -2,7 +2,7 @@
<utility
provides="Products.PageTemplates.interfaces.IUnicodeEncodingConflictResolver"
component="Products.PageTemplates.unicodeconflictresolver.
DefaultUnicodeEncodingConflic
tResolver"
component="Products.PageTemplates.unicodeconflictresolver.
PreferredCharse
tResolver"
/>
</configure>
lib/python/Products/PageTemplates/tests/testZopePageTemplate.py
View file @
020a6d18
...
...
@@ -11,19 +11,25 @@ Note: Tests require Zope >= 2.7
import
unittest
import
Zope2
import
transaction
import
zope.component.testing
from
zope.traversing.adapters
import
DefaultTraversable
from
zope.traversing.adapters
import
DefaultTraversable
,
Traverser
from
zope.publisher.http
import
HTTPCharsets
from
Testing.makerequest
import
makerequest
from
Testing.ZopeTestCase
import
ZopeTestCase
,
installProduct
from
Products.PageTemplates.ZopePageTemplate
import
ZopePageTemplate
,
manage_addPageTemplate
from
Products.PageTemplates.utils
import
encodingFromXMLPreamble
,
charsetFromMetaEquiv
from
zope.component
import
provideUtility
from
Products.PageTemplates.interfaces
import
IUnicodeEncodingConflictResolver
from
Products.PageTemplates.unicodeconflictresolver
import
PreferredCharsetResolver
ascii_str
=
'<html><body>hello world</body></html>'
iso885915_str
=
'<html><body></body></html>'
utf8_str
=
unicode
(
iso885915_str
,
'iso-8859-15'
).
encode
(
'utf-8'
)
xml_template
=
'''<?xml vesion="1.0" encoding="%s"?>
xml_template
=
'''<?xml ve
r
sion="1.0" encoding="%s"?>
<foo>
</foo>
...
...
@@ -78,6 +84,50 @@ class ZPTUtilsTests(unittest.TestCase):
self
.
assertEqual
(
extract
(
'<html>...<html>'
),
None
)
class
ZPTUnicodeEncodingConflictResolution
(
ZopeTestCase
):
def
setUp
(
self
):
super
(
ZPTUnicodeEncodingConflictResolution
,
self
).
setUp
()
zope
.
component
.
provideAdapter
(
DefaultTraversable
,
(
None
,))
zope
.
component
.
provideAdapter
(
HTTPCharsets
,
(
None
,))
provideUtility
(
PreferredCharsetResolver
,
IUnicodeEncodingConflictResolver
)
transaction
.
begin
()
def
tearDown
(
self
):
transaction
.
abort
()
self
.
app
.
_p_jar
.
close
()
def
testISO_8859_15
(
self
):
manage_addPageTemplate
(
self
.
app
,
'test'
,
text
=
'<div tal:content="python: request.get(
\
'
data
\
'
)" />'
,
encoding
=
'ascii'
)
zpt
=
self
.
app
[
'test'
]
self
.
app
.
REQUEST
.
set
(
'HTTP_ACCEPT_CHARSET'
,
'ISO-8859-15,utf-8'
)
self
.
app
.
REQUEST
.
set
(
'data'
,
''
)
result
=
zpt
.
pt_render
()
self
.
assertEqual
(
result
.
startswith
(
unicode
(
'<div></div>'
,
'iso-8859-15'
)),
True
)
def
testUTF8
(
self
):
manage_addPageTemplate
(
self
.
app
,
'test'
,
text
=
'<div tal:content="python: request.get(
\
'
data
\
'
)" />'
,
encoding
=
'ascii'
)
zpt
=
self
.
app
[
'test'
]
self
.
app
.
REQUEST
.
set
(
'HTTP_ACCEPT_CHARSET'
,
'utf-8,ISO-8859-15'
)
self
.
app
.
REQUEST
.
set
(
'data'
,
unicode
(
''
,
'iso-8859-15'
).
encode
(
'utf-8'
))
result
=
zpt
.
pt_render
()
self
.
assertEqual
(
result
.
startswith
(
unicode
(
'<div></div>'
,
'iso-8859-15'
)),
True
)
def
testUTF8WrongPreferredCharset
(
self
):
manage_addPageTemplate
(
self
.
app
,
'test'
,
text
=
'<div tal:content="python: request.get(
\
'
data
\
'
)" />'
,
encoding
=
'ascii'
)
zpt
=
self
.
app
[
'test'
]
self
.
app
.
REQUEST
.
set
(
'HTTP_ACCEPT_CHARSET'
,
'iso-8859-15'
)
self
.
app
.
REQUEST
.
set
(
'data'
,
unicode
(
''
,
'iso-8859-15'
).
encode
(
'utf-8'
))
result
=
zpt
.
pt_render
()
self
.
assertEqual
(
result
.
startswith
(
unicode
(
'<div></div>'
,
'iso-8859-15'
)),
False
)
class
ZopePageTemplateFileTests
(
ZopeTestCase
):
def
testPT_RenderWithAscii
(
self
):
...
...
@@ -273,6 +323,7 @@ def test_suite():
suite
.
addTests
(
unittest
.
makeSuite
(
ZPTUtilsTests
))
suite
.
addTests
(
unittest
.
makeSuite
(
ZPTMacros
))
suite
.
addTests
(
unittest
.
makeSuite
(
ZopePageTemplateFileTests
))
suite
.
addTests
(
unittest
.
makeSuite
(
ZPTUnicodeEncodingConflictResolution
))
return
suite
if
__name__
==
'__main__'
:
...
...
lib/python/Products/PageTemplates/unicodeconflictresolver.py
View file @
020a6d18
...
...
@@ -12,7 +12,10 @@
##############################################################################
import
sys
from
zope.interface
import
implements
from
zope.i18n.interfaces
import
IUserPreferredCharsets
from
Products.PageTemplates.interfaces
import
IUnicodeEncodingConflictResolver
default_encoding
=
sys
.
getdefaultencoding
()
...
...
@@ -47,10 +50,47 @@ class Z2UnicodeEncodingConflictResolver:
try
:
return
unicode
(
text
)
except
UnicodeDecodeError
:
encoding
=
getattr
(
context
,
'managment_page_charset'
,
default_encoding
)
encoding
=
getattr
(
context
,
'manag
e
ment_page_charset'
,
default_encoding
)
return
unicode
(
text
,
encoding
,
self
.
mode
)
class
PreferredCharsetResolver
:
""" A resolver that tries use the encoding information
from the HTTP_ACCEPT_CHARSET header.
"""
implements
(
IUnicodeEncodingConflictResolver
)
def
resolve
(
self
,
context
,
text
,
expression
):
request
=
context
.
REQUEST
charsets
=
getattr
(
request
,
'__zpt_available_charsets'
,
None
)
if
charsets
is
None
:
charsets
=
IUserPreferredCharsets
(
request
).
getPreferredCharsets
()
# add management_page_charset as one fallback
management_charset
=
getattr
(
context
,
'management_page_charset'
,
None
)
if
management_charset
:
charsets
.
append
(
management_charset
)
# add Python's default encoding as last fallback
charsets
.
append
(
default_encoding
)
# cache list of charsets
request
.
__zpt_available_charsets
=
charsets
for
enc
in
charsets
:
if
enc
==
'*'
:
continue
try
:
return
unicode
(
text
,
enc
)
except
UnicodeDecodeError
:
pass
return
text
StrictUnicodeEncodingConflictResolver
=
Z2UnicodeEncodingConflictResolver
(
'strict'
)
ReplacingUnicodeEncodingConflictResolver
=
Z2UnicodeEncodingConflictResolver
(
'replace'
)
IgnoringUnicodeEncodingConflictResolver
=
Z2UnicodeEncodingConflictResolver
(
'ignore'
)
PreferredCharsetResolver
=
PreferredCharsetResolver
()
Write
Preview
Markdown
is supported
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