-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhtml_canvas.js
699 lines (576 loc) · 17.5 KB
/
html_canvas.js
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/*
HTML Canvas
Copyright (C) 2009 James S Urquhart
Refer to LICENSE file for license.
*/
(function(){
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
// document node prototype
function docNode(etype, document) {
this.ownerDocument = document;
this.nodeType = etype;
this.tagName = '';
this.nodeValue = '';
this.index = 0;
this.attributes = {};
this.parentIndex = 0;
// DOM attributes
this.parentNode = null;
this.childNodes = null;
this.firstChild = null;
this.lastChild = null;
this.nextSibling = null;
this.previousSibling = null;
// render temp
this.blockX = 0; // render origin
this.blockY = 0;
this.renderX = 0; // render position
this.renderY = 0;
this.renderWidth = 0; // rendered size (propagates backwards)
this.renderHeight = 0;
}
function docHTML()
{
this.nodes = [];
this.body = null;
this.title = null;
}
// Iterator for nodes
// Returns node in which func returns false, or null
docNode.prototype.iterateNodes = function(func) {
var nodeList = this.ownerDocument.nodes;
var len = nodeList.length;
var curStack = this.index;
for (var i=this.index+1; i<len; i++) {
var node = nodeList[i];
if (node.parentIndex < curStack)
break;
if (!func.call(null, node)) {
return node;
}
}
return null;
}
docNode.prototype.getFirstElementByTagName = function(tag) {
var selectAll = tag == '*';
return this.iterateNodes(function(node){
return !(selectAll || node.tagName == tag);
});
}
// Helpful element functions
docNode.prototype.getElementsByTagName = function(tag) {
var selectAll = tag == '*';
var list = [];
this.iterateNodes(function(node){
if (selectAll || node.tagName == tag)
list.push(node);
return true;
});
return list;
}
docNode.prototype.getElementsByName = function(name) {
var list = [];
this.iterateNodes(function(node){
if (selectAll || node.attr.name == name)
list.push(node);
return true;
});
return list;
}
docNode.prototype.getElementById = function(id) {
return this.iterateNodes(function(node){
return node.attributes.id != id;
});
}
docNode.prototype.getAttribute = function(name) {
return this.attributes(name);
}
docNode.prototype.setAttribute = function(name, value) {
this.attributes[name] = value;
}
// Finds main body tag in document
function findBody(nodeList) {
var len = nodeList.length;
for (var i=0; i<len; i++) {
var obj = nodeList[i];
if (obj.nodeType == 9)
return obj;
}
return 0;
}
// Finds an indexed node in list
function findNodeID(list, id) {
var len = list.length;
for (var i=len-1; i > -1; i--) {
if (list[i].index == id)
return i;
}
return -1;
}
// Pops node stack down to idx
function popToNodeStack(nodeStack, idx) {
var closedNodeIDX = findNodeID(nodeStack, idx);
if (closedNodeIDX >= 0) {
return nodeStack.slice(0, closedNodeIDX+1);
}
return nodeStack;
}
// Finds last open tagName in list
function findOpenNode(list, tagName) {
var len = list.length;
for (var i=len-1; i > -1; i--) {
//debugLog(list[i].tagName + '==' + tagName);
if (list[i].tagName == tagName)
return i;
}
return -1;
}
// Lays out elements on page
function layout(body, nodeList) {
var len = nodeList.length;
var nodeStack = [];
var ctx = document.getElementById('output').getContext("2d");
// initial style
ctx.font = "12pt Arial";
ctx.textAlign = "left";
ctx.textBaseline = "top";
ctx.fillStyle = "rgb(0, 0, 0, 1.0)";
ctx.strokeStyle = "rgb(1, 0, 0, 1.0)";
var curNode = body;
nodeStack.push(body);
for (var i=body.index+1; i<len; i++) {
var obj = nodeList[i];
debugLog("[" + i + "]" + obj.tagName + "=" + obj.nodeValue + ' @ P[' + obj.parentIndex + '] START=' + (curNode.blockX+curNode.renderWidth));
// End of body?
if (obj.parentIndex < body.index) {
break;
}
// NOTE: ideally should figure out a better way of styling!
var doBreak = false;
// Update stack (in case [tag][content][tag]...)
if (obj.parentIndex < curNode.index) {
var closedNodeIDX = findNodeID(nodeStack, obj.parentIndex);
//debugLog('POP STACK');
// Increment width on all nodes up to closing index
for (var j=nodeStack.length-2; j >= closedNodeIDX; j--) {
// TODO: obviously better handling
var node = nodeStack[j+1];
var parentNode = nodeStack[j];
if (parentNode.blockX + parentNode.renderWidth < node.blockX + node.renderWidth)
parentNode.renderWidth = node.blockX + node.renderWidth - parentNode.blockX;
if (parentNode.blockY + parentNode.renderHeight < node.blockY + node.renderHeight)
parentNode.renderHeight = node.blockY + node.renderHeight - parentNode.blockY;
parentNode.renderX = node.blockX + node.renderWidth;
debugLog(" [" + nodeStack[j].index + "] += " + nodeStack[j+1].index);
}
// Test break on P
// TODO: style lookup
if (nodeStack[closedNodeIDX+1].tagName == 'P') {
doBreak = true;
}
nodeStack = popToNodeStack(nodeStack, obj.parentIndex);
curNode = nodeStack[nodeStack.length-1];
}
// Content node?
if (obj.nodeType == 3) {
if (curNode.tagName == 'B')
ctx.font = "bold 12pt Arial";
else
ctx.font = "12pt Arial";
var renderMetrics = ctx.measureText(obj.nodeValue);
//debugLog("ADD ONTO " + curNode.tagName + "," + curNode.index);
debugLog(' R AT ' + curNode.renderX + '[' + curNode.index + ']');
// Plot position in current node render pos
obj.renderX = curNode.renderX;
obj.renderY = curNode.renderY;
obj.renderWidth = renderMetrics.width;
// Increment current node render pos
curNode.renderX += obj.renderWidth;
var delta = curNode.renderX - (curNode.blockX + curNode.renderWidth);
if (delta > 0)
curNode.renderWidth += delta;
curNode.renderHeight = 14;
} else {
// Sanity check
if (obj.parentIndex > curNode.index) {
debugLog("!!Parse oddity");
continue;
}
// Add new node on stack
nodeStack.push(obj);
// Set block origin
if (doBreak) {
obj.blockX = curNode.blockX;
obj.blockY = curNode.renderY + curNode.renderHeight;
} else {
obj.blockX = curNode.renderX;
obj.blockY = curNode.renderY;
}
// Set block render pos
obj.renderX = obj.blockX;
obj.renderY = obj.blockY;
curNode = obj;
}
}
debugLog("LAYOUT:");
debugLog(nodeList);
return nodeList;
}
function debugLog(line) {
if (window.console)
console.log(line);
}
// Renders parsed document
function render(doc) {
var nodeList = doc.nodes;
var len = nodeList.length;
var curX = 0;
var curY = 0;
var styleStack = [];
var nodeStack = [];
var ctx = document.getElementById('output').getContext("2d");
ctx.font = "12pt Arial";
ctx.textAlign = "left";
ctx.textBaseline = "top";
ctx.fillStyle = "rgb(0, 0, 0, 1.0)";
var body = doc.body;
// pre-process layout
nodeList = layout(body, nodeList);
// Iterate nodes
var curNode = body;
for (var i=body.index+1; i<len; i++) {
var obj = nodeList[i];
// End of body?
if (obj.parentIndex < body.index)
break;
// DEBUG
//if (obj.tagName == 'P')
// ctx.strokeRect(obj.blockX, obj.blockY, obj.renderWidth, 12);
// Update stack (in case [tag][content][tag]...)
if (obj.parentIndex < curNode.index) {
nodeStack = popToNodeStack(nodeStack, obj.parentIndex);
curNode = nodeStack[nodeStack.length-1];
}
// Content node?
if (obj.nodeType == 3) {
// TODO: style lookup
if (curNode.tagName == 'B')
ctx.font = "bold 12pt Arial";
else
ctx.font = "12pt Arial";
// Render current style
ctx.fillText(obj.nodeValue, obj.renderX, obj.renderY);
debugLog(obj.nodeValue + " at " + obj.renderX + "," + obj.renderY);
} else {
if (obj.parentIndex > curNode.index) // sanity check
continue;
// New node
nodeStack.push(obj);
curNode = obj;
}
}
}
// Populates DOM attributes
/*
parentNode
The parent of this node.
childNodes
A NodeList that contains all children of this node.
firstChild
The first child of this node.
lastChild
The last child of this node.
previousSibling
The node immediately preceding this node.
nextSibling
The node immediately following this node.
attributes
A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise.
ownerDocument
*/
function populateDOM(root)
{
var nodeStack = [];
var curNode = root;
var lastNode = null;
/*
BODY[ch=[P,P], fc=P, lc=P]
P [pn=BODY, cn=P, ln=P, ch=[B,TX,B], fc=B, lc=B, ns=P]
B [pn=P, cn=B, ns=TX]
TX [cn=TX, ps=B, pn=P, ns=B]
B [cn=B, ps=TX, pn=P]
P [cn=P, ps=P, pn=BODY]
*/
root.iterateNodes(function(node){
// Reset children
node.childNodes = null;
node.firstChild = null;
node.lastChild = null;
node.previousSibling = null;
node.nextSibling = null;
if (node.parentIndex < curNode.index) {
// Closed nodes
nodeStack = popToNodeStack(nodeStack, node.parentIndex);
curNode = nodeStack[nodeStack.length-1];
}
// Set children of parents
if (curNode.childNodes == null) {
curNode.childNodes = [node];
curNode.firstChild = node;
} else {
curNode.childNodes.push(node);
}
// Set siblings
if (curNode.lastChild) {
curNode.lastChild.nextSibling = node;
node.previousSibling = curNode.lastChild;
}
curNode.lastChild = node;
node.parentNode = curNode;
// Update stack
if (node.parentIndex >= curNode.index) {
// Open nodes
nodeStack.push(node);
curNode = node;
}
lastNode = node;
return true;
});
}
// Functions to make basic content nodes
function addContentNode(document, content, parent)
{
var anon = new docNode(3, document);
if (parent.tagName == 'SPAN')
anon.nodeValue = content;
else
anon.nodeValue = content.trim();
anon.index = document.nodes.length;
anon.parentIndex = parent.index;
document.nodes.push(anon);
return anon;
}
function addRootNode(document, content, parent)
{
var anon = new docNode(1, document);
anon.nodeValue = content;
anon.index = document.nodes.length;
anon.parentIndex = -1;
document.nodes.push(anon);
return anon;
}
function addDocAttrNode(document, content, parent)
{
var anon = new docNode(2, document);
anon.nodeValue = content.trim();
anon.index = document.nodes.length;
anon.parentIndex = parent.index;
document.nodes.push(anon);
return anon;
}
function addBodyNode(document, content, parent)
{
var anon = new docNode(9, document);
anon.nodeValue = content.trim();
anon.index = document.nodes.length;
anon.parentIndex = parent.index;
document.nodes.push(anon);
return anon;
}
function addDummyNode(document, content, parent)
{
var anon = new docNode(1, document);
anon.nodeType = 0;
anon.nodeValue = content.trim();
anon.index = document.nodes.length;
anon.parentIndex = parent.index;
document.nodes.push(anon);
return anon;
}
var makeTagFuncs = {
'HTML': addRootNode,
'TITLE': addDocAttrNode,
'BODY': addBodyNode,
'P': addDummyNode,
'B': addDummyNode,
'SPAN': addDummyNode,
};
var HTMLCommentStrip = new RegExp("<![^>]*>"); // a bit simple, but works for now
// Parses document
//
// e.g. "<html><head><title></title></head><body><p class="top">FOO <b>woo</b></p><p>Foo 2</p></body></html>"
//
// [["html", "", 0, -1],
// ["head", "", 1, 0],
// ["title", "", 2, 1],
// ["body", "", 3, 0],
// ["p", "FOO ", 4, 3],
// ["b", "woo", 5, 4],
// ["p", "Foo 2", 6, 3],
// ]
//
function parse(doc) {
var tagParse = new RegExp("</?([A-Za-z]*)( ([a-zA-Z0-9_-]*=(\".*\")|('.*') ?)*)?>", "");
var rawDoc = doc.replace(HTMLCommentStrip, "");
var startIDX = 0;
var curStr = doc;
var nodeStack = [];
var genDoc = new docHTML();
var len = rawDoc.length;
var topNode = null;
// Search for tags...
while (startIDX != -1 && startIDX < len) {
var searchStr = rawDoc.substr(startIDX);
var nextTag = searchStr.search(tagParse);
//debugLog("SS:" + searchStr);
if (nextTag != -1) {
// Found tag, start wih content + name
var content = searchStr.substr(0, nextTag); // existing node content
searchStr = searchStr.substr(nextTag);
var tagName = searchStr.match("</?([A-Za-z]*)")[1].toUpperCase();
var isClosing = searchStr.indexOf('/') == 1;
//debugLog("Found tag " + tagName);
//debugLog("CONTENT:" + content);
//debugLog("CLOSING:" + isClosing);
// Advance next search pos
startIDX += nextTag + searchStr.indexOf('>') + 1;
// Insert content node
if (topNode != 0 && content.length > 0) {
addContentNode(genDoc, content, topNode);
}
// Open or close nodes
if (!isClosing) {
// Add node
var tagFunc = makeTagFuncs[tagName];
if (tagFunc)
topNode = tagFunc(genDoc, tagName, topNode);
else
topNode = addDummyNode(genDoc, tagName, topNode)
topNode.tagName = tagName;
// Parse attributes
var rg = new RegExp("([A-Za-z0-9_-]*)=((?:\"[^\"]*)|(?:'[^']*))", "g");
var scans = searchStr.substr(1, searchStr.indexOf('>')-1);
var attrs = null;
while ((attrs = rg.exec(scans)) != null) {
topNode.attributes[attrs[1]] = attrs[2].substr(1);
}
nodeStack.push(topNode);
} else {
// Close matching node
var closedNodeIDX = findOpenNode(nodeStack, tagName);
//debugLog("CLOSED IDX==" + closedNodeIDX);
//debugLog(nodeStack);
if (closedNodeIDX >= 0) {
nodeStack = nodeStack.slice(0, closedNodeIDX);
}
if (nodeStack.length > 0)
topNode = nodeStack[nodeStack.length-1];
else
topNode = null;
continue;
}
} else // no more tags
break;
}
debugLog("Generated Doc:");
debugLog(genDoc);
// Find useful data
genDoc.body = findBody(genDoc.nodes);
genDoc.head = genDoc.nodes[0].getFirstElementByTagName('HEAD');
if (genDoc.head) {
var title = genDoc.nodes[0].getFirstElementByTagName('TITLE');
if (title) {
title = title.getFirstElementByTagName('*');
if (title)
genDoc.title = title.nodeValue;
}
}
return genDoc;
}
// CSS Parser
var CSSCommentStrip = new RegExp("/\\*[^\\*]*\\*/");
function stripCSSComments(str) {
return str.replace(CSSCommentStrip, "");
}
function parseCSSSelectors(doc) {
// chunker taken from sizzle, (C) Copyright 2009, The Dojo Foundation
var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]*\]|['"][^'"]*['"]|[^[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?/g;
var selectorList = [];
var elements = [];
var nextElement;
var lastElement = null;
var matchParent = false;
var matchSibling = false;
while ((nextElement = chunker.exec(doc)) != null) {
// TODO: hook into an existing CSS selector lib
var rule = nextElement[1];
elements.push(rule);
if (nextElement[2] && nextElement[2].substr(0,1) == ',') {
// Selector ended
selectorList.push(elements);
elements = [];
}
}
// Final element?
if (elements.length != 0)
selectorList.push(elements);
return selectorList;
}
function parseCSSProperties(doc) {
var nameParse = new RegExp("([A-Za-z0-9\-_]+):", "g");
var propParse = /((?:'[^']*')|(?:"[^"]*")|[^;}])*/;
var propertyList = {};
var nextName;
while ((nextName = nameParse.exec(doc)) != null) {
var name = nextName[1];
var foundProp = propParse.exec(doc.substr(nameParse.lastIndex));
propertyList[name] = foundProp[0]; // TODO: parse value -> values
nextName.lastIndex = foundProp.index + foundProp[0].length + 1;
}
return propertyList;
}
function parseCSS(doc) {
var rawDoc = stripCSSComments(doc);
var blockParse = /(?:(@[a-zA-z]+)\s((?:[A-Za-z]+)|(?:'[^']*')|(?:"[^"]*")|(?:(?:[A-Za-z]*)\([^\)]*\)));)|({[^{]*})/g;
var startIDX = 0;
var len = rawDoc.length;
// TODO: handle blocks better
while (startIDX != -1 && startIDX < len) {
var nextBlock = blockParse.exec(rawDoc);
if (nextBlock != null) {
if (nextBlock[1]) {
// key [1]
// value [2]
// TODO
console.log("RULE: " + nextBlock[1] + ',' + nextBlock[2]);
} else if (nextBlock[3]) {
// Normal selector block
var selectors = parseCSSSelectors(rawDoc.substr(startIDX, nextBlock.index-startIDX));
var blockStr = rawDoc.substr(nextBlock.index+1, blockParse.lastIndex-nextBlock.index-1);
debugLog("BLOCK:"+blockStr);
var properties = parseCSSProperties(blockStr);
debugLog('++');
debugLog(selectors);
debugLog("--");
debugLog(properties);
debugLog('++');
}
startIDX = blockParse.lastIndex;
} else
break;
}
}
// Parse and load a sample document
var doc = "<html><head><title>Test</title></head><body><!-- Begin test --><p class=\"woo\" id=\"render\" style=\"display:none;\">Rendering <b>HTML</b>...</p><p><span>In <b>Canvas</b></span>!</p><p>0_0</p></body></html>";
var parsedDoc = parse(doc);
populateDOM(parsedDoc.nodes[0]);
render(parsedDoc);
debugLog("Node function test");
debugLog(parsedDoc.nodes[0].getElementsByTagName('BODY'));
debugLog(parsedDoc.nodes[0].getElementById('render'));
debugLog(parsedDoc.nodes[0].getElementById('render').getElementsByTagName('*'));
debugLog(parsedDoc.nodes[0].getElementsByTagName('*'));
// Test parse CSS
debugLog("Parse css test");
parseCSS("@test testValue; div[foo=lol][goo=boo] > p { foo: 1; woo:\"goo\"; } .wii { text-align: center }");
}());