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

Last change on this file since 24939 was 24939, checked in by davidb, 12 years ago

Some initial work at converting CSS style information into Expeditee attributes

  • Property svn:executable set to *
File size: 3.8 KB
Line 
1//javascript:
2var expSyntaxArray = [];
3
4Element.prototype.getElementWidth = function() {
5 if (typeof this.clip !== "undefined") {
6 return this.clip.width;
7 } else {
8 if (this.style.pixelWidth) {
9 return this.style.pixelWidth;
10 } else {
11 return this.offsetWidth;
12 }
13 }
14};
15
16Element.prototype.getElementHeight = function() {
17 if (typeof this.clip !== "undefined") {
18 return this.clip.height;
19 } else {
20 if (this.style.pixelHeight) {
21 return this.style.pixelHeight;
22 } else {
23 return this.offsetHeight;
24 }
25 }
26};
27
28function getElementWidth(elem)
29{
30 if (typeof elem.clip !== "undefined") {
31 return elem.clip.width;
32 } else {
33 if (elem.style && elem.style.pixelWidth) {
34 return elem.style.pixelWidth;
35 } else {
36 return elem.offsetWidth;
37 }
38 }
39}
40
41function getElementHeight(elem)
42{
43 if (typeof elem.clip !== "undefined") {
44 return elem.clip.height;
45 } else {
46 if (elem.style && elem.style.pixelHeight) {
47 return elem.style.pixelHeight;
48 } else {
49 return elem.offsetHeight;
50 }
51 }
52}
53
54function getStyle(node,styleProp)
55{
56 if (node.currentStyle) {
57 var y = node.currentStyle[styleProp];
58 }
59 else if (window.getComputedStyle) {
60 var y = document.defaultView.getComputedStyle(node,null).getPropertyValue(styleProp);
61 }
62 return y;
63}
64
65function getElementPosition(elem)
66{
67 var origElem = elem;
68
69 var xl_ = 0;
70 var yt_ = 0;
71
72 while(elem && !isNaN(elem.offsetLeft) && !isNaN(elem.offsetTop)) {
73 xl_ += elem.offsetLeft - elem.scrollLeft;
74 yt_ += elem.offsetTop - elem.scrollTop;
75 elem = elem.offsetParent;
76 }
77
78 var xr_ = xl_ + getElementWidth(origElem);
79 var yb_ = yt_ + getElementHeight(origElem);
80
81 return { xl: xl_, yt: yt_, xr: xr_, yb: yb_ };
82}
83
84
85function htmlToExpeditee(node)
86{
87 var nodePos = getElementPosition(node);
88 var pxl = nodePos.xl;
89 var pyt = nodePos.yt;
90 var pxr = nodePos.xr;
91 var pyb = nodePos.yb;
92
93 return htmlToExpediteeRec(node, pxl,pyt, pxl,pyt,pxr,pyb, 0);
94}
95
96
97function htmlToExpediteeRec(node,baseX,baseY, pxl,pyt,pxr,pyb, depth)
98{
99 depth = (depth) ? depth : 0;
100
101 var jsonNode = null;
102
103 if (node.nodeType == 3) { /* text node */
104 var text = node.nodeValue;
105
106 if (text.match(/\S/)) {
107 jsonNode = {};
108
109 jsonNode.type = "text";
110 jsonNode.text = node.nodeValue;
111
112 jsonNode.x = pxl;
113 jsonNode.y = pyt;
114
115 }
116 }
117 else if (node.nodeType == 1) { /* element */
118
119 /* need to handle: img, a, li */
120 /* need to dig out: text size, l/r/justified, font-face, type, colour */
121
122 var elemName = node.nodeName.toLowerCase();
123
124 if (!elemName.match(/script/)) {
125
126 var nodePos = getElementPosition(node);
127 var xl = nodePos.xl;
128 var xr = nodePos.xr;
129 var yt = nodePos.yt;
130 var yb = nodePos.yb;
131
132 jsonNode = {};
133 jsonNode.type = "rect";
134 jsonNode.elem = node.nodeName;
135
136 var rect = { "xl":xl, "yt":yt, "xr":xr, "yb":yb };
137 jsonNode.rect = rect;
138
139
140 var style = {};
141
142 style["font-family"] = getStyle(node,"font-family");
143 style["background-color"] = getStyle(node,"background-color");
144
145 jsonNode.style = style
146
147 var jsonChildNodes = [];
148
149 var cnode = node.firstChild;
150 while (cnode != null) {
151
152 var jsonChildNode
153 = htmlToExpediteeRec(cnode,baseX,baseY,xl,yt,xr,yb,depth+1);
154
155 if (jsonChildNode != null) {
156 jsonChildNodes.push(jsonChildNode);
157 }
158
159 cnode = cnode.nextSibling;
160 }
161
162 jsonNode.childNodes = jsonChildNodes;
163 }
164
165 }
166 /* currently do nothing for the other node types */
167
168 return jsonNode;
169}
170
171
172//var body = document.body;
173//alert(traverseDOM(body));
Note: See TracBrowser for help on using the repository browser.