1 | //javascript:
|
---|
2 | var expSyntaxArray = [];
|
---|
3 |
|
---|
4 | var font_family;
|
---|
5 | var font_size;
|
---|
6 | var font_color;
|
---|
7 | var font_weight;
|
---|
8 | var font_style;
|
---|
9 |
|
---|
10 | var compute_font = false;
|
---|
11 | var compute_width = false;
|
---|
12 |
|
---|
13 | Element.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 |
|
---|
25 | Element.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 |
|
---|
37 | function 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 |
|
---|
50 | function 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 |
|
---|
63 | function hyphenToInitialCapReplacer(str, p1, offset, s)
|
---|
64 | {
|
---|
65 | return p1.toUpperCase();
|
---|
66 | }
|
---|
67 |
|
---|
68 | function 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 |
|
---|
90 | function 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 |
|
---|
110 | function 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 |
|
---|
126 | var pxl = nodePos.xl;
|
---|
127 | var pyt = nodePos.yt;
|
---|
128 | var pxr = nodePos.xr;
|
---|
129 | var pyb = nodePos.yb;
|
---|
130 |
|
---|
131 | return htmlToExpediteeRec(node, pxl,pyt, pxl,pyt,pxr,pyb, 0);
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | function htmlToExpediteeRec(node,baseX,baseY, pxl,pyt,pxr,pyb, depth)
|
---|
136 | {
|
---|
137 | depth = (depth) ? depth : 0;
|
---|
138 |
|
---|
139 | var jsonNode = null;
|
---|
140 |
|
---|
141 | if (node.nodeType == 3) { /* text node */
|
---|
142 |
|
---|
143 |
|
---|
144 | var text = node.nodeValue;
|
---|
145 |
|
---|
146 | if (text.match(/\S/)) {
|
---|
147 | jsonNode = {};
|
---|
148 |
|
---|
149 | jsonNode.type = "text";
|
---|
150 | jsonNode.text = node.nodeValue;
|
---|
151 |
|
---|
152 | jsonNode.xl = pxl;
|
---|
153 | jsonNode.yt = pyt;
|
---|
154 | jsonNode.xr = pxr;
|
---|
155 | jsonNode.yb = pyb;
|
---|
156 |
|
---|
157 | var link = node.link;
|
---|
158 | if(link !== null && link !== undefined){
|
---|
159 | jsonNode.link = link;
|
---|
160 | console.log("Adding link");
|
---|
161 | }else{
|
---|
162 | console.log("no link found");
|
---|
163 | }
|
---|
164 |
|
---|
165 | var parent = node.parentNode;
|
---|
166 |
|
---|
167 | if (parent != null) {
|
---|
168 |
|
---|
169 | if(compute_font){
|
---|
170 | //obtain font info for text nodes from their parent nodes.
|
---|
171 | var new_font_family = getStyle(parent,"font-family");
|
---|
172 | var new_font_size = getStyle(parent,"font-size");
|
---|
173 | var new_font_color = getStyle(parent,"color");
|
---|
174 |
|
---|
175 | var new_font_weight = getStyle(parent,"font-weight");
|
---|
176 | var new_font_style = getStyle(parent,"font-style");
|
---|
177 |
|
---|
178 | if(new_font_family !== undefined && new_font_family !== null)
|
---|
179 | font_family = new_font_family;
|
---|
180 |
|
---|
181 | if(new_font_size !== undefined && new_font_size !== null)
|
---|
182 | font_size = new_font_size;
|
---|
183 |
|
---|
184 | if(new_font_color !== undefined && new_font_color !== null)
|
---|
185 | font_color = new_font_color;
|
---|
186 |
|
---|
187 | //TODO: if parent node is a <b> element, then set style["font-weight"] to be "bold".
|
---|
188 | if(new_font_weight !== undefined && new_font_weight !== null)
|
---|
189 | font_weight = new_font_weight;
|
---|
190 |
|
---|
191 | if(new_font_style !== undefined && new_font_style !== null)
|
---|
192 | font_style = new_font_style;
|
---|
193 |
|
---|
194 | var style = {};
|
---|
195 | style["font-family"] = font_family;
|
---|
196 | style["font-size"] = font_size;
|
---|
197 | style["color"] = font_color;
|
---|
198 | style["font-weight"] = font_weight;
|
---|
199 | style["font-style"] = font_style;
|
---|
200 |
|
---|
201 | jsonNode.style = style;
|
---|
202 | }
|
---|
203 |
|
---|
204 | if(compute_width){
|
---|
205 | var parentPos = getElementPosition(parent);
|
---|
206 | var parentWidth = parentPos.xr - parentPos.xl;
|
---|
207 |
|
---|
208 | jsonNode.width = parentWidth;
|
---|
209 | }
|
---|
210 |
|
---|
211 | var data = parent.attributes["data"];
|
---|
212 |
|
---|
213 | if (data != null) {
|
---|
214 | jsonNode.data = data.value;
|
---|
215 | }
|
---|
216 |
|
---|
217 |
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 |
|
---|
222 |
|
---|
223 | }
|
---|
224 | else if (node.nodeType == 1) { /* element */
|
---|
225 |
|
---|
226 | /* need to handle: img, a, li */
|
---|
227 | /* need to dig out: text size, l/r/justified, font-face, type, colour */
|
---|
228 |
|
---|
229 | if (getStyle(node,"visibility").match("hidden")
|
---|
230 | || getStyle(node,"display").match("none")) {
|
---|
231 |
|
---|
232 | return null;
|
---|
233 | }
|
---|
234 |
|
---|
235 | var elemName = node.nodeName.toLowerCase();
|
---|
236 |
|
---|
237 | if (!elemName.match(/script/)) {
|
---|
238 |
|
---|
239 | var nodePos = getElementPosition(node);
|
---|
240 | var xl = nodePos.xl;
|
---|
241 | var xr = nodePos.xr;
|
---|
242 | var yt = nodePos.yt;
|
---|
243 | var yb = nodePos.yb;
|
---|
244 |
|
---|
245 | jsonNode = {};
|
---|
246 | jsonNode.type = "rect";
|
---|
247 | jsonNode.elem = node.nodeName;
|
---|
248 |
|
---|
249 | if(node.getAttribute("rect") !== undefined){
|
---|
250 |
|
---|
251 | /**This is to ensure rectangles aren't drawn around "non-rectangular" items such
|
---|
252 | as text and empty elements such as <br/>**/
|
---|
253 | if(node.getAttribute("rect") === "norect"){
|
---|
254 | jsonNode.type = "norect";
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | var rect = { "xl":xl, "yt":yt, "xr":xr, "yb":yb };
|
---|
259 | jsonNode.rect = rect;
|
---|
260 |
|
---|
261 | if (elemName.match("img")) {
|
---|
262 | jsonNode.img = node.src;
|
---|
263 | }
|
---|
264 |
|
---|
265 | var style = {};
|
---|
266 |
|
---|
267 | //style["font-family"] = getStyle(node,"font-family");
|
---|
268 | //style["font-size"] = getStyle(node,"font-size");
|
---|
269 | style["background-color"] = getStyle(node,"background-color");
|
---|
270 |
|
---|
271 | jsonNode.style = style;
|
---|
272 |
|
---|
273 | var attr = node.attributes["attr"];
|
---|
274 | if (attr != null) {
|
---|
275 | // console.log("attr = " + attr.value);
|
---|
276 | jsonNode.attr = attr.value;
|
---|
277 | }
|
---|
278 |
|
---|
279 |
|
---|
280 | var jsonChildNodes = [];
|
---|
281 |
|
---|
282 | var cnode = node.firstChild;
|
---|
283 | while (cnode != null) {
|
---|
284 |
|
---|
285 | var jsonChildNode
|
---|
286 | = htmlToExpediteeRec(cnode,baseX,baseY,xl,yt,xr,yb,depth+1);
|
---|
287 |
|
---|
288 | if (jsonChildNode != null) {
|
---|
289 | jsonChildNodes.push(jsonChildNode);
|
---|
290 | }
|
---|
291 |
|
---|
292 | cnode = cnode.nextSibling;
|
---|
293 | }
|
---|
294 |
|
---|
295 | jsonNode.childNodes = jsonChildNodes;
|
---|
296 | }
|
---|
297 |
|
---|
298 | }
|
---|
299 | /* currently do nothing for the other node types */
|
---|
300 |
|
---|
301 | return jsonNode;
|
---|
302 | }
|
---|
303 |
|
---|
304 |
|
---|
305 | //var body = document.body;
|
---|
306 | //alert(traverseDOM(body)); |
---|