Пример #1
0
 def test_urlbase(self):
     testdata =[
         {'url'  : 'http://example.com',
          'base' : 'http://example.com/',
          },
         {'url'  : 'http://example.com/',
          'base' : 'http://example.com/',
          },
         {'url'  : 'http://example.com/foo.html',
          'base' : 'http://example.com/',
          },
         {'url'  : 'http://example.com/baz/something.html',
          'base' : 'http://example.com/baz/',
          },
         {'url'  : 'http://example.com/baz/index.html',
          'base' : 'http://example.com/baz/',
          },
         {'url'  : 'http://example.com/baz/',
          'base' : 'http://example.com/baz/',
          },
         {'url'  : 'http://example.com/baz/x.html?a=2&b=3',
          'base' : 'http://example.com/baz/',
          },
         {'url'  : 'http://example.com/baz/x.html#hey',
          'base' : 'http://example.com/baz/',
          },
         ]
     from mobilize.util import urlbase
     for ii, td in enumerate(testdata):
         actual = urlbase(td['url'])
         msg = 'e: "%s", a; "%s" [%d]' % (td['base'], actual, ii)
         self.assertSequenceEqual(td['base'], actual, msg)
Пример #2
0
def _link_converter(attribute, desktop_url):
    '''
    Create a function that will convert link attributes to absolute desktop urls

    This utility creates and returns a function that can be applied to
    an element, to convert URL attribute which is relative to a full
    absolute URL on the desktop site.  Examples include the 'src'
    attribute of an IMG tag, or the 'href' attribute of an A tag.

    If the value of the URL attribute is already absolute, or starts
    with mobilize.util.STATIC_URL, that value will be unchanged.

    @param attribute   : The name of the element's attribute to inspect, and possibly change
    @type  attribute   : str

    @param desktop_url : The URL of the current page's corresponding desktop version
    @type  desktop_url : str

    @return            : link converter
    @rtype             : function

    '''
    from mobilize.util import urlbase
    from urllib.parse import urlparse
    from mobilize.util import STATIC_URL
    def is_desktop_relative(src):
        if src.startswith(STATIC_URL):
            return False
        for protocol in _protocols:
            if src.startswith(protocol + '://'):
                return False
        return True
    parsed = urlparse(desktop_url)
    desktop_root_url = '%s://%s' % (parsed.scheme, parsed.netloc)
    base_url = urlbase(desktop_url)
    def convert(elem):
        if attribute in elem.attrib and '' != elem.attrib[attribute]:
            if is_desktop_relative(elem.attrib[attribute]):
                if elem.attrib[attribute].startswith('/'):
                    elem.attrib[attribute] = desktop_root_url + elem.attrib[attribute]
                else:
                    elem.attrib[attribute] = base_url + elem.attrib[attribute]
    return convert