Commit 33a37210 authored by Andreas Jung's avatar Andreas Jung

the PUT factory now tries to detect the encoding of uploaded contenthe PUT...

the PUT factory now tries to detect the encoding of uploaded contenthe PUT factory now tries to detect the encoding of uploaded contentt
parent c81a09c8
...@@ -41,7 +41,8 @@ from Products.PageTemplates.PageTemplateFile import guess_type ...@@ -41,7 +41,8 @@ from Products.PageTemplates.PageTemplateFile import guess_type
from Products.PageTemplates.Expressions import SecureModuleImporter from Products.PageTemplates.Expressions import SecureModuleImporter
# regular expression to extract the encoding from the XML preamble # regular expression to extract the encoding from the XML preamble
encoding_reg = re.compile('<\?xml.*?encoding="(.*?)".*?\?>', re.M) encoding_reg = re.compile(r'<\?xml.*?encoding="(.*?)".*?\?>', re.M)
charset_reg = re.compile(r'<meta.*?charset=(?P<charset>[\w\-]*).*?>', (re.I|re.M|re.S))
preferred_encodings = ['utf-8', 'iso-8859-15'] preferred_encodings = ['utf-8', 'iso-8859-15']
if os.environ.has_key('ZPT_PREFERRED_ENCODING'): if os.environ.has_key('ZPT_PREFERRED_ENCODING'):
...@@ -49,10 +50,19 @@ if os.environ.has_key('ZPT_PREFERRED_ENCODING'): ...@@ -49,10 +50,19 @@ if os.environ.has_key('ZPT_PREFERRED_ENCODING'):
def sniffEncoding(text, default_encoding='utf-8'): def sniffEncoding(text, default_encoding='utf-8'):
"""Try to determine the encoding from html or xml""" """Try to determine the encoding from html or xml"""
# sniff into the XML preamble
if text.startswith('<?xml'): if text.startswith('<?xml'):
mo = encoding_reg.search(text) mo = encoding_reg.search(text)
if mo: if mo:
return mo.group(1) return mo.group(1)
# sniff for <meta http-equiv="content-type" ...> header
else:
mo = charset_reg.search(text)
if mo:
return mo.groupdict()['charset']
return default_encoding return default_encoding
class Src(Acquisition.Explicit): class Src(Acquisition.Explicit):
...@@ -79,7 +89,7 @@ class ZopePageTemplate(Script, PageTemplate, Historical, Cacheable, ...@@ -79,7 +89,7 @@ class ZopePageTemplate(Script, PageTemplate, Historical, Cacheable,
func_defaults = None func_defaults = None
func_code = FuncCode((), 0) func_code = FuncCode((), 0)
strict = False strict = True
_default_bindings = {'name_subpath': 'traverse_subpath'} _default_bindings = {'name_subpath': 'traverse_subpath'}
_default_content_fn = os.path.join(package_home(globals()), _default_content_fn = os.path.join(package_home(globals()),
...@@ -109,7 +119,7 @@ class ZopePageTemplate(Script, PageTemplate, Historical, Cacheable, ...@@ -109,7 +119,7 @@ class ZopePageTemplate(Script, PageTemplate, Historical, Cacheable,
'read', 'ZScriptHTML_tryForm') 'read', 'ZScriptHTML_tryForm')
def __init__(self, id, text=None, content_type=None, encoding='utf-8', def __init__(self, id, text=None, content_type=None, encoding='utf-8',
strict=False): strict=True):
self.id = id self.id = id
self.expand = 0 self.expand = 0
self.strict = strict self.strict = strict
...@@ -294,7 +304,9 @@ class ZopePageTemplate(Script, PageTemplate, Historical, Cacheable, ...@@ -294,7 +304,9 @@ class ZopePageTemplate(Script, PageTemplate, Historical, Cacheable,
self.dav__simpleifhandler(REQUEST, RESPONSE, refresh=1) self.dav__simpleifhandler(REQUEST, RESPONSE, refresh=1)
text = REQUEST.get('BODY', '') text = REQUEST.get('BODY', '')
content_type = guess_type('', text) content_type = guess_type('', text)
self.pt_edit(text, content_type, self.output_encoding) encoding = sniffEncoding(text, self.output_encoding)
self.output_encoding = encoding
self.pt_edit(text, content_type, encoding)
RESPONSE.setStatus(204) RESPONSE.setStatus(204)
return RESPONSE return RESPONSE
......
...@@ -93,13 +93,6 @@ class ZPTRegressions(unittest.TestCase): ...@@ -93,13 +93,6 @@ class ZPTRegressions(unittest.TestCase):
pt = self.app.pt1 pt = self.app.pt1
self.assertEqual(pt.document_src(), self.text) self.assertEqual(pt.document_src(), self.text)
def test_BBB_for_strict_attribute(self):
# Collector 2213: old templates don't have 'strict' attribute.
from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
zpt = ZopePageTemplate('issue_2213')
del zpt.strict # simulate old templates
self.assertEqual(zpt.strict, False)
class ZPTMacros(zope.component.testing.PlacelessSetup, unittest.TestCase): class ZPTMacros(zope.component.testing.PlacelessSetup, unittest.TestCase):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment