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

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

Additional javascript code needed for extension

  • Property svn:executable set to *
File size: 3.1 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.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.pixelHeight) {
47 return elem.style.pixelHeight;
48 } else {
49 return elem.offsetHeight;
50 }
51 }
52}
53
54function getStyle(el,styleProp)
55{
56 var x = document.getElementById(el);
57 if (x.currentStyle) {
58 var y = x.currentStyle[styleProp];
59 }
60 else if (window.getComputedStyle) {
61 var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
62 }
63 return y;
64}
65
66function getElementPosition(elem)
67{
68 var origElem = elem;
69
70 var xl_ = 0;
71 var yt_ = 0;
72
73 while(elem && !isNaN(elem.offsetLeft) && !isNaN(elem.offsetTop)) {
74 xl_ += elem.offsetLeft - elem.scrollLeft;
75 yt_ += elem.offsetTop - elem.scrollTop;
76 elem = elem.offsetParent;
77 }
78
79 var xr_ = xl_ + getElementWidth(origElem);
80 var yb_ = yt_ + getElementHeight(origElem);
81
82 return { xl: xl_, yt: yt_, xr: xr_, yb: yb_ };
83}
84
85
86
87function htmlToExpeditee(node,depth)
88{
89 depth = (depth) ? depth : 0;
90
91 var expSyntax = "";
92
93 if (node.nodeType == 3) { /* text node */
94 var text = node.nodeValue;
95
96 if (text.match(/\S/)) {
97 for (var i=0; i<depth; i++) { expSyntax += " "; }
98 expSyntax += "Text item: " + node.nodeValue + "\n";
99 }
100 }
101 else if (node.nodeType == 1) { /* element */
102
103 /* need to handle: img, a, li */
104 /* need to dig out: text size, l/r/justified, font-face, type, colour */
105
106 var elemName = node.Name;
107 if (elemName != "SCRIPT") {
108
109
110 var nodePos = getElementPosition(node);
111 var xl = nodePos.xl;
112 var xr = nodePos.xr;
113 var yt = nodePos.yt;
114 var yb = nodePos.yb;
115
116 var rectSyntax = "";
117 for (var i=0; i<depth; i++) { rectSyntax += " "; }
118 rectSyntax += "Rect item " + node.nodeName + ": ";
119 rectSyntax += xl + " " + yt + " " + xr + " " + yb + "\n";
120
121 expSyntax += rectSyntax;
122
123 var cnode = node.firstChild;
124 while (cnode != null) {
125 expSyntax += htmlToExpeditee(cnode,depth+1);
126
127 cnode = cnode.nextSibling ;
128 }
129
130 }
131
132 }
133 /* currently do nothing for the other node types */
134
135 return expSyntax;
136}
137
138
139//var body = document.body;
140//alert(traverseDOM(body));
Note: See TracBrowser for help on using the repository browser.