-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.py
More file actions
378 lines (292 loc) · 12.3 KB
/
browser.py
File metadata and controls
378 lines (292 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
from typing import List
import tkinter
import tkinter.font
from request import request_url, resolve_url
from entities import chars_to_entity
from layout import VSTEP, DocumentLayout, DrawRect, DrawText, get_font
from css import DescendantSelector, TagSelector, CSSParser, print_rules
from dom import Text, Element, HTMLParser, only_body
def escape_html(html: str):
escaped = ''
for char in html:
escaped += chars_to_entity(char)
return escaped
def build_view_source_html(source: str):
return f"<html><head></head><body>{escape_html(source)}</body></html>"
INHERITED_PROPERTIES = {
"font-size": "16px",
"font-style": "normal",
"font-weight": "normal",
"color": "black",
}
def compute_style(node, property, value):
if property == "font-size":
if value.endswith("px"):
return value
elif value.endswith("%"):
if node.parent:
parent_font_size = node.parent.style["font-size"]
else:
parent_font_size = INHERITED_PROPERTIES["font-size"]
# have to resolve percentage to a pixel value since font-size is a "computed style"
# see https://www.w3.org/TR/CSS2/cascade.html#computed-value for more info
node_pct = float(value[:-1]) / 100
parent_px = float(parent_font_size[:-2])
return str(node_pct * parent_px) + "px"
else:
return None
else:
return value
def apply_rule_body(rule_body, node):
for property, value in rule_body.items():
computed_value = compute_style(node, property, value)
if not computed_value:
continue
node.style[property] = computed_value
def style(node: Text | Element, rules: List[tuple[TagSelector | DescendantSelector, dict]]):
# TODO - should this function be invoked from the nodes themselves?
node.style = {}
# inherit properties
for property, default_value in INHERITED_PROPERTIES.items():
if node.parent:
node.style[property] = node.parent.style[property]
else:
node.style[property] = default_value
# apply global CSS rules
for selector, body in rules:
if selector.matches(node):
apply_rule_body(body, node)
# apply inline styles
if isinstance(node, Element) and "style" in node.attributes:
body = CSSParser(node.attributes["style"]).body()
apply_rule_body(body, node)
for child in node.children:
style(child, rules)
def cascade_priority(rule):
selector, body = rule
return selector.priority
def tree_to_list(tree, list):
list.append(tree)
for child in tree.children:
tree_to_list(child, list)
return list
SCROLL_STEP = 100
CHROME_HEIGHT = 100
class Tab:
def __init__(self, width: int, height: int, trigger_render):
self.set_dimensions(width, height)
self.trigger_render = trigger_render
self.history = []
with open("browser.css") as f:
self.default_style_sheet = CSSParser(f.read()).parse()
def mousewheel(self, delta: int):
scroll_delta = SCROLL_STEP * -delta
if scroll_delta > 0:
self.scrolldown()
elif scroll_delta < 0:
self.scrollup()
def scrolldown(self):
max_y = self.document.height - self.height
self.scroll = min(self.scroll + SCROLL_STEP, max_y)
def scrollup(self):
self.scroll = max(self.scroll - SCROLL_STEP, 0)
def set_dimensions(self, width: int, height: int):
self.width, self.height = width, height
def click(self, x: int, y: int):
y += self.scroll
layouts = tree_to_list(self.document, [])
layouts_under_click = [layout for layout in layouts
if layout.x <= x < layout.x + layout.width
and layout.y <= y < layout.y + layout.height]
element = layouts_under_click[-1].node
if not element:
return
while element:
if isinstance(element, Text):
pass
elif element.tag == 'a':
href = element.attributes['href']
href_url = resolve_url(href, self.url)
self.load(href_url)
if href.startswith('#'):
for layout in filter(lambda layout: isinstance(layout.node, Element), layouts):
attributes = layout.node.attributes
if 'id' in attributes and attributes['id'] == href[1:]:
self.scroll = layout.y
self.trigger_render()
element = element.parent
def go_back(self):
if len(self.history) > 1:
self.history.pop()
back = self.history.pop()
self.load(back)
def draw(self, canvas: tkinter.Canvas):
for command in self.display_list:
if command.top > self.scroll + self.height - CHROME_HEIGHT:
# falls below the viewport
continue
if command.bottom + VSTEP < self.scroll:
# falls above the viewport
continue
command.execute(self.scroll - CHROME_HEIGHT, canvas)
def load(self, url: str):
self.history.append(url)
view_source = url.startswith("view-source:")
if view_source:
_, url = url.split(':', 1)
self.url = url
headers, body = request_url(url)
if view_source:
body = build_view_source_html(body)
self.nodes = HTMLParser(body).parse()
stylesheet_links = [node.attributes['href']
for node in tree_to_list(self.nodes, [])
if isinstance(node, Element)
and node.tag == 'link'
and 'href' in node.attributes
and node.attributes.get("rel") == "stylesheet"]
rules = self.default_style_sheet.copy()
for link in stylesheet_links:
try:
header, body = request_url(resolve_url(link, url))
except:
continue
rules.extend(CSSParser(body).parse())
# Note that before sorting rules, it is in file order. Since Python’s sorted function keeps the
# relative order of things when possible, file order thus acts as a tie breaker, as it should.
# See https://www.w3.org/TR/2011/REC-CSS2-20110607/cascade.html#cascading-order
style(self.nodes, sorted(rules, key=cascade_priority))
self.scroll = 0
self.build_and_paint_document()
def build_and_paint_document(self):
self.document = DocumentLayout(only_body(self.nodes))
self.document.layout(self.width)
self.display_list: List[DrawRect | DrawText] = []
self.document.paint(self.display_list)
HOME_PAGE = "https://browser.engineering/"
class Browser:
def __init__(self, initial_width: int, initial_height: int):
self.width, self.height = initial_width, initial_height
self.window = tkinter.Tk()
self.canvas = tkinter.Canvas(
self.window, width=self.width, height=self.height, bg="white")
self.canvas.pack()
self.tabs = []
self.active_tab = None
self.focus = None
self.address_bar = HOME_PAGE
# wait for TK paint, then bind the event listeners
self.window.wait_visibility(self.canvas)
self.window.bind("<Down>", self.handle_down)
self.window.bind("<Up>", self.handle_up)
self.window.bind("<MouseWheel>", self.handle_mousewheel)
self.window.bind("<Configure>", self.resize)
self.window.bind("<Button-1>", self.handle_click)
self.window.bind("<Key>", self.handle_key)
self.window.bind("<Return>", self.handle_enter)
def handle_down(self, e):
self.tabs[self.active_tab].scrolldown()
self.draw()
def handle_up(self, e):
self.tabs[self.active_tab].scrollup()
self.draw()
def handle_mousewheel(self, e):
self.tabs[self.active_tab].mousewheel(e.delta)
self.draw()
def handle_click(self, e):
self.focus = None
if e.y < CHROME_HEIGHT:
if 40 <= e.x < 40 + 80 * len(self.tabs) and 0 <= e.y < 40:
self.active_tab = int((e.x - 40) / 80)
elif 10 <= e.x < 30 and 10 <= e.y < 30:
self.load(HOME_PAGE)
elif 10 <= e.x < 35 and 40 <= e.y < 90:
self.tabs[self.active_tab].go_back()
elif 50 <= e.x < self.width - 10 and 40 <= e.y < 90:
self.focus = "address bar"
else:
self.tabs[self.active_tab].click(e.x, e.y - CHROME_HEIGHT)
self.draw()
def handle_key(self, e):
is_backspace = e.keysym == 'BackSpace'
if not is_backspace:
if len(e.char) == 0:
return
if not (0x20 <= ord(e.char) < 0x7f):
# outside ascii character range and not backspace
return
if self.focus == "address bar":
if is_backspace:
self.address_bar = self.address_bar[0:-1]
else:
self.address_bar += e.char
self.draw()
def handle_enter(self, e):
if self.focus == "address bar":
self.tabs[self.active_tab].load(self.address_bar)
self.focus = None
self.draw()
def trigger_tab_render(self):
"""Tabs can use this function to trigger a draw"""
self.draw()
def resize(self, e):
self.canvas.pack(fill='both', expand=1)
self.width, self.height = e.width, e.height
self.tabs[self.active_tab].set_dimensions(self.width, self.height)
self.tabs[self.active_tab].build_and_paint_document()
self.draw()
def draw(self):
self.canvas.delete('all')
self.tabs[self.active_tab].draw(self.canvas)
# fill in browser chrome
self.canvas.create_rectangle(
0, 0, self.width, CHROME_HEIGHT, fill="white", width=0)
tab_width, tab_height = 80, 40
tabfont = get_font(20, "normal", "roman")
for i, tab in enumerate(self.tabs):
name = "Tab {}".format(i)
x1, x2 = tab_height + tab_width * i, 120 + tab_width * i
self.canvas.create_line(x1, 0, x1, 40, fill="black")
self.canvas.create_line(x2, 0, x2, 40, fill="black")
self.canvas.create_text(x1 + 10, 10, anchor="nw", text=name,
font=tabfont, fill="black")
if i == self.active_tab:
self.canvas.create_line(0, 40, x1, 40, fill="black")
self.canvas.create_line(x2, 40, self.width, 40, fill="black")
# draw the new-tab button
buttonfont = get_font(30, "normal", "roman")
self.canvas.create_rectangle(10, 10, 30, 30,
outline="black", width=1)
self.canvas.create_text(11, 0, anchor="nw", text="+",
font=buttonfont, fill="black")
# draw the address bar
self.canvas.create_rectangle(40, 50, self.width - 10, 90,
outline="black", width=1)
if self.focus == "address bar":
self.canvas.create_text(
55, 55, anchor='nw', text=self.address_bar,
font=buttonfont, fill="black")
w = buttonfont.measure(self.address_bar)
self.canvas.create_line(55 + w, 55, 55 + w, 85, fill="black")
else:
url = self.tabs[self.active_tab].url
self.canvas.create_text(55, 55, anchor='nw', text=url,
font=buttonfont, fill="black")
# draw back button
self.canvas.create_rectangle(10, 50, 35, 90,
outline="black", width=1)
self.canvas.create_polygon(
15, 70, 30, 55, 30, 85, fill='black')
self.canvas.create_line(
0, CHROME_HEIGHT, self.width, CHROME_HEIGHT, fill="black")
def load(self, url):
new_tab = Tab(self.width, self.height, self.trigger_tab_render)
new_tab.load(url)
self.active_tab = len(self.tabs)
self.tabs.append(new_tab)
self.draw()
if __name__ == '__main__':
import sys
initial_url = sys.argv[1] if len(sys.argv) < 1 else HOME_PAGE
Browser(800, 600).load(initial_url)
tkinter.mainloop()