Exemple #1
0
    def _lazyload(self, element):
        """Inject attributes needed by lazysizes to lazy load elements.
        For more information, see: https://afarkas.github.io/lazysizes
        """
        assert element.tag in ('img', 'iframe', 'blockquote')

        classes = element.attrib.get('class', '').split(' ')
        if 'lazyload' in classes:
            return  # this should never happen

        if element.tag == 'img':
            src = self._lazyload_img(element)
        elif element.tag == 'iframe':
            src = self._lazyload_iframe(element)
        elif element.tag == 'blockquote':
            if 'twitter-tweet' not in classes:
                return
            src = self._lazyload_tweet(element)
            classes.remove('twitter-tweet')

        if src is None:
            return  # something went wrong

        classes.append('lazyload')
        element.attrib['class'] = ' '.join(classes).strip()
        msg = '<{0}> tag with src="{1}" was processed for lazy loading'
        logger.debug(msg.format(element.tag, src))
Exemple #2
0
    def _lazyload(self, element):
        """Inject attributes needed by lazysizes to lazy load elements.
        For more information, see: https://afarkas.github.io/lazysizes
        """
        assert element.tag in ('img', 'iframe', 'blockquote')  # nosec

        classes = element.attrib.get('class', '').split(' ')
        if 'lazyload' in classes:
            return  # this should never happen

        if element.tag == 'img':
            src = self._lazyload_img(element)
        elif element.tag == 'iframe':
            src = self._lazyload_iframe(element)
        elif element.tag == 'blockquote':
            if 'twitter-tweet' not in classes:
                return  # not a tweet
            src = self._lazyload_tweet(element)
            if src is not None:
                classes.remove('twitter-tweet')

        if src is None:
            return  # something went wrong or lazy load not needed

        classes.append('lazyload')
        element.attrib['class'] = ' '.join(classes).strip()
        msg = u'<{0}> tag with src="{1}" was processed for lazy loading'
        logger.debug(msg.format(element.tag, src))
Exemple #3
0
    def _lazyload_tweet(element):
        """Process tweets for lazy loading. Twitter describes tweets
        using <blockquote> tags with a `twitter-tweet` class and loads
        a widget in a sibling <script> tag. To lazy load we need to add
        a `data-twitter` attribute and remove the widget.

        :param element: the HTML node to be processed
        :type element: instance of lxml.html.HtmlElement
        :returns: the URL of the tweet to be lazy loaded
        :rtype: str
        """
        assert element.tag == 'blockquote'  # nosec
        # processing a tweet is tricky and prone to errors
        # abort at any time if the user has modified the code
        element.attrib['data-twitter'] = 'twitter-tweet'
        # remove sibling <script> tag to avoid an useless request
        sibling = element.getnext()
        if sibling is None:
            return  # Twitter's embed code was somehow modified, abort

        widget = '//platform.twitter.com/widgets.js'
        if sibling.tag == 'script' and widget in sibling.attrib['src']:
            parent = element.getparent()
            parent.remove(sibling)
            logger.debug("Twitter's widget <script> tag removed")
        try:
            return element.find('a').attrib['href']
        except AttributeError:
            return  # Twitter's embed code was somehow modified, abort
    def _lazyload_tweet(self, element):
        """Process tweets for lazy loading. Twitter describes tweets
        using <blockquote> tags with a `twitter-tweet` class and loads
        a widget in a sibling <script> tag. To lazy load we need to add
        a `data-twitter` attribute and remove the widget.

        :param element: the HTML node to be processed
        :type element: instance of lxml.html.HtmlElement
        :returns: the URL of the tweet to be lazy loaded
        :rtype: str
        """
        assert element.tag == 'blockquote'
        # processing a tweet is tricky and prone to errors
        # abort at any time if the user has modified the code
        element.attrib['data-twitter'] = 'twitter-tweet'
        # remove sibling <script> tag to avoid an useless request
        sibling = element.getnext()
        if sibling is None:
            return  # Twitter's embed code was somehow modified, abort
        widget = '//platform.twitter.com/widgets.js'
        if sibling.tag == 'script' and widget in sibling.attrib['src']:
            parent = element.getparent()
            parent.remove(sibling)
            logger.debug("Twitter's widget <script> tag removed")
        try:
            return element.find('a').attrib['href']
        except AttributeError:
            return  # Twitter's embed code was somehow modified, abort
Exemple #5
0
    def _lazyload_tweet(self, element):
        """Process tweets for lazy loading. Twitter describes tweets
        using <blockquote> tags with a `twitter-tweet` class and loads
        a widget in a sibling <script> tag. To lazy load we need to add
        a `data-twitter` attribute and remove the widget.

        :param element: the HTML node to be processed
        :type element: instance of lxml.html.HtmlElement
        :returns: the URL of the tweet to be lazy loaded
        :rtype: str
        """
        assert element.tag == 'blockquote'
        element.attrib['data-twitter'] = 'twitter-tweet'
        # remove sibling <script> tag to avoid an useless request
        widget = '//platform.twitter.com/widgets.js'
        sibling = element.getnext()
        if sibling.tag == 'script' and widget in sibling.attrib['src']:
            parent = element.getparent()
            parent.remove(sibling)
            logger.debug("Twitter's widget <script> tag removed")
        return element.find('a').attrib['href']