diff --git a/browser.py b/browser.py index 9be28cf..ab1234a 100644 --- a/browser.py +++ b/browser.py @@ -9,9 +9,16 @@ SUPPORTED_SCHEMES = [ "http", "https", "file", - "data" + "data", + "view-source", ] +ENTITIES = { + "<": "<", + ">": ">", + "&": "&", +} + DEFAULT_FILE = "default.html" @@ -27,6 +34,7 @@ class URL: self.media_type = "" self.media_encoding = "" self.data = "" + self.view_source: bool = False if url_string is not None: parse_url(url_string, self) @@ -142,6 +150,9 @@ def parse_url(url_string: str, url: URL | None = None) -> tuple[URL, bool]: try: url.scheme, url_string = url_string.split(":", 1) assert url.scheme in SUPPORTED_SCHEMES + if url.scheme == "view-source": + url.view_source = True + url.scheme, url_string = url_string.split(":", 1) if url_string.startswith("//"): has_authority = True @@ -210,15 +221,28 @@ def parse_url(url_string: str, url: URL | None = None) -> tuple[URL, bool]: return url, False -def show(body: str) -> None: +def show(body: str, view_source: bool = False) -> None: + if view_source: + print(body) + return in_tag = False - for char in body: - if char == "<": + i = 0 + while i < len(body): + if body[i] == "<": in_tag = True - elif char == ">": + elif body[i] == ">": in_tag = False + elif body[i] == "&" and not in_tag: + index = body.find(";", i, i + 10) + if index >= 0 and body[i:index+1] in ENTITIES: + print(ENTITIES[body[i:index+1]], end="") + i = index +1 + continue + else: + print(body[i], end="") elif not in_tag: - print(char, end="") + print(body[i], end="") + i += 1 def load(url_string: str): @@ -230,7 +254,7 @@ def load(url_string: str): url, _ = parse_url(f"file://{default}") body = Request(url).send_request() - show(body) + show(body, url.view_source) if __name__ == '__main__': diff --git a/default.html b/default.html index 6769c9f..239eff7 100644 --- a/default.html +++ b/default.html @@ -6,5 +6,7 @@
Test
+'<div></div>'
+<div></div>