source: gs3-extensions/html-to-expeditee/trunk/src/src/js/html-to-expeditee.js@ 24925

Last change on this file since 24925 was 24925, checked in by davidb, 12 years ago

Shift from generating string to JSON tree for output

  • Property svn:executable set to *
File size: 3.6 KB
Line 
1//javascript:
2var expSyntaxArray = [];
3
4Element.prototype.getElementWidth = function() {
5 if (typeof this.clip !== "undefined") {
6 return this.clip.width;
7 } else {
8 if (this.style.pixelWidth) {
9 return this.style.pixelWidth;
10 } else {
11 return this.offsetWidth;
12 }
13 }
14};
15
16Element.prototype.getElementHeight = function() {
17 if (typeof this.clip !== "undefined") {
18 return this.clip.height;
19 } else {
20 if (this.style.pixelHeight) {
21 return this.style.pixelHeight;
22 } else {
23 return this.offsetHeight;
24 }
25 }
26};
27
28function getElementWidth(elem)
29{
30 if (typeof elem.clip !== "undefined") {
31 return elem.clip.width;
32 } else {
33 if (elem.style.pixelWidth) {
34 return elem.style.pixelWidth;
35 } else {
36 return elem.offsetWidth;
37 }
38 }
39}
40
41function getElementHeight(elem)
42{
43 if (typeof elem.clip !== "undefined") {
44 return elem.clip.height;
45 } else {
46 if (elem.style.pixelHeight) {
47 return elem.style.pixelHeight;
48 } else {
49 return elem.offsetHeight;
50 }
51 }
52}
53
54function getStyle(el,styleProp)
55{
56 var x = document.getElementById(el);
57 if (x.currentStyle) {
58 var y = x.currentStyle[styleProp];
59 }
60 else if (window.getComputedStyle) {
61 var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
62 }
63 return y;
64}
65
66function getElementPosition(elem)
67{
68 var origElem = elem;
69
70 var xl_ = 0;
71 var yt_ = 0;
72
73 while(elem && !isNaN(elem.offsetLeft) && !isNaN(elem.offsetTop)) {
74 xl_ += elem.offsetLeft - elem.scrollLeft;
75 yt_ += elem.offsetTop - elem.scrollTop;
76 elem = elem.offsetParent;
77 }
78
79 var xr_ = xl_ + getElementWidth(origElem);
80 var yb_ = yt_ + getElementHeight(origElem);
81
82 return { xl: xl_, yt: yt_, xr: xr_, yb: yb_ };
83}
84
85
86
87function htmlToExpeditee(node,depth)
88{
89 depth = (depth) ? depth : 0;
90
91 //var expSyntax = "";
92 var jsonNode = {};
93
94 if (node.nodeType == 3) { /* text node */
95 var text = node.nodeValue;
96
97 if (text.match(/\S/)) {
98 //for (var i=0; i<depth; i++) { expSyntax += " "; }
99 //expSyntax += "Text item: " + node.nodeValue + "\n";
100
101 jsonNode.type = "text";
102 jsonNode.text = node.nodeValue;
103 }
104 }
105 else if (node.nodeType == 1) { /* element */
106
107 /* need to handle: img, a, li */
108 /* need to dig out: text size, l/r/justified, font-face, type, colour */
109
110 var elemName = node.Name;
111 if (elemName != "SCRIPT") {
112
113
114 var nodePos = getElementPosition(node);
115 var xl = nodePos.xl;
116 var xr = nodePos.xr;
117 var yt = nodePos.yt;
118 var yb = nodePos.yb;
119
120/*
121 var rectSyntax = "";
122
123 for (var i=0; i<depth; i++) { rectSyntax += " "; }
124
125 rectSyntax += "Rect item " + node.nodeName + ": ";
126 rectSyntax += xl + " " + yt + " " + xr + " " + yb + "\n";
127
128 expSyntax += rectSyntax;
129*/
130
131 var rect = { "xl":xl, "yt":yt, "xr":xr, "yb":yb };
132
133 jsonNode.type = "rect";
134 jsonNode.elem = node.nodeName;
135 jsonNode.rect = rect;
136
137 var jsonChildNodes = [];
138
139 var cnode = node.firstChild;
140 while (cnode != null) {
141 //expSyntax += htmlToExpeditee(cnode,depth+1);
142
143 var jsonChildNode = htmlToExpeditee(cnode,depth+1);
144 jsonChildNodes.push(jsonChildNode);
145
146 cnode = cnode.nextSibling;
147 }
148
149 jsonNode.childNodes = jsonChildNodes;
150 }
151
152 }
153 /* currently do nothing for the other node types */
154
155 return jsonNode;
156 //return expSyntax;
157}
158
159
160//var body = document.body;
161//alert(traverseDOM(body));
Note: See TracBrowser for help on using the repository browser.