~ubuntu-branches/ubuntu/oneiric/lxml/oneiric

« back to all changes in this revision

Viewing changes to src/lxml/html/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-08-27 09:09:23 UTC
  • mfrom: (1.3.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090827090923-fwhvka191ir73s3x
Tags: 2.2.2-1
* New upstream version. Closes: #525961.
  - Includes html5parser. Closes: #521714.

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
    basestring = (str, bytes)
41
41
 
42
42
def __fix_docstring(s):
 
43
    if not s:
 
44
        return s
43
45
    import sys
44
46
    if sys.version_info[0] >= 3:
45
47
        sub = re.compile(r"^(\s*)u'", re.M).sub
51
53
    'document_fromstring', 'fragment_fromstring', 'fragments_fromstring', 'fromstring',
52
54
    'tostring', 'Element', 'defs', 'open_in_browser', 'submit_form',
53
55
    'find_rel_links', 'find_class', 'make_links_absolute',
54
 
    'resolve_base_href', 'iterlinks', 'rewrite_links', 'open_in_browser']
 
56
    'resolve_base_href', 'iterlinks', 'rewrite_links', 'open_in_browser', 'parse']
55
57
 
56
58
XHTML_NAMESPACE = "http://www.w3.org/1999/xhtml"
57
59
 
65
67
_class_xpath = etree.XPath("descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), concat(' ', $class_name, ' '))]")
66
68
_id_xpath = etree.XPath("descendant-or-self::*[@id=$id]")
67
69
_collect_string_content = etree.XPath("string()")
68
 
_css_url_re = re.compile(r'url\((.*?)\)', re.I)
 
70
_css_url_re = re.compile(r'url\(('+'["][^"]*["]|'+"['][^']*[']|"+r'[^)]*)\)', re.I)
69
71
_css_import_re = re.compile(r'@import "(.*?)"')
70
72
_label_xpath = etree.XPath("//label[@for=$id]|//x:label[@for=$id]",
71
73
                           namespaces={'x':XHTML_NAMESPACE})
72
74
_archive_re = re.compile(r'[^ ]+')
73
75
 
 
76
def _unquote_match(s, pos):
 
77
    if s[:1] == '"' and s[-1:] == '"' or s[:1] == "'" and s[-1:] == "'":
 
78
        return s[1:-1], pos+1
 
79
    else:
 
80
        return s,pos
 
81
 
74
82
def _transform_result(typ, result):
75
83
    """Convert the result back into the input type.
76
84
    """
340
348
                    yield (el, 'value', el.get('value'), 0)
341
349
            if tag == 'style' and el.text:
342
350
                for match in _css_url_re.finditer(el.text):
343
 
                    yield (el, None, match.group(1), match.start(1))
 
351
                    url, start = _unquote_match(match.group(1), match.start(1))
 
352
                    yield (el, None, url, start)
344
353
                for match in _css_import_re.finditer(el.text):
345
354
                    yield (el, None, match.group(1), match.start(1))
346
355
            if 'style' in attribs:
347
356
                for match in _css_url_re.finditer(attribs['style']):
348
 
                    yield (el, 'style', match.group(1), match.start(1))
 
357
                    url, start = _unquote_match(match.group(1), match.start(1))
 
358
                    yield (el, 'style', url, start)
349
359
 
350
360
    def rewrite_links(self, link_repl_func, resolve_base_href=True,
351
361
                      base_href=None):
711
721
            return self.get('name')
712
722
        elif self.get('id'):
713
723
            return '#' + self.get('id')
714
 
        forms = self.body.findall('form')
 
724
        forms = list(self.body.iter('form'))
715
725
        if not forms:
716
 
            forms = self.body.findall('{%s}form' % XHTML_NAMESPACE)
 
726
            forms = list(self.body.iter('{%s}form' % XHTML_NAMESPACE))
717
727
        return str(forms.index(self))
718
728
 
719
729
    def form_values(self):
1369
1379
 
1370
1380
# This isn't a general match, but it's a match for what libxml2
1371
1381
# specifically serialises:
1372
 
__replace_meta_content_type = re.compile(
1373
 
    r'<meta http-equiv="Content-Type".*?>').sub
 
1382
__str_replace_meta_content_type = re.compile(
 
1383
    r'<meta http-equiv="Content-Type"[^>]*>').sub
 
1384
__bytes_replace_meta_content_type = re.compile(
 
1385
    r'<meta http-equiv="Content-Type"[^>]*>'.encode('ASCII')).sub
1374
1386
 
1375
1387
def tostring(doc, pretty_print=False, include_meta_content_type=False,
1376
1388
             encoding=None, method="html"):
1377
1389
    """Return an HTML string representation of the document.
1378
 
 
1379
 
    Note: the 'include_meta_content_type' argument exists purely for
1380
 
    compatibility and does not serve any purpose.
 
1390
 
 
1391
    Note: if include_meta_content_type is true this will create a
 
1392
    ``<meta http-equiv="Content-Type" ...>`` tag in the head;
 
1393
    regardless of the value of include_meta_content_type any existing
 
1394
    ``<meta http-equiv="Content-Type" ...>`` tag will be removed
1381
1395
 
1382
1396
    The ``encoding`` argument controls the output encoding (defauts to
1383
1397
    ASCII, with &#...; character references for any characters outside
1411
1425
    html = etree.tostring(doc, method=method, pretty_print=pretty_print,
1412
1426
                          encoding=encoding)
1413
1427
    if not include_meta_content_type:
1414
 
        html = __replace_meta_content_type('', html)
 
1428
        if isinstance(html, str):
 
1429
            html = __str_replace_meta_content_type('', html)
 
1430
        else:
 
1431
            html = __bytes_replace_meta_content_type(bytes(), html)
1415
1432
    return html
1416
1433
 
1417
1434
tostring.__doc__ = __fix_docstring(tostring.__doc__)