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

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

Now correctly obtaining font information for text nodes - this required getting the style information from the text node's parent element.

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