Example #1
0
def test_backpath_urls():
    current = "http://domain.com/something/another.html"
    url1 = "../script.js"
    expected = "http://domain.com/script.js"
    assert (relative_url(url1, current) == expected)

    current = "http://domain.com/base/deep/nested/something/another.html"
    url1 = "../../../script.js"
    expected = "http://domain.com/base/script.js"
    assert (relative_url(url1, current) == expected)
Example #2
0
 def handle_click(self, e):
     self.focus = None
     if e.y < 60:  # Browser chrome
         if 10 <= e.x < 35 and 10 <= e.y < 50:
             self.go_back()
         elif 50 <= e.x < 790 and 10 <= e.y < 50:
             self.focus = "address bar"
             self.address_bar = ""
             self.render()
     else:
         x, y = e.x, e.y + self.scroll - 60
         obj = find_layout(x, y, self.document)
         if not obj: return
         elt = obj.node
         if elt and self.dispatch_event("click", elt): return
         while elt:
             if isinstance(elt, TextNode):
                 pass
             elif is_link(elt):
                 url = relative_url(elt.attributes["href"], self.url)
                 self.load(url)
             elif elt.tag == "input":
                 elt.attributes["value"] = ""
                 self.focus = obj
                 print("Layout called from handle_click in input elt")
                 return self.reflow(self.focus)
             elif elt.tag == "button":
                 self.submit_form(elt)
             elt = elt.parent
Example #3
0
    def load(self, url, body=None):
        self.address_bar = url
        self.url = url
        self.history.append(url)
        self.timer.start("Downloading")
        req_headers = {"Cookie": self.cookie_string()}
        headers, body = request(url, headers=req_headers, payload=body)
        if "set-cookie" in headers:
            kv, *params = headers["set-cookie"].split(";")
            key, value = kv.split("=", 1)
            self.cookies[key] = value
            print(f"Received Cookie key={key}, value={value}")
            origin = url_origin(self.history[-1])
            self.cookies.setdefault(origin, {})[key] = value
        self.timer.start("Parsing HTML")
        self.nodes = parse(lex(body))

        self.timer.start("Parsing CSS")
        with open("browser/src/browser.css") as f:
            browser_style = f.read()
            rules = CSSParser(browser_style).parse()
        for link in find_links(self.nodes, []):
            headers, body = request(relative_url(link, url),
                                    headers=req_headers)
            rules.extend(CSSParser(body).parse())

        # tree_to_string(self.nodes)
        rules.sort(key=lambda selector_body: selector_body[0].priority(),
                   reverse=True)
        self.rules = rules

        self.timer.start("Running JS")
        self.setup_js()
        for script in find_scripts(self.nodes, []):
            header, body = request(relative_url(script, self.history[-1]),
                                   headers=req_headers)
            try:
                # print("Script returned: ", self.js_environment.evaljs(body))
                self.js_environment.evaljs(body)
            except dukpy.JSRuntimeError as e:
                print("Script", script, "crashed", e)

        print("Layout called from load")
        self.layout(self.nodes)
Example #4
0
    def submit_form(self, elt):
        while elt and elt.tag != "form":
            elt = elt.parent
        if not elt: return
        if self.dispatch_event("submit", elt): return
        inputs = find_inputs(elt, [])
        body = ""
        for input in inputs:
            name = input.attributes["name"]
            value = input.attributes.get("value", "")
            body += "&" + name + "=" + value.replace(" ", "%20")
        body = body[1:]

        url = relative_url(elt.attributes["action"], self.url)
        self.load(url, body=body)
Example #5
0
def test_complete_urls():
    url1 = "http://something.com/another/layer.html"
    assert (relative_url(url1, "") == url1)
    url2 = "https://secure.com/url/layered.html"
    assert (relative_url(url2, "") == url2)
Example #6
0
def test_relative_urls():
    current = "http://domain.com/something/another.html"
    url1 = "script.js"
    expected = "http://domain.com/something/script.js"
    assert (relative_url(url1, current) == expected)
Example #7
0
def test_absolute_urls():
    current = "http://domain.com/something/another.html"
    url1 = "/garbage/script.js"
    expected = "http://domain.com" + url1
    assert (relative_url(url1, current) == expected)