jablonka.czprosek.czf

websvn

Subversion Repositories:
[/] [templates/] [calm/] [star-light/] [src/] [moz-behaviors.xml] - Blame information for rev 2

 

Line No. Rev Author Line
11simandl<?xml version="1.0" encoding="ISO-8859-1"?>
2<bindings xmlns="http://www.mozilla.org/xbl" xmlns:html="http://www.w3.org/1999/xhtml">
3<!--
4 moz-behaviors.xml - version 1.1 (pre-release) (2005/05/15)
5 Copyright 2004-2005, Dean Edwards
6 Web: http://dean.edwards.name/moz-behaviors/
7 
8 This software is licensed under the CC-GNU LGPL
9 Web: http://creativecommons.org/licenses/LGPL/2.1/
10-->
11<!--
12=======================================================================
13 TO DO
14=======================================================================
15 
16hideFocus
17fix CSSStyleDeclaration pixel*.__defineGetter__
18behaviors FAQ
19test Event.returnValue
20 
21=======================================================================
22 DONE
23=======================================================================
24 
25styleFloat
26allow upper case tags/attributes
27support for external JS files
28better support for oncontentready/ondocumentready
29support for multiple behaviors in css
30ActiveXObject
31 
32=======================================================================
33-->
34 
35<!-- add a behavior through css -->
36<binding id="behavior" extends="#-moz-behaviors">
37<implementation><constructor>
38<![CDATA[
39addBehavior(0);
40]]></constructor></implementation>
41</binding>
42 
43<binding id="-moz-behaviors" extends="#-moz-ie">
44<implementation><constructor>
45<![CDATA[if(!this.addBehavior){
46// ------------------------------------------------------------------
47// htc extensions for mozilla
48// ------------------------------------------------------------------
49 
50/* here we define the addBehavior/removeBehavior methods for an element
51 these methods are used to add and remove all dhtml behaviors
52*/
53 
54var _cookie = -1; // no support for removeBehavior yet
55 
56// implement the addBehavior method for all elements
57Element.prototype.addBehavior = function(_url) {
58try {
59 // calling this method with the first argument as zero
60 // initialises the object's behaviors
61 if (_url === 0) {
62 // grab the htc's url from the css setting
63 var $binding = getComputedStyle(this, null).getPropertyValue("-moz-binding");
64 $binding = $binding.replace(/^url\(([^)]*)\)$/, "$1").split("#");
65 if ($binding) {
66 var $path = $binding[0].replace(/[^\/]+$/, "");
67 // support multiple behaviors
68 var $htcs = $binding[1].split("|");
69 var i = $htcs.length;
70 while (i--) this.addBehavior($path + $htcs[i]);
71 }
72 }
73 if (!_url) return;
74 
75 // check the cache
76 if (!document.behaviorUrns[_url]) {
77 // constants
78 var $SEPARATOR = ";";
79 
80 function _getTagName($node) {
81 var $tagName = $node.tagName.toLowerCase();
82 // this fixes a bug(?) in Mozilla 1.6b that includes the
83 // namespace prefix in the tagName
84 return $tagName.slice($tagName.indexOf(":") + 1);
85 };
86 
87 function _getAttribute($node, $attribute) {
88 return $node.getAttribute($attribute) || $node.getAttribute($attribute.toUpperCase());
89 };
90 
91 // this function converts elements in a behavior to a program
92 // declaration, for example:
93 // <public:attach for="window" event="onload" handler="init"/>
94 // becomes:
95 // window.addEventListener("load", init);
96 function _asDeclaration($behaviorNode) {
97 switch (_getTagName($behaviorNode)) {
98 case "event":
99 var id = _getAttribute($behaviorNode, "id");
100 return (id) ? "var " + id + "={fire:function(event){element.fireEvent('" +
101 _getAttribute($behaviorNode, "name") + "',event)}}" : "";
102 case "property":
103 var $name = _getAttribute($behaviorNode, "name");
104 var $get = _getAttribute($behaviorNode, "get") || "function(){return getAttribute('" +
105 $name + "')}";
106 $get = "__defineGetter__('" + $name + "'," + $get + ")";
107 var $put = _getAttribute($behaviorNode, "put") || "";
108 if ($put) $put += ".call(element,value);";
109 $put = "function(value){" + $put + "setAttribute('" + $name + "',value)}";
110 $put = "__defineSetter__('" + $name + "'," + $put + ")";
111 return $get + $SEPARATOR + $put;
112 case "method":
113 return "element." + _getAttribute($behaviorNode, "name") + "=" + _getAttribute($behaviorNode, "name");
114 case "attach":
115 var $handler = _getAttribute($behaviorNode, "handler") || "";
116 $handler += ($handler) ? "()" : _getAttribute($behaviorNode, "onevent");
117 $handler = "function(event){window.event=event;return " + $handler + "}";
118 var $event = _getAttribute($behaviorNode, "event");
119 switch ($event) {
120 case "oncontentready": return "window.setTimeout(" + $handler + ",1)";
121 case "ondocumentready": return "document.behaviorUrns.__private.push(" + $handler + ")";
122 }
123 return (_getAttribute($behaviorNode, "for")||"element") + ".addEventListener('" + $event.slice(2) + "'," + $handler + ",false)";
124 case "defaults":
125 // not implemented
126 default:
127 return "";
128 }
129 };
130 
131 function _asDefault($node) {
132 return (_getAttribute($node, "put")) ? ";var __tmp=getAttribute('" + _getAttribute($node, "name") + "')||" +
133 (_getAttribute($node, "value") || "null") +
134 ";if(__tmp!=null)element['" + _getAttribute($node, "name") + "']=__tmp" : "";
135 };
136 
137 // extract the body of a function
138 function _getFunctionBody($function) {
139 with (String($function)) return slice(indexOf("{") + 1, lastIndexOf("}"));
140 };
141 
142 // behaviors are defined as xml documents, so we can use
143 // the http request object to load them and the dom parser
144 // object to parse them into a dom tree
145 var _httpRequest = new XMLHttpRequest;
146 function _loadFile($url) {
147 try {
148 // load the behavior
149 _httpRequest.open("GET", $url, false);
150 _httpRequest.send(null);
151 return _httpRequest.responseText;
152 } catch ($ignore) {
153 // ignore (but don't crash)
154 }};
155 
156 // analyse the dom tree, build the interface and create the script
157 var _declarations = [];
158 var _defaults = "";
159 var _script = "";
160 function _load() {
161 // build a dom representation of the loaded xml document
162 var $dom = (new DOMParser).parseFromString(_loadFile(_url), "text/xml");
163 var $childNodes = $dom.documentElement.childNodes, $node;
164 for (var i = 0; ( $node = $childNodes[i]); i++) {
165 if ($node.nodeType == Node.ELEMENT_NODE) {
166 if (_getTagName($node) == "script") {
167 var $src = _getAttribute($node, "src");
168 if ($src) {
169 _script += _loadFile($src);
170 } else {
171 // build the script from the text nodes of the script element
172 for (var j = 0; j < $node.childNodes.length; j++)
173 _script += $node.childNodes[j].nodeValue;
174 }
175 } else {
176 // convert the dom node representation of a
177 // <public:declaration/> to a javascript statement
178 // and store it in our declarations collection
179 _declarations.push(_asDeclaration($node));
180 if (_getTagName($node) == "property") {
181 _defaults += _asDefault($node);
182 }
183 }
184 }
185 }
186 _defaults += ";delete __tmp";
187 };
188 _load();
189 // we've finished collecting interface declarations.
190 // they are now held as an array of strings.
191 
192 // build a function from the script and extract the function body
193 // this has the effect of formatting the script (removing comments etc)
194 _script = _getFunctionBody(new Function(_script));
195 
196 // support: new ActiveXObject
197 var $ACTIVEX = /\bnew\s+ActiveXObject\s*\(\s*(["'])\w\.XMLHTTP\1\s*\)/gi;
198 _script = _script.replace($ACTIVEX, "new XMLHttpRequest()");
199 
200 // begin: annoying parse of script to "shuffle" declarations
201 // and inline code.
202 // microsoft dhtml behaviors add the interface first, then
203 // apply inline script.
204 // to achieve this, we have to strip out all of the inline
205 // code, leaving only function declarations. the inline code
206 // then gets appended to the script block for later
207 // execution.
208 // in between the function declarations and inline script, we
209 // sandwich the property getters and setters.
210 // this is a real nuisance actually...
211 
212 // on the upside regular expressions are really quick...
213 
214 // i'm using "#" as a placeholder, so i'll have to escape these out
215 _script = _script.replace(/#/g, "\\x23");
216 
217 // parse out strings, regexps and program
218 // blocks - anything between curly braces {..}
219 var $ = [_declarations.join($SEPARATOR)];
220 var $BLOCKS_REGEXPS_STRINGS = /(\"[^\"\n]+\")|(\/[^\/\n]+\/)|(\{[^\{\}]*\})/g;
221 var _ENCODED = /#(\d+)\b/g;
222 // store a string and return a unique id
223 function _encode($match) {return "#" + $.push($match)};
224 function _decode($match, $index) {return $[$index - 1]};
225 while ($BLOCKS_REGEXPS_STRINGS.test(_script)) {
226 _script = _script.replace($BLOCKS_REGEXPS_STRINGS, _encode);
227 }
228 // we are now left with function declarations and inline statements
229 
230 // remove function declarations and save them
231 var $FUNCTIONS = /\n\s*function[^\n]*\n/g;
232 var _functions = _script.match($FUNCTIONS) || [];
233 _script = _script.replace($FUNCTIONS, "");
234 
235 // re-assemble the encoded script, in the following
236 // order: function declarations, interface definition
237 // (getters and setters), inline script
238 _script = _functions.concat("#1", _script).join($SEPARATOR);
239 
240 // decode the script
241 var i = $.length;
242 do _script = _script.replace("#" + i, $[--i]); while (i);
243 // end: annoying parse of script
244 
245 // build the final script
246 _script += _defaults;
247 
248 // create an anonymous function in the global namespace.
249 // this function will add the interface defined by the dhtml behavior.
250 // after we've built this function we'll store it so that we don't
251 // have to go through this process again.
252 document.behaviorUrns[_url] = new Function("element", "with(this){" + _script + "}");
253 }
254 
255 // because we loaded synchronously (or got it from the cache)
256 // we can apply the behavior immediately...
257 document.behaviorUrns[_url].call(this, this);
258 
259 // this might mean somthing later
260 return _cookie;
261} catch ($error) {
262 return 0;
263}};
264 
265// implement the removeBehavior method for all elements
266Element.prototype.removeBehavior = function($cookie) {
267 // mmm, not in a hurry to write this
268};
269 
270// cache for previously loaded behaviors
271// -also store some "default" behaviors
272document.behaviorUrns = {
273 __private : []
274};
275 
276// support multiple behaviors and ondocumentready
277window.addEventListener("load", function() {
278try {
279 var $handlers = document.behaviorUrns.__private;
280 var i = $handlers.length;
281 while (i) $handlers[--i]();
282 delete document.behaviorUrns.__private;
283} catch ($ignore) {
284}}, false);
285 
286}]]></constructor></implementation>
287</binding>
288 
289<binding id="-moz-ie">
290<implementation><constructor>
291<![CDATA[if(!this.attachEvent){
292// ------------------------------------------------------------------
293// explorer emulation for mozilla
294// ------------------------------------------------------------------
295 
296// thanks to Erik Arvidsson (http://webfx.eae.net/dhtml/ieemu/)
297 
298/* we're going to mess about with some of mozilla's interfaces to
299 make them more explorer-like
300*/
301 
302/* note: in my comments where i say support/mimic a property
303 support = exactly the same as explorer
304 mimic = close enough
305*/
306 
307// CSSStyleDeclaration
308// -------------------
309// support microsoft's styleFloat
310CSSStyleDeclaration.prototype.__defineGetter__("styleFloat", function() {
311 return this.cssFloat;
312});
313CSSStyleDeclaration.prototype.__defineSetter__("styleFloat", function($value) {
314 this.cssFloat = $value;
315});
316// mimic microsoft's pixel representations of left/top/width/height
317// the getters only work for values that are already pixels
318CSSStyleDeclaration.prototype.__defineGetter__("pixelLeft", function() {
319 return parseInt(this.left) || 0;
320});
321CSSStyleDeclaration.prototype.__defineSetter__("pixelLeft", function($value) {
322 this.left = $value + "px";
323});
324CSSStyleDeclaration.prototype.__defineGetter__("pixelHeight", function() {
325 return parseInt(this.height) || 0;
326});
327CSSStyleDeclaration.prototype.__defineSetter__("pixelHeight", function($value) {
328 this.height = $value + "px";
329});
330CSSStyleDeclaration.prototype.__defineGetter__("pixelTop", function() {
331 return parseInt(this.top) || 0;
332});
333CSSStyleDeclaration.prototype.__defineSetter__("pixelTop", function($value) {
334 this.top = $value + "px";
335});
336CSSStyleDeclaration.prototype.__defineGetter__("pixelWidth", function() {
337 return parseInt(this.width) || 0;
338});
339CSSStyleDeclaration.prototype.__defineSetter__("pixelWidth", function($value) {
340 this.width = $value + "px";
341});
342 
343// for older versions of gecko we need to use getPropertyValue() to
344// access css properties returned by getComputedStyle().
345// we don't want this so we fix it.
346try {
347var $computedStyle = getComputedStyle(this, null);
348// the next line will throw an error for some versions of mozilla
349var $test = $computedStyle.display;
350} catch ($ignore) {
351// the previous line will throw an error for some versions of mozilla
352} finally {
353if (!$test) {
354 // the above code didn't work so we need to fix CSSStyleDeclaration
355 var $UPPER = /[A-Z]/g;
356 function _dashLower($match){return "-" + $match.toLowerCase()};
357 function _cssName($propertyName) {return $propertyName.replace($UPPER, _dashLower)};
358 for (var $propertyName in this.style) {
359 if (typeof this.style[$propertyName] == "string") {
360 CSSStyleDeclaration.prototype.__defineGetter__($propertyName, function() {
361 return this.getPropertyValue(_cssName($propertyName));
362 });
363 }
364 }
365}}
366 
367// HTMLDocument
368// ------------
369// support microsoft's "all" property
370HTMLDocument.prototype.__defineGetter__("all", function() {
371 return this.getElementsByTagName("*");
372});
373// mimic the "createEventObject" method for the document object
374HTMLDocument.prototype.createEventObject = function() {
375 return document.createEvent("Events");
376};
377 
378// HTMLElement
379// -----------
380// mimic microsoft's "all" property
381HTMLElement.prototype.__defineGetter__("all", function() {
382 return this.getElementsByTagName("*");
383});
384// support "parentElement"
385HTMLElement.prototype.__defineGetter__("parentElement", function() {
386 return (this.parentNode == this.ownerDocument) ? null : this.parentNode;
387});
388// support "uniqueID"
389HTMLElement.prototype.__defineGetter__("uniqueID", function() {
390 // a global counter is stored privately as a property of this getter function.
391 // initialise the counter
392 if (!arguments.callee.count) arguments.callee.count = 0;
393 // create the id and increment the counter
394 var $uniqueID = "moz_id" + arguments.callee.count++;
395 // creating a unique id, creates a global reference
396 window[$uniqueID] = this;
397 // we don't want to increment next time, so redefine the getter
398 this.__defineGetter__("uniqueID", function(){return $uniqueID});
399 return $uniqueID;
400});
401// mimic microsoft's "currentStyle"
402HTMLElement.prototype.__defineGetter__("currentStyle", function() {
403 return getComputedStyle(this, null);
404});
405// mimic microsoft's "runtimeStyle"
406HTMLElement.prototype.__defineGetter__("runtimeStyle", function() {
407//# this doesn't work yet (https://bugzilla.mozilla.org/show_bug.cgi?id=45424)
408//# return this.ownerDocument.defaultView.getOverrideStyle(this, null);
409 return this.style;
410});
411// support "innerText"
412HTMLElement.prototype.__defineGetter__("innerText", function() {
413 return this.textContent;
414});
415HTMLElement.prototype.__defineSetter__("innerText", function($value) {
416 this.textContent = $value;
417});
418// mimic the "attachEvent" method
419HTMLElement.prototype.attachEvent = function($name, $handler) {
420 this.addEventListener($name.slice(2), $handler, false);
421};
422// mimic the "removeEvent" method
423HTMLElement.prototype.removeEvent = function($name, $handler) {
424 this.removeEventListener($name.slice(2), $handler, false);
425};
426// mimic the "createEventObject" method
427HTMLElement.prototype.createEventObject = function() {
428 return this.ownerDocument.createEventObject();
429};
430// mimic the "fireEvent" method
431HTMLElement.prototype.fireEvent = function($name, $event) {
432 if (!$event) $event = this.ownerDocument.createEventObject();
433 $event.initEvent($name.slice(2), false, false);
434 this.dispatchEvent($event);
435 // not sure that this should be here??
436 if (typeof this[$name] == "function") this[$name]();
437 else if (this.getAttribute($name)) eval(this.getAttribute($name));
438};
439// support the "contains" method
440HTMLElement.prototype.contains = function($element) {
441 return Boolean($element == this || ($element && this.contains($element.parentElement)));
442};
443 
444// Event
445// -----
446// support microsoft's proprietary event properties
447Event.prototype.__defineGetter__("srcElement", function() {
448 return (this.target.nodeType == Node.ELEMENT_NODE) ? this.target : this.target.parentNode;
449});
450Event.prototype.__defineGetter__("fromElement",function() {
451 return (this.type == "mouseover") ? this.relatedTarget : (this.type == "mouseout") ? this.srcElement : null;
452});
453Event.prototype.__defineGetter__("toElement", function() {
454 return (this.type == "mouseout") ? this.relatedTarget : (this.type == "mouseover") ? this.srcElement : null;
455});
456// convert w3c button id's to microsoft's
457Event.prototype.__defineGetter__("button", function() {
458 return (this.which == 1) ? 1 : (this.which == 2) ? 4 : 2;
459});
460// mimic "returnValue" (default is "true")
461Event.prototype.__defineGetter__("returnValue", function() {
462 return true;
463});
464Event.prototype.__defineSetter__("returnValue", function($value) {
465 if (this.cancelable && !$value) {
466 // this can't be undone!
467 this.preventDefault();
468 this.__defineGetter__("returnValue", function() {
469 return false;
470 });
471 }
472});
473// mozilla already supports the read-only "cancelBubble"
474// so we only need to define the setter
475Event.prototype.__defineSetter__("cancelBubble", function($value) {
476 // this can't be undone!
477 if ($value) this.stopPropagation();
478});
479Event.prototype.__defineGetter__("offsetX", function() {
480 return this.layerX;
481});
482Event.prototype.__defineGetter__("offsetY", function() {
483 return this.layerY;
484});
485// and that's it!
486// thanks mozilla for being such a developer's playground :D
487}]]></constructor></implementation>
488</binding>
489 
490<binding id="block-netscape6">
491<content>
492<html:script type="text/javascript"><![CDATA[
493// netscape6 does not retain the -moz-binding css property value
494// so we disable moz-behaviors
495if (/netscape6/i.test(navigator.userAgent)) {
496 document.styleSheets[0].insertRule("*{-moz-binding:none!important}", 0);
497}
498]]></html:script>
499<children/>
500</content>
501</binding>
502 
503<!-- manually attach behaviors to child elements of <table>s -->
504<binding id="table">
505<implementation><constructor><![CDATA[
506 var i, j;
507 if (tHead) tHead.addBehavior(0);
508 for (i = 0; i < tBodies.length; i++) tBodies[i].addBehavior(0);
509 for (i = 0; i < rows.length; i++) {
510 for (j = 0; j < rows[i].cells.length; j++) {
511 rows[i].cells[j].addBehavior(0);
512 }
513 }
514 if (tFoot) tFoot.addBehavior(0);
515]]></constructor></implementation>
516</binding>
517 
518</bindings>
519 

Powered by WebSVN 2.2.1