Skip to content

Commit

Permalink
Fix unicode parsing error with Python 3 (#85)
Browse files Browse the repository at this point in the history
* Fix unicode parsing error with Python 3
  • Loading branch information
agitator committed May 4, 2021
1 parent eb6331c commit 67b1fdf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -4,6 +4,9 @@ Changelog
4.3.3 (unreleased)
------------------

- Fix unicode parsing error on Python 3, resulting in empty mosaic page (`#62 https://github.com/plone/plone.app.mosaic/issues/480`_).
[agitator]

- Update test setup
[ksuess]

Expand Down
38 changes: 27 additions & 11 deletions plone/app/blocks/transform.py
Expand Up @@ -11,9 +11,13 @@
from repoze.xmliter.utils import getHTMLSerializer
from zope.interface import implementer

import six
import re
import logging


logger = logging.getLogger(__name__)

@implementer(ITransform)
class DisableParsing(object):
"""A no-op transform which sets flags to stop plone.app.blocks
Expand Down Expand Up @@ -85,19 +89,31 @@ def transformIterable(self, result, encoding):
try:
# Fix layouts with CR[+LF] line endings not to lose their heads
# (this has been seen with downloaded themes with CR[+LF] endings)
iterable = [
re.sub('
', '\n', re.sub('
\n', '\n', safe_unicode(item))) # noqa
for item in result if item]
result = getHTMLSerializer(
iterable, pretty_print=self.pretty_print, encoding=encoding)
# Fix XHTML layouts with where etree.tostring breaks <![CDATA[
if any(['<![CDATA[' in item for item in iterable]):
result.serializer = html.tostring

if six.PY2:
iterable = [
re.sub('&#13;', '\n', re.sub('&#13;\n', '\n', safe_unicode(item))) # noqa
for item in result if item]
result = getHTMLSerializer(
iterable, pretty_print=self.pretty_print, encoding=encoding)
# Fix XHTML layouts with where etree.tostring breaks <![CDATA[
if any(['<![CDATA[' in item for item in iterable]):
result.serializer = html.tostring
else:
iterable = [
re.sub('&#13;'.encode('utf-8'), '\n'.encode('utf-8'), re.sub('&#13;\n'.encode('utf-8'), '\n'.encode('utf-8'), item)) # noqa
for item in result if item]
result = getHTMLSerializer(
iterable, pretty_print=self.pretty_print, encoding=encoding)
# Fix XHTML layouts with where etree.tostring breaks <![CDATA[
if any([b'<![CDATA[' in item for item in iterable]):
result.serializer = html.tostring

self.request['plone.app.blocks.enabled'] = True
return result
except (AttributeError, TypeError, etree.ParseError):
return None

except (AttributeError, TypeError, etree.ParseError) as e:
logger.error(e)
return None

@implementer(ITransform)
class MergePanels(object):
Expand Down

0 comments on commit 67b1fdf

Please sign in to comment.