-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom.nim
209 lines (173 loc) · 5.64 KB
/
dom.nim
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
import htmltags
import xas
import strutils
type
Attribute* = object
attr*: int
value*: cstring
Attributes = ptr array[1_000, Attribute]
Elements = ptr array[100_000, Element]
Element* = ptr ElementObj
ElementObj = object
tag*: int
nKids*: int
nAttrs*: int
Buf* = object
head*: pointer
tail: pointer
DOMBuilder* = object
current*: Element
contentStack: Buf
elements: Buf
strings: Buf
dom: Buf
template push*[T](buf: var Buf, val: T) =
assert sizeof(T) == sizeof(int)
cast[ptr T](buf.tail)[] = val
buf.tail = cast[pointer](cast[int](buf.tail) +% sizeof(T))
proc pop(buf: var Buf, T: typedesc): T {.inline.} =
buf.tail = cast[pointer](cast[int](buf.tail) -% sizeof(T))
cast[ptr T](buf.tail)[]
template advance(buf: var Buf, size: int) =
buf.tail = cast[pointer](cast[int](buf.tail) +% size)
template write(buf: var Buf, data: pointer, size: int) =
copyMem(buf.tail, data, size)
advance(buf, size)
template trim(buf: var Buf, newTail: pointer) =
buf.tail = newTail
template size(e: Element): int =
sizeof(ElementObj) + (sizeof(Attribute) * e.nAttrs) + (sizeof(Element) * e.nKids)
template attrs*(e: Element): Attributes =
cast[Attributes](cast[int](e) +% sizeof(ElementObj))
template kids*(e: Element): Elements =
cast[Elements](cast[int](e) +% sizeof(ElementObj) +% (sizeof(Attribute) * e.nAttrs))
proc toHtml*(e: Element, output: var string, level: int) =
output.add spaces(level * 2)
output.add '<'
output.add $Tag(e.tag)
let attrs = e.attrs
for i in 0..< e.nAttrs:
output.add ' '
output.add $Attr(attrs[i].attr)
output.add "=\""
output.add attrs[i].value
output.add '"'
if e.nKids == 0:
output.add "/>\n"
else:
output.add ">\n"
let kids = e.kids
for i in 0..< e.nKids:
toHtml(kids[i], output, level + 1)
output.add spaces(level * 2)
output.add "</"
output.add $Tag(e.tag)
output.add ">\n"
proc `$`*(e: Element): string =
result = ""
e.toHtml(result, 0)
proc save(builder: var DOMBuilder, s: string): cstring {.inline.} =
result = cast[cstring](builder.strings.tail)
builder.strings.write(cstring(s), s.len + 1)
proc openTag*(builder: var DOMBuilder, tag: Tag) =
builder.elements.push builder.current
builder.current = cast[Element](builder.contentStack.tail)
builder.contentStack.advance sizeof ElementObj
builder.current.tag = int(tag)
builder.current.nAttrs = 0
builder.current.nKids = 0
proc closeTag*(builder: var DOMBuilder) =
let res = cast[Element](builder.dom.tail)
builder.dom.write(builder.current, builder.current.size)
builder.contentStack.trim(builder.current)
builder.contentStack.push res
builder.current = builder.elements.pop(Element)
inc builder.current.nKids
proc attr*(builder: var DOMBuilder, attr: Attr, value: cstring) =
builder.contentStack.push int(attr)
builder.contentStack.push value
inc builder.current.nAttrs
when false:
let attrs = builder.current.attrs
var i = builder.current.nAttrs - 1
while i >= 0:
if int(attrs[i].attr) > int(attrs[i+1].attr):
swap(attrs[i].attr, attrs[i+1].attr)
swap(attrs[i].value, attrs[i+1].value)
dec i
else:
break
proc attrString*(builder: var DOMBuilder, attr: Attr, value: string) {.inline.} =
attr(builder, attr, builder.save(value))
proc text*(builder: var DOMBuilder, s: cstring) =
# we can further optimize openTag/closeTag call for leaf nodes, for now let's do simple
builder.openTag(Tag.TEXT)
builder.attr(Attr.TEXT, s)
builder.closeTag()
proc textString*(builder: var DOMBuilder, s: string) {.inline.} =
text(builder, builder.save(s))
proc done*(builder: var DOMBuilder) =
let res = cast[Element](builder.dom.tail)
builder.dom.write(builder.current, builder.current.size)
builder.contentStack.trim(builder.current)
builder.contentStack.push res
builder.current = builder.elements.pop(Element)
builder.current = builder.contentStack.pop(Element)
proc initBuf*(buf: var Buf, size: int) =
buf.head = alloc(size)
buf.tail = buf.head
proc initDOMBuilder*(): DOMBuilder =
initBuf(result.contentStack, 1024*1024)
initBuf(result.elements, 8*1024)
initBuf(result.dom, 1024*1024)
initBuf(result.strings, 1024*1024)
openTag(result, Tag.DOCUMENT_ROOT)
proc free(buf: var Buf) =
dealloc(buf.head)
buf.head = nil
buf.tail = nil
proc clear*(buf: var Buf) =
buf.tail = buf.head
proc free(builder: var DOMBuilder) =
free(builder.contentStack)
free(builder.elements)
free(builder.dom)
free(builder.strings)
proc clear*(builder: var DOMBuilder) =
clear(builder.contentStack)
clear(builder.elements)
clear(builder.dom)
clear(builder.strings)
openTag(builder, Tag.DOCUMENT_ROOT)
proc mem*(buf: Buf): int = cast[int](buf.tail) -% cast[int](buf.head)
proc `$`*(builder: DOMBuilder): string =
result = "Memory usage:\n"
result.add "content stack: " & $builder.contentStack.mem
result.add "\n"
result.add "element stack: " & $builder.elements.mem
result.add "\n"
result.add "DOM (structure): " & $builder.dom.mem
result.add "\n"
result.add "DOM (strings): " & $builder.strings.mem
var builder = initDOMBuilder()
# for j in 0..0:
# builder.clear()
# for i in 0..0:
builder.openTag(Tag.section)
builder.attr Attr.width, "vvvvvv"
builder.openTag(Tag.p)
builder.attr Attr.class, "yyy"
builder.attr Attr.value, "title"
builder.attr Attr.placeholder, "vvvvvv"
builder.attr Attr.title, "title"
builder.attr Attr.width, "vvvvvv"
builder.closeTag()
builder.openTag(Tag.a)
builder.attr Attr.width, "vvvvvv"
builder.attr Attr.value, "title"
builder.attr Attr.title, "title"
builder.closeTag()
builder.closeTag()
builder.done()
echo builder
echo builder.current