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

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

More careful recursive traversal to classifier nodes; further developed of CSS to Expeditee attributes (background colour); and slightly less cludgy way to deal with full img URLS

  • Property svn:executable set to *
File size: 4.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 && 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 && elem.style.pixelHeight) {
47 return elem.style.pixelHeight;
48 } else {
49 return elem.offsetHeight;
50 }
51 }
52}
53
54function hyphenToInitialCapReplacer(str, p1, offset, s)
55{
56 return p1.toUpperCase();
57}
58
59function getStyle(node,style_prop)
60{
61 var prop_val;
62 if (node.currentStyle) {
63 /* IE */
64 style_prop = strCssRule.replace(/\-(\w)/g, hyphenToInitialCapReplacer);
65 prop_val = node.currentStyle[style_prop];
66 }
67 else if (window.getComputedStyle) {
68 /* Firefox */
69 var computed_style = document.defaultView.getComputedStyle(node,null)
70 prop_val = computed_style.getPropertyValue(style_prop);
71 }
72 else {
73 /* try for inline value */
74 prop_val = el.style[style_prop];
75 }
76
77 return prop_val;
78
79}
80
81function getElementPosition(elem)
82{
83 var origElem = elem;
84
85 var xl_ = 0;
86 var yt_ = 0;
87
88 while(elem && !isNaN(elem.offsetLeft) && !isNaN(elem.offsetTop)) {
89 xl_ += elem.offsetLeft - elem.scrollLeft;
90 yt_ += elem.offsetTop - elem.scrollTop;
91 elem = elem.offsetParent;
92 }
93
94 var xr_ = xl_ + getElementWidth(origElem);
95 var yb_ = yt_ + getElementHeight(origElem);
96
97 return { xl: xl_, yt: yt_, xr: xr_, yb: yb_ };
98}
99
100
101function htmlToExpeditee(node)
102{
103 var nodePos = getElementPosition(node);
104 var pxl = nodePos.xl;
105 var pyt = nodePos.yt;
106 var pxr = nodePos.xr;
107 var pyb = nodePos.yb;
108
109 return htmlToExpediteeRec(node, pxl,pyt, pxl,pyt,pxr,pyb, 0);
110}
111
112
113function htmlToExpediteeRec(node,baseX,baseY, pxl,pyt,pxr,pyb, depth)
114{
115 depth = (depth) ? depth : 0;
116
117 var jsonNode = null;
118
119 if (node.nodeType == 3) { /* text node */
120 var text = node.nodeValue;
121
122 if (text.match(/\S/)) {
123 jsonNode = {};
124
125 jsonNode.type = "text";
126 jsonNode.text = node.nodeValue;
127
128 jsonNode.xl = pxl;
129 jsonNode.yt = pyt;
130 jsonNode.xr = pxr;
131 jsonNode.yb = pyb;
132
133 }
134 }
135 else if (node.nodeType == 1) { /* element */
136
137 /* need to handle: img, a, li */
138 /* need to dig out: text size, l/r/justified, font-face, type, colour */
139
140 if (getStyle(node,"visibility").match("hidden")
141 || getStyle(node,"display").match("none")) {
142
143 return null;
144 }
145
146 var elemName = node.nodeName.toLowerCase();
147
148 if (!elemName.match(/script/)) {
149
150 var nodePos = getElementPosition(node);
151 var xl = nodePos.xl;
152 var xr = nodePos.xr;
153 var yt = nodePos.yt;
154 var yb = nodePos.yb;
155
156 jsonNode = {};
157 jsonNode.type = "rect";
158 jsonNode.elem = node.nodeName;
159
160 var rect = { "xl":xl, "yt":yt, "xr":xr, "yb":yb };
161 jsonNode.rect = rect;
162
163 if (elemName.match("img")) {
164 jsonNode.img = node.src;
165 }
166
167 var style = {};
168
169 style["font-family"] = getStyle(node,"font-family");
170 style["font-size"] = getStyle(node,"font-size");
171 style["background-color"] = getStyle(node,"background-color");
172
173 // console.log("background color = " + style["background-color"]);
174
175 // console.log("font size = " + style["font-size"]);
176
177 jsonNode.style = style
178
179 var jsonChildNodes = [];
180
181 var cnode = node.firstChild;
182 while (cnode != null) {
183
184 var jsonChildNode
185 = htmlToExpediteeRec(cnode,baseX,baseY,xl,yt,xr,yb,depth+1);
186
187 if (jsonChildNode != null) {
188 jsonChildNodes.push(jsonChildNode);
189 }
190
191 cnode = cnode.nextSibling;
192 }
193
194 jsonNode.childNodes = jsonChildNodes;
195 }
196
197 }
198 /* currently do nothing for the other node types */
199
200 return jsonNode;
201}
202
203
204//var body = document.body;
205//alert(traverseDOM(body));
Note: See TracBrowser for help on using the repository browser.