Exemplo n.º 1
0
 def test_descendant(self):
     css = _parse_selector(".c img")
     cls = _parse_selector(".c")
     img = _parse_selector("img")
     aut_built = cssautomaton.fromselector(css)
     aut = CSSAutomaton()
     #   _>,~_   ______>__________
     #  /     \ /     .c          \
     #  \     // >            >,~  \       .
     #    qin ------> qloop ------> qmid ------> qfin
     #          .c   /     \  *            img
     #               \_>,~_/
     #                  *
     aut.qinit = aut._new_state()
     aut.qfinal = aut._new_state()
     aut.add_tran(Tran(aut.qinit, Arrow.child, _isany, aut.qinit))
     aut.add_tran(Tran(aut.qinit, Arrow.sibling, _isany, aut.qinit))
     qmid = aut._new_state()
     qloop = aut._new_state()
     aut.add_tran(Tran(aut.qinit, Arrow.child, cls, qmid))
     aut.add_tran(Tran(aut.qinit, Arrow.child, cls, qloop))
     aut.add_tran(Tran(qloop, Arrow.child, _isany, qloop))
     aut.add_tran(Tran(qloop, Arrow.sibling, _isany, qloop))
     aut.add_tran(Tran(qloop, Arrow.child, _isany, qmid))
     aut.add_tran(Tran(qloop, Arrow.neighbour, _isany, qmid))
     aut.add_tran(Tran(qmid, Arrow.noop, img, aut.qfinal))
     self.assertEqual(aut, aut_built)
Exemplo n.º 2
0
def _make_selector_automata(css):
    """
    Note: is memoized, call reset_selectors_overlap_memo before using

    :param css:
        A css selector in parsed_tree cssselect format
    :returns:
        An automaton representation of the selector
    """
    if _selectors_automata.has_key(css):
        return _selectors_automata[css]
    else:
        aut = cssautomaton.fromselector(css)
        _selectors_automata[css] = aut
        return aut
Exemplo n.º 3
0
 def test_class(self):
     css = _parse_selector(".c")
     aut_built = cssautomaton.fromselector(css)
     #   _>,~_
     #  /     \
     #  \     /  .
     #    qin ------> qfin
     #          .c
     aut = CSSAutomaton()
     aut.qinit = aut._new_state()
     aut.qfinal = aut._new_state()
     aut.add_tran(Tran(aut.qinit, Arrow.child, _isany, aut.qinit))
     aut.add_tran(Tran(aut.qinit, Arrow.sibling, _isany, aut.qinit))
     aut.add_tran(Tran(aut.qinit, Arrow.noop, css, aut.qfinal))
     self.assertEqual(aut, aut_built)
Exemplo n.º 4
0
 def test_complex_node_selector(self):
     css = _parse_selector(
         ".c[s|a |= yes]#id:disabled:not(:nth-child(3n+4))")
     aut_built = cssautomaton.fromselector(css)
     #   _>,~_
     #  /     \
     #  \     /  .
     #    qin ------> qfin
     #          css
     aut = CSSAutomaton()
     aut.qinit = aut._new_state()
     aut.qfinal = aut._new_state()
     aut.add_tran(Tran(aut.qinit, Arrow.child, _isany, aut.qinit))
     aut.add_tran(Tran(aut.qinit, Arrow.sibling, _isany, aut.qinit))
     aut.add_tran(Tran(aut.qinit, Arrow.noop, css, aut.qfinal))
     self.assertEqual(aut, aut_built)
Exemplo n.º 5
0
 def test_neighbour(self):
     css = _parse_selector(".c + img")
     cls = _parse_selector(".c")
     img = _parse_selector("img")
     aut_built = cssautomaton.fromselector(css)
     #   _>,~_
     #  /     \
     #  \     /  +            .
     #    qin ------> qmid ------> qfin
     #          .c           img
     aut = CSSAutomaton()
     aut.qinit = aut._new_state()
     aut.qfinal = aut._new_state()
     qmid = aut._new_state()
     aut.add_tran(Tran(aut.qinit, Arrow.child, _isany, aut.qinit))
     aut.add_tran(Tran(aut.qinit, Arrow.sibling, _isany, aut.qinit))
     aut.add_tran(Tran(aut.qinit, Arrow.neighbour, cls, qmid))
     aut.add_tran(Tran(qmid, Arrow.noop, img, aut.qfinal))
     self.assertEqual(aut, aut_built)
Exemplo n.º 6
0
    def test_chained_selectors(self):
        css = _parse_selector(".c img ~ :active")
        cls = _parse_selector(".c")
        img = _parse_selector("img")
        act = _parse_selector(":active")
        aut_built = cssautomaton.fromselector(css)
        #   _>,~_   ______>__________       ______+__________
        #  /     \ /     .c          \     /     img         \
        #  \     // >            >,~  \   / +             +   \       .
        #    qin ------> qloop1 -----> q1 ------> qloop2 -----> qtwo ------> qfin
        #          .c   /     \  *          img  /     \  *          :active
        #               \_>,~_/                  \__~__/
        #                  *                        *
        aut = CSSAutomaton()
        aut.qinit = aut._new_state()
        aut.qfinal = aut._new_state()
        aut.add_tran(Tran(aut.qinit, Arrow.child, _isany, aut.qinit))
        aut.add_tran(Tran(aut.qinit, Arrow.sibling, _isany, aut.qinit))

        # kind of rubbish we have to get these in the same order as the aut
        # construction, but i don't know how to solve graph isomorphism easily
        q2 = aut._new_state()
        q1 = aut._new_state()
        qloop1 = aut._new_state()
        qloop2 = aut._new_state()

        aut.add_tran(Tran(aut.qinit, Arrow.child, cls, q1))
        aut.add_tran(Tran(aut.qinit, Arrow.child, cls, qloop1))
        aut.add_tran(Tran(qloop1, Arrow.child, _isany, qloop1))
        aut.add_tran(Tran(qloop1, Arrow.sibling, _isany, qloop1))
        aut.add_tran(Tran(qloop1, Arrow.child, _isany, q1))
        aut.add_tran(Tran(qloop1, Arrow.neighbour, _isany, q1))

        aut.add_tran(Tran(q1, Arrow.neighbour, img, q2))
        aut.add_tran(Tran(q1, Arrow.neighbour, img, qloop2))
        aut.add_tran(Tran(qloop2, Arrow.sibling, _isany, qloop2))
        aut.add_tran(Tran(qloop2, Arrow.neighbour, _isany, q2))

        aut.add_tran(Tran(q2, Arrow.noop, act, aut.qfinal))

        self.assertEqual(aut, aut_built)