예제 #1
0
def test_remove_img_style_size():
    with open(
            os.path.join(file_directory, "extra",
                         "test_remove_img_style_size.html")) as fn:
        root = etree.HTML(fn.read())
        has_thumbinner_width = []
        for n in range(3):
            xpath = '//*[@id="container_{}"]'.format(n)
            node = root.xpath(xpath)[0]
            assert "col-" not in node.attrib["class"]
            thumbinner = node.xpath('.//*[@class="thumbinner"]')[0]
            has_thumbinner_width.append(False)
            if utils.get_node_style(thumbinner).get("width") is not None:
                has_thumbinner_width[n] = True
        images.remove_img_style_size(root)
        for n in range(3):
            node = root.xpath('//*[@id="container_{}"]'.format(n))[0]
            if has_thumbinner_width[n]:
                thumbinner = node.xpath('.//*[@class="thumbinner"]')[0]
                assert utils.get_node_style(thumbinner).get("width") is None
            img = node.xpath(".//img")[0]
            assert utils.get_node_style(img).get("width") is None
            assert node.attrib.get("width") is None
            assert img.attrib.get("width") is None
            assert "col-" in node.attrib["class"]
예제 #2
0
def test_fix_image_widths_tmulti():
    with open(
            os.path.join(file_directory, "extra",
                         "test_remove_img_style_size.html")) as fn:
        root = etree.HTML(fn.read())
        img_container = root.xpath('//*[@id="container_3"]')[0]
        thumbinner = img_container.xpath(
            './/*[contains(@class, "thumbinner")]')[0]
        assert utils.get_node_style(thumbinner).get("max-width") is not None
        images.fix_img_style_size_tmulti(root)
        assert utils.get_node_style(thumbinner).get("max-width") is None
예제 #3
0
def test_fix_election_diagram():
    with open(
            os.path.join(file_directory, "extra",
                         "test_fix_election_diagram.html")) as fn:
        root = etree.HTML(fn.read())
        node = root.xpath('//div[@boxid="1403"]')[0]
        styles = utils.get_node_style(node)
        assert "width" not in styles
        special.fix_election_charts(root)
        styles = utils.get_node_style(node)
        assert "width" in styles
        assert styles["width"] == "100%"
예제 #4
0
def fix_election_charts(root):
    for node in root.xpath(
            '//div[@class="float-right"]/div[contains(@style, "relative;")]/div'
    ):
        styles = utils.get_node_style(node)
        if "width" not in styles:
            utils.add_node_style(node, "width", "100%")
예제 #5
0
def fix_abspos_overlays(root):
    for container in root.xpath(
        ('//*[contains(@style, "position")' ' and contains(@style, "relative")]')
    ):
        w = utils.get_node_width(container, target_unit="px")
        h = utils.get_node_height(container, target_unit="px")
        if not (w and h):
            img = container.xpath(".//img")
            if not img:
                continue
            img = img[0]
            w, h = get_img_size(img)
        for node in container.xpath(
            ('.//*[contains(@style, "position")' ' and contains(@style, "absolute")]')
        ):
            style = utils.get_node_style(node)
            left = style.get("left")
            top = style.get("top")
            for attr in ["left", "top"]:
                val = locals()[attr]
                if not val:
                    continue
                if val.endswith("%"):
                    continue
                elif val.endswith("px"):
                    val = val[:-2]
                elif val.isdigit():
                    pass
                else:
                    continue
                try:
                    new_val = 100 * int(float(val)) / (w if attr == "left" else h)
                except (ValueError, ZeroDivisionError):
                    continue
                utils.add_node_style(node, attr, "{}%".format(new_val))
예제 #6
0
def set_size_attributes(root):
    """
    Set width and height attributes on image (deprecated)
    """
    for img in root.xpath("//img"):
        variants = ["width", "height"]
        for variant in variants:
            if img.get(variant) is None:
                style = utils.get_node_style(img)
                value = style.get(variant)
                try:
                    value = number_re.match(value).group(0)
                except Exception:
                    continue
                img.set(variant, value)
예제 #7
0
def clean_infobox_inner_width(root):
    for node in root.xpath('//*[contains(@class, "infobox")]//div[contains(@style, "width")]'):
        if "width" in utils.get_node_style(node):
            utils.remove_node_styles(node, "width")
예제 #8
0
def markup_floated_tables(root):
    for my_table in root.xpath("//table"):
        styles = utils.get_node_style(my_table)
        if "float" in styles and styles["float"] == "right":
            utils.append_class(my_table, "right-floated-table")