Пример #1
0
    def attach_event(self, elem, evt, h):
        handler = None

        if isinstance(h, six.string_types):
            handler = self.build_event_handler(self.context, h)
            # PyV8.JSEngine.collect()
        elif isinstance(h, PyV8.JSFunction):
            handler = h
        else:
            try:
                handler = getattr(self.context.locals, h, None)
            except Exception:
                pass

        if not handler:
            return

        if getattr(elem, 'name', None) and elem.name in (
                'body', ) and evt in self.window_on_events:
            setattr(self.window, evt, handler)
            return

        if not getattr(elem, '_node', None):
            from thug.DOM.W3C.Core.DOMImplementation import DOMImplementation
            DOMImplementation.createHTMLElement(self.window.doc, elem)

        elem._node._attachEvent(evt, handler, True)
Пример #2
0
    def namedItem(self, name):
        from thug.DOM.W3C.Core.DOMImplementation import DOMImplementation

        for node in self.nodes:
            if 'id' in node.attrs and node.attrs['id'] in (name, ):
                return DOMImplementation.createHTMLElement(self.doc, node)

        for node in self.nodes:
            if 'name' in node.attrs and node.attrs['name'] in (name, ):
                return DOMImplementation.createHTMLElement(self.doc, node)

        return None
Пример #3
0
    def frames(self):
        """an array of all the frames (including iframes) in the current window"""
        from thug.DOM.W3C.HTML.HTMLCollection import HTMLCollection

        frames = set()
        for frame in self._findAll(['frame', 'iframe']):
            if not getattr(frame, '_node', None):
                from thug.DOM.W3C.Core.DOMImplementation import DOMImplementation
                DOMImplementation.createHTMLElement(self.window.doc, frame)

            frames.add(frame._node)

        return HTMLCollection(self.doc, list(frames))
Пример #4
0
    def frames(self):
        """an array of all the frames (including iframes) in the current window"""
        from thug.DOM.W3C.HTML.HTMLCollection import HTMLCollection

        frames = set()
        for frame in self._findAll(['frame', 'iframe']):
            if not getattr(frame, '_node', None):
                from thug.DOM.W3C.Core.DOMImplementation import DOMImplementation
                DOMImplementation.createHTMLElement(self.window.doc, frame)

            frames.add(frame._node)

        return HTMLCollection(self.doc, list(frames))
Пример #5
0
    def currentScript(self):
        from thug.DOM.W3C.Core.DOMImplementation import DOMImplementation

        if self._currentScript:
            return self._currentScript

        return DOMImplementation.createHTMLElement(self.doc, self.current) if self.current else None
Пример #6
0
    def __getattr__(self, attr):
        if attr in ('_listeners', ):
            return self.tag._listeners

        if attr in ('getBoxObjectFor', ) and not log.ThugOpts.Personality.isFirefox():
            raise AttributeError

        if self._win and getattr(self._win, "doc", None):
            if attr in self._win.doc.DFT.handled_on_events:
                return None

            if attr in self._win.doc.DFT._on_events:
                return None

        _attr = self.getElementById(attr)
        if _attr:
            return _attr

        _attr = self.getElementsByName(attr)
        if _attr:
            from thug.DOM.W3C.Core.DOMImplementation import DOMImplementation
            return DOMImplementation.createHTMLElement(self.doc, _attr[0])

        log.info("[HTMLDocument] Undefined: %s", attr)
        raise AttributeError
Пример #7
0
    def __getattr__(self, attr):
        if attr in ('_listeners', ):
            return self.tag._listeners

        if attr in ('getBoxObjectFor',
                    ) and not log.ThugOpts.Personality.isFirefox():
            raise AttributeError

        if self._win and getattr(self._win, "doc", None):
            if attr in self._win.doc.DFT.handled_on_events:
                return None

            if attr in self._win.doc.DFT._on_events:
                return None

        _attr = self.getElementById(attr)
        if _attr:
            return _attr

        _attr = self.getElementsByName(attr)
        if _attr:
            from thug.DOM.W3C.Core.DOMImplementation import DOMImplementation
            return DOMImplementation.createHTMLElement(self.doc, _attr[0])

        log.info("[HTMLDocument] Undefined: %s", attr)
        raise AttributeError
Пример #8
0
    def currentScript(self):
        from thug.DOM.W3C.Core.DOMImplementation import DOMImplementation

        if self._currentScript:
            return self._currentScript # pragma: no cover

        return DOMImplementation.createHTMLElement(self.doc, self.current) if self.current else None
Пример #9
0
    def styleSheets(self):
        from .HTMLCollection import HTMLCollection
        from thug.DOM.W3C.Core.DOMImplementation import DOMImplementation

        return HTMLCollection(self.doc, [
            DOMImplementation.createHTMLElement(self.doc, f)
            for f in self.doc.find_all('style')
        ])
