/
dom.js
executable file
·320 lines (300 loc) · 9.24 KB
/
dom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/**
* @provides FB.Dom
* @layer Basic
* @requires FB.Base
*/
/**
* This provides helper methods related to DOM
* @class FB.Dom
* @private
*/
FB.provide('Dom', {
/**
* @param {Object} dom
* @param {String} newClass
* @static
*/
addCss: function(dom, newClass) {
var cssClassWithSpace = ' ' + dom.className + ' ';
var newClassWithSpace = ' ' + newClass + ' ';
if (cssClassWithSpace.indexOf(newClassWithSpace) < 0) {
dom.className = dom.className + ' ' + newClass;
}
},
/**
* @param {Object} dom
* @param {String} className
* @return Boolean
* @static
*/
containsCss: function(dom, className) {
var cssClassWithSpace = ' ' + dom.className + ' ';
return cssClassWithSpace.indexOf(' ' + className + ' ') >= 0;
},
/**
* @param {Object} dom
* @param {String} className
* @static
*/
removeCss: function(dom, className) {
var cssClassWithSpace = ' ' + dom.className + ' ';
var classNameWithSpace = ' ' + className + ' ';
var index = cssClassWithSpace.indexOf(classNameWithSpace);
if (index >= 0) {
var newClassName = cssClassWithSpace.substring(1, index) + cssClassWithSpace.substring(index + classNameWithSpace.length, cssClassWithSpace.length - 1);
dom.className = newClassName;
}
},
/**
* Create an unique DOM id
* @return {string} an unique DOM id
* @static
*/
createId: function() {
if (!FB.Dom._domId) {
FB.Dom._domId = 1;
}
return '_fb_dom_' + FB.Dom_domId++;
},
/**
* Cross browser way to add event listener to a DOM element, window or document object.
* @param {Object} element
* @param {String} type
* @param {Function} handler
*
* handler should be a function with no parameters.
* @static
*/
addEventListener: function(element, type, handler) {
if (element.addEventListener) {
element.addEventListener(type, handler, false);
}
else {
handler._ieEventHandler = function() {
handler(window.event);
};
element.attachEvent('on' + type, handler._ieEventHandler);
}
},
/*
* Get a hidden DOM container element. This is used to store hidden
* iframes.
* @static
*/
getHidden: function() {
if (FB.$('FB_HiddenContainer') == null) {
var target = document.body;
// For IE, we can't already insert a new element into
// document.body because IE throw operation aborted error
// when document is not completely parsed and a script
// attempts to add, or remove an element from an unclosed ancestor
// in the markup tree (not including the script block's
// immediate parent element).
//
// To get around the problem, we will first attempt to find
// an element where we can insert a <span> element and the element
// doesn't have any children that contains HTML which could contains
// a script that would execute this very function.
//
// I find this algorithm hard to explain (a possible indication of
// flaw in my design :-(). So here are some examples:
//
// <body>
// <p id='good_candidate'>foo</p>
// <script>
// FB.Dom.getHidden();
// <script>
// </body>
//
// <body>
// <div>
// <table>
// </table>
// <div>
// <div id='good_candiate'></div>
// </div>
// </div>
// <div>
// <script>
// FB.Dom.getHidden();
// </script>
// </div>
// </body>
//
// NOTE: The container is a <span>, not a <div> and it cannot contains
// divs as children. This is because the container may be inserted inside
// an inline element like <p>
//
if (FB.Dom.getBrowserType() == 'ie') {
var fn = function(dom) {
var children = dom.children;
excludeTags = {TABLE:1, TR:1, THEAD:1, TBODY:1, TFOOT:1};
if (children.length > 0) {
for (var i=0; i < children.length; i++) {
var child = children[i];
if (child.canHaveChildren && child.canHaveHTML &&
!excludeTags[child.tagName]) {
// div into child. Either the child itself or
// a node inside it must have
// have the right condition
return fn(child);
}
}
}
// If we reach this point, then the dom
// itself is the right candidate
return dom;
}
target = fn(target);
}
var hiddenDiv = document.createElement('span');
hiddenDiv.id = "FB_HiddenContainer";
var s = hiddenDiv.style;
s.position = "absolute";
s.top = "-10000px";
s.width = "0px";
s.height = "0px";
s.display = "block";
return target.appendChild(hiddenDiv);
}
return FB.$('FB_HiddenContainer');
},
/**
* Create a hidden iframe
* @static
*/
createHiddenIFrame: function(src) {
var receiverDom = document.createElement('iframe');
receiverDom.className = 'FB_RECEIVER_DOM';
var span = FB.Dom.getHidden().appendChild(document.createElement('span'));
// There is IE bug with iframe cache that we have to work around:
// Dynamically load the iframe to dummy content before loading the real content, as shown below.
// This works because the cached stream that exists after a refresh is consumed by the initial
// dummy load, and the second load fetches the content as expected.
// Must be javascript:false instead of about:blank, otherwise IE6 will complain in https
if (FB.Dom.getBrowserType() == 'ie') {
span.innerHTML = '<iframe src=\'javascript:false\' ></iframe>';
span.innerHTML = '<iframe name = "' + FB.Util.createUnique() + '" src=\"' + src + '"></iframe>';
} else {
span.innerHTML = '<iframe name = "' + FB.Util.createUnique() + '"></iframe>';
span.childNodes[0].src = src;
}
return span.childNodes[0];
},
/**
* Dynamically add a script tag
* @static
*/
addScript: function(src) {
var script = document.createElement('script');
script.type = "text/javascript";
script.src = src;
return document.getElementsByTagName('HEAD')[0].appendChild(script);
},
addCssRules: function(s, id) {
if (!FB.Dom._cssRules) {
FB.Dom._cssRules = {};
}
// Check if this style sheet is already applied
if (id in FB.Dom._cssRules) {
return;
}
FB.Dom._cssRules[id] = true;
if (FB.Dom.getBrowserType() != 'ie') {
style = document.createElement('style');
style.type = "text/css";
style.innerHTML = s;
document.getElementsByTagName('HEAD')[0].appendChild(style);
} else {
var re = /([\w|#|\.|\\][^{]*){(.*?)}/mg,
a,
style = document.createStyleSheet();
while (a = re.exec(s)) {
var rules = FB.Util.a2a(a[1].split(','), FB.Util.trim);
for (var i=0; i < rules.length; i++) {
style.addRule(rules[i], a[2]);
}
}
}
},
/**
* Get location of the browser window relative to computer screen
* windowLocation
* @return FB.Point
* @static
*/
getWindowLocation: function() {
var lc = {x:0, y:0};
var l, t;
if(window.screenLeft) {
l = window.screenLeft;
t = window.screenTop;
} else {
l = window.screenX;
t = window.screenY;
}
lc.x = l;
lc.y = t;;
// We need to check for undefined because undefined does not
// behave the same as 0 in math operations.
if (lc.x === undefined) {
lc.x = 0;
}
if (lc.y === undefined) {
lc.y = 0;
}
return lc;
},
/**
* Get browser window size
* windowSize
* @return FB.Size
* @static
*/
getWindowSize: function() {
return {w: (window && window.innerWidth) ||
(document && document.documentElement
&& document.documentElement.clientWidth) ||
(document && document.body && document.body.clientWidth) ||
0,
h: (window && window.innerHeight) ||
(document && document.documentElement
&& document.documentElement.clientHeight) ||
(document && document.body && document.body.clientHeight) ||
0};
},
/**
* Remove a dom element from dom tree
* @param {Object} element
* @static
*/
removeDom: function(element) {
if (element) {
var parent = element.parentNode;
if (parent) {
parent.removeChild(element);
}
}
},
/**
* Get browser type
* @return string 'ie' | 'mozilla' |'safari' | 'other'
* @static
*/
getBrowserType: function() {
if (!FB.Dom._browserType) {
var userAgent = window.navigator.userAgent.toLowerCase();
// list of known browser and
keys = ['msie', 'firefox', 'gecko', 'safari'];
names = ['ie', 'mozilla', 'mozilla', 'safari'];
this.hostName = 'other';
for(var i = 0; i < keys.length; i++) {
if (userAgent.indexOf(keys[i]) >= 0) {
FB.Dom._browserType = names[i];
break;;
}
}
}
return FB.Dom._browserType;
}
});