def __contains__(self, path):
     if len(path) >= 2:
         node = path[-1]
         return isinstance(node, xsc.Element) and _is_nth_last_node(
             misc.Iterator(_children_of_type(path[-2], node.xmlname)), node,
             1)
     return False
def test_iterator_getitem():
    e = misc.Iterator(iter(range(10)))
    assert e[0] == 0
    assert e[0] == 1
    assert e[-1] == 9
    with pytest.raises(IndexError):
        e[-1]
 def __contains__(self, path):
     if len(path) >= 2:
         node = path[-1]
         if isinstance(node, xsc.Element):
             return _is_nth_node(
                 misc.Iterator(_children_of_type(path[-2], node.xmlname)),
                 node, self.value)
     return False
def iterrules(node, base=None, media=None, title=None):
    """
	Return an iterator for all CSS rules defined in the HTML tree :obj:`node`.
	This will parse the CSS defined in any :class:`html.style` or
	:class:`html.link` element (and recursively in those stylesheets imported
	via the ``@import`` rule). The rules will be returned as
	:class:`CSSStyleRule` objects from the :mod:`cssutils` package (so this
	requires :mod:`cssutils`).

	The :obj:`base` argument will be used as the base URL for parsing the
	stylesheet references in the tree (so :const:`None` means the URLs will be
	used exactly as they appear in the tree). All URLs in the style properties
	will be resolved.

	If :obj:`media` is given, only rules that apply to this media type will
	be produced.

	:obj:`title` can be used to specify which stylesheet group should be used.
	If :obj:`title` is :const:`None` only the persistent and preferred
	stylesheets will be used. If :obj:`title` is a string only the persistent
	stylesheets and alternate stylesheets with that style name will be used.

	For a description of "persistent", "preferred" and "alternate" stylesheets
	see <http://www.w3.org/TR/2002/WD-xhtml2-20020805/mod-styleSheet.html#sec_20.1.2.>
	"""
    if base is not None:
        base = url.URL(base)

    def matchlink(node):
        if node.attrs.media.hasmedia(media):
            if title is None:
                if "title" not in node.attrs and "alternate" not in str(
                        node.attrs.rel).split():
                    return True
            elif not node.attrs.title.isfancy() and str(
                    node.attrs.title) == title and "alternate" in str(
                        node.attrs.rel).split():
                return True
        return False

    def matchstyle(node):
        if node.attrs.media.hasmedia(media):
            if title is None:
                if "title" not in node.attrs:
                    return True
            elif str(node.attrs.title) == title:
                return True
        return False

    def doiter(node):
        for cssnode in node.walknodes(_isstyle):
            if isinstance(cssnode, html.style):
                href = str(base) if base is not None else None
                if matchstyle(cssnode):
                    stylesheet = cssutils.parseString(str(cssnode.content),
                                                      href=href,
                                                      media=str(
                                                          cssnode.attrs.media))
                    yield from _doimport(media, stylesheet, base)
            else:  # link
                if "href" in cssnode.attrs:
                    href = cssnode.attrs.href.asURL()
                    if base is not None:
                        href = base / href
                    if matchlink(cssnode):
                        stylesheet = cssutils.parseUrl(
                            str(href), media=str(cssnode.attrs.media))
                        yield from _doimport(media, stylesheet, href)

    return misc.Iterator(doiter(node))
def test_iterator_next():
    e = misc.Iterator(iter(range(2)))
    assert next(e) == 0
    assert next(e) == 1
    with pytest.raises(StopIteration):
        next(e)
def test_iterator_bool():
    e = misc.Iterator(iter(range(10)))
    assert e

    e = misc.Iterator(iter([]))
    assert not e