Пример #10
0
    def namedItem(self, name):
        from thug.DOM.W3C.Core.DOMImplementation import DOMImplementation

        for node in self.nodes:
            if node.nodeName == name:
                return DOMImplementation.createHTMLElement(self.doc, node) if node else None

        return None
Пример #11
0
    def elements(self):
        from thug.DOM.W3C.Core.DOMImplementation import DOMImplementation

        nodes = []
        for tag in self.tag.children:
            if getattr(tag, 'name', None) and tag.name not in ('br', ):
                nodes.append(DOMImplementation.createHTMLElement(self.doc, tag))

        return HTMLFormControlsCollection(self.doc, nodes)
Пример #12
0
    def __getattr__(self, key):
        for tag in self.tag.children:
            if tag.name not in ('input', ):
                continue

            if 'name' in tag.attrs and tag.attrs['name'] in (key, ):
                from thug.DOM.W3C.Core.DOMImplementation import DOMImplementation
                return DOMImplementation.createHTMLElement(self.doc, tag)

        raise AttributeError
Пример #13
0
    def __getattr__(self, key):
        for tag in self.tag.children:
            if tag.name not in ('input', ):
                continue

            if 'name' in tag.attrs and tag.attrs['name'] in (key, ):
                from thug.DOM.W3C.Core.DOMImplementation import DOMImplementation
                return DOMImplementation.createHTMLElement(self.doc, tag)

        raise AttributeError
Пример #14
0
    def elements(self):
        from thug.DOM.W3C.Core.DOMImplementation import DOMImplementation

        nodes = []
        for tag in self.tag.children:
            if getattr(tag, 'name', None) and tag.name not in ('br', ):
                nodes.append(DOMImplementation.createHTMLElement(
                    self.doc, tag))

        return HTMLFormControlsCollection(self.doc, nodes)
Пример #15
0
    def getter(self):
        from thug.DOM.W3C.Core.DOMImplementation import DOMImplementation

        if not self.tag.parent.name.lower() in ('form', ):
            return default

        _form = getattr(self, '_form', None)
        if _form is None:
            self._form = DOMImplementation.createHTMLElement(self.doc, self.tag.parent)

        return self._form
Пример #16
0
    def item(self, index):
        from thug.DOM.W3C.Core.DOMImplementation import DOMImplementation

        if index < 0 or index >= self.length:
            return None

        if isinstance(self.nodes[index], bs4.element.Tag):
            return DOMImplementation.createHTMLElement(self.doc,
                                                       self.nodes[index])

        return self.nodes[index]
Пример #17
0
    def getter(self):
        from thug.DOM.W3C.Core.DOMImplementation import DOMImplementation

        if not self.tag.parent.name.lower() in ('form', ):
            return default

        _form = getattr(self, '_form', None)
        if _form is None:
            self._form = DOMImplementation.createHTMLElement(
                self.doc, self.tag.parent)

        return self._form
Пример #18
0
    def getter(self):
        children = getChildren(self.doc, parts)

        if xpath == '/html/body[1]' and not children:
            children = [self.doc]

        if parts[-1] == 'text()':
            return "".join(children)

        m = RE_INDEXED.match(parts[-1])

        if m:
            try:
                from thug.DOM.W3C.Core.DOMImplementation import DOMImplementation
                string.atoi(m.group(2))

                return DOMImplementation.createHTMLElement(
                    self.doc, children[0]) if len(children) > 0 else None
            except ValueError:
                pass

        return HTMLCollection(self.doc, children)
Пример #19
0
def parseString(html, **kwds):
    from thug.DOM.W3C.Core.DOMImplementation import DOMImplementation

    soup = log.HTMLInspector.run(html, "html.parser")
    return DOMImplementation(soup, **kwds)
Пример #20
0
def getDOMImplementation(dom=None, **kwds):
    from thug.DOM.W3C.Core.DOMImplementation import DOMImplementation

    return DOMImplementation(dom if dom else bs4.BeautifulSoup('', 'lxml'),
                             **kwds)
Пример #21
0
def parseString(html, **kwds):
    from thug.DOM.W3C.Core.DOMImplementation import DOMImplementation
    return DOMImplementation(bs4.BeautifulSoup(html, "html.parser"), **kwds)
Пример #22
0
    def parseFromString(self, s, type_):
        from thug.DOM.W3C.Core.DOMImplementation import DOMImplementation

        parser = "lxml" if 'xml' in type_ else "html.parser"
        return DOMImplementation(log.HTMLInspector.run(s, parser))
Пример #23
0
    def parseFromString(self, s, type_):
        from thug.DOM.W3C.Core.DOMImplementation import DOMImplementation

        parser = "lxml" if 'xml' in type_ else "html.parser"
        return DOMImplementation(BeautifulSoup.BeautifulSoup(s, parser))