def _parse_base(html_content, base_url=BASE_URL): document = FakeHTML(string=html_content, base_url=base_url) counter_style = CounterStyle() style_for = get_all_computed_styles(document, counter_style=counter_style) get_image_from_uri = functools.partial(images.get_image_from_uri, {}, document.url_fetcher, False) target_collector = TargetCollector() return (document.etree_element, style_for, get_image_from_uri, base_url, target_collector, counter_style)
def test_font_size(parent_css, parent_size, child_css, child_size): document = FakeHTML(string='<p>a<span>b') style_for = get_all_computed_styles(document, user_stylesheets=[CSS( string='p{font-size:%s}span{font-size:%s}' % (parent_css, child_css))]) _head, body = document.etree_element p, = body span, = p assert isclose(style_for(p)['font_size'], parent_size) assert isclose(style_for(span)['font_size'], child_size)
def __init__(self, raw): self._input = raw if isinstance(raw, six.string_types): document = HTML(string=raw) elif isinstance(raw, (BytesIO, StringIO)): document = HTML(raw) else: raise NotImplementedError return self._root = document.root_element self._style_for = get_all_computed_styles(document)
def test_page_style(page_type, top, right, bottom, left): document = FakeHTML(string=''' <style> @page { margin: 3px } @page name { margin-left: 15px; margin-top: 5px } @page :nth(3) { margin-bottom: 1px } @page :nth(5n+4) { margin-bottom: 2px } @page :first { margin-top: 20px } @page :right { margin-right: 10px; margin-top: 10px } @page :left { margin-left: 10px; margin-top: 10px } </style> ''') style_for = get_all_computed_styles(document) # Force the generation of the style for this page type as it's generally # only done during the rendering. set_page_type_computed_styles(page_type, document, style_for) style = style_for(page_type) assert style['margin_top'] == (top, 'px') assert style['margin_right'] == (right, 'px') assert style['margin_bottom'] == (bottom, 'px') assert style['margin_left'] == (left, 'px')
def test_annotate_document(): document = FakeHTML(resource_filename('doc1.html')) document._ua_stylesheets = lambda: [CSS(resource_filename('mini_ua.css'))] style_for = get_all_computed_styles( document, user_stylesheets=[CSS(resource_filename('user.css'))]) # Element objects behave as lists of their children _head, body = document.etree_element h1, p, ul, div = body li_0, _li_1 = ul a, = li_0 span1, = div span2, = span1 h1 = style_for(h1) p = style_for(p) ul = style_for(ul) li_0 = style_for(li_0) div = style_for(div) after = style_for(a, 'after') a = style_for(a) span1 = style_for(span1) span2 = style_for(span2) assert h1['background_image'] == (( 'url', path2url(resource_filename('logo_small.png'))), ) assert h1['font_weight'] == 700 assert h1['font_size'] == 40 # 2em # x-large * initial = 3/2 * 16 = 24 assert p['margin_top'] == (24, 'px') assert p['margin_right'] == (0, 'px') assert p['margin_bottom'] == (24, 'px') assert p['margin_left'] == (0, 'px') assert p['background_color'] == 'currentColor' # 2em * 1.25ex = 2 * 20 * 1.25 * 0.8 = 40 # 2.5ex * 1.25ex = 2.5 * 0.8 * 20 * 1.25 * 0.8 = 40 # TODO: ex unit doesn't work with @font-face fonts, see computed_values.py # assert ul['margin_top'] == (40, 'px') # assert ul['margin_right'] == (40, 'px') # assert ul['margin_bottom'] == (40, 'px') # assert ul['margin_left'] == (40, 'px') assert ul['font_weight'] == 400 # thick = 5px, 0.25 inches = 96*.25 = 24px assert ul['border_top_width'] == 0 assert ul['border_right_width'] == 5 assert ul['border_bottom_width'] == 0 assert ul['border_left_width'] == 24 assert li_0['font_weight'] == 700 assert li_0['font_size'] == 8 # 6pt assert li_0['margin_top'] == (16, 'px') # 2em assert li_0['margin_right'] == (0, 'px') assert li_0['margin_bottom'] == (16, 'px') assert li_0['margin_left'] == (32, 'px') # 4em assert a['text_decoration_line'] == {'underline'} assert a['font_weight'] == 900 assert a['font_size'] == 24 # 300% of 8px assert a['padding_top'] == (1, 'px') assert a['padding_right'] == (2, 'px') assert a['padding_bottom'] == (3, 'px') assert a['padding_left'] == (4, 'px') assert a['border_top_width'] == 42 assert a['border_bottom_width'] == 42 assert a['color'] == (1, 0, 0, 1) assert a['border_top_color'] == 'currentColor' assert div['font_size'] == 40 # 2 * 20px assert span1['width'] == (160, 'px') # 10 * 16px (root default is 16px) assert span1['height'] == (400, 'px') # 10 * (2 * 20px) assert span2['font_size'] == 32 # The href attr should be as in the source, not made absolute. assert after['content'] == (('string', ' ['), ('string', 'home.html'), ('string', ']')) assert after['background_color'] == (1, 0, 0, 1) assert after['border_top_width'] == 42 assert after['border_bottom_width'] == 3
def test_page(): document = FakeHTML(resource_filename('doc1.html')) style_for = get_all_computed_styles(document, user_stylesheets=[ CSS(string=''' html { color: red } @page { margin: 10px } @page :right { color: blue; margin-bottom: 12pt; font-size: 20px; @top-left { width: 10em } @top-right { font-size: 10px} } ''') ]) page_type = PageType(side='left', first=True, blank=False, index=0, name='') set_page_type_computed_styles(page_type, document, style_for) style = style_for(page_type) assert style['margin_top'] == (5, 'px') assert style['margin_left'] == (10, 'px') assert style['margin_bottom'] == (10, 'px') assert style['color'] == (1, 0, 0, 1) # red, inherited from html page_type = PageType(side='right', first=True, blank=False, index=0, name='') set_page_type_computed_styles(page_type, document, style_for) style = style_for(page_type) assert style['margin_top'] == (5, 'px') assert style['margin_left'] == (10, 'px') assert style['margin_bottom'] == (16, 'px') assert style['color'] == (0, 0, 1, 1) # blue page_type = PageType(side='left', first=False, blank=False, index=1, name='') set_page_type_computed_styles(page_type, document, style_for) style = style_for(page_type) assert style['margin_top'] == (10, 'px') assert style['margin_left'] == (10, 'px') assert style['margin_bottom'] == (10, 'px') assert style['color'] == (1, 0, 0, 1) # red, inherited from html page_type = PageType(side='right', first=False, blank=False, index=1, name='') set_page_type_computed_styles(page_type, document, style_for) style = style_for(page_type) assert style['margin_top'] == (10, 'px') assert style['margin_left'] == (10, 'px') assert style['margin_bottom'] == (16, 'px') assert style['color'] == (0, 0, 1, 1) # blue page_type = PageType(side='left', first=True, blank=False, index=0, name='') set_page_type_computed_styles(page_type, document, style_for) style = style_for(page_type, '@top-left') assert style is None page_type = PageType(side='right', first=True, blank=False, index=0, name='') set_page_type_computed_styles(page_type, document, style_for) style = style_for(page_type, '@top-left') assert style['font_size'] == 20 # inherited from @page assert style['width'] == (200, 'px') page_type = PageType(side='right', first=True, blank=False, index=0, name='') set_page_type_computed_styles(page_type, document, style_for) style = style_for(page_type, '@top-right') assert style['font_size'] == 10