diff --git a/image_explorer/image_explorer.py b/image_explorer/image_explorer.py index 0739446..02238d6 100644 --- a/image_explorer/image_explorer.py +++ b/image_explorer/image_explorer.py @@ -8,6 +8,8 @@ import textwrap from lxml import etree, html from urlparse import urljoin +from parsel import Selector + from django.conf import settings from xblock.core import XBlock @@ -155,11 +157,10 @@ def student_view_data(self, context=None): """ xmltree = etree.fromstring(self.data) - description = self._get_description(xmltree) + description = self._get_description(xmltree, absolute_urls=True) background = self._get_background(xmltree) background['src'] = self._replace_static_from_url(background['src']) - hotspots = self._get_hotspots(xmltree) - + hotspots = self._get_hotspots(xmltree, absolute_urls=True) return { 'description': description, 'background': background, @@ -279,24 +280,36 @@ def _make_url_absolute(self, url): lms_base = '{}://{}'.format(scheme, lms_base) return urljoin(lms_base, url) - def _inner_content(self, tag): + def _inner_content(self, tag, absolute_urls=False): """ Helper met """ if tag is not None: - return u''.join(html.tostring(e) for e in tag) + tag_content = u''.join(html.tostring(e) for e in tag) + if absolute_urls: + return self._change_relative_url_to_absolute(tag_content) + else: + return tag_content return None - def _get_description(self, xmltree): + def _get_description(self, xmltree, absolute_urls=False): """ Parse the XML to get the description information """ description = xmltree.find('description') if description is not None: - return self._inner_content(description) + description = self._inner_content(description, absolute_urls) + return description return None - def _get_hotspots(self, xmltree): + def _change_relative_url_to_absolute(self, text): + if text: + relative_urls = Selector(text=text).css('::attr(href),::attr(src)').extract() + for url in relative_urls: + text = text.replace(url, self._replace_static_from_url(url)) + return text + + def _get_hotspots(self, xmltree, absolute_urls=False): """ Parse the XML to get the hotspot information """ @@ -310,15 +323,14 @@ def _get_hotspots(self, xmltree): feedback.width = feedback_element.get('width') feedback.height = feedback_element.get('height') feedback.max_height = feedback_element.get('max-height') - feedback.header = self._inner_content(feedback_element.find('header')) - + feedback.header = self._inner_content(feedback_element.find('header'), absolute_urls) feedback.side = hotspot_element.get('side', 'auto') feedback.body = None body_element = feedback_element.find('body') if body_element is not None: feedback.type = 'text' - feedback.body = self._inner_content(body_element) + feedback.body = self._inner_content(body_element, absolute_urls) feedback.youtube = None youtube_element = feedback_element.find('youtube') diff --git a/requirements.txt b/requirements.txt index e407da7..70d9095 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ -e . -e git+https://github.com/edx/xblock-utils.git@v1.1.0#egg=xblock-utils==1.1.0 lxml==3.0.1 +parsel==1.2.0 diff --git a/tests/unit/test_image_explorer.py b/tests/unit/test_image_explorer.py index 4da1a27..b9b55b7 100644 --- a/tests/unit/test_image_explorer.py +++ b/tests/unit/test_image_explorer.py @@ -1,10 +1,14 @@ import unittest from lxml import etree +from parsel import Selector + +from django.test import override_settings from xblock.field_data import DictFieldData from image_explorer.image_explorer import ImageExplorerBlock -from ..utils import MockRuntime +from ..utils import MockRuntime, patch_static_replace_module + class TestImageExplorerBlock(unittest.TestCase): """ @@ -16,8 +20,11 @@ def setUp(self): """ super(TestImageExplorerBlock, self).setUp() self.runtime = MockRuntime() - self.image_url = 'http://example.com/test.jpg' - self.image_explorer_description = '
Test Descrption
' + patch_static_replace_module() + + self.processed_absolute_url = 'https://lms/a/dynamic/url' + self.image_url = '/static/test.jpg' + self.image_explorer_description = 'Test Descrption
Test Header
Test Body
+ +Test Body
+