Skip to content

Commit

Permalink
Handle case when image has no wrapper tag
Browse files Browse the repository at this point in the history
  • Loading branch information
cekk committed Oct 11, 2019
1 parent 113d1d3 commit db6980c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Changelog

- Add classifiers for Plone 5.
[agitator]

- Handle case when image has no wrapper tag.
[cekk]

1.0.1 (2018-10-19)
------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ class View(BrowserView):

def getImageTag(self):
scales = api.content.get_view(
name='images',
context=self.context,
request=self.request,
name='images', context=self.context, request=self.request
)
if not scales:
return ''
Expand Down Expand Up @@ -62,9 +60,11 @@ def fixText(self, text):
images = tree.xpath('//img')
for image in images:
paragraph = image.getparent()
# remove the image from its parent
etree.strip_elements(paragraph, 'img', with_tail=False)
pContainer = paragraph.getparent()
pIndex = pContainer.index(paragraph)
pContainer.insert(pIndex, image)
if paragraph is not None:
pContainer = paragraph.getparent()
if pContainer is not None:
# remove the image from its parent
etree.strip_elements(paragraph, 'img', with_tail=False)
pIndex = pContainer.index(paragraph)
pContainer.insert(pIndex, image)
return etree.tostring(tree, encoding='utf-8', method='html')
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
"""Viewlet tests for this package."""
from collective.facebook.instantarticles.browser.viewlets import (
FacebookInstantArticlesViewlet,
) # NOQA
from collective.facebook.instantarticles.interfaces import (
IInstantArticlesSettings,
) # NOQA
from collective.facebook.instantarticles.testing import (
COLLECTIVE_FACEBOOK_INSTANTARTICLES_INTEGRATION_TESTING,
) # NOQA
from plone import api

import unittest


class TestInstantArticleView(unittest.TestCase):
"""
Test support view
"""

layer = COLLECTIVE_FACEBOOK_INSTANTARTICLES_INTEGRATION_TESTING

def setUp(self):
"""Custom shared utility setup for tests."""
self.portal = self.layer['portal']
self.request = self.portal.REQUEST

def testFixText(self):
view = api.content.get_view(
name='instant_article', context=self.portal, request=self.request
)
img_tag = '<img src="something" />'
img_inside_p = '<p><img src="something" /> Example.</p>'
self.assertEqual(
view.fixText(img_tag), '<div><img src="something"></div>'
)
self.assertEqual(
view.fixText(img_inside_p),
'<div><img src="something"><p> Example.</p></div>',
)

0 comments on commit db6980c

Please sign in to comment.