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

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

Now correctly accounts for bold, italic and bold-italic text.

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