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