2011-05-25 17:54:46 +04:00
|
|
|
|
# coding: utf8
|
2012-03-22 02:19:27 +04:00
|
|
|
|
"""
|
|
|
|
|
weasyprint.html
|
|
|
|
|
---------------
|
2011-05-25 17:54:46 +04:00
|
|
|
|
|
2012-03-22 02:19:27 +04:00
|
|
|
|
Specific handling for some HTML elements, especially replaced elements.
|
2011-05-25 17:54:46 +04:00
|
|
|
|
|
2012-03-22 02:19:27 +04:00
|
|
|
|
Replaced elements (eg. <img> elements) are rendered externally and
|
|
|
|
|
behave as an atomic opaque box in CSS. In general, they may or may not
|
|
|
|
|
have intrinsic dimensions. But the only replaced elements currently
|
|
|
|
|
supported in WeasyPrint are images with intrinsic dimensions.
|
2011-08-19 18:52:46 +04:00
|
|
|
|
|
2012-03-22 02:19:27 +04:00
|
|
|
|
:copyright: Copyright 2011-2012 Simon Sapin and contributors, see AUTHORS.
|
|
|
|
|
:license: BSD, see LICENSE for details.
|
2011-08-19 18:52:46 +04:00
|
|
|
|
|
2011-05-25 17:54:46 +04:00
|
|
|
|
"""
|
|
|
|
|
|
2012-02-17 21:49:58 +04:00
|
|
|
|
from __future__ import division, unicode_literals
|
2012-03-25 03:39:41 +04:00
|
|
|
|
import os.path
|
|
|
|
|
import logging
|
2011-08-10 16:51:18 +04:00
|
|
|
|
|
2011-10-14 21:07:13 +04:00
|
|
|
|
from .formatting_structure import boxes
|
2012-05-22 19:44:16 +04:00
|
|
|
|
from .urls import get_url_attribute
|
2012-08-03 17:20:22 +04:00
|
|
|
|
from .compat import xrange, urljoin
|
2012-03-25 03:39:41 +04:00
|
|
|
|
from .logger import LOGGER
|
|
|
|
|
from . import CSS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# XXX temporarily disable logging for user-agent stylesheet
|
|
|
|
|
level = LOGGER.level
|
|
|
|
|
LOGGER.setLevel(logging.ERROR)
|
|
|
|
|
|
|
|
|
|
HTML5_UA_STYLESHEET = CSS(
|
2012-07-27 01:31:03 +04:00
|
|
|
|
filename=os.path.join(os.path.dirname(__file__), 'css', 'html5_ua.css'))
|
2012-03-25 03:39:41 +04:00
|
|
|
|
|
|
|
|
|
LOGGER.setLevel(level)
|
2011-08-10 16:51:18 +04:00
|
|
|
|
|
2011-05-25 17:54:46 +04:00
|
|
|
|
|
2011-08-25 14:48:00 +04:00
|
|
|
|
# Maps HTML tag names to function taking an HTML element and returning a Box.
|
|
|
|
|
HTML_HANDLERS = {}
|
2011-08-20 20:02:04 +04:00
|
|
|
|
|
2011-08-25 19:29:16 +04:00
|
|
|
|
|
2012-07-12 17:54:22 +04:00
|
|
|
|
def handle_element(element, box, get_image_from_uri):
|
2011-08-25 19:29:16 +04:00
|
|
|
|
"""Handle HTML elements that need special care.
|
|
|
|
|
|
2011-11-17 18:39:30 +04:00
|
|
|
|
:returns: a (possibly empty) list of boxes.
|
2011-08-25 19:29:16 +04:00
|
|
|
|
"""
|
2011-12-02 15:36:20 +04:00
|
|
|
|
if box.element_tag in HTML_HANDLERS:
|
2012-07-12 17:54:22 +04:00
|
|
|
|
return HTML_HANDLERS[element.tag](element, box, get_image_from_uri)
|
2011-08-25 19:29:16 +04:00
|
|
|
|
else:
|
2011-11-17 18:39:30 +04:00
|
|
|
|
return [box]
|
2011-08-20 20:02:04 +04:00
|
|
|
|
|
|
|
|
|
|
2011-08-25 14:48:00 +04:00
|
|
|
|
def handler(tag):
|
2011-09-09 01:02:17 +04:00
|
|
|
|
"""Return a decorator registering a function handling ``tag`` elements."""
|
2011-08-22 19:50:32 +04:00
|
|
|
|
def decorator(function):
|
2011-09-09 01:02:17 +04:00
|
|
|
|
"""Decorator registering a function handling ``tag`` elements."""
|
2011-08-25 14:48:00 +04:00
|
|
|
|
HTML_HANDLERS[tag] = function
|
2011-08-22 19:50:32 +04:00
|
|
|
|
return function
|
|
|
|
|
return decorator
|
2011-08-20 20:02:04 +04:00
|
|
|
|
|
|
|
|
|
|
2011-12-08 21:11:32 +04:00
|
|
|
|
def make_replaced_box(element, box, image):
|
|
|
|
|
"""Wrap an image in a replaced box.
|
2011-09-09 01:02:17 +04:00
|
|
|
|
|
|
|
|
|
That box is either block-level or inline-level, depending on what the
|
|
|
|
|
element should be.
|
|
|
|
|
|
2011-08-25 19:29:16 +04:00
|
|
|
|
"""
|
2012-03-14 16:34:08 +04:00
|
|
|
|
if box.style.display in ('block', 'list-item', 'table'):
|
2011-12-05 17:24:43 +04:00
|
|
|
|
type_ = boxes.BlockReplacedBox
|
2011-08-25 14:48:00 +04:00
|
|
|
|
else:
|
2012-03-14 16:34:08 +04:00
|
|
|
|
# TODO: support images with 'display: table-cell'?
|
2011-12-05 17:24:43 +04:00
|
|
|
|
type_ = boxes.InlineReplacedBox
|
2011-12-08 21:11:32 +04:00
|
|
|
|
return type_(element.tag, element.sourceline, box.style, image)
|
2011-08-25 14:48:00 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@handler('img')
|
2012-07-12 17:54:22 +04:00
|
|
|
|
def handle_img(element, box, get_image_from_uri):
|
2012-02-29 20:38:30 +04:00
|
|
|
|
"""Handle ``<img>`` elements, return either an image or the alt-text.
|
2011-09-09 01:02:17 +04:00
|
|
|
|
|
|
|
|
|
See: http://www.w3.org/TR/html5/embedded-content-1.html#the-img-element
|
2011-08-25 19:29:16 +04:00
|
|
|
|
|
2011-08-22 19:55:30 +04:00
|
|
|
|
"""
|
2011-12-02 15:36:20 +04:00
|
|
|
|
src = get_url_attribute(element, 'src')
|
|
|
|
|
alt = element.get('alt')
|
2011-08-25 19:29:16 +04:00
|
|
|
|
if src:
|
2012-07-12 17:54:22 +04:00
|
|
|
|
image = get_image_from_uri(src)
|
2011-12-08 21:11:32 +04:00
|
|
|
|
if image is not None:
|
|
|
|
|
return [make_replaced_box(element, box, image)]
|
2011-08-26 00:16:04 +04:00
|
|
|
|
else:
|
2011-08-25 19:29:16 +04:00
|
|
|
|
# Invalid image, use the alt-text.
|
|
|
|
|
if alt:
|
2012-03-14 16:34:08 +04:00
|
|
|
|
return [box.copy_with_children(
|
|
|
|
|
[boxes.TextBox.anonymous_from(box, alt)])]
|
2011-08-25 19:29:16 +04:00
|
|
|
|
elif alt == '':
|
|
|
|
|
# The element represents nothing
|
2011-11-17 18:39:30 +04:00
|
|
|
|
return []
|
2011-08-25 19:29:16 +04:00
|
|
|
|
else:
|
|
|
|
|
assert alt is None
|
|
|
|
|
# TODO: find some indicator that an image is missing.
|
|
|
|
|
# For now, just remove the image.
|
2011-11-17 18:39:30 +04:00
|
|
|
|
return []
|
2011-08-25 19:29:16 +04:00
|
|
|
|
else:
|
|
|
|
|
if alt:
|
2012-03-14 16:34:08 +04:00
|
|
|
|
return [box.copy_with_children(
|
|
|
|
|
[boxes.TextBox.anonymous_from(box, alt)])]
|
2011-08-25 19:29:16 +04:00
|
|
|
|
else:
|
2011-11-17 18:39:30 +04:00
|
|
|
|
return []
|
2011-05-25 17:54:46 +04:00
|
|
|
|
|
|
|
|
|
|
2012-02-29 20:38:30 +04:00
|
|
|
|
@handler('embed')
|
2012-07-12 17:54:22 +04:00
|
|
|
|
def handle_embed(element, box, get_image_from_uri):
|
2012-02-29 20:38:30 +04:00
|
|
|
|
"""Handle ``<embed>`` elements, return either an image or nothing.
|
|
|
|
|
|
|
|
|
|
See: http://www.w3.org/TR/html5/the-iframe-element.html#the-embed-element
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
src = get_url_attribute(element, 'src')
|
|
|
|
|
type_ = element.get('type', '').strip()
|
|
|
|
|
if src:
|
2012-07-12 17:54:22 +04:00
|
|
|
|
image = get_image_from_uri(src, type_)
|
2012-02-29 20:38:30 +04:00
|
|
|
|
if image is not None:
|
|
|
|
|
return [make_replaced_box(element, box, image)]
|
|
|
|
|
# No fallback.
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@handler('object')
|
2012-07-12 17:54:22 +04:00
|
|
|
|
def handle_object(element, box, get_image_from_uri):
|
2012-02-29 20:38:30 +04:00
|
|
|
|
"""Handle ``<object>`` elements, return either an image or the fallback
|
|
|
|
|
content.
|
|
|
|
|
|
|
|
|
|
See: http://www.w3.org/TR/html5/the-iframe-element.html#the-object-element
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
data = get_url_attribute(element, 'data')
|
|
|
|
|
type_ = element.get('type', '').strip()
|
|
|
|
|
if data:
|
2012-07-12 17:54:22 +04:00
|
|
|
|
image = get_image_from_uri(data, type_)
|
2012-02-29 20:38:30 +04:00
|
|
|
|
if image is not None:
|
|
|
|
|
return [make_replaced_box(element, box, image)]
|
|
|
|
|
# The element’s children are the fallback.
|
|
|
|
|
return [box]
|
|
|
|
|
|
|
|
|
|
|
2011-12-02 15:36:20 +04:00
|
|
|
|
def integer_attribute(element, box, name, minimum=1):
|
2011-11-15 14:44:28 +04:00
|
|
|
|
"""Read an integer attribute from the HTML element and set it on the box.
|
|
|
|
|
|
|
|
|
|
"""
|
2011-12-02 15:36:20 +04:00
|
|
|
|
value = element.get(name, '').strip()
|
2012-02-29 20:38:30 +04:00
|
|
|
|
if value:
|
|
|
|
|
try:
|
|
|
|
|
value = int(value)
|
|
|
|
|
except ValueError:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
if value >= minimum:
|
|
|
|
|
setattr(box, name, value)
|
2011-11-15 14:44:28 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@handler('colgroup')
|
2012-07-12 17:54:22 +04:00
|
|
|
|
def handle_colgroup(element, box, _get_image_from_uri):
|
2011-11-15 14:44:28 +04:00
|
|
|
|
"""Handle the ``span`` attribute."""
|
|
|
|
|
if isinstance(box, boxes.TableColumnGroupBox):
|
2011-12-02 15:36:20 +04:00
|
|
|
|
if any(child.tag == 'col' for child in element):
|
2011-11-15 14:44:28 +04:00
|
|
|
|
box.span = None # sum of the children’s spans
|
|
|
|
|
else:
|
2011-12-02 15:36:20 +04:00
|
|
|
|
integer_attribute(element, box, 'span')
|
2012-02-29 20:38:30 +04:00
|
|
|
|
box.children = (
|
|
|
|
|
boxes.TableColumnBox.anonymous_from(box, [])
|
|
|
|
|
for _i in xrange(box.span))
|
2011-11-17 18:39:30 +04:00
|
|
|
|
return [box]
|
2011-11-15 14:44:28 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@handler('col')
|
2012-07-12 17:54:22 +04:00
|
|
|
|
def handle_col(element, box, _get_image_from_uri):
|
2011-11-15 14:44:28 +04:00
|
|
|
|
"""Handle the ``span`` attribute."""
|
|
|
|
|
if isinstance(box, boxes.TableColumnBox):
|
2011-12-02 15:36:20 +04:00
|
|
|
|
integer_attribute(element, box, 'span')
|
2011-11-17 18:39:30 +04:00
|
|
|
|
if box.span > 1:
|
|
|
|
|
# Generate multiple boxes
|
|
|
|
|
# http://lists.w3.org/Archives/Public/www-style/2011Nov/0293.html
|
2011-12-26 15:47:26 +04:00
|
|
|
|
return [box.copy() for _i in xrange(box.span)]
|
2011-11-17 18:39:30 +04:00
|
|
|
|
return [box]
|
2011-11-15 14:44:28 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@handler('th')
|
|
|
|
|
@handler('td')
|
2012-07-12 17:54:22 +04:00
|
|
|
|
def handle_td(element, box, _get_image_from_uri):
|
2011-11-15 14:44:28 +04:00
|
|
|
|
"""Handle the ``colspan``, ``rowspan`` attributes."""
|
|
|
|
|
if isinstance(box, boxes.TableCellBox):
|
|
|
|
|
# HTML 4.01 gives special meaning to colspan=0
|
|
|
|
|
# http://www.w3.org/TR/html401/struct/tables.html#adef-rowspan
|
|
|
|
|
# but HTML 5 removed it
|
|
|
|
|
# http://www.w3.org/TR/html5/tabular-data.html#attr-tdth-colspan
|
|
|
|
|
# rowspan=0 is still there though.
|
2011-12-02 15:36:20 +04:00
|
|
|
|
integer_attribute(element, box, 'colspan')
|
|
|
|
|
integer_attribute(element, box, 'rowspan', minimum=0)
|
2011-11-17 18:39:30 +04:00
|
|
|
|
return [box]
|
2012-08-03 17:20:22 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_base_url(html_document, fallback_base_url):
|
|
|
|
|
"""Return the base URL for the document.
|
|
|
|
|
|
|
|
|
|
See http://www.w3.org/TR/html5/urls.html#document-base-url
|
|
|
|
|
|
|
|
|
|
"""
|
2012-08-17 19:37:33 +04:00
|
|
|
|
first_base_element = next(iter(html_document.iter('base')), None)
|
2012-08-03 17:20:22 +04:00
|
|
|
|
if first_base_element is not None:
|
|
|
|
|
href = first_base_element.get('href', '').strip()
|
|
|
|
|
if href:
|
|
|
|
|
return urljoin(fallback_base_url, href)
|
|
|
|
|
return fallback_base_url
|