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

Last change on this file since 26731 was 26731, checked in by davidb, 11 years ago

Can now correctly obtain colours and size of text when generating Expeditee frames. Also accounted for when the web browser being used is Firefox (FF uses "700" instead of the word "bold" for font-weight). The width of text items is also calculated and written to Expeditee. Parameters have also been provided so the user can decide whether they want the correct font information and text width used or to just use default values.

  • Property svn:executable set to *
File size: 7.4 KB
Line 
1//javascript:
2var expSyntaxArray = [];
3
4var font_family;
5var font_size;
6var font_color;
7var font_weight;
8var font_style;
9
10var compute_font = false;
11var compute_width = false;
12
13Element.prototype.getElementWidth = function() {
14 if (typeof this.clip !== "undefined") {
15 return this.clip.width;
16 } else {
17 if (this.style.pixelWidth) {
18 return this.style.pixelWidth;
19 } else {
20 return this.offsetWidth;
21 }
22 }
23};
24
25Element.prototype.getElementHeight = function() {
26 if (typeof this.clip !== "undefined") {
27 return this.clip.height;
28 } else {
29 if (this.style.pixelHeight) {
30 return this.style.pixelHeight;
31 } else {
32 return this.offsetHeight;
33 }
34 }
35};
36
37function getElementWidth(elem)
38{
39 if (typeof elem.clip !== "undefined") {
40 return elem.clip.width;
41 } else {
42 if (elem.style && elem.style.pixelWidth) {
43 return elem.style.pixelWidth;
44 } else {
45 return elem.offsetWidth;
46 }
47 }
48}
49
50function getElementHeight(elem)
51{
52 if (typeof elem.clip !== "undefined") {
53 return elem.clip.height;
54 } else {
55 if (elem.style && elem.style.pixelHeight) {
56 return elem.style.pixelHeight;
57 } else {
58 return elem.offsetHeight;
59 }
60 }
61}
62
63function hyphenToInitialCapReplacer(str, p1, offset, s)
64{
65 return p1.toUpperCase();
66}
67
68function getStyle(node,style_prop)
69{
70 var prop_val;
71 if (node.currentStyle) {
72 /* IE */
73 style_prop = strCssRule.replace(/\-(\w)/g, hyphenToInitialCapReplacer);
74 prop_val = node.currentStyle[style_prop];
75 }
76 else if (window.getComputedStyle) {
77 /* Firefox */
78 var computed_style = document.defaultView.getComputedStyle(node,null)
79 prop_val = computed_style.getPropertyValue(style_prop);
80 }
81 else {
82 /* try for inline value */
83 prop_val = el.style[style_prop];
84 }
85
86 return prop_val;
87
88}
89
90function getElementPosition(elem)
91{
92 var origElem = elem;
93
94 var xl_ = 0;
95 var yt_ = 0;
96
97 while(elem && !isNaN(elem.offsetLeft) && !isNaN(elem.offsetTop)) {
98 xl_ += elem.offsetLeft - elem.scrollLeft;
99 yt_ += elem.offsetTop - elem.scrollTop;
100 elem = elem.offsetParent;
101 }
102
103 var xr_ = xl_ + getElementWidth(origElem);
104 var yb_ = yt_ + getElementHeight(origElem);
105
106 return { xl: xl_, yt: yt_, xr: xr_, yb: yb_ };
107}
108
109
110function htmlToExpeditee(node,compFont,compWidth)
111{
112 compute_font = compFont;
113 compute_width = compWidth;
114
115 var nodePos = getElementPosition(node);
116 var parentStyle = node.getAttribute('style');
117
118 //Use these as default values if font info can't be found for text items.
119 //and only if the user wants the font computed (otherwise use Expeditee defaults).
120 if(compute_font){
121 font_family = getStyle(node,'font-family');
122 font_size = getStyle(node,'font-size');
123 font_color = getStyle(node,'color');
124
125 /*console.log("Retrieving node: " + node);
126
127 console.log("Retrieving font family: " + font_family);
128 console.log("Retrieving font size: " + font_size);
129 console.log("Retrieving font color: " + font_color);*/
130 }
131
132 var pxl = nodePos.xl;
133 var pyt = nodePos.yt;
134 var pxr = nodePos.xr;
135 var pyb = nodePos.yb;
136
137 return htmlToExpediteeRec(node, pxl,pyt, pxl,pyt,pxr,pyb, 0);
138}
139
140
141function htmlToExpediteeRec(node,baseX,baseY, pxl,pyt,pxr,pyb, depth)
142{
143 depth = (depth) ? depth : 0;
144
145 var jsonNode = null;
146
147 if (node.nodeType == 3) { /* text node */
148
149
150 var text = node.nodeValue;
151
152 if (text.match(/\S/)) {
153 jsonNode = {};
154
155 jsonNode.type = "text";
156 jsonNode.text = node.nodeValue;
157
158 jsonNode.xl = pxl;
159 jsonNode.yt = pyt;
160 jsonNode.xr = pxr;
161 jsonNode.yb = pyb;
162
163 var parent = node.parentNode;
164
165 if (parent != null) {
166
167 if(compute_font){
168 //obtain font info for text nodes from their parent nodes.
169 var new_font_family = getStyle(parent,"font-family");
170 var new_font_size = getStyle(parent,"font-size");
171 var new_font_color = getStyle(parent,"color");
172
173 var new_font_weight = getStyle(parent,"font-weight");
174 var new_font_style = getStyle(parent,"font-style");
175
176 if(new_font_family !== undefined && new_font_family !== null)
177 font_family = new_font_family;
178
179 if(new_font_size !== undefined && new_font_size !== null)
180 font_size = new_font_size;
181
182 if(new_font_color !== undefined && new_font_color !== null)
183 font_color = new_font_color;
184
185 //TODO: if parent node is a <b> element, then set style["font-weight"] to be "bold".
186 if(new_font_weight !== undefined && new_font_weight !== null)
187 font_weight = new_font_weight;
188
189 if(new_font_style !== undefined && new_font_style !== null)
190 font_style = new_font_style;
191
192 var style = {};
193 style["font-family"] = font_family;
194 style["font-size"] = font_size;
195 style["color"] = font_color;
196 style["font-weight"] = font_weight;
197 style["font-style"] = font_style;
198
199 jsonNode.style = style;
200 }
201
202 if(compute_width){
203 var parentPos = getElementPosition(parent);
204 var parentWidth = parentPos.xr - parentPos.xl;
205
206 /*console.log("parent.xl: " + parentPos.xl);
207 console.log("parent.xr: " + parentPos.xr);
208 console.log("parent width: " + parentWidth);*/
209
210 jsonNode.width = parentWidth;
211 }
212
213 var data = parent.attributes["data"];
214
215 if (data != null) {
216 jsonNode.data = data.value;
217 }
218
219
220 }
221 }
222
223
224
225 }
226 else if (node.nodeType == 1) { /* element */
227
228 /* need to handle: img, a, li */
229 /* need to dig out: text size, l/r/justified, font-face, type, colour */
230
231 if (getStyle(node,"visibility").match("hidden")
232 || getStyle(node,"display").match("none")) {
233
234 return null;
235 }
236
237 var elemName = node.nodeName.toLowerCase();
238
239 if (!elemName.match(/script/)) {
240
241 var nodePos = getElementPosition(node);
242 var xl = nodePos.xl;
243 var xr = nodePos.xr;
244 var yt = nodePos.yt;
245 var yb = nodePos.yb;
246
247 jsonNode = {};
248 jsonNode.type = "rect";
249 jsonNode.elem = node.nodeName;
250
251 if(node.getAttribute("rect") !== undefined){
252
253 /**This is to ensure rectangles aren't drawn around "non-rectangular" items such
254 as text and empty elements such as <br/>**/
255 if(node.getAttribute("rect") === "norect"){
256 jsonNode.type = "norect";
257 }
258 }
259
260 var rect = { "xl":xl, "yt":yt, "xr":xr, "yb":yb };
261 jsonNode.rect = rect;
262
263 if (elemName.match("img")) {
264 jsonNode.img = node.src;
265 }
266
267 var style = {};
268
269 //style["font-family"] = getStyle(node,"font-family");
270 //style["font-size"] = getStyle(node,"font-size");
271 style["background-color"] = getStyle(node,"background-color");
272
273 jsonNode.style = style;
274
275 var attr = node.attributes["attr"];
276 if (attr != null) {
277 // console.log("attr = " + attr.value);
278 jsonNode.attr = attr.value;
279 }
280
281
282 var jsonChildNodes = [];
283
284 var cnode = node.firstChild;
285 while (cnode != null) {
286
287 var jsonChildNode
288 = htmlToExpediteeRec(cnode,baseX,baseY,xl,yt,xr,yb,depth+1);
289
290 if (jsonChildNode != null) {
291 jsonChildNodes.push(jsonChildNode);
292 }
293
294 cnode = cnode.nextSibling;
295 }
296
297 jsonNode.childNodes = jsonChildNodes;
298 }
299
300 }
301 /* currently do nothing for the other node types */
302
303 return jsonNode;
304}
305
306
307//var body = document.body;
308//alert(traverseDOM(body));
Note: See TracBrowser for help on using the repository browser.