diff options
Diffstat (limited to 'dom/tests/mochitest/ajax')
191 files changed, 49567 insertions, 0 deletions
diff --git a/dom/tests/mochitest/ajax/jquery/ChangeLog.txt b/dom/tests/mochitest/ajax/jquery/ChangeLog.txt new file mode 100644 index 0000000000..6d834c1967 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/ChangeLog.txt @@ -0,0 +1,98 @@ +== jQuery ChangeLog == + +== 1.2.2 == + +* show is now element aware (uses default display type instead of just forcing block) +* New special events api: jQuery.events.special +* ready is now a real event and can be bound, unbound and triggered. +* mouseenter and mouseleave now work on all supported browsers +* The hover helper method now uses the mouseenter and mouseleave events +* New offset test suite test/offset.html (requires your pop-up blocker to be disabled) +* Refactored the width and height methods (fixes lots of bugs) +* Fixed event memory leaks in IE (html, remove, empty also no longer leak memory) +* Fixed window/document width/height values +* Fixed event.pageX and event.pageY in IE + +== 1.2 == + +=== 1.1.3 === +* Always create an ActiveXObject when it is available instead of the XMLHttpRequest, even in IE7 +* Removed XMLHttpRequest shadowing, may break code that relies on existence of that function for browser checking +* ... + +=== 1.1.2 === + +* Event handlers (like element.onclick) are now removed when no more functions are bound to the event. +* Fixed DOM Manipulations for form elements. +* Fixed jQuery.isFunction to return false on nodes. +* Fixed jQuery.className.has, escaping regex characters in className (for metadata) +* Fixed an issue in IE where an event on a cloned element is fired during a .clone() inside of an event handler. +* Fixed IE ID selectors selecting by the name attribute. +* Change: Events are now internally stored in elem.$events rather than elem.events (due to a nasty bug relating to DOM 0 expandos). +* .attr('href') is now consistent in all browsers. +* @href is now consistent in all browsers. +* Fixed the slideDown flickering bug. +* Having a \r endline in $("...") caused a never-ending loop. +* Fixed IE6 AJAX memory leak +* Fixed bug in pushStack, reporting an element at [0] in a jQuery object with length 0 + +=== 1.1.1 === + +* Setting the numerical value of a css property failed, for example: .css("opacity",0.5) (also occurred with zIndex, fontWeight) +* Calling $(..., jqobj) with a context of a jQuery object failed. +* Accessing a property on an element that doesn't exist caused an error, for example: $("#foobar").attr("id") +* Calling .load() without a callback caused an error. +* You couldn't cancel an event that was triggered using .trigger() or .click() (for example). +* .add()ing a single DOM element to a jQuery object was broken. +* Passing in undefined values to a $.post() caused weird errors to occur. +* Accessing child nodes within an xml document didn't work properly. +* jQuery.isFunction() was unable to reliably determine a function, in a cross-browser way. +* Triggering a .click() failed in IE. +* Triggered click handlers were executed twice in most browsers. +* A newline passed into $(...) caused Firefox to go into a never-ending loop. +* Calling $.post() without any data caused an error. +* Calling a descendant selector after a child selector caused strange results, for example: $("ul > li ul") +* Triggered events did not occur if an event handler was not bound for that event. + +== 1.1 == + +* Massive speed-ups (4x-10x) in the selector engine. +* You can now unbind event handlers from within themselves +* Added new .one( "type", fn ) method +* text(String) now escapes HTML +* Added attr(String,Function) to calculate the value +* Performming .click(), .blur(), .focus(), .submit() will actually trigger the browsers default action for those events. +* Added global settings for AJAX (in addition to timeout), use $.ajaxSetup() to modify them +* Implemented a better error handling for ajax requests. Exceptions caused by dropping connections are now handled, too. +* Improved event fixing (Opera provides event.srcElement, must ignore it if target is available; only create pageX if clientX is available) +* Fixed nth-child selectors to start on the right number +* jQuery is no longer destructive. Doing var a = $("a"); a.find("span"); does not change the original "a" variable. +* Fixed synchronous requests +* Fixed ID with context selectors (eg. div #id doesn't ignore "div" anymore) +* Fixed docs for html(): Now mentions that is not available for XML documents +* Improved AJAX docs (eg. more examples for $.ajax) +* Documented filter(Function), a very powerful approach for custom filtering +* Improved docs for FX module, merging method descriptions and marking optional arguments +* Improved docs for append, prepend, before and after, merging the three pairs into one +* Improved show/hide animations to show only hidden and hide only visible elements +* Removed .oneEvent() and .unEvent() helper methods. +* Removed all CSS helper methods. +* Removed most attribute helper methods. +* Removed the (undocumented) .find( "selector", fn ) for all destructive methods. +* $.get, $.getIfModified, $.post, $.getScript and $.getJSON now all pass through the XMLHttpRequest as returned by $.ajax + +== 1.0.4 == + +* Tons of bug fixes +* Extensions to $.ajax: $.ajax accepts additonal options: beforeSend, async and processData; returns XMLHttpRequest to allow manual aborting of requests, see docs for details +* AJAX module: the public $.ajax API is now used internally (for $.get/$.post etc.); loading scripts works now much more reliable on all browers except Safari +* New global ajax handler: ajaxSend - called before an ajax request is sent +* Extensions to global ajax handlers: ajaxSend, ajaxSuccess, ajaxError and ajaxComplete get XMLHttpRequest and settings passed as arguments +* Extensions to event handling: pageX and pageY are available x-browser (IE does not provide native pageX/Y) +* Improved docs: $(String) method has now two seperate descriptions, one for selecting elements, one for creating (html on-the-fly) +* FX module: Most inline stlyes added by animations are now removed when the animation is complete, eg. height style when animating height (exception: display styles) +* Added note to attr(String, Object) about issues with setting the name property on input elements +* Seperated internal stuff from get() into set() +* Merged the two API examples for each() into one more precise example +* Improved docs for $.browser and added docs for $.boxModel +* Docs for the jQuery constructor $() were improved: There is now $(String expression[, Object context]) and $(String html) diff --git a/dom/tests/mochitest/ajax/jquery/GPL-LICENSE.txt b/dom/tests/mochitest/ajax/jquery/GPL-LICENSE.txt new file mode 100644 index 0000000000..11dddd00ef --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/GPL-LICENSE.txt @@ -0,0 +1,278 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. diff --git a/dom/tests/mochitest/ajax/jquery/MIT-LICENSE.txt b/dom/tests/mochitest/ajax/jquery/MIT-LICENSE.txt new file mode 100644 index 0000000000..d4215f0e42 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/MIT-LICENSE.txt @@ -0,0 +1,20 @@ +Copyright (c) 2008 John Resig, http://jquery.com/ + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/dom/tests/mochitest/ajax/jquery/dist/jquery.js b/dom/tests/mochitest/ajax/jquery/dist/jquery.js new file mode 100644 index 0000000000..a28e7cd65c --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/dist/jquery.js @@ -0,0 +1,3549 @@ +(function(){ +/* + * jQuery 1.2.6 - New Wave Javascript + * + * Copyright (c) 2008 John Resig (jquery.com) + * Dual licensed under the MIT (MIT-LICENSE.txt) + * and GPL (GPL-LICENSE.txt) licenses. + * + * $Date: 2008-05-24 11:09:21 -0700 (Sat, 24 May 2008) $ + * $Rev: 5683 $ + */ + +// Map over jQuery in case of overwrite +var _jQuery = window.jQuery, +// Map over the $ in case of overwrite + _$ = window.$; + +var jQuery = window.jQuery = window.$ = function( selector, context ) { + // The jQuery object is actually just the init constructor 'enhanced' + return new jQuery.fn.init( selector, context ); +}; + +// A simple way to check for HTML strings or ID strings +// (both of which we optimize for) +var quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#(\w+)$/, + +// Is it a simple selector + isSimple = /^.[^:#\[\.]*$/, + +// Will speed up references to undefined, and allows munging its name. + undefined; + +jQuery.fn = jQuery.prototype = { + init: function( selector, context ) { + // Make sure that a selection was provided + selector = selector || document; + + // Handle $(DOMElement) + if ( selector.nodeType ) { + this[0] = selector; + this.length = 1; + return this; + } + // Handle HTML strings + if ( typeof selector == "string" ) { + // Are we dealing with HTML string or an ID? + var match = quickExpr.exec( selector ); + + // Verify a match, and that no context was specified for #id + if ( match && (match[1] || !context) ) { + + // HANDLE: $(html) -> $(array) + if ( match[1] ) + selector = jQuery.clean( [ match[1] ], context ); + + // HANDLE: $("#id") + else { + var elem = document.getElementById( match[3] ); + + // Make sure an element was located + if ( elem ){ + // Handle the case where IE and Opera return items + // by name instead of ID + if ( elem.id != match[3] ) + return jQuery().find( selector ); + + // Otherwise, we inject the element directly into the jQuery object + return jQuery( elem ); + } + selector = []; + } + + // HANDLE: $(expr, [context]) + // (which is just equivalent to: $(content).find(expr) + } else + return jQuery( context ).find( selector ); + + // HANDLE: $(function) + // Shortcut for document ready + } else if ( jQuery.isFunction( selector ) ) + return jQuery( document )[ jQuery.fn.ready ? "ready" : "load" ]( selector ); + + return this.setArray(jQuery.makeArray(selector)); + }, + + // The current version of jQuery being used + jquery: "1.2.6", + + // The number of elements contained in the matched element set + size: function() { + return this.length; + }, + + // The number of elements contained in the matched element set + length: 0, + + // Get the Nth element in the matched element set OR + // Get the whole matched element set as a clean array + get: function( num ) { + return num == undefined ? + + // Return a 'clean' array + jQuery.makeArray( this ) : + + // Return just the object + this[ num ]; + }, + + // Take an array of elements and push it onto the stack + // (returning the new matched element set) + pushStack: function( elems ) { + // Build a new jQuery matched element set + var ret = jQuery( elems ); + + // Add the old object onto the stack (as a reference) + ret.prevObject = this; + + // Return the newly-formed element set + return ret; + }, + + // Force the current matched set of elements to become + // the specified array of elements (destroying the stack in the process) + // You should use pushStack() in order to do this, but maintain the stack + setArray: function( elems ) { + // Resetting the length to 0, then using the native Array push + // is a super-fast way to populate an object with array-like properties + this.length = 0; + Array.prototype.push.apply( this, elems ); + + return this; + }, + + // Execute a callback for every element in the matched set. + // (You can seed the arguments with an array of args, but this is + // only used internally.) + each: function( callback, args ) { + return jQuery.each( this, callback, args ); + }, + + // Determine the position of an element within + // the matched set of elements + index: function( elem ) { + var ret = -1; + + // Locate the position of the desired element + return jQuery.inArray( + // If it receives a jQuery object, the first element is used + elem && elem.jquery ? elem[0] : elem + , this ); + }, + + attr: function( name, value, type ) { + var options = name; + + // Look for the case where we're accessing a style value + if ( name.constructor == String ) + if ( value === undefined ) + return this[0] && jQuery[ type || "attr" ]( this[0], name ); + + else { + options = {}; + options[ name ] = value; + } + + // Check to see if we're setting style values + return this.each(function(i){ + // Set all the styles + for ( name in options ) + jQuery.attr( + type ? + this.style : + this, + name, jQuery.prop( this, options[ name ], type, i, name ) + ); + }); + }, + + css: function( key, value ) { + // ignore negative width and height values + if ( (key == 'width' || key == 'height') && parseFloat(value) < 0 ) + value = undefined; + return this.attr( key, value, "curCSS" ); + }, + + text: function( text ) { + if ( typeof text != "object" && text != null ) + return this.empty().append( (this[0] && this[0].ownerDocument || document).createTextNode( text ) ); + + var ret = ""; + + jQuery.each( text || this, function(){ + jQuery.each( this.childNodes, function(){ + if ( this.nodeType != 8 ) + ret += this.nodeType != 1 ? + this.nodeValue : + jQuery.fn.text( [ this ] ); + }); + }); + + return ret; + }, + + wrapAll: function( html ) { + if ( this[0] ) + // The elements to wrap the target around + jQuery( html, this[0].ownerDocument ) + .clone() + .insertBefore( this[0] ) + .map(function(){ + var elem = this; + + while ( elem.firstChild ) + elem = elem.firstChild; + + return elem; + }) + .append(this); + + return this; + }, + + wrapInner: function( html ) { + return this.each(function(){ + jQuery( this ).contents().wrapAll( html ); + }); + }, + + wrap: function( html ) { + return this.each(function(){ + jQuery( this ).wrapAll( html ); + }); + }, + + append: function() { + return this.domManip(arguments, true, false, function(elem){ + if (this.nodeType == 1) + this.appendChild( elem ); + }); + }, + + prepend: function() { + return this.domManip(arguments, true, true, function(elem){ + if (this.nodeType == 1) + this.insertBefore( elem, this.firstChild ); + }); + }, + + before: function() { + return this.domManip(arguments, false, false, function(elem){ + this.parentNode.insertBefore( elem, this ); + }); + }, + + after: function() { + return this.domManip(arguments, false, true, function(elem){ + this.parentNode.insertBefore( elem, this.nextSibling ); + }); + }, + + end: function() { + return this.prevObject || jQuery( [] ); + }, + + find: function( selector ) { + var elems = jQuery.map(this, function(elem){ + return jQuery.find( selector, elem ); + }); + + return this.pushStack( /[^+>] [^+>]/.test( selector ) || selector.indexOf("..") > -1 ? + jQuery.unique( elems ) : + elems ); + }, + + clone: function( events ) { + // Do the clone + var ret = this.map(function(){ + if ( jQuery.browser.msie && !jQuery.isXMLDoc(this) ) { + // IE copies events bound via attachEvent when + // using cloneNode. Calling detachEvent on the + // clone will also remove the events from the orignal + // In order to get around this, we use innerHTML. + // Unfortunately, this means some modifications to + // attributes in IE that are actually only stored + // as properties will not be copied (such as the + // the name attribute on an input). + var clone = this.cloneNode(true), + container = document.createElement("div"); + container.appendChild(clone); + return jQuery.clean([container.innerHTML])[0]; + } else + return this.cloneNode(true); + }); + + // Need to set the expando to null on the cloned set if it exists + // removeData doesn't work here, IE removes it from the original as well + // this is primarily for IE but the data expando shouldn't be copied over in any browser + var clone = ret.find("*").andSelf().each(function(){ + if ( this[ expando ] != undefined ) + this[ expando ] = null; + }); + + // Copy the events from the original to the clone + if ( events === true ) + this.find("*").andSelf().each(function(i){ + if (this.nodeType == 3) + return; + var events = jQuery.data( this, "events" ); + + for ( var type in events ) + for ( var handler in events[ type ] ) + jQuery.event.add( clone[ i ], type, events[ type ][ handler ], events[ type ][ handler ].data ); + }); + + // Return the cloned set + return ret; + }, + + filter: function( selector ) { + return this.pushStack( + jQuery.isFunction( selector ) && + jQuery.grep(this, function(elem, i){ + return selector.call( elem, i ); + }) || + + jQuery.multiFilter( selector, this ) ); + }, + + not: function( selector ) { + if ( selector.constructor == String ) + // test special case where just one selector is passed in + if ( isSimple.test( selector ) ) + return this.pushStack( jQuery.multiFilter( selector, this, true ) ); + else + selector = jQuery.multiFilter( selector, this ); + + var isArrayLike = selector.length && selector[selector.length - 1] !== undefined && !selector.nodeType; + return this.filter(function() { + return isArrayLike ? jQuery.inArray( this, selector ) < 0 : this != selector; + }); + }, + + add: function( selector ) { + return this.pushStack( jQuery.unique( jQuery.merge( + this.get(), + typeof selector == 'string' ? + jQuery( selector ) : + jQuery.makeArray( selector ) + ))); + }, + + is: function( selector ) { + return !!selector && jQuery.multiFilter( selector, this ).length > 0; + }, + + hasClass: function( selector ) { + return this.is( "." + selector ); + }, + + val: function( value ) { + if ( value == undefined ) { + + if ( this.length ) { + var elem = this[0]; + + // We need to handle select boxes special + if ( jQuery.nodeName( elem, "select" ) ) { + var index = elem.selectedIndex, + values = [], + options = elem.options, + one = elem.type == "select-one"; + + // Nothing was selected + if ( index < 0 ) + return null; + + // Loop through all the selected options + for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) { + var option = options[ i ]; + + if ( option.selected ) { + // Get the specifc value for the option + value = jQuery.browser.msie && !option.attributes.value.specified ? option.text : option.value; + + // We don't need an array for one selects + if ( one ) + return value; + + // Multi-Selects return an array + values.push( value ); + } + } + + return values; + + // Everything else, we just grab the value + } else + return (this[0].value || "").replace(/\r/g, ""); + + } + + return undefined; + } + + if( value.constructor == Number ) + value += ''; + + return this.each(function(){ + if ( this.nodeType != 1 ) + return; + + if ( value.constructor == Array && /radio|checkbox/.test( this.type ) ) + this.checked = (jQuery.inArray(this.value, value) >= 0 || + jQuery.inArray(this.name, value) >= 0); + + else if ( jQuery.nodeName( this, "select" ) ) { + var values = jQuery.makeArray(value); + + jQuery( "option", this ).each(function(){ + this.selected = (jQuery.inArray( this.value, values ) >= 0 || + jQuery.inArray( this.text, values ) >= 0); + }); + + if ( !values.length ) + this.selectedIndex = -1; + + } else + this.value = value; + }); + }, + + html: function( value ) { + return value == undefined ? + (this[0] ? + this[0].innerHTML : + null) : + this.empty().append( value ); + }, + + replaceWith: function( value ) { + return this.after( value ).remove(); + }, + + eq: function( i ) { + return this.slice( i, i + 1 ); + }, + + slice: function() { + return this.pushStack( Array.prototype.slice.apply( this, arguments ) ); + }, + + map: function( callback ) { + return this.pushStack( jQuery.map(this, function(elem, i){ + return callback.call( elem, i, elem ); + })); + }, + + andSelf: function() { + return this.add( this.prevObject ); + }, + + data: function( key, value ){ + var parts = key.split("."); + parts[1] = parts[1] ? "." + parts[1] : ""; + + if ( value === undefined ) { + var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]); + + if ( data === undefined && this.length ) + data = jQuery.data( this[0], key ); + + return data === undefined && parts[1] ? + this.data( parts[0] ) : + data; + } else + return this.trigger("setData" + parts[1] + "!", [parts[0], value]).each(function(){ + jQuery.data( this, key, value ); + }); + }, + + removeData: function( key ){ + return this.each(function(){ + jQuery.removeData( this, key ); + }); + }, + + domManip: function( args, table, reverse, callback ) { + var clone = this.length > 1, elems; + + return this.each(function(){ + if ( !elems ) { + elems = jQuery.clean( args, this.ownerDocument ); + + if ( reverse ) + elems.reverse(); + } + + var obj = this; + + if ( table && jQuery.nodeName( this, "table" ) && jQuery.nodeName( elems[0], "tr" ) ) + obj = this.getElementsByTagName("tbody")[0] || this.appendChild( this.ownerDocument.createElement("tbody") ); + + var scripts = jQuery( [] ); + + jQuery.each(elems, function(){ + var elem = clone ? + jQuery( this ).clone( true )[0] : + this; + + // execute all scripts after the elements have been injected + if ( jQuery.nodeName( elem, "script" ) ) + scripts = scripts.add( elem ); + else { + // Remove any inner scripts for later evaluation + if ( elem.nodeType == 1 ) + scripts = scripts.add( jQuery( "script", elem ).remove() ); + + // Inject the elements into the document + callback.call( obj, elem ); + } + }); + + scripts.each( evalScript ); + }); + } +}; + +// Give the init function the jQuery prototype for later instantiation +jQuery.fn.init.prototype = jQuery.fn; + +function evalScript( i, elem ) { + if ( elem.src ) + jQuery.ajax({ + url: elem.src, + async: false, + dataType: "script" + }); + + else + jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" ); + + if ( elem.parentNode ) + elem.parentNode.removeChild( elem ); +} + +function now(){ + return +new Date; +} + +jQuery.extend = jQuery.fn.extend = function() { + // copy reference to target object + var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options; + + // Handle a deep copy situation + if ( target.constructor == Boolean ) { + deep = target; + target = arguments[1] || {}; + // skip the boolean and the target + i = 2; + } + + // Handle case when target is a string or something (possible in deep copy) + if ( typeof target != "object" && typeof target != "function" ) + target = {}; + + // extend jQuery itself if only one argument is passed + if ( length == i ) { + target = this; + --i; + } + + for ( ; i < length; i++ ) + // Only deal with non-null/undefined values + if ( (options = arguments[ i ]) != null ) + // Extend the base object + for ( var name in options ) { + var src = target[ name ], copy = options[ name ]; + + // Prevent never-ending loop + if ( target === copy ) + continue; + + // Recurse if we're merging object values + if ( deep && copy && typeof copy == "object" && !copy.nodeType ) + target[ name ] = jQuery.extend( deep, + // Never move original objects, clone them + src || ( copy.length != null ? [ ] : { } ) + , copy ); + + // Don't bring in undefined values + else if ( copy !== undefined ) + target[ name ] = copy; + + } + + // Return the modified object + return target; +}; + +var expando = "jQuery" + now(), uuid = 0, windowData = {}, + // exclude the following css properties to add px + exclude = /z-?index|font-?weight|opacity|zoom|line-?height/i, + // cache defaultView + defaultView = document.defaultView || {}; + +jQuery.extend({ + noConflict: function( deep ) { + window.$ = _$; + + if ( deep ) + window.jQuery = _jQuery; + + return jQuery; + }, + + // See test/unit/core.js for details concerning this function. + isFunction: function( fn ) { + return !!fn && typeof fn != "string" && !fn.nodeName && + fn.constructor != Array && /^[\s[]?function/.test( fn + "" ); + }, + + // check if an element is in a (or is an) XML document + isXMLDoc: function( elem ) { + return elem.documentElement && !elem.body || + elem.tagName && elem.ownerDocument && !elem.ownerDocument.body; + }, + + // Evalulates a script in a global context + globalEval: function( data ) { + data = jQuery.trim( data ); + + if ( data ) { + // Inspired by code by Andrea Giammarchi + // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html + var head = document.getElementsByTagName("head")[0] || document.documentElement, + script = document.createElement("script"); + + script.type = "text/javascript"; + if ( jQuery.browser.msie ) + script.text = data; + else + script.appendChild( document.createTextNode( data ) ); + + // Use insertBefore instead of appendChild to circumvent an IE6 bug. + // This arises when a base node is used (#2709). + head.insertBefore( script, head.firstChild ); + head.removeChild( script ); + } + }, + + nodeName: function( elem, name ) { + return elem.nodeName && elem.nodeName.toUpperCase() == name.toUpperCase(); + }, + + cache: {}, + + data: function( elem, name, data ) { + elem = elem == window ? + windowData : + elem; + + var id = elem[ expando ]; + + // Compute a unique ID for the element + if ( !id ) + id = elem[ expando ] = ++uuid; + + // Only generate the data cache if we're + // trying to access or manipulate it + if ( name && !jQuery.cache[ id ] ) + jQuery.cache[ id ] = {}; + + // Prevent overriding the named cache with undefined values + if ( data !== undefined ) + jQuery.cache[ id ][ name ] = data; + + // Return the named cache data, or the ID for the element + return name ? + jQuery.cache[ id ][ name ] : + id; + }, + + removeData: function( elem, name ) { + elem = elem == window ? + windowData : + elem; + + var id = elem[ expando ]; + + // If we want to remove a specific section of the element's data + if ( name ) { + if ( jQuery.cache[ id ] ) { + // Remove the section of cache data + delete jQuery.cache[ id ][ name ]; + + // If we've removed all the data, remove the element's cache + name = ""; + + for ( name in jQuery.cache[ id ] ) + break; + + if ( !name ) + jQuery.removeData( elem ); + } + + // Otherwise, we want to remove all of the element's data + } else { + // Clean up the element expando + try { + delete elem[ expando ]; + } catch(e){ + // IE has trouble directly removing the expando + // but it's ok with using removeAttribute + if ( elem.removeAttribute ) + elem.removeAttribute( expando ); + } + + // Completely remove the data cache + delete jQuery.cache[ id ]; + } + }, + + // args is for internal usage only + each: function( object, callback, args ) { + var name, i = 0, length = object.length; + + if ( args ) { + if ( length == undefined ) { + for ( name in object ) + if ( callback.apply( object[ name ], args ) === false ) + break; + } else + for ( ; i < length; ) + if ( callback.apply( object[ i++ ], args ) === false ) + break; + + // A special, fast, case for the most common use of each + } else { + if ( length == undefined ) { + for ( name in object ) + if ( callback.call( object[ name ], name, object[ name ] ) === false ) + break; + } else + for ( var value = object[0]; + i < length && callback.call( value, i, value ) !== false; value = object[++i] ){} + } + + return object; + }, + + prop: function( elem, value, type, i, name ) { + // Handle executable functions + if ( jQuery.isFunction( value ) ) + value = value.call( elem, i ); + + // Handle passing in a number to a CSS property + return value && value.constructor == Number && type == "curCSS" && !exclude.test( name ) ? + value + "px" : + value; + }, + + className: { + // internal only, use addClass("class") + add: function( elem, classNames ) { + jQuery.each((classNames || "").split(/\s+/), function(i, className){ + if ( elem.nodeType == 1 && !jQuery.className.has( elem.className, className ) ) + elem.className += (elem.className ? " " : "") + className; + }); + }, + + // internal only, use removeClass("class") + remove: function( elem, classNames ) { + if (elem.nodeType == 1) + elem.className = classNames != undefined ? + jQuery.grep(elem.className.split(/\s+/), function(className){ + return !jQuery.className.has( classNames, className ); + }).join(" ") : + ""; + }, + + // internal only, use hasClass("class") + has: function( elem, className ) { + return jQuery.inArray( className, (elem.className || elem).toString().split(/\s+/) ) > -1; + } + }, + + // A method for quickly swapping in/out CSS properties to get correct calculations + swap: function( elem, options, callback ) { + var old = {}; + // Remember the old values, and insert the new ones + for ( var name in options ) { + old[ name ] = elem.style[ name ]; + elem.style[ name ] = options[ name ]; + } + + callback.call( elem ); + + // Revert the old values + for ( var name in options ) + elem.style[ name ] = old[ name ]; + }, + + css: function( elem, name, force ) { + if ( name == "width" || name == "height" ) { + var val, props = { position: "absolute", visibility: "hidden", display:"block" }, which = name == "width" ? [ "Left", "Right" ] : [ "Top", "Bottom" ]; + + function getWH() { + val = name == "width" ? elem.offsetWidth : elem.offsetHeight; + var padding = 0, border = 0; + jQuery.each( which, function() { + padding += parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0; + border += parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0; + }); + val -= Math.round(padding + border); + } + + if ( jQuery(elem).is(":visible") ) + getWH(); + else + jQuery.swap( elem, props, getWH ); + + return Math.max(0, val); + } + + return jQuery.curCSS( elem, name, force ); + }, + + curCSS: function( elem, name, force ) { + var ret, style = elem.style; + + // A helper method for determining if an element's values are broken + function color( elem ) { + if ( !jQuery.browser.safari ) + return false; + + // defaultView is cached + var ret = defaultView.getComputedStyle( elem, null ); + return !ret || ret.getPropertyValue("color") == ""; + } + + // We need to handle opacity special in IE + if ( name == "opacity" && jQuery.browser.msie ) { + ret = jQuery.attr( style, "opacity" ); + + return ret == "" ? + "1" : + ret; + } + // Opera sometimes will give the wrong display answer, this fixes it, see #2037 + if ( jQuery.browser.opera && name == "display" ) { + var save = style.outline; + style.outline = "0 solid black"; + style.outline = save; + } + + // Make sure we're using the right name for getting the float value + if ( name.match( /float/i ) ) + name = styleFloat; + + if ( !force && style && style[ name ] ) + ret = style[ name ]; + + else if ( defaultView.getComputedStyle ) { + + // Only "float" is needed here + if ( name.match( /float/i ) ) + name = "float"; + + name = name.replace( /([A-Z])/g, "-$1" ).toLowerCase(); + + var computedStyle = defaultView.getComputedStyle( elem, null ); + + if ( computedStyle && !color( elem ) ) + ret = computedStyle.getPropertyValue( name ); + + // If the element isn't reporting its values properly in Safari + // then some display: none elements are involved + else { + var swap = [], stack = [], a = elem, i = 0; + + // Locate all of the parent display: none elements + for ( ; a && color(a); a = a.parentNode ) + stack.unshift(a); + + // Go through and make them visible, but in reverse + // (It would be better if we knew the exact display type that they had) + for ( ; i < stack.length; i++ ) + if ( color( stack[ i ] ) ) { + swap[ i ] = stack[ i ].style.display; + stack[ i ].style.display = "block"; + } + + // Since we flip the display style, we have to handle that + // one special, otherwise get the value + ret = name == "display" && swap[ stack.length - 1 ] != null ? + "none" : + ( computedStyle && computedStyle.getPropertyValue( name ) ) || ""; + + // Finally, revert the display styles back + for ( i = 0; i < swap.length; i++ ) + if ( swap[ i ] != null ) + stack[ i ].style.display = swap[ i ]; + } + + // We should always get a number back from opacity + if ( name == "opacity" && ret == "" ) + ret = "1"; + + } else if ( elem.currentStyle ) { + var camelCase = name.replace(/\-(\w)/g, function(all, letter){ + return letter.toUpperCase(); + }); + + ret = elem.currentStyle[ name ] || elem.currentStyle[ camelCase ]; + + // From the awesome hack by Dean Edwards + // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291 + + // If we're not dealing with a regular pixel number + // but a number that has a weird ending, we need to convert it to pixels + if ( !/^\d+(px)?$/i.test( ret ) && /^\d/.test( ret ) ) { + // Remember the original values + var left = style.left, rsLeft = elem.runtimeStyle.left; + + // Put in the new values to get a computed value out + elem.runtimeStyle.left = elem.currentStyle.left; + style.left = ret || 0; + ret = style.pixelLeft + "px"; + + // Revert the changed values + style.left = left; + elem.runtimeStyle.left = rsLeft; + } + } + + return ret; + }, + + clean: function( elems, context ) { + var ret = []; + context = context || document; + // !context.createElement fails in IE with an error but returns typeof 'object' + if (typeof context.createElement == 'undefined') + context = context.ownerDocument || context[0] && context[0].ownerDocument || document; + + jQuery.each(elems, function(i, elem){ + if ( !elem ) + return; + + if ( elem.constructor == Number ) + elem += ''; + + // Convert html string into DOM nodes + if ( typeof elem == "string" ) { + // Fix "XHTML"-style tags in all browsers + elem = elem.replace(/(<(\w+)[^>]*?)\/>/g, function(all, front, tag){ + return tag.match(/^(abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i) ? + all : + front + "></" + tag + ">"; + }); + + // Trim whitespace, otherwise indexOf won't work as expected + var tags = jQuery.trim( elem ).toLowerCase(), div = context.createElement("div"); + + var wrap = + // option or optgroup + !tags.indexOf("<opt") && + [ 1, "<select multiple='multiple'>", "</select>" ] || + + !tags.indexOf("<leg") && + [ 1, "<fieldset>", "</fieldset>" ] || + + tags.match(/^<(thead|tbody|tfoot|colg|cap)/) && + [ 1, "<table>", "</table>" ] || + + !tags.indexOf("<tr") && + [ 2, "<table><tbody>", "</tbody></table>" ] || + + // <thead> matched above + (!tags.indexOf("<td") || !tags.indexOf("<th")) && + [ 3, "<table><tbody><tr>", "</tr></tbody></table>" ] || + + !tags.indexOf("<col") && + [ 2, "<table><tbody></tbody><colgroup>", "</colgroup></table>" ] || + + // IE can't serialize <link> and <script> tags normally + jQuery.browser.msie && + [ 1, "div<div>", "</div>" ] || + + [ 0, "", "" ]; + + // Go to html and back, then peel off extra wrappers + div.innerHTML = wrap[1] + elem + wrap[2]; + + // Move to the right depth + while ( wrap[0]-- ) + div = div.lastChild; + + // Remove IE's autoinserted <tbody> from table fragments + if ( jQuery.browser.msie ) { + + // String was a <table>, *may* have spurious <tbody> + var tbody = !tags.indexOf("<table") && tags.indexOf("<tbody") < 0 ? + div.firstChild && div.firstChild.childNodes : + + // String was a bare <thead> or <tfoot> + wrap[1] == "<table>" && tags.indexOf("<tbody") < 0 ? + div.childNodes : + []; + + for ( var j = tbody.length - 1; j >= 0 ; --j ) + if ( jQuery.nodeName( tbody[ j ], "tbody" ) && !tbody[ j ].childNodes.length ) + tbody[ j ].parentNode.removeChild( tbody[ j ] ); + + // IE completely kills leading whitespace when innerHTML is used + if ( /^\s/.test( elem ) ) + div.insertBefore( context.createTextNode( elem.match(/^\s*/)[0] ), div.firstChild ); + + } + + elem = jQuery.makeArray( div.childNodes ); + } + + if ( elem.length === 0 && (!jQuery.nodeName( elem, "form" ) && !jQuery.nodeName( elem, "select" )) ) + return; + + if ( elem[0] == undefined || jQuery.nodeName( elem, "form" ) || elem.options ) + ret.push( elem ); + + else + ret = jQuery.merge( ret, elem ); + + }); + + return ret; + }, + + attr: function( elem, name, value ) { + // don't set attributes on text and comment nodes + if (!elem || elem.nodeType == 3 || elem.nodeType == 8) + return undefined; + + var notxml = !jQuery.isXMLDoc( elem ), + // Whether we are setting (or getting) + set = value !== undefined, + msie = jQuery.browser.msie; + + // Try to normalize/fix the name + name = notxml && jQuery.props[ name ] || name; + + // Only do all the following if this is a node (faster for style) + // IE elem.getAttribute passes even for style + if ( elem.tagName ) { + + // These attributes require special treatment + var special = /href|src|style/.test( name ); + + // Safari mis-reports the default selected property of a hidden option + // Accessing the parent's selectedIndex property fixes it + if ( name == "selected" && jQuery.browser.safari ) + elem.parentNode.selectedIndex; + + // If applicable, access the attribute via the DOM 0 way + if ( name in elem && notxml && !special ) { + if ( set ){ + // We can't allow the type property to be changed (since it causes problems in IE) + if ( name == "type" && jQuery.nodeName( elem, "input" ) && elem.parentNode ) + throw "type property can't be changed"; + + elem[ name ] = value; + } + + // browsers index elements by id/name on forms, give priority to attributes. + if( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) + return elem.getAttributeNode( name ).nodeValue; + + return elem[ name ]; + } + + if ( msie && notxml && name == "style" ) + return jQuery.attr( elem.style, "cssText", value ); + + if ( set ) + // convert the value to a string (all browsers do this but IE) see #1070 + elem.setAttribute( name, "" + value ); + + var attr = msie && notxml && special + // Some attributes require a special call on IE + ? elem.getAttribute( name, 2 ) + : elem.getAttribute( name ); + + // Non-existent attributes return null, we normalize to undefined + return attr === null ? undefined : attr; + } + + // elem is actually elem.style ... set the style + + // IE uses filters for opacity + if ( msie && name == "opacity" ) { + if ( set ) { + // IE has trouble with opacity if it does not have layout + // Force it by setting the zoom level + elem.zoom = 1; + + // Set the alpha filter to set the opacity + elem.filter = (elem.filter || "").replace( /alpha\([^)]*\)/, "" ) + + (parseInt( value ) + '' == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")"); + } + + return elem.filter && elem.filter.indexOf("opacity=") >= 0 ? + (parseFloat( elem.filter.match(/opacity=([^)]*)/)[1] ) / 100) + '': + ""; + } + + name = name.replace(/-([a-z])/ig, function(all, letter){ + return letter.toUpperCase(); + }); + + if ( set ) + elem[ name ] = value; + + return elem[ name ]; + }, + + trim: function( text ) { + return (text || "").replace( /^\s+|\s+$/g, "" ); + }, + + makeArray: function( array ) { + var ret = []; + + if( array != null ){ + var i = array.length; + //the window, strings and functions also have 'length' + if( i == null || array.split || array.setInterval || array.call ) + ret[0] = array; + else + while( i ) + ret[--i] = array[i]; + } + + return ret; + }, + + inArray: function( elem, array ) { + for ( var i = 0, length = array.length; i < length; i++ ) + // Use === because on IE, window == document + if ( array[ i ] === elem ) + return i; + + return -1; + }, + + merge: function( first, second ) { + // We have to loop this way because IE & Opera overwrite the length + // expando of getElementsByTagName + var i = 0, elem, pos = first.length; + // Also, we need to make sure that the correct elements are being returned + // (IE returns comment nodes in a '*' query) + if ( jQuery.browser.msie ) { + while ( elem = second[ i++ ] ) + if ( elem.nodeType != 8 ) + first[ pos++ ] = elem; + + } else + while ( elem = second[ i++ ] ) + first[ pos++ ] = elem; + + return first; + }, + + unique: function( array ) { + var ret = [], done = {}; + + try { + + for ( var i = 0, length = array.length; i < length; i++ ) { + var id = jQuery.data( array[ i ] ); + + if ( !done[ id ] ) { + done[ id ] = true; + ret.push( array[ i ] ); + } + } + + } catch( e ) { + ret = array; + } + + return ret; + }, + + grep: function( elems, callback, inv ) { + var ret = []; + + // Go through the array, only saving the items + // that pass the validator function + for ( var i = 0, length = elems.length; i < length; i++ ) + if ( !inv != !callback( elems[ i ], i ) ) + ret.push( elems[ i ] ); + + return ret; + }, + + map: function( elems, callback ) { + var ret = []; + + // Go through the array, translating each of the items to their + // new value (or values). + for ( var i = 0, length = elems.length; i < length; i++ ) { + var value = callback( elems[ i ], i ); + + if ( value != null ) + ret[ ret.length ] = value; + } + + return ret.concat.apply( [], ret ); + } +}); + +var userAgent = navigator.userAgent.toLowerCase(); + +// Figure out what browser is being used +jQuery.browser = { + version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1], + safari: /webkit/.test( userAgent ), + opera: /opera/.test( userAgent ), + msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ), + mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent ) +}; + +var styleFloat = jQuery.browser.msie ? + "styleFloat" : + "cssFloat"; + +jQuery.extend({ + // Check to see if the W3C box model is being used + boxModel: !jQuery.browser.msie || document.compatMode == "CSS1Compat", + + props: { + "for": "htmlFor", + "class": "className", + "float": styleFloat, + cssFloat: styleFloat, + styleFloat: styleFloat, + readonly: "readOnly", + maxlength: "maxLength", + cellspacing: "cellSpacing" + } +}); + +jQuery.each({ + parent: function(elem){return elem.parentNode;}, + parents: function(elem){return jQuery.dir(elem,"parentNode");}, + next: function(elem){return jQuery.nth(elem,2,"nextSibling");}, + prev: function(elem){return jQuery.nth(elem,2,"previousSibling");}, + nextAll: function(elem){return jQuery.dir(elem,"nextSibling");}, + prevAll: function(elem){return jQuery.dir(elem,"previousSibling");}, + siblings: function(elem){return jQuery.sibling(elem.parentNode.firstChild,elem);}, + children: function(elem){return jQuery.sibling(elem.firstChild);}, + contents: function(elem){return jQuery.nodeName(elem,"iframe")?elem.contentDocument||elem.contentWindow.document:jQuery.makeArray(elem.childNodes);} +}, function(name, fn){ + jQuery.fn[ name ] = function( selector ) { + var ret = jQuery.map( this, fn ); + + if ( selector && typeof selector == "string" ) + ret = jQuery.multiFilter( selector, ret ); + + return this.pushStack( jQuery.unique( ret ) ); + }; +}); + +jQuery.each({ + appendTo: "append", + prependTo: "prepend", + insertBefore: "before", + insertAfter: "after", + replaceAll: "replaceWith" +}, function(name, original){ + jQuery.fn[ name ] = function() { + var args = arguments; + + return this.each(function(){ + for ( var i = 0, length = args.length; i < length; i++ ) + jQuery( args[ i ] )[ original ]( this ); + }); + }; +}); + +jQuery.each({ + removeAttr: function( name ) { + jQuery.attr( this, name, "" ); + if (this.nodeType == 1) + this.removeAttribute( name ); + }, + + addClass: function( classNames ) { + jQuery.className.add( this, classNames ); + }, + + removeClass: function( classNames ) { + jQuery.className.remove( this, classNames ); + }, + + toggleClass: function( classNames ) { + jQuery.className[ jQuery.className.has( this, classNames ) ? "remove" : "add" ]( this, classNames ); + }, + + remove: function( selector ) { + if ( !selector || jQuery.filter( selector, [ this ] ).r.length ) { + // Prevent memory leaks + jQuery( "*", this ).add(this).each(function(){ + jQuery.event.remove(this); + jQuery.removeData(this); + }); + if (this.parentNode) + this.parentNode.removeChild( this ); + } + }, + + empty: function() { + // Remove element nodes and prevent memory leaks + jQuery( ">*", this ).remove(); + + // Remove any remaining nodes + while ( this.firstChild ) + this.removeChild( this.firstChild ); + } +}, function(name, fn){ + jQuery.fn[ name ] = function(){ + return this.each( fn, arguments ); + }; +}); + +jQuery.each([ "Height", "Width" ], function(i, name){ + var type = name.toLowerCase(); + + jQuery.fn[ type ] = function( size ) { + // Get window width or height + return this[0] == window ? + // Opera reports document.body.client[Width/Height] properly in both quirks and standards + jQuery.browser.opera && document.body[ "client" + name ] || + + // Safari reports inner[Width/Height] just fine (Mozilla and Opera include scroll bar widths) + jQuery.browser.safari && window[ "inner" + name ] || + + // Everyone else use document.documentElement or document.body depending on Quirks vs Standards mode + document.compatMode == "CSS1Compat" && document.documentElement[ "client" + name ] || document.body[ "client" + name ] : + + // Get document width or height + this[0] == document ? + // Either scroll[Width/Height] or offset[Width/Height], whichever is greater + Math.max( + Math.max(document.body["scroll" + name], document.documentElement["scroll" + name]), + Math.max(document.body["offset" + name], document.documentElement["offset" + name]) + ) : + + // Get or set width or height on the element + size == undefined ? + // Get width or height on the element + (this.length ? jQuery.css( this[0], type ) : null) : + + // Set the width or height on the element (default to pixels if value is unitless) + this.css( type, size.constructor == String ? size : size + "px" ); + }; +}); + +// Helper function used by the dimensions and offset modules +function num(elem, prop) { + return elem[0] && parseInt( jQuery.curCSS(elem[0], prop, true), 10 ) || 0; +}var chars = jQuery.browser.safari && parseInt(jQuery.browser.version) < 417 ? + "(?:[\\w*_-]|\\\\.)" : + "(?:[\\w\u0128-\uFFFF*_-]|\\\\.)", + quickChild = new RegExp("^>\\s*(" + chars + "+)"), + quickID = new RegExp("^(" + chars + "+)(#)(" + chars + "+)"), + quickClass = new RegExp("^([#.]?)(" + chars + "*)"); + +jQuery.extend({ + expr: { + "": function(a,i,m){return m[2]=="*"||jQuery.nodeName(a,m[2]);}, + "#": function(a,i,m){return a.getAttribute("id")==m[2];}, + ":": { + // Position Checks + lt: function(a,i,m){return i<m[3]-0;}, + gt: function(a,i,m){return i>m[3]-0;}, + nth: function(a,i,m){return m[3]-0==i;}, + eq: function(a,i,m){return m[3]-0==i;}, + first: function(a,i){return i==0;}, + last: function(a,i,m,r){return i==r.length-1;}, + even: function(a,i){return i%2==0;}, + odd: function(a,i){return i%2;}, + + // Child Checks + "first-child": function(a){return a.parentNode.getElementsByTagName("*")[0]==a;}, + "last-child": function(a){return jQuery.nth(a.parentNode.lastChild,1,"previousSibling")==a;}, + "only-child": function(a){return !jQuery.nth(a.parentNode.lastChild,2,"previousSibling");}, + + // Parent Checks + parent: function(a){return a.firstChild;}, + empty: function(a){return !a.firstChild;}, + + // Text Check + contains: function(a,i,m){return (a.textContent||a.innerText||jQuery(a).text()||"").indexOf(m[3])>=0;}, + + // Visibility + visible: function(a){return "hidden"!=a.type&&jQuery.css(a,"display")!="none"&&jQuery.css(a,"visibility")!="hidden";}, + hidden: function(a){return "hidden"==a.type||jQuery.css(a,"display")=="none"||jQuery.css(a,"visibility")=="hidden";}, + + // Form attributes + enabled: function(a){return !a.disabled;}, + disabled: function(a){return a.disabled;}, + checked: function(a){return a.checked;}, + selected: function(a){return a.selected||jQuery.attr(a,"selected");}, + + // Form elements + text: function(a){return "text"==a.type;}, + radio: function(a){return "radio"==a.type;}, + checkbox: function(a){return "checkbox"==a.type;}, + file: function(a){return "file"==a.type;}, + password: function(a){return "password"==a.type;}, + submit: function(a){return "submit"==a.type;}, + image: function(a){return "image"==a.type;}, + reset: function(a){return "reset"==a.type;}, + button: function(a){return "button"==a.type||jQuery.nodeName(a,"button");}, + input: function(a){return /input|select|textarea|button/i.test(a.nodeName);}, + + // :has() + has: function(a,i,m){return jQuery.find(m[3],a).length;}, + + // :header + header: function(a){return /h\d/i.test(a.nodeName);}, + + // :animated + animated: function(a){return jQuery.grep(jQuery.timers,function(fn){return a==fn.elem;}).length;} + } + }, + + // The regular expressions that power the parsing engine + parse: [ + // Match: [@value='test'], [@foo] + /^(\[) *@?([\w-]+) *([!*$^~=]*) *('?"?)(.*?)\4 *\]/, + + // Match: :contains('foo') + /^(:)([\w-]+)\("?'?(.*?(\(.*?\))?[^(]*?)"?'?\)/, + + // Match: :even, :last-child, #id, .class + new RegExp("^([:.#]*)(" + chars + "+)") + ], + + multiFilter: function( expr, elems, not ) { + var old, cur = []; + + while ( expr && expr != old ) { + old = expr; + var f = jQuery.filter( expr, elems, not ); + expr = f.t.replace(/^\s*,\s*/, "" ); + cur = not ? elems = f.r : jQuery.merge( cur, f.r ); + } + + return cur; + }, + + find: function( t, context ) { + // Quickly handle non-string expressions + if ( typeof t != "string" ) + return [ t ]; + + // check to make sure context is a DOM element or a document + if ( context && context.nodeType != 1 && context.nodeType != 9) + return [ ]; + + // Set the correct context (if none is provided) + context = context || document; + + // Initialize the search + var ret = [context], done = [], last, nodeName; + + // Continue while a selector expression exists, and while + // we're no longer looping upon ourselves + while ( t && last != t ) { + var r = []; + last = t; + + t = jQuery.trim(t); + + var foundToken = false, + + // An attempt at speeding up child selectors that + // point to a specific element tag + re = quickChild, + + m = re.exec(t); + + if ( m ) { + nodeName = m[1].toUpperCase(); + + // Perform our own iteration and filter + for ( var i = 0; ret[i]; i++ ) + for ( var c = ret[i].firstChild; c; c = c.nextSibling ) + if ( c.nodeType == 1 && (nodeName == "*" || c.nodeName.toUpperCase() == nodeName) ) + r.push( c ); + + ret = r; + t = t.replace( re, "" ); + if ( t.indexOf(" ") == 0 ) continue; + foundToken = true; + } else { + re = /^([>+~])\s*(\w*)/i; + + if ( (m = re.exec(t)) != null ) { + r = []; + + var merge = {}; + nodeName = m[2].toUpperCase(); + m = m[1]; + + for ( var j = 0, rl = ret.length; j < rl; j++ ) { + var n = m == "~" || m == "+" ? ret[j].nextSibling : ret[j].firstChild; + for ( ; n; n = n.nextSibling ) + if ( n.nodeType == 1 ) { + var id = jQuery.data(n); + + if ( m == "~" && merge[id] ) break; + + if (!nodeName || n.nodeName.toUpperCase() == nodeName ) { + if ( m == "~" ) merge[id] = true; + r.push( n ); + } + + if ( m == "+" ) break; + } + } + + ret = r; + + // And remove the token + t = jQuery.trim( t.replace( re, "" ) ); + foundToken = true; + } + } + + // See if there's still an expression, and that we haven't already + // matched a token + if ( t && !foundToken ) { + // Handle multiple expressions + if ( !t.indexOf(",") ) { + // Clean the result set + if ( context == ret[0] ) ret.shift(); + + // Merge the result sets + done = jQuery.merge( done, ret ); + + // Reset the context + r = ret = [context]; + + // Touch up the selector string + t = " " + t.substr(1,t.length); + + } else { + // Optimize for the case nodeName#idName + var re2 = quickID; + var m = re2.exec(t); + + // Re-organize the results, so that they're consistent + if ( m ) { + m = [ 0, m[2], m[3], m[1] ]; + + } else { + // Otherwise, do a traditional filter check for + // ID, class, and element selectors + re2 = quickClass; + m = re2.exec(t); + } + + m[2] = m[2].replace(/\\/g, ""); + + var elem = ret[ret.length-1]; + + // Try to do a global search by ID, where we can + if ( m[1] == "#" && elem && elem.getElementById && !jQuery.isXMLDoc(elem) ) { + // Optimization for HTML document case + var oid = elem.getElementById(m[2]); + + // Do a quick check for the existence of the actual ID attribute + // to avoid selecting by the name attribute in IE + // also check to insure id is a string to avoid selecting an element with the name of 'id' inside a form + if ( (jQuery.browser.msie||jQuery.browser.opera) && oid && typeof oid.id == "string" && oid.id != m[2] ) + oid = jQuery('[@id="'+m[2]+'"]', elem)[0]; + + // Do a quick check for node name (where applicable) so + // that div#foo searches will be really fast + ret = r = oid && (!m[3] || jQuery.nodeName(oid, m[3])) ? [oid] : []; + } else { + // We need to find all descendant elements + for ( var i = 0; ret[i]; i++ ) { + // Grab the tag name being searched for + var tag = m[1] == "#" && m[3] ? m[3] : m[1] != "" || m[0] == "" ? "*" : m[2]; + + // Handle IE7 being really dumb about <object>s + if ( tag == "*" && ret[i].nodeName.toLowerCase() == "object" ) + tag = "param"; + + r = jQuery.merge( r, ret[i].getElementsByTagName( tag )); + } + + // It's faster to filter by class and be done with it + if ( m[1] == "." ) + r = jQuery.classFilter( r, m[2] ); + + // Same with ID filtering + if ( m[1] == "#" ) { + var tmp = []; + + // Try to find the element with the ID + for ( var i = 0; r[i]; i++ ) + if ( r[i].getAttribute("id") == m[2] ) { + tmp = [ r[i] ]; + break; + } + + r = tmp; + } + + ret = r; + } + + t = t.replace( re2, "" ); + } + + } + + // If a selector string still exists + if ( t ) { + // Attempt to filter it + var val = jQuery.filter(t,r); + ret = r = val.r; + t = jQuery.trim(val.t); + } + } + + // An error occurred with the selector; + // just return an empty set instead + if ( t ) + ret = []; + + // Remove the root context + if ( ret && context == ret[0] ) + ret.shift(); + + // And combine the results + done = jQuery.merge( done, ret ); + + return done; + }, + + classFilter: function(r,m,not){ + m = " " + m + " "; + var tmp = []; + for ( var i = 0; r[i]; i++ ) { + var pass = (" " + r[i].className + " ").indexOf( m ) >= 0; + if ( !not && pass || not && !pass ) + tmp.push( r[i] ); + } + return tmp; + }, + + filter: function(t,r,not) { + var last; + + // Look for common filter expressions + while ( t && t != last ) { + last = t; + + var p = jQuery.parse, m; + + for ( var i = 0; p[i]; i++ ) { + m = p[i].exec( t ); + + if ( m ) { + // Remove what we just matched + t = t.substring( m[0].length ); + + m[2] = m[2].replace(/\\/g, ""); + break; + } + } + + if ( !m ) + break; + + // :not() is a special case that can be optimized by + // keeping it out of the expression list + if ( m[1] == ":" && m[2] == "not" ) + // optimize if only one selector found (most common case) + r = isSimple.test( m[3] ) ? + jQuery.filter(m[3], r, true).r : + jQuery( r ).not( m[3] ); + + // We can get a big speed boost by filtering by class here + else if ( m[1] == "." ) + r = jQuery.classFilter(r, m[2], not); + + else if ( m[1] == "[" ) { + var tmp = [], type = m[3]; + + for ( var i = 0, rl = r.length; i < rl; i++ ) { + var a = r[i], z = a[ jQuery.props[m[2]] || m[2] ]; + + if ( z == null || /href|src|selected/.test(m[2]) ) + z = jQuery.attr(a,m[2]) || ''; + + if ( (type == "" && !!z || + type == "=" && z == m[5] || + type == "!=" && z != m[5] || + type == "^=" && z && !z.indexOf(m[5]) || + type == "$=" && z.substr(z.length - m[5].length) == m[5] || + (type == "*=" || type == "~=") && z.indexOf(m[5]) >= 0) ^ not ) + tmp.push( a ); + } + + r = tmp; + + // We can get a speed boost by handling nth-child here + } else if ( m[1] == ":" && m[2] == "nth-child" ) { + var merge = {}, tmp = [], + // parse equations like 'even', 'odd', '5', '2n', '3n+2', '4n-1', '-n+6' + test = /(-?)(\d*)n((?:\+|-)?\d*)/.exec( + m[3] == "even" && "2n" || m[3] == "odd" && "2n+1" || + !/\D/.test(m[3]) && "0n+" + m[3] || m[3]), + // calculate the numbers (first)n+(last) including if they are negative + first = (test[1] + (test[2] || 1)) - 0, last = test[3] - 0; + + // loop through all the elements left in the jQuery object + for ( var i = 0, rl = r.length; i < rl; i++ ) { + var node = r[i], parentNode = node.parentNode, id = jQuery.data(parentNode); + + if ( !merge[id] ) { + var c = 1; + + for ( var n = parentNode.firstChild; n; n = n.nextSibling ) + if ( n.nodeType == 1 ) + n.nodeIndex = c++; + + merge[id] = true; + } + + var add = false; + + if ( first == 0 ) { + if ( node.nodeIndex == last ) + add = true; + } else if ( (node.nodeIndex - last) % first == 0 && (node.nodeIndex - last) / first >= 0 ) + add = true; + + if ( add ^ not ) + tmp.push( node ); + } + + r = tmp; + + // Otherwise, find the expression to execute + } else { + var fn = jQuery.expr[ m[1] ]; + if ( typeof fn == "object" ) + fn = fn[ m[2] ]; + + if ( typeof fn == "string" ) + fn = eval("false||function(a,i){return " + fn + ";}"); + + // Execute it against the current filter + r = jQuery.grep( r, function(elem, i){ + return fn(elem, i, m, r); + }, not ); + } + } + + // Return an array of filtered elements (r) + // and the modified expression string (t) + return { r: r, t: t }; + }, + + dir: function( elem, dir ){ + var matched = [], + cur = elem[dir]; + while ( cur && cur != document ) { + if ( cur.nodeType == 1 ) + matched.push( cur ); + cur = cur[dir]; + } + return matched; + }, + + nth: function(cur,result,dir,elem){ + result = result || 1; + var num = 0; + + for ( ; cur; cur = cur[dir] ) + if ( cur.nodeType == 1 && ++num == result ) + break; + + return cur; + }, + + sibling: function( n, elem ) { + var r = []; + + for ( ; n; n = n.nextSibling ) { + if ( n.nodeType == 1 && n != elem ) + r.push( n ); + } + + return r; + } +}); +/* + * A number of helper functions used for managing events. + * Many of the ideas behind this code orignated from + * Dean Edwards' addEvent library. + */ +jQuery.event = { + + // Bind an event to an element + // Original by Dean Edwards + add: function(elem, types, handler, data) { + if ( elem.nodeType == 3 || elem.nodeType == 8 ) + return; + + // For whatever reason, IE has trouble passing the window object + // around, causing it to be cloned in the process + if ( jQuery.browser.msie && elem.setInterval ) + elem = window; + + // Make sure that the function being executed has a unique ID + if ( !handler.guid ) + handler.guid = this.guid++; + + // if data is passed, bind to handler + if( data != undefined ) { + // Create temporary function pointer to original handler + var fn = handler; + + // Create unique handler function, wrapped around original handler + handler = this.proxy( fn, function() { + // Pass arguments and context to original handler + return fn.apply(this, arguments); + }); + + // Store data in unique handler + handler.data = data; + } + + // Init the element's event structure + var events = jQuery.data(elem, "events") || jQuery.data(elem, "events", {}), + handle = jQuery.data(elem, "handle") || jQuery.data(elem, "handle", function(){ + // Handle the second event of a trigger and when + // an event is called after a page has unloaded + if ( typeof jQuery != "undefined" && !jQuery.event.triggered ) + return jQuery.event.handle.apply(arguments.callee.elem, arguments); + }); + // Add elem as a property of the handle function + // This is to prevent a memory leak with non-native + // event in IE. + handle.elem = elem; + + // Handle multiple events separated by a space + // jQuery(...).bind("mouseover mouseout", fn); + jQuery.each(types.split(/\s+/), function(index, type) { + // Namespaced event handlers + var parts = type.split("."); + type = parts[0]; + handler.type = parts[1]; + + // Get the current list of functions bound to this event + var handlers = events[type]; + + // Init the event handler queue + if (!handlers) { + handlers = events[type] = {}; + + // Check for a special event handler + // Only use addEventListener/attachEvent if the special + // events handler returns false + if ( !jQuery.event.special[type] || jQuery.event.special[type].setup.call(elem) === false ) { + // Bind the global event handler to the element + if (elem.addEventListener) + elem.addEventListener(type, handle, false); + else if (elem.attachEvent) + elem.attachEvent("on" + type, handle); + } + } + + // Add the function to the element's handler list + handlers[handler.guid] = handler; + + // Keep track of which events have been used, for global triggering + jQuery.event.global[type] = true; + }); + + // Nullify elem to prevent memory leaks in IE + elem = null; + }, + + guid: 1, + global: {}, + + // Detach an event or set of events from an element + remove: function(elem, types, handler) { + // don't do events on text and comment nodes + if ( elem.nodeType == 3 || elem.nodeType == 8 ) + return; + + var events = jQuery.data(elem, "events"), ret, index; + + if ( events ) { + // Unbind all events for the element + if ( types == undefined || (typeof types == "string" && types.charAt(0) == ".") ) + for ( var type in events ) + this.remove( elem, type + (types || "") ); + else { + // types is actually an event object here + if ( types.type ) { + handler = types.handler; + types = types.type; + } + + // Handle multiple events seperated by a space + // jQuery(...).unbind("mouseover mouseout", fn); + jQuery.each(types.split(/\s+/), function(index, type){ + // Namespaced event handlers + var parts = type.split("."); + type = parts[0]; + + if ( events[type] ) { + // remove the given handler for the given type + if ( handler ) + delete events[type][handler.guid]; + + // remove all handlers for the given type + else + for ( handler in events[type] ) + // Handle the removal of namespaced events + if ( !parts[1] || events[type][handler].type == parts[1] ) + delete events[type][handler]; + + // remove generic event handler if no more handlers exist + for ( ret in events[type] ) break; + if ( !ret ) { + if ( !jQuery.event.special[type] || jQuery.event.special[type].teardown.call(elem) === false ) { + if (elem.removeEventListener) + elem.removeEventListener(type, jQuery.data(elem, "handle"), false); + else if (elem.detachEvent) + elem.detachEvent("on" + type, jQuery.data(elem, "handle")); + } + ret = null; + delete events[type]; + } + } + }); + } + + // Remove the expando if it's no longer used + for ( ret in events ) break; + if ( !ret ) { + var handle = jQuery.data( elem, "handle" ); + if ( handle ) handle.elem = null; + jQuery.removeData( elem, "events" ); + jQuery.removeData( elem, "handle" ); + } + } + }, + + trigger: function(type, data, elem, donative, extra) { + // Clone the incoming data, if any + data = jQuery.makeArray(data); + + if ( type.indexOf("!") >= 0 ) { + type = type.slice(0, -1); + var exclusive = true; + } + + // Handle a global trigger + if ( !elem ) { + // Only trigger if we've ever bound an event for it + if ( this.global[type] ) + jQuery("*").add([window, document]).trigger(type, data); + + // Handle triggering a single element + } else { + // don't do events on text and comment nodes + if ( elem.nodeType == 3 || elem.nodeType == 8 ) + return undefined; + + var val, ret, fn = jQuery.isFunction( elem[ type ] || null ), + // Check to see if we need to provide a fake event, or not + event = !data[0] || !data[0].preventDefault; + + // Pass along a fake event + if ( event ) { + data.unshift({ + type: type, + target: elem, + preventDefault: function(){}, + stopPropagation: function(){}, + timeStamp: now() + }); + data[0][expando] = true; // no need to fix fake event + } + + // Enforce the right trigger type + data[0].type = type; + if ( exclusive ) + data[0].exclusive = true; + + // Trigger the event, it is assumed that "handle" is a function + var handle = jQuery.data(elem, "handle"); + if ( handle ) + val = handle.apply( elem, data ); + + // Handle triggering native .onfoo handlers (and on links since we don't call .click() for links) + if ( (!fn || (jQuery.nodeName(elem, 'a') && type == "click")) && elem["on"+type] && elem["on"+type].apply( elem, data ) === false ) + val = false; + + // Extra functions don't get the custom event object + if ( event ) + data.shift(); + + // Handle triggering of extra function + if ( extra && jQuery.isFunction( extra ) ) { + // call the extra function and tack the current return value on the end for possible inspection + ret = extra.apply( elem, val == null ? data : data.concat( val ) ); + // if anything is returned, give it precedence and have it overwrite the previous value + if (ret !== undefined) + val = ret; + } + + // Trigger the native events (except for clicks on links) + if ( fn && donative !== false && val !== false && !(jQuery.nodeName(elem, 'a') && type == "click") ) { + this.triggered = true; + try { + elem[ type ](); + // prevent IE from throwing an error for some hidden elements + } catch (e) {} + } + + this.triggered = false; + } + + return val; + }, + + handle: function(event) { + // returned undefined or false + var val, ret, namespace, all, handlers; + + event = arguments[0] = jQuery.event.fix( event || window.event ); + + // Namespaced event handlers + namespace = event.type.split("."); + event.type = namespace[0]; + namespace = namespace[1]; + // Cache this now, all = true means, any handler + all = !namespace && !event.exclusive; + + handlers = ( jQuery.data(this, "events") || {} )[event.type]; + + for ( var j in handlers ) { + var handler = handlers[j]; + + // Filter the functions by class + if ( all || handler.type == namespace ) { + // Pass in a reference to the handler function itself + // So that we can later remove it + event.handler = handler; + event.data = handler.data; + + ret = handler.apply( this, arguments ); + + if ( val !== false ) + val = ret; + + if ( ret === false ) { + event.preventDefault(); + event.stopPropagation(); + } + } + } + + return val; + }, + + fix: function(event) { + if ( event[expando] == true ) + return event; + + // store a copy of the original event object + // and "clone" to set read-only properties + var originalEvent = event; + event = { originalEvent: originalEvent }; + var props = "altKey attrChange attrName bubbles button cancelable charCode clientX clientY ctrlKey currentTarget data detail eventPhase fromElement handler keyCode metaKey newValue originalTarget pageX pageY prevValue relatedNode relatedTarget screenX screenY shiftKey srcElement target timeStamp toElement type view wheelDelta which".split(" "); + for ( var i=props.length; i; i-- ) + event[ props[i] ] = originalEvent[ props[i] ]; + + // Mark it as fixed + event[expando] = true; + + // add preventDefault and stopPropagation since + // they will not work on the clone + event.preventDefault = function() { + // if preventDefault exists run it on the original event + if (originalEvent.preventDefault) + originalEvent.preventDefault(); + // otherwise set the returnValue property of the original event to false (IE) + originalEvent.returnValue = false; + }; + event.stopPropagation = function() { + // if stopPropagation exists run it on the original event + if (originalEvent.stopPropagation) + originalEvent.stopPropagation(); + // otherwise set the cancelBubble property of the original event to true (IE) + originalEvent.cancelBubble = true; + }; + + // Fix timeStamp + event.timeStamp = event.timeStamp || now(); + + // Fix target property, if necessary + if ( !event.target ) + event.target = event.srcElement || document; // Fixes #1925 where srcElement might not be defined either + + // check if target is a textnode (safari) + if ( event.target.nodeType == 3 ) + event.target = event.target.parentNode; + + // Add relatedTarget, if necessary + if ( !event.relatedTarget && event.fromElement ) + event.relatedTarget = event.fromElement == event.target ? event.toElement : event.fromElement; + + // Calculate pageX/Y if missing and clientX/Y available + if ( event.pageX == null && event.clientX != null ) { + var doc = document.documentElement, body = document.body; + event.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc.clientLeft || 0); + event.pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc.clientTop || 0); + } + + // Add which for key events + if ( !event.which && ((event.charCode || event.charCode === 0) ? event.charCode : event.keyCode) ) + event.which = event.charCode || event.keyCode; + + // Add metaKey to non-Mac browsers (use ctrl for PC's and Meta for Macs) + if ( !event.metaKey && event.ctrlKey ) + event.metaKey = event.ctrlKey; + + // Add which for click: 1 == left; 2 == middle; 3 == right + // Note: button is not normalized, so don't use it + if ( !event.which && event.button ) + event.which = (event.button & 1 ? 1 : ( event.button & 2 ? 3 : ( event.button & 4 ? 2 : 0 ) )); + + return event; + }, + + proxy: function( fn, proxy ){ + // Set the guid of unique handler to the same of original handler, so it can be removed + proxy.guid = fn.guid = fn.guid || proxy.guid || this.guid++; + // So proxy can be declared as an argument + return proxy; + }, + + special: { + ready: { + setup: function() { + // Make sure the ready event is setup + bindReady(); + return; + }, + + teardown: function() { return; } + }, + + mouseenter: { + setup: function() { + if ( jQuery.browser.msie ) return false; + jQuery(this).bind("mouseover", jQuery.event.special.mouseenter.handler); + return true; + }, + + teardown: function() { + if ( jQuery.browser.msie ) return false; + jQuery(this).unbind("mouseover", jQuery.event.special.mouseenter.handler); + return true; + }, + + handler: function(event) { + // If we actually just moused on to a sub-element, ignore it + if ( withinElement(event, this) ) return true; + // Execute the right handlers by setting the event type to mouseenter + event.type = "mouseenter"; + return jQuery.event.handle.apply(this, arguments); + } + }, + + mouseleave: { + setup: function() { + if ( jQuery.browser.msie ) return false; + jQuery(this).bind("mouseout", jQuery.event.special.mouseleave.handler); + return true; + }, + + teardown: function() { + if ( jQuery.browser.msie ) return false; + jQuery(this).unbind("mouseout", jQuery.event.special.mouseleave.handler); + return true; + }, + + handler: function(event) { + // If we actually just moused on to a sub-element, ignore it + if ( withinElement(event, this) ) return true; + // Execute the right handlers by setting the event type to mouseleave + event.type = "mouseleave"; + return jQuery.event.handle.apply(this, arguments); + } + } + } +}; + +jQuery.fn.extend({ + bind: function( type, data, fn ) { + return type == "unload" ? this.one(type, data, fn) : this.each(function(){ + jQuery.event.add( this, type, fn || data, fn && data ); + }); + }, + + one: function( type, data, fn ) { + var one = jQuery.event.proxy( fn || data, function(event) { + jQuery(this).unbind(event, one); + return (fn || data).apply( this, arguments ); + }); + return this.each(function(){ + jQuery.event.add( this, type, one, fn && data); + }); + }, + + unbind: function( type, fn ) { + return this.each(function(){ + jQuery.event.remove( this, type, fn ); + }); + }, + + trigger: function( type, data, fn ) { + return this.each(function(){ + jQuery.event.trigger( type, data, this, true, fn ); + }); + }, + + triggerHandler: function( type, data, fn ) { + return this[0] && jQuery.event.trigger( type, data, this[0], false, fn ); + }, + + toggle: function( fn ) { + // Save reference to arguments for access in closure + var args = arguments, i = 1; + + // link all the functions, so any of them can unbind this click handler + while( i < args.length ) + jQuery.event.proxy( fn, args[i++] ); + + return this.click( jQuery.event.proxy( fn, function(event) { + // Figure out which function to execute + this.lastToggle = ( this.lastToggle || 0 ) % i; + + // Make sure that clicks stop + event.preventDefault(); + + // and execute the function + return args[ this.lastToggle++ ].apply( this, arguments ) || false; + })); + }, + + hover: function(fnOver, fnOut) { + return this.bind('mouseenter', fnOver).bind('mouseleave', fnOut); + }, + + ready: function(fn) { + // Attach the listeners + bindReady(); + + // If the DOM is already ready + if ( jQuery.isReady ) + // Execute the function immediately + fn.call( document, jQuery ); + + // Otherwise, remember the function for later + else + // Add the function to the wait list + jQuery.readyList.push( function() { return fn.call(this, jQuery); } ); + + return this; + } +}); + +jQuery.extend({ + isReady: false, + readyList: [], + // Handle when the DOM is ready + ready: function() { + // Make sure that the DOM is not already loaded + if ( !jQuery.isReady ) { + // Remember that the DOM is ready + jQuery.isReady = true; + + // If there are functions bound, to execute + if ( jQuery.readyList ) { + // Execute all of them + jQuery.each( jQuery.readyList, function(){ + this.call( document ); + }); + + // Reset the list of functions + jQuery.readyList = null; + } + + // Trigger any bound ready events + jQuery(document).triggerHandler("ready"); + } + } +}); + +var readyBound = false; + +function bindReady(){ + if ( readyBound ) return; + readyBound = true; + + // Mozilla, Opera (see further below for it) and webkit nightlies currently support this event + if ( document.addEventListener && !jQuery.browser.opera) + // Use the handy event callback + document.addEventListener( "DOMContentLoaded", jQuery.ready, false ); + + // If IE is used and is not in a frame + // Continually check to see if the document is ready + if ( jQuery.browser.msie && window == top ) (function(){ + if (jQuery.isReady) return; + try { + // If IE is used, use the trick by Diego Perini + // http://javascript.nwbox.com/IEContentLoaded/ + document.documentElement.doScroll("left"); + } catch( error ) { + setTimeout( arguments.callee, 0 ); + return; + } + // and execute any waiting functions + jQuery.ready(); + })(); + + if ( jQuery.browser.opera ) + document.addEventListener( "DOMContentLoaded", function () { + if (jQuery.isReady) return; + for (var i = 0; i < document.styleSheets.length; i++) + if (document.styleSheets[i].disabled) { + setTimeout( arguments.callee, 0 ); + return; + } + // and execute any waiting functions + jQuery.ready(); + }, false); + + if ( jQuery.browser.safari ) { + var numStyles; + (function(){ + if (jQuery.isReady) return; + if ( document.readyState != "loaded" && document.readyState != "complete" ) { + setTimeout( arguments.callee, 0 ); + return; + } + if ( numStyles === undefined ) + numStyles = jQuery("style, link[rel=stylesheet]").length; + if ( document.styleSheets.length != numStyles ) { + setTimeout( arguments.callee, 0 ); + return; + } + // and execute any waiting functions + jQuery.ready(); + })(); + } + + // A fallback to window.onload, that will always work + jQuery.event.add( window, "load", jQuery.ready ); +} + +jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," + + "mousedown,mouseup,mousemove,mouseover,mouseout,change,select," + + "submit,keydown,keypress,keyup,error").split(","), function(i, name){ + + // Handle event binding + jQuery.fn[name] = function(fn){ + return fn ? this.bind(name, fn) : this.trigger(name); + }; +}); + +// Checks if an event happened on an element within another element +// Used in jQuery.event.special.mouseenter and mouseleave handlers +var withinElement = function(event, elem) { + // Check if mouse(over|out) are still within the same parent element + var parent = event.relatedTarget; + // Traverse up the tree + while ( parent && parent != elem ) try { parent = parent.parentNode; } catch(error) { parent = elem; } + // Return true if we actually just moused on to a sub-element + return parent == elem; +}; + +// Prevent memory leaks in IE +// And prevent errors on refresh with events like mouseover in other browsers +// Window isn't included so as not to unbind existing unload events +jQuery(window).bind("unload", function() { + jQuery("*").add(document).unbind(); +}); +jQuery.fn.extend({ + // Keep a copy of the old load + _load: jQuery.fn.load, + + load: function( url, params, callback ) { + if ( typeof url != 'string' ) + return this._load( url ); + + var off = url.indexOf(" "); + if ( off >= 0 ) { + var selector = url.slice(off, url.length); + url = url.slice(0, off); + } + + callback = callback || function(){}; + + // Default to a GET request + var type = "GET"; + + // If the second parameter was provided + if ( params ) + // If it's a function + if ( jQuery.isFunction( params ) ) { + // We assume that it's the callback + callback = params; + params = null; + + // Otherwise, build a param string + } else { + params = jQuery.param( params ); + type = "POST"; + } + + var self = this; + + // Request the remote document + jQuery.ajax({ + url: url, + type: type, + dataType: "html", + data: params, + complete: function(res, status){ + // If successful, inject the HTML into all the matched elements + if ( status == "success" || status == "notmodified" ) + // See if a selector was specified + self.html( selector ? + // Create a dummy div to hold the results + jQuery("<div/>") + // inject the contents of the document in, removing the scripts + // to avoid any 'Permission Denied' errors in IE + .append(res.responseText.replace(/<script(.|\s)*?\/script>/g, "")) + + // Locate the specified elements + .find(selector) : + + // If not, just inject the full result + res.responseText ); + + self.each( callback, [res.responseText, status, res] ); + } + }); + return this; + }, + + serialize: function() { + return jQuery.param(this.serializeArray()); + }, + serializeArray: function() { + return this.map(function(){ + return jQuery.nodeName(this, "form") ? + jQuery.makeArray(this.elements) : this; + }) + .filter(function(){ + return this.name && !this.disabled && + (this.checked || /select|textarea/i.test(this.nodeName) || + /text|hidden|password/i.test(this.type)); + }) + .map(function(i, elem){ + var val = jQuery(this).val(); + return val == null ? null : + val.constructor == Array ? + jQuery.map( val, function(val, i){ + return {name: elem.name, value: val}; + }) : + {name: elem.name, value: val}; + }).get(); + } +}); + +// Attach a bunch of functions for handling common AJAX events +jQuery.each( "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess,ajaxSend".split(","), function(i,o){ + jQuery.fn[o] = function(f){ + return this.bind(o, f); + }; +}); + +var jsc = now(); + +jQuery.extend({ + get: function( url, data, callback, type ) { + // shift arguments if data argument was ommited + if ( jQuery.isFunction( data ) ) { + callback = data; + data = null; + } + + return jQuery.ajax({ + type: "GET", + url: url, + data: data, + success: callback, + dataType: type + }); + }, + + getScript: function( url, callback ) { + return jQuery.get(url, null, callback, "script"); + }, + + getJSON: function( url, data, callback ) { + return jQuery.get(url, data, callback, "json"); + }, + + post: function( url, data, callback, type ) { + if ( jQuery.isFunction( data ) ) { + callback = data; + data = {}; + } + + return jQuery.ajax({ + type: "POST", + url: url, + data: data, + success: callback, + dataType: type + }); + }, + + ajaxSetup: function( settings ) { + jQuery.extend( jQuery.ajaxSettings, settings ); + }, + + ajaxSettings: { + url: location.href, + global: true, + type: "GET", + timeout: 0, + contentType: "application/x-www-form-urlencoded", + processData: true, + async: true, + data: null, + username: null, + password: null, + accepts: { + xml: "application/xml, text/xml", + html: "text/html", + script: "text/javascript, application/javascript", + json: "application/json, text/javascript", + text: "text/plain", + _default: "*/*" + } + }, + + // Last-Modified header cache for next request + lastModified: {}, + + ajax: function( s ) { + // Extend the settings, but re-extend 's' so that it can be + // checked again later (in the test suite, specifically) + s = jQuery.extend(true, s, jQuery.extend(true, {}, jQuery.ajaxSettings, s)); + + var jsonp, jsre = /=\?(&|$)/g, status, data, + type = s.type.toUpperCase(); + + // convert data if not already a string + if ( s.data && s.processData && typeof s.data != "string" ) + s.data = jQuery.param(s.data); + + // Handle JSONP Parameter Callbacks + if ( s.dataType == "jsonp" ) { + if ( type == "GET" ) { + if ( !s.url.match(jsre) ) + s.url += (s.url.match(/\?/) ? "&" : "?") + (s.jsonp || "callback") + "=?"; + } else if ( !s.data || !s.data.match(jsre) ) + s.data = (s.data ? s.data + "&" : "") + (s.jsonp || "callback") + "=?"; + s.dataType = "json"; + } + + // Build temporary JSONP function + if ( s.dataType == "json" && (s.data && s.data.match(jsre) || s.url.match(jsre)) ) { + jsonp = "jsonp" + jsc++; + + // Replace the =? sequence both in the query string and the data + if ( s.data ) + s.data = (s.data + "").replace(jsre, "=" + jsonp + "$1"); + s.url = s.url.replace(jsre, "=" + jsonp + "$1"); + + // We need to make sure + // that a JSONP style response is executed properly + s.dataType = "script"; + + // Handle JSONP-style loading + window[ jsonp ] = function(tmp){ + data = tmp; + success(); + complete(); + // Garbage collect + window[ jsonp ] = undefined; + try{ delete window[ jsonp ]; } catch(e){} + if ( head ) + head.removeChild( script ); + }; + } + + if ( s.dataType == "script" && s.cache == null ) + s.cache = false; + + if ( s.cache === false && type == "GET" ) { + var ts = now(); + // try replacing _= if it is there + var ret = s.url.replace(/(\?|&)_=.*?(&|$)/, "$1_=" + ts + "$2"); + // if nothing was replaced, add timestamp to the end + s.url = ret + ((ret == s.url) ? (s.url.match(/\?/) ? "&" : "?") + "_=" + ts : ""); + } + + // If data is available, append data to url for get requests + if ( s.data && type == "GET" ) { + s.url += (s.url.match(/\?/) ? "&" : "?") + s.data; + + // IE likes to send both get and post data, prevent this + s.data = null; + } + + // Watch for a new set of requests + if ( s.global && ! jQuery.active++ ) + jQuery.event.trigger( "ajaxStart" ); + + // Matches an absolute URL, and saves the domain + var remote = /^(?:\w+:)?\/\/([^\/?#]+)/; + + // If we're requesting a remote document + // and trying to load JSON or Script with a GET + if ( s.dataType == "script" && type == "GET" + && remote.test(s.url) && remote.exec(s.url)[1] != location.host ){ + var head = document.getElementsByTagName("head")[0]; + var script = document.createElement("script"); + script.src = s.url; + if (s.scriptCharset) + script.charset = s.scriptCharset; + + // Handle Script loading + if ( !jsonp ) { + var done = false; + + // Attach handlers for all browsers + script.onload = script.onreadystatechange = function(){ + if ( !done && (!this.readyState || + this.readyState == "loaded" || this.readyState == "complete") ) { + done = true; + success(); + complete(); + head.removeChild( script ); + } + }; + } + + head.appendChild(script); + + // We handle everything using the script element injection + return undefined; + } + + var requestDone = false; + + // Create the request object; Microsoft failed to properly + // implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available + var xhr = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); + + // Open the socket + // Passing null username, generates a login popup on Opera (#2865) + if( s.username ) + xhr.open(type, s.url, s.async, s.username, s.password); + else + xhr.open(type, s.url, s.async); + + // Need an extra try/catch for cross domain requests in Firefox 3 + try { + // Set the correct header, if data is being sent + if ( s.data ) + xhr.setRequestHeader("Content-Type", s.contentType); + + // Set the If-Modified-Since header, if ifModified mode. + if ( s.ifModified ) + xhr.setRequestHeader("If-Modified-Since", + jQuery.lastModified[s.url] || "Thu, 01 Jan 1970 00:00:00 GMT" ); + + // Set header so the called script knows that it's an XMLHttpRequest + xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); + + // Set the Accepts header for the server, depending on the dataType + xhr.setRequestHeader("Accept", s.dataType && s.accepts[ s.dataType ] ? + s.accepts[ s.dataType ] + ", */*" : + s.accepts._default ); + } catch(e){} + + // Allow custom headers/mimetypes + if ( s.beforeSend && s.beforeSend(xhr, s) === false ) { + // cleanup active request counter + s.global && jQuery.active--; + // close opended socket + xhr.abort(); + return false; + } + + if ( s.global ) + jQuery.event.trigger("ajaxSend", [xhr, s]); + + // Wait for a response to come back + var onreadystatechange = function(isTimeout){ + // The transfer is complete and the data is available, or the request timed out + if ( !requestDone && xhr && (xhr.readyState == 4 || isTimeout == "timeout") ) { + requestDone = true; + + // clear poll interval + if (ival) { + clearInterval(ival); + ival = null; + } + + status = isTimeout == "timeout" && "timeout" || + !jQuery.httpSuccess( xhr ) && "error" || + s.ifModified && jQuery.httpNotModified( xhr, s.url ) && "notmodified" || + "success"; + + if ( status == "success" ) { + // Watch for, and catch, XML document parse errors + try { + // process the data (runs the xml through httpData regardless of callback) + data = jQuery.httpData( xhr, s.dataType, s.dataFilter ); + } catch(e) { + status = "parsererror"; + } + } + + // Make sure that the request was successful or notmodified + if ( status == "success" ) { + // Cache Last-Modified header, if ifModified mode. + var modRes; + try { + modRes = xhr.getResponseHeader("Last-Modified"); + } catch(e) {} // swallow exception thrown by FF if header is not available + + if ( s.ifModified && modRes ) + jQuery.lastModified[s.url] = modRes; + + // JSONP handles its own success callback + if ( !jsonp ) + success(); + } else + jQuery.handleError(s, xhr, status); + + // Fire the complete handlers + complete(); + + // Stop memory leaks + if ( s.async ) + xhr = null; + } + }; + + if ( s.async ) { + // don't attach the handler to the request, just poll it instead + var ival = setInterval(onreadystatechange, 13); + + // Timeout checker + if ( s.timeout > 0 ) + setTimeout(function(){ + // Check to see if the request is still happening + if ( xhr ) { + // Cancel the request + xhr.abort(); + + if( !requestDone ) + onreadystatechange( "timeout" ); + } + }, s.timeout); + } + + // Send the data + try { + xhr.send(s.data); + } catch(e) { + jQuery.handleError(s, xhr, null, e); + } + + // firefox 1.5 doesn't fire statechange for sync requests + if ( !s.async ) + onreadystatechange(); + + function success(){ + // If a local callback was specified, fire it and pass it the data + if ( s.success ) + s.success( data, status ); + + // Fire the global callback + if ( s.global ) + jQuery.event.trigger( "ajaxSuccess", [xhr, s] ); + } + + function complete(){ + // Process result + if ( s.complete ) + s.complete(xhr, status); + + // The request was completed + if ( s.global ) + jQuery.event.trigger( "ajaxComplete", [xhr, s] ); + + // Handle the global AJAX counter + if ( s.global && ! --jQuery.active ) + jQuery.event.trigger( "ajaxStop" ); + } + + // return XMLHttpRequest to allow aborting the request etc. + return xhr; + }, + + handleError: function( s, xhr, status, e ) { + // If a local callback was specified, fire it + if ( s.error ) s.error( xhr, status, e ); + + // Fire the global callback + if ( s.global ) + jQuery.event.trigger( "ajaxError", [xhr, s, e] ); + }, + + // Counter for holding the number of active queries + active: 0, + + // Determines if an XMLHttpRequest was successful or not + httpSuccess: function( xhr ) { + try { + // IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450 + return !xhr.status && location.protocol == "file:" || + ( xhr.status >= 200 && xhr.status < 300 ) || xhr.status == 304 || xhr.status == 1223 || + jQuery.browser.safari && xhr.status == undefined; + } catch(e){} + return false; + }, + + // Determines if an XMLHttpRequest returns NotModified + httpNotModified: function( xhr, url ) { + try { + var xhrRes = xhr.getResponseHeader("Last-Modified"); + + // Firefox always returns 200. check Last-Modified date + return xhr.status == 304 || xhrRes == jQuery.lastModified[url] || + jQuery.browser.safari && xhr.status == undefined; + } catch(e){} + return false; + }, + + httpData: function( xhr, type, filter ) { + var ct = xhr.getResponseHeader("content-type"), + xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0, + data = xml ? xhr.responseXML : xhr.responseText; + + if ( xml && data.documentElement.tagName == "parsererror" ) + throw "parsererror"; + + // Allow a pre-filtering function to sanitize the response + if( filter ) + data = filter( data, type ); + + // If the type is "script", eval it in global context + if ( type == "script" ) + jQuery.globalEval( data ); + + // Get the JavaScript object, if JSON is used. + if ( type == "json" ) + data = eval("(" + data + ")"); + + return data; + }, + + // Serialize an array of form elements or a set of + // key/values into a query string + param: function( a ) { + var s = []; + + // If an array was passed in, assume that it is an array + // of form elements + if ( a.constructor == Array || a.jquery ) + // Serialize the form elements + jQuery.each( a, function(){ + s.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( this.value ) ); + }); + + // Otherwise, assume that it's an object of key/value pairs + else + // Serialize the key/values + for ( var j in a ) + // If the value is an array then the key names need to be repeated + if ( a[j] && a[j].constructor == Array ) + jQuery.each( a[j], function(){ + s.push( encodeURIComponent(j) + "=" + encodeURIComponent( this ) ); + }); + else + s.push( encodeURIComponent(j) + "=" + encodeURIComponent( jQuery.isFunction(a[j]) ? a[j]() : a[j] ) ); + + // Return the resulting serialization + return s.join("&").replace(/%20/g, "+"); + } + +}); +jQuery.fn.extend({ + show: function(speed,callback){ + return speed ? + this.animate({ + height: "show", width: "show", opacity: "show" + }, speed, callback) : + + this.filter(":hidden").each(function(){ + this.style.display = this.oldblock || ""; + if ( jQuery.css(this,"display") == "none" ) { + var elem = jQuery("<" + this.tagName + " />").appendTo("body"); + this.style.display = elem.css("display"); + // handle an edge condition where css is - div { display:none; } or similar + if (this.style.display == "none") + this.style.display = "block"; + elem.remove(); + } + }).end(); + }, + + hide: function(speed,callback){ + return speed ? + this.animate({ + height: "hide", width: "hide", opacity: "hide" + }, speed, callback) : + + this.filter(":visible").each(function(){ + this.oldblock = this.oldblock || jQuery.css(this,"display"); + this.style.display = "none"; + }).end(); + }, + + // Save the old toggle function + _toggle: jQuery.fn.toggle, + + toggle: function( fn, fn2 ){ + return jQuery.isFunction(fn) && jQuery.isFunction(fn2) ? + this._toggle.apply( this, arguments ) : + fn ? + this.animate({ + height: "toggle", width: "toggle", opacity: "toggle" + }, fn, fn2) : + this.each(function(){ + jQuery(this)[ jQuery(this).is(":hidden") ? "show" : "hide" ](); + }); + }, + + slideDown: function(speed,callback){ + return this.animate({height: "show"}, speed, callback); + }, + + slideUp: function(speed,callback){ + return this.animate({height: "hide"}, speed, callback); + }, + + slideToggle: function(speed, callback){ + return this.animate({height: "toggle"}, speed, callback); + }, + + fadeIn: function(speed, callback){ + return this.animate({opacity: "show"}, speed, callback); + }, + + fadeOut: function(speed, callback){ + return this.animate({opacity: "hide"}, speed, callback); + }, + + fadeTo: function(speed,to,callback){ + return this.animate({opacity: to}, speed, callback); + }, + + animate: function( prop, speed, easing, callback ) { + var optall = jQuery.speed(speed, easing, callback); + + return this[ optall.queue === false ? "each" : "queue" ](function(){ + if ( this.nodeType != 1) + return false; + + var opt = jQuery.extend({}, optall), p, + hidden = jQuery(this).is(":hidden"), self = this; + + for ( p in prop ) { + if ( prop[p] == "hide" && hidden || prop[p] == "show" && !hidden ) + return opt.complete.call(this); + + if ( p == "height" || p == "width" ) { + // Store display property + opt.display = jQuery.css(this, "display"); + + // Make sure that nothing sneaks out + opt.overflow = this.style.overflow; + } + } + + if ( opt.overflow != null ) + this.style.overflow = "hidden"; + + opt.curAnim = jQuery.extend({}, prop); + + jQuery.each( prop, function(name, val){ + var e = new jQuery.fx( self, opt, name ); + + if ( /toggle|show|hide/.test(val) ) + e[ val == "toggle" ? hidden ? "show" : "hide" : val ]( prop ); + else { + var parts = val.toString().match(/^([+-]=)?([\d+-.]+)(.*)$/), + start = e.cur(true) || 0; + + if ( parts ) { + var end = parseFloat(parts[2]), + unit = parts[3] || "px"; + + // We need to compute starting value + if ( unit != "px" ) { + self.style[ name ] = (end || 1) + unit; + start = ((end || 1) / e.cur(true)) * start; + self.style[ name ] = start + unit; + } + + // If a +=/-= token was provided, we're doing a relative animation + if ( parts[1] ) + end = ((parts[1] == "-=" ? -1 : 1) * end) + start; + + e.custom( start, end, unit ); + } else + e.custom( start, val, "" ); + } + }); + + // For JS strict compliance + return true; + }); + }, + + queue: function(type, fn){ + if ( jQuery.isFunction(type) || ( type && type.constructor == Array )) { + fn = type; + type = "fx"; + } + + if ( !type || (typeof type == "string" && !fn) ) + return queue( this[0], type ); + + return this.each(function(){ + if ( fn.constructor == Array ) + queue(this, type, fn); + else { + queue(this, type).push( fn ); + + if ( queue(this, type).length == 1 ) + fn.call(this); + } + }); + }, + + stop: function(clearQueue, gotoEnd){ + var timers = jQuery.timers; + + if (clearQueue) + this.queue([]); + + this.each(function(){ + // go in reverse order so anything added to the queue during the loop is ignored + for ( var i = timers.length - 1; i >= 0; i-- ) + if ( timers[i].elem == this ) { + if (gotoEnd) + // force the next step to be the last + timers[i](true); + timers.splice(i, 1); + } + }); + + // start the next in the queue if the last step wasn't forced + if (!gotoEnd) + this.dequeue(); + + return this; + } + +}); + +var queue = function( elem, type, array ) { + if ( elem ){ + + type = type || "fx"; + + var q = jQuery.data( elem, type + "queue" ); + + if ( !q || array ) + q = jQuery.data( elem, type + "queue", jQuery.makeArray(array) ); + + } + return q; +}; + +jQuery.fn.dequeue = function(type){ + type = type || "fx"; + + return this.each(function(){ + var q = queue(this, type); + + q.shift(); + + if ( q.length ) + q[0].call( this ); + }); +}; + +jQuery.extend({ + + speed: function(speed, easing, fn) { + var opt = speed && speed.constructor == Object ? speed : { + complete: fn || !fn && easing || + jQuery.isFunction( speed ) && speed, + duration: speed, + easing: fn && easing || easing && easing.constructor != Function && easing + }; + + opt.duration = (opt.duration && opt.duration.constructor == Number ? + opt.duration : + jQuery.fx.speeds[opt.duration]) || jQuery.fx.speeds.def; + + // Queueing + opt.old = opt.complete; + opt.complete = function(){ + if ( opt.queue !== false ) + jQuery(this).dequeue(); + if ( jQuery.isFunction( opt.old ) ) + opt.old.call( this ); + }; + + return opt; + }, + + easing: { + linear: function( p, n, firstNum, diff ) { + return firstNum + diff * p; + }, + swing: function( p, n, firstNum, diff ) { + return ((-Math.cos(p*Math.PI)/2) + 0.5) * diff + firstNum; + } + }, + + timers: [], + timerId: null, + + fx: function( elem, options, prop ){ + this.options = options; + this.elem = elem; + this.prop = prop; + + if ( !options.orig ) + options.orig = {}; + } + +}); + +jQuery.fx.prototype = { + + // Simple function for setting a style value + update: function(){ + if ( this.options.step ) + this.options.step.call( this.elem, this.now, this ); + + (jQuery.fx.step[this.prop] || jQuery.fx.step._default)( this ); + + // Set display property to block for height/width animations + if ( this.prop == "height" || this.prop == "width" ) + this.elem.style.display = "block"; + }, + + // Get the current size + cur: function(force){ + if ( this.elem[this.prop] != null && this.elem.style[this.prop] == null ) + return this.elem[ this.prop ]; + + var r = parseFloat(jQuery.css(this.elem, this.prop, force)); + return r && r > -10000 ? r : parseFloat(jQuery.curCSS(this.elem, this.prop)) || 0; + }, + + // Start an animation from one number to another + custom: function(from, to, unit){ + this.startTime = now(); + this.start = from; + this.end = to; + this.unit = unit || this.unit || "px"; + this.now = this.start; + this.pos = this.state = 0; + this.update(); + + var self = this; + function t(gotoEnd){ + return self.step(gotoEnd); + } + + t.elem = this.elem; + + jQuery.timers.push(t); + + if ( jQuery.timerId == null ) { + jQuery.timerId = setInterval(function(){ + var timers = jQuery.timers; + + for ( var i = 0; i < timers.length; i++ ) + if ( !timers[i]() ) + timers.splice(i--, 1); + + if ( !timers.length ) { + clearInterval( jQuery.timerId ); + jQuery.timerId = null; + } + }, 13); + } + }, + + // Simple 'show' function + show: function(){ + // Remember where we started, so that we can go back to it later + this.options.orig[this.prop] = jQuery.attr( this.elem.style, this.prop ); + this.options.show = true; + + // Begin the animation + this.custom(0, this.cur()); + + // Make sure that we start at a small width/height to avoid any + // flash of content + if ( this.prop == "width" || this.prop == "height" ) + this.elem.style[this.prop] = "1px"; + + // Start by showing the element + jQuery(this.elem).show(); + }, + + // Simple 'hide' function + hide: function(){ + // Remember where we started, so that we can go back to it later + this.options.orig[this.prop] = jQuery.attr( this.elem.style, this.prop ); + this.options.hide = true; + + // Begin the animation + this.custom(this.cur(), 0); + }, + + // Each step of an animation + step: function(gotoEnd){ + var t = now(); + + if ( gotoEnd || t > this.options.duration + this.startTime ) { + this.now = this.end; + this.pos = this.state = 1; + this.update(); + + this.options.curAnim[ this.prop ] = true; + + var done = true; + for ( var i in this.options.curAnim ) + if ( this.options.curAnim[i] !== true ) + done = false; + + if ( done ) { + if ( this.options.display != null ) { + // Reset the overflow + this.elem.style.overflow = this.options.overflow; + + // Reset the display + this.elem.style.display = this.options.display; + if ( jQuery.css(this.elem, "display") == "none" ) + this.elem.style.display = "block"; + } + + // Hide the element if the "hide" operation was done + if ( this.options.hide ) + this.elem.style.display = "none"; + + // Reset the properties, if the item has been hidden or shown + if ( this.options.hide || this.options.show ) + for ( var p in this.options.curAnim ) + jQuery.attr(this.elem.style, p, this.options.orig[p]); + } + + if ( done ) + // Execute the complete function + this.options.complete.call( this.elem ); + + return false; + } else { + var n = t - this.startTime; + this.state = n / this.options.duration; + + // Perform the easing function, defaults to swing + this.pos = jQuery.easing[this.options.easing || (jQuery.easing.swing ? "swing" : "linear")](this.state, n, 0, 1, this.options.duration); + this.now = this.start + ((this.end - this.start) * this.pos); + + // Perform the next step of the animation + this.update(); + } + + return true; + } + +}; + +jQuery.extend( jQuery.fx, { + speeds:{ + slow: 600, + fast: 200, + // Default speed + def: 400 + }, + step: { + scrollLeft: function(fx){ + fx.elem.scrollLeft = fx.now; + }, + + scrollTop: function(fx){ + fx.elem.scrollTop = fx.now; + }, + + opacity: function(fx){ + jQuery.attr(fx.elem.style, "opacity", fx.now); + }, + + _default: function(fx){ + fx.elem.style[ fx.prop ] = fx.now + fx.unit; + } + } +}); +// The Offset Method +// Originally By Brandon Aaron, part of the Dimension Plugin +// http://jquery.com/plugins/project/dimensions +jQuery.fn.offset = function() { + var left = 0, top = 0, elem = this[0], results; + + if ( elem ) with ( jQuery.browser ) { + var parent = elem.parentNode, + offsetChild = elem, + offsetParent = elem.offsetParent, + doc = elem.ownerDocument, + safari2 = safari && parseInt(version) < 522 && !/adobeair/i.test(userAgent), + css = jQuery.curCSS, + fixed = css(elem, "position") == "fixed"; + + // Use getBoundingClientRect if available + if ( elem.getBoundingClientRect ) { + var box = elem.getBoundingClientRect(); + + // Add the document scroll offsets + add(box.left + Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft), + box.top + Math.max(doc.documentElement.scrollTop, doc.body.scrollTop)); + + // IE adds the HTML element's border, by default it is medium which is 2px + // IE 6 and 7 quirks mode the border width is overwritable by the following css html { border: 0; } + // IE 7 standards mode, the border is always 2px + // This border/offset is typically represented by the clientLeft and clientTop properties + // However, in IE6 and 7 quirks mode the clientLeft and clientTop properties are not updated when overwriting it via CSS + // Therefore this method will be off by 2px in IE while in quirksmode + add( -doc.documentElement.clientLeft, -doc.documentElement.clientTop ); + + // Otherwise loop through the offsetParents and parentNodes + } else { + + // Initial element offsets + add( elem.offsetLeft, elem.offsetTop ); + + // Get parent offsets + while ( offsetParent ) { + // Add offsetParent offsets + add( offsetParent.offsetLeft, offsetParent.offsetTop ); + + // Mozilla and Safari > 2 does not include the border on offset parents + // However Mozilla adds the border for table or table cells + if ( mozilla && !/^t(able|d|h)$/i.test(offsetParent.tagName) || safari && !safari2 ) + border( offsetParent ); + + // Add the document scroll offsets if position is fixed on any offsetParent + if ( !fixed && css(offsetParent, "position") == "fixed" ) + fixed = true; + + // Set offsetChild to previous offsetParent unless it is the body element + offsetChild = /^body$/i.test(offsetParent.tagName) ? offsetChild : offsetParent; + // Get next offsetParent + offsetParent = offsetParent.offsetParent; + } + + // Get parent scroll offsets + while ( parent && parent.tagName && !/^body|html$/i.test(parent.tagName) ) { + // Remove parent scroll UNLESS that parent is inline or a table to work around Opera inline/table scrollLeft/Top bug + if ( !/^inline|table.*$/i.test(css(parent, "display")) ) + // Subtract parent scroll offsets + add( -parent.scrollLeft, -parent.scrollTop ); + + // Mozilla does not add the border for a parent that has overflow != visible + if ( mozilla && css(parent, "overflow") != "visible" ) + border( parent ); + + // Get next parent + parent = parent.parentNode; + } + + // Safari <= 2 doubles body offsets with a fixed position element/offsetParent or absolutely positioned offsetChild + // Mozilla doubles body offsets with a non-absolutely positioned offsetChild + if ( (safari2 && (fixed || css(offsetChild, "position") == "absolute")) || + (mozilla && css(offsetChild, "position") != "absolute") ) + add( -doc.body.offsetLeft, -doc.body.offsetTop ); + + // Add the document scroll offsets if position is fixed + if ( fixed ) + add(Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft), + Math.max(doc.documentElement.scrollTop, doc.body.scrollTop)); + } + + // Return an object with top and left properties + results = { top: top, left: left }; + } + + function border(elem) { + add( jQuery.curCSS(elem, "borderLeftWidth", true), jQuery.curCSS(elem, "borderTopWidth", true) ); + } + + function add(l, t) { + left += parseInt(l, 10) || 0; + top += parseInt(t, 10) || 0; + } + + return results; +}; + + +jQuery.fn.extend({ + position: function() { + var left = 0, top = 0, results; + + if ( this[0] ) { + // Get *real* offsetParent + var offsetParent = this.offsetParent(), + + // Get correct offsets + offset = this.offset(), + parentOffset = /^body|html$/i.test(offsetParent[0].tagName) ? { top: 0, left: 0 } : offsetParent.offset(); + + // Subtract element margins + // note: when an element has margin: auto the offsetLeft and marginLeft + // are the same in Safari causing offset.left to incorrectly be 0 + offset.top -= num( this, 'marginTop' ); + offset.left -= num( this, 'marginLeft' ); + + // Add offsetParent borders + parentOffset.top += num( offsetParent, 'borderTopWidth' ); + parentOffset.left += num( offsetParent, 'borderLeftWidth' ); + + // Subtract the two offsets + results = { + top: offset.top - parentOffset.top, + left: offset.left - parentOffset.left + }; + } + + return results; + }, + + offsetParent: function() { + var offsetParent = this[0].offsetParent; + while ( offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && jQuery.css(offsetParent, 'position') == 'static') ) + offsetParent = offsetParent.offsetParent; + return jQuery(offsetParent); + } +}); + + +// Create scrollLeft and scrollTop methods +jQuery.each( ['Left', 'Top'], function(i, name) { + var method = 'scroll' + name; + + jQuery.fn[ method ] = function(val) { + if (!this[0]) return; + + return val != undefined ? + + // Set the scroll offset + this.each(function() { + this == window || this == document ? + window.scrollTo( + !i ? val : jQuery(window).scrollLeft(), + i ? val : jQuery(window).scrollTop() + ) : + this[ method ] = val; + }) : + + // Return the scroll offset + this[0] == window || this[0] == document ? + self[ i ? 'pageYOffset' : 'pageXOffset' ] || + jQuery.boxModel && document.documentElement[ method ] || + document.body[ method ] : + this[0][ method ]; + }; +}); +// Create innerHeight, innerWidth, outerHeight and outerWidth methods +jQuery.each([ "Height", "Width" ], function(i, name){ + + var tl = i ? "Left" : "Top", // top or left + br = i ? "Right" : "Bottom"; // bottom or right + + // innerHeight and innerWidth + jQuery.fn["inner" + name] = function(){ + return this[ name.toLowerCase() ]() + + num(this, "padding" + tl) + + num(this, "padding" + br); + }; + + // outerHeight and outerWidth + jQuery.fn["outer" + name] = function(margin) { + return this["inner" + name]() + + num(this, "border" + tl + "Width") + + num(this, "border" + br + "Width") + + (margin ? + num(this, "margin" + tl) + num(this, "margin" + br) : 0); + }; + +});})(); diff --git a/dom/tests/mochitest/ajax/jquery/manifest.json b/dom/tests/mochitest/ajax/jquery/manifest.json new file mode 100644 index 0000000000..d33c00c7f1 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/manifest.json @@ -0,0 +1 @@ +{testcases:["test/index.html"]} diff --git a/dom/tests/mochitest/ajax/jquery/mochitest.ini b/dom/tests/mochitest/ajax/jquery/mochitest.ini new file mode 100644 index 0000000000..3f10b1cf77 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/mochitest.ini @@ -0,0 +1,37 @@ +[DEFAULT] +support-files = + manifest.json + dist/jquery.js + test/index.html + test/offset.html + test/test.js + test/fix.html + test/data/cow.jpg + test/data/dashboard.xml + test/data/iframe.html + test/data/json_assigned_obj.js + test/data/json_obj.js + test/data/name.html + test/data/test.html + test/data/test.js + test/data/test2.html + test/data/test3.html + test/data/testrunner.js + test/data/testsuite.css + test/data/with_fries.xml + test/data/offset/absolute.html + test/data/offset/fixed.html + test/data/offset/relative.html + test/data/offset/scroll.html + test/data/offset/static.html + test/data/offset/table.html + test/unit/ajax.js + test/unit/core.js + test/unit/dimensions.js + test/unit/event.js + test/unit/fx.js + test/unit/offset.js + test/unit/selector.js + +[test_jQuery.html] +skip-if = (os == "android") # Bug 1780219 diff --git a/dom/tests/mochitest/ajax/jquery/test/data/cow.jpg b/dom/tests/mochitest/ajax/jquery/test/data/cow.jpg Binary files differnew file mode 100644 index 0000000000..2c5b672259 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/data/cow.jpg diff --git a/dom/tests/mochitest/ajax/jquery/test/data/dashboard.xml b/dom/tests/mochitest/ajax/jquery/test/data/dashboard.xml new file mode 100644 index 0000000000..10f6b33479 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/data/dashboard.xml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<dashboard> + <locations class="foo"> + <location for="bar"> + <infowindowtab> + <tab title="Location"><![CDATA[blabla]]></tab> + <tab title="Users"><![CDATA[blublu]]></tab> + </infowindowtab> + </location> + </locations> +</dashboard> diff --git a/dom/tests/mochitest/ajax/jquery/test/data/iframe.html b/dom/tests/mochitest/ajax/jquery/test/data/iframe.html new file mode 100644 index 0000000000..3ff26e1612 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/data/iframe.html @@ -0,0 +1,8 @@ +<html> + <head> + <title>iframe</title> + </head> + <body> + <div><span>span text</span></div> + </body> +</html> diff --git a/dom/tests/mochitest/ajax/jquery/test/data/json_assigned_obj.js b/dom/tests/mochitest/ajax/jquery/test/data/json_assigned_obj.js new file mode 100644 index 0000000000..867251dad1 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/data/json_assigned_obj.js @@ -0,0 +1 @@ +json_assigned_obj = { "test" : "worked" }; diff --git a/dom/tests/mochitest/ajax/jquery/test/data/json_obj.js b/dom/tests/mochitest/ajax/jquery/test/data/json_obj.js new file mode 100644 index 0000000000..7fa61820f8 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/data/json_obj.js @@ -0,0 +1 @@ +{ "data": {"lang": "en", "length": 25} } diff --git a/dom/tests/mochitest/ajax/jquery/test/data/name.html b/dom/tests/mochitest/ajax/jquery/test/data/name.html new file mode 100644 index 0000000000..0fa32d1a82 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/data/name.html @@ -0,0 +1 @@ +ERROR <script type="text/javascript">ok( true, "name.html retrieved" );</script> diff --git a/dom/tests/mochitest/ajax/jquery/test/data/offset/absolute.html b/dom/tests/mochitest/ajax/jquery/test/data/offset/absolute.html new file mode 100644 index 0000000000..3bbc8417dc --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/data/offset/absolute.html @@ -0,0 +1,39 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html> + <head> + <meta http-equiv="Content-type" content="text/html; charset=utf-8"> + <title>absolute</title> + <style type="text/css" media="screen"> + body { margin: 1px; padding: 5px; } + div.absolute { position: absolute; margin: 1px; border: 2px solid #000; padding: 5px; width: 100px; height: 100px; background: #fff; } + #absolute-1 { top: 0; left: 0; } + #absolute-1-1 { top: 1px; left: 1px; } + #absolute-1-1-1 { top: 1px; left: 1px; } + #absolute-2 { top: 19px; left: 19px; } + #marker { position: absolute; border: 2px solid #000; width: 50px; height: 50px; background: #ccc; } + p.instructions { position: absolute; bottom: 0; } + </style> + <script type="text/javascript" src="../../../dist/jquery.js"></script> + <script type="text/javascript" charset="utf-8"> + $(function() { + $('.absolute').click(function() { + $('#marker').css( $(this).offset() ); + var pos = $(this).position(); + $(this).css({ top: pos.top, left: pos.left }); + return false; + }); + }); + </script> + </head> + <body> + <div id="absolute-1" class="absolute">absolute-1 + <div id="absolute-1-1" class="absolute">absolute-1-1 + <div id="absolute-1-1-1" class="absolute">absolute-1-1-1</div> + </div> + </div> + <div id="absolute-2" class="absolute">absolute-2</div> + <div id="marker"></div> + <p class="instructions">Click the white box to move the marker to it. Clicking the box also changes the position to absolute (if not already) and sets the position according to the position method.</p> + </body> +</html>
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/jquery/test/data/offset/fixed.html b/dom/tests/mochitest/ajax/jquery/test/data/offset/fixed.html new file mode 100644 index 0000000000..8d69aae94d --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/data/offset/fixed.html @@ -0,0 +1,33 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html> + <head> + <meta http-equiv="Content-type" content="text/html; charset=utf-8"> + <title>fixed</title> + <style type="text/css" media="screen"> + body { margin: 1px; padding: 5px; } + div.fixed { position: fixed; margin: 1px; border: 2px solid #000; padding: 5px; width: 100px; height: 100px; background: #fff; overflow: hidden; } + #fixed-1 { top: 0; left: 0; } + #fixed-2 { top: 20px; left: 20px; } + #forceScroll { width: 5000px; height: 5000px; } + #marker { position: absolute; border: 2px solid #000; width: 50px; height: 50px; background: #ccc; } + </style> + <script type="text/javascript" src="../../../dist/jquery.js"></script> + <script type="text/javascript" charset="utf-8"> + $(function() { + window.scrollTo(1000,1000); + $('.fixed').click(function() { + $('#marker').css( $(this).offset() ); + return false; + }); + }); + </script> + </head> + <body> + <div id="fixed-1" class="fixed"></div> + <div id="fixed-2" class="fixed"></div> + <div id="forceScroll"></div> + <div id="marker"></div> + <p class="instructions">Click the white box to move the marker to it.</p> + </body> +</html>
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/jquery/test/data/offset/relative.html b/dom/tests/mochitest/ajax/jquery/test/data/offset/relative.html new file mode 100644 index 0000000000..89aae83e4a --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/data/offset/relative.html @@ -0,0 +1,31 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html> + <head> + <meta http-equiv="Content-type" content="text/html; charset=utf-8"> + <title>relative</title> + <style type="text/css" media="screen"> + body { margin: 1px; padding: 5px; } + div.relative { position: relative; margin: 1px; border: 2px solid #000; padding: 5px; width: 100px; height: 100px; background: #fff; overflow: hidden; } + #relative-2 { top: 20px; left: 20px; } + #marker { position: absolute; border: 2px solid #000; width: 50px; height: 50px; background: #ccc; } + </style> + <script type="text/javascript" src="../../../dist/jquery.js"></script> + <script type="text/javascript" charset="utf-8"> + $(function() { + $('.relative').click(function() { + $('#marker').css( $(this).offset() ); + var pos = $(this).position(); + $(this).css({ position: 'absolute', top: pos.top, left: pos.left }); + return false; + }); + }); + </script> + </head> + <body> + <div id="relative-1" class="relative"><div id="relative-1-1" class="relative"><div id="relative-1-1-1" class="relative"></div></div></div> + <div id="relative-2" class="relative"></div> + <div id="marker"></div> + <p class="instructions">Click the white box to move the marker to it. Clicking the box also changes the position to absolute (if not already) and sets the position according to the position method.</p> + </body> +</html>
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/jquery/test/data/offset/scroll.html b/dom/tests/mochitest/ajax/jquery/test/data/offset/scroll.html new file mode 100644 index 0000000000..494f640581 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/data/offset/scroll.html @@ -0,0 +1,39 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html> + <head> + <meta http-equiv="Content-type" content="text/html; charset=utf-8"> + <title>scroll</title> + <style type="text/css" media="screen"> + body { margin: 1px; padding: 5px; } + div.scroll { position: relative; margin: 1px; border: 2px solid #000; padding: 5px; width: 100px; height: 100px; background: #fff; overflow: auto; } + #scroll-1 { top: 0; left: 0; } + #scroll-1-1 { top: 1px; left: 1px; } + #scroll-1-1-1 { top: 1px; left: 1px; } + #forceScroll { width: 5000px; height: 5000px; } + #marker { position: absolute; border: 2px solid #000; width: 50px; height: 50px; background: #ccc; } + </style> + <script type="text/javascript" src="../../../dist/jquery.js"></script> + <script type="text/javascript" charset="utf-8"> + $(function() { + window.scrollTo(1000,1000); + $('#scroll-1')[0].scrollLeft = 5; + $('#scroll-1')[0].scrollTop = 5; + $('.scroll').click(function() { + $('#marker').css( $(this).offset() ); + return false; + }); + }); + </script> + </head> + <body> + <div id="scroll-1" class="scroll"> + <div id="scroll-1-1" class="scroll"> + <div id="scroll-1-1-1" class="scroll"></div> + </div> + </div> + <div id="forceScroll"></div> + <div id="marker"></div> + <p class="instructions">Click the white box to move the marker to it.</p> + </body> +</html>
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/jquery/test/data/offset/static.html b/dom/tests/mochitest/ajax/jquery/test/data/offset/static.html new file mode 100644 index 0000000000..10116af65b --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/data/offset/static.html @@ -0,0 +1,31 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html> + <head> + <meta http-equiv="Content-type" content="text/html; charset=utf-8"> + <title>static</title> + <style type="text/css" media="screen"> + body { margin: 1px; padding: 5px; } + div.static { position: static; margin: 1px; border: 2px solid #000; padding: 5px; width: 100px; height: 100px; background: #fff; overflow: hidden; } + #static-2 { top: 20px; left: 20px; } + #marker { position: absolute; border: 2px solid #000; width: 50px; height: 50px; background: #ccc; } + </style> + <script type="text/javascript" src="../../../dist/jquery.js"></script> + <script type="text/javascript" charset="utf-8"> + $(function() { + $('.static').click(function() { + $('#marker').css( $(this).offset() ); + var pos = $(this).position(); + $(this).css({ position: 'absolute', top: pos.top, left: pos.left }); + return false; + }); + }); + </script> + </head> + <body> + <div id="static-1" class="static"><div id="static-1-1" class="static"><div id="static-1-1-1" class="static"></div></div></div> + <div id="static-2" class="static"></div> + <div id="marker"></div> + <p class="instructions">Click the white box to move the marker to it. Clicking the box also changes the position to absolute (if not already) and sets the position according to the position method.</p> + </body> +</html>
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/jquery/test/data/offset/table.html b/dom/tests/mochitest/ajax/jquery/test/data/offset/table.html new file mode 100644 index 0000000000..5d15b903b6 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/data/offset/table.html @@ -0,0 +1,43 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html> + <head> + <meta http-equiv="Content-type" content="text/html; charset=utf-8"> + <title>table</title> + <style type="text/css" media="screen"> + body { margin: 1px; padding: 5px; } + table { border: 2px solid #000; } + th, td { border: 1px solid #000; width: 100px; height: 100px; } + #marker { position: absolute; border: 2px solid #000; width: 50px; height: 50px; background: #ccc; } + </style> + <script type="text/javascript" src="../../../dist/jquery.js"></script> + <script type="text/javascript" charset="utf-8"> + $(function() { + $('table, th, td').click(function() { + $('#marker').css( $(this).offset() ); + return false; + }); + }); + </script> + </head> + <body> + <table id="table-1"> + <thead> + <tr valign="top"> + <th id="th-1">th-1</th> + <th id="th-2">th-2</th> + <th id="th-3">th-3</th> + </tr> + </thead> + <tbody> + <tr valign="top"> + <td id="td-1">td-1</td> + <td id="td-2">td-2</td> + <td id="td-3">td-3</td> + </tr> + </tbody> + </table> + <div id="marker"></div> + <p class="instructions">Click the white box to move the marker to it.</p> + </body> +</html>
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/jquery/test/data/test.html b/dom/tests/mochitest/ajax/jquery/test/data/test.html new file mode 100644 index 0000000000..fc6dc38607 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/data/test.html @@ -0,0 +1,7 @@ +html text<br/> +<script type="text/javascript">/* <![CDATA[ */ +testFoo = "foo"; $('#foo').html('foo'); +ok( true, "test.html executed" ); +/* ]]> */</script> +<script src="data/test.js"></script> +blabla diff --git a/dom/tests/mochitest/ajax/jquery/test/data/test.js b/dom/tests/mochitest/ajax/jquery/test/data/test.js new file mode 100644 index 0000000000..f8bdd091e4 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/data/test.js @@ -0,0 +1,3 @@ +var foobar = "bar";
+$('#ap').html('bar');
+ok( true, "test.js executed");
diff --git a/dom/tests/mochitest/ajax/jquery/test/data/test2.html b/dom/tests/mochitest/ajax/jquery/test/data/test2.html new file mode 100644 index 0000000000..ebf610e438 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/data/test2.html @@ -0,0 +1,5 @@ +<script type="text/javascript"> +var testFoo = "foo"; +$('#foo').html('foo'); +ok( true, "test2.html executed" ); +</script> diff --git a/dom/tests/mochitest/ajax/jquery/test/data/test3.html b/dom/tests/mochitest/ajax/jquery/test/data/test3.html new file mode 100644 index 0000000000..909d41745c --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/data/test3.html @@ -0,0 +1,3 @@ +<div class="user">This is a user</div> +<div class="user">This is a user</div> +<div class="teacher">This is a teacher</div> diff --git a/dom/tests/mochitest/ajax/jquery/test/data/testrunner.js b/dom/tests/mochitest/ajax/jquery/test/data/testrunner.js new file mode 100644 index 0000000000..24bf0a863a --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/data/testrunner.js @@ -0,0 +1,334 @@ +var _config = { + fixture: null, + Test: [], + stats: { + all: 0, + bad: 0 + }, + queue: [], + blocking: true, + timeout: null, + expected: null, + currentModule: null, + asyncTimeout: 2 // seconds for async timeout +}; + +_config.filters = location.search.length > 1 && //restrict modules/tests by get parameters + $.map( location.search.slice(1).split('&'), decodeURIComponent ); + +var isLocal = !!(window.location.protocol == 'file:'); + +$(function() { + $('#userAgent').html(navigator.userAgent); + runTest(); +}); + +function synchronize(callback) { + _config.queue[_config.queue.length] = callback; + if(!_config.blocking) { + process(); + } +} + +function process() { + while(_config.queue.length && !_config.blocking) { + var call = _config.queue[0]; + _config.queue = _config.queue.slice(1); + call(); + } +} + +function stop(allowFailure) { + _config.blocking = true; + var handler = allowFailure ? start : function() { + ok( false, "Test timed out" ); + start(); + }; + // Disabled, caused too many random errors + //_config.timeout = setTimeout(handler, _config.asyncTimeout * 1000); +} +function start() { + // A slight delay, to avoid any current callbacks + setTimeout(function(){ + if(_config.timeout) + clearTimeout(_config.timeout); + _config.blocking = false; + process(); + }, 13); +} + +function validTest( name ) { + var filters = _config.filters; + if( !filters ) + return true; + + var i = filters.length, + run = false; + while( i-- ){ + var filter = filters[i], + not = filter.charAt(0) == '!'; + if( not ) + filter = filter.slice(1); + if( name.indexOf(filter) != -1 ) + return !not; + if( not ) + run = true; + } + return run; +} + +function runTest() { + _config.blocking = false; + var time = new Date(); + _config.fixture = document.getElementById('main').innerHTML; + _config.ajaxSettings = $.ajaxSettings; + synchronize(function() { + time = new Date() - time; + $("<div>").html(['<p class="result">Tests completed in ', + time, ' milliseconds.<br/>', + _config.stats.bad, ' tests of ', _config.stats.all, ' failed.</p>'] + .join('')) + .appendTo("body"); + $("#banner").addClass(_config.stats.bad ? "fail" : "pass"); + if ( parent.runAJAXTest ) + parent.runAJAXTest(); + + }); +} + +function test(name, callback, nowait) { + if(_config.currentModule) + name = _config.currentModule + " module: " + name; + + if ( !validTest(name) ) + return; + + synchronize(function() { + _config.Test = []; + try { + callback(); + } catch(e) { + if( typeof console != "undefined" && console.error && console.warn ) { + console.error("Test " + name + " died, exception and test follows"); + console.error(e); + console.warn(callback.toString()); + } + _config.Test.push( [ false, "Died on test #" + (_config.Test.length+1) + ": " + e.message ] ); + } + }); + synchronize(function() { + reset(); + + // don't output pause tests + if(nowait) return; + + if(_config.expected && _config.expected != _config.Test.length) { + _config.Test.push( [ false, "Expected " + _config.expected + " assertions, but " + _config.Test.length + " were run" ] ); + } + _config.expected = null; + + var good = 0, bad = 0; + var ol = document.createElement("ol"); + ol.style.display = "none"; + var li = "", state = "pass"; + for ( var i = 0; i < _config.Test.length; i++ ) { + var li = document.createElement("li"); + li.className = _config.Test[i][0] ? "pass" : "fail"; + li.innerHTML = _config.Test[i][1]; + ol.appendChild( li ); + + _config.stats.all++; + if ( !_config.Test[i][0] ) { + state = "fail"; + bad++; + _config.stats.bad++; + } else good++; + + if ( parent.SimpleTest ){ + parent.SimpleTest.ok( _config.Test[i][0], `${name}: ${_config.Test[i][1]}` );} + } + + var li = document.createElement("li"); + li.className = state; + + var b = document.createElement("strong"); + b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + _config.Test.length + ")</b>"; + b.onclick = function(){ + var n = this.nextSibling; + if ( jQuery.css( n, "display" ) == "none" ) + n.style.display = "block"; + else + n.style.display = "none"; + }; + $(b).dblclick(function(event) { + var target = jQuery(event.target).filter("strong").clone(); + if ( target.length ) { + target.children().remove(); + location.href = location.href.match(/^(.+?)(\?.*)?$/)[1] + "?" + encodeURIComponent($.trim(target.text())); + } + }); + li.appendChild( b ); + li.appendChild( ol ); + + document.getElementById("tests").appendChild( li ); + }); +} + +// call on start of module test to prepend name to all tests +function module(moduleName) { + _config.currentModule = moduleName; +} + +/** + * Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through. + */ +function expect(asserts) { + _config.expected = asserts; +} + +/** + * Resets the test setup. Useful for tests that modify the DOM. + */ +function reset() { + $("#main").html( _config.fixture ); + $.event.global = {}; + $.ajaxSettings = $.extend({}, _config.ajaxSettings); +} + +/** + * Asserts true. + * @example ok( $("a").size() > 5, "There must be at least 5 anchors" ); + */ +function ok(a, msg) { + _config.Test.push( [ !!a, msg ] ); +} + +/** + * Asserts that two arrays are the same + */ +function isSet(a, b, msg) { + var ret = true; + if ( a && b && a.length != undefined && a.length == b.length ) { + for ( var i = 0; i < a.length; i++ ) + if ( a[i] != b[i] ) + ret = false; + } else + ret = false; + if ( !ret ) + _config.Test.push( [ ret, msg + " expected: " + serialArray(b) + " result: " + serialArray(a) ] ); + else + _config.Test.push( [ ret, msg ] ); +} + +/** + * Asserts that two objects are equivalent + */ +function isObj(a, b, msg) { + var ret = true; + + if ( a && b ) { + for ( var i in a ) + if ( a[i] != b[i] ) + ret = false; + + for ( i in b ) + if ( a[i] != b[i] ) + ret = false; + } else + ret = false; + + _config.Test.push( [ ret, msg ] ); +} + +function serialArray( a ) { + var r = []; + + if ( a && a.length ) + for ( var i = 0; i < a.length; i++ ) { + var str = a[i].nodeName; + if ( str ) { + str = str.toLowerCase(); + if ( a[i].id ) + str += "#" + a[i].id; + } else + str = a[i]; + r.push( str ); + } + + return "[ " + r.join(", ") + " ]"; +} + +/** + * Returns an array of elements with the given IDs, eg. + * @example q("main", "foo", "bar") + * @result [<div id="main">, <span id="foo">, <input id="bar">] + */ +function q() { + var r = []; + for ( var i = 0; i < arguments.length; i++ ) + r.push( document.getElementById( arguments[i] ) ); + return r; +} + +/** + * Asserts that a select matches the given IDs + * @example t("Check for something", "//[a]", ["foo", "baar"]); + * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar' + */ +function t(a,b,c) { + var f = jQuery(b); + var s = ""; + for ( var i = 0; i < f.length; i++ ) + s += (s && ",") + '"' + f[i].id + '"'; + isSet(f, q.apply(q,c), a + " (" + b + ")"); +} + +/** + * Add random number to url to stop IE from caching + * + * @example url("data/test.html") + * @result "data/test.html?10538358428943" + * + * @example url("data/test.php?foo=bar") + * @result "data/test.php?foo=bar&10538358345554" + */ +function url(value) { + return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000); +} + +/** + * Checks that the first two arguments are equal, with an optional message. + * Prints out both expected and actual values on failure. + * + * Prefered to ok( expected == actual, message ) + * + * @example equals( "Expected 2 characters.", v.formatMessage("Expected {0} characters.", 2) ); + * + * @param Object actual + * @param Object expected + * @param String message (optional) + */ +function equals(actual, expected, message) { + var result = expected == actual; + message = message || (result ? "okay" : "failed"); + _config.Test.push( [ result, result ? message + ": " + expected : message + " expected: " + expected + " actual: " + actual ] ); +} + +/** + * Trigger an event on an element. + * + * @example triggerEvent( document.body, "click" ); + * + * @param DOMElement elem + * @param String type + */ +function triggerEvent( elem, type, event ) { + if ( jQuery.browser.mozilla || jQuery.browser.opera ) { + event = document.createEvent("MouseEvents"); + event.initMouseEvent(type, true, true, elem.ownerDocument.defaultView, + 0, 0, 0, 0, 0, false, false, false, false, 0, null); + elem.dispatchEvent( event ); + } else if ( jQuery.browser.msie ) { + elem.fireEvent("on"+type); + } +}
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/jquery/test/data/testsuite.css b/dom/tests/mochitest/ajax/jquery/test/data/testsuite.css new file mode 100644 index 0000000000..4801becd65 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/data/testsuite.css @@ -0,0 +1,117 @@ +body, div, h1 { font-family: 'trebuchet ms', verdana, arial; margin: 0; padding: 0 } +body {font-size: 10pt; } +h1 { padding: 15px; font-size: large; background-color: #06b; color: white; } +h2 { padding: 10px; background-color: #eee; color: black; margin: 0; font-size: small; font-weight: normal } + +.pass { color: green; } +.fail { color: red; } +p.result { margin-left: 1em; } + +#banner { height: 2em; border-bottom: 1px solid white; } +h2.pass { background-color: green; } +h2.fail { background-color: red; } + +ol#tests > li > strong { cursor:pointer; } + +div#fx-tests h4 { + background: red; +} + +div#fx-tests h4.pass { + background: green; +} + +div#fx-tests div.box { + background: red url(data/cow.jpg) no-repeat; + overflow: hidden; + border: 2px solid #000; +} + +div#fx-tests div.overflow { + overflow: visible; +} + +div.inline { + display: inline; +} + +div.autoheight { + height: auto; +} + +div.autowidth { + width: auto; +} + +div.autoopacity { + opacity: auto; +} + +div.largewidth { + width: 100px; +} + +div.largeheight { + height: 100px; +} + +div.largeopacity { + filter: progid:DXImageTransform.Microsoft.Alpha(opacity=100); +} + +div.medwidth { + width: 50px; +} + +div.medheight { + height: 50px; +} + +div.medopacity { + opacity: 0.5; + filter: progid:DXImageTransform.Microsoft.Alpha(opacity=50); +} + +div.nowidth { + width: 0px; +} + +div.noheight { + height: 0px; +} + +div.noopacity { + opacity: 0; + filter: progid:DXImageTransform.Microsoft.Alpha(opacity=0); +} + +div.hidden { + display: none; +} + +div#fx-tests div.widewidth { + background-repeat: repeat-x; +} + +div#fx-tests div.wideheight { + background-repeat: repeat-y; +} + +div#fx-tests div.widewidth.wideheight { + background-repeat: repeat; +} + +div#fx-tests div.noback { + background-image: none; +} + +div.chain, div.chain div { width: 100px; height: 20px; position: relative; float: left; } +div.chain div { position: absolute; top: 0px; left: 0px; } + +div.chain.test { background: red; } +div.chain.test div { background: green; } + +div.chain.out { background: green; } +div.chain.out div { background: red; display: none; } + +div#show-tests * { display: none; }
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/jquery/test/data/with_fries.xml b/dom/tests/mochitest/ajax/jquery/test/data/with_fries.xml new file mode 100644 index 0000000000..88e0e49d57 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/data/with_fries.xml @@ -0,0 +1,25 @@ +<?xml version='1.0' encoding='UTF-8'?> +<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" + xmlns:xsd="http://www.w3.org/2001/XMLSchema" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + <soap:Body> + <jsconf xmlns="http://www.example.com/ns1"> + <response xmlns:ab="http://www.example.com/ns2"> + <meta> + <component id="seite1"> + <properties xmlns:cd="http://www.example.com/ns3"> + <property name="prop1"> + <thing /> + <value>1</value> + </property> + <property name="prop2"> + <thing att="something" /> + </property> + <foo_bar>foo</foo_bar> + </properties> + </component> + </meta> + </response> + </jsconf> + </soap:Body> +</soap:Envelope> diff --git a/dom/tests/mochitest/ajax/jquery/test/fix.html b/dom/tests/mochitest/ajax/jquery/test/fix.html new file mode 100644 index 0000000000..f4edab7919 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/fix.html @@ -0,0 +1,48 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <meta name="generator"
+ content="HTML Tidy, see www.w3.org" />
+
+ <title>Tester</title>
+<style type="text/css">
+ #container { background:yellow; width:400px; height:400px; }
+
+</style>
+<script type="text/javascript" src="../dist/jquery.js">
+</script>
+<script type="text/javascript">
+ function doIt() {
+ $("#adiv").text("click!");
+ $("#adiv").trigger("acustom.atype");
+ }
+
+ function showMouse(e) {
+ $("#adiv").text("( " + e.pageX + ", " + e.pageY + " )");
+ }
+
+ $(function () {
+ $("#doit").bind('click.mine', doIt);
+ $("#container").mousemove(showMouse);
+ $("#adiv").bind("acustom.atype", function () {
+ //console.log("custom");
+ });
+ });
+
+</script>
+ </head>
+
+ <body>
+ <button id="doit">Do It</button>
+
+ <div id="container">
+ Hi
+ </div>
+
+ <div id="adiv">
+ </div>
+ </body>
+</html>
+
diff --git a/dom/tests/mochitest/ajax/jquery/test/index.html b/dom/tests/mochitest/ajax/jquery/test/index.html new file mode 100644 index 0000000000..8608f430b5 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/index.html @@ -0,0 +1,187 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr" id="html"> +<head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + <title>jQuery Test Suite</title> + <link rel="Stylesheet" media="screen" href="data/testsuite.css" /> + <script>var jQuery = "jQuery", $ = "$"; // For testing .noConflict()</script> + <script type="text/javascript" src="../dist/jquery.js"></script> + <script type="text/javascript" src="data/testrunner.js"></script> + <script type="text/javascript" src="unit/core.js"></script> + <script type="text/javascript" src="unit/dimensions.js"></script> + <script type="text/javascript" src="unit/selector.js"></script> + <script type="text/javascript" src="unit/event.js"></script> + <script type="text/javascript" src="unit/ajax.js"></script> + <script type="text/javascript" src="unit/fx.js"></script> +</head> + +<body id="body"> + <h1 id="header">jQuery Test Suite</h1> + <h2 id="banner"></h2> + <h2 id="userAgent"></h2> + + <!-- Test HTML --> + <div id="nothiddendiv" style="height:1px;background:white;"> + <div id="nothiddendivchild"></div> + </div> + <!-- this iframe is outside the #main so it won't reload constantly wasting time, but it means the tests must be "safe" and clean up after themselves --> + <iframe id="loadediframe" name="loadediframe" style="display:none;" src="data/iframe.html"></iframe> + <dl id="dl" style="display:none;"> + <div id="main" style="display: none;"> + <p id="firstp">See <a id="simon1" href="http://simon.incutio.com/archive/2003/03/25/#getElementsBySelector" rel="bookmark">this blog entry</a> for more information.</p> + <p id="ap"> + Here are some links in a normal paragraph: <a id="google" href="http://www.google.com/" title="Google!">Google</a>, + <a id="groups" href="http://groups.google.com/">Google Groups</a>. + This link has <code><a href="http://smin" id="anchor1">class="blog"</a></code>: + <a href="http://diveintomark.org/" class="blog" hreflang="en" id="mark">diveintomark</a> + + </p> + <div id="foo"> + <p id="sndp">Everything inside the red border is inside a div with <code>id="foo"</code>.</p> + <p lang="en" id="en">This is a normal link: <a id="yahoo" href="http://www.yahoo.com/" class="blogTest">Yahoo</a></p> + <p id="sap">This link has <code><a href="#2" id="anchor2">class="blog"</a></code>: <a href="http://simon.incutio.com/" class="blog link" id="simon">Simon Willison's Weblog</a></p> + + </div> + <p id="first">Try them out:</p> + <ul id="firstUL"></ul> + <ol id="empty"></ol> + <form id="form" action="formaction"> + <input type="text" name="action" value="Test" id="text1" minlength="20" maxlength="30"/> + <input type="text" name="text2" value="Test" id="text2" disabled="disabled"/> + <input type="radio" name="radio1" id="radio1" value="on"/> + + <input type="radio" name="radio2" id="radio2" checked="checked"/> + <input type="checkbox" name="check" id="check1" checked="checked"/> + <input type="checkbox" id="check2" value="on"/> + + <input type="hidden" name="hidden" id="hidden1"/> + <input type="text" style="display:none;" name="foo[bar]" id="hidden2"/> + + <input type="text" id="name" name="name" value="name" /> + + <button id="button" name="button">Button</button> + + <textarea id="area1" minlength="20" maxlength="30">foobar</textarea> + + <select name="select1" id="select1"> + <option id="option1a" class="emptyopt" value="">Nothing</option> + <option id="option1b" value="1">1</option> + <option id="option1c" value="2">2</option> + <option id="option1d" value="3">3</option> + </select> + <select name="select2" id="select2"> + <option id="option2a" class="emptyopt" value="">Nothing</option> + <option id="option2b" value="1">1</option> + <option id="option2c" value="2">2</option> + <option id="option2d" selected="selected" value="3">3</option> + </select> + <select name="select3" id="select3" multiple="multiple"> + <option id="option3a" class="emptyopt" value="">Nothing</option> + <option id="option3b" selected="selected" value="1">1</option> + <option id="option3c" selected="selected" value="2">2</option> + <option id="option3d" value="3">3</option> + </select> + + <object id="object1" codebase="stupid"> + <param name="p1" value="x1" /> + <param name="p2" value="x2" /> + </object> + + <span id="台北Táiběi"></span> + <span id="台北" lang="中文"></span> + <span id="utf8class1" class="台北Táiběi 台北"></span> + <span id="utf8class2" class="台北"></span> + <span id="foo:bar" class="foo:bar"></span> + <span id="test.foo[5]bar" class="test.foo[5]bar"></span> + + <foo_bar id="foobar">test element</foo_bar> + </form> + <b id="floatTest">Float test.</b> + <iframe id="iframe" name="iframe"></iframe> + <form id="lengthtest"> + <input type="text" id="length" name="test"/> + <input type="text" id="idTest" name="id"/> + </form> + <table id="table"></table> + + <div id="fx-queue"> + <div id="fadein" class='chain test'>fadeIn<div>fadeIn</div></div> + <div id="fadeout" class='chain test out'>fadeOut<div>fadeOut</div></div> + + <div id="show" class='chain test'>show<div>show</div></div> + <div id="hide" class='chain test out'>hide<div>hide</div></div> + + <div id="togglein" class='chain test'>togglein<div>togglein</div></div> + <div id="toggleout" class='chain test out'>toggleout<div>toggleout</div></div> + + + <div id="slideup" class='chain test'>slideUp<div>slideUp</div></div> + <div id="slidedown" class='chain test out'>slideDown<div>slideDown</div></div> + + <div id="slidetogglein" class='chain test'>slideToggleIn<div>slideToggleIn</div></div> + <div id="slidetoggleout" class='chain test out'>slideToggleOut<div>slideToggleOut</div></div> + </div> + + <div id="fx-tests"></div> + + <form id="testForm" action="#" method="get"> + <textarea name="T3" rows="2" cols="15">? +Z</textarea> + <input type="hidden" name="H1" value="x" /> + <input type="hidden" name="H2" /> + <input name="PWD" type="password" value="" /> + <input name="T1" type="text" /> + <input name="T2" type="text" value="YES" readonly="readonly" /> + <input type="checkbox" name="C1" value="1" /> + <input type="checkbox" name="C2" /> + <input type="radio" name="R1" value="1" /> + <input type="radio" name="R1" value="2" /> + <input type="text" name="My Name" value="me" /> + <input type="reset" name="reset" value="NO" /> + <select name="S1"> + <option value="abc">ABC</option> + <option value="abc">ABC</option> + <option value="abc">ABC</option> + </select> + <select name="S2" multiple="multiple" size="3"> + <option value="abc">ABC</option> + <option value="abc">ABC</option> + <option value="abc">ABC</option> + </select> + <select name="S3"> + <option selected="selected">YES</option> + </select> + <select name="S4"> + <option value="" selected="selected">NO</option> + </select> + <input type="submit" name="sub1" value="NO" /> + <input type="submit" name="sub2" value="NO" /> + <input type="image" name="sub3" value="NO" /> + <button name="sub4" type="submit" value="NO">NO</button> + <input name="D1" type="text" value="NO" disabled="disabled" /> + <input type="checkbox" checked="checked" disabled="disabled" name="D2" value="NO" /> + <input type="radio" name="D3" value="NO" checked="checked" disabled="disabled" /> + <select name="D4" disabled="disabled"> + <option selected="selected" value="NO">NO</option> + </select> + </form> + <div id="moretests"> + <form> + <div id="checkedtest" style="display:none;"> + <input type="radio" name="checkedtestradios" checked="checked"/> + <input type="radio" name="checkedtestradios" value="on"/> + <input type="checkbox" name="checkedtestcheckboxes" checked="checked"/> + <input type="checkbox" name="checkedtestcheckboxes" /> + </div> + </form> + <div id="nonnodes"><span>hi</span> there <!-- mon ami --></div> + <div id="t2037"> + <div><div class="hidden">hidden</div></div> + </div> + </div> + </div> + </dl> + + <ol id="tests"></ol> +</body> +</html> diff --git a/dom/tests/mochitest/ajax/jquery/test/offset.html b/dom/tests/mochitest/ajax/jquery/test/offset.html new file mode 100644 index 0000000000..f802dea346 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/offset.html @@ -0,0 +1,27 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr" id="html"> +<head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + <title>jQuery Offset Test Suite</title> + <link rel="Stylesheet" media="screen" href="data/testsuite.css" /> + <script type="text/javascript" src="../dist/jquery.js"></script> + <script type="text/javascript" src="data/testrunner.js"></script> + <script type="text/javascript" src="unit/offset.js"></script> +</head> + +<body id="body"> + <h1 id="header">jQuery Offset Test Suite</h1> + <h2 id="banner"></h2> + <h2 id="userAgent"></h2> + + <!-- Test HTML --> + <div id="nothiddendiv" style="height:1px;background:white;"></div> + <dl id="dl" style="display:none;"> + <div id="main" style="display: none;"> + + </div> + </dl> + + <ol id="tests"></ol> +</body> +</html> diff --git a/dom/tests/mochitest/ajax/jquery/test/test.js b/dom/tests/mochitest/ajax/jquery/test/test.js new file mode 100644 index 0000000000..e76b795a2c --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/test.js @@ -0,0 +1,41 @@ +load( "build/js/writeFile.js", "build/js/parse.js" ); + +function addParams(name, params) { + if(params.length > 0) { + name += "("; + for ( var i = 0; i < params.length; i++) { + name += params[i].type + ", "; + } + return name.substring(0, name.length - 2) + ")"; + } else { + return name + "()"; + } +} +function addTestWrapper(name, test) { + return 'test("' + name + '", function() {\n' + test + '\n});'; +} + +var dir = arguments[1]; +var jq = parse( read(arguments[0]) ); + +var testFile = []; + +String.prototype.decode = function() { + return this.replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&"); +}; + +for ( var i = 0; i < jq.length; i++ ) { + if ( jq[i].tests.length > 0 ) { + var method = jq[i]; + var name = addParams(method.name, method.params); + for(var j = 0; j < method.tests.length; j++) { + if(j > 0) { + name += "x"; + } + testFile[testFile.length] = addTestWrapper(name, method.tests[j].decode()) + "\n"; + } + } +} + +var indexFile = readFile( "build/test/index.html" ); +writeFile( dir + "/index.html", indexFile.replace( /{TESTS}/g, testFile.join("\n") ) ); diff --git a/dom/tests/mochitest/ajax/jquery/test/unit/ajax.js b/dom/tests/mochitest/ajax/jquery/test/unit/ajax.js new file mode 100644 index 0000000000..b60d39e72f --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/unit/ajax.js @@ -0,0 +1,837 @@ +module("ajax"); + +// Safari 3 randomly crashes when running these tests, +// but only in the full suite - you can run just the Ajax +// tests and they'll pass +//if ( !jQuery.browser.safari ) { + +if ( !isLocal ) { + +test("$.ajax() - success callbacks", function() { + expect( 8 ); + + $.ajaxSetup({ timeout: 0 }); + + stop(); + + setTimeout(function(){ + $('#foo').ajaxStart(function(){ + ok( true, "ajaxStart" ); + }).ajaxStop(function(){ + ok( true, "ajaxStop" ); + start(); + }).ajaxSend(function(){ + ok( true, "ajaxSend" ); + }).ajaxComplete(function(){ + ok( true, "ajaxComplete" ); + }).ajaxError(function(){ + ok( false, "ajaxError" ); + }).ajaxSuccess(function(){ + ok( true, "ajaxSuccess" ); + }); + + $.ajax({ + url: url("data/name.html"), + beforeSend: function(){ ok(true, "beforeSend"); }, + success: function(){ ok(true, "success"); }, + error: function(){ ok(false, "error"); }, + complete: function(){ ok(true, "complete"); } + }); + }, 13); +}); + +/* mozilla: the ajaxSuccess part fails intermittently on MacOSX + +test("$.ajax() - error callbacks", function() { + expect( 7 ); + stop(); + + $('#foo').ajaxStart(function(){ + ok( true, "ajaxStart" ); + }).ajaxStop(function(){ + ok( true, "ajaxStop" ); + start(); + }).ajaxSend(function(){ + ok( true, "ajaxSend" ); + }).ajaxComplete(function(){ + ok( true, "ajaxComplete" ); + }).ajaxError(function(){ + ok( true, "ajaxError" ); + }).ajaxSuccess(function(){ + ok( false, "ajaxSuccess" ); + }) + ; + + $.ajaxSetup({ timeout: 500 }); + + $.ajax({ + url: url("data/name.php?wait=5"), + beforeSend: function(){ ok(true, "beforeSend"); }, + success: function(){ ok(false, "success"); }, + error: function(){ ok(true, "error"); }, + complete: function(){ ok(true, "complete"); } + }); +}); + +*/ + +test("$.ajax() - disabled globals", function() { + expect( 3 ); + stop(); + + $('#foo').ajaxStart(function(){ + ok( false, "ajaxStart" ); + }).ajaxStop(function(){ + ok( false, "ajaxStop" ); + }).ajaxSend(function(){ + ok( false, "ajaxSend" ); + }).ajaxComplete(function(){ + ok( false, "ajaxComplete" ); + }).ajaxError(function(){ + ok( false, "ajaxError" ); + }).ajaxSuccess(function(){ + ok( false, "ajaxSuccess" ); + }); + + $.ajax({ + global: false, + url: url("data/name.html"), + beforeSend: function(){ ok(true, "beforeSend"); }, + success: function(){ ok(true, "success"); }, + error: function(){ ok(false, "error"); }, + complete: function(){ + ok(true, "complete"); + setTimeout(function(){ start(); }, 13); + } + }); +}); + +test("$.ajax - xml: non-namespace elements inside namespaced elements", function() { + expect(3); + stop(); + $.ajax({ + url: url("data/with_fries.xml"), + dataType: "xml", + success: function(resp) { + equals( $("properties", resp).length, 1, 'properties in responseXML' ); + equals( $("jsconf", resp).length, 1, 'jsconf in responseXML' ); + equals( $("thing", resp).length, 2, 'things in responseXML' ); + start(); + } + }); +}); + +test("$.ajax - beforeSend", function() { + expect(1); + stop(); + + var check = false; + + $.ajaxSetup({ timeout: 0 }); + + $.ajax({ + url: url("data/name.html"), + beforeSend: function(xml) { + check = true; + }, + success: function(data) { + ok( check, "check beforeSend was executed" ); + start(); + } + }); +}); + +test("$.ajax - beforeSend, cancel request (#2688)", function() { + expect(2); + var request = $.ajax({ + url: url("data/name.html"), + beforeSend: function() { + ok( true, "beforeSend got called, canceling" ); + return false; + }, + success: function() { + ok( false, "request didn't get canceled" ); + }, + complete: function() { + ok( false, "request didn't get canceled" ); + }, + error: function() { + ok( false, "request didn't get canceled" ); + } + }); + ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" ); +}); + +var foobar; + +test("$.ajax - dataType html", function() { + expect(5); + stop(); + + foobar = null; + testFoo = undefined; + + var verifyEvaluation = function() { + equals( testFoo, "foo", 'Check if script was evaluated for datatype html' ); + equals( foobar, "bar", 'Check if script src was evaluated for datatype html' ); + start(); + }; + + $.ajax({ + dataType: "html", + url: url("data/test.html"), + success: function(data) { + $("#ap").html(data); + ok( data.match(/^html text/), 'Check content for datatype html' ); + setTimeout(verifyEvaluation, 600); + } + }); +}); + +test("serialize()", function() { + expect(6); + + equals( $('#form').serialize(), + "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=1&select3=2", + 'Check form serialization as query string'); + + equals( $('#form :input').serialize(), + "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=1&select3=2", + 'Check input serialization as query string'); + + equals( $('#testForm').serialize(), + 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=', + 'Check form serialization as query string'); + + equals( $('#testForm :input').serialize(), + 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=', + 'Check input serialization as query string'); + + equals( $('#form, #testForm').serialize(), + "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=1&select3=2&T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=", + 'Multiple form serialization as query string'); + + equals( $('#form, #testForm :input').serialize(), + "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=1&select3=2&T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=", + 'Mixed form/input serialization as query string'); +}); + +test("$.param()", function() { + expect(4); + var params = {foo:"bar", baz:42, quux:"All your base are belong to us"}; + equals( $.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" ); + + params = {someName: [1, 2, 3], regularThing: "blah" }; + equals( $.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" ); + + params = {"foo[]":["baz", 42, "All your base are belong to us"]}; + equals( $.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" ); + + params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"}; + equals( $.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us", "even more arrays" ); +}); + +test("synchronous request", function() { + expect(1); + ok( /^{ "data"/.test( $.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" ); +}); + +test("synchronous request with callbacks", function() { + expect(2); + var result; + $.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "success callback executed"); result = data; } }); + ok( /^{ "data"/.test( result ), "check returned text" ); +}); + +test("pass-through request object", function() { + expect(8); + stop(true); + + var target = "data/name.html"; + var successCount = 0; + var errorCount = 0; + var errorEx = ""; + var success = function() { + successCount++; + }; + $("#foo").ajaxError(function (e, xml, s, ex) { + errorCount++; + errorEx += ": " + xml.status; + }); + $("#foo").one('ajaxStop', function () { + equals(successCount, 5, "Check all ajax calls successful"); + equals(errorCount, 0, "Check no ajax errors (status" + errorEx + ")"); + $("#foo").unbind('ajaxError'); + start(); + }); + + ok( $.get(url(target), success), "get" ); + ok( $.post(url(target), success), "post" ); + ok( $.getScript(url("data/test.js"), success), "script" ); + ok( $.getJSON(url("data/json_obj.js"), success), "json" ); + ok( $.ajax({url: url(target), success: success}), "generic" ); +}); + +/* mozilla: php not currently supported in mochitest (08/08/2008) +test("ajax cache", function () { + expect(18); + stop(); + + var count = 0; + + $("#firstp").bind("ajaxSuccess", function (e, xml, s) { + var re = /_=(.*?)(&|$)/g; + var oldOne = null; + for (var i = 0; i < 6; i++) { + var ret = re.exec(s.url); + if (!ret) { + break; + } + oldOne = ret[1]; + } + equals(i, 1, "Test to make sure only one 'no-cache' parameter is there"); + ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced"); + if(++count == 6) + start(); + }); + + ok( $.ajax({url: "data/text.php", cache:false}), "test with no parameters" ); + ok( $.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" ); + ok( $.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" ); + ok( $.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" ); + ok( $.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" ); + ok( $.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" ); +}); +*/ + +test("global ajaxSettings", function() { + expect(2); + + var tmp = jQuery.extend({}, jQuery.ajaxSettings); + var orig = { url: "data/with_fries.xml" }; + var t; + + $.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} }); + + t = jQuery.extend({}, orig); + t.data = {}; + $.ajax(t); + ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" ); + + t = jQuery.extend({}, orig); + t.data = { zoo: 'a', ping: 'b' }; + $.ajax(t); + ok( t.url.indexOf('ping') > -1 && t.url.indexOf('zoo') > -1 && t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending { zoo: 'a', ping: 'b' }" ); + + jQuery.ajaxSettings = tmp; +}); + +test("load(String)", function() { + expect(1); + stop(true); // check if load can be called with only url + $('#first').load("data/name.html", start); +}); + +test("load('url selector')", function() { + expect(1); + stop(true); // check if load can be called with only url + $('#first').load("data/test3.html div.user", function(){ + equals( $(this).children("div").length, 2, "Verify that specific elements were injected" ); + start(); + }); +}); + +test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() { + expect(1); + stop(); + $.ajaxSetup({ dataType: "json" }); + $("#first").ajaxComplete(function (e, xml, s) { + equals( s.dataType, "html", "Verify the load() dataType was html" ); + $("#first").unbind("ajaxComplete"); + $.ajaxSetup({ dataType: "" }); + start(); + }); + $('#first').load("data/test3.html"); +}); + +test("load(String, Function) - simple: inject text into DOM", function() { + expect(2); + stop(); + $('#first').load(url("data/name.html"), function() { + ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' ); + start(); + }); +}); + +test("load(String, Function) - check scripts", function() { + expect(7); + stop(); + window.testFoo = undefined; + window.foobar = null; + var verifyEvaluation = function() { + equals( foobar, "bar", 'Check if script src was evaluated after load' ); + equals( $('#ap').html(), 'bar', 'Check if script evaluation has modified DOM'); + start(); + }; + $('#first').load(url('data/test.html'), function() { + ok( $('#first').html().match(/^html text/), 'Check content after loading html' ); + equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM'); + equals( testFoo, "foo", 'Check if script was evaluated after load' ); + setTimeout(verifyEvaluation, 600); + }); +}); + +test("load(String, Function) - check file with only a script tag", function() { + expect(3); + stop(); + testFoo = undefined; + $('#first').load(url('data/test2.html'), function() { + equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM'); + equals( testFoo, "foo", 'Check if script was evaluated after load' ); + start(); + }); +}); + +test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() { + expect(2); + stop(); + $.get(url('data/dashboard.xml'), function(xml) { + var content = []; + $('tab', xml).each(function() { + content.push($(this).text()); + }); + equals( content[0], 'blabla', 'Check first tab'); + equals( content[1], 'blublu', 'Check second tab'); + start(); + }); +}); + +test("$.getScript(String, Function) - with callback", function() { + expect(2); + stop(); + window.foobar = null; + $.getScript(url("data/test.js"), function() { + equals( foobar, "bar", 'Check if script was evaluated' ); + setTimeout(start, 100); + }); +}); + +test("$.getScript(String, Function) - no callback", function() { + expect(1); + stop(true); + $.getScript(url("data/test.js"), start); +}); + +/* mozilla: Tests using php scripts not currently supported (06/26/2008) + +test("$.ajax() - JSONP, Local", function() { + expect(7); + + var count = 0; + function plus(){ if ( ++count == 7 ) start(); } + + stop(); + + $.ajax({ + url: "data/jsonp.php", + dataType: "jsonp", + success: function(data){ + ok( data.data, "JSON results returned (GET, no callback)" ); + plus(); + }, + error: function(data){ + ok( false, "Ajax error JSON (GET, no callback)" ); + plus(); + } + }); + + $.ajax({ + url: "data/jsonp.php?callback=?", + dataType: "jsonp", + success: function(data){ + ok( data.data, "JSON results returned (GET, url callback)" ); + plus(); + }, + error: function(data){ + ok( false, "Ajax error JSON (GET, url callback)" ); + plus(); + } + }); + + $.ajax({ + url: "data/jsonp.php", + dataType: "jsonp", + data: "callback=?", + success: function(data){ + ok( data.data, "JSON results returned (GET, data callback)" ); + plus(); + }, + error: function(data){ + ok( false, "Ajax error JSON (GET, data callback)" ); + plus(); + } + }); + + $.ajax({ + url: "data/jsonp.php", + dataType: "jsonp", + jsonp: "callback", + success: function(data){ + ok( data.data, "JSON results returned (GET, data obj callback)" ); + plus(); + }, + error: function(data){ + ok( false, "Ajax error JSON (GET, data obj callback)" ); + plus(); + } + }); + + $.ajax({ + type: "POST", + url: "data/jsonp.php", + dataType: "jsonp", + success: function(data){ + ok( data.data, "JSON results returned (POST, no callback)" ); + plus(); + }, + error: function(data){ + ok( false, "Ajax error JSON (GET, data obj callback)" ); + plus(); + } + }); + + $.ajax({ + type: "POST", + url: "data/jsonp.php", + data: "callback=?", + dataType: "jsonp", + success: function(data){ + ok( data.data, "JSON results returned (POST, data callback)" ); + plus(); + }, + error: function(data){ + ok( false, "Ajax error JSON (POST, data callback)" ); + plus(); + } + }); + + $.ajax({ + type: "POST", + url: "data/jsonp.php", + jsonp: "callback", + dataType: "jsonp", + success: function(data){ + ok( data.data, "JSON results returned (POST, data obj callback)" ); + plus(); + }, + error: function(data){ + ok( false, "Ajax error JSON (POST, data obj callback)" ); + plus(); + } + }); +}); + +test("$.ajax() - JSONP, Remote", function() { + expect(4); + + var count = 0; + function plus(){ if ( ++count == 4 ) start(); } + + var base = window.location.href.replace(/\?.*$/, ""); + + stop(); + + $.ajax({ + url: base + "data/jsonp.php", + dataType: "jsonp", + success: function(data){ + ok( data.data, "JSON results returned (GET, no callback)" ); + plus(); + }, + error: function(data){ + ok( false, "Ajax error JSON (GET, no callback)" ); + plus(); + } + }); + + $.ajax({ + url: base + "data/jsonp.php?callback=?", + dataType: "jsonp", + success: function(data){ + ok( data.data, "JSON results returned (GET, url callback)" ); + plus(); + }, + error: function(data){ + ok( false, "Ajax error JSON (GET, url callback)" ); + plus(); + } + }); + + $.ajax({ + url: base + "data/jsonp.php", + dataType: "jsonp", + data: "callback=?", + success: function(data){ + ok( data.data, "JSON results returned (GET, data callback)" ); + plus(); + }, + error: function(data){ + ok( false, "Ajax error JSON (GET, data callback)" ); + plus(); + } + }); + + $.ajax({ + url: base + "data/jsonp.php", + dataType: "jsonp", + jsonp: "callback", + success: function(data){ + ok( data.data, "JSON results returned (GET, data obj callback)" ); + plus(); + }, + error: function(data){ + ok( false, "Ajax error JSON (GET, data obj callback)" ); + plus(); + } + }); +}); + +test("$.ajax() - script, Remote", function() { + expect(2); + + var base = window.location.href.replace(/\?.*$/, ""); + + stop(); + + window.foobar = null; + $.ajax({ + url: base + "data/test.js", + dataType: "script", + success: function(data){ + ok( foobar, "Script results returned (GET, no callback)" ); + start(); + } + }); +}); + +test("$.ajax() - script, Remote with POST", function() { + expect(3); + + var base = window.location.href.replace(/\?.*$/, ""); + + stop(); + + window.foobar = null; + $.ajax({ + url: base + "data/test.js", + type: "POST", + dataType: "script", + success: function(data, status){ + ok( foobar, "Script results returned (GET, no callback)" ); + equals( status, "success", "Script results returned (GET, no callback)" ); + start(); + } + }); +}); + +test("$.ajax() - script, Remote with scheme-less URL", function() { + expect(2); + + var base = window.location.href.replace(/\?.*$/, ""); + base = base.replace(/^.*?\/\//, "//"); + + stop(); + + window.foobar = null; + $.ajax({ + url: base + "data/test.js", + dataType: "script", + success: function(data){ + ok( foobar, "Script results returned (GET, no callback)" ); + start(); + } + }); +}); + +test("$.getJSON(String, Hash, Function) - JSON array", function() { + expect(4); + stop(); + $.getJSON(url("data/json.php"), {json: "array"}, function(json) { + equals( json[0].name, 'John', 'Check JSON: first, name' ); + equals( json[0].age, 21, 'Check JSON: first, age' ); + equals( json[1].name, 'Peter', 'Check JSON: second, name' ); + equals( json[1].age, 25, 'Check JSON: second, age' ); + start(); + }); +}); + +test("$.getJSON(String, Function) - JSON object", function() { + expect(2); + stop(); + $.getJSON(url("data/json.php"), function(json) { + equals( json.data.lang, 'en', 'Check JSON: lang' ); + equals( json.data.length, 25, 'Check JSON: length' ); + start(); + }); +}); + +test("$.getJSON(String, Function) - JSON object with absolute url to local content", function() { + expect(2); + + var base = window.location.href.replace(/\?.*$/, ""); + + stop(); + $.getJSON(url(base + "data/json.php"), function(json) { + equals( json.data.lang, 'en', 'Check JSON: lang' ); + equals( json.data.length, 25, 'Check JSON: length' ); + start(); + }); +}); + +test("$.post(String, Hash, Function) - simple with xml", function() { + expect(4); + stop(); + $.post(url("data/name.php"), {xml: "5-2"}, function(xml){ + $('math', xml).each(function() { + equals( $('calculation', this).text(), '5-2', 'Check for XML' ); + equals( $('result', this).text(), '3', 'Check for XML' ); + }); + }); + + $.post(url("data/name.php?xml=5-2"), {}, function(xml){ + $('math', xml).each(function() { + equals( $('calculation', this).text(), '5-2', 'Check for XML' ); + equals( $('result', this).text(), '3', 'Check for XML' ); + }); + start(); + }); +}); + +test("$.ajaxSetup({timeout: Number}) - with global timeout", function() { + stop(); + + var passed = 0; + + $.ajaxSetup({timeout: 1000}); + + var pass = function() { + passed++; + if ( passed == 2 ) { + ok( true, 'Check local and global callbacks after timeout' ); + $('#main').unbind("ajaxError"); + start(); + } + }; + + var fail = function(a,b,c) { + ok( false, 'Check for timeout failed ' + a + ' ' + b ); + start(); + }; + + $('#main').ajaxError(pass); + + $.ajax({ + type: "GET", + url: url("data/name.php?wait=5"), + error: pass, + success: fail + }); + + // reset timeout + $.ajaxSetup({timeout: 0}); +}); + +test("$.ajaxSetup({timeout: Number}) with localtimeout", function() { + stop(); + $.ajaxSetup({timeout: 50}); + + $.ajax({ + type: "GET", + timeout: 5000, + url: url("data/name.php?wait=1"), + error: function() { + ok( false, 'Check for local timeout failed' ); + start(); + }, + success: function() { + ok( true, 'Check for local timeout' ); + start(); + } + }); + + // reset timeout + $.ajaxSetup({timeout: 0}); +}); + +test("$.ajax - simple get", function() { + expect(1); + stop(); + $.ajax({ + type: "GET", + url: url("data/name.php?name=foo"), + success: function(msg){ + equals( msg, 'bar', 'Check for GET' ); + start(); + } + }); +}); + +test("$.ajax - simple post", function() { + expect(1); + stop(); + $.ajax({ + type: "POST", + url: url("data/name.php"), + data: "name=peter", + success: function(msg){ + equals( msg, 'pan', 'Check for POST' ); + start(); + } + }); +}); + +test("ajaxSetup()", function() { + expect(1); + stop(); + $.ajaxSetup({ + url: url("data/name.php?name=foo"), + success: function(msg){ + equals( msg, 'bar', 'Check for GET' ); + start(); + } + }); + $.ajax(); +}); + +test("custom timeout does not set error message when timeout occurs, see #970", function() { + stop(); + $.ajax({ + url: "data/name.php?wait=10", + timeout: 500, + error: function(request, status) { + ok( status != null, "status shouldn't be null in error handler" ); + equals( "timeout", status ); + start(); + } + }); +}); + +test("data option: evaluate function values (#2806)", function() { + stop(); + $.ajax({ + url: "data/echoQuery.php", + data: { + key: function() { + return "value"; + } + }, + success: function(result) { + equals( result, "key=value" ); + start(); + } + }) +}); +*/ +} + +//} diff --git a/dom/tests/mochitest/ajax/jquery/test/unit/core.js b/dom/tests/mochitest/ajax/jquery/test/unit/core.js new file mode 100644 index 0000000000..74a03192c0 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/unit/core.js @@ -0,0 +1,1700 @@ +module("core");
+
+test("Basic requirements", function() {
+ expect(7);
+ ok( Array.prototype.push, "Array.push()" );
+ ok( Function.prototype.apply, "Function.apply()" );
+ ok( document.getElementById, "getElementById" );
+ ok( document.getElementsByTagName, "getElementsByTagName" );
+ ok( RegExp, "RegExp" );
+ ok( jQuery, "jQuery" );
+ ok( $, "$()" );
+});
+
+test("$()", function() {
+ expect(8);
+
+ var main = $("#main");
+ isSet( $("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" );
+
+/*
+ // disabled since this test was doing nothing. i tried to fix it but i'm not sure
+ // what the expected behavior should even be. FF returns "\n" for the text node
+ // make sure this is handled
+ var crlfContainer = $('<p>\r\n</p>');
+ var x = crlfContainer.contents().get(0).nodeValue;
+ equals( x, what???, "Check for \\r and \\n in jQuery()" );
+*/
+
+ /* // Disabled until we add this functionality in
+ var pass = true;
+ try {
+ $("<div>Testing</div>").appendTo(document.getElementById("iframe").contentDocument.body);
+ } catch(e){
+ pass = false;
+ }
+ ok( pass, "$('<tag>') needs optional document parameter to ease cross-frame DOM wrangling, see #968" );*/
+
+ var code = $("<code/>");
+ equals( code.length, 1, "Correct number of elements generated for code" );
+ var img = $("<img/>");
+ equals( img.length, 1, "Correct number of elements generated for img" );
+ var div = $("<div/><hr/><code/><b/>");
+ equals( div.length, 4, "Correct number of elements generated for div hr code b" );
+
+ // can actually yield more than one, when iframes are included, the window is an array as well
+ equals( $(window).length, 1, "Correct number of elements generated for window" );
+
+ equals( $(document).length, 1, "Correct number of elements generated for document" );
+
+ equals( $([1,2,3]).get(1), 2, "Test passing an array to the factory" );
+
+ equals( $(document.body).get(0), $('body').get(0), "Test passing an html node to the factory" );
+});
+
+test("browser", function() {
+ expect(13);
+ var browsers = {
+ //Internet Explorer
+ "Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)": "6.0",
+ "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727)": "7.0",
+ /** Failing #1876
+ * "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.30)": "7.0",
+ */
+ //Browsers with Gecko engine
+ //Mozilla
+ "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915" : "1.7.12",
+ //Firefox
+ "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3": "1.8.1.3",
+ //Netscape
+ "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20070321 Netscape/8.1.3" : "1.7.5",
+ //Flock
+ "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.11) Gecko/20070321 Firefox/1.5.0.11 Flock/0.7.12" : "1.8.0.11",
+ //Opera browser
+ "Opera/9.20 (X11; Linux x86_64; U; en)": "9.20",
+ "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.20" : "9.20",
+ "Mozilla/5.0 (Windows NT 5.1; U; pl; rv:1.8.0) Gecko/20060728 Firefox/1.5.0 Opera 9.20": "9.20",
+ //WebKit engine
+ "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; sv-se) AppleWebKit/418.9 (KHTML, like Gecko) Safari/419.3": "418.9",
+ "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/418.8 (KHTML, like Gecko) Safari/419.3" : "418.8",
+ "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; sv-se) AppleWebKit/312.8 (KHTML, like Gecko) Safari/312.5": "312.8",
+ //Other user agent string
+ "Other browser's user agent 1.0":null
+ };
+ for (var i in browsers) {
+ var v = i.toLowerCase().match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ); // RegEx from Core jQuery.browser.version check
+ version = v ? v[1] : null;
+ equals( version, browsers[i], "Checking UA string" );
+ }
+});
+
+test("noConflict", function() {
+ expect(6);
+
+ var old = jQuery;
+ var newjQuery = jQuery.noConflict();
+
+ equals( newjQuery, old, "noConflict returned the jQuery object" );
+ equals( jQuery, old, "Make sure jQuery wasn't touched." );
+ equals( $, "$", "Make sure $ was reverted." );
+
+ jQuery = $ = old;
+
+ newjQuery = jQuery.noConflict(true);
+
+ equals( newjQuery, old, "noConflict returned the jQuery object" );
+ equals( jQuery, "jQuery", "Make sure jQuery was reverted." );
+ equals( $, "$", "Make sure $ was reverted." );
+
+ jQuery = $ = old;
+});
+
+test("isFunction", function() {
+ expect(21);
+
+ // Make sure that false values return false
+ ok( !jQuery.isFunction(), "No Value" );
+ ok( !jQuery.isFunction( null ), "null Value" );
+ ok( !jQuery.isFunction( undefined ), "undefined Value" );
+ ok( !jQuery.isFunction( "" ), "Empty String Value" );
+ ok( !jQuery.isFunction( 0 ), "0 Value" );
+
+ // Check built-ins
+ // Safari uses "(Internal Function)"
+ ok( jQuery.isFunction(String), "String Function("+String+")" );
+ ok( jQuery.isFunction(Array), "Array Function("+Array+")" );
+ ok( jQuery.isFunction(Object), "Object Function("+Object+")" );
+ ok( jQuery.isFunction(Function), "Function Function("+Function+")" );
+
+ // When stringified, this could be misinterpreted
+ var mystr = "function";
+ ok( !jQuery.isFunction(mystr), "Function String" );
+
+ // When stringified, this could be misinterpreted
+ var myarr = [ "function" ];
+ ok( !jQuery.isFunction(myarr), "Function Array" );
+
+ // When stringified, this could be misinterpreted
+ var myfunction = { "function": "test" };
+ ok( !jQuery.isFunction(myfunction), "Function Object" );
+
+ // Make sure normal functions still work
+ var fn = function(){};
+ ok( jQuery.isFunction(fn), "Normal Function" );
+
+ var obj = document.createElement("object");
+
+ // Firefox says this is a function
+ ok( !jQuery.isFunction(obj), "Object Element" );
+
+ // IE says this is an object
+ ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
+
+ var nodes = document.body.childNodes;
+
+ // Safari says this is a function
+ ok( !jQuery.isFunction(nodes), "childNodes Property" );
+
+ var first = document.body.firstChild;
+
+ // Normal elements are reported ok everywhere
+ ok( !jQuery.isFunction(first), "A normal DOM Element" );
+
+ var input = document.createElement("input");
+ input.type = "text";
+ document.body.appendChild( input );
+
+ // IE says this is an object
+ ok( jQuery.isFunction(input.focus), "A default function property" );
+
+ document.body.removeChild( input );
+
+ var a = document.createElement("a");
+ a.href = "some-function";
+ document.body.appendChild( a );
+
+ // This serializes with the word 'function' in it
+ ok( !jQuery.isFunction(a), "Anchor Element" );
+
+ document.body.removeChild( a );
+
+ // Recursive function calls have lengths and array-like properties
+ function callme(callback){
+ function fn(response){
+ callback(response);
+ }
+
+ ok( jQuery.isFunction(fn), "Recursive Function Call" );
+
+ fn({ some: "data" });
+ };
+
+ callme(function(){
+ callme(function(){});
+ });
+});
+
+var foo = false;
+
+test("$('html')", function() {
+ expect(6);
+
+ reset();
+ foo = false;
+ var s = $("<script>var foo='test';</script>")[0];
+ ok( s, "Creating a script" );
+ ok( !foo, "Make sure the script wasn't executed prematurely" );
+ $("body").append(s);
+ ok( foo, "Executing a scripts contents in the right context" );
+
+ reset();
+ ok( $("<link rel='stylesheet'/>")[0], "Creating a link" );
+
+ reset();
+
+ var j = $("<span>hi</span> there <!-- mon ami -->");
+ ok( j.length >= 2, "Check node,textnode,comment creation (some browsers delete comments)" );
+
+ ok( !$("<option>test</option>")[0].selected, "Make sure that options are auto-selected #2050" );
+});
+
+test("$('html', context)", function() {
+ expect(1);
+
+ var $div = $("<div/>");
+ var $span = $("<span/>", $div);
+ equals($span.length, 1, "Verify a span created with a div context works, #1763");
+});
+
+if ( !isLocal ) {
+test("$(selector, xml).text(str) - Loaded via XML document", function() {
+ expect(2);
+ stop();
+ $.get('data/dashboard.xml', function(xml) {
+ // tests for #1419 where IE was a problem
+ equals( $("tab:first", xml).text(), "blabla", "Verify initial text correct" );
+ $("tab:first", xml).text("newtext");
+ equals( $("tab:first", xml).text(), "newtext", "Verify new text correct" );
+ start();
+ });
+});
+}
+
+test("length", function() {
+ expect(1);
+ equals( $("p").length, 6, "Get Number of Elements Found" );
+});
+
+test("size()", function() {
+ expect(1);
+ equals( $("p").size(), 6, "Get Number of Elements Found" );
+});
+
+test("get()", function() {
+ expect(1);
+ isSet( $("p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" );
+});
+
+test("get(Number)", function() {
+ expect(1);
+ equals( $("p").get(0), document.getElementById("firstp"), "Get A Single Element" );
+});
+
+test("add(String|Element|Array|undefined)", function() {
+ expect(12);
+ isSet( $("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" );
+ isSet( $("#sndp").add( $("#en")[0] ).add( $("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" );
+ ok( $([]).add($("#form")[0].elements).length >= 13, "Check elements from array" );
+
+ // For the time being, we're discontinuing support for $(form.elements) since it's ambiguous in IE
+ // use $([]).add(form.elements) instead.
+ //equals( $([]).add($("#form")[0].elements).length, $($("#form")[0].elements).length, "Array in constructor must equals array in add()" );
+
+ var x = $([]).add($("<p id='x1'>xxx</p>")).add($("<p id='x2'>xxx</p>"));
+ equals( x[0].id, "x1", "Check on-the-fly element1" );
+ equals( x[1].id, "x2", "Check on-the-fly element2" );
+
+ var x = $([]).add("<p id='x1'>xxx</p>").add("<p id='x2'>xxx</p>");
+ equals( x[0].id, "x1", "Check on-the-fly element1" );
+ equals( x[1].id, "x2", "Check on-the-fly element2" );
+
+ var notDefined;
+ equals( $([]).add(notDefined).length, 0, "Check that undefined adds nothing" );
+
+ // Added after #2811
+ equals( $([]).add([window,document,document.body,document]).length, 3, "Pass an array" );
+ equals( $(document).add(document).length, 1, "Check duplicated elements" );
+ equals( $(window).add(window).length, 1, "Check duplicated elements using the window" );
+ ok( $([]).add( document.getElementById('form') ).length >= 13, "Add a form (adds the elements)" );
+});
+
+test("each(Function)", function() {
+ expect(1);
+ var div = $("div");
+ div.each(function(){this.foo = 'zoo';});
+ var pass = true;
+ for ( var i = 0; i < div.size(); i++ ) {
+ if ( div.get(i).foo != "zoo" ) pass = false;
+ }
+ ok( pass, "Execute a function, Relative" );
+});
+
+test("index(Object)", function() {
+ expect(10);
+
+ var elements = $([window, document]),
+ inputElements = $('#radio1,#radio2,#check1,#check2');
+
+ equals( elements.index(window), 0, "Check for index of elements" );
+ equals( elements.index(document), 1, "Check for index of elements" );
+ equals( inputElements.index(document.getElementById('radio1')), 0, "Check for index of elements" );
+ equals( inputElements.index(document.getElementById('radio2')), 1, "Check for index of elements" );
+ equals( inputElements.index(document.getElementById('check1')), 2, "Check for index of elements" );
+ equals( inputElements.index(document.getElementById('check2')), 3, "Check for index of elements" );
+ equals( inputElements.index(window), -1, "Check for not found index" );
+ equals( inputElements.index(document), -1, "Check for not found index" );
+
+ // enabled since [5500]
+ equals( elements.index( elements ), 0, "Pass in a jQuery object" );
+ equals( elements.index( elements.eq(1) ), 1, "Pass in a jQuery object" );
+});
+
+test("attr(String)", function() {
+ expect(29);
+ equals( $('#text1').attr('value'), "Test", 'Check for value attribute' );
+ equals( $('#text1').attr('value', "Test2").attr('defaultValue'), "Test", 'Check for defaultValue attribute' );
+ equals( $('#text1').attr('type'), "text", 'Check for type attribute' );
+ equals( $('#radio1').attr('type'), "radio", 'Check for type attribute' );
+ equals( $('#check1').attr('type'), "checkbox", 'Check for type attribute' );
+ equals( $('#simon1').attr('rel'), "bookmark", 'Check for rel attribute' );
+ equals( $('#google').attr('title'), "Google!", 'Check for title attribute' );
+ equals( $('#mark').attr('hreflang'), "en", 'Check for hreflang attribute' );
+ equals( $('#en').attr('lang'), "en", 'Check for lang attribute' );
+ equals( $('#simon').attr('class'), "blog link", 'Check for class attribute' );
+ equals( $('#name').attr('name'), "name", 'Check for name attribute' );
+ equals( $('#text1').attr('name'), "action", 'Check for name attribute' );
+ ok( $('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );
+ equals( $('#text1').attr('minlength'), '20', 'Check for minlength attribute' );
+ equals( $('#text1').attr('minLength'), '20', 'Check for minLength attribute' );
+ equals( $('#area1').attr('minLength'), '20', 'Check for minLength attribute' );
+ equals( $('#text1').attr('maxlength'), '30', 'Check for maxlength attribute' );
+ equals( $('#text1').attr('maxLength'), '30', 'Check for maxLength attribute' );
+ equals( $('#area1').attr('maxLength'), '30', 'Check for maxLength attribute' );
+ equals( $('#select2').attr('selectedIndex'), 3, 'Check for selectedIndex attribute' );
+ equals( $('#foo').attr('nodeName'), 'DIV', 'Check for nodeName attribute' );
+ equals( $('#foo').attr('tagName'), 'DIV', 'Check for tagName attribute' );
+
+ $('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path
+ equals( $('#tAnchor5').attr('href'), "#5", 'Check for non-absolute href (an anchor)' );
+
+
+ // Related to [5574] and [5683]
+ var body = document.body, $body = $(body);
+
+ ok( $body.attr('foo') === undefined, 'Make sure that a non existent attribute returns undefined' );
+ ok( $body.attr('nextSibling') === null, 'Make sure a null expando returns null' );
+
+ body.setAttribute('foo', 'baz');
+ equals( $body.attr('foo'), 'baz', 'Make sure the dom attribute is retrieved when no expando is found' );
+
+ body.foo = 'bar';
+ equals( $body.attr('foo'), 'bar', 'Make sure the expando is preferred over the dom attribute' );
+
+ $body.attr('foo','cool');
+ equals( $body.attr('foo'), 'cool', 'Make sure that setting works well when both expando and dom attribute are available' );
+
+ body.foo = undefined;
+ ok( $body.attr('foo') === undefined, 'Make sure the expando is preferred over the dom attribute, even if undefined' );
+
+ body.removeAttribute('foo'); // Cleanup
+});
+
+if ( !isLocal ) {
+ test("attr(String) in XML Files", function() {
+ expect(2);
+ stop();
+ $.get("data/dashboard.xml", function(xml) {
+ equals( $("locations", xml).attr("class"), "foo", "Check class attribute in XML document" );
+ equals( $("location", xml).attr("for"), "bar", "Check for attribute in XML document" );
+ start();
+ });
+ });
+}
+
+test("attr(String, Function)", function() {
+ expect(2);
+ equals( $('#text1').attr('value', function() { return this.id })[0].value, "text1", "Set value from id" );
+ equals( $('#text1').attr('title', function(i) { return i }).attr('title'), "0", "Set value with an index");
+});
+
+test("attr(Hash)", function() {
+ expect(1);
+ var pass = true;
+ $("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
+ if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
+ });
+ ok( pass, "Set Multiple Attributes" );
+});
+
+test("attr(String, Object)", function() {
+ expect(19);
+ var div = $("div").attr("foo", "bar");
+ fail = false;
+ for ( var i = 0; i < div.size(); i++ ) {
+ if ( div.get(i).getAttribute('foo') != "bar" ){
+ fail = i;
+ break;
+ }
+ }
+ equals( fail, false, "Set Attribute, the #"+fail+" element didn't get the attribute 'foo'" );
+
+ ok( $("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
+
+ $("#name").attr('name', 'something');
+ equals( $("#name").attr('name'), 'something', 'Set name attribute' );
+ $("#check2").attr('checked', true);
+ equals( document.getElementById('check2').checked, true, 'Set checked attribute' );
+ $("#check2").attr('checked', false);
+ equals( document.getElementById('check2').checked, false, 'Set checked attribute' );
+ $("#text1").attr('readonly', true);
+ equals( document.getElementById('text1').readOnly, true, 'Set readonly attribute' );
+ $("#text1").attr('readonly', false);
+ equals( document.getElementById('text1').readOnly, false, 'Set readonly attribute' );
+ $("#name").attr('maxlength', '5');
+ equals( document.getElementById('name').maxLength, '5', 'Set maxlength attribute' );
+ $("#name").attr('maxLength', '10');
+ equals( document.getElementById('name').maxLength, '10', 'Set maxlength attribute' );
+ $("#name").attr('minlength', '5');
+ equals( document.getElementById('name').minLength, '5', 'Set minlength attribute' );
+ $("#name").attr('minLength', '10');
+ equals( document.getElementById('name').minLength, '10', 'Set minlength attribute' );
+
+ // for #1070
+ $("#name").attr('someAttr', '0');
+ equals( $("#name").attr('someAttr'), '0', 'Set attribute to a string of "0"' );
+ $("#name").attr('someAttr', 0);
+ equals( $("#name").attr('someAttr'), 0, 'Set attribute to the number 0' );
+ $("#name").attr('someAttr', 1);
+ equals( $("#name").attr('someAttr'), 1, 'Set attribute to the number 1' );
+
+ // using contents will get comments regular, text, and comment nodes
+ var j = $("#nonnodes").contents();
+
+ j.attr("name", "attrvalue");
+ equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
+ j.removeAttr("name");
+
+ reset();
+
+ var type = $("#check2").attr('type');
+ var thrown = false;
+ try {
+ $("#check2").attr('type','hidden');
+ } catch(e) {
+ thrown = true;
+ }
+ ok( thrown, "Exception thrown when trying to change type property" );
+ equals( type, $("#check2").attr('type'), "Verify that you can't change the type of an input element" );
+
+ var check = document.createElement("input");
+ var thrown = true;
+ try {
+ $(check).attr('type','checkbox');
+ } catch(e) {
+ thrown = false;
+ }
+ ok( thrown, "Exception thrown when trying to change type property" );
+ equals( "checkbox", $(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
+});
+
+if ( !isLocal ) {
+ test("attr(String, Object) - Loaded via XML document", function() {
+ expect(2);
+ stop();
+ $.get('data/dashboard.xml', function(xml) {
+ var titles = [];
+ $('tab', xml).each(function() {
+ titles.push($(this).attr('title'));
+ });
+ equals( titles[0], 'Location', 'attr() in XML context: Check first title' );
+ equals( titles[1], 'Users', 'attr() in XML context: Check second title' );
+ start();
+ });
+ });
+}
+
+test("css(String|Hash)", function() {
+ expect(19);
+
+ equals( $('#main').css("display"), 'none', 'Check for css property "display"');
+
+ ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
+ $('#foo').css({display: 'none'});
+ ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
+ $('#foo').css({display: 'block'});
+ ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
+
+ $('#floatTest').css({styleFloat: 'right'});
+ equals( $('#floatTest').css('styleFloat'), 'right', 'Modified CSS float using "styleFloat": Assert float is right');
+ $('#floatTest').css({cssFloat: 'left'});
+ equals( $('#floatTest').css('cssFloat'), 'left', 'Modified CSS float using "cssFloat": Assert float is left');
+ $('#floatTest').css({'float': 'right'});
+ equals( $('#floatTest').css('float'), 'right', 'Modified CSS float using "float": Assert float is right');
+ $('#floatTest').css({'font-size': '30px'});
+ equals( $('#floatTest').css('font-size'), '30px', 'Modified CSS font-size: Assert font-size is 30px');
+
+ $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
+ $('#foo').css({opacity: n});
+ equals( $('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
+ $('#foo').css({opacity: parseFloat(n)});
+ equals( $('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
+ });
+ $('#foo').css({opacity: ''});
+ equals( $('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" );
+});
+
+test("css(String, Object)", function() {
+ expect(21);
+ ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
+ $('#foo').css('display', 'none');
+ ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
+ $('#foo').css('display', 'block');
+ ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
+
+ $('#floatTest').css('styleFloat', 'left');
+ equals( $('#floatTest').css('styleFloat'), 'left', 'Modified CSS float using "styleFloat": Assert float is left');
+ $('#floatTest').css('cssFloat', 'right');
+ equals( $('#floatTest').css('cssFloat'), 'right', 'Modified CSS float using "cssFloat": Assert float is right');
+ $('#floatTest').css('float', 'left');
+ equals( $('#floatTest').css('float'), 'left', 'Modified CSS float using "float": Assert float is left');
+ $('#floatTest').css('font-size', '20px');
+ equals( $('#floatTest').css('font-size'), '20px', 'Modified CSS font-size: Assert font-size is 20px');
+
+ $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
+ $('#foo').css('opacity', n);
+ equals( $('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
+ $('#foo').css('opacity', parseFloat(n));
+ equals( $('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
+ });
+ $('#foo').css('opacity', '');
+ equals( $('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" );
+ // for #1438, IE throws JS error when filter exists but doesn't have opacity in it
+ if (jQuery.browser.msie) {
+ $('#foo').css("filter", "progid:DXImageTransform.Microsoft.Chroma(color='red');");
+ }
+ equals( $('#foo').css('opacity'), '1', "Assert opacity is 1 when a different filter is set in IE, #1438" );
+
+ // using contents will get comments regular, text, and comment nodes
+ var j = $("#nonnodes").contents();
+ j.css("padding-left", "1px");
+ equals( j.css("padding-left"), "1px", "Check node,textnode,comment css works" );
+
+ // opera sometimes doesn't update 'display' correctly, see #2037
+ $("#t2037")[0].innerHTML = $("#t2037")[0].innerHTML
+ equals( $("#t2037 .hidden").css("display"), "none", "Make sure browser thinks it is hidden" );
+});
+
+test("jQuery.css(elem, 'height') doesn't clear radio buttons (bug #1095)", function () {
+ expect(4);
+
+ var $checkedtest = $("#checkedtest");
+ // IE6 was clearing "checked" in jQuery.css(elem, "height");
+ jQuery.css($checkedtest[0], "height");
+ ok( !! $(":radio:first", $checkedtest).attr("checked"), "Check first radio still checked." );
+ ok( ! $(":radio:last", $checkedtest).attr("checked"), "Check last radio still NOT checked." );
+ ok( !! $(":checkbox:first", $checkedtest).attr("checked"), "Check first checkbox still checked." );
+ ok( ! $(":checkbox:last", $checkedtest).attr("checked"), "Check last checkbox still NOT checked." );
+});
+
+test("width()", function() {
+ expect(9);
+
+ var $div = $("#nothiddendiv");
+ $div.width(30);
+ equals($div.width(), 30, "Test set to 30 correctly");
+ $div.width(-1); // handle negative numbers by ignoring #1599
+ equals($div.width(), 30, "Test negative width ignored");
+ $div.css("padding", "20px");
+ equals($div.width(), 30, "Test padding specified with pixels");
+ $div.css("border", "2px solid #fff");
+ equals($div.width(), 30, "Test border specified with pixels");
+ $div.css("padding", "2em");
+ equals($div.width(), 30, "Test padding specified with ems");
+ $div.css("border", "1em solid #fff");
+ equals($div.width(), 30, "Test border specified with ems");
+ $div.css("padding", "2%");
+ equals($div.width(), 30, "Test padding specified with percent");
+ $div.hide();
+ equals($div.width(), 30, "Test hidden div");
+
+ $div.css({ display: "", border: "", padding: "" });
+
+ $("#nothiddendivchild").css({ padding: "3px", border: "2px solid #fff" });
+ equals($("#nothiddendivchild").width(), 20, "Test child width with border and padding");
+ $("#nothiddendiv, #nothiddendivchild").css({ border: "", padding: "", width: "" });
+});
+
+test("height()", function() {
+ expect(8);
+
+ var $div = $("#nothiddendiv");
+ $div.height(30);
+ equals($div.height(), 30, "Test set to 30 correctly");
+ $div.height(-1); // handle negative numbers by ignoring #1599
+ equals($div.height(), 30, "Test negative height ignored");
+ $div.css("padding", "20px");
+ equals($div.height(), 30, "Test padding specified with pixels");
+ $div.css("border", "2px solid #fff");
+ equals($div.height(), 30, "Test border specified with pixels");
+ $div.css("padding", "2em");
+ equals($div.height(), 30, "Test padding specified with ems");
+ $div.css("border", "1em solid #fff");
+ equals($div.height(), 30, "Test border specified with ems");
+ $div.css("padding", "2%");
+ equals($div.height(), 30, "Test padding specified with percent");
+ $div.hide();
+ equals($div.height(), 30, "Test hidden div");
+
+ $div.css({ display: "", border: "", padding: "", height: "1px" });
+});
+
+test("text()", function() {
+ expect(1);
+ var expected = "This link has class=\"blog\": Simon Willison's Weblog";
+ equals( $('#sap').text(), expected, 'Check for merged text of more then one element.' );
+});
+
+test("wrap(String|Element)", function() {
+ expect(8);
+ var defaultText = 'Try them out:'
+ var result = $('#first').wrap('<div class="red"><span></span></div>').text();
+ equals( defaultText, result, 'Check for wrapping of on-the-fly html' );
+ ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
+
+ reset();
+ var defaultText = 'Try them out:'
+ var result = $('#first').wrap(document.getElementById('empty')).parent();
+ ok( result.is('ol'), 'Check for element wrapping' );
+ equals( result.text(), defaultText, 'Check for element wrapping' );
+
+ reset();
+ $('#check1').click(function() {
+ var checkbox = this;
+ ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
+ $(checkbox).wrap( '<div id="c1" style="display:none;"></div>' );
+ ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
+ }).click();
+
+ // using contents will get comments regular, text, and comment nodes
+ var j = $("#nonnodes").contents();
+ j.wrap("<i></i>");
+ equals( $("#nonnodes > i").length, 3, "Check node,textnode,comment wraps ok" );
+ equals( $("#nonnodes > i").text(), j.text() + j[1].nodeValue, "Check node,textnode,comment wraps doesn't hurt text" );
+});
+
+test("wrapAll(String|Element)", function() {
+ expect(8);
+ var prev = $("#first")[0].previousSibling;
+ var p = $("#first")[0].parentNode;
+ var result = $('#first,#firstp').wrapAll('<div class="red"><div id="tmp"></div></div>');
+ equals( result.parent().length, 1, 'Check for wrapping of on-the-fly html' );
+ ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
+ ok( $('#firstp').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
+ equals( $("#first").parent().parent()[0].previousSibling, prev, "Correct Previous Sibling" );
+ equals( $("#first").parent().parent()[0].parentNode, p, "Correct Parent" );
+
+ reset();
+ var prev = $("#first")[0].previousSibling;
+ var p = $("#first")[0].parentNode;
+ var result = $('#first,#firstp').wrapAll(document.getElementById('empty'));
+ equals( $("#first").parent()[0], $("#firstp").parent()[0], "Same Parent" );
+ equals( $("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" );
+ equals( $("#first").parent()[0].parentNode, p, "Correct Parent" );
+});
+
+test("wrapInner(String|Element)", function() {
+ expect(6);
+ var num = $("#first").children().length;
+ var result = $('#first').wrapInner('<div class="red"><div id="tmp"></div></div>');
+ equals( $("#first").children().length, 1, "Only one child" );
+ ok( $("#first").children().is(".red"), "Verify Right Element" );
+ equals( $("#first").children().children().children().length, num, "Verify Elements Intact" );
+
+ reset();
+ var num = $("#first").children().length;
+ var result = $('#first').wrapInner(document.getElementById('empty'));
+ equals( $("#first").children().length, 1, "Only one child" );
+ ok( $("#first").children().is("#empty"), "Verify Right Element" );
+ equals( $("#first").children().children().length, num, "Verify Elements Intact" );
+});
+
+test("append(String|Element|Array<Element>|jQuery)", function() {
+ expect(21);
+ var defaultText = 'Try them out:'
+ var result = $('#first').append('<b>buga</b>');
+ equals( result.text(), defaultText + 'buga', 'Check if text appending works' );
+ equals( $('#select3').append('<option value="appendTest">Append Test</option>').find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
+
+ reset();
+ var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
+ $('#sap').append(document.getElementById('first'));
+ equals( expected, $('#sap').text(), "Check for appending of element" );
+
+ reset();
+ expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
+ $('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]);
+ equals( expected, $('#sap').text(), "Check for appending of array of elements" );
+
+ reset();
+ expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
+ $('#sap').append($("#first, #yahoo"));
+ equals( expected, $('#sap').text(), "Check for appending of jQuery object" );
+
+ reset();
+ $("#sap").append( 5 );
+ ok( $("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
+
+ reset();
+ $("#sap").append( " text with spaces " );
+ ok( $("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
+
+ reset();
+ ok( $("#sap").append([]), "Check for appending an empty array." );
+ ok( $("#sap").append(""), "Check for appending an empty string." );
+ ok( $("#sap").append(document.getElementsByTagName("foo")), "Check for appending an empty nodelist." );
+
+ reset();
+ $("#sap").append(document.getElementById('form'));
+ equals( $("#sap>form").size(), 1, "Check for appending a form" ); // Bug #910
+
+ reset();
+ var pass = true;
+ try {
+ $( $("#iframe")[0].contentWindow.document.body ).append("<div>test</div>");
+ } catch(e) {
+ pass = false;
+ }
+
+ ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
+
+ reset();
+ $('<fieldset/>').appendTo('#form').append('<legend id="legend">test</legend>');
+ t( 'Append legend', '#legend', ['legend'] );
+
+ reset();
+ $('#select1').append('<OPTION>Test</OPTION>');
+ equals( $('#select1 option:last').text(), "Test", "Appending <OPTION> (all caps)" );
+
+ $('#table').append('<colgroup></colgroup>');
+ ok( $('#table colgroup').length, "Append colgroup" );
+
+ $('#table colgroup').append('<col/>');
+ ok( $('#table colgroup col').length, "Append col" );
+
+ reset();
+ $('#table').append('<caption></caption>');
+ ok( $('#table caption').length, "Append caption" );
+
+ reset();
+ $('form:last')
+ .append('<select id="appendSelect1"></select>')
+ .append('<select id="appendSelect2"><option>Test</option></select>');
+
+ t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
+
+ // using contents will get comments regular, text, and comment nodes
+ var j = $("#nonnodes").contents();
+ var d = $("<div/>").appendTo("#nonnodes").append(j);
+ equals( $("#nonnodes").length, 1, "Check node,textnode,comment append moved leaving just the div" );
+ ok( d.contents().length >= 2, "Check node,textnode,comment append works" );
+ d.contents().appendTo("#nonnodes");
+ d.remove();
+ ok( $("#nonnodes").contents().length >= 2, "Check node,textnode,comment append cleanup worked" );
+});
+
+test("appendTo(String|Element|Array<Element>|jQuery)", function() {
+ expect(6);
+ var defaultText = 'Try them out:'
+ $('<b>buga</b>').appendTo('#first');
+ equals( $("#first").text(), defaultText + 'buga', 'Check if text appending works' );
+ equals( $('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
+
+ reset();
+ var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
+ $(document.getElementById('first')).appendTo('#sap');
+ equals( expected, $('#sap').text(), "Check for appending of element" );
+
+ reset();
+ expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
+ $([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
+ equals( expected, $('#sap').text(), "Check for appending of array of elements" );
+
+ reset();
+ expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
+ $("#first, #yahoo").appendTo('#sap');
+ equals( expected, $('#sap').text(), "Check for appending of jQuery object" );
+
+ reset();
+ $('#select1').appendTo('#foo');
+ t( 'Append select', '#foo select', ['select1'] );
+});
+
+test("prepend(String|Element|Array<Element>|jQuery)", function() {
+ expect(5);
+ var defaultText = 'Try them out:'
+ var result = $('#first').prepend('<b>buga</b>');
+ equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
+ equals( $('#select3').prepend('<option value="prependTest">Prepend Test</option>').find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
+
+ reset();
+ var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
+ $('#sap').prepend(document.getElementById('first'));
+ equals( expected, $('#sap').text(), "Check for prepending of element" );
+
+ reset();
+ expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
+ $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]);
+ equals( expected, $('#sap').text(), "Check for prepending of array of elements" );
+
+ reset();
+ expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
+ $('#sap').prepend($("#first, #yahoo"));
+ equals( expected, $('#sap').text(), "Check for prepending of jQuery object" );
+});
+
+test("prependTo(String|Element|Array<Element>|jQuery)", function() {
+ expect(6);
+ var defaultText = 'Try them out:'
+ $('<b>buga</b>').prependTo('#first');
+ equals( $('#first').text(), 'buga' + defaultText, 'Check if text prepending works' );
+ equals( $('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
+
+ reset();
+ var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
+ $(document.getElementById('first')).prependTo('#sap');
+ equals( expected, $('#sap').text(), "Check for prepending of element" );
+
+ reset();
+ expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
+ $([document.getElementById('yahoo'), document.getElementById('first')]).prependTo('#sap');
+ equals( expected, $('#sap').text(), "Check for prepending of array of elements" );
+
+ reset();
+ expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
+ $("#yahoo, #first").prependTo('#sap');
+ equals( expected, $('#sap').text(), "Check for prepending of jQuery object" );
+
+ reset();
+ $('<select id="prependSelect1"></select>').prependTo('form:last');
+ $('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
+
+ t( "Prepend Select", "#prependSelect1, #prependSelect2", ["prependSelect1", "prependSelect2"] );
+});
+
+test("before(String|Element|Array<Element>|jQuery)", function() {
+ expect(4);
+ var expected = 'This is a normal link: bugaYahoo';
+ $('#yahoo').before('<b>buga</b>');
+ equals( expected, $('#en').text(), 'Insert String before' );
+
+ reset();
+ expected = "This is a normal link: Try them out:Yahoo";
+ $('#yahoo').before(document.getElementById('first'));
+ equals( expected, $('#en').text(), "Insert element before" );
+
+ reset();
+ expected = "This is a normal link: Try them out:diveintomarkYahoo";
+ $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]);
+ equals( expected, $('#en').text(), "Insert array of elements before" );
+
+ reset();
+ expected = "This is a normal link: Try them out:diveintomarkYahoo";
+ $('#yahoo').before($("#first, #mark"));
+ equals( expected, $('#en').text(), "Insert jQuery before" );
+});
+
+test("insertBefore(String|Element|Array<Element>|jQuery)", function() {
+ expect(4);
+ var expected = 'This is a normal link: bugaYahoo';
+ $('<b>buga</b>').insertBefore('#yahoo');
+ equals( expected, $('#en').text(), 'Insert String before' );
+
+ reset();
+ expected = "This is a normal link: Try them out:Yahoo";
+ $(document.getElementById('first')).insertBefore('#yahoo');
+ equals( expected, $('#en').text(), "Insert element before" );
+
+ reset();
+ expected = "This is a normal link: Try them out:diveintomarkYahoo";
+ $([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
+ equals( expected, $('#en').text(), "Insert array of elements before" );
+
+ reset();
+ expected = "This is a normal link: Try them out:diveintomarkYahoo";
+ $("#first, #mark").insertBefore('#yahoo');
+ equals( expected, $('#en').text(), "Insert jQuery before" );
+});
+
+test("after(String|Element|Array<Element>|jQuery)", function() {
+ expect(4);
+ var expected = 'This is a normal link: Yahoobuga';
+ $('#yahoo').after('<b>buga</b>');
+ equals( expected, $('#en').text(), 'Insert String after' );
+
+ reset();
+ expected = "This is a normal link: YahooTry them out:";
+ $('#yahoo').after(document.getElementById('first'));
+ equals( expected, $('#en').text(), "Insert element after" );
+
+ reset();
+ expected = "This is a normal link: YahooTry them out:diveintomark";
+ $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]);
+ equals( expected, $('#en').text(), "Insert array of elements after" );
+
+ reset();
+ expected = "This is a normal link: YahooTry them out:diveintomark";
+ $('#yahoo').after($("#first, #mark"));
+ equals( expected, $('#en').text(), "Insert jQuery after" );
+});
+
+test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
+ expect(4);
+ var expected = 'This is a normal link: Yahoobuga';
+ $('<b>buga</b>').insertAfter('#yahoo');
+ equals( expected, $('#en').text(), 'Insert String after' );
+
+ reset();
+ expected = "This is a normal link: YahooTry them out:";
+ $(document.getElementById('first')).insertAfter('#yahoo');
+ equals( expected, $('#en').text(), "Insert element after" );
+
+ reset();
+ expected = "This is a normal link: YahooTry them out:diveintomark";
+ $([document.getElementById('mark'), document.getElementById('first')]).insertAfter('#yahoo');
+ equals( expected, $('#en').text(), "Insert array of elements after" );
+
+ reset();
+ expected = "This is a normal link: YahooTry them out:diveintomark";
+ $("#mark, #first").insertAfter('#yahoo');
+ equals( expected, $('#en').text(), "Insert jQuery after" );
+});
+
+test("replaceWith(String|Element|Array<Element>|jQuery)", function() {
+ expect(10);
+ $('#yahoo').replaceWith('<b id="replace">buga</b>');
+ ok( $("#replace")[0], 'Replace element with string' );
+ ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' );
+
+ reset();
+ $('#yahoo').replaceWith(document.getElementById('first'));
+ ok( $("#first")[0], 'Replace element with element' );
+ ok( !$("#yahoo")[0], 'Verify that original element is gone, after element' );
+
+ reset();
+ $('#yahoo').replaceWith([document.getElementById('first'), document.getElementById('mark')]);
+ ok( $("#first")[0], 'Replace element with array of elements' );
+ ok( $("#mark")[0], 'Replace element with array of elements' );
+ ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
+
+ reset();
+ $('#yahoo').replaceWith($("#first, #mark"));
+ ok( $("#first")[0], 'Replace element with set of elements' );
+ ok( $("#mark")[0], 'Replace element with set of elements' );
+ ok( !$("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
+});
+
+test("replaceAll(String|Element|Array<Element>|jQuery)", function() {
+ expect(10);
+ $('<b id="replace">buga</b>').replaceAll("#yahoo");
+ ok( $("#replace")[0], 'Replace element with string' );
+ ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' );
+
+ reset();
+ $(document.getElementById('first')).replaceAll("#yahoo");
+ ok( $("#first")[0], 'Replace element with element' );
+ ok( !$("#yahoo")[0], 'Verify that original element is gone, after element' );
+
+ reset();
+ $([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
+ ok( $("#first")[0], 'Replace element with array of elements' );
+ ok( $("#mark")[0], 'Replace element with array of elements' );
+ ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
+
+ reset();
+ $("#first, #mark").replaceAll("#yahoo");
+ ok( $("#first")[0], 'Replace element with set of elements' );
+ ok( $("#mark")[0], 'Replace element with set of elements' );
+ ok( !$("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
+});
+
+test("end()", function() {
+ expect(3);
+ equals( 'Yahoo', $('#yahoo').parent().end().text(), 'Check for end' );
+ ok( $('#yahoo').end(), 'Check for end with nothing to end' );
+
+ var x = $('#yahoo');
+ x.parent();
+ equals( 'Yahoo', $('#yahoo').text(), 'Check for non-destructive behaviour' );
+});
+
+test("find(String)", function() {
+ expect(2);
+ equals( 'Yahoo', $('#foo').find('.blogTest').text(), 'Check for find' );
+
+ // using contents will get comments regular, text, and comment nodes
+ var j = $("#nonnodes").contents();
+ equals( j.find("div").length, 0, "Check node,textnode,comment to find zero divs" );
+});
+
+test("clone()", function() {
+ expect(20);
+ equals( 'This is a normal link: Yahoo', $('#en').text(), 'Assert text for #en' );
+ var clone = $('#yahoo').clone();
+ equals( 'Try them out:Yahoo', $('#first').append(clone).text(), 'Check for clone' );
+ equals( 'This is a normal link: Yahoo', $('#en').text(), 'Reassert text for #en' );
+
+ var cloneTags = [
+ "<table/>", "<tr/>", "<td/>", "<div/>",
+ "<button/>", "<ul/>", "<ol/>", "<li/>",
+ "<input type='checkbox' />", "<select/>", "<option/>", "<textarea/>",
+ "<tbody/>", "<thead/>", "<tfoot/>", "<iframe/>"
+ ];
+ for (var i = 0; i < cloneTags.length; i++) {
+ var j = $(cloneTags[i]);
+ equals( j[0].tagName, j.clone()[0].tagName, 'Clone a <' + cloneTags[i].substring(1));
+ }
+
+ // using contents will get comments regular, text, and comment nodes
+ var cl = $("#nonnodes").contents().clone();
+ ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
+});
+
+if (!isLocal) {
+test("clone() on XML nodes", function() {
+ expect(2);
+ stop();
+ $.get("data/dashboard.xml", function (xml) {
+ var root = $(xml.documentElement).clone();
+ $("tab:first", xml).text("origval");
+ $("tab:first", root).text("cloneval");
+ equals($("tab:first", xml).text(), "origval", "Check original XML node was correctly set");
+ equals($("tab:first", root).text(), "cloneval", "Check cloned XML node was correctly set");
+ start();
+ });
+});
+}
+
+test("is(String)", function() {
+ expect(26);
+ ok( $('#form').is('form'), 'Check for element: A form must be a form' );
+ ok( !$('#form').is('div'), 'Check for element: A form is not a div' );
+ ok( $('#mark').is('.blog'), 'Check for class: Expected class "blog"' );
+ ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' );
+ ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' );
+ ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' );
+ ok( $('#en').is('[lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' );
+ ok( !$('#en').is('[lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' );
+ ok( $('#text1').is('[type="text"]'), 'Check for attribute: Expected attribute type to be "text"' );
+ ok( !$('#text1').is('[type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' );
+ ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' );
+ ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' );
+ ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' );
+ ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' );
+ ok( $('#foo').is(':has(p)'), 'Check for child: Expected a child "p" element' );
+ ok( !$('#foo').is(':has(ul)'), 'Check for child: Did not expect "ul" element' );
+ ok( $('#foo').is(':has(p):has(a):has(code)'), 'Check for childs: Expected "p", "a" and "code" child elements' );
+ ok( !$('#foo').is(':has(p):has(a):has(code):has(ol)'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' );
+ ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' );
+ ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' );
+ ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' );
+ ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' );
+
+ // test is() with comma-seperated expressions
+ ok( $('#en').is('[lang="en"],[lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
+ ok( $('#en').is('[lang="de"],[lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
+ ok( $('#en').is('[lang="en"] , [lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
+ ok( $('#en').is('[lang="de"] , [lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
+});
+
+test("$.extend(Object, Object)", function() {
+ expect(20);
+
+ var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
+ options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
+ optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
+ merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },
+ deep1 = { foo: { bar: true } },
+ deep1copy = { foo: { bar: true } },
+ deep2 = { foo: { baz: true }, foo2: document },
+ deep2copy = { foo: { baz: true }, foo2: document },
+ deepmerged = { foo: { bar: true, baz: true }, foo2: document };
+
+ jQuery.extend(settings, options);
+ isObj( settings, merged, "Check if extended: settings must be extended" );
+ isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
+
+ jQuery.extend(settings, null, options);
+ isObj( settings, merged, "Check if extended: settings must be extended" );
+ isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
+
+ jQuery.extend(true, deep1, deep2);
+ isObj( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
+ isObj( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
+ equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );
+
+ var nullUndef;
+ nullUndef = jQuery.extend({}, options, { xnumber2: null });
+ ok( nullUndef.xnumber2 === null, "Check to make sure null values are copied");
+
+ nullUndef = jQuery.extend({}, options, { xnumber2: undefined });
+ ok( nullUndef.xnumber2 === options.xnumber2, "Check to make sure undefined values are not copied");
+
+ nullUndef = jQuery.extend({}, options, { xnumber0: null });
+ ok( nullUndef.xnumber0 === null, "Check to make sure null values are inserted");
+
+ var target = {};
+ var recursive = { foo:target, bar:5 };
+ jQuery.extend(true, target, recursive);
+ isObj( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
+
+ var ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907
+ equals( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );
+
+ var ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } );
+ ok( typeof ret.foo != "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" );
+
+ var ret = jQuery.extend(true, { foo:"bar" }, { foo:null } );
+ ok( typeof ret.foo !== 'undefined', "Make sure a null value doesn't crash with deep extend, for #1908" );
+
+ var obj = { foo:null };
+ jQuery.extend(true, obj, { foo:"notnull" } );
+ equals( obj.foo, "notnull", "Make sure a null value can be overwritten" );
+
+ function func() {}
+ jQuery.extend(func, { key: "value" } );
+ equals( func.key, "value", "Verify a function can be extended" );
+
+ var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
+ defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
+ options1 = { xnumber2: 1, xstring2: "x" },
+ options1Copy = { xnumber2: 1, xstring2: "x" },
+ options2 = { xstring2: "xx", xxx: "newstringx" },
+ options2Copy = { xstring2: "xx", xxx: "newstringx" },
+ merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
+
+ var settings = jQuery.extend({}, defaults, options1, options2);
+ isObj( settings, merged2, "Check if extended: settings must be extended" );
+ isObj( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
+ isObj( options1, options1Copy, "Check if not modified: options1 must not be modified" );
+ isObj( options2, options2Copy, "Check if not modified: options2 must not be modified" );
+});
+
+test("val()", function() {
+ expect(4);
+ equals( $("#text1").val(), "Test", "Check for value of input element" );
+ equals( !$("#text1").val(), "", "Check for value of input element" );
+ // ticket #1714 this caused a JS error in IE
+ equals( $("#first").val(), "", "Check a paragraph element to see if it has a value" );
+ ok( $([]).val() === undefined, "Check an empty jQuery object will return undefined from val" );
+});
+
+test("val(String)", function() {
+ expect(4);
+ document.getElementById('text1').value = "bla";
+ equals( $("#text1").val(), "bla", "Check for modified value of input element" );
+ $("#text1").val('test');
+ ok ( document.getElementById('text1').value == "test", "Check for modified (via val(String)) value of input element" );
+
+ $("#select1").val("3");
+ equals( $("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
+
+ // using contents will get comments regular, text, and comment nodes
+ var j = $("#nonnodes").contents();
+ j.val("asdf");
+ equals( j.val(), "asdf", "Check node,textnode,comment with val()" );
+ j.removeAttr("value");
+});
+
+var scriptorder = 0;
+
+test("html(String)", function() {
+ expect(11);
+ var div = $("#main > div");
+ div.html("<b>test</b>");
+ var pass = true;
+ for ( var i = 0; i < div.size(); i++ ) {
+ if ( div.get(i).childNodes.length != 1 ) pass = false;
+ }
+ ok( pass, "Set HTML" );
+
+ reset();
+ // using contents will get comments regular, text, and comment nodes
+ var j = $("#nonnodes").contents();
+ j.html("<b>bold</b>");
+
+ // this is needed, or the expando added by jQuery unique will yield a different html
+ j.find('b').removeData();
+ equals( j.html().toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
+
+ $("#main").html("<select/>");
+ $("#main select").html("<option>O1</option><option selected='selected'>O2</option><option>O3</option>");
+ equals( $("#main select").val(), "O2", "Selected option correct" );
+
+ stop();
+
+ $("#main").html('<script type="text/javascript">ok( true, "$().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script>');
+
+ $("#main").html('foo <form><script type="text/javascript">ok( true, "$().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script></form>');
+
+ // it was decided that waiting to execute ALL scripts makes sense since nested ones have to wait anyway so this test case is changed, see #1959
+ $("#main").html("<script>equals(scriptorder++, 0, 'Script is executed in order');equals($('#scriptorder').length, 1,'Execute after html (even though appears before)')<\/script><span id='scriptorder'><script>equals(scriptorder++, 1, 'Script (nested) is executed in order');equals($('#scriptorder').length, 1,'Execute after html')<\/script></span><script>equals(scriptorder++, 2, 'Script (unnested) is executed in order');equals($('#scriptorder').length, 1,'Execute after html')<\/script>");
+
+ setTimeout( start, 100 );
+});
+
+test("filter()", function() {
+ expect(6);
+ isSet( $("#form input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );
+ isSet( $("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" );
+ isSet( $("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" );
+ isSet( $("p").filter(function() { return !$("a", this).length }).get(), q("sndp", "first"), "filter(Function)" );
+
+ // using contents will get comments regular, text, and comment nodes
+ var j = $("#nonnodes").contents();
+ equals( j.filter("span").length, 1, "Check node,textnode,comment to filter the one span" );
+ equals( j.filter("[name]").length, 0, "Check node,textnode,comment to filter the one span" );
+});
+
+test("not()", function() {
+ expect(8);
+ equals( $("#main > p#ap > a").not("#google").length, 2, "not('selector')" );
+ equals( $("#main > p#ap > a").not(document.getElementById("google")).length, 2, "not(DOMElement)" );
+ isSet( $("p").not(".result").get(), q("firstp", "ap", "sndp", "en", "sap", "first"), "not('.class')" );
+ isSet( $("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" );
+ isSet( $("p").not($("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );
+ equals( $("p").not(document.getElementsByTagName("p")).length, 0, "not(Array-like DOM collection)" );
+ isSet( $("#form option").not("option.emptyopt:contains('Nothing'),[selected],[value='1']").get(), q("option1c", "option1d", "option2c", "option3d" ), "not('complex selector')");
+
+ var selects = $("#form select");
+ isSet( selects.not( selects[1] ), q("select1", "select3"), "filter out DOM element");
+});
+
+test("andSelf()", function() {
+ expect(4);
+ isSet( $("#en").siblings().andSelf().get(), q("sndp", "sap","en"), "Check for siblings and self" );
+ isSet( $("#foo").children().andSelf().get(), q("sndp", "en", "sap", "foo"), "Check for children and self" );
+ isSet( $("#en, #sndp").parent().andSelf().get(), q("foo","en","sndp"), "Check for parent and self" );
+ isSet( $("#groups").parents("p, div").andSelf().get(), q("ap", "main", "groups"), "Check for parents and self" );
+});
+
+test("siblings([String])", function() {
+ expect(5);
+ isSet( $("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );
+ isSet( $("#sndp").siblings(":has(code)").get(), q("sap"), "Check for filtered siblings (has code child element)" );
+ isSet( $("#sndp").siblings(":has(a)").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
+ isSet( $("#foo").siblings("form, b").get(), q("form", "lengthtest", "testForm", "floatTest"), "Check for multiple filters" );
+ isSet( $("#en, #sndp").siblings().get(), q("sndp", "sap", "en"), "Check for unique results from siblings" );
+});
+
+test("children([String])", function() {
+ expect(3);
+ isSet( $("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" );
+ isSet( $("#foo").children(":has(code)").get(), q("sndp", "sap"), "Check for filtered children" );
+ isSet( $("#foo").children("#en, #sap").get(), q("en", "sap"), "Check for multiple filters" );
+});
+
+test("parent([String])", function() {
+ expect(5);
+ equals( $("#groups").parent()[0].id, "ap", "Simple parent check" );
+ equals( $("#groups").parent("p")[0].id, "ap", "Filtered parent check" );
+ equals( $("#groups").parent("div").length, 0, "Filtered parent check, no match" );
+ equals( $("#groups").parent("div, p")[0].id, "ap", "Check for multiple filters" );
+ isSet( $("#en, #sndp").parent().get(), q("foo"), "Check for unique results from parent" );
+});
+
+test("parents([String])", function() {
+ expect(5);
+ equals( $("#groups").parents()[0].id, "ap", "Simple parents check" );
+ equals( $("#groups").parents("p")[0].id, "ap", "Filtered parents check" );
+ equals( $("#groups").parents("div")[0].id, "main", "Filtered parents check2" );
+ isSet( $("#groups").parents("p, div").get(), q("ap", "main"), "Check for multiple filters" );
+ isSet( $("#en, #sndp").parents().get(), q("foo", "main", "dl", "body", "html"), "Check for unique results from parents" );
+});
+
+test("next([String])", function() {
+ expect(4);
+ equals( $("#ap").next()[0].id, "foo", "Simple next check" );
+ equals( $("#ap").next("div")[0].id, "foo", "Filtered next check" );
+ equals( $("#ap").next("p").length, 0, "Filtered next check, no match" );
+ equals( $("#ap").next("div, p")[0].id, "foo", "Multiple filters" );
+});
+
+test("prev([String])", function() {
+ expect(4);
+ equals( $("#foo").prev()[0].id, "ap", "Simple prev check" );
+ equals( $("#foo").prev("p")[0].id, "ap", "Filtered prev check" );
+ equals( $("#foo").prev("div").length, 0, "Filtered prev check, no match" );
+ equals( $("#foo").prev("p, div")[0].id, "ap", "Multiple filters" );
+});
+
+test("show()", function() {
+ expect(15);
+ var pass = true, div = $("div");
+ div.show().each(function(){
+ if ( this.style.display == "none" ) pass = false;
+ });
+ ok( pass, "Show" );
+
+ $("#main").append('<div id="show-tests"><div><p><a href="#"></a></p><code></code><pre></pre><span></span></div><table><thead><tr><th></th></tr></thead><tbody><tr><td></td></tr></tbody></table><ul><li></li></ul></div>');
+ var test = {
+ "div" : "block",
+ "p" : "block",
+ "a" : "inline",
+ "code" : "inline",
+ "pre" : "block",
+ "span" : "inline",
+ "table" : $.browser.msie ? "block" : "table",
+ "thead" : $.browser.msie ? "block" : "table-header-group",
+ "tbody" : $.browser.msie ? "block" : "table-row-group",
+ "tr" : $.browser.msie ? "block" : "table-row",
+ "th" : $.browser.msie ? "block" : "table-cell",
+ "td" : $.browser.msie ? "block" : "table-cell",
+ "ul" : "block",
+ "li" : $.browser.msie ? "block" : "list-item"
+ };
+
+ $.each(test, function(selector, expected) {
+ var elem = $(selector, "#show-tests").show();
+ equals( elem.css("display"), expected, "Show using correct display type for " + selector );
+ });
+});
+
+test("addClass(String)", function() {
+ expect(2);
+ var div = $("div");
+ div.addClass("test");
+ var pass = true;
+ for ( var i = 0; i < div.size(); i++ ) {
+ if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
+ }
+ ok( pass, "Add Class" );
+
+ // using contents will get regular, text, and comment nodes
+ var j = $("#nonnodes").contents();
+ j.addClass("asdf");
+ ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
+});
+
+test("removeClass(String) - simple", function() {
+ expect(4);
+ var div = $("div").addClass("test").removeClass("test"),
+ pass = true;
+ for ( var i = 0; i < div.size(); i++ ) {
+ if ( div.get(i).className.indexOf("test") != -1 ) pass = false;
+ }
+ ok( pass, "Remove Class" );
+
+ reset();
+ var div = $("div").addClass("test").addClass("foo").addClass("bar");
+ div.removeClass("test").removeClass("bar").removeClass("foo");
+ var pass = true;
+ for ( var i = 0; i < div.size(); i++ ) {
+ if ( div.get(i).className.match(/test|bar|foo/) ) pass = false;
+ }
+ ok( pass, "Remove multiple classes" );
+
+ reset();
+ var div = $("div:eq(0)").addClass("test").removeClass("");
+ ok( div.is('.test'), "Empty string passed to removeClass" );
+
+ // using contents will get regular, text, and comment nodes
+ var j = $("#nonnodes").contents();
+ j.removeClass("asdf");
+ ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
+});
+
+test("toggleClass(String)", function() {
+ expect(3);
+ var e = $("#firstp");
+ ok( !e.is(".test"), "Assert class not present" );
+ e.toggleClass("test");
+ ok( e.is(".test"), "Assert class present" );
+ e.toggleClass("test");
+ ok( !e.is(".test"), "Assert class not present" );
+});
+
+test("removeAttr(String", function() {
+ expect(1);
+ equals( $('#mark').removeAttr("class")[0].className, "", "remove class" );
+});
+
+test("text(String)", function() {
+ expect(4);
+ equals( $("#foo").text("<div><b>Hello</b> cruel world!</div>")[0].innerHTML, "<div><b>Hello</b> cruel world!</div>", "Check escaped text" );
+
+ // using contents will get comments regular, text, and comment nodes
+ var j = $("#nonnodes").contents();
+ j.text("hi!");
+ equals( $(j[0]).text(), "hi!", "Check node,textnode,comment with text()" );
+ equals( j[1].nodeValue, " there ", "Check node,textnode,comment with text()" );
+ equals( j[2].nodeType, 8, "Check node,textnode,comment with text()" );
+});
+
+test("$.each(Object,Function)", function() {
+ expect(12);
+ $.each( [0,1,2], function(i, n){
+ equals( i, n, "Check array iteration" );
+ });
+
+ $.each( [5,6,7], function(i, n){
+ equals( i, n - 5, "Check array iteration" );
+ });
+
+ $.each( { name: "name", lang: "lang" }, function(i, n){
+ equals( i, n, "Check object iteration" );
+ });
+
+ var total = 0;
+ jQuery.each([1,2,3], function(i,v){ total += v; });
+ equals( total, 6, "Looping over an array" );
+ total = 0;
+ jQuery.each([1,2,3], function(i,v){ total += v; if ( i == 1 ) return false; });
+ equals( total, 3, "Looping over an array, with break" );
+ total = 0;
+ jQuery.each({"a":1,"b":2,"c":3}, function(i,v){ total += v; });
+ equals( total, 6, "Looping over an object" );
+ total = 0;
+ jQuery.each({"a":3,"b":3,"c":3}, function(i,v){ total += v; return false; });
+ equals( total, 3, "Looping over an object, with break" );
+});
+
+test("$.prop", function() {
+ expect(2);
+ var handle = function() { return this.id };
+ equals( $.prop($("#ap")[0], handle), "ap", "Check with Function argument" );
+ equals( $.prop($("#ap")[0], "value"), "value", "Check with value argument" );
+});
+
+test("$.className", function() {
+ expect(6);
+ var x = $("<p>Hi</p>")[0];
+ var c = $.className;
+ c.add(x, "hi");
+ equals( x.className, "hi", "Check single added class" );
+ c.add(x, "foo bar");
+ equals( x.className, "hi foo bar", "Check more added classes" );
+ c.remove(x);
+ equals( x.className, "", "Remove all classes" );
+ c.add(x, "hi foo bar");
+ c.remove(x, "foo");
+ equals( x.className, "hi bar", "Check removal of one class" );
+ ok( c.has(x, "hi"), "Check has1" );
+ ok( c.has(x, "bar"), "Check has2" );
+});
+
+test("$.data", function() {
+ expect(5);
+ var div = $("#foo")[0];
+ equals( jQuery.data(div, "test"), undefined, "Check for no data exists" );
+ jQuery.data(div, "test", "success");
+ equals( jQuery.data(div, "test"), "success", "Check for added data" );
+ jQuery.data(div, "test", "overwritten");
+ equals( jQuery.data(div, "test"), "overwritten", "Check for overwritten data" );
+ jQuery.data(div, "test", undefined);
+ equals( jQuery.data(div, "test"), "overwritten", "Check that data wasn't removed");
+ jQuery.data(div, "test", null);
+ ok( jQuery.data(div, "test") === null, "Check for null data");
+});
+
+test(".data()", function() {
+ expect(18);
+ var div = $("#foo");
+ equals( div.data("test"), undefined, "Check for no data exists" );
+ div.data("test", "success");
+ equals( div.data("test"), "success", "Check for added data" );
+ div.data("test", "overwritten");
+ equals( div.data("test"), "overwritten", "Check for overwritten data" );
+ div.data("test", undefined);
+ equals( div.data("test"), "overwritten", "Check that data wasn't removed");
+ div.data("test", null);
+ ok( div.data("test") === null, "Check for null data");
+
+ div.data("test", "overwritten");
+ var hits = {test:0}, gets = {test:0};
+
+ div
+ .bind("setData",function(e,key,value){ hits[key] += value; })
+ .bind("setData.foo",function(e,key,value){ hits[key] += value; })
+ .bind("getData",function(e,key){ gets[key] += 1; })
+ .bind("getData.foo",function(e,key){ gets[key] += 3; });
+
+ div.data("test.foo", 2);
+ equals( div.data("test"), "overwritten", "Check for original data" );
+ equals( div.data("test.foo"), 2, "Check for namespaced data" );
+ equals( div.data("test.bar"), "overwritten", "Check for unmatched namespace" );
+ equals( hits.test, 2, "Check triggered setter functions" );
+ equals( gets.test, 5, "Check triggered getter functions" );
+
+ hits.test = 0;
+ gets.test = 0;
+
+ div.data("test", 1);
+ equals( div.data("test"), 1, "Check for original data" );
+ equals( div.data("test.foo"), 2, "Check for namespaced data" );
+ equals( div.data("test.bar"), 1, "Check for unmatched namespace" );
+ equals( hits.test, 1, "Check triggered setter functions" );
+ equals( gets.test, 5, "Check triggered getter functions" );
+
+ hits.test = 0;
+ gets.test = 0;
+
+ div
+ .bind("getData",function(e,key){ return key + "root"; })
+ .bind("getData.foo",function(e,key){ return key + "foo"; });
+
+ equals( div.data("test"), "testroot", "Check for original data" );
+ equals( div.data("test.foo"), "testfoo", "Check for namespaced data" );
+ equals( div.data("test.bar"), "testroot", "Check for unmatched namespace" );
+});
+
+test("$.removeData", function() {
+ expect(1);
+ var div = $("#foo")[0];
+ jQuery.data(div, "test", "testing");
+ jQuery.removeData(div, "test");
+ equals( jQuery.data(div, "test"), undefined, "Check removal of data" );
+});
+
+test(".removeData()", function() {
+ expect(6);
+ var div = $("#foo");
+ div.data("test", "testing");
+ div.removeData("test");
+ equals( div.data("test"), undefined, "Check removal of data" );
+
+ div.data("test", "testing");
+ div.data("test.foo", "testing2");
+ div.removeData("test.bar");
+ equals( div.data("test.foo"), "testing2", "Make sure data is intact" );
+ equals( div.data("test"), "testing", "Make sure data is intact" );
+
+ div.removeData("test");
+ equals( div.data("test.foo"), "testing2", "Make sure data is intact" );
+ equals( div.data("test"), undefined, "Make sure data is intact" );
+
+ div.removeData("test.foo");
+ equals( div.data("test.foo"), undefined, "Make sure data is intact" );
+});
+
+test("remove()", function() {
+ expect(6);
+ $("#ap").children().remove();
+ ok( $("#ap").text().length > 10, "Check text is not removed" );
+ equals( $("#ap").children().length, 0, "Check remove" );
+
+ reset();
+ $("#ap").children().remove("a");
+ ok( $("#ap").text().length > 10, "Check text is not removed" );
+ equals( $("#ap").children().length, 1, "Check filtered remove" );
+
+ // using contents will get comments regular, text, and comment nodes
+ equals( $("#nonnodes").contents().length, 3, "Check node,textnode,comment remove works" );
+ $("#nonnodes").contents().remove();
+ equals( $("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
+});
+
+test("empty()", function() {
+ expect(3);
+ equals( $("#ap").children().empty().text().length, 0, "Check text is removed" );
+ equals( $("#ap").children().length, 4, "Check elements are not removed" );
+
+ // using contents will get comments regular, text, and comment nodes
+ var j = $("#nonnodes").contents();
+ j.empty();
+ equals( j.html(), "", "Check node,textnode,comment empty works" );
+});
+
+test("slice()", function() {
+ expect(5);
+ isSet( $("#ap a").slice(1,2), q("groups"), "slice(1,2)" );
+ isSet( $("#ap a").slice(1), q("groups", "anchor1", "mark"), "slice(1)" );
+ isSet( $("#ap a").slice(0,3), q("google", "groups", "anchor1"), "slice(0,3)" );
+ isSet( $("#ap a").slice(-1), q("mark"), "slice(-1)" );
+
+ isSet( $("#ap a").eq(1), q("groups"), "eq(1)" );
+});
+
+test("map()", function() {
+ expect(2);//expect(6);
+
+ isSet(
+ $("#ap").map(function(){
+ return $(this).find("a").get();
+ }),
+ q("google", "groups", "anchor1", "mark"),
+ "Array Map"
+ );
+
+ isSet(
+ $("#ap > a").map(function(){
+ return this.parentNode;
+ }),
+ q("ap","ap","ap"),
+ "Single Map"
+ );
+
+ return;//these haven't been accepted yet
+
+ //for #2616
+ var keys = $.map( {a:1,b:2}, function( v, k ){
+ return k;
+ }, [ ] );
+
+ equals( keys.join(""), "ab", "Map the keys from a hash to an array" );
+
+ var values = $.map( {a:1,b:2}, function( v, k ){
+ return v;
+ }, [ ] );
+
+ equals( values.join(""), "12", "Map the values from a hash to an array" );
+
+ var scripts = document.getElementsByTagName("script");
+ var mapped = $.map( scripts, function( v, k ){
+ return v;
+ }, {length:0} );
+
+ equals( mapped.length, scripts.length, "Map an array(-like) to a hash" );
+
+ var flat = $.map( Array(4), function( v, k ){
+ return k % 2 ? k : [k,k,k];//try mixing array and regular returns
+ });
+
+ equals( flat.join(""), "00012223", "try the new flatten technique(#2616)" );
+});
+
+test("contents()", function() {
+ expect(12);
+ equals( $("#ap").contents().length, 9, "Check element contents" );
+ ok( $("#iframe").contents()[0], "Check existance of IFrame document" );
+ var ibody = $("#loadediframe").contents()[0].body;
+ ok( ibody, "Check existance of IFrame body" );
+
+ equals( $("span", ibody).text(), "span text", "Find span in IFrame and check its text" );
+
+ $(ibody).append("<div>init text</div>");
+ equals( $("div", ibody).length, 2, "Check the original div and the new div are in IFrame" );
+
+ equals( $("div:last", ibody).text(), "init text", "Add text to div in IFrame" );
+
+ $("div:last", ibody).text("div text");
+ equals( $("div:last", ibody).text(), "div text", "Add text to div in IFrame" );
+
+ $("div:last", ibody).remove();
+ equals( $("div", ibody).length, 1, "Delete the div and check only one div left in IFrame" );
+
+ equals( $("div", ibody).text(), "span text", "Make sure the correct div is still left after deletion in IFrame" );
+
+ $("<table/>", ibody).append("<tr><td>cell</td></tr>").appendTo(ibody);
+ $("table", ibody).remove();
+ equals( $("div", ibody).length, 1, "Check for JS error on add and delete of a table in IFrame" );
+
+ // using contents will get comments regular, text, and comment nodes
+ var c = $("#nonnodes").contents().contents();
+ equals( c.length, 1, "Check node,textnode,comment contents is just one" );
+ equals( c[0].nodeValue, "hi", "Check node,textnode,comment contents is just the one from span" );
+});
+
+test("$.makeArray", function(){
+ expect(15);
+
+ equals( $.makeArray($('html>*'))[0].nodeName, "HEAD", "Pass makeArray a jQuery object" );
+
+ equals( $.makeArray(document.getElementsByName("PWD")).slice(0,1)[0].name, "PWD", "Pass makeArray a nodelist" );
+
+ equals( (function(){ return $.makeArray(arguments); })(1,2).join(""), "12", "Pass makeArray an arguments array" );
+
+ equals( $.makeArray([1,2,3]).join(""), "123", "Pass makeArray a real array" );
+
+ equals( $.makeArray().length, 0, "Pass nothing to makeArray and expect an empty array" );
+
+ equals( $.makeArray( 0 )[0], 0 , "Pass makeArray a number" );
+
+ equals( $.makeArray( "foo" )[0], "foo", "Pass makeArray a string" );
+
+ equals( $.makeArray( true )[0].constructor, Boolean, "Pass makeArray a boolean" );
+
+ equals( $.makeArray( document.createElement("div") )[0].nodeName, "DIV", "Pass makeArray a single node" );
+
+ equals( $.makeArray( {length:2, 0:"a", 1:"b"} ).join(""), "ab", "Pass makeArray an array like map (with length)" );
+
+ equals( $.makeArray( document.documentElement.childNodes ).slice(0,1)[0].nodeName, "HEAD", "Pass makeArray a childNodes array" );
+
+ //function, is tricky as it has length
+ equals( $.makeArray( function(){ return 1;} )[0](), 1, "Pass makeArray a function" );
+ //window, also has length
+ equals( $.makeArray(window)[0], window, "Pass makeArray the window" );
+
+ equals( $.makeArray(/a/)[0].constructor, RegExp, "Pass makeArray a regex" );
+
+ ok( $.makeArray(document.getElementById('form')).length >= 13, "Pass makeArray a form (treat as elements)" );
+});
diff --git a/dom/tests/mochitest/ajax/jquery/test/unit/dimensions.js b/dom/tests/mochitest/ajax/jquery/test/unit/dimensions.js new file mode 100644 index 0000000000..aac1655f51 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/unit/dimensions.js @@ -0,0 +1,86 @@ +module("dimensions"); + +test("innerWidth()", function() { + expect(3); + + var $div = $("#nothiddendiv"); + // set styles + $div.css({ + margin: 10, + border: "2px solid #fff", + width: 30 + }); + + equals($div.innerWidth(), 30, "Test with margin and border"); + $div.css("padding", "20px"); + equals($div.innerWidth(), 70, "Test with margin, border and padding"); + $div.hide(); + equals($div.innerWidth(), 70, "Test hidden div"); + + // reset styles + $div.css({ display: "", border: "", padding: "", width: "", height: "" }); +}); + +test("innerHeight()", function() { + expect(3); + + var $div = $("#nothiddendiv"); + // set styles + $div.css({ + margin: 10, + border: "2px solid #fff", + height: 30 + }); + + equals($div.innerHeight(), 30, "Test with margin and border"); + $div.css("padding", "20px"); + equals($div.innerHeight(), 70, "Test with margin, border and padding"); + $div.hide(); + equals($div.innerHeight(), 70, "Test hidden div"); + + // reset styles + $div.css({ display: "", border: "", padding: "", width: "", height: "" }); +}); + +test("outerWidth()", function() { + expect(6); + + var $div = $("#nothiddendiv"); + $div.css("width", 30); + + equals($div.outerWidth(), 30, "Test with only width set"); + $div.css("padding", "20px"); + equals($div.outerWidth(), 70, "Test with padding"); + $div.css("border", "2px solid #fff"); + equals($div.outerWidth(), 74, "Test with padding and border"); + $div.css("margin", "10px"); + equals($div.outerWidth(), 74, "Test with padding, border and margin without margin option"); + $div.css("position", "absolute"); + equals($div.outerWidth(true), 94, "Test with padding, border and margin with margin option"); + $div.hide(); + equals($div.outerWidth(true), 94, "Test hidden div with padding, border and margin with margin option"); + + // reset styles + $div.css({ position: "", display: "", border: "", padding: "", width: "", height: "" }); +}); + +test("outerHeight()", function() { + expect(6); + + var $div = $("#nothiddendiv"); + $div.css("height", 30); + + equals($div.outerHeight(), 30, "Test with only width set"); + $div.css("padding", "20px"); + equals($div.outerHeight(), 70, "Test with padding"); + $div.css("border", "2px solid #fff"); + equals($div.outerHeight(), 74, "Test with padding and border"); + $div.css("margin", "10px"); + equals($div.outerHeight(), 74, "Test with padding, border and margin without margin option"); + equals($div.outerHeight(true), 94, "Test with padding, border and margin with margin option"); + $div.hide(); + equals($div.outerHeight(true), 94, "Test hidden div with padding, border and margin with margin option"); + + // reset styles + $div.css({ display: "", border: "", padding: "", width: "", height: "" }); +});
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/jquery/test/unit/event.js b/dom/tests/mochitest/ajax/jquery/test/unit/event.js new file mode 100644 index 0000000000..c393f6d62a --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/unit/event.js @@ -0,0 +1,348 @@ +module("event"); + +test("bind(), with data", function() { + expect(3); + var handler = function(event) { + ok( event.data, "bind() with data, check passed data exists" ); + equals( event.data.foo, "bar", "bind() with data, Check value of passed data" ); + }; + $("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler); + + ok( !jQuery.data($("#firstp")[0], "events"), "Event handler unbound when using data." ); +}); + +test("bind(), with data, trigger with data", function() { + expect(4); + var handler = function(event, data) { + ok( event.data, "check passed data exists" ); + equals( event.data.foo, "bar", "Check value of passed data" ); + ok( data, "Check trigger data" ); + equals( data.bar, "foo", "Check value of trigger data" ); + }; + $("#firstp").bind("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]).unbind("click", handler); +}); + +test("bind(), multiple events at once", function() { + expect(2); + var clickCounter = 0, + mouseoverCounter = 0; + var handler = function(event) { + if (event.type == "click") + clickCounter += 1; + else if (event.type == "mouseover") + mouseoverCounter += 1; + }; + $("#firstp").bind("click mouseover", handler).trigger("click").trigger("mouseover"); + equals( clickCounter, 1, "bind() with multiple events at once" ); + equals( mouseoverCounter, 1, "bind() with multiple events at once" ); +}); + +test("bind(), no data", function() { + expect(1); + var handler = function(event) { + ok ( !event.data, "Check that no data is added to the event object" ); + }; + $("#firstp").bind("click", handler).trigger("click"); +}); + +test("bind(), iframes", function() { + // events don't work with iframes, see #939 - this test fails in IE because of contentDocument + // var doc = document.getElementById("iframe").contentDocument; + // + // doc.body.innerHTML = "<input type='text'/>"; + // + // var input = doc.getElementsByTagName("input")[0]; + // + // $(input).bind("click",function() { + // ok( true, "Binding to element inside iframe" ); + // }).click(); +}); + +test("bind(), trigger change on select", function() { + expect(3); + var counter = 0; + function selectOnChange(event) { + equals( event.data, counter++, "Event.data is not a global event object" ); + }; + $("#form select").each(function(i){ + $(this).bind('change', i, selectOnChange); + }).trigger('change'); +}); + +test("bind(), namespaced events, cloned events", function() { + expect(6); + + $("#firstp").bind("custom.test",function(e){ + ok(true, "Custom event triggered"); + }); + + $("#firstp").bind("click",function(e){ + ok(true, "Normal click triggered"); + }); + + $("#firstp").bind("click.test",function(e){ + ok(true, "Namespaced click triggered"); + }); + + // Trigger both bound fn (2) + $("#firstp").trigger("click"); + + // Trigger one bound fn (1) + $("#firstp").trigger("click.test"); + + // Remove only the one fn + $("#firstp").unbind("click.test"); + + // Trigger the remaining fn (1) + $("#firstp").trigger("click"); + + // Remove the remaining fn + $("#firstp").unbind(".test"); + + // Trigger the remaining fn (0) + $("#firstp").trigger("custom"); + + // using contents will get comments regular, text, and comment nodes + $("#nonnodes").contents().bind("tester", function () { + equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" ); + }).trigger("tester"); + + // Make sure events stick with appendTo'd elements (which are cloned) #2027 + $("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p"); + ok( $("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" ); +}); + +test("trigger() shortcuts", function() { + expect(6); + $('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() { + var close = $('spanx', this); // same with $(this).find('span'); + equals( close.length, 0, "Context element does not exist, length must be zero" ); + ok( !close[0], "Context element does not exist, direct access to element must return undefined" ); + return false; + }).click(); + + $("#check1").click(function() { + ok( true, "click event handler for checkbox gets fired twice, see #815" ); + }).click(); + + var counter = 0; + $('#firstp')[0].onclick = function(event) { + counter++; + }; + $('#firstp').click(); + equals( counter, 1, "Check that click, triggers onclick event handler also" ); + + var clickCounter = 0; + $('#simon1')[0].onclick = function(event) { + clickCounter++; + }; + $('#simon1').click(); + equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" ); + + $('<img />').load(function(){ + ok( true, "Trigger the load event, using the shortcut .load() (#2819)"); + }).load(); +}); + +test("unbind(event)", function() { + expect(8); + var el = $("#firstp"); + el.click(function() { + ok( true, "Fake normal bind" ); + }); + el.click(function(event) { + el.unbind(event); + ok( true, "Fake onebind" ); + }); + el.click().click(); + + el.click(function() { return; }); + el.unbind('click'); + ok( !el[0].onclick, "Handler is removed" ); // Bug #964 + + el.click(function() { return; }); + el.unbind('change',function(){ return; }); + for (var ret in jQuery.data(el[0], "events")['click']) break; + ok( ret, "Extra handlers weren't accidentally removed." ); + + el.unbind('click'); + ok( !jQuery.data(el[0], "events"), "Removed the events expando after all handlers are unbound." ); + + reset(); + var clickCounter = (mouseoverCounter = 0); + var handler = function(event) { + if (event.type == "click") + clickCounter += 1; + else if (event.type == "mouseover") + mouseoverCounter += 1; + }; + $("#firstp").bind("click mouseover", handler).unbind("click mouseover", handler).trigger("click").trigger("mouseover"); + equals( clickCounter, 0, "unbind() with multiple events at once" ); + equals( mouseoverCounter, 0, "unbind() with multiple events at once" ); +}); + +test("trigger(event, [data], [fn])", function() { + expect(67); + + var handler = function(event, a, b, c) { + equals( event.type, "click", "check passed data" ); + equals( a, 1, "check passed data" ); + equals( b, "2", "check passed data" ); + equals( c, "abc", "check passed data" ); + return "test"; + }; + + var handler2 = function(a, b, c) { + equals( a, 1, "check passed data" ); + equals( b, "2", "check passed data" ); + equals( c, "abc", "check passed data" ); + return false; + }; + + var handler3 = function(a, b, c, v) { + equals( a, 1, "check passed data" ); + equals( b, "2", "check passed data" ); + equals( c, "abc", "check passed data" ); + equals( v, "test", "check current value" ); + return "newVal"; + }; + + var handler4 = function(a, b, c, v) { + equals( a, 1, "check passed data" ); + equals( b, "2", "check passed data" ); + equals( c, "abc", "check passed data" ); + equals( v, "test", "check current value" ); + }; + + // Simulate a "native" click + $("#firstp")[0].click = function(){ + ok( true, "Native call was triggered" ); + }; + + // Triggers handlrs and native + // Trigger 5 + $("#firstp").bind("click", handler).trigger("click", [1, "2", "abc"]); + + // Triggers handlers, native, and extra fn + // Triggers 9 + $("#firstp").trigger("click", [1, "2", "abc"], handler4); + + // Simulate a "native" click + $("#firstp")[0].click = function(){ + ok( false, "Native call was triggered" ); + }; + + // Triggers handlers, native, and extra fn + // Triggers 7 + $("#firstp").trigger("click", [1, "2", "abc"], handler2); + + // Trigger only the handlers (no native) + // Triggers 5 + equals( $("#firstp").triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" ); + + // Trigger only the handlers (no native) and extra fn + // Triggers 8 + equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler2), false, "Verify handler response" ); + + // Build fake click event to pass in + var eventObj = jQuery.event.fix({ type: "foo", target: document.body }); + + // Trigger only the handlers (no native), with external event obj + // Triggers 5 + equals( $("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"]), "test", "Verify handler response" ); + + // Trigger only the handlers (no native) and extra fn, with external event obj + // Triggers 9 + eventObj = jQuery.event.fix({ type: "foo", target: document.body }); + equals( $("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"], handler), "test", "Verify handler response" ); + + var pass = true; + try { + $('input:first') + .hide() + .trigger('focus'); + } catch(e) { + pass = false; + } + ok( pass, "Trigger focus on hidden element" ); + + // have the extra handler override the return + // Triggers 9 + equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler3), "newVal", "Verify triggerHandler return is overwritten by extra function" ); + + // have the extra handler leave the return value alone + // Triggers 9 + equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler4), "test", "Verify triggerHandler return is not overwritten by extra function" ); +}); + +test("toggle(Function, Function, ...)", function() { + expect(11); + + var count = 0, + fn1 = function(e) { count++; }, + fn2 = function(e) { count--; }, + preventDefault = function(e) { e.preventDefault() }, + link = $('#mark'); + link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click(); + equals( count, 1, "Check for toggle(fn, fn)" ); + + $("#firstp").toggle(function () { + equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" ) + }, function() {}).trigger("click", [ 1, 2, 3 ]); + + var first = 0; + $("#simon1").one("click", function() { + ok( true, "Execute event only once" ); + $(this).toggle(function() { + equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" ); + }, function() { + equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" ); + }); + return false; + }).click().click().click(); + + var turn = 0; + var fns = [ + function(){ + turn = 1; + }, + function(){ + turn = 2; + }, + function(){ + turn = 3; + } + ]; + + var $div = $("<div> </div>").toggle( fns[0], fns[1], fns[2] ); + $div.click(); + equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1"); + $div.click(); + equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2"); + $div.click(); + equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3"); + $div.click(); + equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1"); + $div.click(); + equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2"); + + $div.unbind('click',fns[0]); + var data = $.data( $div[0], 'events' ); + ok( !data, "Unbinding one function from toggle unbinds them all"); +}); + +test("jQuery(function($) {})", function() { + stop(); + jQuery(function($) { + equals(jQuery, $, "ready doesn't provide an event object, instead it provides a reference to the jQuery function, see http://docs.jquery.com/Events/ready#fn"); + start(); + }); +}); + +test("event properties", function() { + stop(); + $("#simon1").click(function(event) { + ok( event.timeStamp, "assert event.timeStamp is present" ); + start(); + }).click(); +}); diff --git a/dom/tests/mochitest/ajax/jquery/test/unit/fx.js b/dom/tests/mochitest/ajax/jquery/test/unit/fx.js new file mode 100644 index 0000000000..77a54bf65c --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/unit/fx.js @@ -0,0 +1,435 @@ +module("fx"); + +test("animate(Hash, Object, Function)", function() { + expect(1); + stop(); + var hash = {opacity: 'show'}; + var hashCopy = $.extend({}, hash); + $('#foo').animate(hash, 0, function() { + equals( hash.opacity, hashCopy.opacity, 'Check if animate changed the hash parameter' ); + start(); + }); +}); + +/* Commented out because of bug 450190 +test("animate option (queue === false)", function () { + expect(1); + stop(); + + var order = []; + + var $foo = $("#foo"); + $foo.animate({width:'100px'}, 200, function () { + // should finish after unqueued animation so second + order.push(2); + }); + $foo.animate({fontSize:'2em'}, {queue:false, duration:10, complete:function () { + // short duration and out of queue so should finish first + order.push(1); + }}); + $foo.animate({height:'100px'}, 10, function() { + // queued behind the first animation so should finish third + order.push(3); + isSet( order, [ 1, 2, 3], "Animations finished in the correct order" ); + start(); + }); +}); +*/ + +test("queue() defaults to 'fx' type", function () { + expect(2); + stop(); + + var $foo = $("#foo"); + $foo.queue("fx", [ "sample", "array" ]); + var arr = $foo.queue(); + isSet(arr, [ "sample", "array" ], "queue() got an array set with type 'fx'"); + $foo.queue([ "another", "one" ]); + var arr = $foo.queue("fx"); + isSet(arr, [ "another", "one" ], "queue('fx') got an array set with no type"); + // clean up after test + $foo.queue([]); + + start(); +}); + +test("stop()", function() { + expect(3); + stop(); + + var $foo = $("#nothiddendiv"); + var w = 0; + $foo.hide().width(200).width(); + + $foo.animate({ width:'show' }, 1000); + setTimeout(function(){ + var nw = $foo.width(); + ok( nw != w, "An animation occurred " + nw + "px " + w + "px"); + $foo.stop(); + + nw = $foo.width(); + ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px"); + setTimeout(function(){ + equals( nw, $foo.width(), "The animation didn't continue" ); + start(); + }, 100); + }, 100); +}); + +test("stop() - several in queue", function() { +// Merge from jquery test 1.3.2 + expect(2); + stop(); + + var $foo = $("#nothiddendiv"); + var w = 0; + $foo.hide().width(200).width(); + + $foo.animate({ width:'show' }, 1000); + $foo.animate({ width:'hide' }, 1000); + $foo.animate({ width:'show' }, 1000); + setTimeout(function(){ + // Unreliable. See bug 484994. + // equals( $foo.queue().length, 3, "All 3 still in the queue" ); + var nw = $foo.width(); + ok( nw != w, "An animation occurred " + nw + "px " + w + "px"); + $foo.stop(); + + nw = $foo.width(); + ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px"); + // Merged from 1.3.2, commented out for being flaky in 1.3.2 test suite + //equals( $foo.queue().length, 2, "The next animation continued" ); + $foo.stop(true); + start(); + }, 100); +}); + +test("stop(clearQueue)", function() { + expect(4); + stop(); + + var $foo = $("#nothiddendiv"); + var w = 0; + $foo.hide().width(200).width(); + + $foo.animate({ width:'show' }, 1000); + $foo.animate({ width:'hide' }, 1000); + $foo.animate({ width:'show' }, 1000); + setTimeout(function(){ + var nw = $foo.width(); + ok( nw != w, "An animation occurred " + nw + "px " + w + "px"); + $foo.stop(true); + + nw = $foo.width(); + ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px"); + + equals( $foo.queue().length, 0, "The animation queue was cleared" ); + setTimeout(function(){ + equals( nw, $foo.width(), "The animation didn't continue" ); + start(); + }, 100); + }, 100); +}); + +test("stop(clearQueue, gotoEnd)", function() { + // Merge from 1.3.2 - this test marked as being flaky + expect(1); + stop(); + + var $foo = $("#nothiddendiv"); + var w = 0; + $foo.hide().width(200).width(); + + $foo.animate({ width:'show' }, 1000); + $foo.animate({ width:'hide' }, 1000); + $foo.animate({ width:'show' }, 1000); + $foo.animate({ width:'hide' }, 1000); + setTimeout(function(){ + var nw = $foo.width(); + ok( nw != w, "An animation occurred " + nw + "px " + w + "px"); + $foo.stop(false, true); + + nw = $foo.width(); + // Merge from 1.3.2 - marked as flaky in that release + //equals( nw, 200, "Stop() reset the animation" ); + + setTimeout(function(){ + // Merge from 1.3.2 - marked as flaky in that release + //equals( $foo.queue().length, 3, "The next animation continued" ); + $foo.stop(true); + start(); + }, 100); + }, 100); +}); + +test("toggle()", function() { + expect(3); + var x = $("#foo"); + ok( x.is(":visible"), "is visible" ); + x.toggle(); + ok( x.is(":hidden"), "is hidden" ); + x.toggle(); + ok( x.is(":visible"), "is visible again" ); +}); + +var visible = { + Normal: function(elem){}, + "CSS Hidden": function(elem){ + $(this).addClass("hidden"); + }, + "JS Hidden": function(elem){ + $(this).hide(); + } +}; + +var from = { + "CSS Auto": function(elem,prop){ + $(elem).addClass("auto" + prop) + .text("This is a long string of text."); + return ""; + }, + "JS Auto": function(elem,prop){ + $(elem).css(prop,"auto") + .text("This is a long string of text."); + return ""; + }, + "CSS 100": function(elem,prop){ + $(elem).addClass("large" + prop); + return ""; + }, + "JS 100": function(elem,prop){ + $(elem).css(prop,prop == "opacity" ? 1 : "100px"); + return prop == "opacity" ? 1 : 100; + }, + "CSS 50": function(elem,prop){ + $(elem).addClass("med" + prop); + return ""; + }, + "JS 50": function(elem,prop){ + $(elem).css(prop,prop == "opacity" ? 0.50 : "50px"); + return prop == "opacity" ? 0.5 : 50; + }, + "CSS 0": function(elem,prop){ + $(elem).addClass("no" + prop); + return ""; + }, + "JS 0": function(elem,prop){ + $(elem).css(prop,prop == "opacity" ? 0 : "0px"); + return 0; + } +}; + +var to = { + "show": function(elem,prop){ + $(elem).hide().addClass("wide"+prop); + return "show"; + }, + "hide": function(elem,prop){ + $(elem).addClass("wide"+prop); + return "hide"; + }, + "100": function(elem,prop){ + $(elem).addClass("wide"+prop); + return prop == "opacity" ? 1 : 100; + }, + "50": function(elem,prop){ + return prop == "opacity" ? 0.50 : 50; + }, + "0": function(elem,prop){ + $(elem).addClass("noback"); + return 0; + } +}; + +function checkOverflowDisplay(){ + var o = jQuery.css( this, "overflow" ); + + equals(o, "visible", "Overflow should be visible: " + o); + equals(jQuery.css( this, "display" ), "inline", "Display shouldn't be tampered with."); + + start(); +} + +test("JS Overflow and Display", function() { + expect(2); + stop(); + makeTest( "JS Overflow and Display" ) + .addClass("widewidth") + .css({ overflow: "visible", display: "inline" }) + .addClass("widewidth") + .text("Some sample text.") + .before("text before") + .after("text after") + .animate({ opacity: 0.5 }, "slow", checkOverflowDisplay); +}); + +test("CSS Overflow and Display", function() { + expect(2); + stop(); + makeTest( "CSS Overflow and Display" ) + .addClass("overflow inline") + .addClass("widewidth") + .text("Some sample text.") + .before("text before") + .after("text after") + .animate({ opacity: 0.5 }, "slow", checkOverflowDisplay); +}); + +jQuery.each( from, function(fn, f){ + jQuery.each( to, function(tn, t){ + test(fn + " to " + tn, function() { + var elem = makeTest( fn + " to " + tn ); + + var t_w = t( elem, "width" ); + var f_w = f( elem, "width" ); + var t_h = t( elem, "height" ); + var f_h = f( elem, "height" ); + var t_o = t( elem, "opacity" ); + var f_o = f( elem, "opacity" ); + + var num = 0; + + if ( t_h == "show" ) num++; + if ( t_w == "show" ) num++; + if ( t_w == "hide"||t_w == "show" ) num++; + if ( t_h == "hide"||t_h == "show" ) num++; + if ( t_o == "hide"||t_o == "show" ) num++; + if ( t_w == "hide" ) num++; + if ( t_o.constructor == Number ) num += 2; + if ( t_w.constructor == Number ) num += 2; + if ( t_h.constructor == Number ) num +=2; + + expect(num); + stop(); + + var anim = { width: t_w, height: t_h, opacity: t_o }; + + elem.animate(anim, 50, function(){ + if ( t_w == "show" ) + equals( this.style.display, "block", "Showing, display should block: " + this.style.display); + + if ( t_w == "hide"||t_w == "show" ) + equals(this.style.width.indexOf(f_w), 0, "Width must be reset to " + f_w + ": " + this.style.width); + + if ( t_h == "hide"||t_h == "show" ) + equals(this.style.height.indexOf(f_h), 0, "Height must be reset to " + f_h + ": " + this.style.height); + + var cur_o = jQuery.attr(this.style, "opacity"); + if ( cur_o !== "" ) cur_o = parseFloat( cur_o ); + + if ( t_o == "hide"||t_o == "show" ) + equals(cur_o, f_o, "Opacity must be reset to " + f_o + ": " + cur_o); + + if ( t_w == "hide" ) + equals(this.style.display, "none", "Hiding, display should be none: " + this.style.display); + + if ( t_o.constructor == Number ) { + equals(cur_o, t_o, "Final opacity should be " + t_o + ": " + cur_o); + + ok(jQuery.curCSS(this, "opacity") != "" || cur_o == t_o, "Opacity should be explicitly set to " + t_o + ", is instead: " + cur_o); + } + + if ( t_w.constructor == Number ) { + equals(this.style.width, t_w + "px", "Final width should be " + t_w + ": " + this.style.width); + + var cur_w = jQuery.css(this,"width"); + + ok(this.style.width != "" || cur_w == t_w, "Width should be explicitly set to " + t_w + ", is instead: " + cur_w); + } + + if ( t_h.constructor == Number ) { + equals(this.style.height, t_h + "px", "Final height should be " + t_h + ": " + this.style.height); + + var cur_h = jQuery.css(this,"height"); + + ok(this.style.height != "" || cur_h == t_h, "Height should be explicitly set to " + t_h + ", is instead: " + cur_w); + } + + if ( t_h == "show" ) { + var old_h = jQuery.curCSS(this, "height"); + $(elem).append("<br/>Some more text<br/>and some more..."); + ok(old_h != jQuery.css(this, "height" ), "Make sure height is auto."); + } + + start(); + }); + }); + }); +}); + +var check = ['opacity','height','width','display','overflow']; + +jQuery.fn.saveState = function(){ + expect(check.length); + stop(); + return this.each(function(){ + var self = this; + self.save = {}; + jQuery.each(check, function(i,c){ + self.save[c] = jQuery.css(self,c); + }); + }); +}; + +function checkState(){ + var self = this; + jQuery.each(this.save, function(c,v){ + var cur = jQuery.css(self,c); + equals( v, cur, "Make sure that " + c + " is reset (Old: " + v + " Cur: " + cur + ")"); + }); + start(); +} + +// Chaining Tests +test("Chain fadeOut fadeIn", function() { + $('#fadein div').saveState().fadeOut('fast').fadeIn('fast',checkState); +}); +test("Chain fadeIn fadeOut", function() { + $('#fadeout div').saveState().fadeIn('fast').fadeOut('fast',checkState); +}); + +test("Chain hide show", function() { + $('#show div').saveState().hide('fast').show('fast',checkState); +}); +test("Chain show hide", function() { + $('#hide div').saveState().show('fast').hide('fast',checkState); +}); + +test("Chain toggle in", function() { + $('#togglein div').saveState().toggle('fast').toggle('fast',checkState); +}); +test("Chain toggle out", function() { + $('#toggleout div').saveState().toggle('fast').toggle('fast',checkState); +}); + +test("Chain slideDown slideUp", function() { + $('#slidedown div').saveState().slideDown('fast').slideUp('fast',checkState); +}); +test("Chain slideUp slideDown", function() { + $('#slideup div').saveState().slideUp('fast').slideDown('fast',checkState); +}); + +test("Chain slideToggle in", function() { + $('#slidetogglein div').saveState().slideToggle('fast').slideToggle('fast',checkState); +}); +test("Chain slideToggle out", function() { + $('#slidetoggleout div').saveState().slideToggle('fast').slideToggle('fast',checkState); +}); + +function makeTest( text ){ + var elem = $("<div></div>") + .attr("id", "test" + makeTest.id++) + .addClass("box"); + + $("<h4></h4>") + .text( text ) + .appendTo("#fx-tests") + .click(function(){ + $(this).next().toggle(); + }) + .after( elem ); + + return elem; +} + +makeTest.id = 1; diff --git a/dom/tests/mochitest/ajax/jquery/test/unit/offset.js b/dom/tests/mochitest/ajax/jquery/test/unit/offset.js new file mode 100644 index 0000000000..55c3c436a6 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/unit/offset.js @@ -0,0 +1,164 @@ +module("offset"); + +// opens a new window to run the tests against +var testwin = function(name, fn) { + testwin[name] = load_offset_fixture(name); + var interval = setInterval(function() { + if (testwin[name] && testwin[name].$ && testwin[name].$.isReady) { + clearInterval(interval); + test(name, fn); + } + }, 0); + + function load_offset_fixture(name) { + var win = window.open( "./data/offset/" + name + ".html?num"+parseInt(Math.random()*1000), name, 'left=0,top=0,width=500,height=500,toolbar=1,resizable=0' ); + if ( !win ) { + alert("Please disable your popup blocker for the offset test suite"); + throw "Please disable your popup blocker for the offset test suite"; + } + return win; + } +}; + +testwin("absolute", function() { + var $w = testwin["absolute"].$; + + equals( $w('#absolute-1').offset().top, 1, "$('#absolute-1').offset().top" ); + equals( $w('#absolute-1').offset().left, 1, "$('#absolute-1').offset().left" ); + + equals( $w('#absolute-1-1').offset().top, 5, "$('#absolute-1-1').offset().top" ); + equals( $w('#absolute-1-1').offset().left, 5, "$('#absolute-1-1').offset().left" ); + + equals( $w('#absolute-1-1-1').offset().top, 9, "$('#absolute-1-1-1').offset().top" ); + equals( $w('#absolute-1-1-1').offset().left, 9, "$('#absolute-1-1-1').offset().left" ); + + equals( $w('#absolute-2').offset().top, 20, "$('#absolute-2').offset().top" ); + equals( $w('#absolute-2').offset().left, 20, "$('#absolute-2').offset().left" ); + + + equals( $w('#absolute-1').position().top, 0, "$('#absolute-1').position().top" ); + equals( $w('#absolute-1').position().left, 0, "$('#absolute-1').position().left" ); + + equals( $w('#absolute-1-1').position().top, 1, "$('#absolute-1-1').position().top" ); + equals( $w('#absolute-1-1').position().left, 1, "$('#absolute-1-1').position().left" ); + + equals( $w('#absolute-1-1-1').position().top, 1, "$('#absolute-1-1-1').position().top" ); + equals( $w('#absolute-1-1-1').position().left, 1, "$('#absolute-1-1-1').position().left" ); + + equals( $w('#absolute-2').position().top, 19, "$('#absolute-2').position().top" ); + equals( $w('#absolute-2').position().left, 19, "$('#absolute-2').position().left" ); + + testwin["absolute"].close(); +}); + +testwin("relative", function() { + var $w = testwin["relative"].$; + + // IE is collapsing the top margin of 1px + equals( $w('#relative-1').offset().top, $.browser.msie ? 6 : 7, "$('#relative-1').offset().top" ); + equals( $w('#relative-1').offset().left, 7, "$('#relative-1').offset().left" ); + + // IE is collapsing the top margin of 1px + equals( $w('#relative-1-1').offset().top, $.browser.msie ? 13 : 15, "$('#relative-1-1').offset().top" ); + equals( $w('#relative-1-1').offset().left, 15, "$('#relative-1-1').offset().left" ); + + // IE is collapsing the top margin of 1px + equals( $w('#relative-2').offset().top, $.browser.msie ? 141 : 142, "$('#relative-2').offset().top" ); + equals( $w('#relative-2').offset().left, 27, "$('#relative-2').offset().left" ); + + + // IE is collapsing the top margin of 1px + equals( $w('#relative-1').position().top, $.browser.msie ? 5 : 6, "$('#relative-1').position().top" ); + equals( $w('#relative-1').position().left, 6, "$('#relative-1').position().left" ); + + // IE is collapsing the top margin of 1px + equals( $w('#relative-1-1').position().top, $.browser.msie ? 4 : 5, "$('#relative-1-1').position().top" ); + equals( $w('#relative-1-1').position().left, 5, "$('#relative-1-1').position().left" ); + + // IE is collapsing the top margin of 1px + equals( $w('#relative-2').position().top, $.browser.msie ? 140 : 141, "$('#relative-2').position().top" ); + equals( $w('#relative-2').position().left, 26, "$('#relative-2').position().left" ); + + testwin["relative"].close(); +}); + +testwin("static", function() { + var $w = testwin["static"].$; + + // IE is collapsing the top margin of 1px + equals( $w('#static-1').offset().top, $.browser.msie ? 6 : 7, "$('#static-1').offset().top" ); + equals( $w('#static-1').offset().left, 7, "$('#static-1').offset().left" ); + + // IE is collapsing the top margin of 1px + equals( $w('#static-1-1').offset().top, $.browser.msie ? 13 : 15, "$('#static-1-1').offset().top" ); + equals( $w('#static-1-1').offset().left, 15, "$('#static-1-1').offset().left" ); + + // IE is collapsing the top margin of 1px + equals( $w('#static-1-1-1').offset().top, $.browser.msie ? 20 : 23, "$('#static-1-1-1').offset().top" ); + equals( $w('#static-1-1-1').offset().left, 23, "$('#static-1-1-1').offset().left" ); + + // IE is collapsing the top margin of 1px + equals( $w('#static-2').offset().top, $.browser.msie ? 121 : 122, "$('#static-2').offset().top" ); + equals( $w('#static-2').offset().left, 7, "$('#static-2').offset().left" ); + + + // IE is collapsing the top margin of 1px + equals( $w('#static-1').position().top, $.browser.msie ? 5 : 6, "$('#static-1').position().top" ); + equals( $w('#static-1').position().left, 6, "$('#static-1').position().left" ); + + // IE is collapsing the top margin of 1px + equals( $w('#static-1-1').position().top, $.browser.msie ? 12 : 14, "$('#static-1-1').position().top" ); + equals( $w('#static-1-1').position().left, 14, "$('#static-1-1').position().left" ); + + // IE is collapsing the top margin of 1px + equals( $w('#static-1-1-1').position().top, $.browser.msie ? 19 : 22, "$('#static-1-1-1').position().top" ); + equals( $w('#static-1-1-1').position().left, 22, "$('#static-1-1-1').position().left" ); + + // IE is collapsing the top margin of 1px + equals( $w('#static-2').position().top, $.browser.msie ? 120 : 121, "$('#static-2').position().top" ); + equals( $w('#static-2').position().left, 6, "$('#static-2').position().left" ); + + testwin["static"].close(); +}); + +if ( !$.browser.msie || ($.browser.msie && parseInt($.browser.version) > 6) ) + testwin("fixed", function() { + var $w = testwin["fixed"].$; + + equals( $w('#fixed-1').offset().top, 1001, "$('#fixed-1').offset().top" ); + equals( $w('#fixed-1').offset().left, $.browser.msie ? 994 : 1001, "$('#fixed-1').offset().left" ); + + equals( $w('#fixed-2').offset().top, 1021, "$('#fixed-2').offset().top" ); + equals( $w('#fixed-2').offset().left, $.browser.msie ? 1014 : 1021, "$('#fixed-2').offset().left" ); + + testwin["fixed"].close(); + }); + +testwin("table", function() { + var $w = testwin["table"].$; + + equals( $w('#table-1').offset().top, 6, "$('#table-1').offset().top" ); + equals( $w('#table-1').offset().left, 6, "$('#table-1').offset().left" ); + + equals( $w('#th-1').offset().top, 10, "$('#table-1').offset().top" ); + equals( $w('#th-1').offset().left, 10, "$('#table-1').offset().left" ); + + equals( $w('#th-2').offset().top, 10, "$('#table-1').offset().top" ); + equals( $w('#th-2').offset().left, 116, "$('#table-1').offset().left" ); + + testwin["table"].close(); +}); + +testwin("scroll", function() { + var $w = testwin["scroll"].$; + + // IE is collapsing the top margin of 1px + equals( $w('#scroll-1').offset().top, $.browser.msie ? 6 : 7, "$('#scroll-1').offset().top" ); + equals( $w('#scroll-1').offset().left, 7, "$('#scroll-1').offset().left" ); + + // IE is collapsing the top margin of 1px + equals( $w('#scroll-1-1').offset().top, $.browser.msie ? 9 : 11, "$('#scroll-1-1').offset().top" ); + equals( $w('#scroll-1-1').offset().left, 11, "$('#scroll-1-1').offset().left" ); + + testwin["scroll"].close(); +});
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/jquery/test/unit/selector.js b/dom/tests/mochitest/ajax/jquery/test/unit/selector.js new file mode 100644 index 0000000000..07c4434d09 --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test/unit/selector.js @@ -0,0 +1,224 @@ +module("selector"); + +test("element", function() { + expect(9); + ok( $("*").size() >= 30, "Select all" ); + var all = $("*"), good = true; + for ( var i = 0; i < all.length; i++ ) + if ( all[i].nodeType == 8 ) + good = false; + ok( good, "Select all elements, no comment nodes" ); + t( "Element Selector", "p", ["firstp","ap","sndp","en","sap","first"] ); + t( "Element Selector", "body", ["body"] ); + t( "Element Selector", "html", ["html"] ); + t( "Parent Element", "div p", ["firstp","ap","sndp","en","sap","first"] ); + equals( $("param", "#object1").length, 2, "Object/param as context" ); + + ok( $("#length").length, '<input name="length"> cannot be found under IE, see #945' ); + ok( $("#lengthtest input").length, '<input name="length"> cannot be found under IE, see #945' ); +}); + +if ( location.protocol != "file:" ) { + test("Element Selector with underscore", function() { + expect(1); + stop(); + $.get("data/with_fries.xml", function(xml) { + equals( $("foo_bar", xml).length, 1, "Element Selector with underscore" ); + start(); + }); + }); +} + +test("broken", function() { + expect(7); + t( "Broken Selector", "[", [] ); + t( "Broken Selector", "(", [] ); + t( "Broken Selector", "{", [] ); + t( "Broken Selector", "<", [] ); + t( "Broken Selector", "()", [] ); + t( "Broken Selector", "<>", [] ); + t( "Broken Selector", "{}", [] ); +}); + +test("id", function() { + expect(25); + t( "ID Selector", "#body", ["body"] ); + t( "ID Selector w/ Element", "body#body", ["body"] ); + t( "ID Selector w/ Element", "ul#first", [] ); + t( "ID selector with existing ID descendant", "#firstp #simon1", ["simon1"] ); + t( "ID selector with non-existant descendant", "#firstp #foobar", [] ); + t( "ID selector using UTF8", "#台北Táiběi", ["台北Táiběi"] ); + t( "Multiple ID selectors using UTF8", "#台北Táiběi, #台北", ["台北Táiběi","台北"] ); + t( "Descendant ID selector using UTF8", "div #台北", ["台北"] ); + t( "Child ID selector using UTF8", "form > #台北", ["台北"] ); + + t( "Escaped ID", "#foo\\:bar", ["foo:bar"] ); + t( "Escaped ID", "#test\\.foo\\[5\\]bar", ["test.foo[5]bar"] ); + t( "Descendant escaped ID", "div #foo\\:bar", ["foo:bar"] ); + t( "Descendant escaped ID", "div #test\\.foo\\[5\\]bar", ["test.foo[5]bar"] ); + t( "Child escaped ID", "form > #foo\\:bar", ["foo:bar"] ); + t( "Child escaped ID", "form > #test\\.foo\\[5\\]bar", ["test.foo[5]bar"] ); + + t( "ID Selector, child ID present", "#form > #radio1", ["radio1"] ); // bug #267 + t( "ID Selector, not an ancestor ID", "#form #first", [] ); + t( "ID Selector, not a child ID", "#form > #option1a", [] ); + + t( "All Children of ID", "#foo > *", ["sndp", "en", "sap"] ); + t( "All Children of ID with no children", "#firstUL/*", [] ); + + $('<a name="tName1">tName1 A</a><a name="tName2">tName2 A</a><div id="tName1">tName1 Div</div>').appendTo('#main'); + equals( $("#tName1")[0].id, 'tName1', "ID selector with same value for a name attribute" ); + equals( $("#tName2").length, 0, "ID selector non-existing but name attribute on an A tag" ); + t( "ID Selector on Form with an input that has a name of 'id'", "#lengthtest", ["lengthtest"] ); + + t( "ID selector with non-existant ancestor", "#asdfasdf #foobar", [] ); // bug #986 + + isSet( $("body").find("div#form"), [], "ID selector within the context of another element" ); +}); + +test("class", function() { + expect(16); + t( "Class Selector", ".blog", ["mark","simon"] ); + t( "Class Selector", ".blog.link", ["simon"] ); + t( "Class Selector w/ Element", "a.blog", ["mark","simon"] ); + t( "Parent Class Selector", "p .blog", ["mark","simon"] ); + + t( "Class selector using UTF8", ".台北Táiběi", ["utf8class1"] ); + t( "Class selector using UTF8", ".台北", ["utf8class1","utf8class2"] ); + t( "Class selector using UTF8", ".台北Táiběi.台北", ["utf8class1"] ); + t( "Class selector using UTF8", ".台北Táiběi, .台北", ["utf8class1","utf8class2"] ); + t( "Descendant class selector using UTF8", "div .台北Táiběi", ["utf8class1"] ); + t( "Child class selector using UTF8", "form > .台北Táiběi", ["utf8class1"] ); + + t( "Escaped Class", ".foo\\:bar", ["foo:bar"] ); + t( "Escaped Class", ".test\\.foo\\[5\\]bar", ["test.foo[5]bar"] ); + t( "Descendant scaped Class", "div .foo\\:bar", ["foo:bar"] ); + t( "Descendant scaped Class", "div .test\\.foo\\[5\\]bar", ["test.foo[5]bar"] ); + t( "Child escaped Class", "form > .foo\\:bar", ["foo:bar"] ); + t( "Child escaped Class", "form > .test\\.foo\\[5\\]bar", ["test.foo[5]bar"] ); +}); + +test("multiple", function() { + expect(4); + t( "Comma Support", "a.blog, p", ["mark","simon","firstp","ap","sndp","en","sap","first"] ); + t( "Comma Support", "a.blog , p", ["mark","simon","firstp","ap","sndp","en","sap","first"] ); + t( "Comma Support", "a.blog ,p", ["mark","simon","firstp","ap","sndp","en","sap","first"] ); + t( "Comma Support", "a.blog,p", ["mark","simon","firstp","ap","sndp","en","sap","first"] ); +}); + +test("child and adjacent", function() { + expect(37); + t( "Child", "p > a", ["simon1","google","groups","mark","yahoo","simon"] ); + t( "Child", "p> a", ["simon1","google","groups","mark","yahoo","simon"] ); + t( "Child", "p >a", ["simon1","google","groups","mark","yahoo","simon"] ); + t( "Child", "p>a", ["simon1","google","groups","mark","yahoo","simon"] ); + t( "Child w/ Class", "p > a.blog", ["mark","simon"] ); + t( "All Children", "code > *", ["anchor1","anchor2"] ); + t( "All Grandchildren", "p > * > *", ["anchor1","anchor2"] ); + t( "Adjacent", "a + a", ["groups"] ); + t( "Adjacent", "a +a", ["groups"] ); + t( "Adjacent", "a+ a", ["groups"] ); + t( "Adjacent", "a+a", ["groups"] ); + t( "Adjacent", "p + p", ["ap","en","sap"] ); + t( "Comma, Child, and Adjacent", "a + a, code > a", ["groups","anchor1","anchor2"] ); + + t( "First Child", "p:first-child", ["firstp","sndp"] ); + t( "Nth Child", "p:nth-child(1)", ["firstp","sndp"] ); + + t( "Last Child", "p:last-child", ["sap"] ); + t( "Last Child", "a:last-child", ["simon1","anchor1","mark","yahoo","anchor2","simon"] ); + + t( "Nth-child", "#main form#form > *:nth-child(2)", ["text2"] ); + t( "Nth-child", "#main form#form > :nth-child(2)", ["text2"] ); + + t( "Nth-child", "#form select:first option:nth-child(3)", ["option1c"] ); + t( "Nth-child", "#form select:first option:nth-child(0n+3)", ["option1c"] ); + t( "Nth-child", "#form select:first option:nth-child(1n+0)", ["option1a", "option1b", "option1c", "option1d"] ); + t( "Nth-child", "#form select:first option:nth-child(1n)", ["option1a", "option1b", "option1c", "option1d"] ); + t( "Nth-child", "#form select:first option:nth-child(n)", ["option1a", "option1b", "option1c", "option1d"] ); + t( "Nth-child", "#form select:first option:nth-child(even)", ["option1b", "option1d"] ); + t( "Nth-child", "#form select:first option:nth-child(odd)", ["option1a", "option1c"] ); + t( "Nth-child", "#form select:first option:nth-child(2n)", ["option1b", "option1d"] ); + t( "Nth-child", "#form select:first option:nth-child(2n+1)", ["option1a", "option1c"] ); + t( "Nth-child", "#form select:first option:nth-child(3n)", ["option1c"] ); + t( "Nth-child", "#form select:first option:nth-child(3n+1)", ["option1a", "option1d"] ); + t( "Nth-child", "#form select:first option:nth-child(3n+2)", ["option1b"] ); + t( "Nth-child", "#form select:first option:nth-child(3n+3)", ["option1c"] ); + t( "Nth-child", "#form select:first option:nth-child(3n-1)", ["option1b"] ); + t( "Nth-child", "#form select:first option:nth-child(3n-2)", ["option1a", "option1d"] ); + t( "Nth-child", "#form select:first option:nth-child(3n-3)", ["option1c"] ); + t( "Nth-child", "#form select:first option:nth-child(3n+0)", ["option1c"] ); + t( "Nth-child", "#form select:first option:nth-child(-n+3)", ["option1a", "option1b", "option1c"] ); +}); + +test("attributes", function() { + expect(20); + t( "Attribute Exists", "a[title]", ["google"] ); + t( "Attribute Exists", "*[title]", ["google"] ); + t( "Attribute Exists", "[title]", ["google"] ); + + t( "Attribute Equals", "a[rel='bookmark']", ["simon1"] ); + t( "Attribute Equals", 'a[rel="bookmark"]', ["simon1"] ); + t( "Attribute Equals", "a[rel=bookmark]", ["simon1"] ); + t( "Multiple Attribute Equals", "#form input[type='hidden'],#form input[type='radio']", ["hidden1","radio1","radio2"] ); + t( "Multiple Attribute Equals", "#form input[type=\"hidden\"],#form input[type='radio']", ["hidden1","radio1","radio2"] ); + t( "Multiple Attribute Equals", "#form input[type=hidden],#form input[type=radio]", ["hidden1","radio1","radio2"] ); + + t( "Attribute selector using UTF8", "span[lang=中文]", ["台北"] ); + + t( "Attribute Begins With", "a[href ^= 'http://www']", ["google","yahoo"] ); + t( "Attribute Ends With", "a[href $= 'org/']", ["mark"] ); + t( "Attribute Contains", "a[href *= 'google']", ["google","groups"] ); + + t("Select options via [selected]", "#select1 option[selected]", ["option1a"] ); + t("Select options via [selected]", "#select2 option[selected]", ["option2d"] ); + t("Select options via [selected]", "#select3 option[selected]", ["option3b", "option3c"] ); + + t( "Grouped Form Elements", "input[name='foo[bar]']", ["hidden2"] ); + + t( ":not() Existing attribute", "#form select:not([multiple])", ["select1", "select2"]); + t( ":not() Equals attribute", "#form select:not([name=select1])", ["select2", "select3"]); + t( ":not() Equals quoted attribute", "#form select:not([name='select1'])", ["select2", "select3"]); +}); + +test("pseudo (:) selectors", function() { + expect(35); + t( "First Child", "p:first-child", ["firstp","sndp"] ); + t( "Last Child", "p:last-child", ["sap"] ); + t( "Only Child", "a:only-child", ["simon1","anchor1","yahoo","anchor2"] ); + t( "Empty", "ul:empty", ["firstUL"] ); + t( "Enabled UI Element", "#form input:enabled", ["text1","radio1","radio2","check1","check2","hidden1","hidden2","name"] ); + t( "Disabled UI Element", "#form input:disabled", ["text2"] ); + t( "Checked UI Element", "#form input:checked", ["radio2","check1"] ); + t( "Selected Option Element", "#form option:selected", ["option1a","option2d","option3b","option3c"] ); + t( "Text Contains", "a:contains('Google')", ["google","groups"] ); + t( "Text Contains", "a:contains('Google Groups')", ["groups"] ); + t( "Element Preceded By", "p ~ div", ["foo","fx-queue","fx-tests", "moretests"] ); + t( "Not", "a.blog:not(.link)", ["mark"] ); + t( "Not - multiple", "#form option:not(:contains('Nothing'),#option1b,:selected)", ["option1c", "option1d", "option2b", "option2c", "option3d"] ); + t( "Not - complex", "#form option:not([id^='opt']:gt(0):nth-child(-n+3))", [ "option1a", "option1d", "option2d", "option3d"] ); + t( "Not - recursive", "#form option:not(:not(:selected))[id^='option3']", [ "option3b", "option3c"] ); + + t( "nth Element", "p:nth(1)", ["ap"] ); + t( "First Element", "p:first", ["firstp"] ); + t( "Last Element", "p:last", ["first"] ); + t( "Even Elements", "p:even", ["firstp","sndp","sap"] ); + t( "Odd Elements", "p:odd", ["ap","en","first"] ); + t( "Position Equals", "p:eq(1)", ["ap"] ); + t( "Position Greater Than", "p:gt(0)", ["ap","sndp","en","sap","first"] ); + t( "Position Less Than", "p:lt(3)", ["firstp","ap","sndp"] ); + t( "Is A Parent", "p:parent", ["firstp","ap","sndp","en","sap","first"] ); + t( "Is Visible", "#form input:visible", ["text1","text2","radio1","radio2","check1","check2","name"] ); + t( "Is Hidden", "#form input:hidden", ["hidden1","hidden2"] ); + + t( "Form element :input", "#form :input", ["text1", "text2", "radio1", "radio2", "check1", "check2", "hidden1", "hidden2", "name", "button", "area1", "select1", "select2", "select3"] ); + t( "Form element :radio", "#form :radio", ["radio1", "radio2"] ); + t( "Form element :checkbox", "#form :checkbox", ["check1", "check2"] ); + t( "Form element :text", "#form :text", ["text1", "text2", "hidden2", "name"] ); + t( "Form element :radio:checked", "#form :radio:checked", ["radio2"] ); + t( "Form element :checkbox:checked", "#form :checkbox:checked", ["check1"] ); + t( "Form element :checkbox:checked, :radio:checked", "#form :checkbox:checked, #form :radio:checked", ["check1", "radio2"] ); + + t( "Headers", ":header", ["header", "banner", "userAgent"] ); + t( "Has Children - :has()", "p:has(a)", ["firstp","ap","en","sap"] ); +}); diff --git a/dom/tests/mochitest/ajax/jquery/test_jQuery.html b/dom/tests/mochitest/ajax/jquery/test_jQuery.html new file mode 100644 index 0000000000..1a02d3523c --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/test_jQuery.html @@ -0,0 +1,24 @@ +<!DOCTYPE HTML> +<html> +<head> + <title>Test for jQuery</title> + <script src="/MochiKit/Base.js"></script> + <script src="/MochiKit/Async.js"></script> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript"> + // On Linux 64 Stylo Disabled, we observed a failure in the + // test 'stop() - several in queue' in fx.js with a low precision value. + // We specify a value that seems safe. The root cause of this issue is + // believed to be jQuery's use of new Date as a mechanism to advance animations. + SpecialPowers.pushPrefEnv({set: + [["privacy.reduceTimerPrecision", true], + ["privacy.resistFingerprinting.reduceTimerPrecision.microseconds", 2000]]}); + </script> + <script type="text/javascript" src="../lib/AJAX_setup.js"></script> + <link rel="stylesheet" type="text/css" href="../lib/test.css" /> +</head> +<body> + <iframe width="100%" height="500" id="testframe" src=""></iframe> +</body> +</html> + diff --git a/dom/tests/mochitest/ajax/jquery/version.txt b/dom/tests/mochitest/ajax/jquery/version.txt new file mode 100644 index 0000000000..3c43790f5d --- /dev/null +++ b/dom/tests/mochitest/ajax/jquery/version.txt @@ -0,0 +1 @@ +1.2.6 diff --git a/dom/tests/mochitest/ajax/lib/AJAX_setup.js b/dom/tests/mochitest/ajax/lib/AJAX_setup.js new file mode 100644 index 0000000000..e94531db06 --- /dev/null +++ b/dom/tests/mochitest/ajax/lib/AJAX_setup.js @@ -0,0 +1,35 @@ +var AJAXtests = []; + +function runAJAXTest() { + if (AJAXtests.length == 0) { + SimpleTest.finish(); + return; + } + + var test = AJAXtests.shift(); + var testframe = document.getElementById("testframe"); + testframe.src = test; +} + +function onManifestLoad(manifest) { + if (manifest.testcases) { + AJAXtests = manifest.testcases; + runAJAXTest(); + } else { + ok(false, "manifest check", "no manifest!?!"); + SimpleTest.finish(); + } +} + +function fetchManifest() { + var d = loadJSONDoc("manifest.json"); + d.addBoth(onManifestLoad); +} + +// Double timeout duration. Since this test case takes longer than 300 seconds +// on B2G emulator. +// See bug 968783. +SimpleTest.requestLongerTimeout(2); + +SimpleTest.waitForExplicitFinish(); +addLoadEvent(fetchManifest); diff --git a/dom/tests/mochitest/ajax/lib/test.css b/dom/tests/mochitest/ajax/lib/test.css new file mode 100644 index 0000000000..38a401402f --- /dev/null +++ b/dom/tests/mochitest/ajax/lib/test.css @@ -0,0 +1,28 @@ +.test_ok { + color: green; + display: none; +} +.test_not_ok { + color: red; + display: block; +} + +.test_ok, .test_not_ok { + border-bottom-width: 2px; + border-bottom-style: solid; + border-bottom-color: black; +} + +.all_pass { + background-color: lime; +} + +.some_fail { + background-color: red; +} + +.tests_report { + border-width: 2px; + border-style: solid; + width: 20em; +} diff --git a/dom/tests/mochitest/ajax/mochikit/MochiKit/Async.js b/dom/tests/mochitest/ajax/mochikit/MochiKit/Async.js new file mode 100644 index 0000000000..55f4857b97 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/Async.js @@ -0,0 +1,699 @@ +/*** + +MochiKit.Async 1.4 + +See <http://mochikit.com/> for documentation, downloads, license, etc. + +(c) 2005 Bob Ippolito. All rights Reserved. + +***/ + +if (typeof(dojo) != 'undefined') { + dojo.provide("MochiKit.Async"); + dojo.require("MochiKit.Base"); +} +if (typeof(JSAN) != 'undefined') { + JSAN.use("MochiKit.Base", []); +} + +try { + if (typeof(MochiKit.Base) == 'undefined') { + throw ""; + } +} catch (e) { + throw "MochiKit.Async depends on MochiKit.Base!"; +} + +if (typeof(MochiKit.Async) == 'undefined') { + MochiKit.Async = {}; +} + +MochiKit.Async.NAME = "MochiKit.Async"; +MochiKit.Async.VERSION = "1.4"; +MochiKit.Async.__repr__ = function () { + return "[" + this.NAME + " " + this.VERSION + "]"; +}; +MochiKit.Async.toString = function () { + return this.__repr__(); +}; + +/** @id MochiKit.Async.Deferred */ +MochiKit.Async.Deferred = function (/* optional */ canceller) { + this.chain = []; + this.id = this._nextId(); + this.fired = -1; + this.paused = 0; + this.results = [null, null]; + this.canceller = canceller; + this.silentlyCancelled = false; + this.chained = false; +}; + +MochiKit.Async.Deferred.prototype = { + /** @id MochiKit.Async.Deferred.prototype.repr */ + repr: function () { + var state; + if (this.fired == -1) { + state = 'unfired'; + } else if (this.fired === 0) { + state = 'success'; + } else { + state = 'error'; + } + return 'Deferred(' + this.id + ', ' + state + ')'; + }, + + toString: MochiKit.Base.forwardCall("repr"), + + _nextId: MochiKit.Base.counter(), + + /** @id MochiKit.Async.Deferred.prototype.cancel */ + cancel: function () { + var self = MochiKit.Async; + if (this.fired == -1) { + if (this.canceller) { + this.canceller(this); + } else { + this.silentlyCancelled = true; + } + if (this.fired == -1) { + this.errback(new self.CancelledError(this)); + } + } else if ((this.fired === 0) && (this.results[0] instanceof self.Deferred)) { + this.results[0].cancel(); + } + }, + + _resback: function (res) { + /*** + + The primitive that means either callback or errback + + ***/ + this.fired = ((res instanceof Error) ? 1 : 0); + this.results[this.fired] = res; + this._fire(); + }, + + _check: function () { + if (this.fired != -1) { + if (!this.silentlyCancelled) { + throw new MochiKit.Async.AlreadyCalledError(this); + } + this.silentlyCancelled = false; + return; + } + }, + + /** @id MochiKit.Async.Deferred.prototype.callback */ + callback: function (res) { + this._check(); + if (res instanceof MochiKit.Async.Deferred) { + throw new Error("Deferred instances can only be chained if they are the result of a callback"); + } + this._resback(res); + }, + + /** @id MochiKit.Async.Deferred.prototype.errback */ + errback: function (res) { + this._check(); + var self = MochiKit.Async; + if (res instanceof self.Deferred) { + throw new Error("Deferred instances can only be chained if they are the result of a callback"); + } + if (!(res instanceof Error)) { + res = new self.GenericError(res); + } + this._resback(res); + }, + + /** @id MochiKit.Async.Deferred.prototype.addBoth */ + addBoth: function (fn) { + if (arguments.length > 1) { + fn = MochiKit.Base.partial.apply(null, arguments); + } + return this.addCallbacks(fn, fn); + }, + + /** @id MochiKit.Async.Deferred.prototype.addCallback */ + addCallback: function (fn) { + if (arguments.length > 1) { + fn = MochiKit.Base.partial.apply(null, arguments); + } + return this.addCallbacks(fn, null); + }, + + /** @id MochiKit.Async.Deferred.prototype.addErrback */ + addErrback: function (fn) { + if (arguments.length > 1) { + fn = MochiKit.Base.partial.apply(null, arguments); + } + return this.addCallbacks(null, fn); + }, + + /** @id MochiKit.Async.Deferred.prototype.addCallbacks */ + addCallbacks: function (cb, eb) { + if (this.chained) { + throw new Error("Chained Deferreds can not be re-used"); + } + this.chain.push([cb, eb]); + if (this.fired >= 0) { + this._fire(); + } + return this; + }, + + _fire: function () { + /*** + + Used internally to exhaust the callback sequence when a result + is available. + + ***/ + var chain = this.chain; + var fired = this.fired; + var res = this.results[fired]; + var self = this; + var cb = null; + while (chain.length > 0 && this.paused === 0) { + // Array + var pair = chain.shift(); + var f = pair[fired]; + if (f === null) { + continue; + } + try { + res = f(res); + fired = ((res instanceof Error) ? 1 : 0); + if (res instanceof MochiKit.Async.Deferred) { + cb = function (res) { + self._resback(res); + self.paused--; + if ((self.paused === 0) && (self.fired >= 0)) { + self._fire(); + } + }; + this.paused++; + } + } catch (err) { + fired = 1; + if (!(err instanceof Error)) { + err = new MochiKit.Async.GenericError(err); + } + res = err; + } + } + this.fired = fired; + this.results[fired] = res; + if (cb && this.paused) { + // this is for "tail recursion" in case the dependent deferred + // is already fired + res.addBoth(cb); + res.chained = true; + } + } +}; + +MochiKit.Base.update(MochiKit.Async, { + /** @id MochiKit.Async.evalJSONRequest */ + evalJSONRequest: function (req) { + return MochiKit.Base.evalJSON(req.responseText); + }, + + /** @id MochiKit.Async.succeed */ + succeed: function (/* optional */result) { + var d = new MochiKit.Async.Deferred(); + d.callback.apply(d, arguments); + return d; + }, + + /** @id MochiKit.Async.fail */ + fail: function (/* optional */result) { + var d = new MochiKit.Async.Deferred(); + d.errback.apply(d, arguments); + return d; + }, + + /** @id MochiKit.Async.getXMLHttpRequest */ + getXMLHttpRequest: function () { + var self = arguments.callee; + if (!self.XMLHttpRequest) { + var tryThese = [ + function () { return new XMLHttpRequest(); }, + function () { return new ActiveXObject('Msxml2.XMLHTTP'); }, + function () { return new ActiveXObject('Microsoft.XMLHTTP'); }, + function () { return new ActiveXObject('Msxml2.XMLHTTP.4.0'); }, + function () { + throw new MochiKit.Async.BrowserComplianceError("Browser does not support XMLHttpRequest"); + } + ]; + for (var i = 0; i < tryThese.length; i++) { + var func = tryThese[i]; + try { + self.XMLHttpRequest = func; + return func(); + } catch (e) { + // pass + } + } + } + return self.XMLHttpRequest(); + }, + + _xhr_onreadystatechange: function (d) { + // MochiKit.Logging.logDebug('this.readyState', this.readyState); + var m = MochiKit.Base; + if (this.readyState == 4) { + // IE SUCKS + try { + this.onreadystatechange = null; + } catch (e) { + try { + this.onreadystatechange = m.noop; + } catch (e) { + } + } + var status = null; + try { + status = this.status; + if (!status && m.isNotEmpty(this.responseText)) { + // 0 or undefined seems to mean cached or local + status = 304; + } + } catch (e) { + // pass + // MochiKit.Logging.logDebug('error getting status?', repr(items(e))); + } + // 200 is OK, 201 is CREATED, 204 is NO CONTENT + // 304 is NOT MODIFIED, 1223 is apparently a bug in IE + if (status == 200 || status == 201 || status == 204 || + status == 304 || status == 1223) { + d.callback(this); + } else { + var err = new MochiKit.Async.XMLHttpRequestError(this, "Request failed"); + if (err.number) { + // XXX: This seems to happen on page change + d.errback(err); + } else { + // XXX: this seems to happen when the server is unreachable + d.errback(err); + } + } + } + }, + + _xhr_canceller: function (req) { + // IE SUCKS + try { + req.onreadystatechange = null; + } catch (e) { + try { + req.onreadystatechange = MochiKit.Base.noop; + } catch (e) { + } + } + req.abort(); + }, + + + /** @id MochiKit.Async.sendXMLHttpRequest */ + sendXMLHttpRequest: function (req, /* optional */ sendContent) { + if (typeof(sendContent) == "undefined" || sendContent === null) { + sendContent = ""; + } + + var m = MochiKit.Base; + var self = MochiKit.Async; + var d = new self.Deferred(m.partial(self._xhr_canceller, req)); + + try { + req.onreadystatechange = m.bind(self._xhr_onreadystatechange, + req, d); + req.send(sendContent); + } catch (e) { + try { + req.onreadystatechange = null; + } catch (ignore) { + // pass + } + d.errback(e); + } + + return d; + + }, + + /** @id MochiKit.Async.doXHR */ + doXHR: function (url, opts) { + /* + Work around a Firefox bug by dealing with XHR during + the next event loop iteration. Maybe it's this one: + https://bugzilla.mozilla.org/show_bug.cgi?id=249843 + */ + var self = MochiKit.Async; + return self.callLater(0, self._doXHR, url, opts); + }, + + _doXHR: function (url, opts) { + var m = MochiKit.Base; + opts = m.update({ + method: 'GET', + sendContent: '' + /* + queryString: undefined, + username: undefined, + password: undefined, + headers: undefined, + mimeType: undefined + */ + }, opts); + var self = MochiKit.Async; + var req = self.getXMLHttpRequest(); + if (opts.queryString) { + var qs = m.queryString(opts.queryString); + if (qs) { + url += "?" + qs; + } + } + // Safari will send undefined:undefined, so we have to check. + // We can't use apply, since the function is native. + if ('username' in opts) { + req.open(opts.method, url, true, opts.username, opts.password); + } else { + req.open(opts.method, url, true); + } + if (req.overrideMimeType && opts.mimeType) { + req.overrideMimeType(opts.mimeType); + } + if (opts.headers) { + var headers = opts.headers; + if (!m.isArrayLike(headers)) { + headers = m.items(headers); + } + for (var i = 0; i < headers.length; i++) { + var header = headers[i]; + var name = header[0]; + var value = header[1]; + req.setRequestHeader(name, value); + } + } + return self.sendXMLHttpRequest(req, opts.sendContent); + }, + + _buildURL: function (url/*, ...*/) { + if (arguments.length > 1) { + var m = MochiKit.Base; + var qs = m.queryString.apply(null, m.extend(null, arguments, 1)); + if (qs) { + return url + "?" + qs; + } + } + return url; + }, + + /** @id MochiKit.Async.doSimpleXMLHttpRequest */ + doSimpleXMLHttpRequest: function (url/*, ...*/) { + var self = MochiKit.Async; + url = self._buildURL.apply(self, arguments); + return self.doXHR(url); + }, + + /** @id MochiKit.Async.loadJSONDoc */ + loadJSONDoc: function (url/*, ...*/) { + var self = MochiKit.Async; + url = self._buildURL.apply(self, arguments); + var d = self.doXHR(url, { + 'mimeType': 'text/plain', + 'headers': [['Accept', 'application/json']] + }); + d = d.addCallback(self.evalJSONRequest); + return d; + }, + + /** @id MochiKit.Async.wait */ + wait: function (seconds, /* optional */value) { + var d = new MochiKit.Async.Deferred(); + var m = MochiKit.Base; + if (typeof(value) != 'undefined') { + d.addCallback(function () { return value; }); + } + var timeout = setTimeout( + m.bind("callback", d), + Math.floor(seconds * 1000)); + d.canceller = function () { + try { + clearTimeout(timeout); + } catch (e) { + // pass + } + }; + return d; + }, + + /** @id MochiKit.Async.callLater */ + callLater: function (seconds, func) { + var m = MochiKit.Base; + var pfunc = m.partial.apply(m, m.extend(null, arguments, 1)); + return MochiKit.Async.wait(seconds).addCallback( + function (res) { return pfunc(); } + ); + } +}); + + +/** @id MochiKit.Async.DeferredLock */ +MochiKit.Async.DeferredLock = function () { + this.waiting = []; + this.locked = false; + this.id = this._nextId(); +}; + +MochiKit.Async.DeferredLock.prototype = { + __class__: MochiKit.Async.DeferredLock, + /** @id MochiKit.Async.DeferredLock.prototype.acquire */ + acquire: function () { + var d = new MochiKit.Async.Deferred(); + if (this.locked) { + this.waiting.push(d); + } else { + this.locked = true; + d.callback(this); + } + return d; + }, + /** @id MochiKit.Async.DeferredLock.prototype.release */ + release: function () { + if (!this.locked) { + throw TypeError("Tried to release an unlocked DeferredLock"); + } + this.locked = false; + if (this.waiting.length > 0) { + this.locked = true; + this.waiting.shift().callback(this); + } + }, + _nextId: MochiKit.Base.counter(), + repr: function () { + var state; + if (this.locked) { + state = 'locked, ' + this.waiting.length + ' waiting'; + } else { + state = 'unlocked'; + } + return 'DeferredLock(' + this.id + ', ' + state + ')'; + }, + toString: MochiKit.Base.forwardCall("repr") + +}; + +/** @id MochiKit.Async.DeferredList */ +MochiKit.Async.DeferredList = function (list, /* optional */fireOnOneCallback, fireOnOneErrback, consumeErrors, canceller) { + + // call parent constructor + MochiKit.Async.Deferred.apply(this, [canceller]); + + this.list = list; + var resultList = []; + this.resultList = resultList; + + this.finishedCount = 0; + this.fireOnOneCallback = fireOnOneCallback; + this.fireOnOneErrback = fireOnOneErrback; + this.consumeErrors = consumeErrors; + + var cb = MochiKit.Base.bind(this._cbDeferred, this); + for (var i = 0; i < list.length; i++) { + var d = list[i]; + resultList.push(undefined); + d.addCallback(cb, i, true); + d.addErrback(cb, i, false); + } + + if (list.length === 0 && !fireOnOneCallback) { + this.callback(this.resultList); + } + +}; + +MochiKit.Async.DeferredList.prototype = new MochiKit.Async.Deferred(); + +MochiKit.Async.DeferredList.prototype._cbDeferred = function (index, succeeded, result) { + this.resultList[index] = [succeeded, result]; + this.finishedCount += 1; + if (this.fired == -1) { + if (succeeded && this.fireOnOneCallback) { + this.callback([index, result]); + } else if (!succeeded && this.fireOnOneErrback) { + this.errback(result); + } else if (this.finishedCount == this.list.length) { + this.callback(this.resultList); + } + } + if (!succeeded && this.consumeErrors) { + result = null; + } + return result; +}; + +/** @id MochiKit.Async.gatherResults */ +MochiKit.Async.gatherResults = function (deferredList) { + var d = new MochiKit.Async.DeferredList(deferredList, false, true, false); + d.addCallback(function (results) { + var ret = []; + for (var i = 0; i < results.length; i++) { + ret.push(results[i][1]); + } + return ret; + }); + return d; +}; + +/** @id MochiKit.Async.maybeDeferred */ +MochiKit.Async.maybeDeferred = function (func) { + var self = MochiKit.Async; + var result; + try { + var r = func.apply(null, MochiKit.Base.extend([], arguments, 1)); + if (r instanceof self.Deferred) { + result = r; + } else if (r instanceof Error) { + result = self.fail(r); + } else { + result = self.succeed(r); + } + } catch (e) { + result = self.fail(e); + } + return result; +}; + + +MochiKit.Async.EXPORT = [ + "AlreadyCalledError", + "CancelledError", + "BrowserComplianceError", + "GenericError", + "XMLHttpRequestError", + "Deferred", + "succeed", + "fail", + "getXMLHttpRequest", + "doSimpleXMLHttpRequest", + "loadJSONDoc", + "wait", + "callLater", + "sendXMLHttpRequest", + "DeferredLock", + "DeferredList", + "gatherResults", + "maybeDeferred", + "doXHR" +]; + +MochiKit.Async.EXPORT_OK = [ + "evalJSONRequest" +]; + +MochiKit.Async.__new__ = function () { + var m = MochiKit.Base; + var ne = m.partial(m._newNamedError, this); + + ne("AlreadyCalledError", + /** @id MochiKit.Async.AlreadyCalledError */ + function (deferred) { + /*** + + Raised by the Deferred if callback or errback happens + after it was already fired. + + ***/ + this.deferred = deferred; + } + ); + + ne("CancelledError", + /** @id MochiKit.Async.CancelledError */ + function (deferred) { + /*** + + Raised by the Deferred cancellation mechanism. + + ***/ + this.deferred = deferred; + } + ); + + ne("BrowserComplianceError", + /** @id MochiKit.Async.BrowserComplianceError */ + function (msg) { + /*** + + Raised when the JavaScript runtime is not capable of performing + the given function. Technically, this should really never be + raised because a non-conforming JavaScript runtime probably + isn't going to support exceptions in the first place. + + ***/ + this.message = msg; + } + ); + + ne("GenericError", + /** @id MochiKit.Async.GenericError */ + function (msg) { + this.message = msg; + } + ); + + ne("XMLHttpRequestError", + /** @id MochiKit.Async.XMLHttpRequestError */ + function (req, msg) { + /*** + + Raised when an XMLHttpRequest does not complete for any reason. + + ***/ + this.req = req; + this.message = msg; + try { + // Strange but true that this can raise in some cases. + this.number = req.status; + } catch (e) { + // pass + } + } + ); + + + this.EXPORT_TAGS = { + ":common": this.EXPORT, + ":all": m.concat(this.EXPORT, this.EXPORT_OK) + }; + + m.nameFunctions(this); + +}; + +MochiKit.Async.__new__(); + +MochiKit.Base._exportSymbols(this, MochiKit.Async); diff --git a/dom/tests/mochitest/ajax/mochikit/MochiKit/Base.js b/dom/tests/mochitest/ajax/mochikit/MochiKit/Base.js new file mode 100644 index 0000000000..ec1350e60d --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/Base.js @@ -0,0 +1,1412 @@ +/*** + +MochiKit.Base 1.4 + +See <http://mochikit.com/> for documentation, downloads, license, etc. + +(c) 2005 Bob Ippolito. All rights Reserved. + +***/ + +if (typeof(dojo) != 'undefined') { + dojo.provide("MochiKit.Base"); +} +if (typeof(MochiKit) == 'undefined') { + MochiKit = {}; +} +if (typeof(MochiKit.Base) == 'undefined') { + MochiKit.Base = {}; +} +if (typeof(MochiKit.__export__) == "undefined") { + MochiKit.__export__ = (MochiKit.__compat__ || + (typeof(JSAN) == 'undefined' && typeof(dojo) == 'undefined') + ); +} + +MochiKit.Base.VERSION = "1.4"; +MochiKit.Base.NAME = "MochiKit.Base"; +/** @id MochiKit.Base.update */ +MochiKit.Base.update = function (self, obj/*, ... */) { + if (self === null) { + self = {}; + } + for (var i = 1; i < arguments.length; i++) { + var o = arguments[i]; + if (typeof(o) != 'undefined' && o !== null) { + for (var k in o) { + self[k] = o[k]; + } + } + } + return self; +}; + +MochiKit.Base.update(MochiKit.Base, { + __repr__: function () { + return "[" + this.NAME + " " + this.VERSION + "]"; + }, + + toString: function () { + return this.__repr__(); + }, + + /** @id MochiKit.Base.camelize */ + camelize: function (selector) { + /* from dojo.style.toCamelCase */ + var arr = selector.split('-'); + var cc = arr[0]; + for (var i = 1; i < arr.length; i++) { + cc += arr[i].charAt(0).toUpperCase() + arr[i].substring(1); + } + return cc; + }, + + /** @id MochiKit.Base.counter */ + counter: function (n/* = 1 */) { + if (arguments.length === 0) { + n = 1; + } + return function () { + return n++; + }; + }, + + /** @id MochiKit.Base.clone */ + clone: function (obj) { + var me = arguments.callee; + if (arguments.length == 1) { + me.prototype = obj; + return new me(); + } + }, + + _flattenArray: function (res, lst) { + for (var i = 0; i < lst.length; i++) { + var o = lst[i]; + if (o instanceof Array) { + arguments.callee(res, o); + } else { + res.push(o); + } + } + return res; + }, + + /** @id MochiKit.Base.flattenArray */ + flattenArray: function (lst) { + return MochiKit.Base._flattenArray([], lst); + }, + + /** @id MochiKit.Base.flattenArguments */ + flattenArguments: function (lst/* ...*/) { + var res = []; + var m = MochiKit.Base; + var args = m.extend(null, arguments); + while (args.length) { + var o = args.shift(); + if (o && typeof(o) == "object" && typeof(o.length) == "number") { + for (var i = o.length - 1; i >= 0; i--) { + args.unshift(o[i]); + } + } else { + res.push(o); + } + } + return res; + }, + + /** @id MochiKit.Base.extend */ + extend: function (self, obj, /* optional */skip) { + // Extend an array with an array-like object starting + // from the skip index + if (!skip) { + skip = 0; + } + if (obj) { + // allow iterable fall-through, but skip the full isArrayLike + // check for speed, this is called often. + var l = obj.length; + if (typeof(l) != 'number' /* !isArrayLike(obj) */) { + if (typeof(MochiKit.Iter) != "undefined") { + obj = MochiKit.Iter.list(obj); + l = obj.length; + } else { + throw new TypeError("Argument not an array-like and MochiKit.Iter not present"); + } + } + if (!self) { + self = []; + } + for (var i = skip; i < l; i++) { + self.push(obj[i]); + } + } + // This mutates, but it's convenient to return because + // it's often used like a constructor when turning some + // ghetto array-like to a real array + return self; + }, + + + /** @id MochiKit.Base.updatetree */ + updatetree: function (self, obj/*, ...*/) { + if (self === null) { + self = {}; + } + for (var i = 1; i < arguments.length; i++) { + var o = arguments[i]; + if (typeof(o) != 'undefined' && o !== null) { + for (var k in o) { + var v = o[k]; + if (typeof(self[k]) == 'object' && typeof(v) == 'object') { + arguments.callee(self[k], v); + } else { + self[k] = v; + } + } + } + } + return self; + }, + + /** @id MochiKit.Base.setdefault */ + setdefault: function (self, obj/*, ...*/) { + if (self === null) { + self = {}; + } + for (var i = 1; i < arguments.length; i++) { + var o = arguments[i]; + for (var k in o) { + if (!(k in self)) { + self[k] = o[k]; + } + } + } + return self; + }, + + /** @id MochiKit.Base.keys */ + keys: function (obj) { + var rval = []; + for (var prop in obj) { + rval.push(prop); + } + return rval; + }, + + /** @id MochiKit.Base.values */ + values: function (obj) { + var rval = []; + for (var prop in obj) { + rval.push(obj[prop]); + } + return rval; + }, + + /** @id MochiKit.Base.items */ + items: function (obj) { + var rval = []; + var e; + for (var prop in obj) { + var v; + try { + v = obj[prop]; + } catch (e) { + continue; + } + rval.push([prop, v]); + } + return rval; + }, + + + _newNamedError: function (module, name, func) { + func.prototype = new MochiKit.Base.NamedError(module.NAME + "." + name); + module[name] = func; + }, + + + /** @id MochiKit.Base.operator */ + operator: { + // unary logic operators + /** @id MochiKit.Base.truth */ + truth: function (a) { return !!a; }, + /** @id MochiKit.Base.lognot */ + lognot: function (a) { return !a; }, + /** @id MochiKit.Base.identity */ + identity: function (a) { return a; }, + + // bitwise unary operators + /** @id MochiKit.Base.not */ + not: function (a) { return ~a; }, + /** @id MochiKit.Base.neg */ + neg: function (a) { return -a; }, + + // binary operators + /** @id MochiKit.Base.add */ + add: function (a, b) { return a + b; }, + /** @id MochiKit.Base.sub */ + sub: function (a, b) { return a - b; }, + /** @id MochiKit.Base.div */ + div: function (a, b) { return a / b; }, + /** @id MochiKit.Base.mod */ + mod: function (a, b) { return a % b; }, + /** @id MochiKit.Base.mul */ + mul: function (a, b) { return a * b; }, + + // bitwise binary operators + /** @id MochiKit.Base.and */ + and: function (a, b) { return a & b; }, + /** @id MochiKit.Base.or */ + or: function (a, b) { return a | b; }, + /** @id MochiKit.Base.xor */ + xor: function (a, b) { return a ^ b; }, + /** @id MochiKit.Base.lshift */ + lshift: function (a, b) { return a << b; }, + /** @id MochiKit.Base.rshift */ + rshift: function (a, b) { return a >> b; }, + /** @id MochiKit.Base.zrshift */ + zrshift: function (a, b) { return a >>> b; }, + + // near-worthless built-in comparators + /** @id MochiKit.Base.eq */ + eq: function (a, b) { return a == b; }, + /** @id MochiKit.Base.ne */ + ne: function (a, b) { return a != b; }, + /** @id MochiKit.Base.gt */ + gt: function (a, b) { return a > b; }, + /** @id MochiKit.Base.ge */ + ge: function (a, b) { return a >= b; }, + /** @id MochiKit.Base.lt */ + lt: function (a, b) { return a < b; }, + /** @id MochiKit.Base.le */ + le: function (a, b) { return a <= b; }, + + // strict built-in comparators + seq: function (a, b) { return a === b; }, + sne: function (a, b) { return a !== b; }, + + // compare comparators + /** @id MochiKit.Base.ceq */ + ceq: function (a, b) { return MochiKit.Base.compare(a, b) === 0; }, + /** @id MochiKit.Base.cne */ + cne: function (a, b) { return MochiKit.Base.compare(a, b) !== 0; }, + /** @id MochiKit.Base.cgt */ + cgt: function (a, b) { return MochiKit.Base.compare(a, b) == 1; }, + /** @id MochiKit.Base.cge */ + cge: function (a, b) { return MochiKit.Base.compare(a, b) != -1; }, + /** @id MochiKit.Base.clt */ + clt: function (a, b) { return MochiKit.Base.compare(a, b) == -1; }, + /** @id MochiKit.Base.cle */ + cle: function (a, b) { return MochiKit.Base.compare(a, b) != 1; }, + + // binary logical operators + /** @id MochiKit.Base.logand */ + logand: function (a, b) { return a && b; }, + /** @id MochiKit.Base.logor */ + logor: function (a, b) { return a || b; }, + /** @id MochiKit.Base.contains */ + contains: function (a, b) { return b in a; } + }, + + /** @id MochiKit.Base.forwardCall */ + forwardCall: function (func) { + return function () { + return this[func].apply(this, arguments); + }; + }, + + /** @id MochiKit.Base.itemgetter */ + itemgetter: function (func) { + return function (arg) { + return arg[func]; + }; + }, + + /** @id MochiKit.Base.typeMatcher */ + typeMatcher: function (/* typ */) { + var types = {}; + for (var i = 0; i < arguments.length; i++) { + var typ = arguments[i]; + types[typ] = typ; + } + return function () { + for (var i = 0; i < arguments.length; i++) { + if (!(typeof(arguments[i]) in types)) { + return false; + } + } + return true; + }; + }, + + /** @id MochiKit.Base.isNull */ + isNull: function (/* ... */) { + for (var i = 0; i < arguments.length; i++) { + if (arguments[i] !== null) { + return false; + } + } + return true; + }, + + /** @id MochiKit.Base.isUndefinedOrNull */ + isUndefinedOrNull: function (/* ... */) { + for (var i = 0; i < arguments.length; i++) { + var o = arguments[i]; + if (!(typeof(o) == 'undefined' || o === null)) { + return false; + } + } + return true; + }, + + /** @id MochiKit.Base.isEmpty */ + isEmpty: function (obj) { + return !MochiKit.Base.isNotEmpty.apply(this, arguments); + }, + + /** @id MochiKit.Base.isNotEmpty */ + isNotEmpty: function (obj) { + for (var i = 0; i < arguments.length; i++) { + var o = arguments[i]; + if (!(o && o.length)) { + return false; + } + } + return true; + }, + + /** @id MochiKit.Base.isArrayLike */ + isArrayLike: function () { + for (var i = 0; i < arguments.length; i++) { + var o = arguments[i]; + var typ = typeof(o); + if ( + (typ != 'object' && !(typ == 'function' && typeof(o.item) == 'function')) || + o === null || + typeof(o.length) != 'number' || + o.nodeType === 3 + ) { + return false; + } + } + return true; + }, + + /** @id MochiKit.Base.isDateLike */ + isDateLike: function () { + for (var i = 0; i < arguments.length; i++) { + var o = arguments[i]; + if (typeof(o) != "object" || o === null + || typeof(o.getTime) != 'function') { + return false; + } + } + return true; + }, + + + /** @id MochiKit.Base.xmap */ + xmap: function (fn/*, obj... */) { + if (fn === null) { + return MochiKit.Base.extend(null, arguments, 1); + } + var rval = []; + for (var i = 1; i < arguments.length; i++) { + rval.push(fn(arguments[i])); + } + return rval; + }, + + /** @id MochiKit.Base.map */ + map: function (fn, lst/*, lst... */) { + var m = MochiKit.Base; + var itr = MochiKit.Iter; + var isArrayLike = m.isArrayLike; + if (arguments.length <= 2) { + // allow an iterable to be passed + if (!isArrayLike(lst)) { + if (itr) { + // fast path for map(null, iterable) + lst = itr.list(lst); + if (fn === null) { + return lst; + } + } else { + throw new TypeError("Argument not an array-like and MochiKit.Iter not present"); + } + } + // fast path for map(null, lst) + if (fn === null) { + return m.extend(null, lst); + } + // disabled fast path for map(fn, lst) + /* + if (false && typeof(Array.prototype.map) == 'function') { + // Mozilla fast-path + return Array.prototype.map.call(lst, fn); + } + */ + var rval = []; + for (var i = 0; i < lst.length; i++) { + rval.push(fn(lst[i])); + } + return rval; + } else { + // default for map(null, ...) is zip(...) + if (fn === null) { + fn = Array; + } + var length = null; + for (i = 1; i < arguments.length; i++) { + // allow iterables to be passed + if (!isArrayLike(arguments[i])) { + if (itr) { + return itr.list(itr.imap.apply(null, arguments)); + } else { + throw new TypeError("Argument not an array-like and MochiKit.Iter not present"); + } + } + // find the minimum length + var l = arguments[i].length; + if (length === null || length > l) { + length = l; + } + } + rval = []; + for (i = 0; i < length; i++) { + var args = []; + for (var j = 1; j < arguments.length; j++) { + args.push(arguments[j][i]); + } + rval.push(fn.apply(this, args)); + } + return rval; + } + }, + + /** @id MochiKit.Base.xfilter */ + xfilter: function (fn/*, obj... */) { + var rval = []; + if (fn === null) { + fn = MochiKit.Base.operator.truth; + } + for (var i = 1; i < arguments.length; i++) { + var o = arguments[i]; + if (fn(o)) { + rval.push(o); + } + } + return rval; + }, + + /** @id MochiKit.Base.filter */ + filter: function (fn, lst, self) { + var rval = []; + // allow an iterable to be passed + var m = MochiKit.Base; + if (!m.isArrayLike(lst)) { + if (MochiKit.Iter) { + lst = MochiKit.Iter.list(lst); + } else { + throw new TypeError("Argument not an array-like and MochiKit.Iter not present"); + } + } + if (fn === null) { + fn = m.operator.truth; + } + if (typeof(Array.prototype.filter) == 'function') { + // Mozilla fast-path + return Array.prototype.filter.call(lst, fn, self); + } else if (typeof(self) == 'undefined' || self === null) { + for (var i = 0; i < lst.length; i++) { + var o = lst[i]; + if (fn(o)) { + rval.push(o); + } + } + } else { + for (i = 0; i < lst.length; i++) { + o = lst[i]; + if (fn.call(self, o)) { + rval.push(o); + } + } + } + return rval; + }, + + + _wrapDumbFunction: function (func) { + return function () { + // fast path! + switch (arguments.length) { + case 0: return func(); + case 1: return func(arguments[0]); + case 2: return func(arguments[0], arguments[1]); + case 3: return func(arguments[0], arguments[1], arguments[2]); + } + var args = []; + for (var i = 0; i < arguments.length; i++) { + args.push("arguments[" + i + "]"); + } + return eval("(func(" + args.join(",") + "))"); + }; + }, + + /** @id MochiKit.Base.methodcaller */ + methodcaller: function (func/*, args... */) { + var args = MochiKit.Base.extend(null, arguments, 1); + if (typeof(func) == "function") { + return function (obj) { + return func.apply(obj, args); + }; + } else { + return function (obj) { + return obj[func].apply(obj, args); + }; + } + }, + + /** @id MochiKit.Base.method */ + method: function (self, func) { + var m = MochiKit.Base; + return m.bind.apply(this, m.extend([func, self], arguments, 2)); + }, + + /** @id MochiKit.Base.compose */ + compose: function (f1, f2/*, f3, ... fN */) { + var fnlist = []; + var m = MochiKit.Base; + if (arguments.length === 0) { + throw new TypeError("compose() requires at least one argument"); + } + for (var i = 0; i < arguments.length; i++) { + var fn = arguments[i]; + if (typeof(fn) != "function") { + throw new TypeError(m.repr(fn) + " is not a function"); + } + fnlist.push(fn); + } + return function () { + var args = arguments; + for (var i = fnlist.length - 1; i >= 0; i--) { + args = [fnlist[i].apply(this, args)]; + } + return args[0]; + }; + }, + + /** @id MochiKit.Base.bind */ + bind: function (func, self/* args... */) { + if (typeof(func) == "string") { + func = self[func]; + } + var im_func = func.im_func; + var im_preargs = func.im_preargs; + var im_self = func.im_self; + var m = MochiKit.Base; + if (typeof(func) == "function" && typeof(func.apply) == "undefined") { + // this is for cases where JavaScript sucks ass and gives you a + // really dumb built-in function like alert() that doesn't have + // an apply + func = m._wrapDumbFunction(func); + } + if (typeof(im_func) != 'function') { + im_func = func; + } + if (typeof(self) != 'undefined') { + im_self = self; + } + if (typeof(im_preargs) == 'undefined') { + im_preargs = []; + } else { + im_preargs = im_preargs.slice(); + } + m.extend(im_preargs, arguments, 2); + var newfunc = function () { + var args = arguments; + var me = arguments.callee; + if (me.im_preargs.length > 0) { + args = m.concat(me.im_preargs, args); + } + var self = me.im_self; + if (!self) { + self = this; + } + return me.im_func.apply(self, args); + }; + newfunc.im_self = im_self; + newfunc.im_func = im_func; + newfunc.im_preargs = im_preargs; + return newfunc; + }, + + /** @id MochiKit.Base.bindMethods */ + bindMethods: function (self) { + var bind = MochiKit.Base.bind; + for (var k in self) { + var func = self[k]; + if (typeof(func) == 'function') { + self[k] = bind(func, self); + } + } + }, + + /** @id MochiKit.Base.registerComparator */ + registerComparator: function (name, check, comparator, /* optional */ override) { + MochiKit.Base.comparatorRegistry.register(name, check, comparator, override); + }, + + _primitives: {'boolean': true, 'string': true, 'number': true}, + + /** @id MochiKit.Base.compare */ + compare: function (a, b) { + if (a == b) { + return 0; + } + var aIsNull = (typeof(a) == 'undefined' || a === null); + var bIsNull = (typeof(b) == 'undefined' || b === null); + if (aIsNull && bIsNull) { + return 0; + } else if (aIsNull) { + return -1; + } else if (bIsNull) { + return 1; + } + var m = MochiKit.Base; + // bool, number, string have meaningful comparisons + var prim = m._primitives; + if (!(typeof(a) in prim && typeof(b) in prim)) { + try { + return m.comparatorRegistry.match(a, b); + } catch (e) { + if (e != m.NotFound) { + throw e; + } + } + } + if (a < b) { + return -1; + } else if (a > b) { + return 1; + } + // These types can't be compared + var repr = m.repr; + throw new TypeError(repr(a) + " and " + repr(b) + " can not be compared"); + }, + + /** @id MochiKit.Base.compareDateLike */ + compareDateLike: function (a, b) { + return MochiKit.Base.compare(a.getTime(), b.getTime()); + }, + + /** @id MochiKit.Base.compareArrayLike */ + compareArrayLike: function (a, b) { + var compare = MochiKit.Base.compare; + var count = a.length; + var rval = 0; + if (count > b.length) { + rval = 1; + count = b.length; + } else if (count < b.length) { + rval = -1; + } + for (var i = 0; i < count; i++) { + var cmp = compare(a[i], b[i]); + if (cmp) { + return cmp; + } + } + return rval; + }, + + /** @id MochiKit.Base.registerRepr */ + registerRepr: function (name, check, wrap, /* optional */override) { + MochiKit.Base.reprRegistry.register(name, check, wrap, override); + }, + + /** @id MochiKit.Base.repr */ + repr: function (o) { + if (typeof(o) == "undefined") { + return "undefined"; + } else if (o === null) { + return "null"; + } + try { + if (typeof(o.__repr__) == 'function') { + return o.__repr__(); + } else if (typeof(o.repr) == 'function' && o.repr != arguments.callee) { + return o.repr(); + } + return MochiKit.Base.reprRegistry.match(o); + } catch (e) { + if (typeof(o.NAME) == 'string' && ( + o.toString == Function.prototype.toString || + o.toString == Object.prototype.toString + )) { + return o.NAME; + } + } + try { + var ostring = (o + ""); + } catch (e) { + return "[" + typeof(o) + "]"; + } + if (typeof(o) == "function") { + o = ostring.replace(/^\s+/, ""); + var idx = o.indexOf("{"); + if (idx != -1) { + o = o.substr(0, idx) + "{...}"; + } + } + return ostring; + }, + + /** @id MochiKit.Base.reprArrayLike */ + reprArrayLike: function (o) { + var m = MochiKit.Base; + return "[" + m.map(m.repr, o).join(", ") + "]"; + }, + + /** @id MochiKit.Base.reprString */ + reprString: function (o) { + return ('"' + o.replace(/(["\\])/g, '\\$1') + '"' + ).replace(/[\f]/g, "\\f" + ).replace(/[\b]/g, "\\b" + ).replace(/[\n]/g, "\\n" + ).replace(/[\t]/g, "\\t" + ).replace(/[\r]/g, "\\r"); + }, + + /** @id MochiKit.Base.reprNumber */ + reprNumber: function (o) { + return o + ""; + }, + + /** @id MochiKit.Base.registerJSON */ + registerJSON: function (name, check, wrap, /* optional */override) { + MochiKit.Base.jsonRegistry.register(name, check, wrap, override); + }, + + + /** @id MochiKit.Base.evalJSON */ + evalJSON: function () { + return eval("(" + MochiKit.Base._filterJSON(arguments[0]) + ")"); + }, + + _filterJSON: function (s) { + var m = s.match(/^\s*\/\*(.*)\*\/\s*$/); + if (m) { + return m[1]; + } + return s; + }, + + /** @id MochiKit.Base.serializeJSON */ + serializeJSON: function (o) { + var objtype = typeof(o); + if (objtype == "number" || objtype == "boolean") { + return o + ""; + } else if (o === null) { + return "null"; + } + var m = MochiKit.Base; + var reprString = m.reprString; + if (objtype == "string") { + return reprString(o); + } + // recurse + var me = arguments.callee; + // short-circuit for objects that support "json" serialization + // if they return "self" then just pass-through... + var newObj; + if (typeof(o.__json__) == "function") { + newObj = o.__json__(); + if (o !== newObj) { + return me(newObj); + } + } + if (typeof(o.json) == "function") { + newObj = o.json(); + if (o !== newObj) { + return me(newObj); + } + } + // array + if (objtype != "function" && typeof(o.length) == "number") { + var res = []; + for (var i = 0; i < o.length; i++) { + var val = me(o[i]); + if (typeof(val) != "string") { + val = "undefined"; + } + res.push(val); + } + return "[" + res.join(", ") + "]"; + } + // look in the registry + try { + newObj = m.jsonRegistry.match(o); + if (o !== newObj) { + return me(newObj); + } + } catch (e) { + if (e != m.NotFound) { + // something really bad happened + throw e; + } + } + // undefined is outside of the spec + if (objtype == "undefined") { + throw new TypeError("undefined can not be serialized as JSON"); + } + // it's a function with no adapter, bad + if (objtype == "function") { + return null; + } + // generic object code path + res = []; + for (var k in o) { + var useKey; + if (typeof(k) == "number") { + useKey = '"' + k + '"'; + } else if (typeof(k) == "string") { + useKey = reprString(k); + } else { + // skip non-string or number keys + continue; + } + val = me(o[k]); + if (typeof(val) != "string") { + // skip non-serializable values + continue; + } + res.push(useKey + ":" + val); + } + return "{" + res.join(", ") + "}"; + }, + + + /** @id MochiKit.Base.objEqual */ + objEqual: function (a, b) { + return (MochiKit.Base.compare(a, b) === 0); + }, + + /** @id MochiKit.Base.arrayEqual */ + arrayEqual: function (self, arr) { + if (self.length != arr.length) { + return false; + } + return (MochiKit.Base.compare(self, arr) === 0); + }, + + /** @id MochiKit.Base.concat */ + concat: function (/* lst... */) { + var rval = []; + var extend = MochiKit.Base.extend; + for (var i = 0; i < arguments.length; i++) { + extend(rval, arguments[i]); + } + return rval; + }, + + /** @id MochiKit.Base.keyComparator */ + keyComparator: function (key/* ... */) { + // fast-path for single key comparisons + var m = MochiKit.Base; + var compare = m.compare; + if (arguments.length == 1) { + return function (a, b) { + return compare(a[key], b[key]); + }; + } + var compareKeys = m.extend(null, arguments); + return function (a, b) { + var rval = 0; + // keep comparing until something is inequal or we run out of + // keys to compare + for (var i = 0; (rval === 0) && (i < compareKeys.length); i++) { + var key = compareKeys[i]; + rval = compare(a[key], b[key]); + } + return rval; + }; + }, + + /** @id MochiKit.Base.reverseKeyComparator */ + reverseKeyComparator: function (key) { + var comparator = MochiKit.Base.keyComparator.apply(this, arguments); + return function (a, b) { + return comparator(b, a); + }; + }, + + /** @id MochiKit.Base.partial */ + partial: function (func) { + var m = MochiKit.Base; + return m.bind.apply(this, m.extend([func, undefined], arguments, 1)); + }, + + /** @id MochiKit.Base.listMinMax */ + listMinMax: function (which, lst) { + if (lst.length === 0) { + return null; + } + var cur = lst[0]; + var compare = MochiKit.Base.compare; + for (var i = 1; i < lst.length; i++) { + var o = lst[i]; + if (compare(o, cur) == which) { + cur = o; + } + } + return cur; + }, + + /** @id MochiKit.Base.objMax */ + objMax: function (/* obj... */) { + return MochiKit.Base.listMinMax(1, arguments); + }, + + /** @id MochiKit.Base.objMin */ + objMin: function (/* obj... */) { + return MochiKit.Base.listMinMax(-1, arguments); + }, + + /** @id MochiKit.Base.findIdentical */ + findIdentical: function (lst, value, start/* = 0 */, /* optional */end) { + if (typeof(end) == "undefined" || end === null) { + end = lst.length; + } + if (typeof(start) == "undefined" || start === null) { + start = 0; + } + for (var i = start; i < end; i++) { + if (lst[i] === value) { + return i; + } + } + return -1; + }, + + /** @id MochiKit.Base.mean */ + mean: function(/* lst... */) { + /* http://www.nist.gov/dads/HTML/mean.html */ + var sum = 0; + + var m = MochiKit.Base; + var args = m.extend(null, arguments); + var count = args.length; + + while (args.length) { + var o = args.shift(); + if (o && typeof(o) == "object" && typeof(o.length) == "number") { + count += o.length - 1; + for (var i = o.length - 1; i >= 0; i--) { + sum += o[i]; + } + } else { + sum += o; + } + } + + if (count <= 0) { + throw new TypeError('mean() requires at least one argument'); + } + + return sum/count; + }, + + /** @id MochiKit.Base.median */ + median: function(/* lst... */) { + /* http://www.nist.gov/dads/HTML/median.html */ + var data = MochiKit.Base.flattenArguments(arguments); + if (data.length === 0) { + throw new TypeError('median() requires at least one argument'); + } + data.sort(compare); + if (data.length % 2 == 0) { + var upper = data.length / 2; + return (data[upper] + data[upper - 1]) / 2; + } else { + return data[(data.length - 1) / 2]; + } + }, + + /** @id MochiKit.Base.findValue */ + findValue: function (lst, value, start/* = 0 */, /* optional */end) { + if (typeof(end) == "undefined" || end === null) { + end = lst.length; + } + if (typeof(start) == "undefined" || start === null) { + start = 0; + } + var cmp = MochiKit.Base.compare; + for (var i = start; i < end; i++) { + if (cmp(lst[i], value) === 0) { + return i; + } + } + return -1; + }, + + /** @id MochiKit.Base.nodeWalk */ + nodeWalk: function (node, visitor) { + var nodes = [node]; + var extend = MochiKit.Base.extend; + while (nodes.length) { + var res = visitor(nodes.shift()); + if (res) { + extend(nodes, res); + } + } + }, + + + /** @id MochiKit.Base.nameFunctions */ + nameFunctions: function (namespace) { + var base = namespace.NAME; + if (typeof(base) == 'undefined') { + base = ''; + } else { + base = base + '.'; + } + for (var name in namespace) { + var o = namespace[name]; + if (typeof(o) == 'function' && typeof(o.NAME) == 'undefined') { + try { + o.NAME = base + name; + } catch (e) { + // pass + } + } + } + }, + + + /** @id MochiKit.Base.queryString */ + queryString: function (names, values) { + // check to see if names is a string or a DOM element, and if + // MochiKit.DOM is available. If so, drop it like it's a form + // Ugliest conditional in MochiKit? Probably! + if (typeof(MochiKit.DOM) != "undefined" && arguments.length == 1 + && (typeof(names) == "string" || ( + typeof(names.nodeType) != "undefined" && names.nodeType > 0 + )) + ) { + var kv = MochiKit.DOM.formContents(names); + names = kv[0]; + values = kv[1]; + } else if (arguments.length == 1) { + // Allow the return value of formContents to be passed directly + if (typeof(names.length) == "number" && names.length == 2) { + return arguments.callee(names[0], names[1]); + } + var o = names; + names = []; + values = []; + for (var k in o) { + var v = o[k]; + if (typeof(v) == "function") { + continue; + } else if (typeof(v) != "string" && + typeof(v.length) == "number") { + for (var i = 0; i < v.length; i++) { + names.push(k); + values.push(v[i]); + } + } else { + names.push(k); + values.push(v); + } + } + } + var rval = []; + var len = Math.min(names.length, values.length); + var urlEncode = MochiKit.Base.urlEncode; + for (var i = 0; i < len; i++) { + v = values[i]; + if (typeof(v) != 'undefined' && v !== null) { + rval.push(urlEncode(names[i]) + "=" + urlEncode(v)); + } + } + return rval.join("&"); + }, + + + /** @id MochiKit.Base.parseQueryString */ + parseQueryString: function (encodedString, useArrays) { + // strip a leading '?' from the encoded string + var qstr = (encodedString.charAt(0) == "?") + ? encodedString.substring(1) + : encodedString; + var pairs = qstr.replace(/\+/g, "%20").split(/(\&\;|\&\#38\;|\&|\&)/); + var o = {}; + var decode; + if (typeof(decodeURIComponent) != "undefined") { + decode = decodeURIComponent; + } else { + decode = unescape; + } + if (useArrays) { + for (var i = 0; i < pairs.length; i++) { + var pair = pairs[i].split("="); + var name = decode(pair.shift()); + if (!name) { + continue; + } + var arr = o[name]; + if (!(arr instanceof Array)) { + arr = []; + o[name] = arr; + } + arr.push(decode(pair.join("="))); + } + } else { + for (i = 0; i < pairs.length; i++) { + pair = pairs[i].split("="); + var name = pair.shift(); + if (!name) { + continue; + } + o[decode(name)] = decode(pair.join("=")); + } + } + return o; + } +}); + +/** @id MochiKit.Base.AdapterRegistry */ +MochiKit.Base.AdapterRegistry = function () { + this.pairs = []; +}; + +MochiKit.Base.AdapterRegistry.prototype = { + /** @id MochiKit.Base.AdapterRegistry.prototype.register */ + register: function (name, check, wrap, /* optional */ override) { + if (override) { + this.pairs.unshift([name, check, wrap]); + } else { + this.pairs.push([name, check, wrap]); + } + }, + + /** @id MochiKit.Base.AdapterRegistry.prototype.match */ + match: function (/* ... */) { + for (var i = 0; i < this.pairs.length; i++) { + var pair = this.pairs[i]; + if (pair[1].apply(this, arguments)) { + return pair[2].apply(this, arguments); + } + } + throw MochiKit.Base.NotFound; + }, + + /** @id MochiKit.Base.AdapterRegistry.prototype.unregister */ + unregister: function (name) { + for (var i = 0; i < this.pairs.length; i++) { + var pair = this.pairs[i]; + if (pair[0] == name) { + this.pairs.splice(i, 1); + return true; + } + } + return false; + } +}; + + +MochiKit.Base.EXPORT = [ + "flattenArray", + "noop", + "camelize", + "counter", + "clone", + "extend", + "update", + "updatetree", + "setdefault", + "keys", + "values", + "items", + "NamedError", + "operator", + "forwardCall", + "itemgetter", + "typeMatcher", + "isCallable", + "isUndefined", + "isUndefinedOrNull", + "isNull", + "isEmpty", + "isNotEmpty", + "isArrayLike", + "isDateLike", + "xmap", + "map", + "xfilter", + "filter", + "methodcaller", + "compose", + "bind", + "bindMethods", + "NotFound", + "AdapterRegistry", + "registerComparator", + "compare", + "registerRepr", + "repr", + "objEqual", + "arrayEqual", + "concat", + "keyComparator", + "reverseKeyComparator", + "partial", + "merge", + "listMinMax", + "listMax", + "listMin", + "objMax", + "objMin", + "nodeWalk", + "zip", + "urlEncode", + "queryString", + "serializeJSON", + "registerJSON", + "evalJSON", + "parseQueryString", + "findValue", + "findIdentical", + "flattenArguments", + "method", + "average", + "mean", + "median" +]; + +MochiKit.Base.EXPORT_OK = [ + "nameFunctions", + "comparatorRegistry", + "reprRegistry", + "jsonRegistry", + "compareDateLike", + "compareArrayLike", + "reprArrayLike", + "reprString", + "reprNumber" +]; + +MochiKit.Base._exportSymbols = function (globals, module) { + if (!MochiKit.__export__) { + return; + } + var all = module.EXPORT_TAGS[":all"]; + for (var i = 0; i < all.length; i++) { + globals[all[i]] = module[all[i]]; + } +}; + +MochiKit.Base.__new__ = function () { + // A singleton raised when no suitable adapter is found + var m = this; + + // convenience + /** @id MochiKit.Base.noop */ + m.noop = m.operator.identity; + + // Backwards compat + m.forward = m.forwardCall; + m.find = m.findValue; + + if (typeof(encodeURIComponent) != "undefined") { + /** @id MochiKit.Base.urlEncode */ + m.urlEncode = function (unencoded) { + return encodeURIComponent(unencoded).replace(/\'/g, '%27'); + }; + } else { + m.urlEncode = function (unencoded) { + return escape(unencoded + ).replace(/\+/g, '%2B' + ).replace(/\"/g,'%22' + ).rval.replace(/\'/g, '%27'); + }; + } + + /** @id MochiKit.Base.NamedError */ + m.NamedError = function (name) { + this.message = name; + this.name = name; + }; + m.NamedError.prototype = new Error(); + m.update(m.NamedError.prototype, { + repr: function () { + if (this.message && this.message != this.name) { + return this.name + "(" + m.repr(this.message) + ")"; + } else { + return this.name + "()"; + } + }, + toString: m.forwardCall("repr") + }); + + /** @id MochiKit.Base.NotFound */ + m.NotFound = new m.NamedError("MochiKit.Base.NotFound"); + + + /** @id MochiKit.Base.listMax */ + m.listMax = m.partial(m.listMinMax, 1); + /** @id MochiKit.Base.listMin */ + m.listMin = m.partial(m.listMinMax, -1); + + /** @id MochiKit.Base.isCallable */ + m.isCallable = m.typeMatcher('function'); + /** @id MochiKit.Base.isUndefined */ + m.isUndefined = m.typeMatcher('undefined'); + + /** @id MochiKit.Base.merge */ + m.merge = m.partial(m.update, null); + /** @id MochiKit.Base.zip */ + m.zip = m.partial(m.map, null); + + /** @id MochiKit.Base.average */ + m.average = m.mean; + + /** @id MochiKit.Base.comparatorRegistry */ + m.comparatorRegistry = new m.AdapterRegistry(); + m.registerComparator("dateLike", m.isDateLike, m.compareDateLike); + m.registerComparator("arrayLike", m.isArrayLike, m.compareArrayLike); + + /** @id MochiKit.Base.reprRegistry */ + m.reprRegistry = new m.AdapterRegistry(); + m.registerRepr("arrayLike", m.isArrayLike, m.reprArrayLike); + m.registerRepr("string", m.typeMatcher("string"), m.reprString); + m.registerRepr("numbers", m.typeMatcher("number", "boolean"), m.reprNumber); + + /** @id MochiKit.Base.jsonRegistry */ + m.jsonRegistry = new m.AdapterRegistry(); + + var all = m.concat(m.EXPORT, m.EXPORT_OK); + m.EXPORT_TAGS = { + ":common": m.concat(m.EXPORT_OK), + ":all": all + }; + + m.nameFunctions(this); + +}; + +MochiKit.Base.__new__(); + +// +// XXX: Internet Explorer blows +// +if (MochiKit.__export__) { + compare = MochiKit.Base.compare; + compose = MochiKit.Base.compose; + serializeJSON = MochiKit.Base.serializeJSON; +} + +MochiKit.Base._exportSymbols(this, MochiKit.Base); diff --git a/dom/tests/mochitest/ajax/mochikit/MochiKit/Color.js b/dom/tests/mochitest/ajax/mochikit/MochiKit/Color.js new file mode 100644 index 0000000000..708f49017f --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/Color.js @@ -0,0 +1,902 @@ +/*** + +MochiKit.Color 1.4 + +See <http://mochikit.com/> for documentation, downloads, license, etc. + +(c) 2005 Bob Ippolito and others. All rights Reserved. + +***/ + +if (typeof(dojo) != 'undefined') { + dojo.provide('MochiKit.Color'); + dojo.require('MochiKit.Base'); + dojo.require('MochiKit.DOM'); + dojo.require('MochiKit.Style'); +} + +if (typeof(JSAN) != 'undefined') { + JSAN.use("MochiKit.Base", []); + JSAN.use("MochiKit.DOM", []); + JSAN.use("MochiKit.Style", []); +} + +try { + if (typeof(MochiKit.Base) == 'undefined') { + throw ""; + } +} catch (e) { + throw "MochiKit.Color depends on MochiKit.Base"; +} + +try { + if (typeof(MochiKit.DOM) == 'undefined') { + throw ""; + } +} catch (e) { + throw "MochiKit.Color depends on MochiKit.DOM"; +} + +try { + if (typeof(MochiKit.Style) == 'undefined') { + throw ""; + } +} catch (e) { + throw "MochiKit.Color depends on MochiKit.Style"; +} + +if (typeof(MochiKit.Color) == "undefined") { + MochiKit.Color = {}; +} + +MochiKit.Color.NAME = "MochiKit.Color"; +MochiKit.Color.VERSION = "1.4"; + +MochiKit.Color.__repr__ = function () { + return "[" + this.NAME + " " + this.VERSION + "]"; +}; + +MochiKit.Color.toString = function () { + return this.__repr__(); +}; + + +/** @id MochiKit.Color.Color */ +MochiKit.Color.Color = function (red, green, blue, alpha) { + if (typeof(alpha) == 'undefined' || alpha === null) { + alpha = 1.0; + } + this.rgb = { + r: red, + g: green, + b: blue, + a: alpha + }; +}; + + +// Prototype methods + +MochiKit.Color.Color.prototype = { + + __class__: MochiKit.Color.Color, + + /** @id MochiKit.Color.Color.prototype.colorWithAlpha */ + colorWithAlpha: function (alpha) { + var rgb = this.rgb; + var m = MochiKit.Color; + return m.Color.fromRGB(rgb.r, rgb.g, rgb.b, alpha); + }, + + /** @id MochiKit.Color.Color.prototype.colorWithHue */ + colorWithHue: function (hue) { + // get an HSL model, and set the new hue... + var hsl = this.asHSL(); + hsl.h = hue; + var m = MochiKit.Color; + // convert back to RGB... + return m.Color.fromHSL(hsl); + }, + + /** @id MochiKit.Color.Color.prototype.colorWithSaturation */ + colorWithSaturation: function (saturation) { + // get an HSL model, and set the new hue... + var hsl = this.asHSL(); + hsl.s = saturation; + var m = MochiKit.Color; + // convert back to RGB... + return m.Color.fromHSL(hsl); + }, + + /** @id MochiKit.Color.Color.prototype.colorWithLightness */ + colorWithLightness: function (lightness) { + // get an HSL model, and set the new hue... + var hsl = this.asHSL(); + hsl.l = lightness; + var m = MochiKit.Color; + // convert back to RGB... + return m.Color.fromHSL(hsl); + }, + + /** @id MochiKit.Color.Color.prototype.darkerColorWithLevel */ + darkerColorWithLevel: function (level) { + var hsl = this.asHSL(); + hsl.l = Math.max(hsl.l - level, 0); + var m = MochiKit.Color; + return m.Color.fromHSL(hsl); + }, + + /** @id MochiKit.Color.Color.prototype.lighterColorWithLevel */ + lighterColorWithLevel: function (level) { + var hsl = this.asHSL(); + hsl.l = Math.min(hsl.l + level, 1); + var m = MochiKit.Color; + return m.Color.fromHSL(hsl); + }, + + /** @id MochiKit.Color.Color.prototype.blendedColor */ + blendedColor: function (other, /* optional */ fraction) { + if (typeof(fraction) == 'undefined' || fraction === null) { + fraction = 0.5; + } + var sf = 1.0 - fraction; + var s = this.rgb; + var d = other.rgb; + var df = fraction; + return MochiKit.Color.Color.fromRGB( + (s.r * sf) + (d.r * df), + (s.g * sf) + (d.g * df), + (s.b * sf) + (d.b * df), + (s.a * sf) + (d.a * df) + ); + }, + + /** @id MochiKit.Color.Color.prototype.compareRGB */ + compareRGB: function (other) { + var a = this.asRGB(); + var b = other.asRGB(); + return MochiKit.Base.compare( + [a.r, a.g, a.b, a.a], + [b.r, b.g, b.b, b.a] + ); + }, + + /** @id MochiKit.Color.Color.prototype.isLight */ + isLight: function () { + return this.asHSL().b > 0.5; + }, + + /** @id MochiKit.Color.Color.prototype.isDark */ + isDark: function () { + return (!this.isLight()); + }, + + /** @id MochiKit.Color.Color.prototype.toHSLString */ + toHSLString: function () { + var c = this.asHSL(); + var ccc = MochiKit.Color.clampColorComponent; + var rval = this._hslString; + if (!rval) { + var mid = ( + ccc(c.h, 360).toFixed(0) + + "," + ccc(c.s, 100).toPrecision(4) + "%" + + "," + ccc(c.l, 100).toPrecision(4) + "%" + ); + var a = c.a; + if (a >= 1) { + a = 1; + rval = "hsl(" + mid + ")"; + } else { + if (a <= 0) { + a = 0; + } + rval = "hsla(" + mid + "," + a + ")"; + } + this._hslString = rval; + } + return rval; + }, + + /** @id MochiKit.Color.Color.prototype.toRGBString */ + toRGBString: function () { + var c = this.rgb; + var ccc = MochiKit.Color.clampColorComponent; + var rval = this._rgbString; + if (!rval) { + var mid = ( + ccc(c.r, 255).toFixed(0) + + "," + ccc(c.g, 255).toFixed(0) + + "," + ccc(c.b, 255).toFixed(0) + ); + if (c.a != 1) { + rval = "rgba(" + mid + "," + c.a + ")"; + } else { + rval = "rgb(" + mid + ")"; + } + this._rgbString = rval; + } + return rval; + }, + + /** @id MochiKit.Color.Color.prototype.asRGB */ + asRGB: function () { + return MochiKit.Base.clone(this.rgb); + }, + + /** @id MochiKit.Color.Color.prototype.toHexString */ + toHexString: function () { + var m = MochiKit.Color; + var c = this.rgb; + var ccc = MochiKit.Color.clampColorComponent; + var rval = this._hexString; + if (!rval) { + rval = ("#" + + m.toColorPart(ccc(c.r, 255)) + + m.toColorPart(ccc(c.g, 255)) + + m.toColorPart(ccc(c.b, 255)) + ); + this._hexString = rval; + } + return rval; + }, + + /** @id MochiKit.Color.Color.prototype.asHSV */ + asHSV: function () { + var hsv = this.hsv; + var c = this.rgb; + if (typeof(hsv) == 'undefined' || hsv === null) { + hsv = MochiKit.Color.rgbToHSV(this.rgb); + this.hsv = hsv; + } + return MochiKit.Base.clone(hsv); + }, + + /** @id MochiKit.Color.Color.prototype.asHSL */ + asHSL: function () { + var hsl = this.hsl; + var c = this.rgb; + if (typeof(hsl) == 'undefined' || hsl === null) { + hsl = MochiKit.Color.rgbToHSL(this.rgb); + this.hsl = hsl; + } + return MochiKit.Base.clone(hsl); + }, + + /** @id MochiKit.Color.Color.prototype.toString */ + toString: function () { + return this.toRGBString(); + }, + + /** @id MochiKit.Color.Color.prototype.repr */ + repr: function () { + var c = this.rgb; + var col = [c.r, c.g, c.b, c.a]; + return this.__class__.NAME + "(" + col.join(", ") + ")"; + } + +}; + +// Constructor methods + +MochiKit.Base.update(MochiKit.Color.Color, { + /** @id MochiKit.Color.Color.fromRGB */ + fromRGB: function (red, green, blue, alpha) { + // designated initializer + var Color = MochiKit.Color.Color; + if (arguments.length == 1) { + var rgb = red; + red = rgb.r; + green = rgb.g; + blue = rgb.b; + if (typeof(rgb.a) == 'undefined') { + alpha = undefined; + } else { + alpha = rgb.a; + } + } + return new Color(red, green, blue, alpha); + }, + + /** @id MochiKit.Color.Color.fromHSL */ + fromHSL: function (hue, saturation, lightness, alpha) { + var m = MochiKit.Color; + return m.Color.fromRGB(m.hslToRGB.apply(m, arguments)); + }, + + /** @id MochiKit.Color.Color.fromHSV */ + fromHSV: function (hue, saturation, value, alpha) { + var m = MochiKit.Color; + return m.Color.fromRGB(m.hsvToRGB.apply(m, arguments)); + }, + + /** @id MochiKit.Color.Color.fromName */ + fromName: function (name) { + var Color = MochiKit.Color.Color; + // Opera 9 seems to "quote" named colors(?!) + if (name.charAt(0) == '"') { + name = name.substr(1, name.length - 2); + } + var htmlColor = Color._namedColors[name.toLowerCase()]; + if (typeof(htmlColor) == 'string') { + return Color.fromHexString(htmlColor); + } else if (name == "transparent") { + return Color.transparentColor(); + } + return null; + }, + + /** @id MochiKit.Color.Color.fromString */ + fromString: function (colorString) { + var self = MochiKit.Color.Color; + var three = colorString.substr(0, 3); + if (three == "rgb") { + return self.fromRGBString(colorString); + } else if (three == "hsl") { + return self.fromHSLString(colorString); + } else if (colorString.charAt(0) == "#") { + return self.fromHexString(colorString); + } + return self.fromName(colorString); + }, + + + /** @id MochiKit.Color.Color.fromHexString */ + fromHexString: function (hexCode) { + if (hexCode.charAt(0) == '#') { + hexCode = hexCode.substring(1); + } + var components = []; + var i, hex; + if (hexCode.length == 3) { + for (i = 0; i < 3; i++) { + hex = hexCode.substr(i, 1); + components.push(parseInt(hex + hex, 16) / 255.0); + } + } else { + for (i = 0; i < 6; i += 2) { + hex = hexCode.substr(i, 2); + components.push(parseInt(hex, 16) / 255.0); + } + } + var Color = MochiKit.Color.Color; + return Color.fromRGB.apply(Color, components); + }, + + + _fromColorString: function (pre, method, scales, colorCode) { + // parses either HSL or RGB + if (colorCode.indexOf(pre) === 0) { + colorCode = colorCode.substring(colorCode.indexOf("(", 3) + 1, colorCode.length - 1); + } + var colorChunks = colorCode.split(/\s*,\s*/); + var colorFloats = []; + for (var i = 0; i < colorChunks.length; i++) { + var c = colorChunks[i]; + var val; + var three = c.substring(c.length - 3); + if (c.charAt(c.length - 1) == '%') { + val = 0.01 * parseFloat(c.substring(0, c.length - 1)); + } else if (three == "deg") { + val = parseFloat(c) / 360.0; + } else if (three == "rad") { + val = parseFloat(c) / (Math.PI * 2); + } else { + val = scales[i] * parseFloat(c); + } + colorFloats.push(val); + } + return this[method].apply(this, colorFloats); + }, + + /** @id MochiKit.Color.Color.fromComputedStyle */ + fromComputedStyle: function (elem, style) { + var d = MochiKit.DOM; + var cls = MochiKit.Color.Color; + for (elem = d.getElement(elem); elem; elem = elem.parentNode) { + var actualColor = MochiKit.Style.getStyle.apply(d, arguments); + if (!actualColor) { + continue; + } + var color = cls.fromString(actualColor); + if (!color) { + break; + } + if (color.asRGB().a > 0) { + return color; + } + } + return null; + }, + + /** @id MochiKit.Color.Color.fromBackground */ + fromBackground: function (elem) { + var cls = MochiKit.Color.Color; + return cls.fromComputedStyle( + elem, "backgroundColor", "background-color") || cls.whiteColor(); + }, + + /** @id MochiKit.Color.Color.fromText */ + fromText: function (elem) { + var cls = MochiKit.Color.Color; + return cls.fromComputedStyle( + elem, "color", "color") || cls.blackColor(); + }, + + /** @id MochiKit.Color.Color.namedColors */ + namedColors: function () { + return MochiKit.Base.clone(MochiKit.Color.Color._namedColors); + } +}); + + +// Module level functions + +MochiKit.Base.update(MochiKit.Color, { + /** @id MochiKit.Color.clampColorComponent */ + clampColorComponent: function (v, scale) { + v *= scale; + if (v < 0) { + return 0; + } else if (v > scale) { + return scale; + } else { + return v; + } + }, + + _hslValue: function (n1, n2, hue) { + if (hue > 6.0) { + hue -= 6.0; + } else if (hue < 0.0) { + hue += 6.0; + } + var val; + if (hue < 1.0) { + val = n1 + (n2 - n1) * hue; + } else if (hue < 3.0) { + val = n2; + } else if (hue < 4.0) { + val = n1 + (n2 - n1) * (4.0 - hue); + } else { + val = n1; + } + return val; + }, + + /** @id MochiKit.Color.hsvToRGB */ + hsvToRGB: function (hue, saturation, value, alpha) { + if (arguments.length == 1) { + var hsv = hue; + hue = hsv.h; + saturation = hsv.s; + value = hsv.v; + alpha = hsv.a; + } + var red; + var green; + var blue; + if (saturation === 0) { + red = value; + green = value; + blue = value; + } else { + var i = Math.floor(hue * 6); + var f = (hue * 6) - i; + var p = value * (1 - saturation); + var q = value * (1 - (saturation * f)); + var t = value * (1 - (saturation * (1 - f))); + switch (i) { + case 1: red = q; green = value; blue = p; break; + case 2: red = p; green = value; blue = t; break; + case 3: red = p; green = q; blue = value; break; + case 4: red = t; green = p; blue = value; break; + case 5: red = value; green = p; blue = q; break; + case 6: // fall through + case 0: red = value; green = t; blue = p; break; + } + } + return { + r: red, + g: green, + b: blue, + a: alpha + }; + }, + + /** @id MochiKit.Color.hslToRGB */ + hslToRGB: function (hue, saturation, lightness, alpha) { + if (arguments.length == 1) { + var hsl = hue; + hue = hsl.h; + saturation = hsl.s; + lightness = hsl.l; + alpha = hsl.a; + } + var red; + var green; + var blue; + if (saturation === 0) { + red = lightness; + green = lightness; + blue = lightness; + } else { + var m2; + if (lightness <= 0.5) { + m2 = lightness * (1.0 + saturation); + } else { + m2 = lightness + saturation - (lightness * saturation); + } + var m1 = (2.0 * lightness) - m2; + var f = MochiKit.Color._hslValue; + var h6 = hue * 6.0; + red = f(m1, m2, h6 + 2); + green = f(m1, m2, h6); + blue = f(m1, m2, h6 - 2); + } + return { + r: red, + g: green, + b: blue, + a: alpha + }; + }, + + /** @id MochiKit.Color.rgbToHSV */ + rgbToHSV: function (red, green, blue, alpha) { + if (arguments.length == 1) { + var rgb = red; + red = rgb.r; + green = rgb.g; + blue = rgb.b; + alpha = rgb.a; + } + var max = Math.max(Math.max(red, green), blue); + var min = Math.min(Math.min(red, green), blue); + var hue; + var saturation; + var value = max; + if (min == max) { + hue = 0; + saturation = 0; + } else { + var delta = (max - min); + saturation = delta / max; + + if (red == max) { + hue = (green - blue) / delta; + } else if (green == max) { + hue = 2 + ((blue - red) / delta); + } else { + hue = 4 + ((red - green) / delta); + } + hue /= 6; + if (hue < 0) { + hue += 1; + } + if (hue > 1) { + hue -= 1; + } + } + return { + h: hue, + s: saturation, + v: value, + a: alpha + }; + }, + + /** @id MochiKit.Color.rgbToHSL */ + rgbToHSL: function (red, green, blue, alpha) { + if (arguments.length == 1) { + var rgb = red; + red = rgb.r; + green = rgb.g; + blue = rgb.b; + alpha = rgb.a; + } + var max = Math.max(red, Math.max(green, blue)); + var min = Math.min(red, Math.min(green, blue)); + var hue; + var saturation; + var lightness = (max + min) / 2.0; + var delta = max - min; + if (delta === 0) { + hue = 0; + saturation = 0; + } else { + if (lightness <= 0.5) { + saturation = delta / (max + min); + } else { + saturation = delta / (2 - max - min); + } + if (red == max) { + hue = (green - blue) / delta; + } else if (green == max) { + hue = 2 + ((blue - red) / delta); + } else { + hue = 4 + ((red - green) / delta); + } + hue /= 6; + if (hue < 0) { + hue += 1; + } + if (hue > 1) { + hue -= 1; + } + + } + return { + h: hue, + s: saturation, + l: lightness, + a: alpha + }; + }, + + /** @id MochiKit.Color.toColorPart */ + toColorPart: function (num) { + num = Math.round(num); + var digits = num.toString(16); + if (num < 16) { + return '0' + digits; + } + return digits; + }, + + __new__: function () { + var m = MochiKit.Base; + /** @id MochiKit.Color.fromRGBString */ + this.Color.fromRGBString = m.bind( + this.Color._fromColorString, this.Color, "rgb", "fromRGB", + [1.0/255.0, 1.0/255.0, 1.0/255.0, 1] + ); + /** @id MochiKit.Color.fromHSLString */ + this.Color.fromHSLString = m.bind( + this.Color._fromColorString, this.Color, "hsl", "fromHSL", + [1.0/360.0, 0.01, 0.01, 1] + ); + + var third = 1.0 / 3.0; + /** @id MochiKit.Color.colors */ + var colors = { + // NSColor colors plus transparent + /** @id MochiKit.Color.blackColor */ + black: [0, 0, 0], + /** @id MochiKit.Color.blueColor */ + blue: [0, 0, 1], + /** @id MochiKit.Color.brownColor */ + brown: [0.6, 0.4, 0.2], + /** @id MochiKit.Color.cyanColor */ + cyan: [0, 1, 1], + /** @id MochiKit.Color.darkGrayColor */ + darkGray: [third, third, third], + /** @id MochiKit.Color.grayColor */ + gray: [0.5, 0.5, 0.5], + /** @id MochiKit.Color.greenColor */ + green: [0, 1, 0], + /** @id MochiKit.Color.lightGrayColor */ + lightGray: [2 * third, 2 * third, 2 * third], + /** @id MochiKit.Color.magentaColor */ + magenta: [1, 0, 1], + /** @id MochiKit.Color.orangeColor */ + orange: [1, 0.5, 0], + /** @id MochiKit.Color.purpleColor */ + purple: [0.5, 0, 0.5], + /** @id MochiKit.Color.redColor */ + red: [1, 0, 0], + /** @id MochiKit.Color.transparentColor */ + transparent: [0, 0, 0, 0], + /** @id MochiKit.Color.whiteColor */ + white: [1, 1, 1], + /** @id MochiKit.Color.yellowColor */ + yellow: [1, 1, 0] + }; + + var makeColor = function (name, r, g, b, a) { + var rval = this.fromRGB(r, g, b, a); + this[name] = function () { return rval; }; + return rval; + }; + + for (var k in colors) { + var name = k + "Color"; + var bindArgs = m.concat( + [makeColor, this.Color, name], + colors[k] + ); + this.Color[name] = m.bind.apply(null, bindArgs); + } + + var isColor = function () { + for (var i = 0; i < arguments.length; i++) { + if (!(arguments[i] instanceof Color)) { + return false; + } + } + return true; + }; + + var compareColor = function (a, b) { + return a.compareRGB(b); + }; + + m.nameFunctions(this); + + m.registerComparator(this.Color.NAME, isColor, compareColor); + + this.EXPORT_TAGS = { + ":common": this.EXPORT, + ":all": m.concat(this.EXPORT, this.EXPORT_OK) + }; + + } +}); + +MochiKit.Color.EXPORT = [ + "Color" +]; + +MochiKit.Color.EXPORT_OK = [ + "clampColorComponent", + "rgbToHSL", + "hslToRGB", + "rgbToHSV", + "hsvToRGB", + "toColorPart" +]; + +MochiKit.Color.__new__(); + +MochiKit.Base._exportSymbols(this, MochiKit.Color); + +// Full table of css3 X11 colors <http://www.w3.org/TR/css3-color/#X11COLORS> + +MochiKit.Color.Color._namedColors = { + aliceblue: "#f0f8ff", + antiquewhite: "#faebd7", + aqua: "#00ffff", + aquamarine: "#7fffd4", + azure: "#f0ffff", + beige: "#f5f5dc", + bisque: "#ffe4c4", + black: "#000000", + blanchedalmond: "#ffebcd", + blue: "#0000ff", + blueviolet: "#8a2be2", + brown: "#a52a2a", + burlywood: "#deb887", + cadetblue: "#5f9ea0", + chartreuse: "#7fff00", + chocolate: "#d2691e", + coral: "#ff7f50", + cornflowerblue: "#6495ed", + cornsilk: "#fff8dc", + crimson: "#dc143c", + cyan: "#00ffff", + darkblue: "#00008b", + darkcyan: "#008b8b", + darkgoldenrod: "#b8860b", + darkgray: "#a9a9a9", + darkgreen: "#006400", + darkgrey: "#a9a9a9", + darkkhaki: "#bdb76b", + darkmagenta: "#8b008b", + darkolivegreen: "#556b2f", + darkorange: "#ff8c00", + darkorchid: "#9932cc", + darkred: "#8b0000", + darksalmon: "#e9967a", + darkseagreen: "#8fbc8f", + darkslateblue: "#483d8b", + darkslategray: "#2f4f4f", + darkslategrey: "#2f4f4f", + darkturquoise: "#00ced1", + darkviolet: "#9400d3", + deeppink: "#ff1493", + deepskyblue: "#00bfff", + dimgray: "#696969", + dimgrey: "#696969", + dodgerblue: "#1e90ff", + firebrick: "#b22222", + floralwhite: "#fffaf0", + forestgreen: "#228b22", + fuchsia: "#ff00ff", + gainsboro: "#dcdcdc", + ghostwhite: "#f8f8ff", + gold: "#ffd700", + goldenrod: "#daa520", + gray: "#808080", + green: "#008000", + greenyellow: "#adff2f", + grey: "#808080", + honeydew: "#f0fff0", + hotpink: "#ff69b4", + indianred: "#cd5c5c", + indigo: "#4b0082", + ivory: "#fffff0", + khaki: "#f0e68c", + lavender: "#e6e6fa", + lavenderblush: "#fff0f5", + lawngreen: "#7cfc00", + lemonchiffon: "#fffacd", + lightblue: "#add8e6", + lightcoral: "#f08080", + lightcyan: "#e0ffff", + lightgoldenrodyellow: "#fafad2", + lightgray: "#d3d3d3", + lightgreen: "#90ee90", + lightgrey: "#d3d3d3", + lightpink: "#ffb6c1", + lightsalmon: "#ffa07a", + lightseagreen: "#20b2aa", + lightskyblue: "#87cefa", + lightslategray: "#778899", + lightslategrey: "#778899", + lightsteelblue: "#b0c4de", + lightyellow: "#ffffe0", + lime: "#00ff00", + limegreen: "#32cd32", + linen: "#faf0e6", + magenta: "#ff00ff", + maroon: "#800000", + mediumaquamarine: "#66cdaa", + mediumblue: "#0000cd", + mediumorchid: "#ba55d3", + mediumpurple: "#9370db", + mediumseagreen: "#3cb371", + mediumslateblue: "#7b68ee", + mediumspringgreen: "#00fa9a", + mediumturquoise: "#48d1cc", + mediumvioletred: "#c71585", + midnightblue: "#191970", + mintcream: "#f5fffa", + mistyrose: "#ffe4e1", + moccasin: "#ffe4b5", + navajowhite: "#ffdead", + navy: "#000080", + oldlace: "#fdf5e6", + olive: "#808000", + olivedrab: "#6b8e23", + orange: "#ffa500", + orangered: "#ff4500", + orchid: "#da70d6", + palegoldenrod: "#eee8aa", + palegreen: "#98fb98", + paleturquoise: "#afeeee", + palevioletred: "#db7093", + papayawhip: "#ffefd5", + peachpuff: "#ffdab9", + peru: "#cd853f", + pink: "#ffc0cb", + plum: "#dda0dd", + powderblue: "#b0e0e6", + purple: "#800080", + red: "#ff0000", + rosybrown: "#bc8f8f", + royalblue: "#4169e1", + saddlebrown: "#8b4513", + salmon: "#fa8072", + sandybrown: "#f4a460", + seagreen: "#2e8b57", + seashell: "#fff5ee", + sienna: "#a0522d", + silver: "#c0c0c0", + skyblue: "#87ceeb", + slateblue: "#6a5acd", + slategray: "#708090", + slategrey: "#708090", + snow: "#fffafa", + springgreen: "#00ff7f", + steelblue: "#4682b4", + tan: "#d2b48c", + teal: "#008080", + thistle: "#d8bfd8", + tomato: "#ff6347", + turquoise: "#40e0d0", + violet: "#ee82ee", + wheat: "#f5deb3", + white: "#ffffff", + whitesmoke: "#f5f5f5", + yellow: "#ffff00", + yellowgreen: "#9acd32" +}; diff --git a/dom/tests/mochitest/ajax/mochikit/MochiKit/Controls.js b/dom/tests/mochitest/ajax/mochikit/MochiKit/Controls.js new file mode 100644 index 0000000000..c044ef2abf --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/Controls.js @@ -0,0 +1,1388 @@ +/*** +Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) + (c) 2005 Ivan Krstic (http://blogs.law.harvard.edu/ivan) + (c) 2005 Jon Tirsen (http://www.tirsen.com) +Contributors: + Richard Livsey + Rahul Bhargava + Rob Wills + Mochi-ized By Thomas Herve (_firstname_@nimail.org) + +See scriptaculous.js for full license. + +Autocompleter.Base handles all the autocompletion functionality +that's independent of the data source for autocompletion. This +includes drawing the autocompletion menu, observing keyboard +and mouse events, and similar. + +Specific autocompleters need to provide, at the very least, +a getUpdatedChoices function that will be invoked every time +the text inside the monitored textbox changes. This method +should get the text for which to provide autocompletion by +invoking this.getToken(), NOT by directly accessing +this.element.value. This is to allow incremental tokenized +autocompletion. Specific auto-completion logic (AJAX, etc) +belongs in getUpdatedChoices. + +Tokenized incremental autocompletion is enabled automatically +when an autocompleter is instantiated with the 'tokens' option +in the options parameter, e.g.: +new Ajax.Autocompleter('id','upd', '/url/', { tokens: ',' }); +will incrementally autocomplete with a comma as the token. +Additionally, ',' in the above example can be replaced with +a token array, e.g. { tokens: [',', '\n'] } which +enables autocompletion on multiple tokens. This is most +useful when one of the tokens is \n (a newline), as it +allows smart autocompletion after linebreaks. + +***/ + +MochiKit.Base.update(MochiKit.Base, { + ScriptFragment: '(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)', + +/** @id MochiKit.Base.stripScripts */ + stripScripts: function (str) { + return str.replace(new RegExp(MochiKit.Base.ScriptFragment, 'img'), ''); + }, + +/** @id MochiKit.Base.stripTags */ + stripTags: function(str) { + return str.replace(/<\/?[^>]+>/gi, ''); + }, + +/** @id MochiKit.Base.extractScripts */ + extractScripts: function (str) { + var matchAll = new RegExp(MochiKit.Base.ScriptFragment, 'img'); + var matchOne = new RegExp(MochiKit.Base.ScriptFragment, 'im'); + return MochiKit.Base.map(function (scriptTag) { + return (scriptTag.match(matchOne) || ['', ''])[1]; + }, str.match(matchAll) || []); + }, + +/** @id MochiKit.Base.evalScripts */ + evalScripts: function (str) { + return MochiKit.Base.map(function (scr) { + eval(scr); + }, MochiKit.Base.extractScripts(str)); + } +}); + +MochiKit.Form = { + +/** @id MochiKit.Form.serialize */ + serialize: function (form) { + var elements = MochiKit.Form.getElements(form); + var queryComponents = []; + + for (var i = 0; i < elements.length; i++) { + var queryComponent = MochiKit.Form.serializeElement(elements[i]); + if (queryComponent) { + queryComponents.push(queryComponent); + } + } + + return queryComponents.join('&'); + }, + +/** @id MochiKit.Form.getElements */ + getElements: function (form) { + form = MochiKit.DOM.getElement(form); + var elements = []; + + for (var tagName in MochiKit.Form.Serializers) { + var tagElements = form.getElementsByTagName(tagName); + for (var j = 0; j < tagElements.length; j++) { + elements.push(tagElements[j]); + } + } + return elements; + }, + +/** @id MochiKit.Form.serializeElement */ + serializeElement: function (element) { + element = MochiKit.DOM.getElement(element); + var method = element.tagName.toLowerCase(); + var parameter = MochiKit.Form.Serializers[method](element); + + if (parameter) { + var key = encodeURIComponent(parameter[0]); + if (key.length === 0) { + return; + } + + if (!(parameter[1] instanceof Array)) { + parameter[1] = [parameter[1]]; + } + + return parameter[1].map(function (value) { + return key + '=' + encodeURIComponent(value); + }).join('&'); + } + } +}; + +MochiKit.Form.Serializers = { + +/** @id MochiKit.Form.Serializers.input */ + input: function (element) { + switch (element.type.toLowerCase()) { + case 'submit': + case 'hidden': + case 'password': + case 'text': + return MochiKit.Form.Serializers.textarea(element); + case 'checkbox': + case 'radio': + return MochiKit.Form.Serializers.inputSelector(element); + } + return false; + }, + +/** @id MochiKit.Form.Serializers.inputSelector */ + inputSelector: function (element) { + if (element.checked) { + return [element.name, element.value]; + } + }, + +/** @id MochiKit.Form.Serializers.textarea */ + textarea: function (element) { + return [element.name, element.value]; + }, + +/** @id MochiKit.Form.Serializers.select */ + select: function (element) { + return MochiKit.Form.Serializers[element.type == 'select-one' ? + 'selectOne' : 'selectMany'](element); + }, + +/** @id MochiKit.Form.Serializers.selectOne */ + selectOne: function (element) { + var value = '', opt, index = element.selectedIndex; + if (index >= 0) { + opt = element.options[index]; + value = opt.value; + if (!value && !('value' in opt)) { + value = opt.text; + } + } + return [element.name, value]; + }, + +/** @id MochiKit.Form.Serializers.selectMany */ + selectMany: function (element) { + var value = []; + for (var i = 0; i < element.length; i++) { + var opt = element.options[i]; + if (opt.selected) { + var optValue = opt.value; + if (!optValue && !('value' in opt)) { + optValue = opt.text; + } + value.push(optValue); + } + } + return [element.name, value]; + } +}; + +/** @id Ajax */ +var Ajax = { + activeRequestCount: 0 +}; + +Ajax.Responders = { + responders: [], + +/** @id Ajax.Responders.register */ + register: function (responderToAdd) { + if (MochiKit.Base.find(this.responders, responderToAdd) == -1) { + this.responders.push(responderToAdd); + } + }, + +/** @id Ajax.Responders.unregister */ + unregister: function (responderToRemove) { + this.responders = this.responders.without(responderToRemove); + }, + +/** @id Ajax.Responders.dispatch */ + dispatch: function (callback, request, transport, json) { + MochiKit.Iter.forEach(this.responders, function (responder) { + if (responder[callback] && + typeof(responder[callback]) == 'function') { + try { + responder[callback].apply(responder, [request, transport, json]); + } catch (e) {} + } + }); + } +}; + +Ajax.Responders.register({ + +/** @id Ajax.Responders.onCreate */ + onCreate: function () { + Ajax.activeRequestCount++; + }, + +/** @id Ajax.Responders.onComplete */ + onComplete: function () { + Ajax.activeRequestCount--; + } +}); + +/** @id Ajax.Base */ +Ajax.Base = function () {}; + +Ajax.Base.prototype = { + +/** @id Ajax.Base.prototype.setOptions */ + setOptions: function (options) { + this.options = { + method: 'post', + asynchronous: true, + parameters: '' + } + MochiKit.Base.update(this.options, options || {}); + }, + +/** @id Ajax.Base.prototype.responseIsSuccess */ + responseIsSuccess: function () { + return this.transport.status == undefined + || this.transport.status === 0 + || (this.transport.status >= 200 && this.transport.status < 300); + }, + +/** @id Ajax.Base.prototype.responseIsFailure */ + responseIsFailure: function () { + return !this.responseIsSuccess(); + } +}; + +/** @id Ajax.Request */ +Ajax.Request = function (url, options) { + this.__init__(url, options); +}; + +/** @id Ajax.Events */ +Ajax.Request.Events = ['Uninitialized', 'Loading', 'Loaded', + 'Interactive', 'Complete']; + +MochiKit.Base.update(Ajax.Request.prototype, Ajax.Base.prototype); + +MochiKit.Base.update(Ajax.Request.prototype, { + __init__: function (url, options) { + this.transport = MochiKit.Async.getXMLHttpRequest(); + this.setOptions(options); + this.request(url); + }, + +/** @id Ajax.Request.prototype.request */ + request: function (url) { + var parameters = this.options.parameters || ''; + if (parameters.length > 0){ + parameters += '&_='; + } + + try { + this.url = url; + if (this.options.method == 'get' && parameters.length > 0) { + this.url += (this.url.match(/\?/) ? '&' : '?') + parameters; + } + Ajax.Responders.dispatch('onCreate', this, this.transport); + + this.transport.open(this.options.method, this.url, + this.options.asynchronous); + + if (this.options.asynchronous) { + this.transport.onreadystatechange = MochiKit.Base.bind(this.onStateChange, this); + setTimeout(MochiKit.Base.bind(function () { + this.respondToReadyState(1); + }, this), 10); + } + + this.setRequestHeaders(); + + var body = this.options.postBody ? this.options.postBody : parameters; + this.transport.send(this.options.method == 'post' ? body : null); + + } catch (e) { + this.dispatchException(e); + } + }, + +/** @id Ajax.Request.prototype.setRequestHeaders */ + setRequestHeaders: function () { + var requestHeaders = ['X-Requested-With', 'XMLHttpRequest']; + + if (this.options.method == 'post') { + requestHeaders.push('Content-type', + 'application/x-www-form-urlencoded'); + + /* Force 'Connection: close' for Mozilla browsers to work around + * a bug where XMLHttpRequest sends an incorrect Content-length + * header. See Mozilla Bugzilla #246651. + */ + if (this.transport.overrideMimeType) { + requestHeaders.push('Connection', 'close'); + } + } + + if (this.options.requestHeaders) { + requestHeaders.push.apply(requestHeaders, this.options.requestHeaders); + } + + for (var i = 0; i < requestHeaders.length; i += 2) { + this.transport.setRequestHeader(requestHeaders[i], requestHeaders[i+1]); + } + }, + +/** @id Ajax.Request.prototype.onStateChange */ + onStateChange: function () { + var readyState = this.transport.readyState; + if (readyState != 1) { + this.respondToReadyState(this.transport.readyState); + } + }, + +/** @id Ajax.Request.prototype.header */ + header: function (name) { + try { + return this.transport.getResponseHeader(name); + } catch (e) {} + }, + +/** @id Ajax.Request.prototype.evalJSON */ + evalJSON: function () { + try { + return eval(this.header('X-JSON')); + } catch (e) {} + }, + +/** @id Ajax.Request.prototype.evalResponse */ + evalResponse: function () { + try { + return eval(this.transport.responseText); + } catch (e) { + this.dispatchException(e); + } + }, + +/** @id Ajax.Request.prototype.respondToReadyState */ + respondToReadyState: function (readyState) { + var event = Ajax.Request.Events[readyState]; + var transport = this.transport, json = this.evalJSON(); + + if (event == 'Complete') { + try { + (this.options['on' + this.transport.status] + || this.options['on' + (this.responseIsSuccess() ? 'Success' : 'Failure')] + || MochiKit.Base.noop)(transport, json); + } catch (e) { + this.dispatchException(e); + } + + if ((this.header('Content-type') || '').match(/^text\/javascript/i)) { + this.evalResponse(); + } + } + + try { + (this.options['on' + event] || MochiKit.Base.noop)(transport, json); + Ajax.Responders.dispatch('on' + event, this, transport, json); + } catch (e) { + this.dispatchException(e); + } + + /* Avoid memory leak in MSIE: clean up the oncomplete event handler */ + if (event == 'Complete') { + this.transport.onreadystatechange = MochiKit.Base.noop; + } + }, + +/** @id Ajax.Request.prototype.dispatchException */ + dispatchException: function (exception) { + (this.options.onException || MochiKit.Base.noop)(this, exception); + Ajax.Responders.dispatch('onException', this, exception); + } +}); + +/** @id Ajax.Updater */ +Ajax.Updater = function (container, url, options) { + this.__init__(container, url, options); +}; + +MochiKit.Base.update(Ajax.Updater.prototype, Ajax.Request.prototype); + +MochiKit.Base.update(Ajax.Updater.prototype, { + __init__: function (container, url, options) { + this.containers = { + success: container.success ? MochiKit.DOM.getElement(container.success) : MochiKit.DOM.getElement(container), + failure: container.failure ? MochiKit.DOM.getElement(container.failure) : + (container.success ? null : MochiKit.DOM.getElement(container)) + } + this.transport = MochiKit.Async.getXMLHttpRequest(); + this.setOptions(options); + + var onComplete = this.options.onComplete || MochiKit.Base.noop; + this.options.onComplete = MochiKit.Base.bind(function (transport, object) { + this.updateContent(); + onComplete(transport, object); + }, this); + + this.request(url); + }, + +/** @id Ajax.Updater.prototype.updateContent */ + updateContent: function () { + var receiver = this.responseIsSuccess() ? + this.containers.success : this.containers.failure; + var response = this.transport.responseText; + + if (!this.options.evalScripts) { + response = MochiKit.Base.stripScripts(response); + } + + if (receiver) { + if (this.options.insertion) { + new this.options.insertion(receiver, response); + } else { + MochiKit.DOM.getElement(receiver).innerHTML = + MochiKit.Base.stripScripts(response); + setTimeout(function () { + MochiKit.Base.evalScripts(response); + }, 10); + } + } + + if (this.responseIsSuccess()) { + if (this.onComplete) { + setTimeout(MochiKit.Base.bind(this.onComplete, this), 10); + } + } + } +}); + +/** @id Field */ +var Field = { + +/** @id clear */ + clear: function () { + for (var i = 0; i < arguments.length; i++) { + MochiKit.DOM.getElement(arguments[i]).value = ''; + } + }, + +/** @id focus */ + focus: function (element) { + MochiKit.DOM.getElement(element).focus(); + }, + +/** @id present */ + present: function () { + for (var i = 0; i < arguments.length; i++) { + if (MochiKit.DOM.getElement(arguments[i]).value == '') { + return false; + } + } + return true; + }, + +/** @id select */ + select: function (element) { + MochiKit.DOM.getElement(element).select(); + }, + +/** @id activate */ + activate: function (element) { + element = MochiKit.DOM.getElement(element); + element.focus(); + if (element.select) { + element.select(); + } + }, + +/** @id scrollFreeActivate */ + scrollFreeActivate: function (field) { + setTimeout(function () { + Field.activate(field); + }, 1); + } +}; + + +/** @id Autocompleter */ +var Autocompleter = {}; + +/** @id Autocompleter.Base */ +Autocompleter.Base = function () {}; + +Autocompleter.Base.prototype = { + +/** @id Autocompleter.Base.prototype.baseInitialize */ + baseInitialize: function (element, update, options) { + this.element = MochiKit.DOM.getElement(element); + this.update = MochiKit.DOM.getElement(update); + this.hasFocus = false; + this.changed = false; + this.active = false; + this.index = 0; + this.entryCount = 0; + + if (this.setOptions) { + this.setOptions(options); + } + else { + this.options = options || {}; + } + + this.options.paramName = this.options.paramName || this.element.name; + this.options.tokens = this.options.tokens || []; + this.options.frequency = this.options.frequency || 0.4; + this.options.minChars = this.options.minChars || 1; + this.options.onShow = this.options.onShow || function (element, update) { + if (!update.style.position || update.style.position == 'absolute') { + update.style.position = 'absolute'; + MochiKit.Position.clone(element, update, { + setHeight: false, + offsetTop: element.offsetHeight + }); + } + MochiKit.Visual.appear(update, {duration:0.15}); + }; + this.options.onHide = this.options.onHide || function (element, update) { + MochiKit.Visual.fade(update, {duration: 0.15}); + }; + + if (typeof(this.options.tokens) == 'string') { + this.options.tokens = new Array(this.options.tokens); + } + + this.observer = null; + + this.element.setAttribute('autocomplete', 'off'); + + MochiKit.Style.hideElement(this.update); + + MochiKit.Signal.connect(this.element, 'onblur', this, this.onBlur); + MochiKit.Signal.connect(this.element, 'onkeypress', this, this.onKeyPress, this); + }, + +/** @id Autocompleter.Base.prototype.show */ + show: function () { + if (MochiKit.Style.getStyle(this.update, 'display') == 'none') { + this.options.onShow(this.element, this.update); + } + if (!this.iefix && /MSIE/.test(navigator.userAgent && + (MochiKit.Style.getStyle(this.update, 'position') == 'absolute'))) { + new Insertion.After(this.update, + '<iframe id="' + this.update.id + '_iefix" '+ + 'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' + + 'src="javascript:false;" frameborder="0" scrolling="no"></iframe>'); + this.iefix = MochiKit.DOM.getElement(this.update.id + '_iefix'); + } + if (this.iefix) { + setTimeout(MochiKit.Base.bind(this.fixIEOverlapping, this), 50); + } + }, + +/** @id Autocompleter.Base.prototype.fixIEOverlapping */ + fixIEOverlapping: function () { + MochiKit.Position.clone(this.update, this.iefix); + this.iefix.style.zIndex = 1; + this.update.style.zIndex = 2; + MochiKit.Style.showElement(this.iefix); + }, + +/** @id Autocompleter.Base.prototype.hide */ + hide: function () { + this.stopIndicator(); + if (MochiKit.Style.getStyle(this.update, 'display') != 'none') { + this.options.onHide(this.element, this.update); + } + if (this.iefix) { + MochiKit.Style.hideElement(this.iefix); + } + }, + +/** @id Autocompleter.Base.prototype.startIndicator */ + startIndicator: function () { + if (this.options.indicator) { + MochiKit.Style.showElement(this.options.indicator); + } + }, + +/** @id Autocompleter.Base.prototype.stopIndicator */ + stopIndicator: function () { + if (this.options.indicator) { + MochiKit.Style.hideElement(this.options.indicator); + } + }, + +/** @id Autocompleter.Base.prototype.onKeyPress */ + onKeyPress: function (event) { + if (this.active) { + if (event.key().string == "KEY_TAB" || event.key().string == "KEY_RETURN") { + this.selectEntry(); + MochiKit.Event.stop(event); + } else if (event.key().string == "KEY_ESCAPE") { + this.hide(); + this.active = false; + MochiKit.Event.stop(event); + return; + } else if (event.key().string == "KEY_LEFT" || event.key().string == "KEY_RIGHT") { + return; + } else if (event.key().string == "KEY_UP") { + this.markPrevious(); + this.render(); + if (/AppleWebKit'/.test(navigator.appVersion)) { + event.stop(); + } + return; + } else if (event.key().string == "KEY_DOWN") { + this.markNext(); + this.render(); + if (/AppleWebKit'/.test(navigator.appVersion)) { + event.stop(); + } + return; + } + } else { + if (event.key().string == "KEY_TAB" || event.key().string == "KEY_RETURN") { + return; + } + } + + this.changed = true; + this.hasFocus = true; + + if (this.observer) { + clearTimeout(this.observer); + } + this.observer = setTimeout(MochiKit.Base.bind(this.onObserverEvent, this), + this.options.frequency*1000); + }, + +/** @id Autocompleter.Base.prototype.findElement */ + findElement: function (event, tagName) { + var element = event.target; + while (element.parentNode && (!element.tagName || + (element.tagName.toUpperCase() != tagName.toUpperCase()))) { + element = element.parentNode; + } + return element; + }, + +/** @id Autocompleter.Base.prototype.hover */ + onHover: function (event) { + var element = this.findElement(event, 'LI'); + if (this.index != element.autocompleteIndex) { + this.index = element.autocompleteIndex; + this.render(); + } + event.stop(); + }, + +/** @id Autocompleter.Base.prototype.onClick */ + onClick: function (event) { + var element = this.findElement(event, 'LI'); + this.index = element.autocompleteIndex; + this.selectEntry(); + this.hide(); + }, + +/** @id Autocompleter.Base.prototype.onBlur */ + onBlur: function (event) { + // needed to make click events working + setTimeout(MochiKit.Base.bind(this.hide, this), 250); + this.hasFocus = false; + this.active = false; + }, + +/** @id Autocompleter.Base.prototype.render */ + render: function () { + if (this.entryCount > 0) { + for (var i = 0; i < this.entryCount; i++) { + this.index == i ? + MochiKit.DOM.addElementClass(this.getEntry(i), 'selected') : + MochiKit.DOM.removeElementClass(this.getEntry(i), 'selected'); + } + if (this.hasFocus) { + this.show(); + this.active = true; + } + } else { + this.active = false; + this.hide(); + } + }, + +/** @id Autocompleter.Base.prototype.markPrevious */ + markPrevious: function () { + if (this.index > 0) { + this.index-- + } else { + this.index = this.entryCount-1; + } + }, + +/** @id Autocompleter.Base.prototype.markNext */ + markNext: function () { + if (this.index < this.entryCount-1) { + this.index++ + } else { + this.index = 0; + } + }, + +/** @id Autocompleter.Base.prototype.getEntry */ + getEntry: function (index) { + return this.update.firstChild.childNodes[index]; + }, + +/** @id Autocompleter.Base.prototype.getCurrentEntry */ + getCurrentEntry: function () { + return this.getEntry(this.index); + }, + +/** @id Autocompleter.Base.prototype.selectEntry */ + selectEntry: function () { + this.active = false; + this.updateElement(this.getCurrentEntry()); + }, + +/** @id Autocompleter.Base.prototype.collectTextNodesIgnoreClass */ + collectTextNodesIgnoreClass: function (element, className) { + return MochiKit.Base.flattenArray(MochiKit.Base.map(function (node) { + if (node.nodeType == 3) { + return node.nodeValue; + } else if (node.hasChildNodes() && !MochiKit.DOM.hasElementClass(node, className)) { + return this.collectTextNodesIgnoreClass(node, className); + } + return ''; + }, MochiKit.DOM.getElement(element).childNodes)).join(''); + }, + +/** @id Autocompleter.Base.prototype.updateElement */ + updateElement: function (selectedElement) { + if (this.options.updateElement) { + this.options.updateElement(selectedElement); + return; + } + var value = ''; + if (this.options.select) { + var nodes = document.getElementsByClassName(this.options.select, selectedElement) || []; + if (nodes.length > 0) { + value = MochiKit.DOM.scrapeText(nodes[0]); + } + } else { + value = this.collectTextNodesIgnoreClass(selectedElement, 'informal'); + } + var lastTokenPos = this.findLastToken(); + if (lastTokenPos != -1) { + var newValue = this.element.value.substr(0, lastTokenPos + 1); + var whitespace = this.element.value.substr(lastTokenPos + 1).match(/^\s+/); + if (whitespace) { + newValue += whitespace[0]; + } + this.element.value = newValue + value; + } else { + this.element.value = value; + } + this.element.focus(); + + if (this.options.afterUpdateElement) { + this.options.afterUpdateElement(this.element, selectedElement); + } + }, + +/** @id Autocompleter.Base.prototype.updateChoices */ + updateChoices: function (choices) { + if (!this.changed && this.hasFocus) { + this.update.innerHTML = choices; + var d = MochiKit.DOM; + d.removeEmptyTextNodes(this.update); + d.removeEmptyTextNodes(this.update.firstChild); + + if (this.update.firstChild && this.update.firstChild.childNodes) { + this.entryCount = this.update.firstChild.childNodes.length; + for (var i = 0; i < this.entryCount; i++) { + var entry = this.getEntry(i); + entry.autocompleteIndex = i; + this.addObservers(entry); + } + } else { + this.entryCount = 0; + } + + this.stopIndicator(); + + this.index = 0; + this.render(); + } + }, + +/** @id Autocompleter.Base.prototype.addObservers */ + addObservers: function (element) { + MochiKit.Signal.connect(element, 'onmouseover', this, this.onHover); + MochiKit.Signal.connect(element, 'onclick', this, this.onClick); + }, + +/** @id Autocompleter.Base.prototype.onObserverEvent */ + onObserverEvent: function () { + this.changed = false; + if (this.getToken().length >= this.options.minChars) { + this.startIndicator(); + this.getUpdatedChoices(); + } else { + this.active = false; + this.hide(); + } + }, + +/** @id Autocompleter.Base.prototype.getToken */ + getToken: function () { + var tokenPos = this.findLastToken(); + if (tokenPos != -1) { + var ret = this.element.value.substr(tokenPos + 1).replace(/^\s+/,'').replace(/\s+$/,''); + } else { + var ret = this.element.value; + } + return /\n/.test(ret) ? '' : ret; + }, + +/** @id Autocompleter.Base.prototype.findLastToken */ + findLastToken: function () { + var lastTokenPos = -1; + + for (var i = 0; i < this.options.tokens.length; i++) { + var thisTokenPos = this.element.value.lastIndexOf(this.options.tokens[i]); + if (thisTokenPos > lastTokenPos) { + lastTokenPos = thisTokenPos; + } + } + return lastTokenPos; + } +} + +/** @id Ajax.Autocompleter */ +Ajax.Autocompleter = function (element, update, url, options) { + this.__init__(element, update, url, options); +}; + +MochiKit.Base.update(Ajax.Autocompleter.prototype, Autocompleter.Base.prototype); + +MochiKit.Base.update(Ajax.Autocompleter.prototype, { + __init__: function (element, update, url, options) { + this.baseInitialize(element, update, options); + this.options.asynchronous = true; + this.options.onComplete = MochiKit.Base.bind(this.onComplete, this); + this.options.defaultParams = this.options.parameters || null; + this.url = url; + }, + +/** @id Ajax.Autocompleter.prototype.getUpdatedChoices */ + getUpdatedChoices: function () { + var entry = encodeURIComponent(this.options.paramName) + '=' + + encodeURIComponent(this.getToken()); + + this.options.parameters = this.options.callback ? + this.options.callback(this.element, entry) : entry; + + if (this.options.defaultParams) { + this.options.parameters += '&' + this.options.defaultParams; + } + new Ajax.Request(this.url, this.options); + }, + +/** @id Ajax.Autocompleter.prototype.onComplete */ + onComplete: function (request) { + this.updateChoices(request.responseText); + } +}); + +/*** + +The local array autocompleter. Used when you'd prefer to +inject an array of autocompletion options into the page, rather +than sending out Ajax queries, which can be quite slow sometimes. + +The constructor takes four parameters. The first two are, as usual, +the id of the monitored textbox, and id of the autocompletion menu. +The third is the array you want to autocomplete from, and the fourth +is the options block. + +Extra local autocompletion options: +- choices - How many autocompletion choices to offer + +- partialSearch - If false, the autocompleter will match entered + text only at the beginning of strings in the + autocomplete array. Defaults to true, which will + match text at the beginning of any *word* in the + strings in the autocomplete array. If you want to + search anywhere in the string, additionally set + the option fullSearch to true (default: off). + +- fullSsearch - Search anywhere in autocomplete array strings. + +- partialChars - How many characters to enter before triggering + a partial match (unlike minChars, which defines + how many characters are required to do any match + at all). Defaults to 2. + +- ignoreCase - Whether to ignore case when autocompleting. + Defaults to true. + +It's possible to pass in a custom function as the 'selector' +option, if you prefer to write your own autocompletion logic. +In that case, the other options above will not apply unless +you support them. + +***/ + +/** @id Autocompleter.Local */ +Autocompleter.Local = function (element, update, array, options) { + this.__init__(element, update, array, options); +}; + +MochiKit.Base.update(Autocompleter.Local.prototype, Autocompleter.Base.prototype); + +MochiKit.Base.update(Autocompleter.Local.prototype, { + __init__: function (element, update, array, options) { + this.baseInitialize(element, update, options); + this.options.array = array; + }, + +/** @id Autocompleter.Local.prototype.getUpdatedChoices */ + getUpdatedChoices: function () { + this.updateChoices(this.options.selector(this)); + }, + +/** @id Autocompleter.Local.prototype.setOptions */ + setOptions: function (options) { + this.options = MochiKit.Base.update({ + choices: 10, + partialSearch: true, + partialChars: 2, + ignoreCase: true, + fullSearch: false, + selector: function (instance) { + var ret = []; // Beginning matches + var partial = []; // Inside matches + var entry = instance.getToken(); + var count = 0; + + for (var i = 0; i < instance.options.array.length && + ret.length < instance.options.choices ; i++) { + + var elem = instance.options.array[i]; + var foundPos = instance.options.ignoreCase ? + elem.toLowerCase().indexOf(entry.toLowerCase()) : + elem.indexOf(entry); + + while (foundPos != -1) { + if (foundPos === 0 && elem.length != entry.length) { + ret.push('<li><strong>' + elem.substr(0, entry.length) + '</strong>' + + elem.substr(entry.length) + '</li>'); + break; + } else if (entry.length >= instance.options.partialChars && + instance.options.partialSearch && foundPos != -1) { + if (instance.options.fullSearch || /\s/.test(elem.substr(foundPos - 1, 1))) { + partial.push('<li>' + elem.substr(0, foundPos) + '<strong>' + + elem.substr(foundPos, entry.length) + '</strong>' + elem.substr( + foundPos + entry.length) + '</li>'); + break; + } + } + + foundPos = instance.options.ignoreCase ? + elem.toLowerCase().indexOf(entry.toLowerCase(), foundPos + 1) : + elem.indexOf(entry, foundPos + 1); + + } + } + if (partial.length) { + ret = ret.concat(partial.slice(0, instance.options.choices - ret.length)) + } + return '<ul>' + ret.join('') + '</ul>'; + } + }, options || {}); + } +}); + +/*** + +AJAX in-place editor + +see documentation on http://wiki.script.aculo.us/scriptaculous/show/Ajax.InPlaceEditor + +Use this if you notice weird scrolling problems on some browsers, +the DOM might be a bit confused when this gets called so do this +waits 1 ms (with setTimeout) until it does the activation + +***/ + +/** @id Ajax.InPlaceEditor */ +Ajax.InPlaceEditor = function (element, url, options) { + this.__init__(element, url, options); +}; + +/** @id Ajax.InPlaceEditor.defaultHighlightColor */ +Ajax.InPlaceEditor.defaultHighlightColor = '#FFFF99'; + +Ajax.InPlaceEditor.prototype = { + __init__: function (element, url, options) { + this.url = url; + this.element = MochiKit.DOM.getElement(element); + + this.options = MochiKit.Base.update({ + okButton: true, + okText: 'ok', + cancelLink: true, + cancelText: 'cancel', + savingText: 'Saving...', + clickToEditText: 'Click to edit', + okText: 'ok', + rows: 1, + onComplete: function (transport, element) { + new MochiKit.Visual.Highlight(element, {startcolor: this.options.highlightcolor}); + }, + onFailure: function (transport) { + alert('Error communicating with the server: ' + MochiKit.Base.stripTags(transport.responseText)); + }, + callback: function (form) { + return MochiKit.DOM.formContents(form); + }, + handleLineBreaks: true, + loadingText: 'Loading...', + savingClassName: 'inplaceeditor-saving', + loadingClassName: 'inplaceeditor-loading', + formClassName: 'inplaceeditor-form', + highlightcolor: Ajax.InPlaceEditor.defaultHighlightColor, + highlightendcolor: '#FFFFFF', + externalControl: null, + submitOnBlur: false, + ajaxOptions: {} + }, options || {}); + + if (!this.options.formId && this.element.id) { + this.options.formId = this.element.id + '-inplaceeditor'; + if (MochiKit.DOM.getElement(this.options.formId)) { + // there's already a form with that name, don't specify an id + this.options.formId = null; + } + } + + if (this.options.externalControl) { + this.options.externalControl = MochiKit.DOM.getElement(this.options.externalControl); + } + + this.originalBackground = MochiKit.Style.getStyle(this.element, 'background-color'); + if (!this.originalBackground) { + this.originalBackground = 'transparent'; + } + + this.element.title = this.options.clickToEditText; + + this.onclickListener = MochiKit.Signal.connect(this.element, 'onclick', this, this.enterEditMode); + this.mouseoverListener = MochiKit.Signal.connect(this.element, 'onmouseover', this, this.enterHover); + this.mouseoutListener = MochiKit.Signal.connect(this.element, 'onmouseout', this, this.leaveHover); + if (this.options.externalControl) { + this.onclickListenerExternal = MochiKit.Signal.connect(this.options.externalControl, + 'onclick', this, this.enterEditMode); + this.mouseoverListenerExternal = MochiKit.Signal.connect(this.options.externalControl, + 'onmouseover', this, this.enterHover); + this.mouseoutListenerExternal = MochiKit.Signal.connect(this.options.externalControl, + 'onmouseout', this, this.leaveHover); + } + }, + +/** @id Ajax.InPlaceEditor.prototype.enterEditMode */ + enterEditMode: function (evt) { + if (this.saving) { + return; + } + if (this.editing) { + return; + } + this.editing = true; + this.onEnterEditMode(); + if (this.options.externalControl) { + MochiKit.Style.hideElement(this.options.externalControl); + } + MochiKit.Style.hideElement(this.element); + this.createForm(); + this.element.parentNode.insertBefore(this.form, this.element); + Field.scrollFreeActivate(this.editField); + // stop the event to avoid a page refresh in Safari + if (evt) { + evt.stop(); + } + return false; + }, + +/** @id Ajax.InPlaceEditor.prototype.createForm */ + createForm: function () { + this.form = document.createElement('form'); + this.form.id = this.options.formId; + MochiKit.DOM.addElementClass(this.form, this.options.formClassName) + this.form.onsubmit = MochiKit.Base.bind(this.onSubmit, this); + + this.createEditField(); + + if (this.options.textarea) { + var br = document.createElement('br'); + this.form.appendChild(br); + } + + if (this.options.okButton) { + okButton = document.createElement('input'); + okButton.type = 'submit'; + okButton.value = this.options.okText; + this.form.appendChild(okButton); + } + + if (this.options.cancelLink) { + cancelLink = document.createElement('a'); + cancelLink.href = '#'; + cancelLink.appendChild(document.createTextNode(this.options.cancelText)); + cancelLink.onclick = MochiKit.Base.bind(this.onclickCancel, this); + this.form.appendChild(cancelLink); + } + }, + +/** @id Ajax.InPlaceEditor.prototype.hasHTMLLineBreaks */ + hasHTMLLineBreaks: function (string) { + if (!this.options.handleLineBreaks) { + return false; + } + return string.match(/<br/i) || string.match(/<p>/i); + }, + +/** @id Ajax.InPlaceEditor.prototype.convertHTMLLineBreaks */ + convertHTMLLineBreaks: function (string) { + return string.replace(/<br>/gi, '\n').replace(/<br\/>/gi, '\n').replace(/<\/p>/gi, '\n').replace(/<p>/gi, ''); + }, + +/** @id Ajax.InPlaceEditor.prototype.createEditField */ + createEditField: function () { + var text; + if (this.options.loadTextURL) { + text = this.options.loadingText; + } else { + text = this.getText(); + } + + var obj = this; + + if (this.options.rows == 1 && !this.hasHTMLLineBreaks(text)) { + this.options.textarea = false; + var textField = document.createElement('input'); + textField.obj = this; + textField.type = 'text'; + textField.name = 'value'; + textField.value = text; + textField.style.backgroundColor = this.options.highlightcolor; + var size = this.options.size || this.options.cols || 0; + if (size !== 0) { + textField.size = size; + } + if (this.options.submitOnBlur) { + textField.onblur = MochiKit.Base.bind(this.onSubmit, this); + } + this.editField = textField; + } else { + this.options.textarea = true; + var textArea = document.createElement('textarea'); + textArea.obj = this; + textArea.name = 'value'; + textArea.value = this.convertHTMLLineBreaks(text); + textArea.rows = this.options.rows; + textArea.cols = this.options.cols || 40; + if (this.options.submitOnBlur) { + textArea.onblur = MochiKit.Base.bind(this.onSubmit, this); + } + this.editField = textArea; + } + + if (this.options.loadTextURL) { + this.loadExternalText(); + } + this.form.appendChild(this.editField); + }, + +/** @id Ajax.InPlaceEditor.prototype.getText */ + getText: function () { + return this.element.innerHTML; + }, + +/** @id Ajax.InPlaceEditor.prototype.loadExternalText */ + loadExternalText: function () { + MochiKit.DOM.addElementClass(this.form, this.options.loadingClassName); + this.editField.disabled = true; + new Ajax.Request( + this.options.loadTextURL, + MochiKit.Base.update({ + asynchronous: true, + onComplete: MochiKit.Base.bind(this.onLoadedExternalText, this) + }, this.options.ajaxOptions) + ); + }, + +/** @id Ajax.InPlaceEditor.prototype.onLoadedExternalText */ + onLoadedExternalText: function (transport) { + MochiKit.DOM.removeElementClass(this.form, this.options.loadingClassName); + this.editField.disabled = false; + this.editField.value = MochiKit.Base.stripTags(transport); + }, + +/** @id Ajax.InPlaceEditor.prototype.onclickCancel */ + onclickCancel: function () { + this.onComplete(); + this.leaveEditMode(); + return false; + }, + +/** @id Ajax.InPlaceEditor.prototype.onFailure */ + onFailure: function (transport) { + this.options.onFailure(transport); + if (this.oldInnerHTML) { + this.element.innerHTML = this.oldInnerHTML; + this.oldInnerHTML = null; + } + return false; + }, + +/** @id Ajax.InPlaceEditor.prototype.onSubmit */ + onSubmit: function () { + // onLoading resets these so we need to save them away for the Ajax call + var form = this.form; + var value = this.editField.value; + + // do this first, sometimes the ajax call returns before we get a + // chance to switch on Saving which means this will actually switch on + // Saving *after* we have left edit mode causing Saving to be + // displayed indefinitely + this.onLoading(); + + new Ajax.Updater( + { + success: this.element, + // dont update on failure (this could be an option) + failure: null + }, + this.url, + MochiKit.Base.update({ + parameters: this.options.callback(form, value), + onComplete: MochiKit.Base.bind(this.onComplete, this), + onFailure: MochiKit.Base.bind(this.onFailure, this) + }, this.options.ajaxOptions) + ); + // stop the event to avoid a page refresh in Safari + if (arguments.length > 1) { + arguments[0].stop(); + } + return false; + }, + +/** @id Ajax.InPlaceEditor.prototype.onLoading */ + onLoading: function () { + this.saving = true; + this.removeForm(); + this.leaveHover(); + this.showSaving(); + }, + +/** @id Ajax.InPlaceEditor.prototype.onSaving */ + showSaving: function () { + this.oldInnerHTML = this.element.innerHTML; + this.element.innerHTML = this.options.savingText; + MochiKit.DOM.addElementClass(this.element, this.options.savingClassName); + this.element.style.backgroundColor = this.originalBackground; + MochiKit.Style.showElement(this.element); + }, + +/** @id Ajax.InPlaceEditor.prototype.removeForm */ + removeForm: function () { + if (this.form) { + if (this.form.parentNode) { + MochiKit.DOM.removeElement(this.form); + } + this.form = null; + } + }, + +/** @id Ajax.InPlaceEditor.prototype.enterHover */ + enterHover: function () { + if (this.saving) { + return; + } + this.element.style.backgroundColor = this.options.highlightcolor; + if (this.effect) { + this.effect.cancel(); + } + MochiKit.DOM.addElementClass(this.element, this.options.hoverClassName) + }, + +/** @id Ajax.InPlaceEditor.prototype.leaveHover */ + leaveHover: function () { + if (this.options.backgroundColor) { + this.element.style.backgroundColor = this.oldBackground; + } + MochiKit.DOM.removeElementClass(this.element, this.options.hoverClassName) + if (this.saving) { + return; + } + this.effect = new MochiKit.Visual.Highlight(this.element, { + startcolor: this.options.highlightcolor, + endcolor: this.options.highlightendcolor, + restorecolor: this.originalBackground + }); + }, + +/** @id Ajax.InPlaceEditor.prototype.leaveEditMode */ + leaveEditMode: function () { + MochiKit.DOM.removeElementClass(this.element, this.options.savingClassName); + this.removeForm(); + this.leaveHover(); + this.element.style.backgroundColor = this.originalBackground; + MochiKit.Style.showElement(this.element); + if (this.options.externalControl) { + MochiKit.Style.showElement(this.options.externalControl); + } + this.editing = false; + this.saving = false; + this.oldInnerHTML = null; + this.onLeaveEditMode(); + }, + +/** @id Ajax.InPlaceEditor.prototype.onComplete */ + onComplete: function (transport) { + this.leaveEditMode(); + MochiKit.Base.bind(this.options.onComplete, this)(transport, this.element); + }, + +/** @id Ajax.InPlaceEditor.prototype.onEnterEditMode */ + onEnterEditMode: function () {}, + +/** @id Ajax.InPlaceEditor.prototype.onLeaveEditMode */ + onLeaveEditMode: function () {}, + + /** @id Ajax.InPlaceEditor.prototype.dispose */ + dispose: function () { + if (this.oldInnerHTML) { + this.element.innerHTML = this.oldInnerHTML; + } + this.leaveEditMode(); + MochiKit.Signal.disconnect(this.onclickListener); + MochiKit.Signal.disconnect(this.mouseoverListener); + MochiKit.Signal.disconnect(this.mouseoutListener); + if (this.options.externalControl) { + MochiKit.Signal.disconnect(this.onclickListenerExternal); + MochiKit.Signal.disconnect(this.mouseoverListenerExternal); + MochiKit.Signal.disconnect(this.mouseoutListenerExternal); + } + } +}; + diff --git a/dom/tests/mochitest/ajax/mochikit/MochiKit/DOM.js b/dom/tests/mochitest/ajax/mochikit/MochiKit/DOM.js new file mode 100644 index 0000000000..02fe5bfb9c --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/DOM.js @@ -0,0 +1,1276 @@ +/*** + +MochiKit.DOM 1.4 + +See <http://mochikit.com/> for documentation, downloads, license, etc. + +(c) 2005 Bob Ippolito. All rights Reserved. + +***/ + +if (typeof(dojo) != 'undefined') { + dojo.provide("MochiKit.DOM"); + dojo.require("MochiKit.Base"); +} +if (typeof(JSAN) != 'undefined') { + JSAN.use("MochiKit.Base", []); +} + +try { + if (typeof(MochiKit.Base) == 'undefined') { + throw ""; + } +} catch (e) { + throw "MochiKit.DOM depends on MochiKit.Base!"; +} + +if (typeof(MochiKit.DOM) == 'undefined') { + MochiKit.DOM = {}; +} + +MochiKit.DOM.NAME = "MochiKit.DOM"; +MochiKit.DOM.VERSION = "1.4"; +MochiKit.DOM.__repr__ = function () { + return "[" + this.NAME + " " + this.VERSION + "]"; +}; +MochiKit.DOM.toString = function () { + return this.__repr__(); +}; + +MochiKit.DOM.EXPORT = [ + "removeEmptyTextNodes", + "formContents", + "currentWindow", + "currentDocument", + "withWindow", + "withDocument", + "registerDOMConverter", + "coerceToDOM", + "createDOM", + "createDOMFunc", + "isChildNode", + "getNodeAttribute", + "removeNodeAttribute", + "setNodeAttribute", + "updateNodeAttributes", + "appendChildNodes", + "insertSiblingNodesAfter", + "insertSiblingNodesBefore", + "replaceChildNodes", + "removeElement", + "swapDOM", + "BUTTON", + "TT", + "PRE", + "H1", + "H2", + "H3", + "BR", + "CANVAS", + "HR", + "LABEL", + "TEXTAREA", + "FORM", + "STRONG", + "SELECT", + "OPTION", + "OPTGROUP", + "LEGEND", + "FIELDSET", + "P", + "UL", + "OL", + "LI", + "TD", + "TR", + "THEAD", + "TBODY", + "TFOOT", + "TABLE", + "TH", + "INPUT", + "SPAN", + "A", + "DIV", + "IMG", + "getElement", + "$", + "getElementsByTagAndClassName", + "addToCallStack", + "addLoadEvent", + "focusOnLoad", + "setElementClass", + "toggleElementClass", + "addElementClass", + "removeElementClass", + "swapElementClass", + "hasElementClass", + "escapeHTML", + "toHTML", + "emitHTML", + "scrapeText", + "isParent", + "getFirstParentByTagAndClassName", + "makeClipping", + "undoClipping", + "makePositioned", + "undoPositioned", + "getFirstElementByTagAndClassName" +]; + +MochiKit.DOM.EXPORT_OK = [ + "domConverters" +]; + +MochiKit.DOM.DEPRECATED = [ + ['computedStyle', 'MochiKit.Style.getStyle', '1.4'], + /** @id MochiKit.DOM.elementDimensions */ + ['elementDimensions', 'MochiKit.Style.getElementDimensions', '1.4'], + /** @id MochiKit.DOM.elementPosition */ + ['elementPosition', 'MochiKit.Style.getElementPosition', '1.4'], + ['hideElement', 'MochiKit.Style.hideElement', '1.4'], + /** @id MochiKit.DOM.setElementDimensions */ + ['setElementDimensions', 'MochiKit.Style.setElementDimensions', '1.4'], + /** @id MochiKit.DOM.setElementPosition */ + ['setElementPosition', 'MochiKit.Style.setElementPosition', '1.4'], + ['setDisplayForElement', 'MochiKit.Style.setDisplayForElement', '1.4'], + /** @id MochiKit.DOM.setOpacity */ + ['setOpacity', 'MochiKit.Style.setOpacity', '1.4'], + ['showElement', 'MochiKit.Style.showElement', '1.4'], + /** @id MochiKit.DOM.Coordinates */ + ['Coordinates', 'MochiKit.Style.Coordinates', '1.4'], // FIXME: broken + /** @id MochiKit.DOM.Dimensions */ + ['Dimensions', 'MochiKit.Style.Dimensions', '1.4'] // FIXME: broken +]; + +/** @id MochiKit.DOM.getViewportDimensions */ +MochiKit.DOM.getViewportDimensions = new Function('' + + 'if (!MochiKit["Style"]) {' + + ' throw new Error("This function has been deprecated and depends on MochiKit.Style.");' + + '}' + + 'return MochiKit.Style.getViewportDimensions.apply(this, arguments);'); + +MochiKit.Base.update(MochiKit.DOM, { + + /** @id MochiKit.DOM.currentWindow */ + currentWindow: function () { + return MochiKit.DOM._window; + }, + + /** @id MochiKit.DOM.currentDocument */ + currentDocument: function () { + return MochiKit.DOM._document; + }, + + /** @id MochiKit.DOM.withWindow */ + withWindow: function (win, func) { + var self = MochiKit.DOM; + var oldDoc = self._document; + var oldWin = self._window; + var rval; + try { + self._window = win; + self._document = win.document; + rval = func(); + } catch (e) { + self._window = oldWin; + self._document = oldDoc; + throw e; + } + self._window = oldWin; + self._document = oldDoc; + return rval; + }, + + /** @id MochiKit.DOM.formContents */ + formContents: function (elem/* = document.body */) { + var names = []; + var values = []; + var m = MochiKit.Base; + var self = MochiKit.DOM; + if (typeof(elem) == "undefined" || elem === null) { + elem = self._document.body; + } else { + elem = self.getElement(elem); + } + m.nodeWalk(elem, function (elem) { + var name = elem.name; + if (m.isNotEmpty(name)) { + var tagName = elem.tagName.toUpperCase(); + if (tagName === "INPUT" + && (elem.type == "radio" || elem.type == "checkbox") + && !elem.checked + ) { + return null; + } + if (tagName === "SELECT") { + if (elem.type == "select-one") { + if (elem.selectedIndex >= 0) { + var opt = elem.options[elem.selectedIndex]; + var v = opt.value; + if (!v) { + var h = opt.outerHTML; + // internet explorer sure does suck. + if (h && !h.match(/^[^>]+\svalue\s*=/i)) { + v = opt.text; + } + } + names.push(name); + values.push(v); + return null; + } + // no form elements? + names.push(name); + values.push(""); + return null; + } else { + var opts = elem.options; + if (!opts.length) { + names.push(name); + values.push(""); + return null; + } + for (var i = 0; i < opts.length; i++) { + var opt = opts[i]; + if (!opt.selected) { + continue; + } + var v = opt.value; + if (!v) { + var h = opt.outerHTML; + // internet explorer sure does suck. + if (h && !h.match(/^[^>]+\svalue\s*=/i)) { + v = opt.text; + } + } + names.push(name); + values.push(v); + } + return null; + } + } + if (tagName === "FORM" || tagName === "P" || tagName === "SPAN" + || tagName === "DIV" + ) { + return elem.childNodes; + } + names.push(name); + values.push(elem.value || ''); + return null; + } + return elem.childNodes; + }); + return [names, values]; + }, + + /** @id MochiKit.DOM.withDocument */ + withDocument: function (doc, func) { + var self = MochiKit.DOM; + var oldDoc = self._document; + var rval; + try { + self._document = doc; + rval = func(); + } catch (e) { + self._document = oldDoc; + throw e; + } + self._document = oldDoc; + return rval; + }, + + /** @id MochiKit.DOM.registerDOMConverter */ + registerDOMConverter: function (name, check, wrap, /* optional */override) { + MochiKit.DOM.domConverters.register(name, check, wrap, override); + }, + + /** @id MochiKit.DOM.coerceToDOM */ + coerceToDOM: function (node, ctx) { + var m = MochiKit.Base; + var im = MochiKit.Iter; + var self = MochiKit.DOM; + if (im) { + var iter = im.iter; + var repeat = im.repeat; + var map = m.map; + } + var domConverters = self.domConverters; + var coerceToDOM = arguments.callee; + var NotFound = m.NotFound; + while (true) { + if (typeof(node) == 'undefined' || node === null) { + return null; + } + // this is a safari childNodes object, avoiding crashes w/ attr + // lookup + if (typeof(node) == "function" && + typeof(node.length) == "number" && + !(node instanceof Function)) { + node = im.list(node); + } + if (typeof(node.nodeType) != 'undefined' && node.nodeType > 0) { + return node; + } + if (typeof(node) == 'number' || typeof(node) == 'boolean') { + node = node.toString(); + // FALL THROUGH + } + if (typeof(node) == 'string') { + return self._document.createTextNode(node); + } + if (typeof(node.__dom__) == 'function') { + node = node.__dom__(ctx); + continue; + } + if (typeof(node.dom) == 'function') { + node = node.dom(ctx); + continue; + } + if (typeof(node) == 'function') { + node = node.apply(ctx, [ctx]); + continue; + } + + if (im) { + // iterable + var iterNodes = null; + try { + iterNodes = iter(node); + } catch (e) { + // pass + } + if (iterNodes) { + return map(coerceToDOM, iterNodes, repeat(ctx)); + } + } + + // adapter + try { + node = domConverters.match(node, ctx); + continue; + } catch (e) { + if (e != NotFound) { + throw e; + } + } + + // fallback + return self._document.createTextNode(node.toString()); + } + // mozilla warnings aren't too bright + return undefined; + }, + + /** @id MochiKit.DOM.isChildNode */ + isChildNode: function (node, maybeparent) { + var self = MochiKit.DOM; + if (typeof(node) == "string") { + node = self.getElement(node); + } + if (typeof(maybeparent) == "string") { + maybeparent = self.getElement(maybeparent); + } + if (node === maybeparent) { + return true; + } + while (node && node.tagName.toUpperCase() != "BODY") { + node = node.parentNode; + if (node === maybeparent) { + return true; + } + } + return false; + }, + + /** @id MochiKit.DOM.setNodeAttribute */ + setNodeAttribute: function (node, attr, value) { + var o = {}; + o[attr] = value; + try { + return MochiKit.DOM.updateNodeAttributes(node, o); + } catch (e) { + // pass + } + return null; + }, + + /** @id MochiKit.DOM.getNodeAttribute */ + getNodeAttribute: function (node, attr) { + var self = MochiKit.DOM; + var rename = self.attributeArray.renames[attr]; + node = self.getElement(node); + try { + if (rename) { + return node[rename]; + } + return node.getAttribute(attr); + } catch (e) { + // pass + } + return null; + }, + + /** @id MochiKit.DOM.removeNodeAttribute */ + removeNodeAttribute: function (node, attr) { + var self = MochiKit.DOM; + var rename = self.attributeArray.renames[attr]; + node = self.getElement(node); + try { + if (rename) { + return node[rename]; + } + return node.removeAttribute(attr); + } catch (e) { + // pass + } + return null; + }, + + /** @id MochiKit.DOM.updateNodeAttributes */ + updateNodeAttributes: function (node, attrs) { + var elem = node; + var self = MochiKit.DOM; + if (typeof(node) == 'string') { + elem = self.getElement(node); + } + if (attrs) { + var updatetree = MochiKit.Base.updatetree; + if (self.attributeArray.compliant) { + // not IE, good. + for (var k in attrs) { + var v = attrs[k]; + if (typeof(v) == 'object' && typeof(elem[k]) == 'object') { + if (k == "style" && MochiKit.Style) { + MochiKit.Style.setStyle(elem, v); + } else { + updatetree(elem[k], v); + } + } else if (k.substring(0, 2) == "on") { + if (typeof(v) == "string") { + v = new Function(v); + } + elem[k] = v; + } else { + elem.setAttribute(k, v); + } + } + } else { + // IE is insane in the membrane + var renames = self.attributeArray.renames; + for (var k in attrs) { + v = attrs[k]; + var renamed = renames[k]; + if (k == "style" && typeof(v) == "string") { + elem.style.cssText = v; + } else if (typeof(renamed) == "string") { + elem[renamed] = v; + } else if (typeof(elem[k]) == 'object' + && typeof(v) == 'object') { + if (k == "style" && MochiKit.Style) { + MochiKit.Style.setStyle(elem, v); + } else { + updatetree(elem[k], v); + } + } else if (k.substring(0, 2) == "on") { + if (typeof(v) == "string") { + v = new Function(v); + } + elem[k] = v; + } else { + elem.setAttribute(k, v); + } + } + } + } + return elem; + }, + + /** @id MochiKit.DOM.appendChildNodes */ + appendChildNodes: function (node/*, nodes...*/) { + var elem = node; + var self = MochiKit.DOM; + if (typeof(node) == 'string') { + elem = self.getElement(node); + } + var nodeStack = [ + self.coerceToDOM( + MochiKit.Base.extend(null, arguments, 1), + elem + ) + ]; + var concat = MochiKit.Base.concat; + while (nodeStack.length) { + var n = nodeStack.shift(); + if (typeof(n) == 'undefined' || n === null) { + // pass + } else if (typeof(n.nodeType) == 'number') { + elem.appendChild(n); + } else { + nodeStack = concat(n, nodeStack); + } + } + return elem; + }, + + + /** @id MochiKit.DOM.insertSiblingNodesBefore */ + insertSiblingNodesBefore: function (node/*, nodes...*/) { + var elem = node; + var self = MochiKit.DOM; + if (typeof(node) == 'string') { + elem = self.getElement(node); + } + var nodeStack = [ + self.coerceToDOM( + MochiKit.Base.extend(null, arguments, 1), + elem + ) + ]; + var parentnode = elem.parentNode; + var concat = MochiKit.Base.concat; + while (nodeStack.length) { + var n = nodeStack.shift(); + if (typeof(n) == 'undefined' || n === null) { + // pass + } else if (typeof(n.nodeType) == 'number') { + parentnode.insertBefore(n, elem); + } else { + nodeStack = concat(n, nodeStack); + } + } + return parentnode; + }, + + /** @id MochiKit.DOM.insertSiblingNodesAfter */ + insertSiblingNodesAfter: function (node/*, nodes...*/) { + var elem = node; + var self = MochiKit.DOM; + + if (typeof(node) == 'string') { + elem = self.getElement(node); + } + var nodeStack = [ + self.coerceToDOM( + MochiKit.Base.extend(null, arguments, 1), + elem + ) + ]; + + if (elem.nextSibling) { + return self.insertSiblingNodesBefore(elem.nextSibling, nodeStack); + } + else { + return self.appendChildNodes(elem.parentNode, nodeStack); + } + }, + + /** @id MochiKit.DOM.replaceChildNodes */ + replaceChildNodes: function (node/*, nodes...*/) { + var elem = node; + var self = MochiKit.DOM; + if (typeof(node) == 'string') { + elem = self.getElement(node); + arguments[0] = elem; + } + var child; + while ((child = elem.firstChild)) { + elem.removeChild(child); + } + if (arguments.length < 2) { + return elem; + } else { + return self.appendChildNodes.apply(this, arguments); + } + }, + + /** @id MochiKit.DOM.createDOM */ + createDOM: function (name, attrs/*, nodes... */) { + var elem; + var self = MochiKit.DOM; + var m = MochiKit.Base; + if (typeof(attrs) == "string" || typeof(attrs) == "number") { + var args = m.extend([name, null], arguments, 1); + return arguments.callee.apply(this, args); + } + if (typeof(name) == 'string') { + // Internet Explorer is dumb + var xhtml = self._xhtml; + if (attrs && !self.attributeArray.compliant) { + // http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/name_2.asp + var contents = ""; + if ('name' in attrs) { + contents += ' name="' + self.escapeHTML(attrs.name) + '"'; + } + if (name == 'input' && 'type' in attrs) { + contents += ' type="' + self.escapeHTML(attrs.type) + '"'; + } + if (contents) { + name = "<" + name + contents + ">"; + xhtml = false; + } + } + var d = self._document; + if (xhtml && d === document) { + elem = d.createElementNS("http://www.w3.org/1999/xhtml", name); + } else { + elem = d.createElement(name); + } + } else { + elem = name; + } + if (attrs) { + self.updateNodeAttributes(elem, attrs); + } + if (arguments.length <= 2) { + return elem; + } else { + var args = m.extend([elem], arguments, 2); + return self.appendChildNodes.apply(this, args); + } + }, + + /** @id MochiKit.DOM.createDOMFunc */ + createDOMFunc: function (/* tag, attrs, *nodes */) { + var m = MochiKit.Base; + return m.partial.apply( + this, + m.extend([MochiKit.DOM.createDOM], arguments) + ); + }, + + /** @id MochiKit.DOM.removeElement */ + removeElement: function (elem) { + var e = MochiKit.DOM.getElement(elem); + e.parentNode.removeChild(e); + return e; + }, + + /** @id MochiKit.DOM.swapDOM */ + swapDOM: function (dest, src) { + var self = MochiKit.DOM; + dest = self.getElement(dest); + var parent = dest.parentNode; + if (src) { + src = self.getElement(src); + parent.replaceChild(src, dest); + } else { + parent.removeChild(dest); + } + return src; + }, + + /** @id MochiKit.DOM.getElement */ + getElement: function (id) { + var self = MochiKit.DOM; + if (arguments.length == 1) { + return ((typeof(id) == "string") ? + self._document.getElementById(id) : id); + } else { + return MochiKit.Base.map(self.getElement, arguments); + } + }, + + /** @id MochiKit.DOM.getElementsByTagAndClassName */ + getElementsByTagAndClassName: function (tagName, className, + /* optional */parent) { + var self = MochiKit.DOM; + if (typeof(tagName) == 'undefined' || tagName === null) { + tagName = '*'; + } + if (typeof(parent) == 'undefined' || parent === null) { + parent = self._document; + } + parent = self.getElement(parent); + var children = (parent.getElementsByTagName(tagName) + || self._document.all); + if (typeof(className) == 'undefined' || className === null) { + return MochiKit.Base.extend(null, children); + } + + var elements = []; + for (var i = 0; i < children.length; i++) { + var child = children[i]; + var cls = child.className; + if (!cls) { + continue; + } + var classNames = cls.split(' '); + for (var j = 0; j < classNames.length; j++) { + if (classNames[j] == className) { + elements.push(child); + break; + } + } + } + + return elements; + }, + + _newCallStack: function (path, once) { + var rval = function () { + var callStack = arguments.callee.callStack; + for (var i = 0; i < callStack.length; i++) { + if (callStack[i].apply(this, arguments) === false) { + break; + } + } + if (once) { + try { + this[path] = null; + } catch (e) { + // pass + } + } + }; + rval.callStack = []; + return rval; + }, + + /** @id MochiKit.DOM.addToCallStack */ + addToCallStack: function (target, path, func, once) { + var self = MochiKit.DOM; + var existing = target[path]; + var regfunc = existing; + if (!(typeof(existing) == 'function' + && typeof(existing.callStack) == "object" + && existing.callStack !== null)) { + regfunc = self._newCallStack(path, once); + if (typeof(existing) == 'function') { + regfunc.callStack.push(existing); + } + target[path] = regfunc; + } + regfunc.callStack.push(func); + }, + + /** @id MochiKit.DOM.addLoadEvent */ + addLoadEvent: function (func) { + var self = MochiKit.DOM; + self.addToCallStack(self._window, "onload", func, true); + + }, + + /** @id MochiKit.DOM.focusOnLoad */ + focusOnLoad: function (element) { + var self = MochiKit.DOM; + self.addLoadEvent(function () { + element = self.getElement(element); + if (element) { + element.focus(); + } + }); + }, + + /** @id MochiKit.DOM.setElementClass */ + setElementClass: function (element, className) { + var self = MochiKit.DOM; + var obj = self.getElement(element); + if (self.attributeArray.compliant) { + obj.setAttribute("class", className); + } else { + obj.setAttribute("className", className); + } + }, + + /** @id MochiKit.DOM.toggleElementClass */ + toggleElementClass: function (className/*, element... */) { + var self = MochiKit.DOM; + for (var i = 1; i < arguments.length; i++) { + var obj = self.getElement(arguments[i]); + if (!self.addElementClass(obj, className)) { + self.removeElementClass(obj, className); + } + } + }, + + /** @id MochiKit.DOM.addElementClass */ + addElementClass: function (element, className) { + var self = MochiKit.DOM; + var obj = self.getElement(element); + var cls = obj.className; + // trivial case, no className yet + if (cls == undefined || cls.length === 0) { + self.setElementClass(obj, className); + return true; + } + // the other trivial case, already set as the only class + if (cls == className) { + return false; + } + var classes = cls.split(" "); + for (var i = 0; i < classes.length; i++) { + // already present + if (classes[i] == className) { + return false; + } + } + // append class + self.setElementClass(obj, cls + " " + className); + return true; + }, + + /** @id MochiKit.DOM.removeElementClass */ + removeElementClass: function (element, className) { + var self = MochiKit.DOM; + var obj = self.getElement(element); + var cls = obj.className; + // trivial case, no className yet + if (cls == undefined || cls.length === 0) { + return false; + } + // other trivial case, set only to className + if (cls == className) { + self.setElementClass(obj, ""); + return true; + } + var classes = cls.split(" "); + for (var i = 0; i < classes.length; i++) { + // already present + if (classes[i] == className) { + // only check sane case where the class is used once + classes.splice(i, 1); + self.setElementClass(obj, classes.join(" ")); + return true; + } + } + // not found + return false; + }, + + /** @id MochiKit.DOM.swapElementClass */ + swapElementClass: function (element, fromClass, toClass) { + var obj = MochiKit.DOM.getElement(element); + var res = MochiKit.DOM.removeElementClass(obj, fromClass); + if (res) { + MochiKit.DOM.addElementClass(obj, toClass); + } + return res; + }, + + /** @id MochiKit.DOM.hasElementClass */ + hasElementClass: function (element, className/*...*/) { + var obj = MochiKit.DOM.getElement(element); + var cls = obj.className; + if (!cls) { + return false; + } + var classes = cls.split(" "); + for (var i = 1; i < arguments.length; i++) { + var good = false; + for (var j = 0; j < classes.length; j++) { + if (classes[j] == arguments[i]) { + good = true; + break; + } + } + if (!good) { + return false; + } + } + return true; + }, + + /** @id MochiKit.DOM.escapeHTML */ + escapeHTML: function (s) { + return s.replace(/&/g, "&" + ).replace(/"/g, """ + ).replace(/</g, "<" + ).replace(/>/g, ">"); + }, + + /** @id MochiKit.DOM.toHTML */ + toHTML: function (dom) { + return MochiKit.DOM.emitHTML(dom).join(""); + }, + + /** @id MochiKit.DOM.emitHTML */ + emitHTML: function (dom, /* optional */lst) { + if (typeof(lst) == 'undefined' || lst === null) { + lst = []; + } + // queue is the call stack, we're doing this non-recursively + var queue = [dom]; + var self = MochiKit.DOM; + var escapeHTML = self.escapeHTML; + var attributeArray = self.attributeArray; + while (queue.length) { + dom = queue.pop(); + if (typeof(dom) == 'string') { + lst.push(dom); + } else if (dom.nodeType == 1) { + // we're not using higher order stuff here + // because safari has heisenbugs.. argh. + // + // I think it might have something to do with + // garbage collection and function calls. + lst.push('<' + dom.tagName.toLowerCase()); + var attributes = []; + var domAttr = attributeArray(dom); + for (var i = 0; i < domAttr.length; i++) { + var a = domAttr[i]; + attributes.push([ + " ", + a.name, + '="', + escapeHTML(a.value), + '"' + ]); + } + attributes.sort(); + for (i = 0; i < attributes.length; i++) { + var attrs = attributes[i]; + for (var j = 0; j < attrs.length; j++) { + lst.push(attrs[j]); + } + } + if (dom.hasChildNodes()) { + lst.push(">"); + // queue is the FILO call stack, so we put the close tag + // on first + queue.push("</" + dom.tagName.toLowerCase() + ">"); + var cnodes = dom.childNodes; + for (i = cnodes.length - 1; i >= 0; i--) { + queue.push(cnodes[i]); + } + } else { + lst.push('/>'); + } + } else if (dom.nodeType == 3) { + lst.push(escapeHTML(dom.nodeValue)); + } + } + return lst; + }, + + /** @id MochiKit.DOM.scrapeText */ + scrapeText: function (node, /* optional */asArray) { + var rval = []; + (function (node) { + var cn = node.childNodes; + if (cn) { + for (var i = 0; i < cn.length; i++) { + arguments.callee.call(this, cn[i]); + } + } + var nodeValue = node.nodeValue; + if (typeof(nodeValue) == 'string') { + rval.push(nodeValue); + } + })(MochiKit.DOM.getElement(node)); + if (asArray) { + return rval; + } else { + return rval.join(""); + } + }, + + /** @id MochiKit.DOM.removeEmptyTextNodes */ + removeEmptyTextNodes: function (element) { + element = MochiKit.DOM.getElement(element); + for (var i = 0; i < element.childNodes.length; i++) { + var node = element.childNodes[i]; + if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) { + node.parentNode.removeChild(node); + } + } + }, + + /** @id MochiKit.DOM.makeClipping */ + makeClipping: function (element) { + element = MochiKit.DOM.getElement(element); + var oldOverflow = element.style.overflow; + if ((MochiKit.Style.getStyle(element, 'overflow') || 'visible') != 'hidden') { + element.style.overflow = 'hidden'; + } + return oldOverflow; + }, + + /** @id MochiKit.DOM.undoClipping */ + undoClipping: function (element, overflow) { + element = MochiKit.DOM.getElement(element); + if (!overflow) { + return; + } + element.style.overflow = overflow; + }, + + /** @id MochiKit.DOM.makePositioned */ + makePositioned: function (element) { + element = MochiKit.DOM.getElement(element); + var pos = MochiKit.Style.getStyle(element, 'position'); + if (pos == 'static' || !pos) { + element.style.position = 'relative'; + // Opera returns the offset relative to the positioning context, + // when an element is position relative but top and left have + // not been defined + if (/Opera/.test(navigator.userAgent)) { + element.style.top = 0; + element.style.left = 0; + } + } + }, + + /** @id MochiKit.DOM.undoPositioned */ + undoPositioned: function (element) { + element = MochiKit.DOM.getElement(element); + if (element.style.position == 'relative') { + element.style.position = element.style.top = element.style.left = element.style.bottom = element.style.right = ''; + } + }, + + /** @id MochiKit.DOM.getFirstElementByTagAndClassName */ + getFirstElementByTagAndClassName: function (tagName, className, + /* optional */parent) { + var self = MochiKit.DOM; + if (typeof(tagName) == 'undefined' || tagName === null) { + tagName = '*'; + } + if (typeof(parent) == 'undefined' || parent === null) { + parent = self._document; + } + parent = self.getElement(parent); + var children = (parent.getElementsByTagName(tagName) + || self._document.all); + if (typeof(className) == 'undefined' || className === null) { + return children[0]; + } + + for (var i = 0; i < children.length; i++) { + var child = children[i]; + var classNames = child.className.split(' '); + for (var j = 0; j < classNames.length; j++) { + if (classNames[j] == className) { + return child; + } + } + } + }, + + /** @id MochiKit.DOM.getFirstParentByTagAndClassName */ + getFirstParentByTagAndClassName: function (elem, tagName, className) { + var self = MochiKit.DOM; + elem = self.getElement(elem); + if (typeof(tagName) == 'undefined' || tagName === null) { + tagName = '*'; + } else { + tagName = tagName.toUpperCase(); + } + if (typeof(className) == 'undefined' || className === null) { + className = null; + } + + var classList = ''; + var curTagName = ''; + while (elem && elem.tagName) { + elem = elem.parentNode; + if (tagName == '*' && className === null) { + return elem; + } + classList = elem.className.split(' '); + curTagName = elem.tagName.toUpperCase(); + if (className === null && tagName == curTagName) { + return elem; + } else if (className !== null) { + for (var i = 0; i < classList.length; i++) { + if (tagName == '*' && classList[i] == className) { + return elem; + } else if (tagName == curTagName && classList[i] == className) { + return elem; + } + } + } + } + return elem; + }, + + /** @id MochiKit.DOM.isParent */ + isParent: function (child, element) { + if (!child.parentNode || child == element) { + return false; + } + + if (child.parentNode == element) { + return true; + } + + return MochiKit.DOM.isParent(child.parentNode, element); + }, + + __new__: function (win) { + + var m = MochiKit.Base; + if (typeof(document) != "undefined") { + this._document = document; + var kXULNSURI = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; + this._xhtml = (document.documentElement && + document.createElementNS && + document.documentElement.namespaceURI === kXULNSURI); + } else if (MochiKit.MockDOM) { + this._document = MochiKit.MockDOM.document; + } + this._window = win; + + this.domConverters = new m.AdapterRegistry(); + + var __tmpElement = this._document.createElement("span"); + var attributeArray; + if (__tmpElement && __tmpElement.attributes && + __tmpElement.attributes.length > 0) { + // for braindead browsers (IE) that insert extra junk + var filter = m.filter; + attributeArray = function (node) { + return filter(attributeArray.ignoreAttrFilter, node.attributes); + }; + attributeArray.ignoreAttr = {}; + var attrs = __tmpElement.attributes; + var ignoreAttr = attributeArray.ignoreAttr; + for (var i = 0; i < attrs.length; i++) { + var a = attrs[i]; + ignoreAttr[a.name] = a.value; + } + attributeArray.ignoreAttrFilter = function (a) { + return (attributeArray.ignoreAttr[a.name] != a.value); + }; + attributeArray.compliant = false; + attributeArray.renames = { + "class": "className", + "checked": "defaultChecked", + "usemap": "useMap", + "for": "htmlFor", + "readonly": "readOnly", + "colspan": "colSpan", + "bgcolor": "bgColor", + "cellspacing": "cellSpacing", + "cellpadding": "cellPadding" + }; + } else { + attributeArray = function (node) { + /*** + + Return an array of attributes for a given node, + filtering out attributes that don't belong for + that are inserted by "Certain Browsers". + + ***/ + return node.attributes; + }; + attributeArray.compliant = true; + attributeArray.renames = {}; + } + this.attributeArray = attributeArray; + + // FIXME: this really belongs in Base, and could probably be cleaner + var _deprecated = function(fromModule, arr) { + var modules = arr[1].split('.'); + var str = ''; + var obj = {}; + + str += 'if (!MochiKit.' + modules[1] + ') { throw new Error("'; + str += 'This function has been deprecated and depends on MochiKit.'; + str += modules[1] + '.");}'; + str += 'return MochiKit.' + modules[1] + '.' + arr[0]; + str += '.apply(this, arguments);'; + + obj[modules[2]] = new Function(str); + MochiKit.Base.update(MochiKit[fromModule], obj); + } + for (var i; i < MochiKit.DOM.DEPRECATED.length; i++) { + _deprecated('DOM', MochiKit.DOM.DEPRECATED[i]); + } + + // shorthand for createDOM syntax + var createDOMFunc = this.createDOMFunc; + /** @id MochiKit.DOM.UL */ + this.UL = createDOMFunc("ul"); + /** @id MochiKit.DOM.OL */ + this.OL = createDOMFunc("ol"); + /** @id MochiKit.DOM.LI */ + this.LI = createDOMFunc("li"); + /** @id MochiKit.DOM.TD */ + this.TD = createDOMFunc("td"); + /** @id MochiKit.DOM.TR */ + this.TR = createDOMFunc("tr"); + /** @id MochiKit.DOM.TBODY */ + this.TBODY = createDOMFunc("tbody"); + /** @id MochiKit.DOM.THEAD */ + this.THEAD = createDOMFunc("thead"); + /** @id MochiKit.DOM.TFOOT */ + this.TFOOT = createDOMFunc("tfoot"); + /** @id MochiKit.DOM.TABLE */ + this.TABLE = createDOMFunc("table"); + /** @id MochiKit.DOM.TH */ + this.TH = createDOMFunc("th"); + /** @id MochiKit.DOM.INPUT */ + this.INPUT = createDOMFunc("input"); + /** @id MochiKit.DOM.SPAN */ + this.SPAN = createDOMFunc("span"); + /** @id MochiKit.DOM.A */ + this.A = createDOMFunc("a"); + /** @id MochiKit.DOM.DIV */ + this.DIV = createDOMFunc("div"); + /** @id MochiKit.DOM.IMG */ + this.IMG = createDOMFunc("img"); + /** @id MochiKit.DOM.BUTTON */ + this.BUTTON = createDOMFunc("button"); + /** @id MochiKit.DOM.TT */ + this.TT = createDOMFunc("tt"); + /** @id MochiKit.DOM.PRE */ + this.PRE = createDOMFunc("pre"); + /** @id MochiKit.DOM.H1 */ + this.H1 = createDOMFunc("h1"); + /** @id MochiKit.DOM.H2 */ + this.H2 = createDOMFunc("h2"); + /** @id MochiKit.DOM.H3 */ + this.H3 = createDOMFunc("h3"); + /** @id MochiKit.DOM.BR */ + this.BR = createDOMFunc("br"); + /** @id MochiKit.DOM.HR */ + this.HR = createDOMFunc("hr"); + /** @id MochiKit.DOM.LABEL */ + this.LABEL = createDOMFunc("label"); + /** @id MochiKit.DOM.TEXTAREA */ + this.TEXTAREA = createDOMFunc("textarea"); + /** @id MochiKit.DOM.FORM */ + this.FORM = createDOMFunc("form"); + /** @id MochiKit.DOM.P */ + this.P = createDOMFunc("p"); + /** @id MochiKit.DOM.SELECT */ + this.SELECT = createDOMFunc("select"); + /** @id MochiKit.DOM.OPTION */ + this.OPTION = createDOMFunc("option"); + /** @id MochiKit.DOM.OPTGROUP */ + this.OPTGROUP = createDOMFunc("optgroup"); + /** @id MochiKit.DOM.LEGEND */ + this.LEGEND = createDOMFunc("legend"); + /** @id MochiKit.DOM.FIELDSET */ + this.FIELDSET = createDOMFunc("fieldset"); + /** @id MochiKit.DOM.STRONG */ + this.STRONG = createDOMFunc("strong"); + /** @id MochiKit.DOM.CANVAS */ + this.CANVAS = createDOMFunc("canvas"); + + /** @id MochiKit.DOM.$ */ + this.$ = this.getElement; + + this.EXPORT_TAGS = { + ":common": this.EXPORT, + ":all": m.concat(this.EXPORT, this.EXPORT_OK) + }; + + m.nameFunctions(this); + + } +}); + + +MochiKit.DOM.__new__(((typeof(window) == "undefined") ? this : window)); + +// +// XXX: Internet Explorer blows +// +if (MochiKit.__export__) { + withWindow = MochiKit.DOM.withWindow; + withDocument = MochiKit.DOM.withDocument; +} + +MochiKit.Base._exportSymbols(this, MochiKit.DOM); diff --git a/dom/tests/mochitest/ajax/mochikit/MochiKit/DateTime.js b/dom/tests/mochitest/ajax/mochikit/MochiKit/DateTime.js new file mode 100644 index 0000000000..c0b03ee9db --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/DateTime.js @@ -0,0 +1,216 @@ +/*** + +MochiKit.DateTime 1.4 + +See <http://mochikit.com/> for documentation, downloads, license, etc. + +(c) 2005 Bob Ippolito. All rights Reserved. + +***/ + +if (typeof(dojo) != 'undefined') { + dojo.provide('MochiKit.DateTime'); +} + +if (typeof(MochiKit) == 'undefined') { + MochiKit = {}; +} + +if (typeof(MochiKit.DateTime) == 'undefined') { + MochiKit.DateTime = {}; +} + +MochiKit.DateTime.NAME = "MochiKit.DateTime"; +MochiKit.DateTime.VERSION = "1.4"; +MochiKit.DateTime.__repr__ = function () { + return "[" + this.NAME + " " + this.VERSION + "]"; +}; +MochiKit.DateTime.toString = function () { + return this.__repr__(); +}; + +/** @id MochiKit.DateTime.isoDate */ +MochiKit.DateTime.isoDate = function (str) { + str = str + ""; + if (typeof(str) != "string" || str.length === 0) { + return null; + } + var iso = str.split('-'); + if (iso.length === 0) { + return null; + } + return new Date(iso[0], iso[1] - 1, iso[2]); +}; + +MochiKit.DateTime._isoRegexp = /(\d{4,})(?:-(\d{1,2})(?:-(\d{1,2})(?:[T ](\d{1,2}):(\d{1,2})(?::(\d{1,2})(?:\.(\d+))?)?(?:(Z)|([+-])(\d{1,2})(?::(\d{1,2}))?)?)?)?)?/; + +/** @id MochiKit.DateTime.isoTimestamp */ +MochiKit.DateTime.isoTimestamp = function (str) { + str = str + ""; + if (typeof(str) != "string" || str.length === 0) { + return null; + } + var res = str.match(MochiKit.DateTime._isoRegexp); + if (typeof(res) == "undefined" || res === null) { + return null; + } + var year, month, day, hour, min, sec, msec; + year = parseInt(res[1], 10); + if (typeof(res[2]) == "undefined" || res[2] === '') { + return new Date(year); + } + month = parseInt(res[2], 10) - 1; + day = parseInt(res[3], 10); + if (typeof(res[4]) == "undefined" || res[4] === '') { + return new Date(year, month, day); + } + hour = parseInt(res[4], 10); + min = parseInt(res[5], 10); + sec = (typeof(res[6]) != "undefined" && res[6] !== '') ? parseInt(res[6], 10) : 0; + if (typeof(res[7]) != "undefined" && res[7] !== '') { + msec = Math.round(1000.0 * parseFloat("0." + res[7])); + } else { + msec = 0; + } + if ((typeof(res[8]) == "undefined" || res[8] === '') && (typeof(res[9]) == "undefined" || res[9] === '')) { + return new Date(year, month, day, hour, min, sec, msec); + } + var ofs; + if (typeof(res[9]) != "undefined" && res[9] !== '') { + ofs = parseInt(res[10], 10) * 3600000; + if (typeof(res[11]) != "undefined" && res[11] !== '') { + ofs += parseInt(res[11], 10) * 60000; + } + if (res[9] == "-") { + ofs = -ofs; + } + } else { + ofs = 0; + } + return new Date(Date.UTC(year, month, day, hour, min, sec, msec) - ofs); +}; + +/** @id MochiKit.DateTime.toISOTime */ +MochiKit.DateTime.toISOTime = function (date, realISO/* = false */) { + if (typeof(date) == "undefined" || date === null) { + return null; + } + var hh = date.getHours(); + var mm = date.getMinutes(); + var ss = date.getSeconds(); + var lst = [ + ((realISO && (hh < 10)) ? "0" + hh : hh), + ((mm < 10) ? "0" + mm : mm), + ((ss < 10) ? "0" + ss : ss) + ]; + return lst.join(":"); +}; + +/** @id MochiKit.DateTime.toISOTimeStamp */ +MochiKit.DateTime.toISOTimestamp = function (date, realISO/* = false*/) { + if (typeof(date) == "undefined" || date === null) { + return null; + } + var sep = realISO ? "T" : " "; + var foot = realISO ? "Z" : ""; + if (realISO) { + date = new Date(date.getTime() + (date.getTimezoneOffset() * 60000)); + } + return MochiKit.DateTime.toISODate(date) + sep + MochiKit.DateTime.toISOTime(date, realISO) + foot; +}; + +/** @id MochiKit.DateTime.toISODate */ +MochiKit.DateTime.toISODate = function (date) { + if (typeof(date) == "undefined" || date === null) { + return null; + } + var _padTwo = MochiKit.DateTime._padTwo; + return [ + date.getFullYear(), + _padTwo(date.getMonth() + 1), + _padTwo(date.getDate()) + ].join("-"); +}; + +/** @id MochiKit.DateTime.americanDate */ +MochiKit.DateTime.americanDate = function (d) { + d = d + ""; + if (typeof(d) != "string" || d.length === 0) { + return null; + } + var a = d.split('/'); + return new Date(a[2], a[0] - 1, a[1]); +}; + +MochiKit.DateTime._padTwo = function (n) { + return (n > 9) ? n : "0" + n; +}; + +/** @id MochiKit.DateTime.toPaddedAmericanDate */ +MochiKit.DateTime.toPaddedAmericanDate = function (d) { + if (typeof(d) == "undefined" || d === null) { + return null; + } + var _padTwo = MochiKit.DateTime._padTwo; + return [ + _padTwo(d.getMonth() + 1), + _padTwo(d.getDate()), + d.getFullYear() + ].join('/'); +}; + +/** @id MochiKit.DateTime.toAmericanDate */ +MochiKit.DateTime.toAmericanDate = function (d) { + if (typeof(d) == "undefined" || d === null) { + return null; + } + return [d.getMonth() + 1, d.getDate(), d.getFullYear()].join('/'); +}; + +MochiKit.DateTime.EXPORT = [ + "isoDate", + "isoTimestamp", + "toISOTime", + "toISOTimestamp", + "toISODate", + "americanDate", + "toPaddedAmericanDate", + "toAmericanDate" +]; + +MochiKit.DateTime.EXPORT_OK = []; +MochiKit.DateTime.EXPORT_TAGS = { + ":common": MochiKit.DateTime.EXPORT, + ":all": MochiKit.DateTime.EXPORT +}; + +MochiKit.DateTime.__new__ = function () { + // MochiKit.Base.nameFunctions(this); + var base = this.NAME + "."; + for (var k in this) { + var o = this[k]; + if (typeof(o) == 'function' && typeof(o.NAME) == 'undefined') { + try { + o.NAME = base + k; + } catch (e) { + // pass + } + } + } +}; + +MochiKit.DateTime.__new__(); + +if (typeof(MochiKit.Base) != "undefined") { + MochiKit.Base._exportSymbols(this, MochiKit.DateTime); +} else { + (function (globals, module) { + if ((typeof(JSAN) == 'undefined' && typeof(dojo) == 'undefined') + || (MochiKit.__export__ === false)) { + var all = module.EXPORT_TAGS[":all"]; + for (var i = 0; i < all.length; i++) { + globals[all[i]] = module[all[i]]; + } + } + })(this, MochiKit.DateTime); +} diff --git a/dom/tests/mochitest/ajax/mochikit/MochiKit/DragAndDrop.js b/dom/tests/mochitest/ajax/mochikit/MochiKit/DragAndDrop.js new file mode 100644 index 0000000000..c471ffe900 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/DragAndDrop.js @@ -0,0 +1,824 @@ +/*** +MochiKit.DragAndDrop 1.4 + +See <http://mochikit.com/> for documentation, downloads, license, etc. + +Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) + Mochi-ized By Thomas Herve (_firstname_@nimail.org) + +***/ + +if (typeof(dojo) != 'undefined') { + dojo.provide('MochiKit.DragAndDrop'); + dojo.require('MochiKit.Base'); + dojo.require('MochiKit.DOM'); + dojo.require('MochiKit.Iter'); + dojo.require('MochiKit.Visual'); + dojo.require('MochiKit.Signal'); +} + +if (typeof(JSAN) != 'undefined') { + JSAN.use("MochiKit.Base", []); + JSAN.use("MochiKit.DOM", []); + JSAN.use("MochiKit.Visual", []); + JSAN.use("MochiKit.Iter", []); + JSAN.use("MochiKit.Signal", []); +} + +try { + if (typeof(MochiKit.Base) == 'undefined' || + typeof(MochiKit.DOM) == 'undefined' || + typeof(MochiKit.Visual) == 'undefined' || + typeof(MochiKit.Signal) == 'undefined' || + typeof(MochiKit.Iter) == 'undefined') { + throw ""; + } +} catch (e) { + throw "MochiKit.DragAndDrop depends on MochiKit.Base, MochiKit.DOM, MochiKit.Visual, MochiKit.Signal and MochiKit.Iter!"; +} + +if (typeof(MochiKit.DragAndDrop) == 'undefined') { + MochiKit.DragAndDrop = {}; +} + +MochiKit.DragAndDrop.NAME = 'MochiKit.DragAndDrop'; +MochiKit.DragAndDrop.VERSION = '1.4'; + +MochiKit.DragAndDrop.__repr__ = function () { + return '[' + this.NAME + ' ' + this.VERSION + ']'; +}; + +MochiKit.DragAndDrop.toString = function () { + return this.__repr__(); +}; + +MochiKit.DragAndDrop.EXPORT = [ + "Droppable", + "Draggable" +]; + +MochiKit.DragAndDrop.EXPORT_OK = [ + "Droppables", + "Draggables" +]; + +MochiKit.DragAndDrop.Droppables = { + /*** + + Manage all droppables. Shouldn't be used, use the Droppable object instead. + + ***/ + drops: [], + + remove: function (element) { + this.drops = MochiKit.Base.filter(function (d) { + return d.element != MochiKit.DOM.getElement(element); + }, this.drops); + }, + + register: function (drop) { + this.drops.push(drop); + }, + + unregister: function (drop) { + this.drops = MochiKit.Base.filter(function (d) { + return d != drop; + }, this.drops); + }, + + prepare: function (element) { + MochiKit.Base.map(function (drop) { + if (drop.isAccepted(element)) { + if (drop.options.activeclass) { + MochiKit.DOM.addElementClass(drop.element, + drop.options.activeclass); + } + drop.options.onactive(drop.element, element); + } + }, this.drops); + }, + + findDeepestChild: function (drops) { + deepest = drops[0]; + + for (i = 1; i < drops.length; ++i) { + if (MochiKit.DOM.isParent(drops[i].element, deepest.element)) { + deepest = drops[i]; + } + } + return deepest; + }, + + show: function (point, element) { + if (!this.drops.length) { + return; + } + var affected = []; + + if (this.last_active) { + this.last_active.deactivate(); + } + MochiKit.Iter.forEach(this.drops, function (drop) { + if (drop.isAffected(point, element)) { + affected.push(drop); + } + }); + if (affected.length > 0) { + drop = this.findDeepestChild(affected); + MochiKit.Position.within(drop.element, point.page.x, point.page.y); + drop.options.onhover(element, drop.element, + MochiKit.Position.overlap(drop.options.overlap, drop.element)); + drop.activate(); + } + }, + + fire: function (event, element) { + if (!this.last_active) { + return; + } + MochiKit.Position.prepare(); + + if (this.last_active.isAffected(event.mouse(), element)) { + this.last_active.options.ondrop(element, + this.last_active.element, event); + } + }, + + reset: function (element) { + MochiKit.Base.map(function (drop) { + if (drop.options.activeclass) { + MochiKit.DOM.removeElementClass(drop.element, + drop.options.activeclass); + } + drop.options.ondesactive(drop.element, element); + }, this.drops); + if (this.last_active) { + this.last_active.deactivate(); + } + } +}; + +/** @id MochiKit.DragAndDrop.Droppable */ +MochiKit.DragAndDrop.Droppable = function (element, options) { + var cls = arguments.callee; + if (!(this instanceof cls)) { + return new cls(element, options); + } + this.__init__(element, options); +}; + +MochiKit.DragAndDrop.Droppable.prototype = { + /*** + + A droppable object. Simple use is to create giving an element: + + new MochiKit.DragAndDrop.Droppable('myelement'); + + Generally you'll want to define the 'ondrop' function and maybe the + 'accept' option to filter draggables. + + ***/ + __class__: MochiKit.DragAndDrop.Droppable, + + __init__: function (element, /* optional */options) { + var d = MochiKit.DOM; + var b = MochiKit.Base; + this.element = d.getElement(element); + this.options = b.update({ + + /** @id MochiKit.DragAndDrop.greedy */ + greedy: true, + + /** @id MochiKit.DragAndDrop.hoverclass */ + hoverclass: null, + + /** @id MochiKit.DragAndDrop.activeclass */ + activeclass: null, + + /** @id MochiKit.DragAndDrop.hoverfunc */ + hoverfunc: b.noop, + + /** @id MochiKit.DragAndDrop.accept */ + accept: null, + + /** @id MochiKit.DragAndDrop.onactive */ + onactive: b.noop, + + /** @id MochiKit.DragAndDrop.ondesactive */ + ondesactive: b.noop, + + /** @id MochiKit.DragAndDrop.onhover */ + onhover: b.noop, + + /** @id MochiKit.DragAndDrop.ondrop */ + ondrop: b.noop, + + /** @id MochiKit.DragAndDrop.containment */ + containment: [], + tree: false + }, options || {}); + + // cache containers + this.options._containers = []; + b.map(MochiKit.Base.bind(function (c) { + this.options._containers.push(d.getElement(c)); + }, this), this.options.containment); + + d.makePositioned(this.element); // fix IE + + MochiKit.DragAndDrop.Droppables.register(this); + }, + + /** @id MochiKit.DragAndDrop.isContained */ + isContained: function (element) { + if (this.options._containers.length) { + var containmentNode; + if (this.options.tree) { + containmentNode = element.treeNode; + } else { + containmentNode = element.parentNode; + } + return MochiKit.Iter.some(this.options._containers, function (c) { + return containmentNode == c; + }); + } else { + return true; + } + }, + + /** @id MochiKit.DragAndDrop.isAccepted */ + isAccepted: function (element) { + return ((!this.options.accept) || MochiKit.Iter.some( + this.options.accept, function (c) { + return MochiKit.DOM.hasElementClass(element, c); + })); + }, + + /** @id MochiKit.DragAndDrop.isAffected */ + isAffected: function (point, element) { + return ((this.element != element) && + this.isContained(element) && + this.isAccepted(element) && + MochiKit.Position.within(this.element, point.page.x, + point.page.y)); + }, + + /** @id MochiKit.DragAndDrop.deactivate */ + deactivate: function () { + /*** + + A droppable is deactivate when a draggable has been over it and left. + + ***/ + if (this.options.hoverclass) { + MochiKit.DOM.removeElementClass(this.element, + this.options.hoverclass); + } + this.options.hoverfunc(this.element, false); + MochiKit.DragAndDrop.Droppables.last_active = null; + }, + + /** @id MochiKit.DragAndDrop.activate */ + activate: function () { + /*** + + A droppable is active when a draggable is over it. + + ***/ + if (this.options.hoverclass) { + MochiKit.DOM.addElementClass(this.element, this.options.hoverclass); + } + this.options.hoverfunc(this.element, true); + MochiKit.DragAndDrop.Droppables.last_active = this; + }, + + /** @id MochiKit.DragAndDrop.destroy */ + destroy: function () { + /*** + + Delete this droppable. + + ***/ + MochiKit.DragAndDrop.Droppables.unregister(this); + }, + + /** @id MochiKit.DragAndDrop.repr */ + repr: function () { + return '[' + this.__class__.NAME + ", options:" + MochiKit.Base.repr(this.options) + "]"; + } +}; + +MochiKit.DragAndDrop.Draggables = { + /*** + + Manage draggables elements. Not intended to direct use. + + ***/ + drags: [], + + register: function (draggable) { + if (this.drags.length === 0) { + var conn = MochiKit.Signal.connect; + this.eventMouseUp = conn(document, 'onmouseup', this, this.endDrag); + this.eventMouseMove = conn(document, 'onmousemove', this, + this.updateDrag); + this.eventKeypress = conn(document, 'onkeypress', this, + this.keyPress); + } + this.drags.push(draggable); + }, + + unregister: function (draggable) { + this.drags = MochiKit.Base.filter(function (d) { + return d != draggable; + }, this.drags); + if (this.drags.length === 0) { + var disc = MochiKit.Signal.disconnect; + disc(this.eventMouseUp); + disc(this.eventMouseMove); + disc(this.eventKeypress); + } + }, + + activate: function (draggable) { + // allows keypress events if window is not currently focused + // fails for Safari + window.focus(); + this.activeDraggable = draggable; + }, + + deactivate: function () { + this.activeDraggable = null; + }, + + updateDrag: function (event) { + if (!this.activeDraggable) { + return; + } + var pointer = event.mouse(); + // Mozilla-based browsers fire successive mousemove events with + // the same coordinates, prevent needless redrawing (moz bug?) + if (this._lastPointer && (MochiKit.Base.repr(this._lastPointer.page) == + MochiKit.Base.repr(pointer.page))) { + return; + } + this._lastPointer = pointer; + this.activeDraggable.updateDrag(event, pointer); + }, + + endDrag: function (event) { + if (!this.activeDraggable) { + return; + } + this._lastPointer = null; + this.activeDraggable.endDrag(event); + this.activeDraggable = null; + }, + + keyPress: function (event) { + if (this.activeDraggable) { + this.activeDraggable.keyPress(event); + } + }, + + notify: function (eventName, draggable, event) { + MochiKit.Signal.signal(this, eventName, draggable, event); + } +}; + +/** @id MochiKit.DragAndDrop.Draggable */ +MochiKit.DragAndDrop.Draggable = function (element, options) { + var cls = arguments.callee; + if (!(this instanceof cls)) { + return new cls(element, options); + } + this.__init__(element, options); +}; + +MochiKit.DragAndDrop.Draggable.prototype = { + /*** + + A draggable object. Simple instantiate : + + new MochiKit.DragAndDrop.Draggable('myelement'); + + ***/ + __class__ : MochiKit.DragAndDrop.Draggable, + + __init__: function (element, /* optional */options) { + var v = MochiKit.Visual; + var b = MochiKit.Base; + options = b.update({ + + /** @id MochiKit.DragAndDrop.handle */ + handle: false, + + /** @id MochiKit.DragAndDrop.starteffect */ + starteffect: function (innerelement) { + this._savedOpacity = MochiKit.Style.getStyle(innerelement, 'opacity') || 1.0; + new v.Opacity(innerelement, {duration:0.2, from:this._savedOpacity, to:0.7}); + }, + /** @id MochiKit.DragAndDrop.reverteffect */ + reverteffect: function (innerelement, top_offset, left_offset) { + var dur = Math.sqrt(Math.abs(top_offset^2) + + Math.abs(left_offset^2))*0.02; + return new v.Move(innerelement, + {x: -left_offset, y: -top_offset, duration: dur}); + }, + + /** @id MochiKit.DragAndDrop.endeffect */ + endeffect: function (innerelement) { + new v.Opacity(innerelement, {duration:0.2, from:0.7, to:this._savedOpacity}); + }, + + /** @id MochiKit.DragAndDrop.onchange */ + onchange: b.noop, + + /** @id MochiKit.DragAndDrop.zindex */ + zindex: 1000, + + /** @id MochiKit.DragAndDrop.revert */ + revert: false, + + /** @id MochiKit.DragAndDrop.scroll */ + scroll: false, + + /** @id MochiKit.DragAndDrop.scrollSensitivity */ + scrollSensitivity: 20, + + /** @id MochiKit.DragAndDrop.scrollSpeed */ + scrollSpeed: 15, + // false, or xy or [x, y] or function (x, y){return [x, y];} + + /** @id MochiKit.DragAndDrop.snap */ + snap: false + }, options || {}); + + var d = MochiKit.DOM; + this.element = d.getElement(element); + + if (options.handle && (typeof(options.handle) == 'string')) { + this.handle = d.getFirstElementByTagAndClassName(null, + options.handle, this.element); + } + if (!this.handle) { + this.handle = d.getElement(options.handle); + } + if (!this.handle) { + this.handle = this.element; + } + + if (options.scroll && !options.scroll.scrollTo && !options.scroll.outerHTML) { + options.scroll = d.getElement(options.scroll); + this._isScrollChild = MochiKit.DOM.isChildNode(this.element, options.scroll); + } + + d.makePositioned(this.element); // fix IE + + this.delta = this.currentDelta(); + this.options = options; + this.dragging = false; + + this.eventMouseDown = MochiKit.Signal.connect(this.handle, + 'onmousedown', this, this.initDrag); + MochiKit.DragAndDrop.Draggables.register(this); + }, + + /** @id MochiKit.DragAndDrop.destroy */ + destroy: function () { + MochiKit.Signal.disconnect(this.eventMouseDown); + MochiKit.DragAndDrop.Draggables.unregister(this); + }, + + /** @id MochiKit.DragAndDrop.currentDelta */ + currentDelta: function () { + var s = MochiKit.Style.getStyle; + return [ + parseInt(s(this.element, 'left') || '0'), + parseInt(s(this.element, 'top') || '0')]; + }, + + /** @id MochiKit.DragAndDrop.initDrag */ + initDrag: function (event) { + if (!event.mouse().button.left) { + return; + } + // abort on form elements, fixes a Firefox issue + var src = event.target(); + var tagName = (src.tagName || '').toUpperCase(); + if (tagName === 'INPUT' || tagName === 'SELECT' || + tagName === 'OPTION' || tagName === 'BUTTON' || + tagName === 'TEXTAREA') { + return; + } + + if (this._revert) { + this._revert.cancel(); + this._revert = null; + } + + var pointer = event.mouse(); + var pos = MochiKit.Position.cumulativeOffset(this.element); + this.offset = [pointer.page.x - pos.x, pointer.page.y - pos.y]; + + MochiKit.DragAndDrop.Draggables.activate(this); + event.stop(); + }, + + /** @id MochiKit.DragAndDrop.startDrag */ + startDrag: function (event) { + this.dragging = true; + if (this.options.selectclass) { + MochiKit.DOM.addElementClass(this.element, + this.options.selectclass); + } + if (this.options.zindex) { + this.originalZ = parseInt(MochiKit.Style.getStyle(this.element, + 'z-index') || '0'); + this.element.style.zIndex = this.options.zindex; + } + + if (this.options.ghosting) { + this._clone = this.element.cloneNode(true); + this.ghostPosition = MochiKit.Position.absolutize(this.element); + this.element.parentNode.insertBefore(this._clone, this.element); + } + + if (this.options.scroll) { + if (this.options.scroll == window) { + var where = this._getWindowScroll(this.options.scroll); + this.originalScrollLeft = where.left; + this.originalScrollTop = where.top; + } else { + this.originalScrollLeft = this.options.scroll.scrollLeft; + this.originalScrollTop = this.options.scroll.scrollTop; + } + } + + MochiKit.DragAndDrop.Droppables.prepare(this.element); + MochiKit.DragAndDrop.Draggables.notify('start', this, event); + if (this.options.starteffect) { + this.options.starteffect(this.element); + } + }, + + /** @id MochiKit.DragAndDrop.updateDrag */ + updateDrag: function (event, pointer) { + if (!this.dragging) { + this.startDrag(event); + } + MochiKit.Position.prepare(); + MochiKit.DragAndDrop.Droppables.show(pointer, this.element); + MochiKit.DragAndDrop.Draggables.notify('drag', this, event); + this.draw(pointer); + this.options.onchange(this); + + if (this.options.scroll) { + this.stopScrolling(); + var p, q; + if (this.options.scroll == window) { + var s = this._getWindowScroll(this.options.scroll); + p = new MochiKit.Style.Coordinates(s.left, s.top); + q = new MochiKit.Style.Coordinates(s.left + s.width, + s.top + s.height); + } else { + p = MochiKit.Position.page(this.options.scroll); + p.x += this.options.scroll.scrollLeft; + p.y += this.options.scroll.scrollTop; + p.x += (window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0); + p.y += (window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0); + q = new MochiKit.Style.Coordinates(p.x + this.options.scroll.offsetWidth, + p.y + this.options.scroll.offsetHeight); + } + var speed = [0, 0]; + if (pointer.page.x > (q.x - this.options.scrollSensitivity)) { + speed[0] = pointer.page.x - (q.x - this.options.scrollSensitivity); + } else if (pointer.page.x < (p.x + this.options.scrollSensitivity)) { + speed[0] = pointer.page.x - (p.x + this.options.scrollSensitivity); + } + if (pointer.page.y > (q.y - this.options.scrollSensitivity)) { + speed[1] = pointer.page.y - (q.y - this.options.scrollSensitivity); + } else if (pointer.page.y < (p.y + this.options.scrollSensitivity)) { + speed[1] = pointer.page.y - (p.y + this.options.scrollSensitivity); + } + this.startScrolling(speed); + } + + // fix AppleWebKit rendering + if (/AppleWebKit'/.test(navigator.appVersion)) { + window.scrollBy(0, 0); + } + event.stop(); + }, + + /** @id MochiKit.DragAndDrop.finishDrag */ + finishDrag: function (event, success) { + var dr = MochiKit.DragAndDrop; + this.dragging = false; + if (this.options.selectclass) { + MochiKit.DOM.removeElementClass(this.element, + this.options.selectclass); + } + + if (this.options.ghosting) { + // XXX: from a user point of view, it would be better to remove + // the node only *after* the MochiKit.Visual.Move end when used + // with revert. + MochiKit.Position.relativize(this.element, this.ghostPosition); + MochiKit.DOM.removeElement(this._clone); + this._clone = null; + } + + if (success) { + dr.Droppables.fire(event, this.element); + } + dr.Draggables.notify('end', this, event); + + var revert = this.options.revert; + if (revert && typeof(revert) == 'function') { + revert = revert(this.element); + } + + var d = this.currentDelta(); + if (revert && this.options.reverteffect) { + this._revert = this.options.reverteffect(this.element, + d[1] - this.delta[1], d[0] - this.delta[0]); + } else { + this.delta = d; + } + + if (this.options.zindex) { + this.element.style.zIndex = this.originalZ; + } + + if (this.options.endeffect) { + this.options.endeffect(this.element); + } + + dr.Draggables.deactivate(); + dr.Droppables.reset(this.element); + }, + + /** @id MochiKit.DragAndDrop.keyPress */ + keyPress: function (event) { + if (event.key().string != "KEY_ESCAPE") { + return; + } + this.finishDrag(event, false); + event.stop(); + }, + + /** @id MochiKit.DragAndDrop.endDrag */ + endDrag: function (event) { + if (!this.dragging) { + return; + } + this.stopScrolling(); + this.finishDrag(event, true); + event.stop(); + }, + + /** @id MochiKit.DragAndDrop.draw */ + draw: function (point) { + var pos = MochiKit.Position.cumulativeOffset(this.element); + var d = this.currentDelta(); + pos.x -= d[0]; + pos.y -= d[1]; + + if (this.options.scroll && (this.options.scroll != window && this._isScrollChild)) { + pos.x -= this.options.scroll.scrollLeft - this.originalScrollLeft; + pos.y -= this.options.scroll.scrollTop - this.originalScrollTop; + } + + var p = [point.page.x - pos.x - this.offset[0], + point.page.y - pos.y - this.offset[1]]; + + if (this.options.snap) { + if (typeof(this.options.snap) == 'function') { + p = this.options.snap(p[0], p[1]); + } else { + if (this.options.snap instanceof Array) { + var i = -1; + p = MochiKit.Base.map(MochiKit.Base.bind(function (v) { + i += 1; + return Math.round(v/this.options.snap[i]) * + this.options.snap[i]; + }, this), p); + } else { + p = MochiKit.Base.map(MochiKit.Base.bind(function (v) { + return Math.round(v/this.options.snap) * + this.options.snap; + }, this), p); + } + } + } + var style = this.element.style; + if ((!this.options.constraint) || + (this.options.constraint == 'horizontal')) { + style.left = p[0] + 'px'; + } + if ((!this.options.constraint) || + (this.options.constraint == 'vertical')) { + style.top = p[1] + 'px'; + } + if (style.visibility == 'hidden') { + style.visibility = ''; // fix gecko rendering + } + }, + + /** @id MochiKit.DragAndDrop.stopScrolling */ + stopScrolling: function () { + if (this.scrollInterval) { + clearInterval(this.scrollInterval); + this.scrollInterval = null; + MochiKit.DragAndDrop.Draggables._lastScrollPointer = null; + } + }, + + /** @id MochiKit.DragAndDrop.startScrolling */ + startScrolling: function (speed) { + if (!speed[0] && !speed[1]) { + return; + } + this.scrollSpeed = [speed[0] * this.options.scrollSpeed, + speed[1] * this.options.scrollSpeed]; + this.lastScrolled = new Date(); + this.scrollInterval = setInterval(MochiKit.Base.bind(this.scroll, this), 10); + }, + + /** @id MochiKit.DragAndDrop.scroll */ + scroll: function () { + var current = new Date(); + var delta = current - this.lastScrolled; + this.lastScrolled = current; + + if (this.options.scroll == window) { + var s = this._getWindowScroll(this.options.scroll); + if (this.scrollSpeed[0] || this.scrollSpeed[1]) { + var dm = delta / 1000; + this.options.scroll.scrollTo(s.left + dm * this.scrollSpeed[0], + s.top + dm * this.scrollSpeed[1]); + } + } else { + this.options.scroll.scrollLeft += this.scrollSpeed[0] * delta / 1000; + this.options.scroll.scrollTop += this.scrollSpeed[1] * delta / 1000; + } + + var d = MochiKit.DragAndDrop; + + MochiKit.Position.prepare(); + d.Droppables.show(d.Draggables._lastPointer, this.element); + d.Draggables.notify('drag', this); + if (this._isScrollChild) { + d.Draggables._lastScrollPointer = d.Draggables._lastScrollPointer || d.Draggables._lastPointer; + d.Draggables._lastScrollPointer.x += this.scrollSpeed[0] * delta / 1000; + d.Draggables._lastScrollPointer.y += this.scrollSpeed[1] * delta / 1000; + if (d.Draggables._lastScrollPointer.x < 0) { + d.Draggables._lastScrollPointer.x = 0; + } + if (d.Draggables._lastScrollPointer.y < 0) { + d.Draggables._lastScrollPointer.y = 0; + } + this.draw(d.Draggables._lastScrollPointer); + } + + this.options.onchange(this); + }, + + _getWindowScroll: function (win) { + var vp, w, h; + MochiKit.DOM.withWindow(win, function () { + vp = MochiKit.Style.getViewportPosition(win.document); + }); + if (win.innerWidth) { + w = win.innerWidth; + h = win.innerHeight; + } else if (win.document.documentElement && win.document.documentElement.clientWidth) { + w = win.document.documentElement.clientWidth; + h = win.document.documentElement.clientHeight; + } else { + w = win.document.body.offsetWidth; + h = win.document.body.offsetHeight; + } + return {top: vp.x, left: vp.y, width: w, height: h}; + }, + + /** @id MochiKit.DragAndDrop.repr */ + repr: function () { + return '[' + this.__class__.NAME + ", options:" + MochiKit.Base.repr(this.options) + "]"; + } +}; + +MochiKit.DragAndDrop.__new__ = function () { + MochiKit.Base.nameFunctions(this); + + this.EXPORT_TAGS = { + ":common": this.EXPORT, + ":all": MochiKit.Base.concat(this.EXPORT, this.EXPORT_OK) + }; +}; + +MochiKit.DragAndDrop.__new__(); + +MochiKit.Base._exportSymbols(this, MochiKit.DragAndDrop); + diff --git a/dom/tests/mochitest/ajax/mochikit/MochiKit/Format.js b/dom/tests/mochitest/ajax/mochikit/MochiKit/Format.js new file mode 100644 index 0000000000..937b681f70 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/Format.js @@ -0,0 +1,304 @@ +/*** + +MochiKit.Format 1.4 + +See <http://mochikit.com/> for documentation, downloads, license, etc. + +(c) 2005 Bob Ippolito. All rights Reserved. + +***/ + +if (typeof(dojo) != 'undefined') { + dojo.provide('MochiKit.Format'); +} + +if (typeof(MochiKit) == 'undefined') { + MochiKit = {}; +} + +if (typeof(MochiKit.Format) == 'undefined') { + MochiKit.Format = {}; +} + +MochiKit.Format.NAME = "MochiKit.Format"; +MochiKit.Format.VERSION = "1.4"; +MochiKit.Format.__repr__ = function () { + return "[" + this.NAME + " " + this.VERSION + "]"; +}; +MochiKit.Format.toString = function () { + return this.__repr__(); +}; + +MochiKit.Format._numberFormatter = function (placeholder, header, footer, locale, isPercent, precision, leadingZeros, separatorAt, trailingZeros) { + return function (num) { + num = parseFloat(num); + if (typeof(num) == "undefined" || num === null || isNaN(num)) { + return placeholder; + } + var curheader = header; + var curfooter = footer; + if (num < 0) { + num = -num; + } else { + curheader = curheader.replace(/-/, ""); + } + var me = arguments.callee; + var fmt = MochiKit.Format.formatLocale(locale); + if (isPercent) { + num = num * 100.0; + curfooter = fmt.percent + curfooter; + } + num = MochiKit.Format.roundToFixed(num, precision); + var parts = num.split(/\./); + var whole = parts[0]; + var frac = (parts.length == 1) ? "" : parts[1]; + var res = ""; + while (whole.length < leadingZeros) { + whole = "0" + whole; + } + if (separatorAt) { + while (whole.length > separatorAt) { + var i = whole.length - separatorAt; + //res = res + fmt.separator + whole.substring(i, whole.length); + res = fmt.separator + whole.substring(i, whole.length) + res; + whole = whole.substring(0, i); + } + } + res = whole + res; + if (precision > 0) { + while (frac.length < trailingZeros) { + frac = frac + "0"; + } + res = res + fmt.decimal + frac; + } + return curheader + res + curfooter; + }; +}; + +/** @id MochiKit.Format.numberFormatter */ +MochiKit.Format.numberFormatter = function (pattern, placeholder/* = "" */, locale/* = "default" */) { + // http://java.sun.com/docs/books/tutorial/i18n/format/numberpattern.html + // | 0 | leading or trailing zeros + // | # | just the number + // | , | separator + // | . | decimal separator + // | % | Multiply by 100 and format as percent + if (typeof(placeholder) == "undefined") { + placeholder = ""; + } + var match = pattern.match(/((?:[0#]+,)?[0#]+)(?:\.([0#]+))?(%)?/); + if (!match) { + throw TypeError("Invalid pattern"); + } + var header = pattern.substr(0, match.index); + var footer = pattern.substr(match.index + match[0].length); + if (header.search(/-/) == -1) { + header = header + "-"; + } + var whole = match[1]; + var frac = (typeof(match[2]) == "string" && match[2] != "") ? match[2] : ""; + var isPercent = (typeof(match[3]) == "string" && match[3] != ""); + var tmp = whole.split(/,/); + var separatorAt; + if (typeof(locale) == "undefined") { + locale = "default"; + } + if (tmp.length == 1) { + separatorAt = null; + } else { + separatorAt = tmp[1].length; + } + var leadingZeros = whole.length - whole.replace(/0/g, "").length; + var trailingZeros = frac.length - frac.replace(/0/g, "").length; + var precision = frac.length; + var rval = MochiKit.Format._numberFormatter( + placeholder, header, footer, locale, isPercent, precision, + leadingZeros, separatorAt, trailingZeros + ); + var m = MochiKit.Base; + if (m) { + var fn = arguments.callee; + var args = m.concat(arguments); + rval.repr = function () { + return [ + self.NAME, + "(", + map(m.repr, args).join(", "), + ")" + ].join(""); + }; + } + return rval; +}; + +/** @id MochiKit.Format.formatLocale */ +MochiKit.Format.formatLocale = function (locale) { + if (typeof(locale) == "undefined" || locale === null) { + locale = "default"; + } + if (typeof(locale) == "string") { + var rval = MochiKit.Format.LOCALE[locale]; + if (typeof(rval) == "string") { + rval = arguments.callee(rval); + MochiKit.Format.LOCALE[locale] = rval; + } + return rval; + } else { + return locale; + } +}; + +/** @id MochiKit.Format.twoDigitAverage */ +MochiKit.Format.twoDigitAverage = function (numerator, denominator) { + if (denominator) { + var res = numerator / denominator; + if (!isNaN(res)) { + return MochiKit.Format.twoDigitFloat(numerator / denominator); + } + } + return "0"; +}; + +/** @id MochiKit.Format.twoDigitFloat */ +MochiKit.Format.twoDigitFloat = function (someFloat) { + var sign = (someFloat < 0 ? '-' : ''); + var s = Math.floor(Math.abs(someFloat) * 100).toString(); + if (s == '0') { + return s; + } + if (s.length < 3) { + while (s.charAt(s.length - 1) == '0') { + s = s.substring(0, s.length - 1); + } + return sign + '0.' + s; + } + var head = sign + s.substring(0, s.length - 2); + var tail = s.substring(s.length - 2, s.length); + if (tail == '00') { + return head; + } else if (tail.charAt(1) == '0') { + return head + '.' + tail.charAt(0); + } else { + return head + '.' + tail; + } +}; + +/** @id MochiKit.Format.lstrip */ +MochiKit.Format.lstrip = function (str, /* optional */chars) { + str = str + ""; + if (typeof(str) != "string") { + return null; + } + if (!chars) { + return str.replace(/^\s+/, ""); + } else { + return str.replace(new RegExp("^[" + chars + "]+"), ""); + } +}; + +/** @id MochiKit.Format.rstrip */ +MochiKit.Format.rstrip = function (str, /* optional */chars) { + str = str + ""; + if (typeof(str) != "string") { + return null; + } + if (!chars) { + return str.replace(/\s+$/, ""); + } else { + return str.replace(new RegExp("[" + chars + "]+$"), ""); + } +}; + +/** @id MochiKit.Format.strip */ +MochiKit.Format.strip = function (str, /* optional */chars) { + var self = MochiKit.Format; + return self.rstrip(self.lstrip(str, chars), chars); +}; + +/** @id MochiKit.Format.truncToFixed */ +MochiKit.Format.truncToFixed = function (aNumber, precision) { + aNumber = Math.floor(aNumber * Math.pow(10, precision)); + var res = (aNumber * Math.pow(10, -precision)).toFixed(precision); + if (res.charAt(0) == ".") { + res = "0" + res; + } + return res; +}; + +/** @id MochiKit.Format.roundToFixed */ +MochiKit.Format.roundToFixed = function (aNumber, precision) { + return MochiKit.Format.truncToFixed( + aNumber + 0.5 * Math.pow(10, -precision), + precision + ); +}; + +/** @id MochiKit.Format.percentFormat */ +MochiKit.Format.percentFormat = function (someFloat) { + return MochiKit.Format.twoDigitFloat(100 * someFloat) + '%'; +}; + +MochiKit.Format.EXPORT = [ + "truncToFixed", + "roundToFixed", + "numberFormatter", + "formatLocale", + "twoDigitAverage", + "twoDigitFloat", + "percentFormat", + "lstrip", + "rstrip", + "strip" +]; + +MochiKit.Format.LOCALE = { + en_US: {separator: ",", decimal: ".", percent: "%"}, + de_DE: {separator: ".", decimal: ",", percent: "%"}, + fr_FR: {separator: " ", decimal: ",", percent: "%"}, + "default": "en_US" +}; + +MochiKit.Format.EXPORT_OK = []; +MochiKit.Format.EXPORT_TAGS = { + ':all': MochiKit.Format.EXPORT, + ':common': MochiKit.Format.EXPORT +}; + +MochiKit.Format.__new__ = function () { + // MochiKit.Base.nameFunctions(this); + var base = this.NAME + "."; + var k, v, o; + for (k in this.LOCALE) { + o = this.LOCALE[k]; + if (typeof(o) == "object") { + o.repr = function () { return this.NAME; }; + o.NAME = base + "LOCALE." + k; + } + } + for (k in this) { + o = this[k]; + if (typeof(o) == 'function' && typeof(o.NAME) == 'undefined') { + try { + o.NAME = base + k; + } catch (e) { + // pass + } + } + } +}; + +MochiKit.Format.__new__(); + +if (typeof(MochiKit.Base) != "undefined") { + MochiKit.Base._exportSymbols(this, MochiKit.Format); +} else { + (function (globals, module) { + if ((typeof(JSAN) == 'undefined' && typeof(dojo) == 'undefined') + || (MochiKit.__export__ === false)) { + var all = module.EXPORT_TAGS[":all"]; + for (var i = 0; i < all.length; i++) { + globals[all[i]] = module[all[i]]; + } + } + })(this, MochiKit.Format); +} diff --git a/dom/tests/mochitest/ajax/mochikit/MochiKit/Iter.js b/dom/tests/mochitest/ajax/mochikit/MochiKit/Iter.js new file mode 100644 index 0000000000..c2fcbeee05 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/Iter.js @@ -0,0 +1,851 @@ +/*** + +MochiKit.Iter 1.4 + +See <http://mochikit.com/> for documentation, downloads, license, etc. + +(c) 2005 Bob Ippolito. All rights Reserved. + +***/ + +if (typeof(dojo) != 'undefined') { + dojo.provide('MochiKit.Iter'); + dojo.require('MochiKit.Base'); +} + +if (typeof(JSAN) != 'undefined') { + JSAN.use("MochiKit.Base", []); +} + +try { + if (typeof(MochiKit.Base) == 'undefined') { + throw ""; + } +} catch (e) { + throw "MochiKit.Iter depends on MochiKit.Base!"; +} + +if (typeof(MochiKit.Iter) == 'undefined') { + MochiKit.Iter = {}; +} + +MochiKit.Iter.NAME = "MochiKit.Iter"; +MochiKit.Iter.VERSION = "1.4"; +MochiKit.Base.update(MochiKit.Iter, { + __repr__: function () { + return "[" + this.NAME + " " + this.VERSION + "]"; + }, + toString: function () { + return this.__repr__(); + }, + + /** @id MochiKit.Iter.registerIteratorFactory */ + registerIteratorFactory: function (name, check, iterfactory, /* optional */ override) { + MochiKit.Iter.iteratorRegistry.register(name, check, iterfactory, override); + }, + + /** @id MochiKit.Iter.iter */ + iter: function (iterable, /* optional */ sentinel) { + var self = MochiKit.Iter; + if (arguments.length == 2) { + return self.takewhile( + function (a) { return a != sentinel; }, + iterable + ); + } + if (typeof(iterable.next) == 'function') { + return iterable; + } else if (typeof(iterable.iter) == 'function') { + return iterable.iter(); + /* + } else if (typeof(iterable.__iterator__) == 'function') { + // + // XXX: We can't support JavaScript 1.7 __iterator__ directly + // because of Object.prototype.__iterator__ + // + return iterable.__iterator__(); + */ + } + + try { + return self.iteratorRegistry.match(iterable); + } catch (e) { + var m = MochiKit.Base; + if (e == m.NotFound) { + e = new TypeError(typeof(iterable) + ": " + m.repr(iterable) + " is not iterable"); + } + throw e; + } + }, + + /** @id MochiKit.Iter.count */ + count: function (n) { + if (!n) { + n = 0; + } + var m = MochiKit.Base; + return { + repr: function () { return "count(" + n + ")"; }, + toString: m.forwardCall("repr"), + next: m.counter(n) + }; + }, + + /** @id MochiKit.Iter.cycle */ + cycle: function (p) { + var self = MochiKit.Iter; + var m = MochiKit.Base; + var lst = []; + var iterator = self.iter(p); + return { + repr: function () { return "cycle(...)"; }, + toString: m.forwardCall("repr"), + next: function () { + try { + var rval = iterator.next(); + lst.push(rval); + return rval; + } catch (e) { + if (e != self.StopIteration) { + throw e; + } + if (lst.length === 0) { + this.next = function () { + throw self.StopIteration; + }; + } else { + var i = -1; + this.next = function () { + i = (i + 1) % lst.length; + return lst[i]; + }; + } + return this.next(); + } + } + }; + }, + + /** @id MochiKit.Iter.repeat */ + repeat: function (elem, /* optional */n) { + var m = MochiKit.Base; + if (typeof(n) == 'undefined') { + return { + repr: function () { + return "repeat(" + m.repr(elem) + ")"; + }, + toString: m.forwardCall("repr"), + next: function () { + return elem; + } + }; + } + return { + repr: function () { + return "repeat(" + m.repr(elem) + ", " + n + ")"; + }, + toString: m.forwardCall("repr"), + next: function () { + if (n <= 0) { + throw MochiKit.Iter.StopIteration; + } + n -= 1; + return elem; + } + }; + }, + + /** @id MochiKit.Iter.next */ + next: function (iterator) { + return iterator.next(); + }, + + /** @id MochiKit.Iter.izip */ + izip: function (p, q/*, ...*/) { + var m = MochiKit.Base; + var self = MochiKit.Iter; + var next = self.next; + var iterables = m.map(self.iter, arguments); + return { + repr: function () { return "izip(...)"; }, + toString: m.forwardCall("repr"), + next: function () { return m.map(next, iterables); } + }; + }, + + /** @id MochiKit.Iter.ifilter */ + ifilter: function (pred, seq) { + var m = MochiKit.Base; + seq = MochiKit.Iter.iter(seq); + if (pred === null) { + pred = m.operator.truth; + } + return { + repr: function () { return "ifilter(...)"; }, + toString: m.forwardCall("repr"), + next: function () { + while (true) { + var rval = seq.next(); + if (pred(rval)) { + return rval; + } + } + // mozilla warnings aren't too bright + return undefined; + } + }; + }, + + /** @id MochiKit.Iter.ifilterfalse */ + ifilterfalse: function (pred, seq) { + var m = MochiKit.Base; + seq = MochiKit.Iter.iter(seq); + if (pred === null) { + pred = m.operator.truth; + } + return { + repr: function () { return "ifilterfalse(...)"; }, + toString: m.forwardCall("repr"), + next: function () { + while (true) { + var rval = seq.next(); + if (!pred(rval)) { + return rval; + } + } + // mozilla warnings aren't too bright + return undefined; + } + }; + }, + + /** @id MochiKit.Iter.islice */ + islice: function (seq/*, [start,] stop[, step] */) { + var self = MochiKit.Iter; + var m = MochiKit.Base; + seq = self.iter(seq); + var start = 0; + var stop = 0; + var step = 1; + var i = -1; + if (arguments.length == 2) { + stop = arguments[1]; + } else if (arguments.length == 3) { + start = arguments[1]; + stop = arguments[2]; + } else { + start = arguments[1]; + stop = arguments[2]; + step = arguments[3]; + } + return { + repr: function () { + return "islice(" + ["...", start, stop, step].join(", ") + ")"; + }, + toString: m.forwardCall("repr"), + next: function () { + var rval; + while (i < start) { + rval = seq.next(); + i++; + } + if (start >= stop) { + throw self.StopIteration; + } + start += step; + return rval; + } + }; + }, + + /** @id MochiKit.Iter.imap */ + imap: function (fun, p, q/*, ...*/) { + var m = MochiKit.Base; + var self = MochiKit.Iter; + var iterables = m.map(self.iter, m.extend(null, arguments, 1)); + var map = m.map; + var next = self.next; + return { + repr: function () { return "imap(...)"; }, + toString: m.forwardCall("repr"), + next: function () { + return fun.apply(this, map(next, iterables)); + } + }; + }, + + /** @id MochiKit.Iter.applymap */ + applymap: function (fun, seq, self) { + seq = MochiKit.Iter.iter(seq); + var m = MochiKit.Base; + return { + repr: function () { return "applymap(...)"; }, + toString: m.forwardCall("repr"), + next: function () { + return fun.apply(self, seq.next()); + } + }; + }, + + /** @id MochiKit.Iter.chain */ + chain: function (p, q/*, ...*/) { + // dumb fast path + var self = MochiKit.Iter; + var m = MochiKit.Base; + if (arguments.length == 1) { + return self.iter(arguments[0]); + } + var argiter = m.map(self.iter, arguments); + return { + repr: function () { return "chain(...)"; }, + toString: m.forwardCall("repr"), + next: function () { + while (argiter.length > 1) { + try { + return argiter[0].next(); + } catch (e) { + if (e != self.StopIteration) { + throw e; + } + argiter.shift(); + } + } + if (argiter.length == 1) { + // optimize last element + var arg = argiter.shift(); + this.next = m.bind("next", arg); + return this.next(); + } + throw self.StopIteration; + } + }; + }, + + /** @id MochiKit.Iter.takewhile */ + takewhile: function (pred, seq) { + var self = MochiKit.Iter; + seq = self.iter(seq); + return { + repr: function () { return "takewhile(...)"; }, + toString: MochiKit.Base.forwardCall("repr"), + next: function () { + var rval = seq.next(); + if (!pred(rval)) { + this.next = function () { + throw self.StopIteration; + }; + this.next(); + } + return rval; + } + }; + }, + + /** @id MochiKit.Iter.dropwhile */ + dropwhile: function (pred, seq) { + seq = MochiKit.Iter.iter(seq); + var m = MochiKit.Base; + var bind = m.bind; + return { + "repr": function () { return "dropwhile(...)"; }, + "toString": m.forwardCall("repr"), + "next": function () { + while (true) { + var rval = seq.next(); + if (!pred(rval)) { + break; + } + } + this.next = bind("next", seq); + return rval; + } + }; + }, + + _tee: function (ident, sync, iterable) { + sync.pos[ident] = -1; + var m = MochiKit.Base; + var listMin = m.listMin; + return { + repr: function () { return "tee(" + ident + ", ...)"; }, + toString: m.forwardCall("repr"), + next: function () { + var rval; + var i = sync.pos[ident]; + + if (i == sync.max) { + rval = iterable.next(); + sync.deque.push(rval); + sync.max += 1; + sync.pos[ident] += 1; + } else { + rval = sync.deque[i - sync.min]; + sync.pos[ident] += 1; + if (i == sync.min && listMin(sync.pos) != sync.min) { + sync.min += 1; + sync.deque.shift(); + } + } + return rval; + } + }; + }, + + /** @id MochiKit.Iter.tee */ + tee: function (iterable, n/* = 2 */) { + var rval = []; + var sync = { + "pos": [], + "deque": [], + "max": -1, + "min": -1 + }; + if (arguments.length == 1 || typeof(n) == "undefined" || n === null) { + n = 2; + } + var self = MochiKit.Iter; + iterable = self.iter(iterable); + var _tee = self._tee; + for (var i = 0; i < n; i++) { + rval.push(_tee(i, sync, iterable)); + } + return rval; + }, + + /** @id MochiKit.Iter.list */ + list: function (iterable) { + // Fast-path for Array and Array-like + var rval; + if (iterable instanceof Array) { + return iterable.slice(); + } + // this is necessary to avoid a Safari crash + if (typeof(iterable) == "function" && + !(iterable instanceof Function) && + typeof(iterable.length) == 'number') { + rval = []; + for (var i = 0; i < iterable.length; i++) { + rval.push(iterable[i]); + } + return rval; + } + + var self = MochiKit.Iter; + iterable = self.iter(iterable); + var rval = []; + try { + while (true) { + rval.push(iterable.next()); + } + } catch (e) { + if (e != self.StopIteration) { + throw e; + } + return rval; + } + // mozilla warnings aren't too bright + return undefined; + }, + + + /** @id MochiKit.Iter.reduce */ + reduce: function (fn, iterable, /* optional */initial) { + var i = 0; + var x = initial; + var self = MochiKit.Iter; + iterable = self.iter(iterable); + if (arguments.length < 3) { + try { + x = iterable.next(); + } catch (e) { + if (e == self.StopIteration) { + e = new TypeError("reduce() of empty sequence with no initial value"); + } + throw e; + } + i++; + } + try { + while (true) { + x = fn(x, iterable.next()); + } + } catch (e) { + if (e != self.StopIteration) { + throw e; + } + } + return x; + }, + + /** @id MochiKit.Iter.range */ + range: function (/* [start,] stop[, step] */) { + var start = 0; + var stop = 0; + var step = 1; + if (arguments.length == 1) { + stop = arguments[0]; + } else if (arguments.length == 2) { + start = arguments[0]; + stop = arguments[1]; + } else if (arguments.length == 3) { + start = arguments[0]; + stop = arguments[1]; + step = arguments[2]; + } else { + throw new TypeError("range() takes 1, 2, or 3 arguments!"); + } + if (step === 0) { + throw new TypeError("range() step must not be 0"); + } + return { + next: function () { + if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) { + throw MochiKit.Iter.StopIteration; + } + var rval = start; + start += step; + return rval; + }, + repr: function () { + return "range(" + [start, stop, step].join(", ") + ")"; + }, + toString: MochiKit.Base.forwardCall("repr") + }; + }, + + /** @id MochiKit.Iter.sum */ + sum: function (iterable, start/* = 0 */) { + if (typeof(start) == "undefined" || start === null) { + start = 0; + } + var x = start; + var self = MochiKit.Iter; + iterable = self.iter(iterable); + try { + while (true) { + x += iterable.next(); + } + } catch (e) { + if (e != self.StopIteration) { + throw e; + } + } + return x; + }, + + /** @id MochiKit.Iter.exhaust */ + exhaust: function (iterable) { + var self = MochiKit.Iter; + iterable = self.iter(iterable); + try { + while (true) { + iterable.next(); + } + } catch (e) { + if (e != self.StopIteration) { + throw e; + } + } + }, + + /** @id MochiKit.Iter.forEach */ + forEach: function (iterable, func, /* optional */self) { + var m = MochiKit.Base; + if (arguments.length > 2) { + func = m.bind(func, self); + } + // fast path for array + if (m.isArrayLike(iterable)) { + try { + for (var i = 0; i < iterable.length; i++) { + func(iterable[i]); + } + } catch (e) { + if (e != MochiKit.Iter.StopIteration) { + throw e; + } + } + } else { + self = MochiKit.Iter; + self.exhaust(self.imap(func, iterable)); + } + }, + + /** @id MochiKit.Iter.every */ + every: function (iterable, func) { + var self = MochiKit.Iter; + try { + self.ifilterfalse(func, iterable).next(); + return false; + } catch (e) { + if (e != self.StopIteration) { + throw e; + } + return true; + } + }, + + /** @id MochiKit.Iter.sorted */ + sorted: function (iterable, /* optional */cmp) { + var rval = MochiKit.Iter.list(iterable); + if (arguments.length == 1) { + cmp = MochiKit.Base.compare; + } + rval.sort(cmp); + return rval; + }, + + /** @id MochiKit.Iter.reversed */ + reversed: function (iterable) { + var rval = MochiKit.Iter.list(iterable); + rval.reverse(); + return rval; + }, + + /** @id MochiKit.Iter.some */ + some: function (iterable, func) { + var self = MochiKit.Iter; + try { + self.ifilter(func, iterable).next(); + return true; + } catch (e) { + if (e != self.StopIteration) { + throw e; + } + return false; + } + }, + + /** @id MochiKit.Iter.iextend */ + iextend: function (lst, iterable) { + if (MochiKit.Base.isArrayLike(iterable)) { + // fast-path for array-like + for (var i = 0; i < iterable.length; i++) { + lst.push(iterable[i]); + } + } else { + var self = MochiKit.Iter; + iterable = self.iter(iterable); + try { + while (true) { + lst.push(iterable.next()); + } + } catch (e) { + if (e != self.StopIteration) { + throw e; + } + } + } + return lst; + }, + + /** @id MochiKit.Iter.groupby */ + groupby: function(iterable, /* optional */ keyfunc) { + var m = MochiKit.Base; + var self = MochiKit.Iter; + if (arguments.length < 2) { + keyfunc = m.operator.identity; + } + iterable = self.iter(iterable); + + // shared + var pk = undefined; + var k = undefined; + var v; + + function fetch() { + v = iterable.next(); + k = keyfunc(v); + }; + + function eat() { + var ret = v; + v = undefined; + return ret; + }; + + var first = true; + var compare = m.compare; + return { + repr: function () { return "groupby(...)"; }, + next: function() { + // iterator-next + + // iterate until meet next group + while (compare(k, pk) === 0) { + fetch(); + if (first) { + first = false; + break; + } + } + pk = k; + return [k, { + next: function() { + // subiterator-next + if (v == undefined) { // Is there something to eat? + fetch(); + } + if (compare(k, pk) !== 0) { + throw self.StopIteration; + } + return eat(); + } + }]; + } + }; + }, + + /** @id MochiKit.Iter.groupby_as_array */ + groupby_as_array: function (iterable, /* optional */ keyfunc) { + var m = MochiKit.Base; + var self = MochiKit.Iter; + if (arguments.length < 2) { + keyfunc = m.operator.identity; + } + + iterable = self.iter(iterable); + var result = []; + var first = true; + var prev_key; + var compare = m.compare; + while (true) { + try { + var value = iterable.next(); + var key = keyfunc(value); + } catch (e) { + if (e == self.StopIteration) { + break; + } + throw e; + } + if (first || compare(key, prev_key) !== 0) { + var values = []; + result.push([key, values]); + } + values.push(value); + first = false; + prev_key = key; + } + return result; + }, + + /** @id MochiKit.Iter.arrayLikeIter */ + arrayLikeIter: function (iterable) { + var i = 0; + return { + repr: function () { return "arrayLikeIter(...)"; }, + toString: MochiKit.Base.forwardCall("repr"), + next: function () { + if (i >= iterable.length) { + throw MochiKit.Iter.StopIteration; + } + return iterable[i++]; + } + }; + }, + + /** @id MochiKit.Iter.hasIterateNext */ + hasIterateNext: function (iterable) { + return (iterable && typeof(iterable.iterateNext) == "function"); + }, + + /** @id MochiKit.Iter.iterateNextIter */ + iterateNextIter: function (iterable) { + return { + repr: function () { return "iterateNextIter(...)"; }, + toString: MochiKit.Base.forwardCall("repr"), + next: function () { + var rval = iterable.iterateNext(); + if (rval === null || rval === undefined) { + throw MochiKit.Iter.StopIteration; + } + return rval; + } + }; + } +}); + + +MochiKit.Iter.EXPORT_OK = [ + "iteratorRegistry", + "arrayLikeIter", + "hasIterateNext", + "iterateNextIter", +]; + +MochiKit.Iter.EXPORT = [ + "StopIteration", + "registerIteratorFactory", + "iter", + "count", + "cycle", + "repeat", + "next", + "izip", + "ifilter", + "ifilterfalse", + "islice", + "imap", + "applymap", + "chain", + "takewhile", + "dropwhile", + "tee", + "list", + "reduce", + "range", + "sum", + "exhaust", + "forEach", + "every", + "sorted", + "reversed", + "some", + "iextend", + "groupby", + "groupby_as_array" +]; + +MochiKit.Iter.__new__ = function () { + var m = MochiKit.Base; + // Re-use StopIteration if exists (e.g. SpiderMonkey) + if (typeof(StopIteration) != "undefined") { + this.StopIteration = StopIteration; + } else { + /** @id MochiKit.Iter.StopIteration */ + this.StopIteration = new m.NamedError("StopIteration"); + } + this.iteratorRegistry = new m.AdapterRegistry(); + // Register the iterator factory for arrays + this.registerIteratorFactory( + "arrayLike", + m.isArrayLike, + this.arrayLikeIter + ); + + this.registerIteratorFactory( + "iterateNext", + this.hasIterateNext, + this.iterateNextIter + ); + + this.EXPORT_TAGS = { + ":common": this.EXPORT, + ":all": m.concat(this.EXPORT, this.EXPORT_OK) + }; + + m.nameFunctions(this); + +}; + +MochiKit.Iter.__new__(); + +// +// XXX: Internet Explorer blows +// +if (MochiKit.__export__) { + reduce = MochiKit.Iter.reduce; +} + +MochiKit.Base._exportSymbols(this, MochiKit.Iter); diff --git a/dom/tests/mochitest/ajax/mochikit/MochiKit/Logging.js b/dom/tests/mochitest/ajax/mochikit/MochiKit/Logging.js new file mode 100644 index 0000000000..ac7678b5e6 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/Logging.js @@ -0,0 +1,321 @@ +/*** + +MochiKit.Logging 1.4 + +See <http://mochikit.com/> for documentation, downloads, license, etc. + +(c) 2005 Bob Ippolito. All rights Reserved. + +***/ + +if (typeof(dojo) != 'undefined') { + dojo.provide('MochiKit.Logging'); + dojo.require('MochiKit.Base'); +} + +if (typeof(JSAN) != 'undefined') { + JSAN.use("MochiKit.Base", []); +} + +try { + if (typeof(MochiKit.Base) == 'undefined') { + throw ""; + } +} catch (e) { + throw "MochiKit.Logging depends on MochiKit.Base!"; +} + +if (typeof(MochiKit.Logging) == 'undefined') { + MochiKit.Logging = {}; +} + +MochiKit.Logging.NAME = "MochiKit.Logging"; +MochiKit.Logging.VERSION = "1.4"; +MochiKit.Logging.__repr__ = function () { + return "[" + this.NAME + " " + this.VERSION + "]"; +}; + +MochiKit.Logging.toString = function () { + return this.__repr__(); +}; + + +MochiKit.Logging.EXPORT = [ + "LogLevel", + "LogMessage", + "Logger", + "alertListener", + "logger", + "log", + "logError", + "logDebug", + "logFatal", + "logWarning" +]; + + +MochiKit.Logging.EXPORT_OK = [ + "logLevelAtLeast", + "isLogMessage", + "compareLogMessage" +]; + + +/** @id MochiKit.Logging.LogMessage */ +MochiKit.Logging.LogMessage = function (num, level, info) { + this.num = num; + this.level = level; + this.info = info; + this.timestamp = new Date(); +}; + +MochiKit.Logging.LogMessage.prototype = { + /** @id MochiKit.Logging.LogMessage.prototype.repr */ + repr: function () { + var m = MochiKit.Base; + return 'LogMessage(' + + m.map( + m.repr, + [this.num, this.level, this.info] + ).join(', ') + ')'; + }, + /** @id MochiKit.Logging.LogMessage.prototype.toString */ + toString: MochiKit.Base.forwardCall("repr") +}; + +MochiKit.Base.update(MochiKit.Logging, { + /** @id MochiKit.Logging.logLevelAtLeast */ + logLevelAtLeast: function (minLevel) { + var self = MochiKit.Logging; + if (typeof(minLevel) == 'string') { + minLevel = self.LogLevel[minLevel]; + } + return function (msg) { + var msgLevel = msg.level; + if (typeof(msgLevel) == 'string') { + msgLevel = self.LogLevel[msgLevel]; + } + return msgLevel >= minLevel; + }; + }, + + /** @id MochiKit.Logging.isLogMessage */ + isLogMessage: function (/* ... */) { + var LogMessage = MochiKit.Logging.LogMessage; + for (var i = 0; i < arguments.length; i++) { + if (!(arguments[i] instanceof LogMessage)) { + return false; + } + } + return true; + }, + + /** @id MochiKit.Logging.compareLogMessage */ + compareLogMessage: function (a, b) { + return MochiKit.Base.compare([a.level, a.info], [b.level, b.info]); + }, + + /** @id MochiKit.Logging.alertListener */ + alertListener: function (msg) { + alert( + "num: " + msg.num + + "\nlevel: " + msg.level + + "\ninfo: " + msg.info.join(" ") + ); + } + +}); + +/** @id MochiKit.Logging.Logger */ +MochiKit.Logging.Logger = function (/* optional */maxSize) { + this.counter = 0; + if (typeof(maxSize) == 'undefined' || maxSize === null) { + maxSize = -1; + } + this.maxSize = maxSize; + this._messages = []; + this.listeners = {}; + this.useNativeConsole = false; +}; + +MochiKit.Logging.Logger.prototype = { + /** @id MochiKit.Logging.Logger.prototype.clear */ + clear: function () { + this._messages.splice(0, this._messages.length); + }, + + /** @id MochiKit.Logging.Logger.prototype.logToConsole */ + logToConsole: function (msg) { + if (typeof(window) != "undefined" && window.console + && window.console.log) { + // Safari and FireBug 0.4 + // Percent replacement is a workaround for cute Safari crashing bug + window.console.log(msg.replace(/%/g, '\uFF05')); + } else if (typeof(opera) != "undefined" && opera.postError) { + // Opera + opera.postError(msg); + } else if (typeof(printfire) == "function") { + // FireBug 0.3 and earlier + printfire(msg); + } else if (typeof(Debug) != "undefined" && Debug.writeln) { + // IE Web Development Helper (?) + // http://www.nikhilk.net/Entry.aspx?id=93 + Debug.writeln(msg); + } else if (typeof(debug) != "undefined" && debug.trace) { + // Atlas framework (?) + // http://www.nikhilk.net/Entry.aspx?id=93 + debug.trace(msg); + } + }, + + /** @id MochiKit.Logging.Logger.prototype.dispatchListeners */ + dispatchListeners: function (msg) { + for (var k in this.listeners) { + var pair = this.listeners[k]; + if (pair.ident != k || (pair[0] && !pair[0](msg))) { + continue; + } + pair[1](msg); + } + }, + + /** @id MochiKit.Logging.Logger.prototype.addListener */ + addListener: function (ident, filter, listener) { + if (typeof(filter) == 'string') { + filter = MochiKit.Logging.logLevelAtLeast(filter); + } + var entry = [filter, listener]; + entry.ident = ident; + this.listeners[ident] = entry; + }, + + /** @id MochiKit.Logging.Logger.prototype.removeListener */ + removeListener: function (ident) { + delete this.listeners[ident]; + }, + + /** @id MochiKit.Logging.Logger.prototype.baseLog */ + baseLog: function (level, message/*, ...*/) { + var msg = new MochiKit.Logging.LogMessage( + this.counter, + level, + MochiKit.Base.extend(null, arguments, 1) + ); + this._messages.push(msg); + this.dispatchListeners(msg); + if (this.useNativeConsole) { + this.logToConsole(msg.level + ": " + msg.info.join(" ")); + } + this.counter += 1; + while (this.maxSize >= 0 && this._messages.length > this.maxSize) { + this._messages.shift(); + } + }, + + /** @id MochiKit.Logging.Logger.prototype.getMessages */ + getMessages: function (howMany) { + var firstMsg = 0; + if (!(typeof(howMany) == 'undefined' || howMany === null)) { + firstMsg = Math.max(0, this._messages.length - howMany); + } + return this._messages.slice(firstMsg); + }, + + /** @id MochiKit.Logging.Logger.prototype.getMessageText */ + getMessageText: function (howMany) { + if (typeof(howMany) == 'undefined' || howMany === null) { + howMany = 30; + } + var messages = this.getMessages(howMany); + if (messages.length) { + var lst = map(function (m) { + return '\n [' + m.num + '] ' + m.level + ': ' + m.info.join(' '); + }, messages); + lst.unshift('LAST ' + messages.length + ' MESSAGES:'); + return lst.join(''); + } + return ''; + }, + + /** @id MochiKit.Logging.Logger.prototype.debuggingBookmarklet */ + debuggingBookmarklet: function (inline) { + if (typeof(MochiKit.LoggingPane) == "undefined") { + alert(this.getMessageText()); + } else { + MochiKit.LoggingPane.createLoggingPane(inline || false); + } + } +}; + +MochiKit.Logging.__new__ = function () { + this.LogLevel = { + ERROR: 40, + FATAL: 50, + WARNING: 30, + INFO: 20, + DEBUG: 10 + }; + + var m = MochiKit.Base; + m.registerComparator("LogMessage", + this.isLogMessage, + this.compareLogMessage + ); + + var partial = m.partial; + + var Logger = this.Logger; + var baseLog = Logger.prototype.baseLog; + m.update(this.Logger.prototype, { + debug: partial(baseLog, 'DEBUG'), + log: partial(baseLog, 'INFO'), + error: partial(baseLog, 'ERROR'), + fatal: partial(baseLog, 'FATAL'), + warning: partial(baseLog, 'WARNING') + }); + + // indirectly find logger so it can be replaced + var self = this; + var connectLog = function (name) { + return function () { + self.logger[name].apply(self.logger, arguments); + }; + }; + + /** @id MochiKit.Logging.log */ + this.log = connectLog('log'); + /** @id MochiKit.Logging.logError */ + this.logError = connectLog('error'); + /** @id MochiKit.Logging.logDebug */ + this.logDebug = connectLog('debug'); + /** @id MochiKit.Logging.logFatal */ + this.logFatal = connectLog('fatal'); + /** @id MochiKit.Logging.logWarning */ + this.logWarning = connectLog('warning'); + this.logger = new Logger(); + this.logger.useNativeConsole = true; + + this.EXPORT_TAGS = { + ":common": this.EXPORT, + ":all": m.concat(this.EXPORT, this.EXPORT_OK) + }; + + m.nameFunctions(this); + +}; + +if (typeof(printfire) == "undefined" && + typeof(document) != "undefined" && document.createEvent && + typeof(dispatchEvent) != "undefined") { + // FireBug really should be less stupid about this global function + printfire = function () { + printfire.args = arguments; + var ev = document.createEvent("Events"); + ev.initEvent("printfire", false, true); + dispatchEvent(ev); + }; +} + +MochiKit.Logging.__new__(); + +MochiKit.Base._exportSymbols(this, MochiKit.Logging); diff --git a/dom/tests/mochitest/ajax/mochikit/MochiKit/LoggingPane.js b/dom/tests/mochitest/ajax/mochikit/MochiKit/LoggingPane.js new file mode 100644 index 0000000000..3798ae404e --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/LoggingPane.js @@ -0,0 +1,374 @@ +/*** + +MochiKit.LoggingPane 1.4 + +See <http://mochikit.com/> for documentation, downloads, license, etc. + +(c) 2005 Bob Ippolito. All rights Reserved. + +***/ + +if (typeof(dojo) != 'undefined') { + dojo.provide('MochiKit.LoggingPane'); + dojo.require('MochiKit.Logging'); + dojo.require('MochiKit.Base'); +} + +if (typeof(JSAN) != 'undefined') { + JSAN.use("MochiKit.Logging", []); + JSAN.use("MochiKit.Base", []); +} + +try { + if (typeof(MochiKit.Base) == 'undefined' || typeof(MochiKit.Logging) == 'undefined') { + throw ""; + } +} catch (e) { + throw "MochiKit.LoggingPane depends on MochiKit.Base and MochiKit.Logging!"; +} + +if (typeof(MochiKit.LoggingPane) == 'undefined') { + MochiKit.LoggingPane = {}; +} + +MochiKit.LoggingPane.NAME = "MochiKit.LoggingPane"; +MochiKit.LoggingPane.VERSION = "1.4"; +MochiKit.LoggingPane.__repr__ = function () { + return "[" + this.NAME + " " + this.VERSION + "]"; +}; + +MochiKit.LoggingPane.toString = function () { + return this.__repr__(); +}; + +/** @id MochiKit.LoggingPane.createLoggingPane */ +MochiKit.LoggingPane.createLoggingPane = function (inline/* = false */) { + var m = MochiKit.LoggingPane; + inline = !(!inline); + if (m._loggingPane && m._loggingPane.inline != inline) { + m._loggingPane.closePane(); + m._loggingPane = null; + } + if (!m._loggingPane || m._loggingPane.closed) { + m._loggingPane = new m.LoggingPane(inline, MochiKit.Logging.logger); + } + return m._loggingPane; +}; + +/** @id MochiKit.LoggingPane.LoggingPane */ +MochiKit.LoggingPane.LoggingPane = function (inline/* = false */, logger/* = MochiKit.Logging.logger */) { + + /* Use a div if inline, pop up a window if not */ + /* Create the elements */ + if (typeof(logger) == "undefined" || logger === null) { + logger = MochiKit.Logging.logger; + } + this.logger = logger; + var update = MochiKit.Base.update; + var updatetree = MochiKit.Base.updatetree; + var bind = MochiKit.Base.bind; + var clone = MochiKit.Base.clone; + var win = window; + var uid = "_MochiKit_LoggingPane"; + if (typeof(MochiKit.DOM) != "undefined") { + win = MochiKit.DOM.currentWindow(); + } + if (!inline) { + // name the popup with the base URL for uniqueness + var url = win.location.href.split("?")[0].replace(/[#:\/.><&-]/g, "_"); + var name = uid + "_" + url; + var nwin = win.open("", name, "dependent,resizable,height=200"); + if (!nwin) { + alert("Not able to open debugging window due to pop-up blocking."); + return undefined; + } + nwin.document.write( + '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ' + + '"http://www.w3.org/TR/html4/loose.dtd">' + + '<html><head><title>[MochiKit.LoggingPane]</title></head>' + + '<body></body></html>' + ); + nwin.document.close(); + nwin.document.title += ' ' + win.document.title; + win = nwin; + } + var doc = win.document; + this.doc = doc; + + // Connect to the debug pane if it already exists (i.e. in a window orphaned by the page being refreshed) + var debugPane = doc.getElementById(uid); + var existing_pane = !!debugPane; + if (debugPane && typeof(debugPane.loggingPane) != "undefined") { + debugPane.loggingPane.logger = this.logger; + debugPane.loggingPane.buildAndApplyFilter(); + return debugPane.loggingPane; + } + + if (existing_pane) { + // clear any existing contents + var child; + while ((child = debugPane.firstChild)) { + debugPane.removeChild(child); + } + } else { + debugPane = doc.createElement("div"); + debugPane.id = uid; + } + debugPane.loggingPane = this; + var levelFilterField = doc.createElement("input"); + var infoFilterField = doc.createElement("input"); + var filterButton = doc.createElement("button"); + var loadButton = doc.createElement("button"); + var clearButton = doc.createElement("button"); + var closeButton = doc.createElement("button"); + var logPaneArea = doc.createElement("div"); + var logPane = doc.createElement("div"); + + /* Set up the functions */ + var listenerId = uid + "_Listener"; + this.colorTable = clone(this.colorTable); + var messages = []; + var messageFilter = null; + + /** @id MochiKit.LoggingPane.messageLevel */ + var messageLevel = function (msg) { + var level = msg.level; + if (typeof(level) == "number") { + level = MochiKit.Logging.LogLevel[level]; + } + return level; + }; + + /** @id MochiKit.LoggingPane.messageText */ + var messageText = function (msg) { + return msg.info.join(" "); + }; + + /** @id MochiKit.LoggingPane.addMessageText */ + var addMessageText = bind(function (msg) { + var level = messageLevel(msg); + var text = messageText(msg); + var c = this.colorTable[level]; + var p = doc.createElement("span"); + p.className = "MochiKit-LogMessage MochiKit-LogLevel-" + level; + p.style.cssText = "margin: 0px; white-space: -moz-pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; white-space: pre-line; word-wrap: break-word; wrap-option: emergency; color: " + c; + p.appendChild(doc.createTextNode(level + ": " + text)); + logPane.appendChild(p); + logPane.appendChild(doc.createElement("br")); + if (logPaneArea.offsetHeight > logPaneArea.scrollHeight) { + logPaneArea.scrollTop = 0; + } else { + logPaneArea.scrollTop = logPaneArea.scrollHeight; + } + }, this); + + /** @id MochiKit.LoggingPane.addMessage */ + var addMessage = function (msg) { + messages[messages.length] = msg; + addMessageText(msg); + }; + + /** @id MochiKit.LoggingPane.buildMessageFilter */ + var buildMessageFilter = function () { + var levelre, infore; + try { + /* Catch any exceptions that might arise due to invalid regexes */ + levelre = new RegExp(levelFilterField.value); + infore = new RegExp(infoFilterField.value); + } catch(e) { + /* If there was an error with the regexes, do no filtering */ + logDebug("Error in filter regex: " + e.message); + return null; + } + + return function (msg) { + return ( + levelre.test(messageLevel(msg)) && + infore.test(messageText(msg)) + ); + }; + }; + + /** @id MochiKit.LoggingPane.clearMessagePane */ + var clearMessagePane = function () { + while (logPane.firstChild) { + logPane.removeChild(logPane.firstChild); + } + }; + + /** @id MochiKit.LoggingPane.clearMessages */ + var clearMessages = function () { + messages = []; + clearMessagePane(); + }; + + /** @id MochiKit.LoggingPane.closePane */ + var closePane = bind(function () { + if (this.closed) { + return; + } + this.closed = true; + if (MochiKit.LoggingPane._loggingPane == this) { + MochiKit.LoggingPane._loggingPane = null; + } + this.logger.removeListener(listenerId); + try { + try { + debugPane.loggingPane = null; + } catch(e) { logFatal("Bookmarklet was closed incorrectly."); } + if (inline) { + debugPane.parentNode.removeChild(debugPane); + } else { + this.win.close(); + } + } catch(e) {} + }, this); + + /** @id MochiKit.LoggingPane.filterMessages */ + var filterMessages = function () { + clearMessagePane(); + + for (var i = 0; i < messages.length; i++) { + var msg = messages[i]; + if (messageFilter === null || messageFilter(msg)) { + addMessageText(msg); + } + } + }; + + this.buildAndApplyFilter = function () { + messageFilter = buildMessageFilter(); + + filterMessages(); + + this.logger.removeListener(listenerId); + this.logger.addListener(listenerId, messageFilter, addMessage); + }; + + + /** @id MochiKit.LoggingPane.loadMessages */ + var loadMessages = bind(function () { + messages = this.logger.getMessages(); + filterMessages(); + }, this); + + /** @id MochiKit.LoggingPane.filterOnEnter */ + var filterOnEnter = bind(function (event) { + event = event || window.event; + key = event.which || event.keyCode; + if (key == 13) { + this.buildAndApplyFilter(); + } + }, this); + + /* Create the debug pane */ + var style = "display: block; z-index: 1000; left: 0px; bottom: 0px; position: fixed; width: 100%; background-color: white; font: " + this.logFont; + if (inline) { + style += "; height: 10em; border-top: 2px solid black"; + } else { + style += "; height: 100%;"; + } + debugPane.style.cssText = style; + + if (!existing_pane) { + doc.body.appendChild(debugPane); + } + + /* Create the filter fields */ + style = {"cssText": "width: 33%; display: inline; font: " + this.logFont}; + + updatetree(levelFilterField, { + "value": "FATAL|ERROR|WARNING|INFO|DEBUG", + "onkeypress": filterOnEnter, + "style": style + }); + debugPane.appendChild(levelFilterField); + + updatetree(infoFilterField, { + "value": ".*", + "onkeypress": filterOnEnter, + "style": style + }); + debugPane.appendChild(infoFilterField); + + /* Create the buttons */ + style = "width: 8%; display:inline; font: " + this.logFont; + + filterButton.appendChild(doc.createTextNode("Filter")); + filterButton.onclick = bind("buildAndApplyFilter", this); + filterButton.style.cssText = style; + debugPane.appendChild(filterButton); + + loadButton.appendChild(doc.createTextNode("Load")); + loadButton.onclick = loadMessages; + loadButton.style.cssText = style; + debugPane.appendChild(loadButton); + + clearButton.appendChild(doc.createTextNode("Clear")); + clearButton.onclick = clearMessages; + clearButton.style.cssText = style; + debugPane.appendChild(clearButton); + + closeButton.appendChild(doc.createTextNode("Close")); + closeButton.onclick = closePane; + closeButton.style.cssText = style; + debugPane.appendChild(closeButton); + + /* Create the logging pane */ + logPaneArea.style.cssText = "overflow: auto; width: 100%"; + logPane.style.cssText = "width: 100%; height: " + (inline ? "8em" : "100%"); + + logPaneArea.appendChild(logPane); + debugPane.appendChild(logPaneArea); + + this.buildAndApplyFilter(); + loadMessages(); + + if (inline) { + this.win = undefined; + } else { + this.win = win; + } + this.inline = inline; + this.closePane = closePane; + this.closed = false; + + + return this; +}; + +MochiKit.LoggingPane.LoggingPane.prototype = { + "logFont": "8pt Verdana,sans-serif", + "colorTable": { + "ERROR": "red", + "FATAL": "darkred", + "WARNING": "blue", + "INFO": "black", + "DEBUG": "green" + } +}; + + +MochiKit.LoggingPane.EXPORT_OK = [ + "LoggingPane" +]; + +MochiKit.LoggingPane.EXPORT = [ + "createLoggingPane" +]; + +MochiKit.LoggingPane.__new__ = function () { + this.EXPORT_TAGS = { + ":common": this.EXPORT, + ":all": MochiKit.Base.concat(this.EXPORT, this.EXPORT_OK) + }; + + MochiKit.Base.nameFunctions(this); + + MochiKit.LoggingPane._loggingPane = null; + +}; + +MochiKit.LoggingPane.__new__(); + +MochiKit.Base._exportSymbols(this, MochiKit.LoggingPane); diff --git a/dom/tests/mochitest/ajax/mochikit/MochiKit/MochiKit.js b/dom/tests/mochitest/ajax/mochikit/MochiKit/MochiKit.js new file mode 100644 index 0000000000..3a13b241fe --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/MochiKit.js @@ -0,0 +1,154 @@ +/*** + +MochiKit.MochiKit 1.4 + +See <http://mochikit.com/> for documentation, downloads, license, etc. + +(c) 2005 Bob Ippolito. All rights Reserved. + +***/ + +if (typeof(MochiKit) == 'undefined') { + MochiKit = {}; +} + +if (typeof(MochiKit.MochiKit) == 'undefined') { + /** @id MochiKit.MochiKit */ + MochiKit.MochiKit = {}; +} + +MochiKit.MochiKit.NAME = "MochiKit.MochiKit"; +MochiKit.MochiKit.VERSION = "1.4"; +MochiKit.MochiKit.__repr__ = function () { + return "[" + this.NAME + " " + this.VERSION + "]"; +}; + +/** @id MochiKit.MochiKit.toString */ +MochiKit.MochiKit.toString = function () { + return this.__repr__(); +}; + +/** @id MochiKit.MochiKit.SUBMODULES */ +MochiKit.MochiKit.SUBMODULES = [ + "Base", + "Iter", + "Logging", + "DateTime", + "Format", + "Async", + "DOM", + "Selector", + "Style", + "LoggingPane", + "Color", + "Signal", + "Position", + "Visual" +]; + +if (typeof(JSAN) != 'undefined' || typeof(dojo) != 'undefined') { + if (typeof(dojo) != 'undefined') { + dojo.provide('MochiKit.MochiKit'); + dojo.require("MochiKit.*"); + } + if (typeof(JSAN) != 'undefined') { + (function (lst) { + for (var i = 0; i < lst.length; i++) { + JSAN.use("MochiKit." + lst[i], []); + } + })(MochiKit.MochiKit.SUBMODULES); + } + (function () { + var extend = MochiKit.Base.extend; + var self = MochiKit.MochiKit; + var modules = self.SUBMODULES; + var EXPORT = []; + var EXPORT_OK = []; + var EXPORT_TAGS = {}; + var i, k, m, all; + for (i = 0; i < modules.length; i++) { + m = MochiKit[modules[i]]; + extend(EXPORT, m.EXPORT); + extend(EXPORT_OK, m.EXPORT_OK); + for (k in m.EXPORT_TAGS) { + EXPORT_TAGS[k] = extend(EXPORT_TAGS[k], m.EXPORT_TAGS[k]); + } + all = m.EXPORT_TAGS[":all"]; + if (!all) { + all = extend(null, m.EXPORT, m.EXPORT_OK); + } + var j; + for (j = 0; j < all.length; j++) { + k = all[j]; + self[k] = m[k]; + } + } + self.EXPORT = EXPORT; + self.EXPORT_OK = EXPORT_OK; + self.EXPORT_TAGS = EXPORT_TAGS; + }()); + +} else { + if (typeof(MochiKit.__compat__) == 'undefined') { + MochiKit.__compat__ = true; + } + (function () { + if (typeof(document) == "undefined") { + return; + } + var scripts = document.getElementsByTagName("script"); + var kXULNSURI = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; + var base = null; + var baseElem = null; + var allScripts = {}; + var i; + for (i = 0; i < scripts.length; i++) { + var src = scripts[i].getAttribute("src"); + if (!src) { + continue; + } + allScripts[src] = true; + if (src.match(/MochiKit.js$/)) { + base = src.substring(0, src.lastIndexOf('MochiKit.js')); + baseElem = scripts[i]; + } + } + if (base === null) { + return; + } + var modules = MochiKit.MochiKit.SUBMODULES; + for (var i = 0; i < modules.length; i++) { + if (MochiKit[modules[i]]) { + continue; + } + var uri = base + modules[i] + '.js'; + if (uri in allScripts) { + continue; + } + if (document.documentElement && + document.documentElement.namespaceURI == kXULNSURI) { + // XUL + var s = document.createElementNS(kXULNSURI, 'script'); + s.setAttribute("id", "MochiKit_" + base + modules[i]); + s.setAttribute("src", uri); + s.setAttribute("type", "application/x-javascript"); + baseElem.parentNode.appendChild(s); + } else { + // HTML + /* + DOM can not be used here because Safari does + deferred loading of scripts unless they are + in the document or inserted with document.write + + This is not XHTML compliant. If you want XHTML + compliance then you must use the packed version of MochiKit + or include each script individually (basically unroll + these document.write calls into your XHTML source) + + */ + document.write('<script src="' + uri + + '" type="text/javascript"></script>'); + } + }; + })(); +} diff --git a/dom/tests/mochitest/ajax/mochikit/MochiKit/MockDOM.js b/dom/tests/mochitest/ajax/mochikit/MochiKit/MockDOM.js new file mode 100644 index 0000000000..92558cb7bf --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/MockDOM.js @@ -0,0 +1,115 @@ +/*** + +MochiKit.MockDOM 1.4 + +See <http://mochikit.com/> for documentation, downloads, license, etc. + +(c) 2005 Bob Ippolito. All rights Reserved. + +***/ + +if (typeof(MochiKit) == "undefined") { + MochiKit = {}; +} + +if (typeof(MochiKit.MockDOM) == "undefined") { + MochiKit.MockDOM = {}; +} + +MochiKit.MockDOM.NAME = "MochiKit.MockDOM"; +MochiKit.MockDOM.VERSION = "1.4"; + +MochiKit.MockDOM.__repr__ = function () { + return "[" + this.NAME + " " + this.VERSION + "]"; +}; + +/** @id MochiKit.MockDOM.toString */ +MochiKit.MockDOM.toString = function () { + return this.__repr__(); +}; + +/** @id MochiKit.MockDOM.createDocument */ +MochiKit.MockDOM.createDocument = function () { + var doc = new MochiKit.MockDOM.MockElement("DOCUMENT"); + doc.body = doc.createElement("BODY"); + doc.appendChild(doc.body); + return doc; +}; + +/** @id MochiKit.MockDOM.MockElement */ +MochiKit.MockDOM.MockElement = function (name, data, ownerDocument) { + this.tagName = this.nodeName = name.toUpperCase(); + this.ownerDocument = ownerDocument || null; + if (name == "DOCUMENT") { + this.nodeType = 9; + this.childNodes = []; + } else if (typeof(data) == "string") { + this.nodeValue = data; + this.nodeType = 3; + } else { + this.nodeType = 1; + this.childNodes = []; + } + if (name.substring(0, 1) == "<") { + var nameattr = name.substring( + name.indexOf('"') + 1, name.lastIndexOf('"')); + name = name.substring(1, name.indexOf(" ")); + this.tagName = this.nodeName = name.toUpperCase(); + this.setAttribute("name", nameattr); + } +}; + +MochiKit.MockDOM.MockElement.prototype = { + /** @id MochiKit.MockDOM.MockElement.prototype.createElement */ + createElement: function (tagName) { + return new MochiKit.MockDOM.MockElement(tagName, null, this.nodeType == 9 ? this : this.ownerDocument); + }, + /** @id MochiKit.MockDOM.MockElement.prototype.createTextNode */ + createTextNode: function (text) { + return new MochiKit.MockDOM.MockElement("text", text, this.nodeType == 9 ? this : this.ownerDocument); + }, + /** @id MochiKit.MockDOM.MockElement.prototype.setAttribute */ + setAttribute: function (name, value) { + this[name] = value; + }, + /** @id MochiKit.MockDOM.MockElement.prototype.getAttribute */ + getAttribute: function (name) { + return this[name]; + }, + /** @id MochiKit.MockDOM.MockElement.prototype.appendChild */ + appendChild: function (child) { + this.childNodes.push(child); + }, + /** @id MochiKit.MockDOM.MockElement.prototype.toString */ + toString: function () { + return "MockElement(" + this.tagName + ")"; + }, + /** @id MochiKit.MockDOM.MockElement.prototype.getElementsByTagName */ + getElementsByTagName: function (tagName) { + var foundElements = []; + MochiKit.Base.nodeWalk(this, function(node){ + if (tagName == '*' || tagName == node.tagName) { + foundElements.push(node); + return node.childNodes; + } + }); + return foundElements; + } +}; + + /** @id MochiKit.MockDOM.EXPORT_OK */ +MochiKit.MockDOM.EXPORT_OK = [ + "mockElement", + "createDocument" +]; + + /** @id MochiKit.MockDOM.EXPORT */ +MochiKit.MockDOM.EXPORT = [ + "document" +]; + +MochiKit.MockDOM.__new__ = function () { + this.document = this.createDocument(); +}; + +MochiKit.MockDOM.__new__(); diff --git a/dom/tests/mochitest/ajax/mochikit/MochiKit/Position.js b/dom/tests/mochitest/ajax/mochikit/MochiKit/Position.js new file mode 100644 index 0000000000..23b0f181ce --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/Position.js @@ -0,0 +1,258 @@ +/*** + +MochiKit.Position 1.4 + +See <http://mochikit.com/> for documentation, downloads, license, etc. + +(c) 2005-2006 Bob Ippolito and others. All rights Reserved. + +***/ + +if (typeof(dojo) != 'undefined') { + dojo.provide('MochiKit.Position'); + dojo.require('MochiKit.Base'); + dojo.require('MochiKit.DOM'); + dojo.require('MochiKit.Style'); +} +if (typeof(JSAN) != 'undefined') { + JSAN.use('MochiKit.Base', []); + JSAN.use('MochiKit.DOM', []); + JSAN.use('MochiKit.Style', []); +} + +try { + if (typeof(MochiKit.Base) == 'undefined' || + typeof(MochiKit.Style) == 'undefined' || + typeof(MochiKit.DOM) == 'undefined') { + throw ''; + } +} catch (e) { + throw 'MochiKit.Style depends on MochiKit.Base, MochiKit.DOM, and MochiKit.Style!'; +} + +if (typeof(MochiKit.Position) == 'undefined') { + MochiKit.Position = {}; +} + +MochiKit.Position.NAME = 'MochiKit.Position'; +MochiKit.Position.VERSION = '1.4'; +MochiKit.Position.__repr__ = function () { + return '[' + this.NAME + ' ' + this.VERSION + ']'; +}; +MochiKit.Position.toString = function () { + return this.__repr__(); +}; + +MochiKit.Position.EXPORT_OK = []; + +MochiKit.Position.EXPORT = [ +]; + + +MochiKit.Base.update(MochiKit.Position, { + // set to true if needed, warning: firefox performance problems + // NOT neeeded for page scrolling, only if draggable contained in + // scrollable elements + includeScrollOffsets: false, + + /** @id MochiKit.Position.prepare */ + prepare: function () { + var deltaX = window.pageXOffset + || document.documentElement.scrollLeft + || document.body.scrollLeft + || 0; + var deltaY = window.pageYOffset + || document.documentElement.scrollTop + || document.body.scrollTop + || 0; + this.windowOffset = new MochiKit.Style.Coordinates(deltaX, deltaY); + }, + + /** @id MochiKit.Position.cumulativeOffset */ + cumulativeOffset: function (element) { + var valueT = 0; + var valueL = 0; + do { + valueT += element.offsetTop || 0; + valueL += element.offsetLeft || 0; + element = element.offsetParent; + } while (element); + return new MochiKit.Style.Coordinates(valueL, valueT); + }, + + /** @id MochiKit.Position.realOffset */ + realOffset: function (element) { + var valueT = 0; + var valueL = 0; + do { + valueT += element.scrollTop || 0; + valueL += element.scrollLeft || 0; + element = element.parentNode; + } while (element); + return new MochiKit.Style.Coordinates(valueL, valueT); + }, + + /** @id MochiKit.Position.within */ + within: function (element, x, y) { + if (this.includeScrollOffsets) { + return this.withinIncludingScrolloffsets(element, x, y); + } + this.xcomp = x; + this.ycomp = y; + this.offset = this.cumulativeOffset(element); + if (element.style.position == "fixed") { + this.offset.x += this.windowOffset.x; + this.offset.y += this.windowOffset.y; + } + + return (y >= this.offset.y && + y < this.offset.y + element.offsetHeight && + x >= this.offset.x && + x < this.offset.x + element.offsetWidth); + }, + + /** @id MochiKit.Position.withinIncludingScrolloffsets */ + withinIncludingScrolloffsets: function (element, x, y) { + var offsetcache = this.realOffset(element); + + this.xcomp = x + offsetcache.x - this.windowOffset.x; + this.ycomp = y + offsetcache.y - this.windowOffset.y; + this.offset = this.cumulativeOffset(element); + + return (this.ycomp >= this.offset.y && + this.ycomp < this.offset.y + element.offsetHeight && + this.xcomp >= this.offset.x && + this.xcomp < this.offset.x + element.offsetWidth); + }, + + // within must be called directly before + /** @id MochiKit.Position.overlap */ + overlap: function (mode, element) { + if (!mode) { + return 0; + } + if (mode == 'vertical') { + return ((this.offset.y + element.offsetHeight) - this.ycomp) / + element.offsetHeight; + } + if (mode == 'horizontal') { + return ((this.offset.x + element.offsetWidth) - this.xcomp) / + element.offsetWidth; + } + }, + + /** @id MochiKit.Position.absolutize */ + absolutize: function (element) { + element = MochiKit.DOM.getElement(element); + if (element.style.position == 'absolute') { + return; + } + MochiKit.Position.prepare(); + + var offsets = MochiKit.Position.positionedOffset(element); + var width = element.clientWidth; + var height = element.clientHeight; + + var oldStyle = { + 'position': element.style.position, + 'left': offsets.x - parseFloat(element.style.left || 0), + 'top': offsets.y - parseFloat(element.style.top || 0), + 'width': element.style.width, + 'height': element.style.height + }; + + element.style.position = 'absolute'; + element.style.top = offsets.y + 'px'; + element.style.left = offsets.x + 'px'; + element.style.width = width + 'px'; + element.style.height = height + 'px'; + + return oldStyle; + }, + + /** @id MochiKit.Position.positionedOffset */ + positionedOffset: function (element) { + var valueT = 0, valueL = 0; + do { + valueT += element.offsetTop || 0; + valueL += element.offsetLeft || 0; + element = element.offsetParent; + if (element) { + p = MochiKit.Style.getStyle(element, 'position'); + if (p == 'relative' || p == 'absolute') { + break; + } + } + } while (element); + return new MochiKit.Style.Coordinates(valueL, valueT); + }, + + /** @id MochiKit.Position.relativize */ + relativize: function (element, oldPos) { + element = MochiKit.DOM.getElement(element); + if (element.style.position == 'relative') { + return; + } + MochiKit.Position.prepare(); + + var top = parseFloat(element.style.top || 0) - + (oldPos['top'] || 0); + var left = parseFloat(element.style.left || 0) - + (oldPos['left'] || 0); + + element.style.position = oldPos['position']; + element.style.top = top + 'px'; + element.style.left = left + 'px'; + element.style.width = oldPos['width']; + element.style.height = oldPos['height']; + }, + + /** @id MochiKit.Position.clone */ + clone: function (source, target) { + source = MochiKit.DOM.getElement(source); + target = MochiKit.DOM.getElement(target); + target.style.position = 'absolute'; + var offsets = this.cumulativeOffset(source); + target.style.top = offsets.y + 'px'; + target.style.left = offsets.x + 'px'; + target.style.width = source.offsetWidth + 'px'; + target.style.height = source.offsetHeight + 'px'; + }, + + /** @id MochiKit.Position.page */ + page: function (forElement) { + var valueT = 0; + var valueL = 0; + + var element = forElement; + do { + valueT += element.offsetTop || 0; + valueL += element.offsetLeft || 0; + + // Safari fix + if (element.offsetParent == document.body && MochiKit.Style.getStyle(element, 'position') == 'absolute') { + break; + } + } while (element = element.offsetParent); + + element = forElement; + do { + valueT -= element.scrollTop || 0; + valueL -= element.scrollLeft || 0; + } while (element = element.parentNode); + + return new MochiKit.Style.Coordinates(valueL, valueT); + } +}); + +MochiKit.Position.__new__ = function (win) { + var m = MochiKit.Base; + this.EXPORT_TAGS = { + ':common': this.EXPORT, + ':all': m.concat(this.EXPORT, this.EXPORT_OK) + }; + + m.nameFunctions(this); +}; + +MochiKit.Position.__new__(this);
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/mochikit/MochiKit/Selector.js b/dom/tests/mochitest/ajax/mochikit/MochiKit/Selector.js new file mode 100644 index 0000000000..f5d1afac31 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/Selector.js @@ -0,0 +1,431 @@ +/*** + +MochiKit.Selector 1.4 + +See <http://mochikit.com/> for documentation, downloads, license, etc. + +(c) 2005 Bob Ippolito and others. All rights Reserved. + +***/ + +if (typeof(dojo) != 'undefined') { + dojo.provide('MochiKit.Selector'); + dojo.require('MochiKit.Base'); + dojo.require('MochiKit.DOM'); + dojo.require('MochiKit.Iter'); +} + +if (typeof(JSAN) != 'undefined') { + JSAN.use("MochiKit.Base", []); + JSAN.use("MochiKit.DOM", []); + JSAN.use("MochiKit.Iter", []); +} + +try { + if (typeof(MochiKit.Base) === 'undefined' || + typeof(MochiKit.DOM) === 'undefined' || + typeof(MochiKit.Iter) === 'undefined') { + throw ""; + } +} catch (e) { + throw "MochiKit.Selector depends on MochiKit.Base, MochiKit.DOM and MochiKit.Iter!"; +} + +if (typeof(MochiKit.Selector) == 'undefined') { + MochiKit.Selector = {}; +} + +MochiKit.Selector.NAME = "MochiKit.Selector"; + +MochiKit.Selector.VERSION = "1.4"; + +MochiKit.Selector.__repr__ = function () { + return "[" + this.NAME + " " + this.VERSION + "]"; +}; + +MochiKit.Selector.toString = function () { + return this.__repr__(); +}; + +MochiKit.Selector.EXPORT = [ + "Selector", + "findChildElements", + "findDocElements", + "$$" +]; + +MochiKit.Selector.EXPORT_OK = [ +]; + +MochiKit.Selector.Selector = function (expression) { + this.params = {classNames: [], pseudoClassNames: []}; + this.expression = expression.toString().replace(/(^\s+|\s+$)/g, ''); + this.parseExpression(); + this.compileMatcher(); +}; + +MochiKit.Selector.Selector.prototype = { + /*** + + Selector class: convenient object to make CSS selections. + + ***/ + __class__: MochiKit.Selector.Selector, + + /** @id MochiKit.Selector.Selector.prototype.parseExpression */ + parseExpression: function () { + function abort(message) { + throw 'Parse error in selector: ' + message; + } + + if (this.expression == '') { + abort('empty expression'); + } + + var repr = MochiKit.Base.repr; + var params = this.params; + var expr = this.expression; + var match, modifier, clause, rest; + while (match = expr.match(/^(.*)\[([a-z0-9_:-]+?)(?:([~\|!^$*]?=)(?:"([^"]*)"|([^\]\s]*)))?\]$/i)) { + params.attributes = params.attributes || []; + params.attributes.push({name: match[2], operator: match[3], value: match[4] || match[5] || ''}); + expr = match[1]; + } + + if (expr == '*') { + return this.params.wildcard = true; + } + + while (match = expr.match(/^([^a-z0-9_-])?([a-z0-9_-]+(?:\([^)]*\))?)(.*)/i)) { + modifier = match[1]; + clause = match[2]; + rest = match[3]; + switch (modifier) { + case '#': + params.id = clause; + break; + case '.': + params.classNames.push(clause); + break; + case ':': + params.pseudoClassNames.push(clause); + break; + case '': + case undefined: + params.tagName = clause.toUpperCase(); + break; + default: + abort(repr(expr)); + } + expr = rest; + } + + if (expr.length > 0) { + abort(repr(expr)); + } + }, + + /** @id MochiKit.Selector.Selector.prototype.buildMatchExpression */ + buildMatchExpression: function () { + var repr = MochiKit.Base.repr; + var params = this.params; + var conditions = []; + var clause, i; + + function childElements(element) { + return "MochiKit.Base.filter(function (node) { return node.nodeType == 1; }, " + element + ".childNodes)"; + } + + if (params.wildcard) { + conditions.push('true'); + } + if (clause = params.id) { + conditions.push('element.id == ' + repr(clause)); + } + if (clause = params.tagName) { + conditions.push('element.tagName.toUpperCase() == ' + repr(clause)); + } + if ((clause = params.classNames).length > 0) { + for (i = 0; i < clause.length; i++) { + conditions.push('MochiKit.DOM.hasElementClass(element, ' + repr(clause[i]) + ')'); + } + } + if ((clause = params.pseudoClassNames).length > 0) { + for (i = 0; i < clause.length; i++) { + var match = clause[i].match(/^([^(]+)(?:\((.*)\))?$/); + var pseudoClass = match[1]; + var pseudoClassArgument = match[2]; + switch (pseudoClass) { + case 'root': + conditions.push('element.nodeType == 9 || element === element.ownerDocument.documentElement'); break; + case 'nth-child': + case 'nth-last-child': + case 'nth-of-type': + case 'nth-last-of-type': + match = pseudoClassArgument.match(/^((?:(\d+)n\+)?(\d+)|odd|even)$/); + if (!match) { + throw "Invalid argument to pseudo element nth-child: " + pseudoClassArgument; + } + var a, b; + if (match[0] == 'odd') { + a = 2; + b = 1; + } else if (match[0] == 'even') { + a = 2; + b = 0; + } else { + a = match[2] && parseInt(match) || null; + b = parseInt(match[3]); + } + conditions.push('this.nthChild(element,' + a + ',' + b + + ',' + !!pseudoClass.match('^nth-last') // Reverse + + ',' + !!pseudoClass.match('of-type$') // Restrict to same tagName + + ')'); + break; + case 'first-child': + conditions.push('this.nthChild(element, null, 1)'); + break; + case 'last-child': + conditions.push('this.nthChild(element, null, 1, true)'); + break; + case 'first-of-type': + conditions.push('this.nthChild(element, null, 1, false, true)'); + break; + case 'last-of-type': + conditions.push('this.nthChild(element, null, 1, true, true)'); + break; + case 'only-child': + conditions.push(childElements('element.parentNode') + '.length == 1'); + break; + case 'only-of-type': + conditions.push('MochiKit.Base.filter(function (node) { return node.tagName == element.tagName; }, ' + childElements('element.parentNode') + ').length == 1'); + break; + case 'empty': + conditions.push('element.childNodes.length == 0'); + break; + case 'enabled': + conditions.push('(this.isUIElement(element) && element.disabled === false)'); + break; + case 'disabled': + conditions.push('(this.isUIElement(element) && element.disabled === true)'); + break; + case 'checked': + conditions.push('(this.isUIElement(element) && element.checked === true)'); + break; + case 'not': + var subselector = new MochiKit.Selector.Selector(pseudoClassArgument); + conditions.push('!( ' + subselector.buildMatchExpression() + ')') + break; + } + } + } + if (clause = params.attributes) { + MochiKit.Base.map(function (attribute) { + var value = 'MochiKit.DOM.getNodeAttribute(element, ' + repr(attribute.name) + ')'; + var splitValueBy = function (delimiter) { + return value + '.split(' + repr(delimiter) + ')'; + } + + switch (attribute.operator) { + case '=': + conditions.push(value + ' == ' + repr(attribute.value)); + break; + case '~=': + conditions.push(value + ' && MochiKit.Base.findValue(' + splitValueBy(' ') + ', ' + repr(attribute.value) + ') > -1'); + break; + case '^=': + conditions.push(value + '.substring(0, ' + attribute.value.length + ') == ' + repr(attribute.value)); + break; + case '$=': + conditions.push(value + '.substring(' + value + '.length - ' + attribute.value.length + ') == ' + repr(attribute.value)); + break; + case '*=': + conditions.push(value + '.match(' + repr(attribute.value) + ')'); + break; + case '|=': + conditions.push( + value + ' && ' + splitValueBy('-') + '[0].toUpperCase() == ' + repr(attribute.value.toUpperCase()) + ); + break; + case '!=': + conditions.push(value + ' != ' + repr(attribute.value)); + break; + case '': + case undefined: + conditions.push(value + ' != null'); + break; + default: + throw 'Unknown operator ' + attribute.operator + ' in selector'; + } + }, clause); + } + + return conditions.join(' && '); + }, + + /** @id MochiKit.Selector.Selector.prototype.compileMatcher */ + compileMatcher: function () { + this.match = new Function('element', 'if (!element.tagName) return false; \ + return ' + this.buildMatchExpression()); + }, + + /** @id MochiKit.Selector.Selector.prototype.nthChild */ + nthChild: function (element, a, b, reverse, sametag){ + var siblings = MochiKit.Base.filter(function (node) { + return node.nodeType == 1; + }, element.parentNode.childNodes); + if (sametag) { + siblings = MochiKit.Base.filter(function (node) { + return node.tagName == element.tagName; + }, siblings); + } + if (reverse) { + siblings = MochiKit.Iter.reversed(siblings); + } + if (a) { + var actualIndex = MochiKit.Base.findIdentical(siblings, element); + return ((actualIndex + 1 - b) / a) % 1 == 0; + } else { + return b == MochiKit.Base.findIdentical(siblings, element) + 1; + } + }, + + /** @id MochiKit.Selector.Selector.prototype.isUIElement */ + isUIElement: function (element) { + return MochiKit.Base.findValue(['input', 'button', 'select', 'option', 'textarea', 'object'], + element.tagName.toLowerCase()) > -1; + }, + + /** @id MochiKit.Selector.Selector.prototype.findElements */ + findElements: function (scope, axis) { + var element; + + if (axis == undefined) { + axis = ""; + } + + function inScope(element, scope) { + if (axis == "") { + return MochiKit.DOM.isChildNode(element, scope); + } else if (axis == ">") { + return element.parentNode == scope; + } else if (axis == "+") { + return element == nextSiblingElement(scope); + } else if (axis == "~") { + var sibling = scope; + while (sibling = nextSiblingElement(sibling)) { + if (element == sibling) { + return true; + } + } + return false; + } else { + throw "Invalid axis: " + axis; + } + } + + if (element = MochiKit.DOM.getElement(this.params.id)) { + if (this.match(element)) { + if (!scope || inScope(element, scope)) { + return [element]; + } + } + } + + function nextSiblingElement(node) { + node = node.nextSibling; + while (node && node.nodeType != 1) { + node = node.nextSibling; + } + return node; + } + + if (axis == "") { + scope = (scope || MochiKit.DOM.currentDocument()).getElementsByTagName(this.params.tagName || '*'); + } else if (axis == ">") { + if (!scope) { + throw "> combinator not allowed without preceeding expression"; + } + scope = MochiKit.Base.filter(function (node) { + return node.nodeType == 1; + }, scope.childNodes); + } else if (axis == "+") { + if (!scope) { + throw "+ combinator not allowed without preceeding expression"; + } + scope = nextSiblingElement(scope) && [nextSiblingElement(scope)]; + } else if (axis == "~") { + if (!scope) { + throw "~ combinator not allowed without preceeding expression"; + } + var newscope = []; + while (nextSiblingElement(scope)) { + scope = nextSiblingElement(scope); + newscope.push(scope); + } + scope = newscope; + } + + if (!scope) { + return []; + } + + var results = MochiKit.Base.filter(MochiKit.Base.bind(function (scopeElt) { + return this.match(scopeElt); + }, this), scope); + + return results; + }, + + /** @id MochiKit.Selector.Selector.prototype.repr */ + repr: function () { + return 'Selector(' + this.expression + ')'; + }, + + toString: MochiKit.Base.forwardCall("repr") +}; + +MochiKit.Base.update(MochiKit.Selector, { + + /** @id MochiKit.Selector.findChildElements */ + findChildElements: function (element, expressions) { + return MochiKit.Base.flattenArray(MochiKit.Base.map(function (expression) { + var nextScope = ""; + return MochiKit.Iter.reduce(function (results, expr) { + if (match = expr.match(/^[>+~]$/)) { + nextScope = match[0]; + return results; + } else { + var selector = new MochiKit.Selector.Selector(expr); + var elements = MochiKit.Iter.reduce(function (elements, result) { + return MochiKit.Base.extend(elements, selector.findElements(result || element, nextScope)); + }, results, []); + nextScope = ""; + return elements; + } + }, expression.replace(/(^\s+|\s+$)/g, '').split(/\s+/), [null]); + }, expressions)); + }, + + findDocElements: function () { + return MochiKit.Selector.findChildElements(MochiKit.DOM.currentDocument(), arguments); + }, + + __new__: function () { + var m = MochiKit.Base; + + this.$$ = this.findDocElements; + + this.EXPORT_TAGS = { + ":common": this.EXPORT, + ":all": m.concat(this.EXPORT, this.EXPORT_OK) + }; + + m.nameFunctions(this); + } +}); + +MochiKit.Selector.__new__(); + +MochiKit.Base._exportSymbols(this, MochiKit.Selector); + diff --git a/dom/tests/mochitest/ajax/mochikit/MochiKit/Signal.js b/dom/tests/mochitest/ajax/mochikit/MochiKit/Signal.js new file mode 100644 index 0000000000..fd267a994e --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/Signal.js @@ -0,0 +1,899 @@ +/*** + +MochiKit.Signal 1.4 + +See <http://mochikit.com/> for documentation, downloads, license, etc. + +(c) 2006 Jonathan Gardner, Beau Hartshorne, Bob Ippolito. All rights Reserved. + +***/ + +if (typeof(dojo) != 'undefined') { + dojo.provide('MochiKit.Signal'); + dojo.require('MochiKit.Base'); + dojo.require('MochiKit.DOM'); + dojo.require('MochiKit.Style'); +} +if (typeof(JSAN) != 'undefined') { + JSAN.use('MochiKit.Base', []); + JSAN.use('MochiKit.DOM', []); + JSAN.use('MochiKit.Style', []); +} + +try { + if (typeof(MochiKit.Base) == 'undefined') { + throw ''; + } +} catch (e) { + throw 'MochiKit.Signal depends on MochiKit.Base!'; +} + +try { + if (typeof(MochiKit.DOM) == 'undefined') { + throw ''; + } +} catch (e) { + throw 'MochiKit.Signal depends on MochiKit.DOM!'; +} + +try { + if (typeof(MochiKit.Style) == 'undefined') { + throw ''; + } +} catch (e) { + throw 'MochiKit.Signal depends on MochiKit.Style!'; +} + +if (typeof(MochiKit.Signal) == 'undefined') { + MochiKit.Signal = {}; +} + +MochiKit.Signal.NAME = 'MochiKit.Signal'; +MochiKit.Signal.VERSION = '1.4'; + +MochiKit.Signal._observers = []; + +/** @id MochiKit.Signal.Event */ +MochiKit.Signal.Event = function (src, e) { + this._event = e || window.event; + this._src = src; +}; + +MochiKit.Base.update(MochiKit.Signal.Event.prototype, { + + __repr__: function () { + var repr = MochiKit.Base.repr; + var str = '{event(): ' + repr(this.event()) + + ', src(): ' + repr(this.src()) + + ', type(): ' + repr(this.type()) + + ', target(): ' + repr(this.target()); + + if (this.type() && + this.type().indexOf('key') === 0 || + this.type().indexOf('mouse') === 0 || + this.type().indexOf('click') != -1 || + this.type() == 'contextmenu') { + str += ', modifier(): ' + '{alt: ' + repr(this.modifier().alt) + + ', ctrl: ' + repr(this.modifier().ctrl) + + ', meta: ' + repr(this.modifier().meta) + + ', shift: ' + repr(this.modifier().shift) + + ', any: ' + repr(this.modifier().any) + '}'; + } + + if (this.type() && this.type().indexOf('key') === 0) { + str += ', key(): {code: ' + repr(this.key().code) + + ', string: ' + repr(this.key().string) + '}'; + } + + if (this.type() && ( + this.type().indexOf('mouse') === 0 || + this.type().indexOf('click') != -1 || + this.type() == 'contextmenu')) { + + str += ', mouse(): {page: ' + repr(this.mouse().page) + + ', client: ' + repr(this.mouse().client); + + if (this.type() != 'mousemove') { + str += ', button: {left: ' + repr(this.mouse().button.left) + + ', middle: ' + repr(this.mouse().button.middle) + + ', right: ' + repr(this.mouse().button.right) + '}}'; + } else { + str += '}'; + } + } + if (this.type() == 'mouseover' || this.type() == 'mouseout') { + str += ', relatedTarget(): ' + repr(this.relatedTarget()); + } + str += '}'; + return str; + }, + + /** @id MochiKit.Signal.Event.prototype.toString */ + toString: function () { + return this.__repr__(); + }, + + /** @id MochiKit.Signal.Event.prototype.src */ + src: function () { + return this._src; + }, + + /** @id MochiKit.Signal.Event.prototype.event */ + event: function () { + return this._event; + }, + + /** @id MochiKit.Signal.Event.prototype.type */ + type: function () { + return this._event.type || undefined; + }, + + /** @id MochiKit.Signal.Event.prototype.target */ + target: function () { + return this._event.target || this._event.srcElement; + }, + + _relatedTarget: null, + /** @id MochiKit.Signal.Event.prototype.relatedTarget */ + relatedTarget: function () { + if (this._relatedTarget !== null) { + return this._relatedTarget; + } + + var elem = null; + if (this.type() == 'mouseover') { + elem = (this._event.relatedTarget || + this._event.fromElement); + } else if (this.type() == 'mouseout') { + elem = (this._event.relatedTarget || + this._event.toElement); + } + if (elem !== null) { + this._relatedTarget = elem; + return elem; + } + + return undefined; + }, + + _modifier: null, + /** @id MochiKit.Signal.Event.prototype.modifier */ + modifier: function () { + if (this._modifier !== null) { + return this._modifier; + } + var m = {}; + m.alt = this._event.altKey; + m.ctrl = this._event.ctrlKey; + m.meta = this._event.metaKey || false; // IE and Opera punt here + m.shift = this._event.shiftKey; + m.any = m.alt || m.ctrl || m.shift || m.meta; + this._modifier = m; + return m; + }, + + _key: null, + /** @id MochiKit.Signal.Event.prototype.key */ + key: function () { + if (this._key !== null) { + return this._key; + } + var k = {}; + if (this.type() && this.type().indexOf('key') === 0) { + + /* + + If you're looking for a special key, look for it in keydown or + keyup, but never keypress. If you're looking for a Unicode + chracter, look for it with keypress, but never keyup or + keydown. + + Notes: + + FF key event behavior: + key event charCode keyCode + DOWN ku,kd 0 40 + DOWN kp 0 40 + ESC ku,kd 0 27 + ESC kp 0 27 + a ku,kd 0 65 + a kp 97 0 + shift+a ku,kd 0 65 + shift+a kp 65 0 + 1 ku,kd 0 49 + 1 kp 49 0 + shift+1 ku,kd 0 0 + shift+1 kp 33 0 + + IE key event behavior: + (IE doesn't fire keypress events for special keys.) + key event keyCode + DOWN ku,kd 40 + DOWN kp undefined + ESC ku,kd 27 + ESC kp 27 + a ku,kd 65 + a kp 97 + shift+a ku,kd 65 + shift+a kp 65 + 1 ku,kd 49 + 1 kp 49 + shift+1 ku,kd 49 + shift+1 kp 33 + + Safari key event behavior: + (Safari sets charCode and keyCode to something weird for + special keys.) + key event charCode keyCode + DOWN ku,kd 63233 40 + DOWN kp 63233 63233 + ESC ku,kd 27 27 + ESC kp 27 27 + a ku,kd 97 65 + a kp 97 97 + shift+a ku,kd 65 65 + shift+a kp 65 65 + 1 ku,kd 49 49 + 1 kp 49 49 + shift+1 ku,kd 33 49 + shift+1 kp 33 33 + + */ + + /* look for special keys here */ + if (this.type() == 'keydown' || this.type() == 'keyup') { + k.code = this._event.keyCode; + k.string = (MochiKit.Signal._specialKeys[k.code] || + 'KEY_UNKNOWN'); + this._key = k; + return k; + + /* look for characters here */ + } else if (this.type() == 'keypress') { + + /* + + Special key behavior: + + IE: does not fire keypress events for special keys + FF: sets charCode to 0, and sets the correct keyCode + Safari: sets keyCode and charCode to something stupid + + */ + + k.code = 0; + k.string = ''; + + if (typeof(this._event.charCode) != 'undefined' && + this._event.charCode !== 0 && + !MochiKit.Signal._specialMacKeys[this._event.charCode]) { + k.code = this._event.charCode; + k.string = String.fromCharCode(k.code); + } else if (this._event.keyCode && + typeof(this._event.charCode) == 'undefined') { // IE + k.code = this._event.keyCode; + k.string = String.fromCharCode(k.code); + } + + this._key = k; + return k; + } + } + return undefined; + }, + + _mouse: null, + /** @id MochiKit.Signal.Event.prototype.mouse */ + mouse: function () { + if (this._mouse !== null) { + return this._mouse; + } + + var m = {}; + var e = this._event; + + if (this.type() && ( + this.type().indexOf('mouse') === 0 || + this.type().indexOf('click') != -1 || + this.type() == 'contextmenu')) { + + m.client = new MochiKit.Style.Coordinates(0, 0); + if (e.clientX || e.clientY) { + m.client.x = (!e.clientX || e.clientX < 0) ? 0 : e.clientX; + m.client.y = (!e.clientY || e.clientY < 0) ? 0 : e.clientY; + } + + m.page = new MochiKit.Style.Coordinates(0, 0); + if (e.pageX || e.pageY) { + m.page.x = (!e.pageX || e.pageX < 0) ? 0 : e.pageX; + m.page.y = (!e.pageY || e.pageY < 0) ? 0 : e.pageY; + } else { + /* + + The IE shortcut can be off by two. We fix it. See: + http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/getboundingclientrect.asp + + This is similar to the method used in + MochiKit.Style.getElementPosition(). + + */ + var de = MochiKit.DOM._document.documentElement; + var b = MochiKit.DOM._document.body; + + m.page.x = e.clientX + + (de.scrollLeft || b.scrollLeft) - + (de.clientLeft || 0); + + m.page.y = e.clientY + + (de.scrollTop || b.scrollTop) - + (de.clientTop || 0); + + } + if (this.type() != 'mousemove') { + m.button = {}; + m.button.left = false; + m.button.right = false; + m.button.middle = false; + + /* we could check e.button, but which is more consistent */ + if (e.which) { + m.button.left = (e.which == 1); + m.button.middle = (e.which == 2); + m.button.right = (e.which == 3); + + /* + + Mac browsers and right click: + + - Safari doesn't fire any click events on a right + click: + http://bugs.webkit.org/show_bug.cgi?id=6595 + + - Firefox fires the event, and sets ctrlKey = true + + - Opera fires the event, and sets metaKey = true + + oncontextmenu is fired on right clicks between + browsers and across platforms. + + */ + + } else { + m.button.left = !!(e.button & 1); + m.button.right = !!(e.button & 2); + m.button.middle = !!(e.button & 4); + } + } + this._mouse = m; + return m; + } + return undefined; + }, + + /** @id MochiKit.Signal.Event.prototype.stop */ + stop: function () { + this.stopPropagation(); + this.preventDefault(); + }, + + /** @id MochiKit.Signal.Event.prototype.stopPropagation */ + stopPropagation: function () { + if (this._event.stopPropagation) { + this._event.stopPropagation(); + } else { + this._event.cancelBubble = true; + } + }, + + /** @id MochiKit.Signal.Event.prototype.preventDefault */ + preventDefault: function () { + if (this._event.preventDefault) { + this._event.preventDefault(); + } else if (this._confirmUnload === null) { + this._event.returnValue = false; + } + }, + + _confirmUnload: null, + + /** @id MochiKit.Signal.Event.prototype.confirmUnload */ + confirmUnload: function (msg) { + if (this.type() == 'beforeunload') { + this._confirmUnload = msg; + this._event.returnValue = msg; + } + } +}); + +/* Safari sets keyCode to these special values onkeypress. */ +MochiKit.Signal._specialMacKeys = { + 3: 'KEY_ENTER', + 63289: 'KEY_NUM_PAD_CLEAR', + 63276: 'KEY_PAGE_UP', + 63277: 'KEY_PAGE_DOWN', + 63275: 'KEY_END', + 63273: 'KEY_HOME', + 63234: 'KEY_ARROW_LEFT', + 63232: 'KEY_ARROW_UP', + 63235: 'KEY_ARROW_RIGHT', + 63233: 'KEY_ARROW_DOWN', + 63302: 'KEY_INSERT', + 63272: 'KEY_DELETE' +}; + +/* for KEY_F1 - KEY_F12 */ +(function () { + var _specialMacKeys = MochiKit.Signal._specialMacKeys; + for (i = 63236; i <= 63242; i++) { + // no F0 + _specialMacKeys[i] = 'KEY_F' + (i - 63236 + 1); + } +})(); + +/* Standard keyboard key codes. */ +MochiKit.Signal._specialKeys = { + 8: 'KEY_BACKSPACE', + 9: 'KEY_TAB', + 12: 'KEY_NUM_PAD_CLEAR', // weird, for Safari and Mac FF only + 13: 'KEY_ENTER', + 16: 'KEY_SHIFT', + 17: 'KEY_CTRL', + 18: 'KEY_ALT', + 19: 'KEY_PAUSE', + 20: 'KEY_CAPS_LOCK', + 27: 'KEY_ESCAPE', + 32: 'KEY_SPACEBAR', + 33: 'KEY_PAGE_UP', + 34: 'KEY_PAGE_DOWN', + 35: 'KEY_END', + 36: 'KEY_HOME', + 37: 'KEY_ARROW_LEFT', + 38: 'KEY_ARROW_UP', + 39: 'KEY_ARROW_RIGHT', + 40: 'KEY_ARROW_DOWN', + 44: 'KEY_PRINT_SCREEN', + 45: 'KEY_INSERT', + 46: 'KEY_DELETE', + 59: 'KEY_SEMICOLON', // weird, for Safari and IE only + 91: 'KEY_WINDOWS_LEFT', + 92: 'KEY_WINDOWS_RIGHT', + 93: 'KEY_SELECT', + 106: 'KEY_NUM_PAD_ASTERISK', + 107: 'KEY_NUM_PAD_PLUS_SIGN', + 109: 'KEY_NUM_PAD_HYPHEN-MINUS', + 110: 'KEY_NUM_PAD_FULL_STOP', + 111: 'KEY_NUM_PAD_SOLIDUS', + 144: 'KEY_NUM_LOCK', + 145: 'KEY_SCROLL_LOCK', + 186: 'KEY_SEMICOLON', + 187: 'KEY_EQUALS_SIGN', + 188: 'KEY_COMMA', + 189: 'KEY_HYPHEN-MINUS', + 190: 'KEY_FULL_STOP', + 191: 'KEY_SOLIDUS', + 192: 'KEY_GRAVE_ACCENT', + 219: 'KEY_LEFT_SQUARE_BRACKET', + 220: 'KEY_REVERSE_SOLIDUS', + 221: 'KEY_RIGHT_SQUARE_BRACKET', + 222: 'KEY_APOSTROPHE' + // undefined: 'KEY_UNKNOWN' +}; + +(function () { + /* for KEY_0 - KEY_9 */ + var _specialKeys = MochiKit.Signal._specialKeys; + for (var i = 48; i <= 57; i++) { + _specialKeys[i] = 'KEY_' + (i - 48); + } + + /* for KEY_A - KEY_Z */ + for (i = 65; i <= 90; i++) { + _specialKeys[i] = 'KEY_' + String.fromCharCode(i); + } + + /* for KEY_NUM_PAD_0 - KEY_NUM_PAD_9 */ + for (i = 96; i <= 105; i++) { + _specialKeys[i] = 'KEY_NUM_PAD_' + (i - 96); + } + + /* for KEY_F1 - KEY_F12 */ + for (i = 112; i <= 123; i++) { + // no F0 + _specialKeys[i] = 'KEY_F' + (i - 112 + 1); + } +})(); + +/* Internal object to keep track of created signals. */ +MochiKit.Signal.Ident = function (ident) { + this.source = ident.source; + this.signal = ident.signal; + this.listener = ident.listener; + this.isDOM = ident.isDOM; + this.objOrFunc = ident.objOrFunc; + this.funcOrStr = ident.funcOrStr; + this.connected = ident.connected; +}; + +MochiKit.Signal.Ident.prototype = {}; + +MochiKit.Base.update(MochiKit.Signal, { + + __repr__: function () { + return '[' + this.NAME + ' ' + this.VERSION + ']'; + }, + + toString: function () { + return this.__repr__(); + }, + + _unloadCache: function () { + var self = MochiKit.Signal; + var observers = self._observers; + + for (var i = 0; i < observers.length; i++) { + if (observers[i].signal !== 'onload' && observers[i].signal !== 'onunload') { + self._disconnect(observers[i]); + } + } + }, + + _listener: function (src, sig, func, obj, isDOM) { + var self = MochiKit.Signal; + var E = self.Event; + if (!isDOM) { + return MochiKit.Base.bind(func, obj); + } + obj = obj || src; + if (typeof(func) == "string") { + if (sig === 'onload' || sig === 'onunload') { + return function (nativeEvent) { + obj[func].apply(obj, [new E(src, nativeEvent)]); + + var ident = new MochiKit.Signal.Ident({ + source: src, signal: sig, objOrFunc: obj, funcOrStr: func}); + + MochiKit.Signal._disconnect(ident); + }; + } else { + return function (nativeEvent) { + obj[func].apply(obj, [new E(src, nativeEvent)]); + }; + } + } else { + if (sig === 'onload' || sig === 'onunload') { + return function (nativeEvent) { + func.apply(obj, [new E(src, nativeEvent)]); + MochiKit.Signal.disconnect(src, sig, func); + + var ident = new MochiKit.Signal.Ident({ + source: src, signal: sig, objOrFunc: func}); + + MochiKit.Signal._disconnect(ident); + }; + } else { + return function (nativeEvent) { + func.apply(obj, [new E(src, nativeEvent)]); + }; + } + } + }, + + _browserAlreadyHasMouseEnterAndLeave: function () { + return /MSIE/.test(navigator.userAgent); + }, + + _mouseEnterListener: function (src, sig, func, obj) { + var E = MochiKit.Signal.Event; + return function (nativeEvent) { + var e = new E(src, nativeEvent); + try { + e.relatedTarget().nodeName; + } catch (err) { + /* probably hit a permission denied error; possibly one of + * firefox's screwy anonymous DIVs inside an input element. + * Allow this event to propogate up. + */ + return; + } + e.stop(); + if (MochiKit.DOM.isChildNode(e.relatedTarget(), src)) { + /* We've moved between our node and a child. Ignore. */ + return; + } + e.type = function () { return sig; }; + if (typeof(func) == "string") { + return obj[func].apply(obj, [e]); + } else { + return func.apply(obj, [e]); + } + }; + }, + + _getDestPair: function (objOrFunc, funcOrStr) { + var obj = null; + var func = null; + if (typeof(funcOrStr) != 'undefined') { + obj = objOrFunc; + func = funcOrStr; + if (typeof(funcOrStr) == 'string') { + if (typeof(objOrFunc[funcOrStr]) != "function") { + throw new Error("'funcOrStr' must be a function on 'objOrFunc'"); + } + } else if (typeof(funcOrStr) != 'function') { + throw new Error("'funcOrStr' must be a function or string"); + } + } else if (typeof(objOrFunc) != "function") { + throw new Error("'objOrFunc' must be a function if 'funcOrStr' is not given"); + } else { + func = objOrFunc; + } + return [obj, func]; + }, + + /** @id MochiKit.Signal.connect */ + connect: function (src, sig, objOrFunc/* optional */, funcOrStr) { + src = MochiKit.DOM.getElement(src); + var self = MochiKit.Signal; + + if (typeof(sig) != 'string') { + throw new Error("'sig' must be a string"); + } + + var destPair = self._getDestPair(objOrFunc, funcOrStr); + var obj = destPair[0]; + var func = destPair[1]; + if (typeof(obj) == 'undefined' || obj === null) { + obj = src; + } + + var isDOM = !!(src.addEventListener || src.attachEvent); + if (isDOM && (sig === "onmouseenter" || sig === "onmouseleave") + && !self._browserAlreadyHasMouseEnterAndLeave()) { + var listener = self._mouseEnterListener(src, sig.substr(2), func, obj); + if (sig === "onmouseenter") { + sig = "onmouseover"; + } else { + sig = "onmouseout"; + } + } else { + var listener = self._listener(src, sig, func, obj, isDOM); + } + + if (src.addEventListener) { + src.addEventListener(sig.substr(2), listener, false); + } else if (src.attachEvent) { + src.attachEvent(sig, listener); // useCapture unsupported + } + + var ident = new MochiKit.Signal.Ident({ + source: src, + signal: sig, + listener: listener, + isDOM: isDOM, + objOrFunc: objOrFunc, + funcOrStr: funcOrStr, + connected: true + }); + self._observers.push(ident); + + if (!isDOM && typeof(src.__connect__) == 'function') { + var args = MochiKit.Base.extend([ident], arguments, 1); + src.__connect__.apply(src, args); + } + + return ident; + }, + + _disconnect: function (ident) { + // already disconnected + if (!ident.connected) { + return; + } + ident.connected = false; + // check isDOM + if (!ident.isDOM) { + return; + } + var src = ident.source; + var sig = ident.signal; + var listener = ident.listener; + + if (src.removeEventListener) { + src.removeEventListener(sig.substr(2), listener, false); + } else if (src.detachEvent) { + src.detachEvent(sig, listener); // useCapture unsupported + } else { + throw new Error("'src' must be a DOM element"); + } + }, + + /** @id MochiKit.Signal.disconnect */ + disconnect: function (ident) { + var self = MochiKit.Signal; + var observers = self._observers; + var m = MochiKit.Base; + if (arguments.length > 1) { + // compatibility API + var src = MochiKit.DOM.getElement(arguments[0]); + var sig = arguments[1]; + var obj = arguments[2]; + var func = arguments[3]; + for (var i = observers.length - 1; i >= 0; i--) { + var o = observers[i]; + if (o.source === src && o.signal === sig && o.objOrFunc === obj && o.funcOrStr === func) { + self._disconnect(o); + if (!self._lock) { + observers.splice(i, 1); + } else { + self._dirty = true; + } + return true; + } + } + } else { + var idx = m.findIdentical(observers, ident); + if (idx >= 0) { + self._disconnect(ident); + if (!self._lock) { + observers.splice(idx, 1); + } else { + self._dirty = true; + } + return true; + } + } + return false; + }, + + /** @id MochiKit.Signal.disconnectAllTo */ + disconnectAllTo: function (objOrFunc, /* optional */funcOrStr) { + var self = MochiKit.Signal; + var observers = self._observers; + var disconnect = self._disconnect; + var locked = self._lock; + var dirty = self._dirty; + if (typeof(funcOrStr) === 'undefined') { + funcOrStr = null; + } + for (var i = observers.length - 1; i >= 0; i--) { + var ident = observers[i]; + if (ident.objOrFunc === objOrFunc && + (funcOrStr === null || ident.funcOrStr === funcOrStr)) { + disconnect(ident); + if (locked) { + dirty = true; + } else { + observers.splice(i, 1); + } + } + } + self._dirty = dirty; + }, + + /** @id MochiKit.Signal.disconnectAll */ + disconnectAll: function (src/* optional */, sig) { + src = MochiKit.DOM.getElement(src); + var m = MochiKit.Base; + var signals = m.flattenArguments(m.extend(null, arguments, 1)); + var self = MochiKit.Signal; + var disconnect = self._disconnect; + var observers = self._observers; + var i, ident; + var locked = self._lock; + var dirty = self._dirty; + if (signals.length === 0) { + // disconnect all + for (i = observers.length - 1; i >= 0; i--) { + ident = observers[i]; + if (ident.source === src) { + disconnect(ident); + if (!locked) { + observers.splice(i, 1); + } else { + dirty = true; + } + } + } + } else { + var sigs = {}; + for (i = 0; i < signals.length; i++) { + sigs[signals[i]] = true; + } + for (i = observers.length - 1; i >= 0; i--) { + ident = observers[i]; + if (ident.source === src && ident.signal in sigs) { + disconnect(ident); + if (!locked) { + observers.splice(i, 1); + } else { + dirty = true; + } + } + } + } + self._dirty = dirty; + }, + + /** @id MochiKit.Signal.signal */ + signal: function (src, sig) { + var self = MochiKit.Signal; + var observers = self._observers; + src = MochiKit.DOM.getElement(src); + var args = MochiKit.Base.extend(null, arguments, 2); + var errors = []; + self._lock = true; + for (var i = 0; i < observers.length; i++) { + var ident = observers[i]; + if (ident.source === src && ident.signal === sig && + ident.connected) { + try { + ident.listener.apply(src, args); + } catch (e) { + errors.push(e); + } + } + } + self._lock = false; + if (self._dirty) { + self._dirty = false; + for (var i = observers.length - 1; i >= 0; i--) { + if (!observers[i].connected) { + observers.splice(i, 1); + } + } + } + if (errors.length == 1) { + throw errors[0]; + } else if (errors.length > 1) { + var e = new Error("Multiple errors thrown in handling 'sig', see errors property"); + e.errors = errors; + throw e; + } + } + +}); + +MochiKit.Signal.EXPORT_OK = []; + +MochiKit.Signal.EXPORT = [ + 'connect', + 'disconnect', + 'signal', + 'disconnectAll', + 'disconnectAllTo' +]; + +MochiKit.Signal.__new__ = function (win) { + var m = MochiKit.Base; + this._document = document; + this._window = win; + this._lock = false; + this._dirty = false; + + try { + this.connect(window, 'onunload', this._unloadCache); + } catch (e) { + // pass: might not be a browser + } + + this.EXPORT_TAGS = { + ':common': this.EXPORT, + ':all': m.concat(this.EXPORT, this.EXPORT_OK) + }; + + m.nameFunctions(this); +}; + +MochiKit.Signal.__new__(this); + +// +// XXX: Internet Explorer blows +// +if (MochiKit.__export__) { + connect = MochiKit.Signal.connect; + disconnect = MochiKit.Signal.disconnect; + disconnectAll = MochiKit.Signal.disconnectAll; + signal = MochiKit.Signal.signal; +} + +MochiKit.Base._exportSymbols(this, MochiKit.Signal); diff --git a/dom/tests/mochitest/ajax/mochikit/MochiKit/Sortable.js b/dom/tests/mochitest/ajax/mochikit/MochiKit/Sortable.js new file mode 100644 index 0000000000..8976ec0b27 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/Sortable.js @@ -0,0 +1,589 @@ +/*** +Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) + Mochi-ized By Thomas Herve (_firstname_@nimail.org) + +See scriptaculous.js for full license. + +***/ + +if (typeof(dojo) != 'undefined') { + dojo.provide('MochiKit.Sortable'); + dojo.require('MochiKit.Base'); + dojo.require('MochiKit.DOM'); + dojo.require('MochiKit.Iter'); +} + +if (typeof(JSAN) != 'undefined') { + JSAN.use("MochiKit.Base", []); + JSAN.use("MochiKit.DOM", []); + JSAN.use("MochiKit.Iter", []); +} + +try { + if (typeof(MochiKit.Base) == 'undefined' || + typeof(MochiKit.DOM) == 'undefined' || + typeof(MochiKit.Iter) == 'undefined') { + throw ""; + } +} catch (e) { + throw "MochiKit.DragAndDrop depends on MochiKit.Base, MochiKit.DOM and MochiKit.Iter!"; +} + +if (typeof(MochiKit.Sortable) == 'undefined') { + MochiKit.Sortable = {}; +} + +MochiKit.Sortable.NAME = 'MochiKit.Sortable'; +MochiKit.Sortable.VERSION = '1.4'; + +MochiKit.Sortable.__repr__ = function () { + return '[' + this.NAME + ' ' + this.VERSION + ']'; +}; + +MochiKit.Sortable.toString = function () { + return this.__repr__(); +}; + +MochiKit.Sortable.EXPORT = [ +]; + +MochiKit.Sortable.EXPORT_OK = [ +]; + +MochiKit.Base.update(MochiKit.Sortable, { + /*** + + Manage sortables. Mainly use the create function to add a sortable. + + ***/ + sortables: {}, + + _findRootElement: function (element) { + while (element.tagName.toUpperCase() != "BODY") { + if (element.id && MochiKit.Sortable.sortables[element.id]) { + return element; + } + element = element.parentNode; + } + }, + + /** @id MochiKit.Sortable.options */ + options: function (element) { + element = MochiKit.Sortable._findRootElement(MochiKit.DOM.getElement(element)); + if (!element) { + return; + } + return MochiKit.Sortable.sortables[element.id]; + }, + + /** @id MochiKit.Sortable.destroy */ + destroy: function (element){ + var s = MochiKit.Sortable.options(element); + var b = MochiKit.Base; + var d = MochiKit.DragAndDrop; + + if (s) { + MochiKit.Signal.disconnect(s.startHandle); + MochiKit.Signal.disconnect(s.endHandle); + b.map(function (dr) { + d.Droppables.remove(dr); + }, s.droppables); + b.map(function (dr) { + dr.destroy(); + }, s.draggables); + + delete MochiKit.Sortable.sortables[s.element.id]; + } + }, + + /** @id MochiKit.Sortable.create */ + create: function (element, options) { + element = MochiKit.DOM.getElement(element); + var self = MochiKit.Sortable; + + /** @id MochiKit.Sortable.options */ + options = MochiKit.Base.update({ + + /** @id MochiKit.Sortable.element */ + element: element, + + /** @id MochiKit.Sortable.tag */ + tag: 'li', // assumes li children, override with tag: 'tagname' + + /** @id MochiKit.Sortable.dropOnEmpty */ + dropOnEmpty: false, + + /** @id MochiKit.Sortable.tree */ + tree: false, + + /** @id MochiKit.Sortable.treeTag */ + treeTag: 'ul', + + /** @id MochiKit.Sortable.overlap */ + overlap: 'vertical', // one of 'vertical', 'horizontal' + + /** @id MochiKit.Sortable.constraint */ + constraint: 'vertical', // one of 'vertical', 'horizontal', false + // also takes array of elements (or ids); or false + + /** @id MochiKit.Sortable.containment */ + containment: [element], + + /** @id MochiKit.Sortable.handle */ + handle: false, // or a CSS class + + /** @id MochiKit.Sortable.only */ + only: false, + + /** @id MochiKit.Sortable.hoverclass */ + hoverclass: null, + + /** @id MochiKit.Sortable.ghosting */ + ghosting: false, + + /** @id MochiKit.Sortable.scroll */ + scroll: false, + + /** @id MochiKit.Sortable.scrollSensitivity */ + scrollSensitivity: 20, + + /** @id MochiKit.Sortable.scrollSpeed */ + scrollSpeed: 15, + + /** @id MochiKit.Sortable.format */ + format: /^[^_]*_(.*)$/, + + /** @id MochiKit.Sortable.onChange */ + onChange: MochiKit.Base.noop, + + /** @id MochiKit.Sortable.onUpdate */ + onUpdate: MochiKit.Base.noop, + + /** @id MochiKit.Sortable.accept */ + accept: null + }, options); + + // clear any old sortable with same element + self.destroy(element); + + // build options for the draggables + var options_for_draggable = { + revert: true, + ghosting: options.ghosting, + scroll: options.scroll, + scrollSensitivity: options.scrollSensitivity, + scrollSpeed: options.scrollSpeed, + constraint: options.constraint, + handle: options.handle + }; + + if (options.starteffect) { + options_for_draggable.starteffect = options.starteffect; + } + + if (options.reverteffect) { + options_for_draggable.reverteffect = options.reverteffect; + } else if (options.ghosting) { + options_for_draggable.reverteffect = function (innerelement) { + innerelement.style.top = 0; + innerelement.style.left = 0; + }; + } + + if (options.endeffect) { + options_for_draggable.endeffect = options.endeffect; + } + + if (options.zindex) { + options_for_draggable.zindex = options.zindex; + } + + // build options for the droppables + var options_for_droppable = { + overlap: options.overlap, + containment: options.containment, + hoverclass: options.hoverclass, + onhover: self.onHover, + tree: options.tree, + accept: options.accept + } + + var options_for_tree = { + onhover: self.onEmptyHover, + overlap: options.overlap, + containment: options.containment, + hoverclass: options.hoverclass, + accept: options.accept + } + + // fix for gecko engine + MochiKit.DOM.removeEmptyTextNodes(element); + + options.draggables = []; + options.droppables = []; + + // drop on empty handling + if (options.dropOnEmpty || options.tree) { + new MochiKit.DragAndDrop.Droppable(element, options_for_tree); + options.droppables.push(element); + } + MochiKit.Base.map(function (e) { + // handles are per-draggable + var handle = options.handle ? + MochiKit.DOM.getFirstElementByTagAndClassName(null, + options.handle, e) : e; + options.draggables.push( + new MochiKit.DragAndDrop.Draggable(e, + MochiKit.Base.update(options_for_draggable, + {handle: handle}))); + new MochiKit.DragAndDrop.Droppable(e, options_for_droppable); + if (options.tree) { + e.treeNode = element; + } + options.droppables.push(e); + }, (self.findElements(element, options) || [])); + + if (options.tree) { + MochiKit.Base.map(function (e) { + new MochiKit.DragAndDrop.Droppable(e, options_for_tree); + e.treeNode = element; + options.droppables.push(e); + }, (self.findTreeElements(element, options) || [])); + } + + // keep reference + self.sortables[element.id] = options; + + options.lastValue = self.serialize(element); + options.startHandle = MochiKit.Signal.connect(MochiKit.DragAndDrop.Draggables, 'start', + MochiKit.Base.partial(self.onStart, element)); + options.endHandle = MochiKit.Signal.connect(MochiKit.DragAndDrop.Draggables, 'end', + MochiKit.Base.partial(self.onEnd, element)); + }, + + /** @id MochiKit.Sortable.onStart */ + onStart: function (element, draggable) { + var self = MochiKit.Sortable; + var options = self.options(element); + options.lastValue = self.serialize(options.element); + }, + + /** @id MochiKit.Sortable.onEnd */ + onEnd: function (element, draggable) { + var self = MochiKit.Sortable; + self.unmark(); + var options = self.options(element); + if (options.lastValue != self.serialize(options.element)) { + options.onUpdate(options.element); + } + }, + + // return all suitable-for-sortable elements in a guaranteed order + + /** @id MochiKit.Sortable.findElements */ + findElements: function (element, options) { + return MochiKit.Sortable.findChildren( + element, options.only, options.tree ? true : false, options.tag); + }, + + /** @id MochiKit.Sortable.findTreeElements */ + findTreeElements: function (element, options) { + return MochiKit.Sortable.findChildren( + element, options.only, options.tree ? true : false, options.treeTag); + }, + + /** @id MochiKit.Sortable.findChildren */ + findChildren: function (element, only, recursive, tagName) { + if (!element.hasChildNodes()) { + return null; + } + tagName = tagName.toUpperCase(); + if (only) { + only = MochiKit.Base.flattenArray([only]); + } + var elements = []; + MochiKit.Base.map(function (e) { + if (e.tagName && + e.tagName.toUpperCase() == tagName && + (!only || + MochiKit.Iter.some(only, function (c) { + return MochiKit.DOM.hasElementClass(e, c); + }))) { + elements.push(e); + } + if (recursive) { + var grandchildren = MochiKit.Sortable.findChildren(e, only, recursive, tagName); + if (grandchildren && grandchildren.length > 0) { + elements = elements.concat(grandchildren); + } + } + }, element.childNodes); + return elements; + }, + + /** @id MochiKit.Sortable.onHover */ + onHover: function (element, dropon, overlap) { + if (MochiKit.DOM.isParent(dropon, element)) { + return; + } + var self = MochiKit.Sortable; + + if (overlap > .33 && overlap < .66 && self.options(dropon).tree) { + return; + } else if (overlap > 0.5) { + self.mark(dropon, 'before'); + if (dropon.previousSibling != element) { + var oldParentNode = element.parentNode; + element.style.visibility = 'hidden'; // fix gecko rendering + dropon.parentNode.insertBefore(element, dropon); + if (dropon.parentNode != oldParentNode) { + self.options(oldParentNode).onChange(element); + } + self.options(dropon.parentNode).onChange(element); + } + } else { + self.mark(dropon, 'after'); + var nextElement = dropon.nextSibling || null; + if (nextElement != element) { + var oldParentNode = element.parentNode; + element.style.visibility = 'hidden'; // fix gecko rendering + dropon.parentNode.insertBefore(element, nextElement); + if (dropon.parentNode != oldParentNode) { + self.options(oldParentNode).onChange(element); + } + self.options(dropon.parentNode).onChange(element); + } + } + }, + + _offsetSize: function (element, type) { + if (type == 'vertical' || type == 'height') { + return element.offsetHeight; + } else { + return element.offsetWidth; + } + }, + + /** @id MochiKit.Sortable.onEmptyHover */ + onEmptyHover: function (element, dropon, overlap) { + var oldParentNode = element.parentNode; + var self = MochiKit.Sortable; + var droponOptions = self.options(dropon); + + if (!MochiKit.DOM.isParent(dropon, element)) { + var index; + + var children = self.findElements(dropon, {tag: droponOptions.tag, + only: droponOptions.only}); + var child = null; + + if (children) { + var offset = self._offsetSize(dropon, droponOptions.overlap) * (1.0 - overlap); + + for (index = 0; index < children.length; index += 1) { + if (offset - self._offsetSize(children[index], droponOptions.overlap) >= 0) { + offset -= self._offsetSize(children[index], droponOptions.overlap); + } else if (offset - (self._offsetSize (children[index], droponOptions.overlap) / 2) >= 0) { + child = index + 1 < children.length ? children[index + 1] : null; + break; + } else { + child = children[index]; + break; + } + } + } + + dropon.insertBefore(element, child); + + self.options(oldParentNode).onChange(element); + droponOptions.onChange(element); + } + }, + + /** @id MochiKit.Sortable.unmark */ + unmark: function () { + var m = MochiKit.Sortable._marker; + if (m) { + MochiKit.Style.hideElement(m); + } + }, + + /** @id MochiKit.Sortable.mark */ + mark: function (dropon, position) { + // mark on ghosting only + var d = MochiKit.DOM; + var self = MochiKit.Sortable; + var sortable = self.options(dropon.parentNode); + if (sortable && !sortable.ghosting) { + return; + } + + if (!self._marker) { + self._marker = d.getElement('dropmarker') || + document.createElement('DIV'); + MochiKit.Style.hideElement(self._marker); + d.addElementClass(self._marker, 'dropmarker'); + self._marker.style.position = 'absolute'; + document.getElementsByTagName('body').item(0).appendChild(self._marker); + } + var offsets = MochiKit.Position.cumulativeOffset(dropon); + self._marker.style.left = offsets.x + 'px'; + self._marker.style.top = offsets.y + 'px'; + + if (position == 'after') { + if (sortable.overlap == 'horizontal') { + self._marker.style.left = (offsets.x + dropon.clientWidth) + 'px'; + } else { + self._marker.style.top = (offsets.y + dropon.clientHeight) + 'px'; + } + } + MochiKit.Style.showElement(self._marker); + }, + + _tree: function (element, options, parent) { + var self = MochiKit.Sortable; + var children = self.findElements(element, options) || []; + + for (var i = 0; i < children.length; ++i) { + var match = children[i].id.match(options.format); + + if (!match) { + continue; + } + + var child = { + id: encodeURIComponent(match ? match[1] : null), + element: element, + parent: parent, + children: [], + position: parent.children.length, + container: self._findChildrenElement(children[i], options.treeTag.toUpperCase()) + } + + /* Get the element containing the children and recurse over it */ + if (child.container) { + self._tree(child.container, options, child) + } + + parent.children.push (child); + } + + return parent; + }, + + /* Finds the first element of the given tag type within a parent element. + Used for finding the first LI[ST] within a L[IST]I[TEM].*/ + _findChildrenElement: function (element, containerTag) { + if (element && element.hasChildNodes) { + containerTag = containerTag.toUpperCase(); + for (var i = 0; i < element.childNodes.length; ++i) { + if (element.childNodes[i].tagName.toUpperCase() == containerTag) { + return element.childNodes[i]; + } + } + } + return null; + }, + + /** @id MochiKit.Sortable.tree */ + tree: function (element, options) { + element = MochiKit.DOM.getElement(element); + var sortableOptions = MochiKit.Sortable.options(element); + options = MochiKit.Base.update({ + tag: sortableOptions.tag, + treeTag: sortableOptions.treeTag, + only: sortableOptions.only, + name: element.id, + format: sortableOptions.format + }, options || {}); + + var root = { + id: null, + parent: null, + children: new Array, + container: element, + position: 0 + } + + return MochiKit.Sortable._tree(element, options, root); + }, + + /** + * Specifies the sequence for the Sortable. + * @param {Node} element Element to use as the Sortable. + * @param {Object} newSequence New sequence to use. + * @param {Object} options Options to use fro the Sortable. + */ + setSequence: function (element, newSequence, options) { + var self = MochiKit.Sortable; + var b = MochiKit.Base; + element = MochiKit.DOM.getElement(element); + options = b.update(self.options(element), options || {}); + + var nodeMap = {}; + b.map(function (n) { + var m = n.id.match(options.format); + if (m) { + nodeMap[m[1]] = [n, n.parentNode]; + } + n.parentNode.removeChild(n); + }, self.findElements(element, options)); + + b.map(function (ident) { + var n = nodeMap[ident]; + if (n) { + n[1].appendChild(n[0]); + delete nodeMap[ident]; + } + }, newSequence); + }, + + /* Construct a [i] index for a particular node */ + _constructIndex: function (node) { + var index = ''; + do { + if (node.id) { + index = '[' + node.position + ']' + index; + } + } while ((node = node.parent) != null); + return index; + }, + + /** @id MochiKit.Sortable.sequence */ + sequence: function (element, options) { + element = MochiKit.DOM.getElement(element); + var self = MochiKit.Sortable; + var options = MochiKit.Base.update(self.options(element), options || {}); + + return MochiKit.Base.map(function (item) { + return item.id.match(options.format) ? item.id.match(options.format)[1] : ''; + }, MochiKit.DOM.getElement(self.findElements(element, options) || [])); + }, + + /** + * Serializes the content of a Sortable. Useful to send this content through a XMLHTTPRequest. + * These options override the Sortable options for the serialization only. + * @param {Node} element Element to serialize. + * @param {Object} options Serialization options. + */ + serialize: function (element, options) { + element = MochiKit.DOM.getElement(element); + var self = MochiKit.Sortable; + options = MochiKit.Base.update(self.options(element), options || {}); + var name = encodeURIComponent(options.name || element.id); + + if (options.tree) { + return MochiKit.Base.flattenArray(MochiKit.Base.map(function (item) { + return [name + self._constructIndex(item) + "[id]=" + + encodeURIComponent(item.id)].concat(item.children.map(arguments.callee)); + }, self.tree(element, options).children)).join('&'); + } else { + return MochiKit.Base.map(function (item) { + return name + "[]=" + encodeURIComponent(item); + }, self.sequence(element, options)).join('&'); + } + } +}); + +// trunk compatibility +MochiKit.Sortable.Sortable = MochiKit.Sortable; diff --git a/dom/tests/mochitest/ajax/mochikit/MochiKit/Style.js b/dom/tests/mochitest/ajax/mochikit/MochiKit/Style.js new file mode 100644 index 0000000000..93e77862af --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/Style.js @@ -0,0 +1,445 @@ +/*** + +MochiKit.Style 1.4 + +See <http://mochikit.com/> for documentation, downloads, license, etc. + +(c) 2005-2006 Bob Ippolito, Beau Hartshorne. All rights Reserved. + +***/ + +if (typeof(dojo) != 'undefined') { + dojo.provide('MochiKit.Style'); + dojo.require('MochiKit.Base'); + dojo.require('MochiKit.DOM'); +} +if (typeof(JSAN) != 'undefined') { + JSAN.use('MochiKit.Base', []); + JSAN.use('MochiKit.DOM', []); +} + +try { + if (typeof(MochiKit.Base) == 'undefined') { + throw ''; + } +} catch (e) { + throw 'MochiKit.Style depends on MochiKit.Base!'; +} + +try { + if (typeof(MochiKit.DOM) == 'undefined') { + throw ''; + } +} catch (e) { + throw 'MochiKit.Style depends on MochiKit.DOM!'; +} + + +if (typeof(MochiKit.Style) == 'undefined') { + MochiKit.Style = {}; +} + +MochiKit.Style.NAME = 'MochiKit.Style'; +MochiKit.Style.VERSION = '1.4'; +MochiKit.Style.__repr__ = function () { + return '[' + this.NAME + ' ' + this.VERSION + ']'; +}; +MochiKit.Style.toString = function () { + return this.__repr__(); +}; + +MochiKit.Style.EXPORT_OK = []; + +MochiKit.Style.EXPORT = [ + 'setStyle', + 'setOpacity', + 'getStyle', + 'getElementDimensions', + 'elementDimensions', // deprecated + 'setElementDimensions', + 'getElementPosition', + 'elementPosition', // deprecated + 'setElementPosition', + 'setDisplayForElement', + 'hideElement', + 'showElement', + 'getViewportDimensions', + 'getViewportPosition', + 'Dimensions', + 'Coordinates' +]; + + +/* + + Dimensions + +*/ +/** @id MochiKit.Style.Dimensions */ +MochiKit.Style.Dimensions = function (w, h) { + this.w = w; + this.h = h; +}; + +MochiKit.Style.Dimensions.prototype.__repr__ = function () { + var repr = MochiKit.Base.repr; + return '{w: ' + repr(this.w) + ', h: ' + repr(this.h) + '}'; +}; + +MochiKit.Style.Dimensions.prototype.toString = function () { + return this.__repr__(); +}; + + +/* + + Coordinates + +*/ +/** @id MochiKit.Style.Coordinates */ +MochiKit.Style.Coordinates = function (x, y) { + this.x = x; + this.y = y; +}; + +MochiKit.Style.Coordinates.prototype.__repr__ = function () { + var repr = MochiKit.Base.repr; + return '{x: ' + repr(this.x) + ', y: ' + repr(this.y) + '}'; +}; + +MochiKit.Style.Coordinates.prototype.toString = function () { + return this.__repr__(); +}; + + +MochiKit.Base.update(MochiKit.Style, { + + /** @id MochiKit.Style.getStyle */ + getStyle: function (elem, cssProperty) { + var dom = MochiKit.DOM; + var d = dom._document; + + elem = dom.getElement(elem); + cssProperty = MochiKit.Base.camelize(cssProperty); + + if (!elem || elem == d) { + return undefined; + } + if (cssProperty == 'opacity' && elem.filters) { + var opacity = (MochiKit.Style.getStyle(elem, 'filter') || '').match(/alpha\(opacity=(.*)\)/); + if (opacity && opacity[1]) { + return parseFloat(opacity[1]) / 100; + } + return 1.0; + } + var value = elem.style ? elem.style[cssProperty] : null; + if (!value) { + if (d.defaultView && d.defaultView.getComputedStyle) { + var css = d.defaultView.getComputedStyle(elem, null); + cssProperty = cssProperty.replace(/([A-Z])/g, '-$1' + ).toLowerCase(); // from dojo.style.toSelectorCase + value = css ? css.getPropertyValue(cssProperty) : null; + } else if (elem.currentStyle) { + value = elem.currentStyle[cssProperty]; + } + } + if (cssProperty == 'opacity') { + value = parseFloat(value); + } + + if (/Opera/.test(navigator.userAgent) && (MochiKit.Base.find(['left', 'top', 'right', 'bottom'], cssProperty) != -1)) { + if (MochiKit.Style.getStyle(elem, 'position') == 'static') { + value = 'auto'; + } + } + + return value == 'auto' ? null : value; + }, + + /** @id MochiKit.Style.setStyle */ + setStyle: function (elem, style) { + elem = MochiKit.DOM.getElement(elem); + for (var name in style) { + if (name == 'opacity') { + MochiKit.Style.setOpacity(elem, style[name]); + } else { + elem.style[MochiKit.Base.camelize(name)] = style[name]; + } + } + }, + + /** @id MochiKit.Style.setOpacity */ + setOpacity: function (elem, o) { + elem = MochiKit.DOM.getElement(elem); + var self = MochiKit.Style; + if (o == 1) { + var toSet = /Gecko/.test(navigator.userAgent) && !(/Konqueror|AppleWebKit|KHTML/.test(navigator.userAgent)); + elem.style["opacity"] = toSet ? 0.999999 : 1.0; + if (/MSIE/.test(navigator.userAgent)) { + elem.style['filter'] = + self.getStyle(elem, 'filter').replace(/alpha\([^\)]*\)/gi, ''); + } + } else { + if (o < 0.00001) { + o = 0; + } + elem.style["opacity"] = o; + if (/MSIE/.test(navigator.userAgent)) { + elem.style['filter'] = + self.getStyle(elem, 'filter').replace(/alpha\([^\)]*\)/gi, '') + 'alpha(opacity=' + o * 100 + ')'; + } + } + }, + + /* + + getElementPosition is adapted from YAHOO.util.Dom.getXY v0.9.0. + Copyright: Copyright (c) 2006, Yahoo! Inc. All rights reserved. + License: BSD, http://developer.yahoo.net/yui/license.txt + + */ + + /** @id MochiKit.Style.getElementPosition */ + getElementPosition: function (elem, /* optional */relativeTo) { + var self = MochiKit.Style; + var dom = MochiKit.DOM; + elem = dom.getElement(elem); + + if (!elem || + (!(elem.x && elem.y) && + (!elem.parentNode === null || + self.getStyle(elem, 'display') == 'none'))) { + return undefined; + } + + var c = new self.Coordinates(0, 0); + var box = null; + var parent = null; + + var d = MochiKit.DOM._document; + var de = d.documentElement; + var b = d.body; + + if (!elem.parentNode && elem.x && elem.y) { + /* it's just a MochiKit.Style.Coordinates object */ + c.x += elem.x || 0; + c.y += elem.y || 0; + } else if (elem.getBoundingClientRect) { // IE shortcut + /* + + The IE shortcut can be off by two. We fix it. See: + http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/getboundingclientrect.asp + + This is similar to the method used in + MochiKit.Signal.Event.mouse(). + + */ + box = elem.getBoundingClientRect(); + + c.x += box.left + + (de.scrollLeft || b.scrollLeft) - + (de.clientLeft || 0); + + c.y += box.top + + (de.scrollTop || b.scrollTop) - + (de.clientTop || 0); + + } else if (elem.offsetParent) { + c.x += elem.offsetLeft; + c.y += elem.offsetTop; + parent = elem.offsetParent; + + if (parent != elem) { + while (parent) { + c.x += parent.offsetLeft; + c.y += parent.offsetTop; + parent = parent.offsetParent; + } + } + + /* + + Opera < 9 and old Safari (absolute) incorrectly account for + body offsetTop and offsetLeft. + + */ + var ua = navigator.userAgent.toLowerCase(); + if ((typeof(opera) != 'undefined' && + parseFloat(opera.version()) < 9) || + (ua.indexOf('AppleWebKit') != -1 && + self.getStyle(elem, 'position') == 'absolute')) { + + c.x -= b.offsetLeft; + c.y -= b.offsetTop; + + } + } + + if (typeof(relativeTo) != 'undefined') { + relativeTo = arguments.callee(relativeTo); + if (relativeTo) { + c.x -= (relativeTo.x || 0); + c.y -= (relativeTo.y || 0); + } + } + + if (elem.parentNode) { + parent = elem.parentNode; + } else { + parent = null; + } + + while (parent) { + var tagName = parent.tagName.toUpperCase(); + if (tagName === 'BODY' || tagName === 'HTML') { + break; + } + var disp = self.getStyle(parent, 'display'); + // Handle strange Opera bug for some display + if (disp != 'inline' && disp != 'table-row') { + c.x -= parent.scrollLeft; + c.y -= parent.scrollTop; + } + if (parent.parentNode) { + parent = parent.parentNode; + } else { + parent = null; + } + } + + return c; + }, + + /** @id MochiKit.Style.setElementPosition */ + setElementPosition: function (elem, newPos/* optional */, units) { + elem = MochiKit.DOM.getElement(elem); + if (typeof(units) == 'undefined') { + units = 'px'; + } + var newStyle = {}; + var isUndefNull = MochiKit.Base.isUndefinedOrNull; + if (!isUndefNull(newPos.x)) { + newStyle['left'] = newPos.x + units; + } + if (!isUndefNull(newPos.y)) { + newStyle['top'] = newPos.y + units; + } + MochiKit.DOM.updateNodeAttributes(elem, {'style': newStyle}); + }, + + /** @id MochiKit.Style.getElementDimensions */ + getElementDimensions: function (elem) { + var self = MochiKit.Style; + var dom = MochiKit.DOM; + if (typeof(elem.w) == 'number' || typeof(elem.h) == 'number') { + return new self.Dimensions(elem.w || 0, elem.h || 0); + } + elem = dom.getElement(elem); + if (!elem) { + return undefined; + } + var disp = self.getStyle(elem, 'display'); + // display can be empty/undefined on WebKit/KHTML + if (disp != 'none' && disp !== '' && typeof(disp) != 'undefined') { + return new self.Dimensions(elem.offsetWidth || 0, + elem.offsetHeight || 0); + } + var s = elem.style; + var originalVisibility = s.visibility; + var originalPosition = s.position; + s.visibility = 'hidden'; + s.position = 'absolute'; + s.display = ''; + var originalWidth = elem.offsetWidth; + var originalHeight = elem.offsetHeight; + s.display = 'none'; + s.position = originalPosition; + s.visibility = originalVisibility; + return new self.Dimensions(originalWidth, originalHeight); + }, + + /** @id MochiKit.Style.setElementDimensions */ + setElementDimensions: function (elem, newSize/* optional */, units) { + elem = MochiKit.DOM.getElement(elem); + if (typeof(units) == 'undefined') { + units = 'px'; + } + var newStyle = {}; + var isUndefNull = MochiKit.Base.isUndefinedOrNull; + if (!isUndefNull(newSize.w)) { + newStyle['width'] = newSize.w + units; + } + if (!isUndefNull(newSize.h)) { + newStyle['height'] = newSize.h + units; + } + MochiKit.DOM.updateNodeAttributes(elem, {'style': newStyle}); + }, + + /** @id MochiKit.Style.setDisplayForElement */ + setDisplayForElement: function (display, element/*, ...*/) { + var elements = MochiKit.Base.extend(null, arguments, 1); + var getElement = MochiKit.DOM.getElement; + for (var i = 0; i < elements.length; i++) { + element = getElement(elements[i]); + if (element) { + element.style.display = display; + } + } + }, + + /** @id MochiKit.Style.getViewportDimensions */ + getViewportDimensions: function () { + var d = new MochiKit.Style.Dimensions(); + + var w = MochiKit.DOM._window; + var b = MochiKit.DOM._document.body; + + if (w.innerWidth) { + d.w = w.innerWidth; + d.h = w.innerHeight; + } else if (b.parentElement.clientWidth) { + d.w = b.parentElement.clientWidth; + d.h = b.parentElement.clientHeight; + } else if (b && b.clientWidth) { + d.w = b.clientWidth; + d.h = b.clientHeight; + } + return d; + }, + + /** @id MochiKit.Style.getViewportPosition */ + getViewportPosition: function () { + var c = new MochiKit.Style.Coordinates(0, 0); + var d = MochiKit.DOM._document; + var de = d.documentElement; + var db = d.body; + if (de && (de.scrollTop || de.scrollLeft)) { + c.x = de.scrollLeft; + c.y = de.scrollTop; + } else if (db) { + c.x = db.scrollLeft; + c.y = db.scrollTop; + } + return c; + }, + + __new__: function () { + var m = MochiKit.Base; + + this.elementPosition = this.getElementPosition; + this.elementDimensions = this.getElementDimensions; + + this.hideElement = m.partial(this.setDisplayForElement, 'none'); + this.showElement = m.partial(this.setDisplayForElement, 'block'); + + this.EXPORT_TAGS = { + ':common': this.EXPORT, + ':all': m.concat(this.EXPORT, this.EXPORT_OK) + }; + + m.nameFunctions(this); + } +}); + +MochiKit.Style.__new__(); +MochiKit.Base._exportSymbols(this, MochiKit.Style); diff --git a/dom/tests/mochitest/ajax/mochikit/MochiKit/Test.js b/dom/tests/mochitest/ajax/mochikit/MochiKit/Test.js new file mode 100644 index 0000000000..494a5b440c --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/Test.js @@ -0,0 +1,181 @@ +/*** + +MochiKit.Test 1.4 + +See <http://mochikit.com/> for documentation, downloads, license, etc. + +(c) 2005 Bob Ippolito. All rights Reserved. + +***/ + +if (typeof(dojo) != 'undefined') { + dojo.provide('MochiKit.Test'); + dojo.require('MochiKit.Base'); +} + +if (typeof(JSAN) != 'undefined') { + JSAN.use("MochiKit.Base", []); +} + +try { + if (typeof(MochiKit.Base) == 'undefined') { + throw ""; + } +} catch (e) { + throw "MochiKit.Test depends on MochiKit.Base!"; +} + +if (typeof(MochiKit.Test) == 'undefined') { + MochiKit.Test = {}; +} + +MochiKit.Test.NAME = "MochiKit.Test"; +MochiKit.Test.VERSION = "1.4"; +MochiKit.Test.__repr__ = function () { + return "[" + this.NAME + " " + this.VERSION + "]"; +}; + +MochiKit.Test.toString = function () { + return this.__repr__(); +}; + + +MochiKit.Test.EXPORT = ["runTests"]; +MochiKit.Test.EXPORT_OK = []; + +MochiKit.Test.runTests = function (obj) { + if (typeof(obj) == "string") { + obj = JSAN.use(obj); + } + var suite = new MochiKit.Test.Suite(); + suite.run(obj); +}; + +MochiKit.Test.Suite = function () { + this.testIndex = 0; + MochiKit.Base.bindMethods(this); +}; + +MochiKit.Test.Suite.prototype = { + run: function (obj) { + try { + obj(this); + } catch (e) { + this.traceback(e); + } + }, + traceback: function (e) { + var items = MochiKit.Iter.sorted(MochiKit.Base.items(e)); + print("not ok " + this.testIndex + " - Error thrown"); + for (var i = 0; i < items.length; i++) { + var kv = items[i]; + if (kv[0] == "stack") { + kv[1] = kv[1].split(/\n/)[0]; + } + this.print("# " + kv.join(": ")); + } + }, + print: function (s) { + print(s); + }, + is: function (got, expected, /* optional */message) { + var res = 1; + var msg = null; + try { + res = MochiKit.Base.compare(got, expected); + } catch (e) { + msg = "Can not compare " + typeof(got) + ":" + typeof(expected); + } + if (res) { + msg = "Expected value did not compare equal"; + } + if (!res) { + return this.testResult(true, message); + } + return this.testResult(false, message, + [[msg], ["got:", got], ["expected:", expected]]); + }, + + testResult: function (pass, msg, failures) { + this.testIndex += 1; + if (pass) { + this.print("ok " + this.testIndex + " - " + msg); + return; + } + this.print("not ok " + this.testIndex + " - " + msg); + if (failures) { + for (var i = 0; i < failures.length; i++) { + this.print("# " + failures[i].join(" ")); + } + } + }, + + isDeeply: function (got, expected, /* optional */message) { + var m = MochiKit.Base; + var res = 1; + try { + res = m.compare(got, expected); + } catch (e) { + // pass + } + if (res === 0) { + return this.ok(true, message); + } + var gk = m.keys(got); + var ek = m.keys(expected); + gk.sort(); + ek.sort(); + if (m.compare(gk, ek)) { + // differing keys + var cmp = {}; + var i; + for (i = 0; i < gk.length; i++) { + cmp[gk[i]] = "got"; + } + for (i = 0; i < ek.length; i++) { + if (ek[i] in cmp) { + delete cmp[ek[i]]; + } else { + cmp[ek[i]] = "expected"; + } + } + var diffkeys = m.keys(cmp); + diffkeys.sort(); + var gotkeys = []; + var expkeys = []; + while (diffkeys.length) { + var k = diffkeys.shift(); + if (k in Object.prototype) { + continue; + } + (cmp[k] == "got" ? gotkeys : expkeys).push(k); + } + + + } + + return this.testResult((!res), msg, + (msg ? [["got:", got], ["expected:", expected]] : undefined) + ); + }, + + ok: function (res, message) { + return this.testResult(res, message); + } +}; + +MochiKit.Test.__new__ = function () { + var m = MochiKit.Base; + + this.EXPORT_TAGS = { + ":common": this.EXPORT, + ":all": m.concat(this.EXPORT, this.EXPORT_OK) + }; + + m.nameFunctions(this); + +}; + +MochiKit.Test.__new__(); + +MochiKit.Base._exportSymbols(this, MochiKit.Test); diff --git a/dom/tests/mochitest/ajax/mochikit/MochiKit/Visual.js b/dom/tests/mochitest/ajax/mochikit/MochiKit/Visual.js new file mode 100644 index 0000000000..b7d8d87b22 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/Visual.js @@ -0,0 +1,1981 @@ +/*** + +MochiKit.Visual 1.4 + +See <http://mochikit.com/> for documentation, downloads, license, etc. + +(c) 2005 Bob Ippolito and others. All rights Reserved. + +***/ + +if (typeof(dojo) != 'undefined') { + dojo.provide('MochiKit.Visual'); + dojo.require('MochiKit.Base'); + dojo.require('MochiKit.DOM'); + dojo.require('MochiKit.Style'); + dojo.require('MochiKit.Color'); + dojo.require('MochiKit.Position'); +} + +if (typeof(JSAN) != 'undefined') { + JSAN.use("MochiKit.Base", []); + JSAN.use("MochiKit.DOM", []); + JSAN.use("MochiKit.Style", []); + JSAN.use("MochiKit.Color", []); + JSAN.use("MochiKit.Position", []); +} + +try { + if (typeof(MochiKit.Base) === 'undefined' || + typeof(MochiKit.DOM) === 'undefined' || + typeof(MochiKit.Style) === 'undefined' || + typeof(MochiKit.Position) === 'undefined' || + typeof(MochiKit.Color) === 'undefined') { + throw ""; + } +} catch (e) { + throw "MochiKit.Visual depends on MochiKit.Base, MochiKit.DOM, MochiKit.Style, MochiKit.Position and MochiKit.Color!"; +} + +if (typeof(MochiKit.Visual) == "undefined") { + MochiKit.Visual = {}; +} + +MochiKit.Visual.NAME = "MochiKit.Visual"; +MochiKit.Visual.VERSION = "1.4"; + +MochiKit.Visual.__repr__ = function () { + return "[" + this.NAME + " " + this.VERSION + "]"; +}; + +MochiKit.Visual.toString = function () { + return this.__repr__(); +}; + +MochiKit.Visual._RoundCorners = function (e, options) { + e = MochiKit.DOM.getElement(e); + this._setOptions(options); + if (this.options.__unstable__wrapElement) { + e = this._doWrap(e); + } + + var color = this.options.color; + var C = MochiKit.Color.Color; + if (this.options.color === "fromElement") { + color = C.fromBackground(e); + } else if (!(color instanceof C)) { + color = C.fromString(color); + } + this.isTransparent = (color.asRGB().a <= 0); + + var bgColor = this.options.bgColor; + if (this.options.bgColor === "fromParent") { + bgColor = C.fromBackground(e.offsetParent); + } else if (!(bgColor instanceof C)) { + bgColor = C.fromString(bgColor); + } + + this._roundCornersImpl(e, color, bgColor); +}; + +MochiKit.Visual._RoundCorners.prototype = { + _doWrap: function (e) { + var parent = e.parentNode; + var doc = MochiKit.DOM.currentDocument(); + if (typeof(doc.defaultView) === "undefined" + || doc.defaultView === null) { + return e; + } + var style = doc.defaultView.getComputedStyle(e, null); + if (typeof(style) === "undefined" || style === null) { + return e; + } + var wrapper = MochiKit.DOM.DIV({"style": { + display: "block", + // convert padding to margin + marginTop: style.getPropertyValue("padding-top"), + marginRight: style.getPropertyValue("padding-right"), + marginBottom: style.getPropertyValue("padding-bottom"), + marginLeft: style.getPropertyValue("padding-left"), + // remove padding so the rounding looks right + padding: "0px" + /* + paddingRight: "0px", + paddingLeft: "0px" + */ + }}); + wrapper.innerHTML = e.innerHTML; + e.innerHTML = ""; + e.appendChild(wrapper); + return e; + }, + + _roundCornersImpl: function (e, color, bgColor) { + if (this.options.border) { + this._renderBorder(e, bgColor); + } + if (this._isTopRounded()) { + this._roundTopCorners(e, color, bgColor); + } + if (this._isBottomRounded()) { + this._roundBottomCorners(e, color, bgColor); + } + }, + + _renderBorder: function (el, bgColor) { + var borderValue = "1px solid " + this._borderColor(bgColor); + var borderL = "border-left: " + borderValue; + var borderR = "border-right: " + borderValue; + var style = "style='" + borderL + ";" + borderR + "'"; + el.innerHTML = "<div " + style + ">" + el.innerHTML + "</div>"; + }, + + _roundTopCorners: function (el, color, bgColor) { + var corner = this._createCorner(bgColor); + for (var i = 0; i < this.options.numSlices; i++) { + corner.appendChild( + this._createCornerSlice(color, bgColor, i, "top") + ); + } + el.style.paddingTop = 0; + el.insertBefore(corner, el.firstChild); + }, + + _roundBottomCorners: function (el, color, bgColor) { + var corner = this._createCorner(bgColor); + for (var i = (this.options.numSlices - 1); i >= 0; i--) { + corner.appendChild( + this._createCornerSlice(color, bgColor, i, "bottom") + ); + } + el.style.paddingBottom = 0; + el.appendChild(corner); + }, + + _createCorner: function (bgColor) { + var dom = MochiKit.DOM; + return dom.DIV({style: {backgroundColor: bgColor.toString()}}); + }, + + _createCornerSlice: function (color, bgColor, n, position) { + var slice = MochiKit.DOM.SPAN(); + + var inStyle = slice.style; + inStyle.backgroundColor = color.toString(); + inStyle.display = "block"; + inStyle.height = "1px"; + inStyle.overflow = "hidden"; + inStyle.fontSize = "1px"; + + var borderColor = this._borderColor(color, bgColor); + if (this.options.border && n === 0) { + inStyle.borderTopStyle = "solid"; + inStyle.borderTopWidth = "1px"; + inStyle.borderLeftWidth = "0px"; + inStyle.borderRightWidth = "0px"; + inStyle.borderBottomWidth = "0px"; + // assumes css compliant box model + inStyle.height = "0px"; + inStyle.borderColor = borderColor.toString(); + } else if (borderColor) { + inStyle.borderColor = borderColor.toString(); + inStyle.borderStyle = "solid"; + inStyle.borderWidth = "0px 1px"; + } + + if (!this.options.compact && (n == (this.options.numSlices - 1))) { + inStyle.height = "2px"; + } + + this._setMargin(slice, n, position); + this._setBorder(slice, n, position); + + return slice; + }, + + _setOptions: function (options) { + this.options = { + corners: "all", + color: "fromElement", + bgColor: "fromParent", + blend: true, + border: false, + compact: false, + __unstable__wrapElement: false + }; + MochiKit.Base.update(this.options, options); + + this.options.numSlices = (this.options.compact ? 2 : 4); + }, + + _whichSideTop: function () { + var corners = this.options.corners; + if (this._hasString(corners, "all", "top")) { + return ""; + } + + var has_tl = (corners.indexOf("tl") != -1); + var has_tr = (corners.indexOf("tr") != -1); + if (has_tl && has_tr) { + return ""; + } + if (has_tl) { + return "left"; + } + if (has_tr) { + return "right"; + } + return ""; + }, + + _whichSideBottom: function () { + var corners = this.options.corners; + if (this._hasString(corners, "all", "bottom")) { + return ""; + } + + var has_bl = (corners.indexOf('bl') != -1); + var has_br = (corners.indexOf('br') != -1); + if (has_bl && has_br) { + return ""; + } + if (has_bl) { + return "left"; + } + if (has_br) { + return "right"; + } + return ""; + }, + + _borderColor: function (color, bgColor) { + if (color == "transparent") { + return bgColor; + } else if (this.options.border) { + return this.options.border; + } else if (this.options.blend) { + return bgColor.blendedColor(color); + } + return ""; + }, + + + _setMargin: function (el, n, corners) { + var marginSize = this._marginSize(n) + "px"; + var whichSide = ( + corners == "top" ? this._whichSideTop() : this._whichSideBottom() + ); + var style = el.style; + + if (whichSide == "left") { + style.marginLeft = marginSize; + style.marginRight = "0px"; + } else if (whichSide == "right") { + style.marginRight = marginSize; + style.marginLeft = "0px"; + } else { + style.marginLeft = marginSize; + style.marginRight = marginSize; + } + }, + + _setBorder: function (el, n, corners) { + var borderSize = this._borderSize(n) + "px"; + var whichSide = ( + corners == "top" ? this._whichSideTop() : this._whichSideBottom() + ); + + var style = el.style; + if (whichSide == "left") { + style.borderLeftWidth = borderSize; + style.borderRightWidth = "0px"; + } else if (whichSide == "right") { + style.borderRightWidth = borderSize; + style.borderLeftWidth = "0px"; + } else { + style.borderLeftWidth = borderSize; + style.borderRightWidth = borderSize; + } + }, + + _marginSize: function (n) { + if (this.isTransparent) { + return 0; + } + + var o = this.options; + if (o.compact && o.blend) { + var smBlendedMarginSizes = [1, 0]; + return smBlendedMarginSizes[n]; + } else if (o.compact) { + var compactMarginSizes = [2, 1]; + return compactMarginSizes[n]; + } else if (o.blend) { + var blendedMarginSizes = [3, 2, 1, 0]; + return blendedMarginSizes[n]; + } else { + var marginSizes = [5, 3, 2, 1]; + return marginSizes[n]; + } + }, + + _borderSize: function (n) { + var o = this.options; + var borderSizes; + if (o.compact && (o.blend || this.isTransparent)) { + return 1; + } else if (o.compact) { + borderSizes = [1, 0]; + } else if (o.blend) { + borderSizes = [2, 1, 1, 1]; + } else if (o.border) { + borderSizes = [0, 2, 0, 0]; + } else if (this.isTransparent) { + borderSizes = [5, 3, 2, 1]; + } else { + return 0; + } + return borderSizes[n]; + }, + + _hasString: function (str) { + for (var i = 1; i< arguments.length; i++) { + if (str.indexOf(arguments[i]) != -1) { + return true; + } + } + return false; + }, + + _isTopRounded: function () { + return this._hasString(this.options.corners, + "all", "top", "tl", "tr" + ); + }, + + _isBottomRounded: function () { + return this._hasString(this.options.corners, + "all", "bottom", "bl", "br" + ); + }, + + _hasSingleTextChild: function (el) { + return (el.childNodes.length == 1 && el.childNodes[0].nodeType == 3); + } +}; + +/** @id MochiKit.Visual.roundElement */ +MochiKit.Visual.roundElement = function (e, options) { + new MochiKit.Visual._RoundCorners(e, options); +}; + +/** @id MochiKit.Visual.roundClass */ +MochiKit.Visual.roundClass = function (tagName, className, options) { + var elements = MochiKit.DOM.getElementsByTagAndClassName( + tagName, className + ); + for (var i = 0; i < elements.length; i++) { + MochiKit.Visual.roundElement(elements[i], options); + } +}; + +/** @id MochiKit.Visual.tagifyText */ +MochiKit.Visual.tagifyText = function (element, /* optional */tagifyStyle) { + /*** + + Change a node text to character in tags. + + @param tagifyStyle: the style to apply to character nodes, default to + 'position: relative'. + + ***/ + tagifyStyle = tagifyStyle || 'position:relative'; + if (/MSIE/.test(navigator.userAgent)) { + tagifyStyle += ';zoom:1'; + } + element = MochiKit.DOM.getElement(element); + var ma = MochiKit.Base.map; + ma(function (child) { + if (child.nodeType == 3) { + ma(function (character) { + element.insertBefore( + MochiKit.DOM.SPAN({style: tagifyStyle}, + character == ' ' ? String.fromCharCode(160) : character), child); + }, child.nodeValue.split('')); + MochiKit.DOM.removeElement(child); + } + }, element.childNodes); +}; + +/** @id MochiKit.Visual.forceRerendering */ +MochiKit.Visual.forceRerendering = function (element) { + try { + element = MochiKit.DOM.getElement(element); + var n = document.createTextNode(' '); + element.appendChild(n); + element.removeChild(n); + } catch(e) { + } +}; + +/** @id MochiKit.Visual.multiple */ +MochiKit.Visual.multiple = function (elements, effect, /* optional */options) { + /*** + + Launch the same effect subsequently on given elements. + + ***/ + options = MochiKit.Base.update({ + speed: 0.1, delay: 0.0 + }, options || {}); + var masterDelay = options.delay; + var index = 0; + MochiKit.Base.map(function (innerelement) { + options.delay = index * options.speed + masterDelay; + new effect(innerelement, options); + index += 1; + }, elements); +}; + +MochiKit.Visual.PAIRS = { + 'slide': ['slideDown', 'slideUp'], + 'blind': ['blindDown', 'blindUp'], + 'appear': ['appear', 'fade'], + 'size': ['grow', 'shrink'] +}; + +/** @id MochiKit.Visual.toggle */ +MochiKit.Visual.toggle = function (element, /* optional */effect, /* optional */options) { + /*** + + Toggle an item between two state depending of its visibility, making + a effect between these states. Default effect is 'appear', can be + 'slide' or 'blind'. + + ***/ + element = MochiKit.DOM.getElement(element); + effect = (effect || 'appear').toLowerCase(); + options = MochiKit.Base.update({ + queue: {position: 'end', scope: (element.id || 'global'), limit: 1} + }, options || {}); + var v = MochiKit.Visual; + v[MochiKit.Style.getStyle(element, 'display') != 'none' ? + v.PAIRS[effect][1] : v.PAIRS[effect][0]](element, options); +}; + +/*** + +Transitions: define functions calculating variations depending of a position. + +***/ + +MochiKit.Visual.Transitions = {}; + +/** @id MochiKit.Visual.Transitions.linear */ +MochiKit.Visual.Transitions.linear = function (pos) { + return pos; +}; + +/** @id MochiKit.Visual.Transitions.sinoidal */ +MochiKit.Visual.Transitions.sinoidal = function (pos) { + return (-Math.cos(pos*Math.PI)/2) + 0.5; +}; + +/** @id MochiKit.Visual.Transitions.reverse */ +MochiKit.Visual.Transitions.reverse = function (pos) { + return 1 - pos; +}; + +/** @id MochiKit.Visual.Transitions.flicker */ +MochiKit.Visual.Transitions.flicker = function (pos) { + return ((-Math.cos(pos*Math.PI)/4) + 0.75) + Math.random()/4; +}; + +/** @id MochiKit.Visual.Transitions.wobble */ +MochiKit.Visual.Transitions.wobble = function (pos) { + return (-Math.cos(pos*Math.PI*(9*pos))/2) + 0.5; +}; + +/** @id MochiKit.Visual.Transitions.pulse */ +MochiKit.Visual.Transitions.pulse = function (pos, pulses) { + if (!pulses) { + return (Math.floor(pos*10) % 2 === 0 ? + (pos*10 - Math.floor(pos*10)) : 1 - (pos*10 - Math.floor(pos*10))); + } + return (Math.round((pos % (1/pulses)) * pulses) == 0 ? + ((pos * pulses * 2) - Math.floor(pos * pulses * 2)) : + 1 - ((pos * pulses * 2) - Math.floor(pos * pulses * 2))); +}; + +/** @id MochiKit.Visual.Transitions.none */ +MochiKit.Visual.Transitions.none = function (pos) { + return 0; +}; + +/** @id MochiKit.Visual.Transitions.full */ +MochiKit.Visual.Transitions.full = function (pos) { + return 1; +}; + +/*** + +Core effects + +***/ + +MochiKit.Visual.ScopedQueue = function () { + var cls = arguments.callee; + if (!(this instanceof cls)) { + return new cls(); + } + this.__init__(); +}; + +MochiKit.Base.update(MochiKit.Visual.ScopedQueue.prototype, { + __init__: function () { + this.effects = []; + this.interval = null; + }, + + /** @id MochiKit.Visual.ScopedQueue.prototype.add */ + add: function (effect) { + var timestamp = new Date().getTime(); + + var position = (typeof(effect.options.queue) == 'string') ? + effect.options.queue : effect.options.queue.position; + + var ma = MochiKit.Base.map; + switch (position) { + case 'front': + // move unstarted effects after this effect + ma(function (e) { + if (e.state == 'idle') { + e.startOn += effect.finishOn; + e.finishOn += effect.finishOn; + } + }, this.effects); + break; + case 'end': + var finish; + // start effect after last queued effect has finished + ma(function (e) { + var i = e.finishOn; + if (i >= (finish || i)) { + finish = i; + } + }, this.effects); + timestamp = finish || timestamp; + break; + case 'break': + ma(function (e) { + e.finalize(); + }, this.effects); + break; + } + + effect.startOn += timestamp; + effect.finishOn += timestamp; + if (!effect.options.queue.limit || + this.effects.length < effect.options.queue.limit) { + this.effects.push(effect); + } + + if (!this.interval) { + this.interval = this.startLoop(MochiKit.Base.bind(this.loop, this), + 40); + } + }, + + /** @id MochiKit.Visual.ScopedQueue.prototype.startLoop */ + startLoop: function (func, interval) { + return setInterval(func, interval); + }, + + /** @id MochiKit.Visual.ScopedQueue.prototype.remove */ + remove: function (effect) { + this.effects = MochiKit.Base.filter(function (e) { + return e != effect; + }, this.effects); + if (!this.effects.length) { + this.stopLoop(this.interval); + this.interval = null; + } + }, + + /** @id MochiKit.Visual.ScopedQueue.prototype.stopLoop */ + stopLoop: function (interval) { + clearInterval(interval); + }, + + /** @id MochiKit.Visual.ScopedQueue.prototype.loop */ + loop: function () { + var timePos = new Date().getTime(); + MochiKit.Base.map(function (effect) { + effect.loop(timePos); + }, this.effects); + } +}); + +MochiKit.Visual.Queues = { + instances: {}, + + get: function (queueName) { + if (typeof(queueName) != 'string') { + return queueName; + } + + if (!this.instances[queueName]) { + this.instances[queueName] = new MochiKit.Visual.ScopedQueue(); + } + return this.instances[queueName]; + } +}; + +MochiKit.Visual.Queue = MochiKit.Visual.Queues.get('global'); + +MochiKit.Visual.DefaultOptions = { + transition: MochiKit.Visual.Transitions.sinoidal, + duration: 1.0, // seconds + fps: 25.0, // max. 25fps due to MochiKit.Visual.Queue implementation + sync: false, // true for combining + from: 0.0, + to: 1.0, + delay: 0.0, + queue: 'parallel' +}; + +MochiKit.Visual.Base = function () {}; + +MochiKit.Visual.Base.prototype = { + /*** + + Basic class for all Effects. Define a looping mechanism called for each step + of an effect. Don't instantiate it, only subclass it. + + ***/ + + __class__ : MochiKit.Visual.Base, + + /** @id MochiKit.Visual.Base.prototype.start */ + start: function (options) { + var v = MochiKit.Visual; + this.options = MochiKit.Base.setdefault(options || {}, + v.DefaultOptions); + this.currentFrame = 0; + this.state = 'idle'; + this.startOn = this.options.delay*1000; + this.finishOn = this.startOn + (this.options.duration*1000); + this.event('beforeStart'); + if (!this.options.sync) { + v.Queues.get(typeof(this.options.queue) == 'string' ? + 'global' : this.options.queue.scope).add(this); + } + }, + + /** @id MochiKit.Visual.Base.prototype.loop */ + loop: function (timePos) { + if (timePos >= this.startOn) { + if (timePos >= this.finishOn) { + return this.finalize(); + } + var pos = (timePos - this.startOn) / (this.finishOn - this.startOn); + var frame = + Math.round(pos * this.options.fps * this.options.duration); + if (frame > this.currentFrame) { + this.render(pos); + this.currentFrame = frame; + } + } + }, + + /** @id MochiKit.Visual.Base.prototype.render */ + render: function (pos) { + if (this.state == 'idle') { + this.state = 'running'; + this.event('beforeSetup'); + this.setup(); + this.event('afterSetup'); + } + if (this.state == 'running') { + if (this.options.transition) { + pos = this.options.transition(pos); + } + pos *= (this.options.to - this.options.from); + pos += this.options.from; + this.event('beforeUpdate'); + this.update(pos); + this.event('afterUpdate'); + } + }, + + /** @id MochiKit.Visual.Base.prototype.cancel */ + cancel: function () { + if (!this.options.sync) { + MochiKit.Visual.Queues.get(typeof(this.options.queue) == 'string' ? + 'global' : this.options.queue.scope).remove(this); + } + this.state = 'finished'; + }, + + /** @id MochiKit.Visual.Base.prototype.finalize */ + finalize: function () { + this.render(1.0); + this.cancel(); + this.event('beforeFinish'); + this.finish(); + this.event('afterFinish'); + }, + + setup: function () { + }, + + finish: function () { + }, + + update: function (position) { + }, + + /** @id MochiKit.Visual.Base.prototype.event */ + event: function (eventName) { + if (this.options[eventName + 'Internal']) { + this.options[eventName + 'Internal'](this); + } + if (this.options[eventName]) { + this.options[eventName](this); + } + }, + + /** @id MochiKit.Visual.Base.prototype.repr */ + repr: function () { + return '[' + this.__class__.NAME + ', options:' + + MochiKit.Base.repr(this.options) + ']'; + } +}; + + /** @id MochiKit.Visual.Parallel */ +MochiKit.Visual.Parallel = function (effects, options) { + var cls = arguments.callee; + if (!(this instanceof cls)) { + return new cls(effects, options); + } + + this.__init__(effects, options); +}; + +MochiKit.Visual.Parallel.prototype = new MochiKit.Visual.Base(); + +MochiKit.Base.update(MochiKit.Visual.Parallel.prototype, { + /*** + + Run multiple effects at the same time. + + ***/ + + __class__ : MochiKit.Visual.Parallel, + + __init__: function (effects, options) { + this.effects = effects || []; + this.start(options); + }, + + /** @id MochiKit.Visual.Parallel.prototype.update */ + update: function (position) { + MochiKit.Base.map(function (effect) { + effect.render(position); + }, this.effects); + }, + + /** @id MochiKit.Visual.Parallel.prototype.finish */ + finish: function () { + MochiKit.Base.map(function (effect) { + effect.finalize(); + }, this.effects); + } +}); + +/** @id MochiKit.Visual.Opacity */ +MochiKit.Visual.Opacity = function (element, options) { + var cls = arguments.callee; + if (!(this instanceof cls)) { + return new cls(element, options); + } + this.__init__(element, options); +}; + +MochiKit.Visual.Opacity.prototype = new MochiKit.Visual.Base(); + +MochiKit.Base.update(MochiKit.Visual.Opacity.prototype, { + /*** + + Change the opacity of an element. + + @param options: 'from' and 'to' change the starting and ending opacities. + Must be between 0.0 and 1.0. Default to current opacity and 1.0. + + ***/ + + __class__ : MochiKit.Visual.Opacity, + + __init__: function (element, /* optional */options) { + var b = MochiKit.Base; + var s = MochiKit.Style; + this.element = MochiKit.DOM.getElement(element); + // make this work on IE on elements without 'layout' + if (this.element.currentStyle && + (!this.element.currentStyle.hasLayout)) { + s.setStyle(this.element, {zoom: 1}); + } + options = b.update({ + from: s.getStyle(this.element, 'opacity') || 0.0, + to: 1.0 + }, options || {}); + this.start(options); + }, + + /** @id MochiKit.Visual.Opacity.prototype.update */ + update: function (position) { + MochiKit.Style.setStyle(this.element, {'opacity': position}); + } +}); + +/** @id MochiKit.Visual.Move.prototype */ +MochiKit.Visual.Move = function (element, options) { + var cls = arguments.callee; + if (!(this instanceof cls)) { + return new cls(element, options); + } + this.__init__(element, options); +}; + +MochiKit.Visual.Move.prototype = new MochiKit.Visual.Base(); + +MochiKit.Base.update(MochiKit.Visual.Move.prototype, { + /*** + + Move an element between its current position to a defined position + + @param options: 'x' and 'y' for final positions, default to 0, 0. + + ***/ + + __class__ : MochiKit.Visual.Move, + + __init__: function (element, /* optional */options) { + this.element = MochiKit.DOM.getElement(element); + options = MochiKit.Base.update({ + x: 0, + y: 0, + mode: 'relative' + }, options || {}); + this.start(options); + }, + + /** @id MochiKit.Visual.Move.prototype.setup */ + setup: function () { + // Bug in Opera: Opera returns the 'real' position of a static element + // or relative element that does not have top/left explicitly set. + // ==> Always set top and left for position relative elements in your + // stylesheets (to 0 if you do not need them) + MochiKit.DOM.makePositioned(this.element); + + var s = this.element.style; + var originalVisibility = s.visibility; + var originalDisplay = s.display; + if (originalDisplay == 'none') { + s.visibility = 'hidden'; + s.display = ''; + } + + this.originalLeft = parseFloat(MochiKit.Style.getStyle(this.element, 'left') || '0'); + this.originalTop = parseFloat(MochiKit.Style.getStyle(this.element, 'top') || '0'); + + if (this.options.mode == 'absolute') { + // absolute movement, so we need to calc deltaX and deltaY + this.options.x -= this.originalLeft; + this.options.y -= this.originalTop; + } + if (originalDisplay == 'none') { + s.visibility = originalVisibility; + s.display = originalDisplay; + } + }, + + /** @id MochiKit.Visual.Move.prototype.update */ + update: function (position) { + MochiKit.Style.setStyle(this.element, { + left: Math.round(this.options.x * position + this.originalLeft) + 'px', + top: Math.round(this.options.y * position + this.originalTop) + 'px' + }); + } +}); + +/** @id MochiKit.Visual.Scale */ +MochiKit.Visual.Scale = function (element, percent, options) { + var cls = arguments.callee; + if (!(this instanceof cls)) { + return new cls(element, percent, options); + } + this.__init__(element, percent, options); +}; + +MochiKit.Visual.Scale.prototype = new MochiKit.Visual.Base(); + +MochiKit.Base.update(MochiKit.Visual.Scale.prototype, { + /*** + + Change the size of an element. + + @param percent: final_size = percent*original_size + + @param options: several options changing scale behaviour + + ***/ + + __class__ : MochiKit.Visual.Scale, + + __init__: function (element, percent, /* optional */options) { + this.element = MochiKit.DOM.getElement(element); + options = MochiKit.Base.update({ + scaleX: true, + scaleY: true, + scaleContent: true, + scaleFromCenter: false, + scaleMode: 'box', // 'box' or 'contents' or {} with provided values + scaleFrom: 100.0, + scaleTo: percent + }, options || {}); + this.start(options); + }, + + /** @id MochiKit.Visual.Scale.prototype.setup */ + setup: function () { + this.restoreAfterFinish = this.options.restoreAfterFinish || false; + this.elementPositioning = MochiKit.Style.getStyle(this.element, + 'position'); + + var ma = MochiKit.Base.map; + var b = MochiKit.Base.bind; + this.originalStyle = {}; + ma(b(function (k) { + this.originalStyle[k] = this.element.style[k]; + }, this), ['top', 'left', 'width', 'height', 'fontSize']); + + this.originalTop = this.element.offsetTop; + this.originalLeft = this.element.offsetLeft; + + var fontSize = MochiKit.Style.getStyle(this.element, + 'font-size') || '100%'; + ma(b(function (fontSizeType) { + if (fontSize.indexOf(fontSizeType) > 0) { + this.fontSize = parseFloat(fontSize); + this.fontSizeType = fontSizeType; + } + }, this), ['em', 'px', '%']); + + this.factor = (this.options.scaleTo - this.options.scaleFrom)/100; + + if (/^content/.test(this.options.scaleMode)) { + this.dims = [this.element.scrollHeight, this.element.scrollWidth]; + } else if (this.options.scaleMode == 'box') { + this.dims = [this.element.offsetHeight, this.element.offsetWidth]; + } else { + this.dims = [this.options.scaleMode.originalHeight, + this.options.scaleMode.originalWidth]; + } + }, + + /** @id MochiKit.Visual.Scale.prototype.update */ + update: function (position) { + var currentScale = (this.options.scaleFrom/100.0) + + (this.factor * position); + if (this.options.scaleContent && this.fontSize) { + MochiKit.Style.setStyle(this.element, { + fontSize: this.fontSize * currentScale + this.fontSizeType + }); + } + this.setDimensions(this.dims[0] * currentScale, + this.dims[1] * currentScale); + }, + + /** @id MochiKit.Visual.Scale.prototype.finish */ + finish: function () { + if (this.restoreAfterFinish) { + MochiKit.Style.setStyle(this.element, this.originalStyle); + } + }, + + /** @id MochiKit.Visual.Scale.prototype.setDimensions */ + setDimensions: function (height, width) { + var d = {}; + var r = Math.round; + if (/MSIE/.test(navigator.userAgent)) { + r = Math.ceil; + } + if (this.options.scaleX) { + d.width = r(width) + 'px'; + } + if (this.options.scaleY) { + d.height = r(height) + 'px'; + } + if (this.options.scaleFromCenter) { + var topd = (height - this.dims[0])/2; + var leftd = (width - this.dims[1])/2; + if (this.elementPositioning == 'absolute') { + if (this.options.scaleY) { + d.top = this.originalTop - topd + 'px'; + } + if (this.options.scaleX) { + d.left = this.originalLeft - leftd + 'px'; + } + } else { + if (this.options.scaleY) { + d.top = -topd + 'px'; + } + if (this.options.scaleX) { + d.left = -leftd + 'px'; + } + } + } + MochiKit.Style.setStyle(this.element, d); + } +}); + +/** @id MochiKit.Visual.Highlight */ +MochiKit.Visual.Highlight = function (element, options) { + var cls = arguments.callee; + if (!(this instanceof cls)) { + return new cls(element, options); + } + this.__init__(element, options); +}; + +MochiKit.Visual.Highlight.prototype = new MochiKit.Visual.Base(); + +MochiKit.Base.update(MochiKit.Visual.Highlight.prototype, { + /*** + + Highlight an item of the page. + + @param options: 'startcolor' for choosing highlighting color, default + to '#ffff99'. + + ***/ + + __class__ : MochiKit.Visual.Highlight, + + __init__: function (element, /* optional */options) { + this.element = MochiKit.DOM.getElement(element); + options = MochiKit.Base.update({ + startcolor: '#ffff99' + }, options || {}); + this.start(options); + }, + + /** @id MochiKit.Visual.Highlight.prototype.setup */ + setup: function () { + var b = MochiKit.Base; + var s = MochiKit.Style; + // Prevent executing on elements not in the layout flow + if (s.getStyle(this.element, 'display') == 'none') { + this.cancel(); + return; + } + // Disable background image during the effect + this.oldStyle = { + backgroundImage: s.getStyle(this.element, 'background-image') + }; + s.setStyle(this.element, { + backgroundImage: 'none' + }); + + if (!this.options.endcolor) { + this.options.endcolor = + MochiKit.Color.Color.fromBackground(this.element).toHexString(); + } + if (b.isUndefinedOrNull(this.options.restorecolor)) { + this.options.restorecolor = s.getStyle(this.element, + 'background-color'); + } + // init color calculations + this._base = b.map(b.bind(function (i) { + return parseInt( + this.options.startcolor.slice(i*2 + 1, i*2 + 3), 16); + }, this), [0, 1, 2]); + this._delta = b.map(b.bind(function (i) { + return parseInt(this.options.endcolor.slice(i*2 + 1, i*2 + 3), 16) + - this._base[i]; + }, this), [0, 1, 2]); + }, + + /** @id MochiKit.Visual.Highlight.prototype.update */ + update: function (position) { + var m = '#'; + MochiKit.Base.map(MochiKit.Base.bind(function (i) { + m += MochiKit.Color.toColorPart(Math.round(this._base[i] + + this._delta[i]*position)); + }, this), [0, 1, 2]); + MochiKit.Style.setStyle(this.element, { + backgroundColor: m + }); + }, + + /** @id MochiKit.Visual.Highlight.prototype.finish */ + finish: function () { + MochiKit.Style.setStyle(this.element, + MochiKit.Base.update(this.oldStyle, { + backgroundColor: this.options.restorecolor + })); + } +}); + +/** @id MochiKit.Visual.ScrollTo */ +MochiKit.Visual.ScrollTo = function (element, options) { + var cls = arguments.callee; + if (!(this instanceof cls)) { + return new cls(element, options); + } + this.__init__(element, options); +}; + +MochiKit.Visual.ScrollTo.prototype = new MochiKit.Visual.Base(); + +MochiKit.Base.update(MochiKit.Visual.ScrollTo.prototype, { + /*** + + Scroll to an element in the page. + + ***/ + + __class__ : MochiKit.Visual.ScrollTo, + + __init__: function (element, /* optional */options) { + this.element = MochiKit.DOM.getElement(element); + this.start(options || {}); + }, + + /** @id MochiKit.Visual.ScrollTo.prototype.setup */ + setup: function () { + var p = MochiKit.Position; + p.prepare(); + var offsets = p.cumulativeOffset(this.element); + if (this.options.offset) { + offsets.y += this.options.offset; + } + var max; + if (window.innerHeight) { + max = window.innerHeight - window.height; + } else if (document.documentElement && + document.documentElement.clientHeight) { + max = document.documentElement.clientHeight - + document.body.scrollHeight; + } else if (document.body) { + max = document.body.clientHeight - document.body.scrollHeight; + } + this.scrollStart = p.windowOffset.y; + this.delta = (offsets.y > max ? max : offsets.y) - this.scrollStart; + }, + + /** @id MochiKit.Visual.ScrollTo.prototype.update */ + update: function (position) { + var p = MochiKit.Position; + p.prepare(); + window.scrollTo(p.windowOffset.x, this.scrollStart + (position * this.delta)); + } +}); + +MochiKit.Visual.CSS_LENGTH = /^(([\+\-]?[0-9\.]+)(em|ex|px|in|cm|mm|pt|pc|\%))|0$/; + +MochiKit.Visual.Morph = function (element, options) { + var cls = arguments.callee; + if (!(this instanceof cls)) { + return new cls(element, options); + } + this.__init__(element, options); +}; + +MochiKit.Visual.Morph.prototype = new MochiKit.Visual.Base(); + +MochiKit.Base.update(MochiKit.Visual.Morph.prototype, { + /*** + + Morph effect: make a transformation from current style to the given style, + automatically making a transition between the two. + + ***/ + + __class__ : MochiKit.Visual.Morph, + + __init__: function (element, /* optional */options) { + this.element = MochiKit.DOM.getElement(element); + this.start(options || {}); + }, + + /** @id MochiKit.Visual.Morph.prototype.setup */ + setup: function () { + var b = MochiKit.Base; + var style = this.options.style; + this.styleStart = {}; + this.styleEnd = {}; + this.units = {}; + var value, unit; + for (var s in style) { + value = style[s]; + s = b.camelize(s); + if (MochiKit.Visual.CSS_LENGTH.test(value)) { + var components = value.match(/^([\+\-]?[0-9\.]+)(.*)$/); + value = parseFloat(components[1]); + unit = (components.length == 3) ? components[2] : null; + this.styleEnd[s] = value; + this.units[s] = unit; + value = MochiKit.Style.getStyle(this.element, s); + components = value.match(/^([\+\-]?[0-9\.]+)(.*)$/); + value = parseFloat(components[1]); + this.styleStart[s] = value; + } else { + var c = MochiKit.Color.Color; + value = c.fromString(value); + if (value) { + this.units[s] = "color"; + this.styleEnd[s] = value.toHexString(); + value = MochiKit.Style.getStyle(this.element, s); + this.styleStart[s] = c.fromString(value).toHexString(); + + this.styleStart[s] = b.map(b.bind(function (i) { + return parseInt( + this.styleStart[s].slice(i*2 + 1, i*2 + 3), 16); + }, this), [0, 1, 2]); + this.styleEnd[s] = b.map(b.bind(function (i) { + return parseInt( + this.styleEnd[s].slice(i*2 + 1, i*2 + 3), 16); + }, this), [0, 1, 2]); + } + } + } + }, + + /** @id MochiKit.Visual.Morph.prototype.update */ + update: function (position) { + var value; + for (var s in this.styleStart) { + if (this.units[s] == "color") { + var m = '#'; + var start = this.styleStart[s]; + var end = this.styleEnd[s]; + MochiKit.Base.map(MochiKit.Base.bind(function (i) { + m += MochiKit.Color.toColorPart(Math.round(start[i] + + (end[i] - start[i])*position)); + }, this), [0, 1, 2]); + this.element.style[s] = m; + } else { + value = this.styleStart[s] + Math.round((this.styleEnd[s] - this.styleStart[s]) * position * 1000) / 1000 + this.units[s]; + this.element.style[s] = value; + } + } + } +}); + +/*** + +Combination effects. + +***/ + +/** @id MochiKit.Visual.fade */ +MochiKit.Visual.fade = function (element, /* optional */ options) { + /*** + + Fade a given element: change its opacity and hide it in the end. + + @param options: 'to' and 'from' to change opacity. + + ***/ + var s = MochiKit.Style; + var oldOpacity = s.getStyle(element, 'opacity'); + options = MochiKit.Base.update({ + from: s.getStyle(element, 'opacity') || 1.0, + to: 0.0, + afterFinishInternal: function (effect) { + if (effect.options.to !== 0) { + return; + } + s.hideElement(effect.element); + s.setStyle(effect.element, {'opacity': oldOpacity}); + } + }, options || {}); + return new MochiKit.Visual.Opacity(element, options); +}; + +/** @id MochiKit.Visual.appear */ +MochiKit.Visual.appear = function (element, /* optional */ options) { + /*** + + Make an element appear. + + @param options: 'to' and 'from' to change opacity. + + ***/ + var s = MochiKit.Style; + var v = MochiKit.Visual; + options = MochiKit.Base.update({ + from: (s.getStyle(element, 'display') == 'none' ? 0.0 : + s.getStyle(element, 'opacity') || 0.0), + to: 1.0, + // force Safari to render floated elements properly + afterFinishInternal: function (effect) { + v.forceRerendering(effect.element); + }, + beforeSetupInternal: function (effect) { + s.setStyle(effect.element, {'opacity': effect.options.from}); + s.showElement(effect.element); + } + }, options || {}); + return new v.Opacity(element, options); +}; + +/** @id MochiKit.Visual.puff */ +MochiKit.Visual.puff = function (element, /* optional */ options) { + /*** + + 'Puff' an element: grow it to double size, fading it and make it hidden. + + ***/ + var s = MochiKit.Style; + var v = MochiKit.Visual; + element = MochiKit.DOM.getElement(element); + var oldStyle = { + position: s.getStyle(element, 'position'), + top: element.style.top, + left: element.style.left, + width: element.style.width, + height: element.style.height, + opacity: s.getStyle(element, 'opacity') + }; + options = MochiKit.Base.update({ + beforeSetupInternal: function (effect) { + MochiKit.Position.absolutize(effect.effects[0].element); + }, + afterFinishInternal: function (effect) { + s.hideElement(effect.effects[0].element); + s.setStyle(effect.effects[0].element, oldStyle); + }, + scaleContent: true, + scaleFromCenter: true + }, options || {}); + return new v.Parallel( + [new v.Scale(element, 200, + {sync: true, scaleFromCenter: options.scaleFromCenter, + scaleContent: options.scaleContent, restoreAfterFinish: true}), + new v.Opacity(element, {sync: true, to: 0.0 })], + options); +}; + +/** @id MochiKit.Visual.blindUp */ +MochiKit.Visual.blindUp = function (element, /* optional */ options) { + /*** + + Blind an element up: change its vertical size to 0. + + ***/ + var d = MochiKit.DOM; + element = d.getElement(element); + var elemClip = d.makeClipping(element); + options = MochiKit.Base.update({ + scaleContent: false, + scaleX: false, + restoreAfterFinish: true, + afterFinishInternal: function (effect) { + MochiKit.Style.hideElement(effect.element); + d.undoClipping(effect.element, elemClip); + } + }, options || {}); + + return new MochiKit.Visual.Scale(element, 0, options); +}; + +/** @id MochiKit.Visual.blindDown */ +MochiKit.Visual.blindDown = function (element, /* optional */ options) { + /*** + + Blind an element down: restore its vertical size. + + ***/ + var d = MochiKit.DOM; + var s = MochiKit.Style; + element = d.getElement(element); + var elementDimensions = s.getElementDimensions(element); + var elemClip; + options = MochiKit.Base.update({ + scaleContent: false, + scaleX: false, + scaleFrom: 0, + scaleMode: {originalHeight: elementDimensions.h, + originalWidth: elementDimensions.w}, + restoreAfterFinish: true, + afterSetupInternal: function (effect) { + elemClip = d.makeClipping(effect.element); + s.setStyle(effect.element, {height: '0px'}); + s.showElement(effect.element); + }, + afterFinishInternal: function (effect) { + d.undoClipping(effect.element, elemClip); + } + }, options || {}); + return new MochiKit.Visual.Scale(element, 100, options); +}; + +/** @id MochiKit.Visual.switchOff */ +MochiKit.Visual.switchOff = function (element, /* optional */ options) { + /*** + + Apply a switch-off-like effect. + + ***/ + var d = MochiKit.DOM; + element = d.getElement(element); + var oldOpacity = MochiKit.Style.getStyle(element, 'opacity'); + var elemClip; + options = MochiKit.Base.update({ + duration: 0.3, + scaleFromCenter: true, + scaleX: false, + scaleContent: false, + restoreAfterFinish: true, + beforeSetupInternal: function (effect) { + d.makePositioned(effect.element); + elemClip = d.makeClipping(effect.element); + }, + afterFinishInternal: function (effect) { + MochiKit.Style.hideElement(effect.element); + d.undoClipping(effect.element, elemClip); + d.undoPositioned(effect.element); + MochiKit.Style.setStyle(effect.element, {'opacity': oldOpacity}); + } + }, options || {}); + var v = MochiKit.Visual; + return new v.appear(element, { + duration: 0.4, + from: 0, + transition: v.Transitions.flicker, + afterFinishInternal: function (effect) { + new v.Scale(effect.element, 1, options); + } + }); +}; + +/** @id MochiKit.Visual.dropOut */ +MochiKit.Visual.dropOut = function (element, /* optional */ options) { + /*** + + Make an element fall and disappear. + + ***/ + var d = MochiKit.DOM; + var s = MochiKit.Style; + element = d.getElement(element); + var oldStyle = { + top: s.getStyle(element, 'top'), + left: s.getStyle(element, 'left'), + opacity: s.getStyle(element, 'opacity') + }; + + options = MochiKit.Base.update({ + duration: 0.5, + distance: 100, + beforeSetupInternal: function (effect) { + d.makePositioned(effect.effects[0].element); + }, + afterFinishInternal: function (effect) { + s.hideElement(effect.effects[0].element); + d.undoPositioned(effect.effects[0].element); + s.setStyle(effect.effects[0].element, oldStyle); + } + }, options || {}); + var v = MochiKit.Visual; + return new v.Parallel( + [new v.Move(element, {x: 0, y: options.distance, sync: true}), + new v.Opacity(element, {sync: true, to: 0.0})], + options); +}; + +/** @id MochiKit.Visual.shake */ +MochiKit.Visual.shake = function (element, /* optional */ options) { + /*** + + Move an element from left to right several times. + + ***/ + var d = MochiKit.DOM; + var v = MochiKit.Visual; + var s = MochiKit.Style; + element = d.getElement(element); + options = MochiKit.Base.update({ + x: -20, + y: 0, + duration: 0.05, + afterFinishInternal: function (effect) { + d.undoPositioned(effect.element); + s.setStyle(effect.element, oldStyle); + } + }, options || {}); + var oldStyle = { + top: s.getStyle(element, 'top'), + left: s.getStyle(element, 'left') }; + return new v.Move(element, + {x: 20, y: 0, duration: 0.05, afterFinishInternal: function (effect) { + new v.Move(effect.element, + {x: -40, y: 0, duration: 0.1, afterFinishInternal: function (effect) { + new v.Move(effect.element, + {x: 40, y: 0, duration: 0.1, afterFinishInternal: function (effect) { + new v.Move(effect.element, + {x: -40, y: 0, duration: 0.1, afterFinishInternal: function (effect) { + new v.Move(effect.element, + {x: 40, y: 0, duration: 0.1, afterFinishInternal: function (effect) { + new v.Move(effect.element, options + ) }}) }}) }}) }}) }}); +}; + +/** @id MochiKit.Visual.slideDown */ +MochiKit.Visual.slideDown = function (element, /* optional */ options) { + /*** + + Slide an element down. + It needs to have the content of the element wrapped in a container + element with fixed height. + + ***/ + var d = MochiKit.DOM; + var b = MochiKit.Base; + var s = MochiKit.Style; + element = d.getElement(element); + if (!element.firstChild) { + throw "MochiKit.Visual.slideDown must be used on a element with a child"; + } + d.removeEmptyTextNodes(element); + var oldInnerBottom = s.getStyle(element.firstChild, 'bottom') || 0; + var elementDimensions = s.getElementDimensions(element); + var elemClip; + options = b.update({ + scaleContent: false, + scaleX: false, + scaleFrom: 0, + scaleMode: {originalHeight: elementDimensions.h, + originalWidth: elementDimensions.w}, + restoreAfterFinish: true, + afterSetupInternal: function (effect) { + d.makePositioned(effect.element); + d.makePositioned(effect.element.firstChild); + if (/Opera/.test(navigator.userAgent)) { + s.setStyle(effect.element, {top: ''}); + } + elemClip = d.makeClipping(effect.element); + s.setStyle(effect.element, {height: '0px'}); + s.showElement(effect.element); + }, + afterUpdateInternal: function (effect) { + s.setStyle(effect.element.firstChild, + {bottom: (effect.dims[0] - effect.element.clientHeight) + 'px'}); + }, + afterFinishInternal: function (effect) { + d.undoClipping(effect.element, elemClip); + // IE will crash if child is undoPositioned first + if (/MSIE/.test(navigator.userAgent)) { + d.undoPositioned(effect.element); + d.undoPositioned(effect.element.firstChild); + } else { + d.undoPositioned(effect.element.firstChild); + d.undoPositioned(effect.element); + } + s.setStyle(effect.element.firstChild, + {bottom: oldInnerBottom}); + } + }, options || {}); + + return new MochiKit.Visual.Scale(element, 100, options); +}; + +/** @id MochiKit.Visual.slideUp */ +MochiKit.Visual.slideUp = function (element, /* optional */ options) { + /*** + + Slide an element up. + It needs to have the content of the element wrapped in a container + element with fixed height. + + ***/ + var d = MochiKit.DOM; + var b = MochiKit.Base; + var s = MochiKit.Style; + element = d.getElement(element); + if (!element.firstChild) { + throw "MochiKit.Visual.slideUp must be used on a element with a child"; + } + d.removeEmptyTextNodes(element); + var oldInnerBottom = s.getStyle(element.firstChild, 'bottom'); + var elemClip; + options = b.update({ + scaleContent: false, + scaleX: false, + scaleMode: 'box', + scaleFrom: 100, + restoreAfterFinish: true, + beforeStartInternal: function (effect) { + d.makePositioned(effect.element); + d.makePositioned(effect.element.firstChild); + if (/Opera/.test(navigator.userAgent)) { + s.setStyle(effect.element, {top: ''}); + } + elemClip = d.makeClipping(effect.element); + s.showElement(effect.element); + }, + afterUpdateInternal: function (effect) { + s.setStyle(effect.element.firstChild, + {bottom: (effect.dims[0] - effect.element.clientHeight) + 'px'}); + }, + afterFinishInternal: function (effect) { + s.hideElement(effect.element); + d.undoClipping(effect.element, elemClip); + d.undoPositioned(effect.element.firstChild); + d.undoPositioned(effect.element); + s.setStyle(effect.element.firstChild, {bottom: oldInnerBottom}); + } + }, options || {}); + return new MochiKit.Visual.Scale(element, 0, options); +}; + +// Bug in opera makes the TD containing this element expand for a instance +// after finish +/** @id MochiKit.Visual.squish */ +MochiKit.Visual.squish = function (element, /* optional */ options) { + /*** + + Reduce an element and make it disappear. + + ***/ + var d = MochiKit.DOM; + var b = MochiKit.Base; + var elemClip; + options = b.update({ + restoreAfterFinish: true, + beforeSetupInternal: function (effect) { + elemClip = d.makeClipping(effect.element); + }, + afterFinishInternal: function (effect) { + MochiKit.Style.hideElement(effect.element); + d.undoClipping(effect.element, elemClip); + } + }, options || {}); + + return new MochiKit.Visual.Scale(element, /Opera/.test(navigator.userAgent) ? 1 : 0, options); +}; + +/** @id MochiKit.Visual.grow */ +MochiKit.Visual.grow = function (element, /* optional */ options) { + /*** + + Grow an element to its original size. Make it zero-sized before + if necessary. + + ***/ + var d = MochiKit.DOM; + var v = MochiKit.Visual; + var s = MochiKit.Style; + element = d.getElement(element); + options = MochiKit.Base.update({ + direction: 'center', + moveTransition: v.Transitions.sinoidal, + scaleTransition: v.Transitions.sinoidal, + opacityTransition: v.Transitions.full, + scaleContent: true, + scaleFromCenter: false + }, options || {}); + var oldStyle = { + top: element.style.top, + left: element.style.left, + height: element.style.height, + width: element.style.width, + opacity: s.getStyle(element, 'opacity') + }; + + var dims = s.getElementDimensions(element); + var initialMoveX, initialMoveY; + var moveX, moveY; + + switch (options.direction) { + case 'top-left': + initialMoveX = initialMoveY = moveX = moveY = 0; + break; + case 'top-right': + initialMoveX = dims.w; + initialMoveY = moveY = 0; + moveX = -dims.w; + break; + case 'bottom-left': + initialMoveX = moveX = 0; + initialMoveY = dims.h; + moveY = -dims.h; + break; + case 'bottom-right': + initialMoveX = dims.w; + initialMoveY = dims.h; + moveX = -dims.w; + moveY = -dims.h; + break; + case 'center': + initialMoveX = dims.w / 2; + initialMoveY = dims.h / 2; + moveX = -dims.w / 2; + moveY = -dims.h / 2; + break; + } + + var optionsParallel = MochiKit.Base.update({ + beforeSetupInternal: function (effect) { + s.setStyle(effect.effects[0].element, {height: '0px'}); + s.showElement(effect.effects[0].element); + }, + afterFinishInternal: function (effect) { + d.undoClipping(effect.effects[0].element); + d.undoPositioned(effect.effects[0].element); + s.setStyle(effect.effects[0].element, oldStyle); + } + }, options || {}); + + return new v.Move(element, { + x: initialMoveX, + y: initialMoveY, + duration: 0.01, + beforeSetupInternal: function (effect) { + s.hideElement(effect.element); + d.makeClipping(effect.element); + d.makePositioned(effect.element); + }, + afterFinishInternal: function (effect) { + new v.Parallel( + [new v.Opacity(effect.element, { + sync: true, to: 1.0, from: 0.0, + transition: options.opacityTransition + }), + new v.Move(effect.element, { + x: moveX, y: moveY, sync: true, + transition: options.moveTransition + }), + new v.Scale(effect.element, 100, { + scaleMode: {originalHeight: dims.h, + originalWidth: dims.w}, + sync: true, + scaleFrom: /Opera/.test(navigator.userAgent) ? 1 : 0, + transition: options.scaleTransition, + scaleContent: options.scaleContent, + scaleFromCenter: options.scaleFromCenter, + restoreAfterFinish: true + }) + ], optionsParallel + ); + } + }); +}; + +/** @id MochiKit.Visual.shrink */ +MochiKit.Visual.shrink = function (element, /* optional */ options) { + /*** + + Shrink an element and make it disappear. + + ***/ + var d = MochiKit.DOM; + var v = MochiKit.Visual; + var s = MochiKit.Style; + element = d.getElement(element); + options = MochiKit.Base.update({ + direction: 'center', + moveTransition: v.Transitions.sinoidal, + scaleTransition: v.Transitions.sinoidal, + opacityTransition: v.Transitions.none, + scaleContent: true, + scaleFromCenter: false + }, options || {}); + var oldStyle = { + top: element.style.top, + left: element.style.left, + height: element.style.height, + width: element.style.width, + opacity: s.getStyle(element, 'opacity') + }; + + var dims = s.getElementDimensions(element); + var moveX, moveY; + + switch (options.direction) { + case 'top-left': + moveX = moveY = 0; + break; + case 'top-right': + moveX = dims.w; + moveY = 0; + break; + case 'bottom-left': + moveX = 0; + moveY = dims.h; + break; + case 'bottom-right': + moveX = dims.w; + moveY = dims.h; + break; + case 'center': + moveX = dims.w / 2; + moveY = dims.h / 2; + break; + } + var elemClip; + + var optionsParallel = MochiKit.Base.update({ + beforeStartInternal: function (effect) { + elemClip = d.makePositioned(effect.effects[0].element); + d.makeClipping(effect.effects[0].element); + }, + afterFinishInternal: function (effect) { + s.hideElement(effect.effects[0].element); + d.undoClipping(effect.effects[0].element, elemClip); + d.undoPositioned(effect.effects[0].element); + s.setStyle(effect.effects[0].element, oldStyle); + } + }, options || {}); + + return new v.Parallel( + [new v.Opacity(element, { + sync: true, to: 0.0, from: 1.0, + transition: options.opacityTransition + }), + new v.Scale(element, /Opera/.test(navigator.userAgent) ? 1 : 0, { + sync: true, transition: options.scaleTransition, + scaleContent: options.scaleContent, + scaleFromCenter: options.scaleFromCenter, + restoreAfterFinish: true + }), + new v.Move(element, { + x: moveX, y: moveY, sync: true, transition: options.moveTransition + }) + ], optionsParallel + ); +}; + +/** @id MochiKit.Visual.pulsate */ +MochiKit.Visual.pulsate = function (element, /* optional */ options) { + /*** + + Pulse an element between appear/fade. + + ***/ + var d = MochiKit.DOM; + var v = MochiKit.Visual; + var b = MochiKit.Base; + var oldOpacity = MochiKit.Style.getStyle(element, 'opacity'); + options = b.update({ + duration: 3.0, + from: 0, + afterFinishInternal: function (effect) { + MochiKit.Style.setStyle(effect.element, {'opacity': oldOpacity}); + } + }, options || {}); + var transition = options.transition || v.Transitions.sinoidal; + var reverser = b.bind(function (pos) { + return transition(1 - v.Transitions.pulse(pos, options.pulses)); + }, transition); + b.bind(reverser, transition); + return new v.Opacity(element, b.update({ + transition: reverser}, options)); +}; + +/** @id MochiKit.Visual.fold */ +MochiKit.Visual.fold = function (element, /* optional */ options) { + /*** + + Fold an element, first vertically, then horizontally. + + ***/ + var d = MochiKit.DOM; + var v = MochiKit.Visual; + var s = MochiKit.Style; + element = d.getElement(element); + var oldStyle = { + top: element.style.top, + left: element.style.left, + width: element.style.width, + height: element.style.height + }; + var elemClip = d.makeClipping(element); + options = MochiKit.Base.update({ + scaleContent: false, + scaleX: false, + afterFinishInternal: function (effect) { + new v.Scale(element, 1, { + scaleContent: false, + scaleY: false, + afterFinishInternal: function (effect) { + s.hideElement(effect.element); + d.undoClipping(effect.element, elemClip); + s.setStyle(effect.element, oldStyle); + } + }); + } + }, options || {}); + return new v.Scale(element, 5, options); +}; + + +// Compatibility with MochiKit 1.0 +MochiKit.Visual.Color = MochiKit.Color.Color; +MochiKit.Visual.getElementsComputedStyle = MochiKit.DOM.computedStyle; + +/* end of Rico adaptation */ + +MochiKit.Visual.__new__ = function () { + var m = MochiKit.Base; + + m.nameFunctions(this); + + this.EXPORT_TAGS = { + ":common": this.EXPORT, + ":all": m.concat(this.EXPORT, this.EXPORT_OK) + }; + +}; + +MochiKit.Visual.EXPORT = [ + "roundElement", + "roundClass", + "tagifyText", + "multiple", + "toggle", + "Parallel", + "Opacity", + "Move", + "Scale", + "Highlight", + "ScrollTo", + "Morph", + "fade", + "appear", + "puff", + "blindUp", + "blindDown", + "switchOff", + "dropOut", + "shake", + "slideDown", + "slideUp", + "squish", + "grow", + "shrink", + "pulsate", + "fold" +]; + +MochiKit.Visual.EXPORT_OK = [ + "Base", + "PAIRS" +]; + +MochiKit.Visual.__new__(); + +MochiKit.Base._exportSymbols(this, MochiKit.Visual); diff --git a/dom/tests/mochitest/ajax/mochikit/MochiKit/__package__.js b/dom/tests/mochitest/ajax/mochikit/MochiKit/__package__.js new file mode 100644 index 0000000000..8d644b19e9 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/__package__.js @@ -0,0 +1,18 @@ +dojo.kwCompoundRequire({ + "common": [ + "MochiKit.Base", + "MochiKit.Iter", + "MochiKit.Logging", + "MochiKit.DateTime", + "MochiKit.Format", + "MochiKit.Async", + "MochiKit.DOM", + "MochiKit.Style", + "MochiKit.LoggingPane", + "MochiKit.Color", + "MochiKit.Signal", + "MochiKit.Position", + "MochiKit.Visual" + ] +}); +dojo.provide("MochiKit.*"); diff --git a/dom/tests/mochitest/ajax/mochikit/manifest.json b/dom/tests/mochitest/ajax/mochikit/manifest.json new file mode 100644 index 0000000000..5bf0e943de --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/manifest.json @@ -0,0 +1 @@ +{testcases:['tests/MochiKit-Async.html','tests/MochiKit-Base.html','tests/MochiKit-DateTime.html','tests/MochiKit-DOM.html','tests/MochiKit-Style.html','tests/MochiKit-Format.html','tests/MochiKit-Iter.html','tests/MochiKit-Logging.html','tests/MochiKit-MochiKit.html','tests/MochiKit-Color.html','tests/MochiKit-Selector.html','tests/MochiKit-Signal.html','tests/MochiKit-Visual.html']} diff --git a/dom/tests/mochitest/ajax/mochikit/mochitest.ini b/dom/tests/mochitest/ajax/mochikit/mochitest.ini new file mode 100644 index 0000000000..25f301e302 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/mochitest.ini @@ -0,0 +1,26 @@ +[DEFAULT] +support-files = + manifest.json + MochiKit/Async.js + MochiKit/Base.js + MochiKit/Color.js + MochiKit/Controls.js + MochiKit/DOM.js + MochiKit/DateTime.js + MochiKit/DragAndDrop.js + MochiKit/Format.js + MochiKit/Iter.js + MochiKit/Logging.js + MochiKit/LoggingPane.js + MochiKit/MochiKit.js + MochiKit/MockDOM.js + MochiKit/Position.js + MochiKit/Selector.js + MochiKit/Signal.js + MochiKit/Sortable.js + MochiKit/Style.js + MochiKit/Test.js + MochiKit/Visual.js + MochiKit/__package__.js + +[test_Mochikit.html] diff --git a/dom/tests/mochitest/ajax/mochikit/test_Mochikit.html b/dom/tests/mochitest/ajax/mochikit/test_Mochikit.html new file mode 100644 index 0000000000..a94ce14a4f --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/test_Mochikit.html @@ -0,0 +1,15 @@ +<!DOCTYPE HTML> +<html> +<head> + <title>Test for Mochikit</title> + <script src="/MochiKit/Base.js"></script> + <script src="/MochiKit/Async.js"></script> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="../lib/AJAX_setup.js"></script> + <link rel="stylesheet" type="text/css" href="../lib/test.css" /> +</head> +<body> + <iframe width="100%" height="500" id="testframe" src=""></iframe> +</body> +</html> + diff --git a/dom/tests/mochitest/ajax/mochikit/tests/FakeJSAN.js b/dom/tests/mochitest/ajax/mochikit/tests/FakeJSAN.js new file mode 100644 index 0000000000..639519ecf0 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/FakeJSAN.js @@ -0,0 +1,40 @@ +var JSAN = { + global: this, + use: function (module, symbols) { + var components = module.split(/\./); + var fn = components.join('/') + '.js'; + var o = JSAN.global; + var i, c; + for (i = 0; i < components.length; i++) { + o = o[components[i]]; + if (typeof(o) == 'undefined') { + break; + } + } + if (typeof(o) != 'undefined') { + return o; + } + + load(fn); + o = JSAN.global; + for (i = 0; i < components.length; i++) { + o = o[components[i]]; + if (typeof(o) == 'undefined') { + return undefined; + } + } + if (!symbols) { + var tags = o.EXPORT_TAGS; + if (tags) { + symbols = tags[':common'] || tags[':all']; + } + } + if (symbols) { + for (i = 0; i < symbols.length; i++) { + c = symbols[i]; + JSAN.global[c] = o[c]; + } + } + return o; + } +}; diff --git a/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Async.html b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Async.html new file mode 100644 index 0000000000..32889ea821 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Async.html @@ -0,0 +1,408 @@ +<html> +<head> + <script type="text/javascript" src="../MochiKit/Base.js"></script> + <script type="text/javascript" src="../MochiKit/Async.js"></script> + <script type="text/javascript" src="../MochiKit/Iter.js"></script> + <script type="text/javascript" src="../MochiKit/DOM.js"></script> + <script type="text/javascript" src="../MochiKit/Style.js"></script> + <script type="text/javascript" src="SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="SimpleTest/test.css"> +</head> +<body> + +<pre id="test"> +<script type="text/javascript"> +try { + + var increment = function (res) { + return res + 1; + } + + var throwStuff = function (res) { + throw new GenericError(res); + } + + var catchStuff = function (res) { + return res.message; + } + + var returnError = function (res) { + return new GenericError(res); + } + + var anythingOkCallback = function (msg) { + return function (res) { + ok(true, msg); + return res; + } + } + + var testEqCallback = function () { + /* + sort of emulate how deferreds work in Twisted + for "convenient" testing + */ + var args = []; + for (var i = 0; i < arguments.length; i++) { + args.push(arguments[i]); + } + return function (res) { + var nargs = args.slice(); + nargs.unshift(res); + is.apply(this, nargs); + return res; + } + } + + var neverHappen = function (d) { + ok(false, "this should never happen"); + } + + /* + Test normal Deferred operation + */ + var d = new Deferred(); + d.addCallback(testEqCallback(1, "pre-deferred callback")); + d.callback(1); + d.addCallback(increment); + d.addCallback(testEqCallback(2, "post-deferred callback")); + d.addCallback(throwStuff); + d.addCallback(neverHappen); + d.addErrback(catchStuff); + d.addCallback(testEqCallback(2, "throw -> err, catch -> success")); + d.addCallback(returnError); + d.addCallback(neverHappen); + d.addErrback(catchStuff); + d.addCallback(testEqCallback(2, "return -> err, catch -> succcess")); + + /* + Test Deferred cancellation + */ + var cancelled = function (d) { + ok(true, "canceller called!"); + } + + var cancelledError = function (res) { + ok(res instanceof CancelledError, "CancelledError here"); + } + + d = new Deferred(cancelled); + d.addCallback(neverHappen); + d.addErrback(cancelledError); + d.cancel(); + + /* + Test succeed / fail + */ + + d = succeed(1).addCallback(testEqCallback(1, "succeed")); + + // default error + d = fail().addCallback(neverHappen); + d = d.addErrback(anythingOkCallback("default fail")); + + // default wrapped error + d = fail("web taco").addCallback(neverHappen).addErrback(catchStuff); + d = d.addCallback(testEqCallback("web taco", "wrapped fail")); + + // default unwrapped error + d = fail(new GenericError("ugh")).addCallback(neverHappen).addErrback(catchStuff); + d = d.addCallback(testEqCallback("ugh", "unwrapped fail")); + + /* + Test deferred dependencies + */ + + var deferredIncrement = function (res) { + var rval = succeed(res); + rval.addCallback(increment); + return rval; + } + + d = succeed(1).addCallback(deferredIncrement); + d = d.addCallback(testEqCallback(2, "dependent deferred succeed")); + + var deferredFailure = function (res) { + return fail(res); + } + + d = succeed("ugh").addCallback(deferredFailure).addErrback(catchStuff); + d = d.addCallback(testEqCallback("ugh", "dependent deferred fail")); + + /* + Test double-calling, double-failing, etc. + */ + try { + succeed(1).callback(2); + neverHappen(); + } catch (e) { + ok(e instanceof AlreadyCalledError, "double-call"); + } + try { + fail(1).errback(2); + neverHappen(); + } catch (e) { + ok(e instanceof AlreadyCalledError, "double-fail"); + } + try { + d = succeed(1); + d.cancel(); + d = d.callback(2); + ok(true, "swallowed one callback, no canceller"); + d.callback(3); + neverHappen(); + } catch (e) { + ok(e instanceof AlreadyCalledError, "swallow cancel"); + } + try { + d = new Deferred(cancelled); + d.cancel(); + d = d.callback(1); + neverHappen(); + } catch (e) { + ok(e instanceof AlreadyCalledError, "non-swallowed cancel"); + } + + /* Test incorrect Deferred usage */ + + d = new Deferred(); + try { + d.callback(new Deferred()); + neverHappen(); + } catch (e) { + ok (e instanceof Error, "deferred not allowed for callback"); + } + d = new Deferred(); + try { + d.errback(new Deferred()); + neverHappen(); + } catch (e) { + ok (e instanceof Error, "deferred not allowed for errback"); + } + + d = new Deferred(); + (new Deferred()).addCallback(function () { return d; }).callback(1); + try { + d.addCallback(function () {}); + neverHappen(); + } catch (e) { + ok (e instanceof Error, "chained deferred not allowed to be re-used"); + } + + /* + evalJSONRequest test + */ + var fakeReq = {"responseText":'[1,2,3,4,"asdf",{"a":["b", "c"]}]'}; + var obj = [1,2,3,4,"asdf",{"a":["b", "c"]}]; + isDeeply(obj, evalJSONRequest(fakeReq), "evalJSONRequest"); + + try { + MochiKit.Async.getXMLHttpRequest(); + ok(true, "getXMLHttpRequest"); + } catch (e) { + ok(false, "no love from getXMLHttpRequest"); + } + + var lock = new DeferredLock(); + var lst = []; + var pushNumber = function (x) { + return function (res) { lst.push(x); } + }; + lock.acquire().addCallback(pushNumber(1)); + is( compare(lst, [1]), 0, "lock acquired" ); + lock.acquire().addCallback(pushNumber(2)); + is( compare(lst, [1]), 0, "lock waiting for release" ); + lock.acquire().addCallback(pushNumber(3)); + is( compare(lst, [1]), 0, "lock waiting for release" ); + lock.release(); + is( compare(lst, [1, 2]), 0, "lock passed on" ); + lock.release(); + is( compare(lst, [1, 2, 3]), 0, "lock passed on" ); + lock.release(); + try { + lock.release(); + ok( false, "over-release didn't raise" ); + } catch (e) { + ok( true, "over-release raised" ); + } + lock.acquire().addCallback(pushNumber(1)); + is( compare(lst, [1, 2, 3, 1]), 0, "lock acquired" ); + lock.release(); + is( compare(lst, [1, 2, 3, 1]), 0, "lock released" ); + + var d = new Deferred(); + lst = []; + d.addCallback(operator.add, 2); + d.addBoth(operator.add, 4); + d.addCallback(bind(lst.push, lst)); + d.callback(1); + is( lst[0], 7, "auto-partial addCallback addBoth" ); + d.addCallback(function () { throw new Error(); }); + ebTest = function(a, b) { + map(bind(lst.push, lst), arguments); + }; + d.addErrback(ebTest, "foo"); + is( lst[1], "foo", "auto-partial errback" ); + is( lst.length, 3, "auto-partial errback" ); + + /* + Test DeferredList + */ + + var callList = [new Deferred(), new Deferred(), new Deferred()]; + callList[0].addCallback(increment); + callList[1].addCallback(increment); + callList[2].addCallback(increment); + var defList = new DeferredList(callList); + ok(defList instanceof Deferred, "DeferredList looks like a Deferred"); + + callList[0].callback(3); + callList[1].callback(5); + callList[2].callback(4); + + defList.addCallback(function (lst) { + is( arrayEqual(lst, [[true, 4], [true, 6], [true, 5]]), 1, + "deferredlist result ok" ); + }); + + /* + Test fireOnOneCallback + */ + + var callList2 = [new Deferred(), new Deferred(), new Deferred()]; + callList2[0].addCallback(increment); + callList2[1].addCallback(increment); + callList2[2].addCallback(increment); + var defList2 = new DeferredList(callList2, true); + callList2[1].callback(5); + callList2[0].callback(3); + callList2[2].callback(4); + + defList2.addCallback(function (lst) { + is( arrayEqual(lst, [1, 6]), 1, "deferredlist fireOnOneCallback ok" ); + }); + + /* + Test fireOnOneErrback + */ + + var callList3 = [new Deferred(), new Deferred(), new Deferred()]; + callList3[0].addCallback(increment); + callList3[1].addCallback(throwStuff); + callList3[2].addCallback(increment); + var defList3 = new DeferredList(callList3, false, true); + defList3.callback = neverHappen; + callList3[0].callback(3); + callList3[1].callback("foo"); + callList3[2].callback(4); + + defList3.addErrback(function (err) { + is( err.message, "foo", "deferredlist fireOnOneErrback ok" ); + }); + + /* + Test consumeErrors + */ + + var callList4 = [new Deferred(), new Deferred(), new Deferred()]; + callList4[0].addCallback(increment); + callList4[1].addCallback(throwStuff); + callList4[2].addCallback(increment); + var defList4 = new DeferredList(callList4, false, false, true); + defList4.addErrback(neverHappen); + callList4[1].addCallback(function (arg) { + is(arg, null, "deferredlist consumeErrors ok" ); + }); + callList4[0].callback(3); + callList4[1].callback("foo"); + callList4[2].callback(4); + + /* + Test gatherResults + */ + + var callList5 = [new Deferred(), new Deferred(), new Deferred()]; + callList5[0].addCallback(increment); + callList5[1].addCallback(increment); + callList5[2].addCallback(increment); + var gatherRet = gatherResults(callList5); + callList5[0].callback(3); + callList5[1].callback(5); + callList5[2].callback(4); + + gatherRet.addCallback(function (lst) { + is( arrayEqual(lst, [4, 6, 5]), 1, + "gatherResults result ok" ); + }); + + /* + Test maybeDeferred + */ + + var maybeDef = maybeDeferred(increment, 4); + maybeDef.addCallback(testEqCallback(5, "maybeDeferred sync ok")); + + var maybeDef2 = deferredIncrement(8); + maybeDef2.addCallback(testEqCallback(9, "maybeDeferred async ok")); + + ok( true, "synchronous test suite finished!"); + + var t = (new Date().getTime()); + SimpleTest.waitForExplicitFinish(); + checkCallLater = function (originalTime) { + is(originalTime, t, "argument passed in OK"); + is(arguments.length, 1, "argument count right"); + }; + var lock = new DeferredLock(); + withLock = function (msg) { + var cb = partial.apply(null, extend(null, arguments, 1)); + var d = lock.acquire().addCallback(cb); + d.addErrback(ok, false, msg); + d.addCallback(function () { + ok(true, msg); + lock.release(); + }); + return d; + } + withLock("callLater", function () { + return callLater(0.05, checkCallLater, t); + }); + withLock("wait", function () { + return wait(0.05, t).addCallback(checkCallLater); + }); + withLock("loadJSONDoc", function () { + var d = loadJSONDoc("test_MochiKit-Async.json"); + d.addCallback(function (doc) { + is(doc.passed, true, "loadJSONDoc passed"); + }); + d.addErrback(function (doc) { + ok(false, "loadJSONDoc failed"); + }); + return d; + }); + lock.acquire().addCallback(function () { + ok(true, "async suite finished"); + SimpleTest.finish(); + }); + + +} catch (err) { + + var s = "test suite failure!\n"; + var o = {}; + var k = null; + for (k in err) { + // ensure unique keys?! + if (!o[k]) { + s += k + ": " + err[k] + "\n"; + o[k] = err[k]; + } + } + ok ( false, s ); + SimpleTest.finish(); + +} +</script> +</pre> +</body> +</html> diff --git a/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Base.html b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Base.html new file mode 100644 index 0000000000..3db71e1e75 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Base.html @@ -0,0 +1,34 @@ +<html> +<head> + <script type="text/javascript" src="../MochiKit/Base.js"></script> + <script type="text/javascript" src="../MochiKit/Iter.js"></script> + <script type="text/javascript" src="../MochiKit/DOM.js"></script> + <script type="text/javascript" src="../MochiKit/Style.js"></script> + <script type="text/javascript" src="SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="SimpleTest/test.css"> +</head> +<body> + +<pre id="test"> +<script type="text/javascript" src="test_Base.js"></script> +<script type="text/javascript"> +try { + tests.test_Base({ok:ok,is:is}); + ok( true, "test suite finished!"); +} catch (err) { + var s = "test suite failure!\n"; + var o = {}; + var k = null; + for (k in err) { + // ensure unique keys?! + if (!o[k]) { + s += k + ": " + err[k] + "\n"; + o[k] = err[k]; + } + } + ok ( false, s ); +} +</script> +</pre> +</body> +</html> diff --git a/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Color.html b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Color.html new file mode 100644 index 0000000000..bad32dfb18 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Color.html @@ -0,0 +1,84 @@ +<html> +<head> + <script type="text/javascript" src="../MochiKit/Base.js"></script> + <script type="text/javascript" src="../MochiKit/Iter.js"></script> + <script type="text/javascript" src="../MochiKit/DOM.js"></script> + <script type="text/javascript" src="../MochiKit/Style.js"></script> + <script type="text/javascript" src="../MochiKit/Logging.js"></script> + <script type="text/javascript" src="../MochiKit/Color.js"></script> + <script type="text/javascript" src="SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="SimpleTest/test.css"> + <style type="text/css">.redtext {color: red}</style> +</head> +<body> +<div style="position:absolute; top: 0px; left:0px; width:0px; height:0px"> + <span style="color: red" id="c_direct"></span> + <span class="redtext" id="c_indirect"></span> +</div> +<pre id="test"> +<script type="text/javascript" src="test_Color.js"></script> +<script type="text/javascript"> +try { + + var t = {ok:ok, is:is}; + tests.test_Color({ok:ok, is:is}); + is( + Color.fromText(SPAN()).toHexString(), + "#000000", + "fromText no style" + ); + + is( + Color.fromText("c_direct").toHexString(), + Color.fromName("red").toHexString(), + "fromText direct style" + ); + + is( + Color.fromText("c_indirect").toHexString(), + Color.fromName("red").toHexString(), + "fromText indirect style" + ); + + is( + Color.fromComputedStyle("c_direct", "color").toHexString(), + Color.fromName("red").toHexString(), + "fromComputedStyle direct style" + ); + + is( + Color.fromComputedStyle("c_indirect", "color").toHexString(), + Color.fromName("red").toHexString(), + "fromComputedStyle indirect style" + ); + + is( + Color.fromBackground((SPAN(null, 'test'))).toHexString(), + Color.fromName("white").toHexString(), + "fromBackground with DOM" + ); + + + // Done! + + ok( true, "test suite finished!"); + +} catch (err) { + + var s = "test suite failure!\n"; + var o = {}; + var k = null; + for (k in err) { + // ensure unique keys?! + if (!o[k]) { + s += k + ": " + err[k] + "\n"; + o[k] = err[k]; + } + } + ok ( false, s ); + +} +</script> +</pre> +</body> +</html> diff --git a/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-DOM.html b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-DOM.html new file mode 100644 index 0000000000..45036d9c74 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-DOM.html @@ -0,0 +1,316 @@ +<html> +<head> + <script type="text/javascript" src="../MochiKit/MockDOM.js"></script> + <script type="text/javascript" src="../MochiKit/Base.js"></script> + <script type="text/javascript" src="../MochiKit/Iter.js"></script> + <script type="text/javascript" src="../MochiKit/DOM.js"></script> + <script type="text/javascript" src="../MochiKit/Style.js"></script> + <script type="text/javascript" src="SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="SimpleTest/test.css"> +</head> +<body> + +<div style="display: none;"> + <form id="form_test"> + <select name="select"> + <option value="foo" selected="selected">foo</option> + <option value="bar">bar</option> + <option value="baz">baz</option> + </select> + <select name="selmultiple" multiple="multiple"> + <option value="bar" selected="selected">bar</option> + <option value="baz" selected="selected">baz</option> + <option value="foo">foo</option> + </select> + <input type="hidden" name="hidden" value="test" /> + <input type="radio" name="radio_off" value="1" /> + <input type="radio" name="radio_off" value="2" /> + <input type="radio" name="radio_off" value="3" /> + <input type="radio" name="radio_on" value="1" /> + <input type="radio" name="radio_on" value="2" checked="checked" /> + <input type="radio" name="radio_on" value="3" /> + </form> + <form id="form_test2"> + <select name="selempty"> + <option value="" selected="selected">foo</option> + </select> + <select name="selempty2"> + <option selected="selected">foo</option> + </select> + </form> + <div id="parentTwo" class="two"> + <div id="parentOne" class="one"> + <div id="parentZero" class="zero"> + <span id="child">child</span> + </div> + </div> + </div> +</div> + +<pre id="test"> +<script type="text/javascript"> +try { + + lst = []; + o = {"blah": function () { lst.push("original"); }}; + addToCallStack(o, "blah", function () { lst.push("new"); }, true); + addToCallStack(o, "blah", function () { lst.push("stuff"); }, true); + is( typeof(o.blah), 'function', 'addToCallStack has a function' ); + is( o.blah.callStack.length, 3, 'callStack length 3' ); + o.blah(); + is( lst.join(" "), "original new stuff", "callStack in correct order" ); + is( o.blah, null, "set to null" ); + lst = []; + o = {"blah": function () { lst.push("original"); }}; + addToCallStack(o, "blah", + function () { lst.push("new"); return false;}, false); + addToCallStack(o, "blah", function () { lst.push("stuff"); }, false); + o.blah(); + is( lst.join(" "), "original new", "callStack in correct order (abort)" ); + o.blah(); + is( lst.join(" "), "original new original new", "callStack in correct order (again)" ); + + + is( escapeHTML("<>\"&bar"), "<>"&bar", "escapeHTML" ); // for emacs highlighting: " + + var isDOM = function (value, expected, message) { + is( escapeHTML(toHTML(value)), escapeHTML(expected), message ); + }; + + var d = document.createElement('span'); + updateNodeAttributes(d, {"foo": "bar", "baz": "wibble"}); + isDOM( d, '<span baz="wibble" foo="bar"/>', "updateNodeAttributes" ); + + var d = document.createElement('span'); + appendChildNodes(d, 'word up', [document.createElement('span')]); + isDOM( d, '<span>word up<span/></span>', 'appendChildNodes' ); + + replaceChildNodes(d, 'Think Different'); + isDOM( d, '<span>Think Different</span>', 'replaceChildNodes' ); + + + insertSiblingNodesBefore(d.childNodes[0], 'word up', document.createElement('span')); + isDOM( d, '<span>word up<span/>Think Different</span>', 'insertSiblingNodesBefore' ); + + insertSiblingNodesAfter(d.childNodes[0], 'purple monkey', document.createElement('span')); + isDOM( d, '<span>word uppurple monkey<span/><span/>Think Different</span>', 'insertSiblingNodesAfter' ); + + d = createDOM("span"); + isDOM( d, "<span/>", "createDOM empty" ); + + + d = createDOM("span", {"foo": "bar", "baz": "wibble"}); + isDOM( d, '<span baz="wibble" foo="bar"/>', "createDOM attributes" ); + + d = createDOM("span", {"foo": "bar", "baz": "wibble", "spam": "egg"}, "one", "two", "three"); + is( getNodeAttribute(d, 'foo'), "bar", "createDOM attribute" ); + is( getNodeAttribute(d, 'baz'), "wibble", "createDOM attribute" ); + removeNodeAttribute(d, "spam"); + is( scrapeText(d), "onetwothree", "createDOM contents" ); + + isDOM( d, '<span baz="wibble" foo="bar">onetwothree</span>', "createDOM contents" ); + + d = createDOM("span", null, function (f) { + return this.nodeName.toLowerCase() + "hi" + f.nodeName.toLowerCase();}); + isDOM( d, '<span>spanhispan</span>', 'createDOM function call' ); + + d = createDOM("span", null, {msg: "hi", dom: function (f) { + return f.nodeName.toLowerCase() + this.msg; }}); + isDOM( d, '<span>spanhi</span>', 'createDOM this.dom() call' ); + + d = createDOM("span", null, {msg: "hi", __dom__: function (f) { + return f.nodeName.toLowerCase() + this.msg; }}); + isDOM( d, '<span>spanhi</span>', 'createDOM this.__dom__() call' ); + + d = createDOM("span", null, range(4)); + isDOM( d, '<span>0123</span>', 'createDOM iterable' ); + + + var d = {"taco": "pork"}; + registerDOMConverter("taco", + function (o) { return !isUndefinedOrNull(o.taco); }, + function (o) { return "Goddamn, I like " + o.taco + " tacos"; } + ); + d = createDOM("span", null, d); + // not yet public API + domConverters.unregister("taco"); + + isDOM( d, "<span>Goddamn, I like pork tacos</span>", "createDOM with custom converter" ); + + is( + escapeHTML(toHTML(SPAN(null))), + escapeHTML(toHTML(createDOM("span", null))), + "createDOMFunc vs createDOM" + ); + + is( scrapeText(d), "Goddamn, I like pork tacos", "scrape OK" ); + is( scrapeText(d, true).join(""), "Goddamn, I like pork tacos", "scrape Array OK" ); + + var st = DIV(null, STRONG(null, "d"), "oor ", STRONG(null, "f", SPAN(null, "r"), "a"), "me"); + is( scrapeText(st), "door frame", "scrape in-order" ); + + + ok( !isUndefinedOrNull(getElement("test")), "getElement might work" ); + ok( !isUndefinedOrNull($("test")), "$alias$$ CASH MONEY alias might work" ); + + d = createDOM("span", null, "one", "two"); + swapDOM(d.childNodes[0], document.createTextNode("uno")); + isDOM( d, "<span>unotwo</span>", "swapDOM" ); + + is( scrapeText(d, true).join(" "), "uno two", "multi-node scrapeText" ); + /* + + TODO: + addLoadEvent (async test?) + + */ + + d = createDOM("span", {"class": "foo"}); + setElementClass(d, "bar baz"); + ok( d.className == "bar baz", "setElementClass"); + toggleElementClass("bar", d); + ok( d.className == "baz", "toggleElementClass: " + d.className); + toggleElementClass("bar", d); + ok( hasElementClass(d, "baz", "bar"), + "toggleElementClass 2: " + d.className); + addElementClass(d, "bar"); + ok( hasElementClass(d, "baz", "bar"), + "toggleElementClass 3: " + d.className); + ok( addElementClass(d, "blah"), "addElementClass return"); + ok( hasElementClass(d, "baz", "bar", "blah"), "addElementClass action"); + ok( !hasElementClass(d, "not"), "hasElementClass single"); + ok( !hasElementClass(d, "baz", "not"), "hasElementClass multiple"); + ok( removeElementClass(d, "blah"), "removeElementClass" ); + ok( !removeElementClass(d, "blah"), "removeElementClass again" ); + ok( !hasElementClass(d, "blah"), "removeElementClass again (hasElement)" ); + removeElementClass(d, "baz"); + ok( !swapElementClass(d, "blah", "baz"), "false swapElementClass" ); + ok( !hasElementClass(d, "baz"), "false swapElementClass from" ); + ok( !hasElementClass(d, "blah"), "false swapElementClass to" ); + addElementClass(d, "blah"); + ok( swapElementClass(d, "blah", "baz"), "swapElementClass" ); + ok( hasElementClass(d, "baz"), "swapElementClass has toClass" ); + ok( !hasElementClass(d, "blah"), "swapElementClass !has fromClass" ); + ok( !swapElementClass(d, "blah", "baz"), "swapElementClass twice" ); + ok( hasElementClass(d, "baz"), "swapElementClass has toClass" ); + ok( !hasElementClass(d, "blah"), "swapElementClass !has fromClass" ); + + TABLE; + TBODY; + TR; + var t = TABLE(null, + TBODY({"class": "foo bar", "id":"tbody0"}, + TR({"class": "foo", "id":"tr0"}), + TR({"class": "bar", "id":"tr1"}) + ) + ); + + var matchElements = getElementsByTagAndClassName; + is( + map(itemgetter("id"), matchElements(null, "foo", t)).join(" "), + "tbody0 tr0", + "getElementsByTagAndClassName found all tags with foo class" + ); + is( + map(itemgetter("id"), matchElements("tr", "foo", t)).join(" "), + "tr0", + "getElementsByTagAndClassName found all tr tags with foo class" + ); + is( + map(itemgetter("id"), matchElements("tr", null, t)).join(" "), + "tr0 tr1", + "getElementsByTagAndClassName found all tr tags" + ); + + var oldDoc = document; + var doc = MochiKit.MockDOM.createDocument(); + is( currentDocument(), document, "currentDocument() correct" ); + withDocument(doc, function () { + ok( document != doc, "global doc unchanged" ); + is( currentDocument(), doc, "currentDocument() correct" ); + var h1 = H1(); + var span = SPAN(null, "foo", h1); + appendChildNodes(currentDocument().body, span); + }); + is( document, oldDoc, "doc restored" ); + is( doc.childNodes.length, 1, "doc has one child" ); + is( doc.body.childNodes.length, 1, "body has one child" ); + var sp = doc.body.childNodes[0]; + is( sp.nodeName, "SPAN", "only child is SPAN" ); + is( sp.childNodes.length, 2, "SPAN has two childNodes" ); + is( sp.childNodes[0].nodeValue, "foo", "first node is text" ); + is( sp.childNodes[1].nodeName, "H1", "second child is H1" ); + + is( currentDocument(), document, "currentDocument() correct" ); + try { + withDocument(doc, function () { + ok( document != doc, "global doc unchanged" ); + is( currentDocument(), doc, "currentDocument() correct" ); + throw new Error("foo"); + }); + ok( false, "didn't throw" ); + } catch (e) { + ok( true, "threw" ); + } + + var mockWindow = {"foo": "bar"}; + is (currentWindow(), window, "currentWindow ok"); + withWindow(mockWindow, function () { + is(currentWindow(), mockWindow, "withWindow ok"); + }); + is (currentWindow(), window, "currentWindow ok"); + + doc = MochiKit.MockDOM.createDocument(); + var frm; + withDocument(doc, function () { + frm = FORM({name: "ignore"}, + INPUT({name:"foo", value:"bar"}), + INPUT({name:"foo", value:"bar"}), + INPUT({name:"baz", value:"bar"}) + ); + }); + var kv = formContents(frm); + is( kv[0].join(","), "foo,foo,baz", "mock formContents names" ); + is( kv[1].join(","), "bar,bar,bar", "mock formContents values" ); + is( queryString(frm), "foo=bar&foo=bar&baz=bar", "mock queryString hook" ); + + var kv = formContents("form_test"); + is( kv[0].join(","), "select,selmultiple,selmultiple,hidden,radio_on", "formContents names" ); + is( kv[1].join(","), "foo,bar,baz,test,2", "formContents values" ); + is( queryString("form_test"), "select=foo&selmultiple=bar&selmultiple=baz&hidden=test&radio_on=2", "queryString hook" ); + kv = formContents("form_test2"); + is( kv[0].join(","), "selempty,selempty2", "formContents names empty option values" ); + is( kv[1].join(","), ",foo", "formContents empty option values" ); + is( queryString("form_test2"), "selempty=&selempty2=foo", "queryString empty option values" ); + + var d = DIV(null, SPAN(), " \n\t", SPAN(), "foo", SPAN(), " "); + is( d.childNodes.length, 6, "removeEmptyNodes test conditions correct" ); + removeEmptyTextNodes(d); + is( d.childNodes.length, 4, "removeEmptyNodes" ); + + is( getFirstParentByTagAndClassName('child', 'div', 'two'), getElement("parentTwo"), "getFirstParentByTagAndClassName found parent" ); + is( getFirstParentByTagAndClassName('child', 'div'), getElement("parentZero"), "getFirstParentByTagAndClassName found parent (any class)" ); + is( getFirstParentByTagAndClassName('child', '*', 'two'), getElement("parentTwo"), "getFirstParentByTagAndClassName found parent (any tag)" ); + + ok( true, "test suite finished!"); + + +} catch (err) { + + var s = "test suite failure!\n"; + var o = {}; + var k = null; + for (k in err) { + // ensure unique keys?! + if (!o[k]) { + s += k + ": " + err[k] + "\n"; + o[k] = err[k]; + } + } + ok ( false, s ); + +} +</script> +</pre> +</body> +</html> diff --git a/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-DateTime.html b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-DateTime.html new file mode 100644 index 0000000000..43ad9d4abd --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-DateTime.html @@ -0,0 +1,39 @@ +<html> +<head> + <script type="text/javascript" src="../MochiKit/Base.js"></script> + <script type="text/javascript" src="../MochiKit/DateTime.js"></script> + <script type="text/javascript" src="../MochiKit/Iter.js"></script> + <script type="text/javascript" src="../MochiKit/DOM.js"></script> + <script type="text/javascript" src="../MochiKit/Style.js"></script> + <script type="text/javascript" src="SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="SimpleTest/test.css"> +</head> +<body> + +<pre id="test"> +<script type="text/javascript" src="test_DateTime.js"></script> +<script type="text/javascript"> +try { + + tests.test_DateTime({ok:ok, is:is}); + ok( true, "test suite finished!"); + +} catch (err) { + + var s = "test suite failure!\n"; + var o = {}; + var k = null; + for (k in err) { + // ensure unique keys?! + if (!o[k]) { + s += k + ": " + err[k] + "\n"; + o[k] = err[k]; + } + } + ok ( false, s ); + +} +</script> +</pre> +</body> +</html> diff --git a/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-DragAndDrop.html b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-DragAndDrop.html new file mode 100644 index 0000000000..a191a53bee --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-DragAndDrop.html @@ -0,0 +1,54 @@ +<html> +<head> + <script type="text/javascript" src="../MochiKit/Base.js"></script> + <script type="text/javascript" src="../MochiKit/Iter.js"></script> + <script type="text/javascript" src="../MochiKit/DOM.js"></script> + <script type="text/javascript" src="../MochiKit/Style.js"></script> + <script type="text/javascript" src="../MochiKit/Color.js"></script> + <script type="text/javascript" src="../MochiKit/Signal.js"></script> + <script type="text/javascript" src="../MochiKit/Visual.js"></script> + <script type="text/javascript" src="../MochiKit/DragAndDrop.js"></script> + <script type="text/javascript" src="SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="SimpleTest/test.css"> + <style type="text/css"> + .drop-hover { + } + #drag1 { + visibility: hidden; + } + #drop1 { + visibility: hidden; + } + </style> +</head> +<body> +<div id='drag1'>drag1</div> +<div id='drop1'>drop1</div> +<pre id="test"> +<script type="text/javascript" src="test_DragAndDrop.js"></script> +<script type="text/javascript"> +try { + + // Counting the number of tests is really lame + tests.test_DragAndDrop({ok:ok, is:is}); + ok( true, "test suite finished!"); + +} catch (err) { + + var s = "test suite failure!\n"; + var o = {}; + var k = null; + for (k in err) { + // ensure unique keys?! + if (!o[k]) { + s += k + ": " + err[k] + "\n"; + o[k] = err[k]; + } + } + ok ( false, s ); + +} +</script> +</pre> +</body> +</html> diff --git a/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Format.html b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Format.html new file mode 100644 index 0000000000..58bffa6bf8 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Format.html @@ -0,0 +1,39 @@ +<html> +<head> + <script type="text/javascript" src="../MochiKit/Base.js"></script> + <script type="text/javascript" src="../MochiKit/Format.js"></script> + <script type="text/javascript" src="../MochiKit/Iter.js"></script> + <script type="text/javascript" src="../MochiKit/DOM.js"></script> + <script type="text/javascript" src="../MochiKit/Style.js"></script> + <script type="text/javascript" src="SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="SimpleTest/test.css"> +</head> +<body> + +<pre id="test"> +<script type="text/javascript" src="test_Format.js"></script> +<script type="text/javascript"> +try { + + tests.test_Format({ok:ok, is:is}); + ok( true, "test suite finished!"); + +} catch (err) { + + var s = "test suite failure!\n"; + var o = {}; + var k = null; + for (k in err) { + // ensure unique keys?! + if (!o[k]) { + s += k + ": " + err[k] + "\n"; + o[k] = err[k]; + } + } + ok ( false, s ); + +} +</script> +</pre> +</body> +</html> diff --git a/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Iter.html b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Iter.html new file mode 100644 index 0000000000..8086acc2db --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Iter.html @@ -0,0 +1,38 @@ +<html> +<head> + <script type="text/javascript" src="../MochiKit/Base.js"></script> + <script type="text/javascript" src="../MochiKit/Iter.js"></script> + <script type="text/javascript" src="../MochiKit/DOM.js"></script> + <script type="text/javascript" src="../MochiKit/Style.js"></script> + <script type="text/javascript" src="SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="SimpleTest/test.css"> +</head> +<body> + +<pre id="test"> +<script type="text/javascript" src="test_Iter.js"></script> +<script type="text/javascript"> +try { + + tests.test_Iter({ok:ok, is:is}); + ok( true, "test suite finished!"); + +} catch (err) { + + var s = "test suite failure!\n"; + var o = {}; + var k = null; + for (k in err) { + // ensure unique keys?! + if (!o[k]) { + s += k + ": " + err[k] + "\n"; + o[k] = err[k]; + } + } + ok ( false, s ); + +} +</script> +</pre> +</body> +</html> diff --git a/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-JSAN.html b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-JSAN.html new file mode 100644 index 0000000000..53a0e0ed04 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-JSAN.html @@ -0,0 +1,32 @@ +<html> +<head> + <script type="text/javascript" src="JSAN.js"></script> +</head> +<body> + +<pre id="test"> +<script type="text/javascript"> + // TODO: Make this a harness for the other tests + JSAN.use('Test.More'); + JSAN.addRepository('..'); + var lst = []; + plan({"tests": 1}); + var wc = {}; + wc['MochiKit'] = true; + for (var k in window) { wc[k] = true; } + for (var k in window) { wc[k] = true; } + JSAN.use('MochiKit.MochiKit', []); + for (var k in window) { + if (!(k in wc) && !(k.charAt(0) == '[')) { + lst.push(k); + } + } + lst.sort(); + pollution = lst.join(" "); + is(pollution, "compare reduce", "namespace pollution?"); + JSAN.use('MochiKit.MochiKit'); + +</script> +</pre> +</body> +</html> diff --git a/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Logging.html b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Logging.html new file mode 100644 index 0000000000..d92229a6e0 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Logging.html @@ -0,0 +1,40 @@ +<html> +<head> + <script type="text/javascript" src="../MochiKit/Base.js"></script> + <script type="text/javascript" src="../MochiKit/Logging.js"></script> + <script type="text/javascript" src="../MochiKit/Iter.js"></script> + <script type="text/javascript" src="../MochiKit/DOM.js"></script> + <script type="text/javascript" src="../MochiKit/Style.js"></script> + <script type="text/javascript" src="SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="SimpleTest/test.css"> + +</head> +<body> + +<pre id="test"> +<script type="text/javascript" src="test_Logging.js"></script> +<script type="text/javascript"> +try { + + tests.test_Logging({ok:ok, is:is}); + ok( true, "test suite finished!"); + +} catch (err) { + + var s = "test suite failure!\n"; + var o = {}; + var k = null; + for (k in err) { + // ensure unique keys?! + if (!o[k]) { + s += k + ": " + err[k] + "\n"; + o[k] = err[k]; + } + } + ok ( false, s ); + +} +</script> +</pre> +</body> +</html> diff --git a/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-MochiKit.html b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-MochiKit.html new file mode 100644 index 0000000000..d1a8b60d89 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-MochiKit.html @@ -0,0 +1,18 @@ +<html> +<head> + <script type="text/javascript" src="../MochiKit/MochiKit.js"></script> + <script type="text/javascript" src="SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="SimpleTest/test.css"> +</head> +<body> + +<pre id="test"> +<script type="text/javascript"> + is( isUndefined(null), false, "null is not undefined" ); + is( isUndefined(""), false, "empty string is not undefined" ); + is( isUndefined(undefined), true, "undefined is undefined" ); + is( isUndefined({}.foo), true, "missing property is undefined" ); +</script> +</pre> +</body> +</html> diff --git a/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Selector.html b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Selector.html new file mode 100644 index 0000000000..503acef071 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Selector.html @@ -0,0 +1,274 @@ +<html> +<head> + <script type="text/javascript" src="../MochiKit/MockDOM.js"></script> + <script type="text/javascript" src="../MochiKit/Base.js"></script> + <script type="text/javascript" src="../MochiKit/Iter.js"></script> + <script type="text/javascript" src="../MochiKit/DOM.js"></script> + <script type="text/javascript" src="../MochiKit/Style.js"></script> + <script type="text/javascript" src="../MochiKit/Selector.js"></script> + <script type="text/javascript" src="SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="SimpleTest/test.css"> + <style type="text/css"> + p, #sequence { + display: none; + } + </style> +</head> +<body> + <p>Test originally from <a href="http://simon.incutio.com/archive/2003/03/25/#getElementsBySelector" rel="bookmark">this blog entry</a>.</p> + + <p>Here are some links in a normal paragraph: <a href="http://www.google.com/" title="Google!">Google</a>, <a href="http://groups.google.com/">Google Groups</a>. This link has <code>class="blog"</code>: <a href="http://diveintomark.org/" class="blog" fakeattribute="bla">diveintomark</a></p> + <div id="foo"> + <p>Everything inside the red border is inside a div with <code>id="foo"</code>.</p> + <p>This is a normal link: <a href="http://www.yahoo.com/">Yahoo</a></p> + + <a style="display: none" href="http://www.example.com/outsidep">This a is not inside a p</a> + + <p>This link has <code>class="blog"</code>: <a href="http://simon.incutio.com/" class="blog">Simon Willison's Weblog</a></p> + <p>This <span><a href="http://www.example.com/insidespan">link</a></span> is inside a span, not directly child of p</p> + <p lang="en-us">Nonninn</p> + <p lang="is-IS">Sniðugt</p> + <p> + <input type="button" name="enabled" value="enabled" id="enabled"> + <input type="button" name="disabled" value="disabled" id="disabled" disabled="1" /> + <input type="checkbox" name="checked" value="checked" id="checked" checked="1" /> + </p> + </div> + + <div id="sequence"> + <a href="http://www.example.com/link1">Link 1</a> + <a href="http://www.example.com/link2">Link 2</a> + <a href="http://www.example.com/link3">Link 3</a> + <a href="http://www.example.com/link4">Link 4</a> + <p>Something else</p> + <a href="http://www.example.com/link5">Link 5</a> + <a href="http://www.example.com/link6">Link 6</a> + <a href="http://www.example.com/link7">Link 7</a> + <a href="http://www.example.com/link8">Link 8</a> + </div> + + <div id="multiclass" class="multiple classnames here"></div> +<pre id="test"> +<script type="text/javascript"> +try { + + var testExpected = function (res, exp, lbl) { + for (var i=0; i < res.length; i ++) { + is( res[i].href, exp[i], lbl + ' (' + i + ')'); + } + }; + + var expected = ['http://simon.incutio.com/archive/2003/03/25/#getElementsBySelector', + 'http://www.google.com/', + 'http://groups.google.com/', + 'http://diveintomark.org/', + 'http://www.yahoo.com/', + 'http://www.example.com/outsidep', + 'http://simon.incutio.com/', + 'http://www.example.com/insidespan', + 'http://www.example.com/link1', + 'http://www.example.com/link2', + 'http://www.example.com/link3', + 'http://www.example.com/link4', + 'http://www.example.com/link5', + 'http://www.example.com/link6', + 'http://www.example.com/link7', + 'http://www.example.com/link8']; + var results = $$('a'); + testExpected(results, expected, "'a' selector"); + + expected = ['http://diveintomark.org/', 'http://simon.incutio.com/']; + results = $$('p a.blog'); + testExpected(results, expected, "'p a.blog' selector"); + + expected = ['http://www.yahoo.com/', + 'http://www.example.com/outsidep', + 'http://simon.incutio.com/', + 'http://www.example.com/insidespan', + 'http://www.example.com/link1', + 'http://www.example.com/link2', + 'http://www.example.com/link3', + 'http://www.example.com/link4', + 'http://www.example.com/link5', + 'http://www.example.com/link6', + 'http://www.example.com/link7', + 'http://www.example.com/link8']; + results = $$('div a'); + testExpected(results, expected, "'div a' selector"); + + expected = ['http://www.yahoo.com/', + 'http://www.example.com/outsidep', + 'http://simon.incutio.com/', + 'http://www.example.com/insidespan']; + results = $$('div#foo a'); + testExpected(results, expected, "'div#foo a' selector"); + + expected = ['http://simon.incutio.com/', + 'http://www.example.com/insidespan']; + results = $$('#foo a.blog'); + testExpected(results, expected, "'#foo a.blog' selector"); + + expected = ['http://diveintomark.org/', + 'http://simon.incutio.com/', + 'http://www.example.com/insidespan']; + results = $$('.blog'); + testExpected(results, expected, "'.blog' selector"); + + expected = ['http://www.google.com/', + 'http://www.yahoo.com/', + 'http://www.example.com/outsidep', + 'http://www.example.com/insidespan', + 'http://www.example.com/link1', + 'http://www.example.com/link2', + 'http://www.example.com/link3', + 'http://www.example.com/link4', + 'http://www.example.com/link5', + 'http://www.example.com/link6', + 'http://www.example.com/link7', + 'http://www.example.com/link8']; + results = $$('a[href^="http://www"]'); + testExpected(results, expected, "'a[href^=http://www]' selector"); + + expected = ['http://diveintomark.org/']; + results = $$('a[href$="org/"]'); + testExpected(results, expected, "'a[href$=org/]' selector"); + + expected = ['http://www.google.com/', + 'http://groups.google.com/']; + results = $$('a[href*="google"]'); + testExpected(results, expected, "'a[href*=google]' selector"); + + expected = ['http://simon.incutio.com/archive/2003/03/25/#getElementsBySelector']; + results = $$('a[rel="bookmark"]'); + testExpected(results, expected, "'a[rel=bookmark]' selector"); + + expected = ['http://diveintomark.org/']; + results = $$('a[fakeattribute]'); + testExpected(results, expected, "'a[fakeattribute]' selector"); + + /* This doesn't work in IE due to silly DOM implementation + expected = ['http://www.google.com/']; + results = $$('a[title]'); + testExpected(results, expected, "'a[title]' selector"); + */ + + results = $$('p[lang|="en"]'); + is( results[0].firstChild.nodeValue, 'Nonninn', "'p[lang|=en]' selector"); + + expected = ['http://simon.incutio.com/archive/2003/03/25/#getElementsBySelector', + 'http://www.google.com/', + 'http://groups.google.com/', + 'http://diveintomark.org/', + 'http://www.yahoo.com/', + 'http://simon.incutio.com/', + 'http://www.example.com/insidespan']; + results = $$('p > a'); + testExpected(results, expected, "'p > a' selector"); + + expected = ['http://www.example.com/insidespan']; + results = $$('span > a'); + testExpected(results, expected, "'span > a' selector"); + + expected = ['http://groups.google.com/', + 'http://www.example.com/link2', + 'http://www.example.com/link3', + 'http://www.example.com/link4', + 'http://www.example.com/link6', + 'http://www.example.com/link7', + 'http://www.example.com/link8']; + results = $$('a + a'); + testExpected(results, expected, "'a + a' selector"); + + expected = ['http://www.example.com/link1', + 'http://www.example.com/link3', + 'http://www.example.com/link6', + 'http://www.example.com/link8']; + results = $$('#sequence a:nth-child(odd)'); + testExpected(results, expected, "'#sequence a:nth-child(odd)' selector"); + + expected = ['http://www.example.com/link1', + 'http://www.example.com/link3', + 'http://www.example.com/link5', + 'http://www.example.com/link7']; + results = $$('#sequence a:nth-of-type(odd)'); + testExpected(results, expected, "'#sequence a:nth-of-type(odd)' selector"); + + expected = ['http://www.example.com/link1', + 'http://www.example.com/link4', + 'http://www.example.com/link7']; + results = $$('#sequence a:nth-of-type(3n+1)'); + testExpected(results, expected, "'#sequence a:nth-of-type(3n+1)' selector"); + + expected = ['http://www.example.com/link5']; + results = $$('#sequence a:nth-child(6)'); + testExpected(results, expected, "'#sequence a:nth-child(6)' selector"); + + expected = ['http://www.example.com/link5']; + results = $$('#sequence a:nth-of-type(5)'); + testExpected(results, expected, "'#sequence a:nth-of-type(5)' selector"); + + expected = [$('enabled'), $('checked')]; + results = $$('body :enabled'); + for (var i=0; i < results.length; i ++) { + is( results[i], expected[i], "'body :enabled" + ' (' + i + ')'); + } + + expected = [$('disabled')]; + results = $$('body :disabled'); + for (var i=0; i < results.length; i ++) { + is( results[i], expected[i], "'body :disabled" + ' (' + i + ')'); + } + + expected = [$('checked')]; + results = $$('body :checked'); + for (var i=0; i < results.length; i ++) { + is( results[i], expected[i], "'body :checked" + ' (' + i + ')'); + } + + expected = document.getElementsByTagName('p'); + results = $$('a[href$=outsidep] ~ *'); + for (var i=0; i < results.length; i ++) { + is( results[i], expected[i+4], "'a[href$=outsidep] ~ *' selector" + ' (' + i + ')'); + } + + expected = [document.documentElement]; + results = $$(':root'); + for (var i=0; i < results.length; i ++) { + is( results[i], expected[i], "':root' selector" + ' (' + i + ')'); + } + + expected = [$('multiclass')]; + results = $$('[class~=classnames]'); + for (var i=0; i < results.length; i ++) { + is( results[i], expected[i], "'~=' attribute test" + ' (' + i + ')'); + } + + var doc = MochiKit.MockDOM.createDocument(); + appendChildNodes(doc.body, A({"href": "http://www.example.com/insideAnotherDocument"}, "Inside a document")); + withDocument(doc, function(){ + is( $$(":root")[0], doc, ":root on a different document" ); + is( $$("a")[0], doc.body.firstChild, "a inside a different document" ); + }); + + ok( true, "test suite finished!"); + + +} catch (err) { + + var s = "test suite failure!\n"; + var o = {}; + var k = null; + for (k in err) { + // ensure unique keys?! + if (!o[k]) { + s += k + ": " + err[k] + "\n"; + o[k] = err[k]; + } + } + ok ( false, s ); + +} +</script> +</pre> +</body> +</html> diff --git a/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Signal.html b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Signal.html new file mode 100644 index 0000000000..9c37416d68 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Signal.html @@ -0,0 +1,43 @@ +<html> +<head> + <script type="text/javascript" src="../MochiKit/Base.js"></script> + <script type="text/javascript" src="../MochiKit/Iter.js"></script> + <script type="text/javascript" src="../MochiKit/DOM.js"></script> + <script type="text/javascript" src="../MochiKit/Style.js"></script> + <script type="text/javascript" src="../MochiKit/Signal.js"></script> + <script type="text/javascript" src="../MochiKit/Logging.js"></script> + <script type="text/javascript" src="SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="SimpleTest/test.css"> + +</head> +<body> + +Please ignore this button: <input type="submit" id="submit" /><br /> + +<pre id="test"> +<script type="text/javascript" src="test_Signal.js"></script> +<script type="text/javascript"> +try { + + tests.test_Signal({ok:ok, is:is}); + ok(true, "test suite finished!"); + +} catch (err) { + + var s = "test suite failure!\n"; + var o = {}; + var k = null; + for (k in err) { + // ensure unique keys?! + if (!o[k]) { + s += k + ": " + err[k] + "\n"; + o[k] = err[k]; + } + } + ok(false, s); + +} +</script> +</pre> +</body> +</html> diff --git a/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Style.html b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Style.html new file mode 100644 index 0000000000..b01adc6e7e --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Style.html @@ -0,0 +1,141 @@ +<html> +<head> + <script type="text/javascript" src="../MochiKit/MockDOM.js"></script> + <script type="text/javascript" src="../MochiKit/Base.js"></script> + <script type="text/javascript" src="../MochiKit/DOM.js"></script> + <script type="text/javascript" src="../MochiKit/Iter.js"></script> + <script type="text/javascript" src="../MochiKit/Style.js"></script> + <script type="text/javascript" src="../MochiKit/Color.js"></script> + <script type="text/javascript" src="../MochiKit/Logging.js"></script> + <script type="text/javascript" src="SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="SimpleTest/test.css"> +</head> +<body style="border: 0; margin: 0; padding: 0;"> + +<div id="styleTest" style="position: absolute; left: 400px; top: 100px; width: 100px; height: 100px; background: rgb(255, 0, 0); opacity: 0.5; filter: alpha(opacity=50); font-size: 10px">TEST<span id="styleSubTest">SUB</span></div> + +<pre id="test"> +<script type="text/javascript"> + +try { + + // initial + var pos = getElementPosition('styleTest'); + is(pos.x, 400, 'initial x position'); + is(pos.y, 100, 'initial y position'); + + // moved + var newPos = new MochiKit.Style.Coordinates(500, 200); + setElementPosition('styleTest', newPos); + pos = getElementPosition('styleTest'); + is(pos.x, 500, 'updated x position'); + is(pos.y, 200, 'updated y position'); + + // moved with relativeTo + anotherPos = new MochiKit.Style.Coordinates(100, 100); + pos = getElementPosition('styleTest', anotherPos); + is(pos.x, 400, 'updated x position (using relativeTo parameter)'); + is(pos.y, 100, 'updated y position (using relativeTo parameter)'); + + // Coordinates object + pos = getElementPosition({x: 123, y: 321}); + is(pos.x, 123, 'passthrough x position'); + is(pos.y, 321, 'passthrough y position'); + + // Coordinates object with relativeTo + pos = getElementPosition({x: 123, y: 321}, {x: 100, y: 50}); + is(pos.x, 23, 'passthrough x position (using relativeTo parameter)'); + is(pos.y, 271, 'passthrough y position (using relativeTo parameter)'); + + pos = getElementPosition('garbage'); + is(typeof(pos), 'undefined', + 'invalid element should return an undefined position'); + + // Only set one coordinate + setElementPosition('styleTest', {'x': 300}); + pos = getElementPosition('styleTest'); + is(pos.x, 300, 'updated only x position'); + is(pos.y, 200, 'not updated y position'); + + var mc = MochiKit.Color.Color; + var red = mc.fromString('rgb(255,0,0)'); + var color = null; + + color = mc.fromString(getStyle('styleTest', 'background-color')); + is(color.toHexString(), red.toHexString(), + 'test getStyle selector case'); + + color = mc.fromString(getStyle('styleTest', 'backgroundColor')); + is(color.toHexString(), red.toHexString(), + 'test getStyle camel case'); + + is(getStyle('styleSubTest', 'font-size'), '10px', + 'test computed getStyle selector case'); + + is(getStyle('styleSubTest', 'fontSize'), '10px', + 'test computed getStyle camel case'); + + is(eval(getStyle('styleTest', 'opacity')), 0.5, + 'test getStyle opacity'); + + is(getStyle('styleTest', 'opacity'), 0.5, 'test getOpacity'); + + setStyle('styleTest', {'opacity': 0.2}); + is(getStyle('styleTest', 'opacity'), 0.2, 'test setOpacity'); + + setStyle('styleTest', {'opacity': 0}); + is(getStyle('styleTest', 'opacity'), 0, 'test setOpacity'); + + setStyle('styleTest', {'opacity': 1}); + var t = getStyle('styleTest', 'opacity'); + ok(t > 0.999 && t <= 1, 'test setOpacity'); + + var dims = getElementDimensions('styleTest'); + is(dims.w, 100, 'getElementDimensions w ok'); + is(dims.h, 100, 'getElementDimensions h ok'); + + setElementDimensions('styleTest', {'w': 200, 'h': 150}); + dims = getElementDimensions('styleTest'); + is(dims.w, 200, 'setElementDimensions w ok'); + is(dims.h, 150, 'setElementDimensions h ok'); + + setElementDimensions('styleTest', {'w': 150}); + dims = getElementDimensions('styleTest'); + is(dims.w, 150, 'setElementDimensions only w ok'); + is(dims.h, 150, 'setElementDimensions h not updated ok'); + + hideElement('styleTest'); + dims = getElementDimensions('styleTest'); + is(dims.w, 150, 'getElementDimensions w ok when display none'); + is(dims.h, 150, 'getElementDimensions h ok when display none'); + + dims = getViewportDimensions(); + is(dims.w > 0, true, 'test getViewportDimensions w'); + is(dims.h > 0, true, 'test getViewportDimensions h'); + + pos = getViewportPosition(); + is(pos.x, 0, 'test getViewportPosition x'); + is(pos.y, 0, 'test getViewportPosition y'); + + ok( true, "test suite finished!"); + + +} catch (err) { + + var s = "test suite failure!\n"; + var o = {}; + var k = null; + for (k in err) { + // ensure unique keys?! + if (!o[k]) { + s += k + ": " + err[k] + "\n"; + o[k] = err[k]; + } + } + ok ( false, s ); + +} +</script> +</pre> +</body> +</html> diff --git a/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Visual.html b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Visual.html new file mode 100644 index 0000000000..e4a40c7e48 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/MochiKit-Visual.html @@ -0,0 +1,190 @@ +<html> +<head> + <script type="text/javascript" src="../MochiKit/Base.js"></script> + <script type="text/javascript" src="../MochiKit/Iter.js"></script> + <script type="text/javascript" src="../MochiKit/DOM.js"></script> + <script type="text/javascript" src="../MochiKit/Async.js"></script> + <script type="text/javascript" src="../MochiKit/Style.js"></script> + <script type="text/javascript" src="../MochiKit/Color.js"></script> + <script type="text/javascript" src="../MochiKit/Signal.js"></script> + <script type="text/javascript" src="../MochiKit/Position.js"></script> + <script type="text/javascript" src="../MochiKit/Visual.js"></script> + <script type="text/javascript" src="SimpleTest/SimpleTest.js"></script> + + <link rel="stylesheet" type="text/css" href="SimpleTest/test.css"> + <style type="text/css"> + #elt1, #elt2, #ctn1 { + visibility: hidden; + font-size: 1em; + margin: 2px; + } + #elt3 { + display: none; + } + #ctn1 { + height: 2px; + } + </style> +</head> +<body> + +<div id='elt1'>elt1</div> +<div id='ctn1'><div id='elt2'></div></div> +<div id='elt3'>elt3</div> +<pre id="test"> +<script type="text/javascript"> +try { + var TestQueue = function () { + }; + + TestQueue.prototype = new MochiKit.Visual.ScopedQueue(); + + MochiKit.Base.update(TestQueue.prototype, { + startLoop: function (func, interval) { + this.started = true; + var timePos = new Date().getTime(); + while (this.started) { + timePos += interval; + MochiKit.Base.map(function (effect) { + effect.loop(timePos); + }, this.effects); + } + }, + stopLoop: function () { + this.started = false; + } + }); + + var gl = new TestQueue(); + MochiKit.Visual.Queues.instances['global'] = gl; + MochiKit.Visual.Queues.instances['elt1'] = gl; + MochiKit.Visual.Queues.instances['elt2'] = gl; + MochiKit.Visual.Queues.instances['elt3'] = gl; + MochiKit.Visual.Queues.instances['ctn1'] = gl; + MochiKit.Visual.Queue = gl; + + pulsate("elt1", {afterFinish: function () { + is(getElement('elt1').style.display != 'none', true, "pulsate ok"); + }}); + + pulsate("elt1", {pulses: 2, afterFinish: function () { + is(getElement('elt1').style.display != 'none', true, "pulsate with numbered pulses ok"); + }}); + + shake("elt1", {afterFinish: function () { + is(getElement('elt1').style.display != 'none', true, "shake ok"); + }}); + + fade("elt1", {afterFinish: function () { + is(getElement('elt1').style.display, 'none', "fade ok"); + }}); + + appear("elt1", {afterFinish: function () { + is(getElement('elt1').style.display != 'none', true, "appear ok"); + }}); + + toggle("elt1", "size", {afterFinish: function () { + is(getElement('elt1').style.display, 'none', "toggle size ok"); + }}); + + toggle("elt1", "size", {afterFinish: function () { + is(getElement('elt1').style.display != 'none', true, "toggle size reverse ok"); + }}); + + Morph("elt1", {"style": {"font-size": "2em"}, afterFinish: function () { + is(getStyle("elt1", "font-size"), "2em", "Morph OK"); + }}); + + Morph("elt1", {"style": {"font-size": "1em", "margin-left": "4px"}, afterFinish: function () { + is(getStyle("elt1", "font-size"), "1em", "Morph multiple (font) OK"); + is(getStyle("elt1", "margin-left"), "4px", "Morph multiple (margin) OK"); + }}); + + switchOff("elt1", {afterFinish: function () { + is(getElement('elt1').style.display, 'none', "switchOff ok"); + }}); + + grow("elt1", {afterFinish: function () { + is(getElement('elt1').style.display != 'none', true, "grow ok"); + }}); + + shrink("elt1", {afterFinish: function () { + is(getElement('elt1').style.display, 'none', "shrink ok"); + }}); + + showElement('elt1'); + dropOut("elt1", {afterFinish: function () { + is(getElement('elt1').style.display, 'none', "dropOut ok"); + }}); + + showElement('elt1'); + puff("elt1", {afterFinish: function () { + is(getElement('elt1').style.display, 'none', "puff ok"); + }}); + + showElement('elt1'); + fold("elt1", {afterFinish: function () { + is(getElement('elt1').style.display, 'none', "fold ok"); + }}); + + showElement('elt1'); + squish("elt1", {afterFinish: function () { + is(getElement('elt1').style.display, 'none', "squish ok"); + }}); + + slideUp("ctn1", {afterFinish: function () { + is(getElement('ctn1').style.display, 'none', "slideUp ok"); + }}); + + slideDown("ctn1", {afterFinish: function () { + is(getElement('ctn1').style.display != 'none', true, "slideDown ok"); + }}); + + blindDown("ctn1", {afterFinish: function () { + is(getElement('ctn1').style.display != 'none', true, "blindDown ok"); + }}); + + blindUp("ctn1", {afterFinish: function () { + is(getElement('ctn1').style.display, 'none', "blindUp ok"); + }}); + + multiple(["elt1", "ctn1"], appear, {afterFinish: function (effect) { + is(effect.element.style.display != 'none', true, "multiple ok"); + }}); + + toggle("elt3", "size", {afterFinish: function () { + is(getElement('elt3').style.display != 'none', true, "toggle with css ok"); + }}); + + toggle("elt3", "size", {afterFinish: function () { + is(getElement('elt3').style.display, 'none', "toggle with css ok"); + }}); + + var toTests = [roundElement, roundClass, tagifyText, Opacity, Move, Scale, Highlight, ScrollTo, Morph]; + for (var m in toTests) { + toTests[m]("elt1"); + ok(true, toTests[m].NAME + " doesn't need 'new' keyword"); + } + ok(true, "visual suite finished"); + +} catch (err) { + + var s = "test suite failure!\n"; + var o = {}; + var k = null; + for (k in err) { + // ensure unique keys?! + if (!o[k]) { + s += k + ": " + err[k] + "\n"; + o[k] = err[k]; + } + } + ok ( false, s ); + SimpleTest.finish(); + +} +</script> +</pre> +</body> +</html> + diff --git a/dom/tests/mochitest/ajax/mochikit/tests/SimpleTest/SimpleTest.js b/dom/tests/mochitest/ajax/mochikit/tests/SimpleTest/SimpleTest.js new file mode 100644 index 0000000000..53c2a87939 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/SimpleTest/SimpleTest.js @@ -0,0 +1,473 @@ +/** + * SimpleTest, a partial Test.Simple/Test.More API compatible test library. + * + * Why? + * + * Test.Simple doesn't work on IE < 6. + * TODO: + * * Support the Test.Simple API used by MochiKit, to be able to test MochiKit + * itself against IE 5.5 + * +**/ + +if (typeof(SimpleTest) == "undefined") { + var SimpleTest = {}; +} + +var parentRunner = null; +if (typeof(parent) != "undefined" && parent.TestRunner) { + parentRunner = parent.TestRunner; +} else if (parent && parent.wrappedJSObject && + parent.wrappedJSObject.TestRunner) { + parentRunner = parent.wrappedJSObject.TestRunner; +} + +// Check to see if the TestRunner is present and has logging +if (parentRunner) { + SimpleTest._logEnabled = parentRunner.logEnabled; +} + +SimpleTest._tests = []; +SimpleTest._stopOnLoad = true; + +/** + * Something like assert. +**/ +SimpleTest.ok = function (condition, name) { + if (arguments.length > 2) { + const diag = "Too many arguments passed to `ok(condition, name)`"; + SimpleTest.record(false, name, diag); + } else { + SimpleTest.record(condition, name); + } +} + +SimpleTest.record = function (condition, name, diag) { + var test = {'result': !!condition, 'name': name, 'diag': diag || ""}; + if (SimpleTest._logEnabled) + SimpleTest._logResult(test, "TEST-PASS", "TEST-UNEXPECTED-FAIL"); + SimpleTest._tests.push(test); +}; + +/** + * Roughly equivalent to ok(a==b, name) +**/ +SimpleTest.is = function (a, b, name) { + var repr = MochiKit.Base.repr; + SimpleTest.record(a == b, name, "got " + repr(a) + ", expected " + repr(b)); +}; + +SimpleTest.isnot = function (a, b, name) { + var repr = MochiKit.Base.repr; + SimpleTest.record(a != b, name, "Didn't expect " + repr(a) + ", but got it."); +}; + +// --------------- Test.Builder/Test.More todo() ----------------- + +SimpleTest.todo = function(condition, name, diag) { + var test = {'result': !!condition, 'name': name, 'diag': diag || "", todo: true}; + if (SimpleTest._logEnabled) + SimpleTest._logResult(test, "TEST-UNEXPECTED-PASS", "TEST-KNOWN-FAIL"); + SimpleTest._tests.push(test); +}; + +SimpleTest._logResult = function(test, passString, failString) { + var msg = test.result ? passString : failString; + msg += " | "; + if (parentRunner.currentTestURL) + msg += parentRunner.currentTestURL; + msg += " | " + test.name; + var diag = ""; + if (test.diag) + diag = " - " + test.diag; + if (test.result) { + if (test.todo) + parentRunner.logger.error(msg + diag); + else + parentRunner.logger.log(msg); + } else { + if (test.todo) + parentRunner.logger.log(msg); + else + parentRunner.logger.error(msg + diag); + } +}; + +/** + * Copies of is and isnot with the call to ok replaced by a call to todo. +**/ + +SimpleTest.todo_is = function (a, b, name) { + var repr = MochiKit.Base.repr; + SimpleTest.todo(a == b, name, "got " + repr(a) + ", expected " + repr(b)); +}; + +SimpleTest.todo_isnot = function (a, b, name) { + var repr = MochiKit.Base.repr; + SimpleTest.todo(a != b, name, "Didn't expect " + repr(a) + ", but got it."); +}; + + +/** + * Makes a test report, returns it as a DIV element. +**/ +SimpleTest.report = function () { + var DIV = MochiKit.DOM.DIV; + var passed = 0; + var failed = 0; + var todo = 0; + var results = MochiKit.Base.map( + function (test) { + var cls, msg; + if (test.todo && !test.result) { + todo++; + cls = "test_todo"; + msg = "todo - " + test.name + " " + test.diag; + } else if (test.result &&!test.todo) { + passed++; + cls = "test_ok"; + msg = "ok - " + test.name; + } else { + failed++; + cls = "test_not_ok"; + msg = "not ok - " + test.name + " " + test.diag; + } + return DIV({"class": cls}, msg); + }, + SimpleTest._tests + ); + var summary_class = ((failed == 0) ? 'all_pass' : 'some_fail'); + return DIV({'class': 'tests_report'}, + DIV({'class': 'tests_summary ' + summary_class}, + DIV({'class': 'tests_passed'}, "Passed: " + passed), + DIV({'class': 'tests_failed'}, "Failed: " + failed), + DIV({'class': 'tests_todo'}, "Todo: " + todo)), + results + ); +}; + +/** + * Toggle element visibility +**/ +SimpleTest.toggle = function(el) { + if (MochiKit.Style.computedStyle(el, 'display') == 'block') { + el.style.display = 'none'; + } else { + el.style.display = 'block'; + } +}; + +/** + * Toggle visibility for divs with a specific class. +**/ +SimpleTest.toggleByClass = function (cls, evt) { + var elems = getElementsByTagAndClassName('div', cls); + MochiKit.Base.map(SimpleTest.toggle, elems); + if (evt) + evt.preventDefault(); +}; + +/** + * Shows the report in the browser +**/ + +SimpleTest.showReport = function() { + var togglePassed = A({'href': '#'}, "Toggle passed tests"); + var toggleFailed = A({'href': '#'}, "Toggle failed tests"); + togglePassed.onclick = partial(SimpleTest.toggleByClass, 'test_ok'); + toggleFailed.onclick = partial(SimpleTest.toggleByClass, 'test_not_ok'); + var body = document.body; // Handles HTML documents + if (!body) { + // Do the XML thing + body = document.getElementsByTagNameNS("http://www.w3.org/1999/xhtml", + "body")[0] + } + var firstChild = body.childNodes[0]; + var addNode; + if (firstChild) { + addNode = function (el) { + body.insertBefore(el, firstChild); + }; + } else { + addNode = function (el) { + body.appendChild(el) + }; + } + addNode(togglePassed); + addNode(SPAN(null, " ")); + addNode(toggleFailed); + addNode(SimpleTest.report()); +}; + +/** + * Tells SimpleTest to don't finish the test when the document is loaded, + * useful for asynchronous tests. + * + * When SimpleTest.waitForExplicitFinish is called, + * explicit SimpleTest.finish() is required. +**/ +SimpleTest.waitForExplicitFinish = function () { + SimpleTest._stopOnLoad = false; +}; + +/** + * Talks to the TestRunner if being ran on a iframe and the parent has a + * TestRunner object. +**/ +SimpleTest.talkToRunner = function () { + if (parentRunner) { + parentRunner.testFinished(document); + } +}; + +/** + * Finishes the tests. This is automatically called, except when + * SimpleTest.waitForExplicitFinish() has been invoked. +**/ +SimpleTest.finish = function () { + SimpleTest.showReport(); + SimpleTest.talkToRunner(); +}; + + +addLoadEvent(function() { + if (SimpleTest._stopOnLoad) { + SimpleTest.finish(); + } +}); + +// --------------- Test.Builder/Test.More isDeeply() ----------------- + + +SimpleTest.DNE = {dne: 'Does not exist'}; +SimpleTest.LF = "\r\n"; +SimpleTest._isRef = function (object) { + var type = typeof(object); + return type == 'object' || type == 'function'; +}; + + +SimpleTest._deepCheck = function (e1, e2, stack, seen) { + var ok = false; + // Either they're both references or both not. + var sameRef = !(!SimpleTest._isRef(e1) ^ !SimpleTest._isRef(e2)); + if (e1 == null && e2 == null) { + ok = true; + } else if (e1 != null ^ e2 != null) { + ok = false; + } else if (e1 == SimpleTest.DNE ^ e2 == SimpleTest.DNE) { + ok = false; + } else if (sameRef && e1 == e2) { + // Handles primitives and any variables that reference the same + // object, including functions. + ok = true; + } else if (SimpleTest.isa(e1, 'Array') && SimpleTest.isa(e2, 'Array')) { + ok = SimpleTest._eqArray(e1, e2, stack, seen); + } else if (typeof e1 == "object" && typeof e2 == "object") { + ok = SimpleTest._eqAssoc(e1, e2, stack, seen); + } else { + // If we get here, they're not the same (function references must + // always simply rererence the same function). + stack.push({ vals: [e1, e2] }); + ok = false; + } + return ok; +}; + +SimpleTest._eqArray = function (a1, a2, stack, seen) { + // Return if they're the same object. + if (a1 == a2) return true; + + // JavaScript objects have no unique identifiers, so we have to store + // references to them all in an array, and then compare the references + // directly. It's slow, but probably won't be much of an issue in + // practice. Start by making a local copy of the array to as to avoid + // confusing a reference seen more than once (such as [a, a]) for a + // circular reference. + for (var j = 0; j < seen.length; j++) { + if (seen[j][0] == a1) { + return seen[j][1] == a2; + } + } + + // If we get here, we haven't seen a1 before, so store it with reference + // to a2. + seen.push([ a1, a2 ]); + + var ok = true; + // Only examines enumerable attributes. Only works for numeric arrays! + // Associative arrays return 0. So call _eqAssoc() for them, instead. + var max = a1.length > a2.length ? a1.length : a2.length; + if (max == 0) return SimpleTest._eqAssoc(a1, a2, stack, seen); + for (var i = 0; i < max; i++) { + var e1 = i > a1.length - 1 ? SimpleTest.DNE : a1[i]; + var e2 = i > a2.length - 1 ? SimpleTest.DNE : a2[i]; + stack.push({ type: 'Array', idx: i, vals: [e1, e2] }); + if (ok = SimpleTest._deepCheck(e1, e2, stack, seen)) { + stack.pop(); + } else { + break; + } + } + return ok; +}; + +SimpleTest._eqAssoc = function (o1, o2, stack, seen) { + // Return if they're the same object. + if (o1 == o2) return true; + + // JavaScript objects have no unique identifiers, so we have to store + // references to them all in an array, and then compare the references + // directly. It's slow, but probably won't be much of an issue in + // practice. Start by making a local copy of the array to as to avoid + // confusing a reference seen more than once (such as [a, a]) for a + // circular reference. + seen = seen.slice(0); + for (var j = 0; j < seen.length; j++) { + if (seen[j][0] == o1) { + return seen[j][1] == o2; + } + } + + // If we get here, we haven't seen o1 before, so store it with reference + // to o2. + seen.push([ o1, o2 ]); + + // They should be of the same class. + + var ok = true; + // Only examines enumerable attributes. + var o1Size = 0; for (var i in o1) o1Size++; + var o2Size = 0; for (var i in o2) o2Size++; + var bigger = o1Size > o2Size ? o1 : o2; + for (var i in bigger) { + var e1 = o1[i] == undefined ? SimpleTest.DNE : o1[i]; + var e2 = o2[i] == undefined ? SimpleTest.DNE : o2[i]; + stack.push({ type: 'Object', idx: i, vals: [e1, e2] }); + if (ok = SimpleTest._deepCheck(e1, e2, stack, seen)) { + stack.pop(); + } else { + break; + } + } + return ok; +}; + +SimpleTest._formatStack = function (stack) { + var variable = '$Foo'; + for (var i = 0; i < stack.length; i++) { + var entry = stack[i]; + var type = entry['type']; + var idx = entry['idx']; + if (idx != null) { + if (/^\d+$/.test(idx)) { + // Numeric array index. + variable += '[' + idx + ']'; + } else { + // Associative array index. + idx = idx.replace("'", "\\'"); + variable += "['" + idx + "']"; + } + } + } + + var vals = stack[stack.length-1]['vals'].slice(0, 2); + var vars = [ + variable.replace('$Foo', 'got'), + variable.replace('$Foo', 'expected') + ]; + + var out = "Structures begin differing at:" + SimpleTest.LF; + for (var i = 0; i < vals.length; i++) { + var val = vals[i]; + if (val == null) { + val = 'undefined'; + } else { + val == SimpleTest.DNE ? "Does not exist" : "'" + val + "'"; + } + } + + out += vars[0] + ' = ' + vals[0] + SimpleTest.LF; + out += vars[1] + ' = ' + vals[1] + SimpleTest.LF; + + return ' ' + out; +}; + + +SimpleTest.isDeeply = function (it, as, name) { + var ok; + // ^ is the XOR operator. + if (SimpleTest._isRef(it) ^ SimpleTest._isRef(as)) { + // One's a reference, one isn't. + ok = false; + } else if (!SimpleTest._isRef(it) && !SimpleTest._isRef(as)) { + // Neither is an object. + ok = SimpleTest.is(it, as, name); + } else { + // We have two objects. Do a deep comparison. + var stack = [], seen = []; + if ( SimpleTest._deepCheck(it, as, stack, seen)) { + ok = SimpleTest.ok(true, name); + } else { + ok = SimpleTest.ok(false, name, SimpleTest._formatStack(stack)); + } + } + return ok; +}; + +SimpleTest.typeOf = function (object) { + var c = Object.prototype.toString.apply(object); + var name = c.substring(8, c.length - 1); + if (name != 'Object') return name; + // It may be a non-core class. Try to extract the class name from + // the constructor function. This may not work in all implementations. + if (/function ([^(\s]+)/.test(Function.toString.call(object.constructor))) { + return RegExp.$1; + } + // No idea. :-( + return name; +}; + +SimpleTest.isa = function (object, clas) { + return SimpleTest.typeOf(object) == clas; +}; + +if ( parent.SimpleTest && parent.runAJAXTest ) (function(){ + var oldRecord = SimpleTest.record; + + SimpleTest.record = function(condition, name, diag, stack) { + parent.SimpleTest.record( condition, name, diag, stack ); + return oldRecord( condition, name, diag, stack ); + }; + + var oldFinish = SimpleTest.finish; + + SimpleTest.finish = function() { + oldFinish(); + parent.runAJAXTest(); + }; +})(); + +// Global symbols: +var ok = SimpleTest.ok; +var record = SimpleTest.record; +var is = SimpleTest.is; +var isnot = SimpleTest.isnot; +var todo = SimpleTest.todo; +var todo_is = SimpleTest.todo_is; +var todo_isnot = SimpleTest.todo_isnot; +var isDeeply = SimpleTest.isDeeply; +var oldOnError = window.onerror; +window.onerror = function (ev) { + is(0, 1, "Error thrown during test: " + ev); + if (oldOnError) { + try { + oldOnError(ev); + } catch (e) { + } + } + if (SimpleTest._stopOnLoad == false) { + // Need to finish() manually here + SimpleTest.finish(); + } +} diff --git a/dom/tests/mochitest/ajax/mochikit/tests/SimpleTest/TestRunner.js b/dom/tests/mochitest/ajax/mochikit/tests/SimpleTest/TestRunner.js new file mode 100644 index 0000000000..a4e3130759 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/SimpleTest/TestRunner.js @@ -0,0 +1,177 @@ +/** + * TestRunner: A test runner for SimpleTest + * TODO: + * + * * Avoid moving iframes: That causes reloads on mozilla and opera. + * + * +**/ +var TestRunner = {}; +TestRunner.logEnabled = false; +TestRunner._iframes = {}; +TestRunner._iframeDocuments = {}; +TestRunner._iframeRows = {}; +TestRunner._currentTest = 0; +TestRunner._urls = []; +TestRunner._testsDiv = DIV(); +TestRunner._progressDiv = DIV(); +TestRunner._summaryDiv = DIV(null, + H1(null, "Tests Summary"), + TABLE(null, + THEAD(null, + TR(null, + TH(null, "Test"), + TH(null, "Passed"), + TH(null, "Failed") + ) + ), + TBODY() + ) +); + +/** + * This function is called after generating the summary. +**/ +TestRunner.onComplete = null; + +/** + * If logEnabled is true, this is the logger that will be used. +**/ +TestRunner.logger = MochiKit.Logging.logger; + +/** + * Toggle element visibility +**/ +TestRunner._toggle = function(el) { + if (el.className == "noshow") { + el.className = ""; + el.style.cssText = ""; + } else { + el.className = "noshow"; + el.style.cssText = "width:0px; height:0px; border:0px;"; + } +}; + + +/** + * Creates the iframe that contains a test +**/ +TestRunner._makeIframe = function (url) { + var iframe = document.createElement('iframe'); + iframe.src = url; + iframe.name = url; + iframe.width = "500"; + var tbody = TestRunner._summaryDiv.getElementsByTagName("tbody")[0]; + var tr = TR(null, TD({'colspan': '3'}, iframe)); + iframe._row = tr; + tbody.appendChild(tr); + return iframe; +}; + +/** + * TestRunner entry point. + * + * The arguments are the URLs of the test to be ran. + * +**/ +TestRunner.runTests = function (/*url...*/) { + if (TestRunner.logEnabled) + TestRunner.logger.log("SimpleTest START"); + + var body = document.getElementsByTagName("body")[0]; + appendChildNodes(body, + TestRunner._testsDiv, + TestRunner._progressDiv, + TestRunner._summaryDiv + ); + for (var i = 0; i < arguments.length; i++) { + TestRunner._urls.push(arguments[i]); + } + TestRunner.runNextTest(); +}; + +/** + * Run the next test. If no test remains, calls makeSummary +**/ +TestRunner.runNextTest = function() { + if (TestRunner._currentTest < TestRunner._urls.length) { + var url = TestRunner._urls[TestRunner._currentTest]; + var progress = SPAN(null, + "Running ", A({href:url}, url), "..." + ); + + if (TestRunner.logEnabled) + TestRunner.logger.log(scrapeText(progress)); + + TestRunner._progressDiv.appendChild(progress); + TestRunner._iframes[url] = TestRunner._makeIframe(url); + } else { + TestRunner.makeSummary(); + if (TestRunner.onComplete) + TestRunner.onComplete(); + } +}; + +/** + * This stub is called by SimpleTest when a test is finished. +**/ +TestRunner.testFinished = function (doc) { + appendChildNodes(TestRunner._progressDiv, SPAN(null, "Done"), BR()); + var finishedURL = TestRunner._urls[TestRunner._currentTest]; + + if (TestRunner.logEnabled) + TestRunner.logger.debug("SimpleTest finished " + finishedURL); + + TestRunner._iframeDocuments[finishedURL] = doc; + // TestRunner._iframes[finishedURL].style.display = "none"; + TestRunner._toggle(TestRunner._iframes[finishedURL]); + TestRunner._currentTest++; + TestRunner.runNextTest(); +}; + +/** + * Display the summary in the browser +**/ +TestRunner.makeSummary = function() { + if (TestRunner.logEnabled) + TestRunner.logger.log("SimpleTest FINISHED"); + var rows = []; + for (var url in TestRunner._iframeDocuments) { + var doc = TestRunner._iframeDocuments[url]; + var nOK = withDocument(doc, + partial(getElementsByTagAndClassName, 'div', 'test_ok') + ).length; + var nNotOK = withDocument(doc, + partial(getElementsByTagAndClassName, 'div', 'test_not_ok') + ).length; + var toggle = partial(TestRunner._toggle, TestRunner._iframes[url]); + var jsurl = "TestRunner._toggle(TestRunner._iframes['" + url + "'])"; + var row = TR( + {'style': {'backgroundColor': nNotOK > 0 ? "#f00":"#0f0"}}, + TD(null, url), + TD(null, nOK), + TD(null, nNotOK) + ); + row.onclick = toggle; + var tbody = TestRunner._summaryDiv.getElementsByTagName("tbody")[0]; + tbody.insertBefore(row, TestRunner._iframes[url]._row) + } +}; + +if ( parent.SimpleTest && parent.runAJAXTest ) { + TestRunner.makeSummary = function() { + for (var url in TestRunner._iframeDocuments) { + var doc = TestRunner._iframeDocuments[url]; + + var OK = withDocument(doc, partial(getElementsByTagAndClassName, 'div', 'test_ok')); + for ( var i = 0; i < OK.length; i++ ) + parent.SimpleTest.ok( true, OK[i].innerHTML ); + + var NotOK = withDocument(doc, partial(getElementsByTagAndClassName, 'div', 'test_not_ok')); + for ( var i = 0; i < NotOK.length; i++ ) + parent.SimpleTest.ok( false, NotOK[i].innerHTML ); + } + + parent.runAJAXTest(); + }; +} diff --git a/dom/tests/mochitest/ajax/mochikit/tests/SimpleTest/test.css b/dom/tests/mochitest/ajax/mochikit/tests/SimpleTest/test.css new file mode 100644 index 0000000000..38a401402f --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/SimpleTest/test.css @@ -0,0 +1,28 @@ +.test_ok { + color: green; + display: none; +} +.test_not_ok { + color: red; + display: block; +} + +.test_ok, .test_not_ok { + border-bottom-width: 2px; + border-bottom-style: solid; + border-bottom-color: black; +} + +.all_pass { + background-color: lime; +} + +.some_fail { + background-color: red; +} + +.tests_report { + border-width: 2px; + border-style: solid; + width: 20em; +} diff --git a/dom/tests/mochitest/ajax/mochikit/tests/cli.js b/dom/tests/mochitest/ajax/mochikit/tests/cli.js new file mode 100644 index 0000000000..18434604c8 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/cli.js @@ -0,0 +1,6 @@ +MochiKit = {__export__: true}; +load('tests/FakeJSAN.js') +JSAN.use('MochiKit.MockDOM'); +var window = this; +var document = MochiKit.MockDOM.document; +JSAN.use('MochiKit.MochiKit'); diff --git a/dom/tests/mochitest/ajax/mochikit/tests/index.html b/dom/tests/mochitest/ajax/mochikit/tests/index.html new file mode 100644 index 0000000000..da46bd32ce --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/index.html @@ -0,0 +1,25 @@ +<html> +<head> + <script type="text/javascript" src="../MochiKit/MochiKit.js"></script> + <script type="text/javascript" src="SimpleTest/TestRunner.js"></script> +</head> +<body> +<script type="text/javascript"> +TestRunner.runTests( + 'MochiKit-Async.html', + 'MochiKit-Base.html', + 'MochiKit-DateTime.html', + 'MochiKit-DOM.html', + 'MochiKit-Style.html', + 'MochiKit-Format.html', + 'MochiKit-Iter.html', + 'MochiKit-Logging.html', + 'MochiKit-MochiKit.html', + 'MochiKit-Color.html', + 'MochiKit-Selector.html', + 'MochiKit-Signal.html', + 'MochiKit-Visual.html' +); +</script> +</body> +</html> diff --git a/dom/tests/mochitest/ajax/mochikit/tests/standalone.js b/dom/tests/mochitest/ajax/mochikit/tests/standalone.js new file mode 100644 index 0000000000..d48571238f --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/standalone.js @@ -0,0 +1,16 @@ +load('tests/cli.js'); + +JSAN.use('MochiKit.Test'); + +print("[[ MochiKit.Base ]]"); +runTests('tests.test_Base'); +print("[[ MochiKit.Color ]]"); +runTests('tests.test_Color'); +print("[[ MochiKit.DateTime ]]"); +runTests('tests.test_DateTime'); +print("[[ MochiKit.Format ]]"); +runTests('tests.test_Format'); +print("[[ MochiKit.Iter ]]"); +runTests('tests.test_Iter'); +print("[[ MochiKit.Logging ]]"); +runTests('tests.test_Logging'); diff --git a/dom/tests/mochitest/ajax/mochikit/tests/test_Base.js b/dom/tests/mochitest/ajax/mochikit/tests/test_Base.js new file mode 100644 index 0000000000..2cbf2848d1 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/test_Base.js @@ -0,0 +1,509 @@ +if (typeof(dojo) != 'undefined') { dojo.require('MochiKit.Base'); } +if (typeof(JSAN) != 'undefined') { JSAN.use('MochiKit.Base'); } +if (typeof(tests) == 'undefined') { tests = {}; } + +tests.test_Base = function (t) { + // test bind + var not_self = {"toString": function () { return "not self"; } }; + var self = {"toString": function () { return "self"; } }; + var func = function (arg) { return this.toString() + " " + arg; }; + var boundFunc = bind(func, self); + not_self.boundFunc = boundFunc; + + t.is( isEmpty([], [], ""), true, "isEmpty true" ) + t.is( isEmpty([], [1], ""), true, "isEmpty true" ) + t.is( isNotEmpty([], [], ""), false, "isNotEmpty false" ) + t.is( isNotEmpty([], [1], ""), false, "isNotEmpty false" ) + + t.is( isEmpty([1], [1], "1"), false, "isEmpty false" ) + t.is( isEmpty([1], [1], "1"), false, "isEmpty false" ) + t.is( isNotEmpty([1], [1], "1"), true, "isNotEmpty true" ) + t.is( isNotEmpty([1], [1], "1"), true, "isNotEmpty true" ) + + t.is( boundFunc("foo"), "self foo", "boundFunc bound to self properly" ); + t.is( not_self.boundFunc("foo"), "self foo", "boundFunc bound to self on another obj" ); + t.is( bind(boundFunc, not_self)("foo"), "not self foo", "boundFunc successfully rebound!" ); + t.is( bind(boundFunc, undefined, "foo")(), "self foo", "boundFunc partial no self change" ); + t.is( bind(boundFunc, not_self, "foo")(), "not self foo", "boundFunc partial self change" ); + + // test method + not_self = {"toString": function () { return "not self"; } }; + self = {"toString": function () { return "self"; } }; + func = function (arg) { return this.toString() + " " + arg; }; + var boundMethod = method(self, func); + not_self.boundMethod = boundMethod; + + t.is( boundMethod("foo"), "self foo", "boundMethod bound to self properly" ); + t.is( not_self.boundMethod("foo"), "self foo", "boundMethod bound to self on another obj" ); + t.is( method(not_self, boundMethod)("foo"), "not self foo", "boundMethod successfully rebound!" ); + t.is( method(undefined, boundMethod, "foo")(), "self foo", "boundMethod partial no self change" ); + t.is( method(not_self, boundMethod, "foo")(), "not self foo", "boundMethod partial self change" ); + + + + + // test bindMethods + + var O = function (value) { + bindMethods(this); + this.value = value; + }; + O.prototype.func = function () { + return this.value; + }; + + var o = new O("boring"); + var p = {}; + p.func = o.func; + var func = o.func; + t.is( o.func(), "boring", "bindMethods doesn't break shit" ); + t.is( p.func(), "boring", "bindMethods works on other objects" ); + t.is( func(), "boring", "bindMethods works on functions" ); + + var p = clone(o); + t.ok( p instanceof O, "cloned correct inheritance" ); + var q = clone(p); + t.ok( q instanceof O, "clone-cloned correct inheritance" ); + q.foo = "bar"; + t.is( p.foo, undefined, "clone-clone is copy-on-write" ); + p.bar = "foo"; + t.is( o.bar, undefined, "clone is copy-on-write" ); + t.is( q.bar, "foo", "clone-clone has proper delegation" ); + // unbind + p.func = bind(p.func, null); + t.is( p.func(), "boring", "clone function calls correct" ); + q.value = "awesome"; + t.is( q.func(), "awesome", "clone really does work" ); + + // test boring boolean funcs + + t.is( isCallable(isCallable), true, "isCallable returns true on itself" ); + t.is( isCallable(1), false, "isCallable returns false on numbers" ); + + t.is( isUndefined(null), false, "null is not undefined" ); + t.is( isUndefined(""), false, "empty string is not undefined" ); + t.is( isUndefined(undefined), true, "undefined is undefined" ); + t.is( isUndefined({}.foo), true, "missing property is undefined" ); + + t.is( isUndefinedOrNull(null), true, "null is undefined or null" ); + t.is( isUndefinedOrNull(""), false, "empty string is not undefined or null" ); + t.is( isUndefinedOrNull(undefined), true, "undefined is undefined or null" ); + t.is( isUndefinedOrNull({}.foo), true, "missing property is undefined or null" ); + + // test extension of arrays + var a = []; + var b = []; + var three = [1, 2, 3]; + + extend(a, three, 1); + t.ok( objEqual(a, [2, 3]), "extend to an empty array" ); + extend(a, three, 1) + t.ok( objEqual(a, [2, 3, 2, 3]), "extend to a non-empty array" ); + + extend(b, three); + t.ok( objEqual(b, three), "extend of an empty array" ); + + t.is( compare(1, 2), -1, "numbers compare lt" ); + t.is( compare(2, 1), 1, "numbers compare gt" ); + t.is( compare(1, 1), 0, "numbers compare eq" ); + t.is( compare([1], [1]), 0, "arrays compare eq" ); + t.is( compare([1], [1, 2]), -1, "arrays compare lt (length)" ); + t.is( compare([1, 2], [2, 1]), -1, "arrays compare lt (contents)" ); + t.is( compare([1, 2], [1]), 1, "arrays compare gt (length)" ); + t.is( compare([2, 1], [1, 1]), 1, "arrays compare gt (contents)" ); + + // test partial application + var a = []; + var func = function (a, b) { + if (arguments.length != 2) { + return "bad args"; + } else { + return this.value + a + b; + } + }; + var self = {"value": 1, "func": func}; + var self2 = {"value": 2}; + t.is( self.func(2, 3), 6, "setup for test is correct" ); + self.funcTwo = partial(self.func, 2); + t.is( self.funcTwo(3), 6, "partial application works" ); + t.is( self.funcTwo(3), 6, "partial application works still" ); + t.is( bind(self.funcTwo, self2)(3), 7, "rebinding partial works" ); + self.funcTwo = bind(bind(self.funcTwo, self2), null); + t.is( self.funcTwo(3), 6, "re-unbinding partial application works" ); + + + // nodeWalk test + // ... looks a lot like a DOM tree on purpose + var tree = { + "id": "nodeWalkTestTree", + "test:int": "1", + "childNodes": [ + { + "test:int": "2", + "childNodes": [ + {"test:int": "5"}, + "ignored string", + {"ignored": "object"}, + ["ignored", "list"], + { + "test:skipchildren": "1", + "childNodes": [{"test:int": 6}] + } + ] + }, + {"test:int": "3"}, + {"test:int": "4"} + ] + } + + var visitedNodes = []; + nodeWalk(tree, function (node) { + var attr = node["test:int"]; + if (attr) { + visitedNodes.push(attr); + } + if (node["test:skipchildren"]) { + return; + } + return node.childNodes; + }); + + t.ok( objEqual(visitedNodes, ["1", "2", "3", "4", "5"]), "nodeWalk looks like it works"); + + // test map + var minusOne = function (x) { return x - 1; }; + var res = map(minusOne, [1, 2, 3]); + t.ok( objEqual(res, [0, 1, 2]), "map works" ); + + var res2 = xmap(minusOne, 1, 2, 3); + t.ok( objEqual(res2, res), "xmap works" ); + + res = map(operator.add, [1, 2, 3], [2, 4, 6]); + t.ok( objEqual(res, [3, 6, 9]), "map(fn, p, q) works" ); + + res = map(operator.add, [1, 2, 3], [2, 4, 6, 8]); + t.ok( objEqual(res, [3, 6, 9]), "map(fn, p, q) works (q long)" ); + + res = map(operator.add, [1, 2, 3, 4], [2, 4, 6]); + t.ok( objEqual(res, [3, 6, 9]), "map(fn, p, q) works (p long)" ); + + res = map(null, [1, 2, 3], [2, 4, 6]); + t.ok( objEqual(res, [[1, 2], [2, 4], [3, 6]]), "map(null, p, q) works" ); + + res = zip([1, 2, 3], [2, 4, 6]); + t.ok( objEqual(res, [[1, 2], [2, 4], [3, 6]]), "zip(p, q) works" ); + + res = map(null, [1, 2, 3]); + t.ok( objEqual(res, [1, 2, 3]), "map(null, lst) works" ); + + + + + t.is( isNotEmpty("foo"), true, "3 char string is not empty" ); + t.is( isNotEmpty(""), false, "0 char string is empty" ); + t.is( isNotEmpty([1, 2, 3]), true, "3 element list is not empty" ); + t.is( isNotEmpty([]), false, "0 element list is empty" ); + + // test filter + var greaterThanThis = function (x) { return x > this; }; + var greaterThanOne = function (x) { return x > 1; }; + var res = filter(greaterThanOne, [-1, 0, 1, 2, 3]); + t.ok( objEqual(res, [2, 3]), "filter works" ); + var res = filter(greaterThanThis, [-1, 0, 1, 2, 3], 1); + t.ok( objEqual(res, [2, 3]), "filter self works" ); + var res2 = xfilter(greaterThanOne, -1, 0, 1, 2, 3); + t.ok( objEqual(res2, res), "xfilter works" ); + + t.is(objMax(1, 2, 9, 12, 42, -16, 16), 42, "objMax works (with numbers)"); + t.is(objMin(1, 2, 9, 12, 42, -16, 16), -16, "objMin works (with numbers)"); + + // test adapter registry + + var R = new AdapterRegistry(); + R.register("callable", isCallable, function () { return "callable"; }); + R.register("arrayLike", isArrayLike, function () { return "arrayLike"; }); + t.is( R.match(function () {}), "callable", "registry found callable" ); + t.is( R.match([]), "arrayLike", "registry found ArrayLike" ); + try { + R.match(null); + t.ok( false, "non-matching didn't raise!" ); + } catch (e) { + t.is( e, NotFound, "non-matching raised correctly" ); + } + R.register("undefinedOrNull", isUndefinedOrNull, function () { return "undefinedOrNull" }); + R.register("undefined", isUndefined, function () { return "undefined" }); + t.is( R.match(undefined), "undefinedOrNull", "priorities are as documented" ); + t.ok( R.unregister("undefinedOrNull"), "removed adapter" ); + t.is( R.match(undefined), "undefined", "adapter was removed" ); + R.register("undefinedOrNull", isUndefinedOrNull, function () { return "undefinedOrNull" }, true); + t.is( R.match(undefined), "undefinedOrNull", "override works" ); + + var a1 = {"a": 1, "b": 2, "c": 2}; + var a2 = {"a": 2, "b": 1, "c": 2}; + t.is( keyComparator("a")(a1, a2), -1, "keyComparator 1 lt" ); + t.is( keyComparator("c")(a1, a2), 0, "keyComparator 1 eq" ); + t.is( keyComparator("c", "b")(a1, a2), 1, "keyComparator 2 eq gt" ); + t.is( keyComparator("c", "a")(a1, a2), -1, "keyComparator 2 eq lt" ); + t.is( reverseKeyComparator("a")(a1, a2), 1, "reverseKeyComparator" ); + t.is( compare(concat([1], [2], [3]), [1, 2, 3]), 0, "concat" ); + t.is( repr("foo"), '"foo"', "string repr" ); + t.is( repr(1), '1', "number repr" ); + t.is( listMin([1, 3, 5, 3, -1]), -1, "listMin" ); + t.is( objMin(1, 3, 5, 3, -1), -1, "objMin" ); + t.is( listMax([1, 3, 5, 3, -1]), 5, "listMax" ); + t.is( objMax(1, 3, 5, 3, -1), 5, "objMax" ); + + var v = keys(a1); + v.sort(); + t.is( compare(v, ["a", "b", "c"]), 0, "keys" ); + v = items(a1); + v.sort(); + t.is( compare(v, [["a", 1], ["b", 2], ["c", 2]]), 0, "items" ); + + var StringMap = function() {}; + a = new StringMap(); + a.foo = "bar"; + b = new StringMap(); + b.foo = "bar"; + try { + compare(a, b); + t.ok( false, "bad comparison registered!?" ); + } catch (e) { + t.ok( e instanceof TypeError, "bad comparison raised TypeError" ); + } + + t.is( repr(a), "[object Object]", "default repr for StringMap" ); + var isStringMap = function () { + for (var i = 0; i < arguments.length; i++) { + if (!(arguments[i] instanceof StringMap)) { + return false; + } + } + return true; + }; + + registerRepr("stringMap", + isStringMap, + function (obj) { + return "StringMap(" + repr(items(obj)) + ")"; + } + ); + + t.is( repr(a), 'StringMap([["foo", "bar"]])', "repr worked" ); + + // not public API + MochiKit.Base.reprRegistry.unregister("stringMap"); + + t.is( repr(a), "[object Object]", "default repr for StringMap" ); + + registerComparator("stringMap", + isStringMap, + function (a, b) { + // no sorted(...) in base + a = items(a); + b = items(b); + a.sort(compare); + b.sort(compare); + return compare(a, b); + } + ); + + t.is( compare(a, b), 0, "registerComparator" ); + + update(a, {"foo": "bar"}, {"wibble": "baz"}, undefined, null, {"grr": 1}); + t.is( a.foo, "bar", "update worked (first obj)" ); + t.is( a.wibble, "baz", "update worked (second obj)" ); + t.is( a.grr, 1, "update worked (skipped undefined and null)" ); + t.is( compare(a, b), 1, "update worked (comparison)" ); + + + setdefault(a, {"foo": "unf"}, {"bar": "web taco"} ); + t.is( a.foo, "bar", "setdefault worked (skipped existing)" ); + t.is( a.bar, "web taco", "setdefault worked (set non-existing)" ); + + var c = items(merge({"foo": "bar"}, {"wibble": "baz"})); + c.sort(compare); + t.is( compare(c, [["foo", "bar"], ["wibble", "baz"]]), 0, "merge worked" ); + + // not public API + MochiKit.Base.comparatorRegistry.unregister("stringMap"); + + try { + compare(a, b); + t.ok( false, "bad comparison registered!?" ); + } catch (e) { + t.ok( e instanceof TypeError, "bad comparison raised TypeError" ); + } + + var o = {"__repr__": function () { return "__repr__"; }}; + t.is( repr(o), "__repr__", "__repr__ protocol" ); + t.is( repr(MochiKit.Base), MochiKit.Base.__repr__(), "__repr__ protocol when repr is defined" ); + var o = {"NAME": "NAME"}; + t.is( repr(o), "NAME", "NAME protocol (obj)" ); + o = function () { return "TACO" }; + o.NAME = "NAME"; + t.is( repr(o), "NAME", "NAME protocol (func)" ); + + t.is( repr(MochiKit.Base.nameFunctions), "MochiKit.Base.nameFunctions", "test nameFunctions" ); + // Done! + + t.is( urlEncode("1+2=2").toUpperCase(), "1%2B2%3D2", "urlEncode" ); + t.is( queryString(["a", "b"], [1, "two"]), "a=1&b=two", "queryString"); + t.is( queryString({"a": 1}), "a=1", "one item alternate form queryString" ); + var o = {"a": 1, "b": 2, "c": function() {}}; + var res = queryString(o).split("&"); + res.sort(); + t.is( res.join("&"), "a=1&b=2", "two item alternate form queryString, function skip" ); + var res = parseQueryString("1+1=2&b=3%3D2"); + t.is( res["1 1"], "2", "parseQueryString pathological name" ); + t.is( res.b, "3=2", "parseQueryString second name:value pair" ); + var res = parseQueryString("foo=one&foo=two", true); + t.is( res["foo"].join(" "), "one two", "parseQueryString useArrays" ); + var res = parseQueryString("?foo=2&bar=1"); + t.is( res["foo"], "2", "parseQueryString strip leading question mark"); + + t.is( serializeJSON("foo\n\r\b\f\t"), "\"foo\\n\\r\\b\\f\\t\"", "string JSON" ); + t.is( serializeJSON(null), "null", "null JSON"); + try { + serializeJSON(undefined); + t.ok(false, "undefined should not be serializable"); + } catch (e) { + t.ok(e instanceof TypeError, "undefined not serializable"); + } + t.is( serializeJSON(1), "1", "1 JSON"); + t.is( serializeJSON(1.23), "1.23", "1.23 JSON"); + t.is( serializeJSON(serializeJSON), null, "function JSON (null, not string)" ); + t.is( serializeJSON([1, "2", 3.3]), "[1, \"2\", 3.3]", "array JSON" ); + var res = evalJSON(serializeJSON({"a":1, "b":2})); + t.is( res.a, 1, "evalJSON on an object (1)" ); + t.is( res.b, 2, "evalJSON on an object (2)" ); + var res = {"a": 1, "b": 2, "json": function () { return this; }}; + var res = evalJSON(serializeJSON(res)); + t.is( res.a, 1, "evalJSON on an object that jsons self (1)" ); + t.is( res.b, 2, "evalJSON on an object that jsons self (2)" ); + var strJSON = {"a": 1, "b": 2, "json": function () { return "json"; }}; + t.is( serializeJSON(strJSON), "\"json\"", "json serialization calling" ); + t.is( serializeJSON([strJSON]), "[\"json\"]", "json serialization calling in a structure" ); + t.is( evalJSON('/* {"result": 1} */').result, 1, "json comment stripping" ); + t.is( evalJSON('/* {"*/ /*": 1} */')['*/ /*'], 1, "json comment stripping" ); + registerJSON("isDateLike", + isDateLike, + function (d) { + return "this was a date"; + } + ); + t.is( serializeJSON(new Date()), "\"this was a date\"", "json registry" ); + MochiKit.Base.jsonRegistry.unregister("isDateLike"); + + var a = {"foo": {"bar": 12, "wibble": 13}}; + var b = {"foo": {"baz": 4, "bar": 16}, "bar": 4}; + updatetree(a, b); + var expect = [["bar", 16], ["baz", 4], ["wibble", 13]]; + var got = items(a.foo); + got.sort(compare); + t.is( repr(got), repr(expect), "updatetree merge" ); + t.is( a.bar, 4, "updatetree insert" ); + + var c = counter(); + t.is( c(), 1, "counter starts at 1" ); + t.is( c(), 2, "counter increases" ); + c = counter(2); + t.is( c(), 2, "counter starts at 2" ); + t.is( c(), 3, "counter increases" ); + + t.is( findValue([1, 2, 3], 4), -1, "findValue returns -1 on not found"); + t.is( findValue([1, 2, 3], 1), 0, "findValue returns correct index"); + t.is( findValue([1, 2, 3], 1, 1), -1, "findValue honors start"); + t.is( findValue([1, 2, 3], 2, 0, 1), -1, "findValue honors end"); + t.is( findIdentical([1, 2, 3], 4), -1, "findIdentical returns -1"); + t.is( findIdentical([1, 2, 3], 1), 0, "findIdentical returns correct index"); + t.is( findIdentical([1, 2, 3], 1, 1), -1, "findIdentical honors start"); + t.is( findIdentical([1, 2, 3], 2, 0, 1), -1, "findIdentical honors end"); + t.is( isNull(undefined), false, "isNull doesn't match undefined" ); + + var flat = flattenArguments(1, "2", 3, [4, [5, [6, 7], 8, [], 9]]); + var expect = [1, "2", 3, 4, 5, 6, 7, 8, 9]; + t.is( repr(flat), repr(expect), "flattenArguments" ); + + var fn = function () { + return [this, concat(arguments)]; + } + t.is( methodcaller("toLowerCase")("FOO"), "foo", "methodcaller with a method name" ); + t.is( repr(methodcaller(fn, 2, 3)(1)), "[1, [2, 3]]", "methodcaller with a function" ); + + var f1 = function (x) { return [1, x]; }; + var f2 = function (x) { return [2, x]; }; + var f3 = function (x) { return [3, x]; }; + t.is( repr(f1(f2(f3(4)))), "[1, [2, [3, 4]]]", "test the compose test" ); + t.is( repr(compose(f1,f2,f3)(4)), "[1, [2, [3, 4]]]", "three fn composition works" ); + t.is( repr(compose(compose(f1,f2),f3)(4)), "[1, [2, [3, 4]]]", "associative left" ); + t.is( repr(compose(f1,compose(f2,f3))(4)), "[1, [2, [3, 4]]]", "associative right" ); + + try { + compose(f1, "foo"); + t.ok( false, "wrong compose argument not raised!" ); + } catch (e) { + t.is( e.name, 'TypeError', "wrong compose argument raised correctly" ); + } + + t.is(camelize('one'), 'one', 'one word'); + t.is(camelize('one-two'), 'oneTwo', 'two words'); + t.is(camelize('one-two-three'), 'oneTwoThree', 'three words'); + t.is(camelize('1-one'), '1One', 'letter and word'); + t.is(camelize('one-'), 'one', 'trailing hyphen'); + t.is(camelize('-one'), 'One', 'starting hyphen'); + t.is(camelize('o-two'), 'oTwo', 'one character and word'); + + var flat = flattenArray([1, "2", 3, [4, [5, [6, 7], 8, [], 9]]]); + var expect = [1, "2", 3, 4, 5, 6, 7, 8, 9]; + t.is( repr(flat), repr(expect), "flattenArray" ); + + /* mean */ + try { + mean(); + t.ok( false, "no arguments didn't raise!" ); + } catch (e) { + t.is( e.name, 'TypeError', "no arguments raised correctly" ); + } + t.is( mean(1), 1, 'single argument (arg list)'); + t.is( mean([1]), 1, 'single argument (array)'); + t.is( mean(1,2,3), 2, 'three arguments (arg list)'); + t.is( mean([1,2,3]), 2, 'three arguments (array)'); + t.is( average(1), 1, 'test the average alias'); + + /* median */ + try { + median(); + t.ok( false, "no arguments didn't raise!" ); + } catch (e) { + t.is( e.name, 'TypeError', "no arguments raised correctly" ); + } + t.is( median(1), 1, 'single argument (arg list)'); + t.is( median([1]), 1, 'single argument (array)'); + t.is( median(3,1,2), 2, 'three arguments (arg list)'); + t.is( median([3,1,2]), 2, 'three arguments (array)'); + t.is( median(3,1,2,4), 2.5, 'four arguments (arg list)'); + t.is( median([3,1,2,4]), 2.5, 'four arguments (array)'); + + /* #185 */ + t.is( serializeJSON(parseQueryString("")), "{}", "parseQueryString('')" ); + t.is( serializeJSON(parseQueryString("", true)), "{}", "parseQueryString('', true)" ); + + /* #109 */ + t.is( queryString({ids: [1,2,3]}), "ids=1&ids=2&ids=3", "queryString array value" ); + t.is( queryString({ids: "123"}), "ids=123", "queryString string value" ); + + /* test values */ + var o = {a: 1, b: 2, c: 4, d: -1}; + var got = values(o); + got.sort(); + t.is( repr(got), repr([-1, 1, 2, 4]), "values()" ); + + t.is( queryString([["foo", "bar"], ["baz", "wibble"]]), "foo=baz&bar=wibble" ); + o = parseQueryString("foo=1=1=1&bar=2&baz&wibble="); + t.is( o.foo, "1=1=1", "parseQueryString multiple = first" ); + t.is( o.bar, "2", "parseQueryString multiple = second" ); + t.is( o.baz, "", "parseQueryString multiple = third" ); + t.is( o.wibble, "", "parseQueryString multiple = fourth" ); + +}; diff --git a/dom/tests/mochitest/ajax/mochikit/tests/test_Color.js b/dom/tests/mochitest/ajax/mochikit/tests/test_Color.js new file mode 100644 index 0000000000..c736c8db34 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/test_Color.js @@ -0,0 +1,137 @@ +if (typeof(dojo) != 'undefined') { dojo.require('MochiKit.Color'); } +if (typeof(JSAN) != 'undefined') { JSAN.use('MochiKit.Color'); } +if (typeof(tests) == 'undefined') { tests = {}; } + +tests.test_Color = function (t) { + var approx = function (a, b, msg) { + return t.is(a.toPrecision(4), b.toPrecision(4), msg); + }; + + t.is( Color.whiteColor().toHexString(), "#ffffff", "whiteColor has right hex" ); + t.is( Color.blackColor().toHexString(), "#000000", "blackColor has right hex" ); + t.is( Color.blueColor().toHexString(), "#0000ff", "blueColor has right hex" ); + t.is( Color.redColor().toHexString(), "#ff0000", "redColor has right hex" ); + t.is( Color.greenColor().toHexString(), "#00ff00", "greenColor has right hex" ); + t.is( compare(Color.whiteColor(), Color.whiteColor()), 0, "default colors compare right" ); + t.ok( Color.whiteColor() == Color.whiteColor(), "default colors are interned" ); + t.is( Color.whiteColor().toRGBString(), "rgb(255,255,255)", "toRGBString white" ); + t.is( Color.blueColor().toRGBString(), "rgb(0,0,255)", "toRGBString blue" ); + t.is( Color.fromRGB(190/255, 222/255, 173/255).toHexString(), "#bedead", "fromRGB works" ); + t.is( Color.fromRGB(226/255, 15.9/255, 182/255).toHexString(), "#e210b6", "fromRGB < 16 works" ); + t.is( Color.fromRGB({r:190/255,g:222/255,b:173/255}).toHexString(), "#bedead", "alt fromRGB works" ); + t.is( Color.fromHexString("#bedead").toHexString(), "#bedead", "round-trip hex" ); + t.is( Color.fromString("#bedead").toHexString(), "#bedead", "round-trip string(hex)" ); + t.is( Color.fromRGBString("rgb(190,222,173)").toHexString(), "#bedead", "round-trip rgb" ); + t.is( Color.fromString("rgb(190,222,173)").toHexString(), "#bedead", "round-trip rgb" ); + + var hsl = Color.redColor().asHSL(); + approx( hsl.h, 0.0, "red hsl.h" ); + approx( hsl.s, 1.0, "red hsl.s" ); + approx( hsl.l, 0.5, "red hsl.l" ); + hsl = Color.fromRGB(0, 0, 0.5).asHSL(); + approx( hsl.h, 2/3, "darkblue hsl.h" ); + approx( hsl.s, 1.0, "darkblue hsl.s" ); + approx( hsl.l, 0.25, "darkblue hsl.l" ); + hsl = Color.fromString("#4169E1").asHSL(); + approx( hsl.h, (5/8), "4169e1 h"); + approx( hsl.s, (8/11), "4169e1 s"); + approx( hsl.l, (29/51), "4169e1 l"); + hsl = Color.fromString("#555544").asHSL(); + approx( hsl.h, (1/6), "555544 h" ); + approx( hsl.s, (1/9), "555544 s" ); + approx( hsl.l, (3/10), "555544 l" ); + hsl = Color.fromRGB(0.5, 1, 0.5).asHSL(); + approx( hsl.h, 1/3, "aqua hsl.h" ); + approx( hsl.s, 1.0, "aqua hsl.s" ); + approx( hsl.l, 0.75, "aqua hsl.l" ); + t.is( + Color.fromHSL(hsl.h, hsl.s, hsl.l).toHexString(), + Color.fromRGB(0.5, 1, 0.5).toHexString(), + "fromHSL works with components" + ); + t.is( + Color.fromHSL(hsl).toHexString(), + Color.fromRGB(0.5, 1, 0.5).toHexString(), + "fromHSL alt form" + ); + t.is( + Color.fromString("hsl(120,100%,75%)").toHexString(), + "#80ff80", + "fromHSLString" + ); + t.is( + Color.fromRGB(0.5, 1, 0.5).toHSLString(), + "hsl(120,100.0%,75.00%)", + "toHSLString" + ); + t.is( Color.fromHSL(0, 0, 0).toHexString(), "#000000", "fromHSL to black" ); + hsl = Color.blackColor().asHSL(); + approx( hsl.h, 0.0, "black hsl.h" ); + approx( hsl.s, 0.0, "black hsl.s" ); + approx( hsl.l, 0.0, "black hsl.l" ); + hsl.h = 1.0; + hsl = Color.blackColor().asHSL(); + approx( hsl.h, 0.0, "asHSL returns copy" ); + var rgb = Color.brownColor().asRGB(); + approx( rgb.r, 153/255, "brown rgb.r" ); + approx( rgb.g, 102/255, "brown rgb.g" ); + approx( rgb.b, 51/255, "brown rgb.b" ); + rgb.r = 0; + rgb = Color.brownColor().asRGB(); + approx( rgb.r, 153/255, "asRGB returns copy" ); + + t.is( Color.fromName("aqua").toHexString(), "#00ffff", "aqua fromName" ); + t.is( Color.fromString("aqua").toHexString(), "#00ffff", "aqua fromString" ); + t.is( Color.fromName("transparent"), Color.transparentColor(), "transparent fromName" ); + t.is( Color.fromString("transparent"), Color.transparentColor(), "transparent fromString" ); + t.is( Color.transparentColor().toRGBString(), "rgba(0,0,0,0)", "transparent toRGBString" ); + t.is( Color.fromRGBString("rgba( 0, 255, 255, 50%)").asRGB().a, 0.5, "rgba parsing alpha correctly" ); + t.is( Color.fromRGBString("rgba( 0, 255, 255, 50%)").toRGBString(), "rgba(0,255,255,0.5)", "rgba output correctly" ); + t.is( Color.fromRGBString("rgba( 0, 255, 255, 1)").toHexString(), "#00ffff", "fromRGBString with spaces and alpha" ); + t.is( Color.fromRGBString("rgb( 0, 255, 255)").toHexString(), "#00ffff", "fromRGBString with spaces" ); + t.is( Color.fromRGBString("rgb( 0, 100%, 255)").toHexString(), "#00ffff", "fromRGBString with percents" ); + + var hsv = Color.redColor().asHSV(); + approx( hsv.h, 0.0, "red hsv.h" ); + approx( hsv.s, 1.0, "red hsv.s" ); + approx( hsv.v, 1.0, "red hsv.v" ); + t.is( Color.fromHSV(hsv).toHexString(), Color.redColor().toHexString(), "red hexstring" ); + hsv = Color.fromRGB(0, 0, 0.5).asHSV(); + approx( hsv.h, 2/3, "darkblue hsv.h" ); + approx( hsv.s, 1.0, "darkblue hsv.s" ); + approx( hsv.v, 0.5, "darkblue hsv.v" ); + t.is( Color.fromHSV(hsv).toHexString(), Color.fromRGB(0, 0, 0.5).toHexString(), "darkblue hexstring" ); + hsv = Color.fromString("#4169E1").asHSV(); + approx( hsv.h, 5/8, "4169e1 h"); + approx( hsv.s, 32/45, "4169e1 s"); + approx( hsv.v, 15/17, "4169e1 l"); + t.is( Color.fromHSV(hsv).toHexString(), "#4169e1", "4169e1 hexstring" ); + hsv = Color.fromString("#555544").asHSV(); + approx( hsv.h, 1/6, "555544 h" ); + approx( hsv.s, 1/5, "555544 s" ); + approx( hsv.v, 1/3, "555544 l" ); + t.is( Color.fromHSV(hsv).toHexString(), "#555544", "555544 hexstring" ); + hsv = Color.fromRGB(0.5, 1, 0.5).asHSV(); + approx( hsv.h, 1/3, "aqua hsv.h" ); + approx( hsv.s, 0.5, "aqua hsv.s" ); + approx( hsv.v, 1, "aqua hsv.v" ); + t.is( + Color.fromHSV(hsv.h, hsv.s, hsv.v).toHexString(), + Color.fromRGB(0.5, 1, 0.5).toHexString(), + "fromHSV works with components" + ); + t.is( + Color.fromHSV(hsv).toHexString(), + Color.fromRGB(0.5, 1, 0.5).toHexString(), + "fromHSV alt form" + ); + hsv = Color.fromRGB(1, 1, 1).asHSV() + approx( hsv.h, 0, 'white hsv.h' ); + approx( hsv.s, 0, 'white hsv.s' ); + approx( hsv.v, 1, 'white hsv.v' ); + t.is( + Color.fromHSV(0, 0, 1).toHexString(), + '#ffffff', + 'HSV saturation' + ); +}; diff --git a/dom/tests/mochitest/ajax/mochikit/tests/test_DateTime.js b/dom/tests/mochitest/ajax/mochikit/tests/test_DateTime.js new file mode 100644 index 0000000000..cc2d958e3c --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/test_DateTime.js @@ -0,0 +1,45 @@ +if (typeof(dojo) != 'undefined') { dojo.require('MochiKit.DateTime'); } +if (typeof(JSAN) != 'undefined') { JSAN.use('MochiKit.DateTime'); } +if (typeof(tests) == 'undefined') { tests = {}; } + +tests.test_DateTime = function (t) { + var testDate = isoDate('2005-2-3'); + t.is(testDate.getFullYear(), 2005, "isoDate year ok"); + t.is(testDate.getDate(), 3, "isoDate day ok"); + t.is(testDate.getMonth(), 1, "isoDate month ok"); + t.ok(objEqual(testDate, new Date("February 3, 2005")), "matches string date"); + t.is(toISODate(testDate), '2005-02-03', 'toISODate ok'); + + var testDate = isoDate('2005-06-08'); + t.is(testDate.getFullYear(), 2005, "isoDate year ok"); + t.is(testDate.getDate(), 8, "isoDate day ok"); + t.is(testDate.getMonth(), 5, "isoDate month ok"); + t.ok(objEqual(testDate, new Date("June 8, 2005")), "matches string date"); + t.is(toISODate(testDate), '2005-06-08', 'toISODate ok'); + + t.is(compare(new Date("February 3, 2005"), new Date(2005, 1, 3)), 0, "dates compare eq"); + t.is(compare(new Date("February 3, 2005"), new Date(2005, 2, 3)), -1, "dates compare lt"); + t.is(compare(new Date("February 3, 2005"), new Date(2005, 0, 3)), 1, "dates compare gt"); + + var testDate = isoDate('2005-2-3'); + t.is(compare(americanDate('2/3/2005'), testDate), 0, "americanDate eq"); + t.is(compare('2/3/2005', toAmericanDate(testDate)), 0, "toAmericanDate eq"); + + var testTimestamp = isoTimestamp('2005-2-3 22:01:03'); + t.is(compare(testTimestamp, new Date(2005,1,3,22,1,3)), 0, "isoTimestamp eq"); + t.is(compare(testTimestamp, isoTimestamp('2005-2-3T22:01:03')), 0, "isoTimestamp (real ISO) eq"); + t.is(compare(toISOTimestamp(testTimestamp), '2005-02-03 22:01:03'), 0, "toISOTimestamp eq"); + testTimestamp = isoTimestamp('2005-2-3T22:01:03Z'); + t.is(toISOTimestamp(testTimestamp, true), '2005-02-03T22:01:03Z', "toISOTimestamp (real ISO) eq"); + + var localTZ = Math.round((new Date(2005,1,3,22,1,3)).getTimezoneOffset()/60) + var direction = (localTZ < 0) ? "+" : "-"; + localTZ = Math.abs(localTZ); + localTZ = direction + ((localTZ < 10) ? "0" : "") + localTZ; + testTimestamp = isoTimestamp("2005-2-3T22:01:03" + localTZ); + var testDateTimestamp = new Date(2005,1,3,22,1,3); + t.is(compare(testTimestamp, testDateTimestamp), 0, "equal with local tz"); + testTimestamp = isoTimestamp("2005-2-3T17:01:03-05"); + var testDateTimestamp = new Date(Date.UTC(2005,1,3,22,1,3)); + t.is(compare(testTimestamp, testDateTimestamp), 0, "equal with specific tz"); +}; diff --git a/dom/tests/mochitest/ajax/mochikit/tests/test_DragAndDrop.js b/dom/tests/mochitest/ajax/mochikit/tests/test_DragAndDrop.js new file mode 100644 index 0000000000..d3a3c58379 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/test_DragAndDrop.js @@ -0,0 +1,30 @@ +if (typeof(dojo) != 'undefined') { dojo.require('MochiKit.Signal'); } +if (typeof(JSAN) != 'undefined') { JSAN.use('MochiKit.Signal'); } +if (typeof(tests) == 'undefined') { tests = {}; } + +tests.test_DragAndDrop = function (t) { + + var drag1 = new MochiKit.DragAndDrop.Draggable('drag1', {'revert': true, 'ghosting': true}); + + var drop1 = new MochiKit.DragAndDrop.Droppable('drop1', {'hoverclass': 'drop-hover'}); + drop1.activate(); + t.is(hasElementClass('drop1', 'drop-hover'), true, "hoverclass ok"); + drop1.deactivate(); + t.is(hasElementClass('drop1', 'drop-hover'), false, "remove hoverclass ok"); + drop1.destroy(); + + t.is( isEmpty(MochiKit.DragAndDrop.Droppables.drops), true, "Unregister droppable ok"); + + var onhover = function (element) { + t.is(element, getElement('drag1'), 'onhover ok'); + }; + var drop2 = new MochiKit.DragAndDrop.Droppable('drop1', {'onhover': onhover}); + var pos = getElementPosition('drop1'); + pos = {"x": pos.x + 5, "y": pos.y + 5}; + MochiKit.DragAndDrop.Droppables.show({"page": pos}, getElement('drag1')); + + drag1.destroy(); + t.is( isEmpty(MochiKit.DragAndDrop.Draggables.drops), true, "Unregister draggable ok"); + +}; + diff --git a/dom/tests/mochitest/ajax/mochikit/tests/test_Format.js b/dom/tests/mochitest/ajax/mochikit/tests/test_Format.js new file mode 100644 index 0000000000..785cebf823 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/test_Format.js @@ -0,0 +1,80 @@ +if (typeof(dojo) != 'undefined') { dojo.require('MochiKit.Format'); } +if (typeof(JSAN) != 'undefined') { JSAN.use('MochiKit.Format'); } +if (typeof(tests) == 'undefined') { tests = {}; } + +tests.test_Format = function (t) { + t.is( truncToFixed(0.1234, 3), "0.123", "truncToFixed truncate" ); + t.is( truncToFixed(0.12, 3), "0.120", "truncToFixed trailing zeros" ); + t.is( truncToFixed(0.15, 1), "0.1", "truncToFixed no round" ); + t.is( truncToFixed(0.15, 0), "0", "truncToFixed zero (edge case)" ); + + t.is( roundToFixed(0.1234, 3), "0.123", "roundToFixed truncate" ); + t.is( roundToFixed(0.12, 3), "0.120", "roundToFixed trailing zeros" ); + t.is( roundToFixed(0.15, 1), "0.2", "roundToFixed round" ); + t.is( roundToFixed(0.15, 0), "0", "roundToFixed zero (edge case)" ); + + t.is( twoDigitFloat(-0.1234), "-0.12", "twoDigitFloat -0.1234 correct"); + t.is( twoDigitFloat(-0.1), "-0.1", "twoDigitFloat -0.1 correct"); + t.is( twoDigitFloat(-0), "0", "twoDigitFloat -0 correct"); + t.is( twoDigitFloat(0), "0", "twoDigitFloat 0 correct"); + t.is( twoDigitFloat(1), "1", "twoDigitFloat 1 correct"); + t.is( twoDigitFloat(1.0), "1", "twoDigitFloat 1.0 correct"); + t.is( twoDigitFloat(1.2), "1.2", "twoDigitFloat 1.2 correct"); + t.is( twoDigitFloat(1.234), "1.23", "twoDigitFloat 1.234 correct"); + t.is( percentFormat(123), "12300%", "percentFormat 123 correct"); + t.is( percentFormat(1.23), "123%", "percentFormat 123 correct"); + t.is( twoDigitAverage(1, 0), "0", "twoDigitAverage dbz correct"); + t.is( twoDigitAverage(1, 1), "1", "twoDigitAverage 1 correct"); + t.is( twoDigitAverage(1, 10), "0.1", "twoDigitAverage .1 correct"); + function reprIs(a, b) { + arguments[0] = repr(a); + arguments[1] = repr(b); + t.is.apply(this, arguments); + } + reprIs( lstrip("\r\t\n foo \n\t\r"), "foo \n\t\r", "lstrip whitespace chars" ); + reprIs( rstrip("\r\t\n foo \n\t\r"), "\r\t\n foo", "rstrip whitespace chars" ); + reprIs( strip("\r\t\n foo \n\t\r"), "foo", "strip whitespace chars" ); + reprIs( lstrip("\r\n\t \r", "\r"), "\n\t \r", "lstrip custom chars" ); + reprIs( rstrip("\r\n\t \r", "\r"), "\r\n\t ", "rstrip custom chars" ); + reprIs( strip("\r\n\t \r", "\r"), "\n\t ", "strip custom chars" ); + + var nf = numberFormatter("$###,###.00 footer"); + t.is( nf(1000.1), "$1,000.10 footer", "trailing zeros" ); + t.is( nf(1000000.1), "$1,000,000.10 footer", "two seps" ); + t.is( nf(100), "$100.00 footer", "shorter than sep" ); + t.is( nf(100.555), "$100.56 footer", "rounding" ); + t.is( nf(-100.555), "$-100.56 footer", "default neg" ); + nf = numberFormatter("-$###,###.00"); + t.is( nf(-100.555), "-$100.56", "custom neg" ); + nf = numberFormatter("0000.0000"); + t.is( nf(0), "0000.0000", "leading and trailing" ); + t.is( nf(1.1), "0001.1000", "leading and trailing" ); + t.is( nf(12345.12345), "12345.1235", "no need for leading/trailing" ); + nf = numberFormatter("0000.0000"); + t.is( nf("taco"), "", "default placeholder" ); + nf = numberFormatter("###,###.00", "foo", "de_DE"); + t.is( nf("taco"), "foo", "custom placeholder" ); + t.is( nf(12345.12345), "12.345,12", "de_DE locale" ); + nf = numberFormatter("#%"); + t.is( nf(1), "100%", "trivial percent" ); + t.is( nf(0.55), "55%", "percent" ); + + var customLocale = { + separator: " apples and ", + decimal: " bagels at ", + percent: "am for breakfast"}; + var customFormatter = numberFormatter("###,###.0%", "No breakfast", customLocale); + t.is( customFormatter(23.458), "2 apples and 345 bagels at 8am for breakfast", "custom locale" ); + + nf = numberFormatter("###,###"); + t.is( nf(123), "123", "large number format" ); + t.is( nf(1234), "1,234", "large number format" ); + t.is( nf(12345), "12,345", "large number format" ); + t.is( nf(123456), "123,456", "large number format" ); + t.is( nf(1234567), "1,234,567", "large number format" ); + t.is( nf(12345678), "12,345,678", "large number format" ); + t.is( nf(123456789), "123,456,789", "large number format" ); + t.is( nf(1234567890), "1,234,567,890", "large number format" ); + t.is( nf(12345678901), "12,345,678,901", "large number format" ); + t.is( nf(123456789012), "123,456,789,012", "large number format" ); +}; diff --git a/dom/tests/mochitest/ajax/mochikit/tests/test_Iter.js b/dom/tests/mochitest/ajax/mochikit/tests/test_Iter.js new file mode 100644 index 0000000000..d0ff146ad1 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/test_Iter.js @@ -0,0 +1,176 @@ +if (typeof(dojo) != 'undefined') { dojo.require('MochiKit.Iter'); } +if (typeof(JSAN) != 'undefined') { JSAN.use('MochiKit.Iter'); } +if (typeof(tests) == 'undefined') { tests = {}; } + +tests.test_Iter = function (t) { + t.is( sum([1, 2, 3, 4, 5]), 15, "sum works on Arrays" ); + t.is( compare(list([1, 2, 3]), [1, 2, 3]), 0, "list([x]) == [x]" ); + t.is( compare(list(range(6, 0, -1)), [6, 5, 4, 3, 2, 1]), 0, "list(range(6, 0, -1)"); + t.is( compare(list(range(6)), [0, 1, 2, 3, 4, 5]), 0, "list(range(6))" ); + var moreThanTwo = partial(operator.lt, 2); + t.is( sum(ifilter(moreThanTwo, range(6))), 12, "sum(ifilter(, range()))" ); + t.is( sum(ifilterfalse(moreThanTwo, range(6))), 3, "sum(ifilterfalse(, range()))" ); + + var c = count(10); + t.is( compare([c.next(), c.next(), c.next()], [10, 11, 12]), 0, "count()" ); + c = cycle([1, 2]); + t.is( compare([c.next(), c.next(), c.next()], [1, 2, 1]), 0, "cycle()" ); + c = repeat("foo", 3); + t.is( compare(list(c), ["foo", "foo", "foo"]), 0, "repeat()" ); + c = izip([1, 2], [3, 4, 5], repeat("foo")); + t.is( compare(list(c), [[1, 3, "foo"], [2, 4, "foo"]]), 0, "izip()" ); + + t.is( compare(list(range(5)), [0, 1, 2, 3, 4]), 0, "range(x)" ); + c = islice(range(10), 0, 10, 2); + t.is( compare(list(c), [0, 2, 4, 6, 8]), 0, "islice(x, y, z)" ); + + c = imap(operator.add, [1, 2, 3], [2, 4, 6]); + t.is( compare(list(c), [3, 6, 9]), 0, "imap(fn, p, q)" ); + + c = filter(partial(operator.lt, 1), iter([1, 2, 3])); + t.is( compare(c, [2, 3]), 0, "filter(fn, iterable)" ); + + c = map(partial(operator.add, -1), iter([1, 2, 3])); + t.is( compare(c, [0, 1, 2]), 0, "map(fn, iterable)" ); + + c = map(operator.add, iter([1, 2, 3]), [2, 4, 6]); + t.is( compare(c, [3, 6, 9]), 0, "map(fn, iterable, q)" ); + + c = map(operator.add, iter([1, 2, 3]), iter([2, 4, 6])); + t.is( compare(c, [3, 6, 9]), 0, "map(fn, iterable, iterable)" ); + + c = applymap(operator.add, [[1, 2], [2, 4], [3, 6]]); + t.is( compare(list(c), [3, 6, 9]), 0, "applymap()" ); + + c = applymap(function (a) { return [this, a]; }, [[1], [2]], 1); + t.is( compare(list(c), [[1, 1], [1, 2]]), 0, "applymap(self)" ); + + c = chain(range(2), range(3)); + t.is( compare(list(c), [0, 1, 0, 1, 2]), 0, "chain(p, q)" ); + + var lessThanFive = partial(operator.gt, 5); + c = takewhile(lessThanFive, count()); + t.is( compare(list(c), [0, 1, 2, 3, 4]), 0, "takewhile()" ); + + c = dropwhile(lessThanFive, range(10)); + t.is( compare(list(c), [5, 6, 7, 8, 9]), 0, "dropwhile()" ); + + c = tee(range(5), 3); + t.is( compare(list(c[0]), list(c[1])), 0, "tee(..., 3) p0 == p1" ); + t.is( compare(list(c[2]), [0, 1, 2, 3, 4]), 0, "tee(..., 3) p2 == fixed" ); + + t.is( compare(reduce(operator.add, range(10)), 45), 0, "reduce(op.add)" ); + + try { + reduce(operator.add, []); + t.ok( false, "reduce didn't raise anything with empty list and no start?!" ); + } catch (e) { + if (e instanceof TypeError) { + t.ok( true, "reduce raised TypeError correctly" ); + } else { + t.ok( false, "reduce raised the wrong exception?" ); + } + } + + t.is( reduce(operator.add, [], 10), 10, "range initial value OK empty" ); + t.is( reduce(operator.add, [1], 10), 11, "range initial value OK populated" ); + + t.is( compare(iextend([1], range(2)), [1, 0, 1]), 0, "iextend(...)" ); + + var x = []; + exhaust(imap(bind(x.push, x), range(5))); + t.is( compare(x, [0, 1, 2, 3, 4]), 0, "exhaust(...)" ); + + t.is( every([1, 2, 3, 4, 5, 4], lessThanFive), false, "every false" ); + t.is( every([1, 2, 3, 4, 4], lessThanFive), true, "every true" ); + t.is( some([1, 2, 3, 4, 4], lessThanFive), true, "some true" ); + t.is( some([5, 6, 7, 8, 9], lessThanFive), false, "some false" ); + t.is( some([5, 6, 7, 8, 4], lessThanFive), true, "some true" ); + + var rval = []; + forEach(range(2), rval.push, rval); + t.is( compare(rval, [0, 1]), 0, "forEach works bound" ); + + function foo(o) { + rval.push(o); + } + forEach(range(2), foo); + t.is( compare(rval, [0, 1, 0, 1]), 0, "forEach works unbound" ); + + t.is( compare(sorted([3, 2, 1]), [1, 2, 3]), 0, "sorted default" ); + rval = sorted(["aaa", "bb", "c"], keyComparator("length")); + t.is(compare(rval, ["c", "bb", "aaa"]), 0, "sorted custom"); + + t.is( compare(reversed(range(4)), [3, 2, 1, 0]), 0, "reversed iterator" ); + t.is( compare(reversed([5, 6, 7]), [7, 6, 5]), 0, "reversed list" ); + + var o = {lst: [1, 2, 3], iterateNext: function () { return this.lst.shift(); }}; + t.is( compare(list(o), [1, 2, 3]), 0, "iterateNext" ); + + + function except(exc, func) { + try { + func(); + t.ok(false, exc.name + " was not raised."); + } catch (e) { + if (e == exc) { + t.ok( true, "raised " + exc.name + " correctly" ); + } else { + t.ok( false, "raised the wrong exception?" ); + } + } + } + + odd = partial(operator.and, 1) + + // empty + grouped = groupby([]); + except(StopIteration, grouped.next); + + // exhaust sub-iterator + grouped = groupby([2,4,6,7], odd); + kv = grouped.next(); k = kv[0], subiter = kv[1]; + t.is(k, 0, "odd(2) = odd(4) = odd(6) == 0"); + t.is(subiter.next(), 2, "sub-iterator.next() == 2"); + t.is(subiter.next(), 4, "sub-iterator.next() == 4"); + t.is(subiter.next(), 6, "sub-iterator.next() == 6"); + except(StopIteration, subiter.next); + kv = grouped.next(); key = kv[0], subiter = kv[1]; + t.is(key, 1, "odd(7) == 1"); + t.is(subiter.next(), 7, "sub-iterator.next() == 7"); + except(StopIteration, subiter.next); + + // not consume sub-iterator + grouped = groupby([2,4,6,7], odd); + kv = grouped.next(); key = kv[0], subiter = kv[1]; + t.is(key, 0, "0 = odd(2) = odd(4) = odd(6)"); + kv = grouped.next(); key = kv[0], subiter = kv[1]; + t.is(key, 1, "1 = odd(7)"); + except(StopIteration, grouped.next); + + // consume sub-iterator partially + grouped = groupby([3,1,1,2], odd); + kv = grouped.next(); key = kv[0], subiter = kv[1]; + t.is(key, 1, "odd(1) == 1"); + t.is(subiter.next(), 3, "sub-iterator.next() == 3"); + kv = grouped.next(); key = kv[0], v = kv[1]; + t.is(key, 0, "skip (1,1), odd(2) == 0"); + except(StopIteration, grouped.next); + + // null + grouped = groupby([null,null]); + kv = grouped.next(); k = kv[0], v = kv[1]; + t.is(k, null, "null ok"); + + // groupby - array version + isEqual = (t.isDeeply || function (a, b, msg) { + return t.ok(compare(a, b) == 0, msg); + }); + isEqual(groupby_as_array([ ] ), [ ], "empty"); + isEqual(groupby_as_array([1,1,1]), [ [1,[1,1,1]] ], "[1,1,1]: [1,1,1]"); + isEqual(groupby_as_array([1,2,2]), [ [1,[1] ], [2,[2,2]] ], "[1,2,2]: [1], [2,2]"); + isEqual(groupby_as_array([1,1,2]), [ [1,[1,1] ], [2,[2 ]] ], "[1,1,2]: [1,1], [2]"); + isEqual(groupby_as_array([null,null] ), [ [null,[null,null]] ], "[null,null]: [null,null]"); + grouped = groupby_as_array([1,1,3,2,4,6,8], odd); + isEqual(grouped, [[1, [1,1,3]], [0,[2,4,6,8]]], "[1,1,3,2,4,6,7] odd: [1,1,3], [2,4,6,8]"); +}; diff --git a/dom/tests/mochitest/ajax/mochikit/tests/test_Logging.js b/dom/tests/mochitest/ajax/mochikit/tests/test_Logging.js new file mode 100644 index 0000000000..66f4989b02 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/test_Logging.js @@ -0,0 +1,75 @@ +if (typeof(dojo) != 'undefined') { dojo.require('MochiKit.Logging'); } +if (typeof(JSAN) != 'undefined') { JSAN.use('MochiKit.Logging'); } +if (typeof(tests) == 'undefined') { tests = {}; } + +tests.test_Logging = function (t) { + + // just in case + logger.clear(); + + t.is( logLevelAtLeast('DEBUG')('INFO'), false, 'logLevelAtLeast false' ); + t.is( logLevelAtLeast('WARNING')('INFO'), false, 'logLevelAtLeast true' ); + t.ok( logger instanceof Logger, "global logger installed" ); + + var allMessages = []; + logger.addListener("allMessages", null, + bind(allMessages.push, allMessages)); + + var fatalMessages = []; + logger.addListener("fatalMessages", "FATAL", + bind(fatalMessages.push, fatalMessages)); + + var firstTwo = []; + logger.addListener("firstTwo", null, + bind(firstTwo.push, firstTwo)); + + + log("foo"); + var msgs = logger.getMessages(); + t.is( msgs.length, 1, 'global log() put one message in queue' ); + t.is( compare(allMessages, msgs), 0, "allMessages listener" ); + var msg = msgs.pop(); + t.is( compare(msg.info, ["foo"]), 0, "info matches" ); + t.is( msg.level, "INFO", "level matches" ); + + logDebug("debugFoo"); + t.is( msgs.length, 0, 'getMessages() returns copy' ); + msgs = logger.getMessages(); + t.is( compare(allMessages, msgs), 0, "allMessages listener" ); + t.is( msgs.length, 2, 'logDebug()' ); + msg = msgs.pop(); + t.is( compare(msg.info, ["debugFoo"]), 0, "info matches" ); + t.is( msg.level, "DEBUG", "level matches" ); + + logger.removeListener("firstTwo"); + + logError("errorFoo"); + msgs = logger.getMessages(); + t.is( compare(allMessages, msgs), 0, "allMessages listener" ); + t.is( msgs.length, 3, 'logError()' ); + msg = msgs.pop(); + t.is( compare(msg.info, ["errorFoo"]), 0, "info matches" ); + t.is( msg.level, "ERROR", "level matches" ); + + logWarning("warningFoo"); + msgs = logger.getMessages(); + t.is( compare(allMessages, msgs), 0, "allMessages listener" ); + t.is( msgs.length, 4, 'logWarning()' ); + msg = msgs.pop(); + t.is( compare(msg.info, ["warningFoo"]), 0, "info matches" ); + t.is( msg.level, "WARNING", "level matches" ); + + logFatal("fatalFoo"); + msgs = logger.getMessages(); + t.is( compare(allMessages, msgs), 0, "allMessages listener" ); + t.is( msgs.length, 5, 'logFatal()' ); + msg = msgs.pop(); + t.is( compare(fatalMessages, [msg]), 0, "fatalMessages listener" ); + t.is( compare(msg.info, ["fatalFoo"]), 0, "info matches" ); + t.is( msg.level, "FATAL", "level matches" ); + + logger.removeListener("allMessages"); + logger.removeListener("fatalMessages"); + + t.is( compare(firstTwo, logger.getMessages().slice(0, 2)), 0, "firstTwo" ); +}; diff --git a/dom/tests/mochitest/ajax/mochikit/tests/test_MochiKit-Async.json b/dom/tests/mochitest/ajax/mochikit/tests/test_MochiKit-Async.json new file mode 100644 index 0000000000..037e18c818 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/test_MochiKit-Async.json @@ -0,0 +1 @@ +{"passed": true} diff --git a/dom/tests/mochitest/ajax/mochikit/tests/test_Signal.js b/dom/tests/mochitest/ajax/mochikit/tests/test_Signal.js new file mode 100644 index 0000000000..6a905952f7 --- /dev/null +++ b/dom/tests/mochitest/ajax/mochikit/tests/test_Signal.js @@ -0,0 +1,441 @@ +if (typeof(dojo) != 'undefined') { dojo.require('MochiKit.Signal'); } +if (typeof(JSAN) != 'undefined') { JSAN.use('MochiKit.Signal'); } +if (typeof(tests) == 'undefined') { tests = {}; } + +tests.test_Signal = function (t) { + + var submit = MochiKit.DOM.getElement('submit'); + var ident = null; + var i = 0; + var aFunction = function() { + t.ok(this === submit, "aFunction should have 'this' as submit"); + i++; + if (typeof(this.someVar) != 'undefined') { + i += this.someVar; + } + }; + + var aObject = {}; + aObject.aMethod = function() { + t.ok(this === aObject, "aMethod should have 'this' as aObject"); + i++; + }; + + ident = connect('submit', 'onclick', aFunction); + MochiKit.DOM.getElement('submit').click(); + t.is(i, 1, 'HTML onclick event can be connected to a function'); + + disconnect(ident); + MochiKit.DOM.getElement('submit').click(); + t.is(i, 1, 'HTML onclick can be disconnected from a function'); + + var submit = MochiKit.DOM.getElement('submit'); + + ident = connect(submit, 'onclick', aFunction); + submit.click(); + t.is(i, 2, 'Checking that a DOM element can be connected to a function'); + + disconnect(ident); + submit.click(); + t.is(i, 2, '...and then disconnected'); + + if (MochiKit.DOM.getElement('submit').fireEvent || + (document.createEvent && + typeof(document.createEvent('MouseEvents').initMouseEvent) == 'function')) { + + /* + + Adapted from: + http://www.devdaily.com/java/jwarehouse/jforum/tests/selenium/javascript/htmlutils.js.shtml + License: Apache + Copyright: Copyright 2004 ThoughtWorks, Inc + + */ + var triggerMouseEvent = function(element, eventType, canBubble) { + element = MochiKit.DOM.getElement(element); + canBubble = (typeof(canBubble) == 'undefined') ? true : canBubble; + if (element.fireEvent) { + var newEvt = document.createEventObject(); + newEvt.clientX = 1; + newEvt.clientY = 1; + newEvt.button = 1; + element.fireEvent('on' + eventType, newEvt); + } else if (document.createEvent && (typeof(document.createEvent('MouseEvents').initMouseEvent) == 'function')) { + var evt = document.createEvent('MouseEvents'); + evt.initMouseEvent(eventType, canBubble, true, // event, bubbles, cancelable + document.defaultView, 1, // view, # of clicks + 1, 0, 0, 0, // screenX, screenY, clientX, clientY + false, false, false, false, // ctrlKey, altKey, shiftKey, metaKey + 0, null); // buttonCode, relatedTarget + element.dispatchEvent(evt); + } + }; + + var eventTest = function(e) { + i++; + t.ok((typeof(e.event()) === 'object'), 'checking that event() is an object'); + t.ok((typeof(e.type()) === 'string'), 'checking that type() is a string'); + t.ok((e.target() === MochiKit.DOM.getElement('submit')), 'checking that target is "submit"'); + t.ok((typeof(e.modifier()) === 'object'), 'checking that modifier() is an object'); + t.ok(e.modifier().alt === false, 'checking that modifier().alt is defined, but false'); + t.ok(e.modifier().ctrl === false, 'checking that modifier().ctrl is defined, but false'); + t.ok(e.modifier().meta === false, 'checking that modifier().meta is defined, but false'); + t.ok(e.modifier().shift === false, 'checking that modifier().shift is defined, but false'); + t.ok((typeof(e.mouse()) === 'object'), 'checking that mouse() is an object'); + t.ok((typeof(e.mouse().button) === 'object'), 'checking that mouse().button is an object'); + t.ok(e.mouse().button.left === true, 'checking that mouse().button.left is true'); + t.ok(e.mouse().button.middle === false, 'checking that mouse().button.middle is false'); + t.ok(e.mouse().button.right === false, 'checking that mouse().button.right is false'); + t.ok((typeof(e.mouse().page) === 'object'), 'checking that mouse().page is an object'); + t.ok((typeof(e.mouse().page.x) === 'number'), 'checking that mouse().page.x is a number'); + t.ok((typeof(e.mouse().page.y) === 'number'), 'checking that mouse().page.y is a number'); + t.ok((typeof(e.mouse().client) === 'object'), 'checking that mouse().client is an object'); + t.ok((typeof(e.mouse().client.x) === 'number'), 'checking that mouse().client.x is a number'); + t.ok((typeof(e.mouse().client.y) === 'number'), 'checking that mouse().client.y is a number'); + + /* these should not be defined */ + t.ok((typeof(e.relatedTarget()) === 'undefined'), 'checking that relatedTarget() is undefined'); + t.ok((typeof(e.key()) === 'undefined'), 'checking that key() is undefined'); + }; + + + ident = connect('submit', 'onmousedown', eventTest); + triggerMouseEvent('submit', 'mousedown', false); + t.is(i, 3, 'Connecting an event to an HTML object and firing a synthetic event'); + + disconnect(ident); + triggerMouseEvent('submit', 'mousedown', false); + t.is(i, 3, 'Disconnecting an event to an HTML object and firing a synthetic event'); + + + + } + + // non-DOM tests + + var hasNoSignals = {}; + + var hasSignals = {someVar: 1}; + + var i = 0; + + var aFunction = function() { + i++; + if (typeof(this.someVar) != 'undefined') { + i += this.someVar; + } + }; + + var bFunction = function(someArg, someOtherArg) { + i += someArg + someOtherArg; + }; + + + var aObject = {}; + aObject.aMethod = function() { + i++; + }; + + aObject.bMethod = function() { + i++; + }; + + var bObject = {}; + bObject.bMethod = function() { + i++; + }; + + + ident = connect(hasSignals, 'signalOne', aFunction); + signal(hasSignals, 'signalOne'); + t.is(i, 2, 'Connecting function'); + i = 0; + + disconnect(ident); + signal(hasSignals, 'signalOne'); + t.is(i, 0, 'New style disconnecting function'); + i = 0; + + + ident = connect(hasSignals, 'signalOne', bFunction); + signal(hasSignals, 'signalOne', 1, 2); + t.is(i, 3, 'Connecting function'); + i = 0; + + disconnect(ident); + signal(hasSignals, 'signalOne', 1, 2); + t.is(i, 0, 'New style disconnecting function'); + i = 0; + + + connect(hasSignals, 'signalOne', aFunction); + signal(hasSignals, 'signalOne'); + t.is(i, 2, 'Connecting function'); + i = 0; + + disconnect(hasSignals, 'signalOne', aFunction); + signal(hasSignals, 'signalOne'); + t.is(i, 0, 'Old style disconnecting function'); + i = 0; + + + ident = connect(hasSignals, 'signalOne', aObject, aObject.aMethod); + signal(hasSignals, 'signalOne'); + t.is(i, 1, 'Connecting obj-function'); + i = 0; + + disconnect(ident); + signal(hasSignals, 'signalOne'); + t.is(i, 0, 'New style disconnecting obj-function'); + i = 0; + + connect(hasSignals, 'signalOne', aObject, aObject.aMethod); + signal(hasSignals, 'signalOne'); + t.is(i, 1, 'Connecting obj-function'); + i = 0; + + disconnect(hasSignals, 'signalOne', aObject, aObject.aMethod); + signal(hasSignals, 'signalOne'); + t.is(i, 0, 'Disconnecting obj-function'); + i = 0; + + + ident = connect(hasSignals, 'signalTwo', aObject, 'aMethod'); + signal(hasSignals, 'signalTwo'); + t.is(i, 1, 'Connecting obj-string'); + i = 0; + + disconnect(ident); + signal(hasSignals, 'signalTwo'); + t.is(i, 0, 'New style disconnecting obj-string'); + i = 0; + + + connect(hasSignals, 'signalTwo', aObject, 'aMethod'); + signal(hasSignals, 'signalTwo'); + t.is(i, 1, 'Connecting obj-string'); + i = 0; + + disconnect(hasSignals, 'signalTwo', aObject, 'aMethod'); + signal(hasSignals, 'signalTwo'); + t.is(i, 0, 'Old style disconnecting obj-string'); + i = 0; + + + var shouldRaise = function() { return undefined.attr; }; + + try { + connect(hasSignals, 'signalOne', shouldRaise); + signal(hasSignals, 'signalOne'); + t.ok(false, 'An exception was not raised'); + } catch (e) { + t.ok(true, 'An exception was raised'); + } + disconnect(hasSignals, 'signalOne', shouldRaise); + t.is(i, 0, 'Exception raised, signal should not have fired'); + i = 0; + + + connect(hasSignals, 'signalOne', aObject, 'aMethod'); + connect(hasSignals, 'signalOne', aObject, 'bMethod'); + signal(hasSignals, 'signalOne'); + t.is(i, 2, 'Connecting one signal to two slots in one object'); + i = 0; + + disconnect(hasSignals, 'signalOne', aObject, 'aMethod'); + disconnect(hasSignals, 'signalOne', aObject, 'bMethod'); + signal(hasSignals, 'signalOne'); + t.is(i, 0, 'Disconnecting one signal from two slots in one object'); + i = 0; + + + connect(hasSignals, 'signalOne', aObject, 'aMethod'); + connect(hasSignals, 'signalOne', bObject, 'bMethod'); + signal(hasSignals, 'signalOne'); + t.is(i, 2, 'Connecting one signal to two slots in two objects'); + i = 0; + + disconnect(hasSignals, 'signalOne', aObject, 'aMethod'); + disconnect(hasSignals, 'signalOne', bObject, 'bMethod'); + signal(hasSignals, 'signalOne'); + t.is(i, 0, 'Disconnecting one signal from two slots in two objects'); + i = 0; + + + try { + connect(nothing, 'signalOne', aObject, 'aMethod'); + signal(nothing, 'signalOne'); + t.ok(false, 'An exception was not raised when connecting undefined'); + } catch (e) { + t.ok(true, 'An exception was raised when connecting undefined'); + } + + try { + disconnect(nothing, 'signalOne', aObject, 'aMethod'); + t.ok(false, 'An exception was not raised when disconnecting undefined'); + } catch (e) { + t.ok(true, 'An exception was raised when disconnecting undefined'); + } + + + try { + connect(hasSignals, 'signalOne', nothing); + signal(hasSignals, 'signalOne'); + t.ok(false, 'An exception was not raised when connecting an undefined function'); + } catch (e) { + t.ok(true, 'An exception was raised when connecting an undefined function'); + } + + try { + disconnect(hasSignals, 'signalOne', nothing); + t.ok(false, 'An exception was not raised when disconnecting an undefined function'); + } catch (e) { + t.ok(true, 'An exception was raised when disconnecting an undefined function'); + } + + + try { + connect(hasSignals, 'signalOne', aObject, aObject.nothing); + signal(hasSignals, 'signalOne'); + t.ok(false, 'An exception was not raised when connecting an undefined method'); + } catch (e) { + t.ok(true, 'An exception was raised when connecting an undefined method'); + } + + try { + connect(hasSignals, 'signalOne', aObject, 'nothing'); + signal(hasSignals, 'signalOne'); + t.ok(false, 'An exception was not raised when connecting an undefined method (as string)'); + } catch (e) { + t.ok(true, 'An exception was raised when connecting an undefined method (as string)'); + } + + t.is(i, 0, 'Signals should not have fired'); + + connect(hasSignals, 'signalOne', aFunction); + connect(hasSignals, 'signalOne', aObject, 'aMethod'); + disconnectAll(hasSignals, 'signalOne'); + signal(hasSignals, 'signalOne'); + t.is(i, 0, 'disconnectAll works with single explicit signal'); + i = 0; + + connect(hasSignals, 'signalOne', aFunction); + connect(hasSignals, 'signalOne', aObject, 'aMethod'); + connect(hasSignals, 'signalTwo', aFunction); + connect(hasSignals, 'signalTwo', aObject, 'aMethod'); + disconnectAll(hasSignals, 'signalOne'); + signal(hasSignals, 'signalOne'); + t.is(i, 0, 'disconnectAll works with single explicit signal'); + signal(hasSignals, 'signalTwo'); + t.is(i, 3, 'disconnectAll does not disconnect unrelated signals'); + i = 0; + + connect(hasSignals, 'signalOne', aFunction); + connect(hasSignals, 'signalOne', aObject, 'aMethod'); + connect(hasSignals, 'signalTwo', aFunction); + connect(hasSignals, 'signalTwo', aObject, 'aMethod'); + disconnectAll(hasSignals, 'signalOne', 'signalTwo'); + signal(hasSignals, 'signalOne'); + signal(hasSignals, 'signalTwo'); + t.is(i, 0, 'disconnectAll works with two explicit signals'); + i = 0; + + connect(hasSignals, 'signalOne', aFunction); + connect(hasSignals, 'signalOne', aObject, 'aMethod'); + connect(hasSignals, 'signalTwo', aFunction); + connect(hasSignals, 'signalTwo', aObject, 'aMethod'); + disconnectAll(hasSignals, ['signalOne', 'signalTwo']); + signal(hasSignals, 'signalOne'); + signal(hasSignals, 'signalTwo'); + t.is(i, 0, 'disconnectAll works with two explicit signals as a list'); + i = 0; + + connect(hasSignals, 'signalOne', aFunction); + connect(hasSignals, 'signalOne', aObject, 'aMethod'); + connect(hasSignals, 'signalTwo', aFunction); + connect(hasSignals, 'signalTwo', aObject, 'aMethod'); + disconnectAll(hasSignals); + signal(hasSignals, 'signalOne'); + signal(hasSignals, 'signalTwo'); + t.is(i, 0, 'disconnectAll works with implicit signals'); + i = 0; + + var toggle = function() { + disconnectAll(hasSignals, 'signalOne'); + connect(hasSignals, 'signalOne', aFunction); + i++; + }; + + connect(hasSignals, 'signalOne', aFunction); + connect(hasSignals, 'signalTwo', function() { i++; }); + connect(hasSignals, 'signalTwo', toggle); + connect(hasSignals, 'signalTwo', function() { i++; }); // #147 + connect(hasSignals, 'signalTwo', function() { i++; }); + signal(hasSignals, 'signalTwo'); + t.is(i, 4, 'disconnectAll fired in a signal loop works'); + i = 0; + disconnectAll('signalOne'); + disconnectAll('signalTwo'); + + var testfunc = function () { arguments.callee.count++; }; + testfunc.count = 0; + var testObj = { + methOne: function () { this.countOne++; }, countOne: 0, + methTwo: function () { this.countTwo++; }, countTwo: 0 + }; + connect(hasSignals, 'signalOne', testfunc); + connect(hasSignals, 'signalTwo', testfunc); + signal(hasSignals, 'signalOne'); + signal(hasSignals, 'signalTwo'); + t.is(testfunc.count, 2, 'disconnectAllTo func precondition'); + disconnectAllTo(testfunc); + signal(hasSignals, 'signalOne'); + signal(hasSignals, 'signalTwo'); + t.is(testfunc.count, 2, 'disconnectAllTo func'); + + connect(hasSignals, 'signalOne', testObj, 'methOne'); + connect(hasSignals, 'signalTwo', testObj, 'methTwo'); + signal(hasSignals, 'signalOne'); + signal(hasSignals, 'signalTwo'); + t.is(testObj.countOne, 1, 'disconnectAllTo obj precondition'); + t.is(testObj.countTwo, 1, 'disconnectAllTo obj precondition'); + disconnectAllTo(testObj); + signal(hasSignals, 'signalOne'); + signal(hasSignals, 'signalTwo'); + t.is(testObj.countOne, 1, 'disconnectAllTo obj'); + t.is(testObj.countTwo, 1, 'disconnectAllTo obj'); + + testObj.countOne = testObj.countTwo = 0; + connect(hasSignals, 'signalOne', testObj, 'methOne'); + connect(hasSignals, 'signalTwo', testObj, 'methTwo'); + disconnectAllTo(testObj, 'methOne'); + signal(hasSignals, 'signalOne'); + signal(hasSignals, 'signalTwo'); + t.is(testObj.countOne, 0, 'disconnectAllTo obj+str'); + t.is(testObj.countTwo, 1, 'disconnectAllTo obj+str'); + + has__Connect = { + count: 0, + __connect__: function (ident) { + this.count += arguments.length; + disconnect(ident); + } + }; + + connect(has__Connect, 'signalOne', aFunction); + t.is(has__Connect.count, 3, '__connect__ is called when it exists'); + signal(has__Connect, 'signalOne'); + t.is(has__Connect.count, 3, '__connect__ can disconnect the signal'); + + var events = {}; + var test_ident = connect(events, "test", function() { + var fail_ident = connect(events, "fail", function () { + events.failed = true; + }); + disconnect(fail_ident); + signal(events, "fail"); + }); + signal(events, "test"); + t.is(events.failed, undefined, 'disconnected slots do not fire'); + +}; diff --git a/dom/tests/mochitest/ajax/prototype/dist/prototype.js b/dom/tests/mochitest/ajax/prototype/dist/prototype.js new file mode 100644 index 0000000000..69f82032a8 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/dist/prototype.js @@ -0,0 +1,4681 @@ +/* Prototype JavaScript framework, version 1.6.0.2 + * (c) 2005-2007 Sam Stephenson + * + * Prototype is freely distributable under the terms of an MIT-style license. + * For details, see the Prototype web site: http://www.prototypejs.org/ + * + *--------------------------------------------------------------------------*/ + +var Prototype = { + Version: '1.6.0.2', + + Browser: { + IE: !!(window.attachEvent && !window.opera), + Opera: !!window.opera, + WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1, + Gecko: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1, + MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/) + }, + + BrowserFeatures: { + XPath: !!document.evaluate, + SelectorsAPI: !!document.querySelector, + ElementExtensions: !!window.HTMLElement, + SpecificElementExtensions: + document.createElement('div')['__proto__'] && + document.createElement('div')['__proto__'] !== + document.createElement('form')['__proto__'] + }, + + ScriptFragment: '<script[^>]*>([^\\x00]*?)<\/script>', + JSONFilter: /^\/\*-secure-([\s\S]*)\*\/\s*$/, + + emptyFunction: function() { }, + K: function(x) { return x } +}; + +if (Prototype.Browser.MobileSafari) + Prototype.BrowserFeatures.SpecificElementExtensions = false; + + +/* Based on Alex Arnell's inheritance implementation. */ +var Class = { + create: function() { + var parent = null, properties = $A(arguments); + if (Object.isFunction(properties[0])) + parent = properties.shift(); + + function klass() { + this.initialize.apply(this, arguments); + } + + Object.extend(klass, Class.Methods); + klass.superclass = parent; + klass.subclasses = []; + + if (parent) { + var subclass = function() { }; + subclass.prototype = parent.prototype; + klass.prototype = new subclass; + parent.subclasses.push(klass); + } + + for (var i = 0; i < properties.length; i++) + klass.addMethods(properties[i]); + + if (!klass.prototype.initialize) + klass.prototype.initialize = Prototype.emptyFunction; + + klass.prototype.constructor = klass; + + return klass; + } +}; + +Class.Methods = { + addMethods: function(source) { + var ancestor = this.superclass && this.superclass.prototype; + var properties = Object.keys(source); + + if (!Object.keys({ toString: true }).length) + properties.push("toString", "valueOf"); + + for (var i = 0, length = properties.length; i < length; i++) { + var property = properties[i], value = source[property]; + if (ancestor && Object.isFunction(value) && + value.argumentNames().first() == "$super") { + var method = value, value = (function(m) { + return function() { return ancestor[m].apply(this, arguments) }; + })(property).wrap(method); + + value.valueOf = method.valueOf.bind(method); + value.toString = method.toString.bind(method); + } + this.prototype[property] = value; + } + + return this; + } +}; + +var Abstract = { }; + +Object.extend = function(destination, source) { + for (var property in source) + destination[property] = source[property]; + return destination; +}; + +Object.extend(Object, { + inspect: function(object) { + try { + if (Object.isUndefined(object)) return 'undefined'; + if (object === null) return 'null'; + return object.inspect ? object.inspect() : String(object); + } catch (e) { + if (e instanceof RangeError) return '...'; + throw e; + } + }, + + toJSON: function(object) { + var type = typeof object; + switch (type) { + case 'undefined': + case 'function': + case 'unknown': return; + case 'boolean': return object.toString(); + } + + if (object === null) return 'null'; + if (object.toJSON) return object.toJSON(); + if (Object.isElement(object)) return; + + var results = []; + for (var property in object) { + var value = Object.toJSON(object[property]); + if (!Object.isUndefined(value)) + results.push(property.toJSON() + ': ' + value); + } + + return '{' + results.join(', ') + '}'; + }, + + toQueryString: function(object) { + return $H(object).toQueryString(); + }, + + toHTML: function(object) { + return object && object.toHTML ? object.toHTML() : String.interpret(object); + }, + + keys: function(object) { + var keys = []; + for (var property in object) + keys.push(property); + return keys; + }, + + values: function(object) { + var values = []; + for (var property in object) + values.push(object[property]); + return values; + }, + + clone: function(object) { + return Object.extend({ }, object); + }, + + isElement: function(object) { + return !!(object && object.nodeType == 1); + }, + + isArray: function(object) { + return object != null && typeof object == "object" && + 'splice' in object && 'join' in object; + }, + + isHash: function(object) { + return object && object instanceof Hash; + }, + + isFunction: function(object) { + return typeof object == "function" && typeof object.call == "function"; + }, + + isString: function(object) { + return typeof object == "string"; + }, + + isNumber: function(object) { + return typeof object == "number"; + }, + + isUndefined: function(object) { + return typeof object == "undefined"; + } +}); + +Object.extend(Function.prototype, { + argumentNames: function() { + var names = this.toString().match(/^[\s\(]*function[^(]*\(([^\)]*)\)/)[1] + .replace(/\s+/g, '').split(','); + return names.length == 1 && !names[0] ? [] : names; + }, + + bind: function() { + if (arguments.length < 2 && Object.isUndefined(arguments[0])) return this; + var __method = this, args = $A(arguments), object = args.shift(); + return function() { + return __method.apply(object, args.concat($A(arguments))); + } + }, + + bindAsEventListener: function() { + var __method = this, args = $A(arguments), object = args.shift(); + return function(event) { + return __method.apply(object, [event || window.event].concat(args)); + } + }, + + curry: function() { + if (!arguments.length) return this; + var __method = this, args = $A(arguments); + return function() { + return __method.apply(this, args.concat($A(arguments))); + } + }, + + delay: function() { + var __method = this, args = $A(arguments), timeout = args.shift() * 1000; + return window.setTimeout(function() { + return __method.apply(__method, args); + }, timeout); + }, + + defer: function() { + var args = [0.01].concat($A(arguments)); + return this.delay.apply(this, args); + }, + + wrap: function(wrapper) { + var __method = this; + return function() { + return wrapper.apply(this, [__method.bind(this)].concat($A(arguments))); + } + }, + + methodize: function() { + if (this._methodized) return this._methodized; + var __method = this; + return this._methodized = function() { + return __method.apply(null, [this].concat($A(arguments))); + }; + } +}); + +Date.prototype.toJSON = function() { + return '"' + this.getUTCFullYear() + '-' + + (this.getUTCMonth() + 1).toPaddedString(2) + '-' + + this.getUTCDate().toPaddedString(2) + 'T' + + this.getUTCHours().toPaddedString(2) + ':' + + this.getUTCMinutes().toPaddedString(2) + ':' + + this.getUTCSeconds().toPaddedString(2) + 'Z"'; +}; + +var Try = { + these: function() { + var returnValue; + + for (var i = 0, length = arguments.length; i < length; i++) { + var lambda = arguments[i]; + try { + returnValue = lambda(); + break; + } catch (e) { } + } + + return returnValue; + } +}; + +RegExp.prototype.match = RegExp.prototype.test; + +RegExp.escape = function(str) { + return String(str).replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1'); +}; + +/*--------------------------------------------------------------------------*/ + +var PeriodicalExecuter = Class.create({ + initialize: function(callback, frequency) { + this.callback = callback; + this.frequency = frequency; + this.currentlyExecuting = false; + + this.registerCallback(); + }, + + registerCallback: function() { + this.timer = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000); + }, + + execute: function() { + this.callback(this); + }, + + stop: function() { + if (!this.timer) return; + clearInterval(this.timer); + this.timer = null; + }, + + onTimerEvent: function() { + if (!this.currentlyExecuting) { + try { + this.currentlyExecuting = true; + this.execute(); + } finally { + this.currentlyExecuting = false; + } + } + } +}); +Object.extend(String, { + interpret: function(value) { + return value == null ? '' : String(value); + }, + specialChar: { + '\b': '\\b', + '\t': '\\t', + '\n': '\\n', + '\f': '\\f', + '\r': '\\r', + '\\': '\\\\' + } +}); + +Object.extend(String.prototype, { + gsub: function(pattern, replacement) { + var result = '', source = this, match; + replacement = arguments.callee.prepareReplacement(replacement); + + while (source.length > 0) { + if (match = source.match(pattern)) { + result += source.slice(0, match.index); + result += String.interpret(replacement(match)); + source = source.slice(match.index + match[0].length); + } else { + result += source, source = ''; + } + } + return result; + }, + + sub: function(pattern, replacement, count) { + replacement = this.gsub.prepareReplacement(replacement); + count = Object.isUndefined(count) ? 1 : count; + + return this.gsub(pattern, function(match) { + if (--count < 0) return match[0]; + return replacement(match); + }); + }, + + scan: function(pattern, iterator) { + this.gsub(pattern, iterator); + return String(this); + }, + + truncate: function(length, truncation) { + length = length || 30; + truncation = Object.isUndefined(truncation) ? '...' : truncation; + return this.length > length ? + this.slice(0, length - truncation.length) + truncation : String(this); + }, + + strip: function() { + return this.replace(/^\s+/, '').replace(/\s+$/, ''); + }, + + stripTags: function() { + return this.replace(/<\/?[^>]+>/gi, ''); + }, + + stripScripts: function() { + return this.replace(new RegExp(Prototype.ScriptFragment, 'img'), ''); + }, + + extractScripts: (function() { + var matchAll = new RegExp(Prototype.ScriptFragment, 'ig'); + var matchOne = new RegExp(Prototype.ScriptFragment, 'i'); + var matchComments = new RegExp('<!--\\s*' + Prototype.ScriptFragment + '\\s*-->', 'i'); + + return function() { + if (this.indexOf('<script') == -1) return []; + return (this.replace(matchComments, '').match(matchAll) || []).map(function(scriptTag) { + return (scriptTag.match(matchOne) || ['', ''])[1]; + }); + } + })(), + + evalScripts: function() { + return this.extractScripts().map(function(script) { return eval(script) }); + }, + + escapeHTML: function() { + var self = arguments.callee; + self.text.data = this; + return self.container.innerHTML.replace(/"/g, '"'); + }, + + unescapeHTML: function() { + var div = new Element('div'); + // Safari requires the text nested inside another element to render correctly + div.innerHTML = '<pre>' + this.stripTags() + '</pre>'; + div = div.firstChild; + return div.childNodes[0] ? (div.childNodes.length > 1 ? + $A(div.childNodes).inject('', function(memo, node) { return memo + node.nodeValue }) : + div.childNodes[0].nodeValue) : ''; + }, + + toQueryParams: function(separator) { + var match = this.strip().match(/([^?#]*)(#.*)?$/); + if (!match) return { }; + + return match[1].split(separator || '&').inject({ }, function(hash, pair) { + if ((pair = pair.split('='))[0]) { + var key = decodeURIComponent(pair.shift()); + var value = pair.length > 1 ? pair.join('=') : pair[0]; + if (value != undefined) value = decodeURIComponent(value); + + if (key in hash) { + if (!Object.isArray(hash[key])) hash[key] = [hash[key]]; + hash[key].push(value); + } + else hash[key] = value; + } + return hash; + }); + }, + + toArray: function() { + return this.split(''); + }, + + succ: function() { + return this.slice(0, this.length - 1) + + String.fromCharCode(this.charCodeAt(this.length - 1) + 1); + }, + + times: function(count) { + return count < 1 ? '' : new Array(count + 1).join(this); + }, + + camelize: function() { + var parts = this.split('-'), len = parts.length; + if (len == 1) return parts[0]; + + var camelized = this.charAt(0) == '-' + ? parts[0].charAt(0).toUpperCase() + parts[0].substring(1) + : parts[0]; + + for (var i = 1; i < len; i++) + camelized += parts[i].charAt(0).toUpperCase() + parts[i].substring(1); + + return camelized; + }, + + capitalize: function() { + return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase(); + }, + + underscore: function() { + return this.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'#{1}_#{2}').gsub(/([a-z\d])([A-Z])/,'#{1}_#{2}').gsub(/-/,'_').toLowerCase(); + }, + + dasherize: function() { + return this.gsub(/_/,'-'); + }, + + inspect: function(useDoubleQuotes) { + var escapedString = this.gsub(/[\x00-\x1f\\]/, function(match) { + var character = String.specialChar[match[0]]; + return character ? character : '\\u00' + match[0].charCodeAt().toPaddedString(2, 16); + }); + if (useDoubleQuotes) return '"' + escapedString.replace(/"/g, '\\"') + '"'; + return "'" + escapedString.replace(/'/g, '\\\'') + "'"; + }, + + toJSON: function() { + return this.inspect(true); + }, + + unfilterJSON: function(filter) { + return this.sub(filter || Prototype.JSONFilter, '#{1}'); + }, + + isJSON: function() { + var str = this; + if (str.blank()) return false; + str = this.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, ''); + return (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(str); + }, + + evalJSON: function(sanitize) { + var json = this.unfilterJSON(); + try { + if (!sanitize || json.isJSON()) return eval('(' + json + ')'); + } catch (e) { } + throw new SyntaxError('Badly formed JSON string: ' + this.inspect()); + }, + + include: function(pattern) { + return this.indexOf(pattern) > -1; + }, + + startsWith: function(pattern) { + return this.indexOf(pattern) === 0; + }, + + endsWith: function(pattern) { + var d = this.length - pattern.length; + return d >= 0 && this.lastIndexOf(pattern) === d; + }, + + empty: function() { + return this == ''; + }, + + blank: function() { + return /^\s*$/.test(this); + }, + + interpolate: function(object, pattern) { + return new Template(this, pattern).evaluate(object); + } +}); + +String.prototype.gsub.prepareReplacement = function(replacement) { + if (Object.isFunction(replacement)) return replacement; + var template = new Template(replacement); + return function(match) { return template.evaluate(match) }; +}; + +String.prototype.parseQuery = String.prototype.toQueryParams; + +Object.extend(String.prototype.escapeHTML, { + container: document.createElement('pre'), + text: document.createTextNode('') +}); + +String.prototype.escapeHTML.container.appendChild(String.prototype.escapeHTML.text); + +if (Prototype.Browser.IE) { + // IE converts all newlines to carriage returns so we swap them back + String.prototype.unescapeHTML = String.prototype.unescapeHTML.wrap(function(proceed) { + return proceed().replace(/\r/g, '\n') + }); +} + +if ('>'.escapeHTML() !== '>') { + // Safari 3.x has issues with escaping the ">" character + (function() { + var escapeHTML = String.prototype.escapeHTML; + Object.extend( + String.prototype.escapeHTML = escapeHTML.wrap(function(proceed) { + return proceed().replace(/>/g, ">") + }), { + container: escapeHTML.container, + text: escapeHTML.text + }) + })(); +} + +if ('&'.escapeHTML() !== '&') { + // Safari 2.x has issues with escaping html inside a "pre" element so we use the deprecated "xmp" element instead + Object.extend(String.prototype.escapeHTML, { + container: document.createElement('xmp'), + text: document.createTextNode('') + }); + String.prototype.escapeHTML.container.appendChild(String.prototype.escapeHTML.text); +} + +var Template = Class.create({ + initialize: function(template, pattern) { + this.template = template.toString(); + this.pattern = pattern || Template.Pattern; + }, + + evaluate: function(object) { + if (Object.isFunction(object.toTemplateReplacements)) + object = object.toTemplateReplacements(); + + return this.template.gsub(this.pattern, function(match) { + if (object == null) return ''; + + var before = match[1] || ''; + if (before == '\\') return match[2]; + + var ctx = object, expr = match[3]; + var pattern = /^([^.[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/; + match = pattern.exec(expr); + if (match == null) return before; + + while (match != null) { + var comp = match[1].startsWith('[') ? match[2].gsub('\\\\]', ']') : match[1]; + ctx = ctx[comp]; + if (null == ctx || '' == match[3]) break; + expr = expr.substring('[' == match[3] ? match[1].length : match[0].length); + match = pattern.exec(expr); + } + + return before + String.interpret(ctx); + }); + } +}); +Template.Pattern = /(^|.|\r|\n)(#\{(.*?)\})/; + +var $break = { }; + +var Enumerable = { + each: function(iterator, context) { + var index = 0; + try { + this._each(function(value) { + iterator.call(context, value, index++); + }); + } catch (e) { + if (e != $break) throw e; + } + return this; + }, + + eachSlice: function(number, iterator, context) { + var index = -number, slices = [], array = this.toArray(); + if (number < 1) return array; + while ((index += number) < array.length) + slices.push(array.slice(index, index+number)); + return slices.collect(iterator, context); + }, + + all: function(iterator, context) { + iterator = iterator || Prototype.K; + var result = true; + this.each(function(value, index) { + result = result && !!iterator.call(context, value, index); + if (!result) throw $break; + }); + return result; + }, + + any: function(iterator, context) { + iterator = iterator || Prototype.K; + var result = false; + this.each(function(value, index) { + if (result = !!iterator.call(context, value, index)) + throw $break; + }); + return result; + }, + + collect: function(iterator, context) { + iterator = iterator || Prototype.K; + var results = []; + this.each(function(value, index) { + results.push(iterator.call(context, value, index)); + }); + return results; + }, + + detect: function(iterator, context) { + var result; + this.each(function(value, index) { + if (iterator.call(context, value, index)) { + result = value; + throw $break; + } + }); + return result; + }, + + findAll: function(iterator, context) { + var results = []; + this.each(function(value, index) { + if (iterator.call(context, value, index)) + results.push(value); + }); + return results; + }, + + grep: function(filter, iterator, context) { + iterator = iterator || Prototype.K; + var results = []; + + if (Object.isString(filter)) + filter = new RegExp(filter); + + this.each(function(value, index) { + if (filter.match(value)) + results.push(iterator.call(context, value, index)); + }); + return results; + }, + + include: function(object) { + if (Object.isFunction(this.indexOf)) + if (this.indexOf(object) != -1) return true; + + var found = false; + this.each(function(value) { + if (value == object) { + found = true; + throw $break; + } + }); + return found; + }, + + inGroupsOf: function(number, fillWith) { + fillWith = Object.isUndefined(fillWith) ? null : fillWith; + return this.eachSlice(number, function(slice) { + while (slice.length < number) slice.push(fillWith); + return slice; + }); + }, + + inject: function(memo, iterator, context) { + this.each(function(value, index) { + memo = iterator.call(context, memo, value, index); + }); + return memo; + }, + + invoke: function(method) { + var args = $A(arguments).slice(1); + return this.map(function(value) { + return value[method].apply(value, args); + }); + }, + + max: function(iterator, context) { + iterator = iterator || Prototype.K; + var result; + this.each(function(value, index) { + value = iterator.call(context, value, index); + if (result == null || value >= result) + result = value; + }); + return result; + }, + + min: function(iterator, context) { + iterator = iterator || Prototype.K; + var result; + this.each(function(value, index) { + value = iterator.call(context, value, index); + if (result == null || value < result) + result = value; + }); + return result; + }, + + partition: function(iterator, context) { + iterator = iterator || Prototype.K; + var trues = [], falses = []; + this.each(function(value, index) { + (iterator.call(context, value, index) ? + trues : falses).push(value); + }); + return [trues, falses]; + }, + + pluck: function(property) { + var results = []; + this.each(function(value) { + results.push(value[property]); + }); + return results; + }, + + reject: function(iterator, context) { + var results = []; + this.each(function(value, index) { + if (!iterator.call(context, value, index)) + results.push(value); + }); + return results; + }, + + sortBy: function(iterator, context) { + return this.map(function(value, index) { + return { + value: value, + criteria: iterator.call(context, value, index) + }; + }).sort(function(left, right) { + var a = left.criteria, b = right.criteria; + return a < b ? -1 : a > b ? 1 : 0; + }).pluck('value'); + }, + + toArray: function() { + return this.map(); + }, + + zip: function() { + var iterator = Prototype.K, args = $A(arguments); + if (Object.isFunction(args.last())) + iterator = args.pop(); + + var collections = [this].concat(args).map($A); + return this.map(function(value, index) { + return iterator(collections.pluck(index)); + }); + }, + + size: function() { + return this.toArray().length; + }, + + inspect: function() { + return '#<Enumerable:' + this.toArray().inspect() + '>'; + } +}; + +Object.extend(Enumerable, { + map: Enumerable.collect, + find: Enumerable.detect, + select: Enumerable.findAll, + filter: Enumerable.findAll, + member: Enumerable.include, + entries: Enumerable.toArray, + every: Enumerable.all, + some: Enumerable.any +}); +function $A(iterable) { + if (!iterable) return []; + if (iterable.toArray) return iterable.toArray(); + var length = iterable.length || 0, results = new Array(length); + while (length--) results[length] = iterable[length]; + return results; +} + +if (Prototype.Browser.WebKit) { + $A = function(iterable) { + if (!iterable) return []; + if (!(Object.isFunction(iterable) && iterable == '[object NodeList]') && + iterable.toArray) return iterable.toArray(); + var length = iterable.length || 0, results = new Array(length); + while (length--) results[length] = iterable[length]; + return results; + }; +} + +Array.from = $A; + +Object.extend(Array.prototype, Enumerable); + +if (!Array.prototype._reverse) Array.prototype._reverse = Array.prototype.reverse; + +Object.extend(Array.prototype, { + _each: function(iterator) { + for (var i = 0, length = this.length; i < length; i++) + iterator(this[i]); + }, + + clear: function() { + this.length = 0; + return this; + }, + + first: function() { + return this[0]; + }, + + last: function() { + return this[this.length - 1]; + }, + + compact: function() { + return this.select(function(value) { + return value != null; + }); + }, + + flatten: function() { + return this.inject([], function(array, value) { + return array.concat(Object.isArray(value) ? + value.flatten() : [value]); + }); + }, + + without: function() { + var values = $A(arguments); + return this.select(function(value) { + return !values.include(value); + }); + }, + + reverse: function(inline) { + return (inline !== false ? this : this.toArray())._reverse(); + }, + + reduce: function() { + return this.length > 1 ? this : this[0]; + }, + + uniq: function(sorted) { + return this.inject([], function(array, value, index) { + if (0 == index || (sorted ? array.last() != value : !array.include(value))) + array.push(value); + return array; + }); + }, + + intersect: function(array) { + var length = array.length, i; + return this.uniq().findAll(function(item) { + i = length; + while (i--) if (item === array[i]) return true; + return false; + }); + }, + + clone: function() { + return [].concat(this); + }, + + size: function() { + return this.length; + }, + + inspect: function() { + return '[' + this.map(Object.inspect).join(', ') + ']'; + }, + + toJSON: function() { + var results = []; + this.each(function(object) { + var value = Object.toJSON(object); + if (!Object.isUndefined(value)) results.push(value); + }); + return '[' + results.join(', ') + ']'; + } +}); + +// use native browser JS 1.6 implementation if available +if (Object.isFunction(Array.prototype.forEach)) + Array.prototype._each = Array.prototype.forEach; + +if (!Array.prototype.indexOf) Array.prototype.indexOf = function(item, i) { + i || (i = 0); + var length = this.length; + if (i < 0) i = length + i; + for (; i < length; i++) + if (this[i] === item) return i; + return -1; +}; + +if (!Array.prototype.lastIndexOf) Array.prototype.lastIndexOf = function(item, i) { + i = isNaN(i) ? this.length : (i < 0 ? this.length + i : i) + 1; + var n = this.slice(0, i).reverse().indexOf(item); + return (n < 0) ? n : i - n - 1; +}; + +Array.prototype.toArray = Array.prototype.clone; + +function $w(string) { + if (!Object.isString(string)) return []; + string = string.strip(); + return string ? string.split(/\s+/) : []; +} + +if (Prototype.Browser.Opera){ + Array.prototype.concat = function() { + var array = []; + for (var i = 0, length = this.length; i < length; i++) array.push(this[i]); + for (var i = 0, length = arguments.length; i < length; i++) { + if (Object.isArray(arguments[i])) { + for (var j = 0, arrayLength = arguments[i].length; j < arrayLength; j++) + array.push(arguments[i][j]); + } else { + array.push(arguments[i]); + } + } + return array; + }; +} +Object.extend(Number.prototype, { + toColorPart: function() { + return this.toPaddedString(2, 16); + }, + + succ: function() { + return this + 1; + }, + + times: function(iterator, context) { + $R(0, this, true).each(iterator, context); + return this; + }, + + toPaddedString: function(length, radix) { + var string = this.toString(radix || 10); + return '0'.times(length - string.length) + string; + }, + + toJSON: function() { + return isFinite(this) ? this.toString() : 'null'; + } +}); + +$w('abs round ceil floor').each(function(method){ + Number.prototype[method] = Math[method].methodize(); +}); +function $H(object) { + return new Hash(object); +}; + +var Hash = Class.create(Enumerable, (function() { + + function toQueryPair(key, value) { + if (Object.isUndefined(value)) return key; + return key + '=' + encodeURIComponent(String.interpret(value)); + } + + return { + initialize: function(object) { + this._object = Object.isHash(object) ? object.toObject() : Object.clone(object); + }, + + _each: function(iterator) { + for (var key in this._object) { + var value = this._object[key], pair = [key, value]; + pair.key = key; + pair.value = value; + iterator(pair); + } + }, + + set: function(key, value) { + return this._object[key] = value; + }, + + get: function(key) { + // simulating poorly supported hasOwnProperty + if (this._object[key] !== Object.prototype[key]) + return this._object[key]; + }, + + unset: function(key) { + var value = this._object[key]; + delete this._object[key]; + return value; + }, + + toObject: function() { + return Object.clone(this._object); + }, + + keys: function() { + return this.pluck('key'); + }, + + values: function() { + return this.pluck('value'); + }, + + index: function(value) { + var match = this.detect(function(pair) { + return pair.value === value; + }); + return match && match.key; + }, + + merge: function(object) { + return this.clone().update(object); + }, + + update: function(object) { + return new Hash(object).inject(this, function(result, pair) { + result.set(pair.key, pair.value); + return result; + }); + }, + + toQueryString: function() { + return this.inject([], function(results, pair) { + var key = encodeURIComponent(pair.key), values = pair.value; + + if (values && typeof values == 'object') { + if (Object.isArray(values)) + return results.concat(values.map(toQueryPair.curry(key))); + } else results.push(toQueryPair(key, values)); + return results; + }).join('&'); + }, + + inspect: function() { + return '#<Hash:{' + this.map(function(pair) { + return pair.map(Object.inspect).join(': '); + }).join(', ') + '}>'; + }, + + toJSON: function() { + return Object.toJSON(this.toObject()); + }, + + clone: function() { + return new Hash(this); + } + } +})()); + +Hash.prototype.toTemplateReplacements = Hash.prototype.toObject; +Hash.from = $H; +var ObjectRange = Class.create(Enumerable, { + initialize: function(start, end, exclusive) { + this.start = start; + this.end = end; + this.exclusive = exclusive; + }, + + _each: function(iterator) { + var value = this.start; + while (this.include(value)) { + iterator(value); + value = value.succ(); + } + }, + + include: function(value) { + if (value < this.start) + return false; + if (this.exclusive) + return value < this.end; + return value <= this.end; + } +}); + +var $R = function(start, end, exclusive) { + return new ObjectRange(start, end, exclusive); +}; + +var Ajax = { + getTransport: function() { + var transport = false; + Ajax.getTransport = Try.these( + function() { + /* fallback on activex xmlhttp to avoid IE7 local file-system read error */ + if (Prototype.Browser.IE && window.location.href.indexOf('file://') == 0) throw 'skip'; + transport = new XMLHttpRequest(); + return function(){ return new XMLHttpRequest()} + }, + function() { + transport = new ActiveXObject('Msxml2.XMLHTTP'); + return function(){ return new ActiveXObject('Msxml2.XMLHTTP')} + }, + function() { + transport = new ActiveXObject('Microsoft.XMLHTTP'); + return function(){ return new ActiveXObject('Microsoft.XMLHTTP')} + } + ) || false; + + return transport; + }, + + activeRequestCount: 0 +}; + +Ajax.Responders = { + responders: [], + + _each: function(iterator) { + this.responders._each(iterator); + }, + + register: function(responder) { + if (!this.include(responder)) + this.responders.push(responder); + }, + + unregister: function(responder) { + this.responders = this.responders.without(responder); + }, + + dispatch: function(callback, request, transport, json) { + this.each(function(responder) { + if (Object.isFunction(responder[callback])) { + try { + responder[callback].apply(responder, [request, transport, json]); + } catch (e) { } + } + }); + } +}; + +Object.extend(Ajax.Responders, Enumerable); + +Ajax.Responders.register({ + onCreate: function() { Ajax.activeRequestCount++ }, + onComplete: function() { Ajax.activeRequestCount-- } +}); + +Ajax.Base = Class.create({ + initialize: function(options) { + this.allowStatusZero = false; + this.options = { + method: 'post', + asynchronous: true, + contentType: 'application/x-www-form-urlencoded', + encoding: 'UTF-8', + parameters: '', + evalJSON: true, + evalJS: true + }; + Object.extend(this.options, options || { }); + + this.options.method = this.options.method.toLowerCase(); + + if (Object.isString(this.options.parameters)) + this.options.parameters = this.options.parameters.toQueryParams(); + else if (Object.isHash(this.options.parameters)) + this.options.parameters = this.options.parameters.toObject(); + } +}); + +Ajax.Request = Class.create(Ajax.Base, { + _complete: false, + _allowStatusZero: false, + + initialize: function($super, url, options) { + $super(options); + this.transport = Ajax.getTransport(); + this.request(url); + }, + + request: (function() { + + var absoluteExp = /^[a-z]{3,5}:/, fileExp = /^(file|ftp):/; + function isRelative(url) { + return !absoluteExp.test(url); + } + + function isFileProtocol(url) { + return fileExp.test(url); + } + + return function(url) { + var base; + if (Prototype.Browser.Opera && opera.version() < 9.5 && + isRelative(url) && (base = $(document.documentElement).down('base'))) + url = base.readAttribute('href') + url; + + this.url = url; + this.method = this.options.method; + var params = Object.clone(this.options.parameters); + this._allowStatusZero = isFileProtocol(this.url) || + (isRelative(url) && isFileProtocol(window.location.protocol)); + + if (!['get', 'post'].include(this.method)) { + // simulate other verbs over post + params['_method'] = this.method; + this.method = 'post'; + } + + this.parameters = params; + + if (params = Object.toQueryString(params)) { + this.url += (this.url.include('?') ? '&' : '?') + params; + if (this.method == 'post' && + /Konqueror|Safari|KHTML/.test(navigator.userAgent)) + params += '&_='; + } + + try { + var response = new Ajax.Response(this); + if (this.options.onCreate) this.options.onCreate(response); + Ajax.Responders.dispatch('onCreate', this, response); + + this.transport.open(this.method.toUpperCase(), this.url, + this.options.asynchronous); + + if (this.options.asynchronous) this.respondToReadyState.bind(this).defer(1); + + this.transport.onreadystatechange = this.onStateChange.bind(this); + this.setRequestHeaders(); + + this.body = this.method == 'post' ? (this.options.postBody || params) : null; + this.transport.send(this.body); + + /* Force Firefox to handle ready state 4 for synchronous requests */ + if (!this.options.asynchronous && this.transport.overrideMimeType) + this.onStateChange(); + } + catch (e) { + this.dispatchException(e); + } + } + })(), + + onStateChange: function() { + var readyState = this.transport.readyState; + if (readyState > 1 && !((readyState == 4) && this._complete)) + this.respondToReadyState(this.transport.readyState); + }, + + setRequestHeaders: function() { + var headers = { + 'X-Requested-With': 'XMLHttpRequest', + 'X-Prototype-Version': Prototype.Version, + 'Accept': 'text/javascript, text/html, application/xml, text/xml, */*' + }; + + if (this.method == 'post') { + headers['Content-type'] = this.options.contentType + + (this.options.encoding ? '; charset=' + this.options.encoding : ''); + + /* Force "Connection: close" for older Mozilla browsers to work + * around a bug where XMLHttpRequest sends an incorrect + * Content-length header. See Mozilla Bugzilla #246651. + */ + if (this.transport.overrideMimeType && + (navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005])[1] < 2005) + headers['Connection'] = 'close'; + } + + // user-defined headers + if (typeof this.options.requestHeaders == 'object') { + var extras = this.options.requestHeaders; + + if (Object.isFunction(extras.push)) + for (var i = 0, length = extras.length; i < length; i += 2) + headers[extras[i]] = extras[i+1]; + else + $H(extras).each(function(pair) { headers[pair.key] = pair.value }); + } + + for (var name in headers) + this.transport.setRequestHeader(name, headers[name]); + }, + + success: function() { + var status = this.getStatus(); + return (!status && this._allowStatusZero) || (status >= 200 && status < 300); + }, + + getStatus: function() { + try { + return this.transport.status || 0; + } catch (e) { return 0 } + }, + + respondToReadyState: function(readyState) { + var state = Ajax.Request.Events[readyState], response = new Ajax.Response(this); + + if (state == 'Complete') { + try { + this._complete = true; + (this.options['on' + response.status] + || this.options['on' + (this.success() ? 'Success' : 'Failure')] + || Prototype.emptyFunction)(response, response.headerJSON); + } catch (e) { + this.dispatchException(e); + } + + var contentType = response.getHeader('Content-type'); + if (this.options.evalJS == 'force' + || (this.options.evalJS && this.isSameOrigin() && contentType + && contentType.match(/^\s*(text|application)\/(x-)?(java|ecma)script(;.*)?\s*$/i))) + this.evalResponse(); + } + + try { + (this.options['on' + state] || Prototype.emptyFunction)(response, response.headerJSON); + Ajax.Responders.dispatch('on' + state, this, response, response.headerJSON); + } catch (e) { + this.dispatchException(e); + } + + if (state == 'Complete') { + // avoid memory leak in MSIE: clean up + this.transport.onreadystatechange = Prototype.emptyFunction; + } + }, + + isSameOrigin: function() { + var m = this.url.match(/^\s*https?:\/\/[^\/]*/); + return !m || (m[0] == '#{protocol}//#{domain}#{port}'.interpolate({ + protocol: location.protocol, + domain: document.domain, + port: location.port ? ':' + location.port : '' + })); + }, + + getHeader: function(name) { + try { + return this.transport.getResponseHeader(name) || null; + } catch (e) { return null } + }, + + evalResponse: function() { + try { + return eval((this.transport.responseText || '').unfilterJSON()); + } catch (e) { + this.dispatchException(e); + } + }, + + dispatchException: function(exception) { + (this.options.onException || Prototype.emptyFunction)(this, exception); + Ajax.Responders.dispatch('onException', this, exception); + } +}); + +Ajax.Request.Events = + ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete']; + +Ajax.Response = Class.create({ + initialize: function(request){ + this.request = request; + var transport = this.transport = request.transport, + readyState = this.readyState = transport.readyState; + + if ((readyState > 2 && !Prototype.Browser.IE) || readyState == 4) { + this.status = this.getStatus(); + this.statusText = this.getStatusText(); + this.responseText = String.interpret(transport.responseText); + this.headerJSON = this._getHeaderJSON(); + } + + if (readyState == 4) { + var xml = transport.responseXML; + this.responseXML = Object.isUndefined(xml) ? null : xml; + this.responseJSON = this._getResponseJSON(); + } + }, + + status: 0, + statusText: '', + + getStatus: Ajax.Request.prototype.getStatus, + + getStatusText: function() { + try { + return this.transport.statusText || ''; + } catch (e) { return '' } + }, + + getHeader: Ajax.Request.prototype.getHeader, + + getAllHeaders: function() { + try { + return this.getAllResponseHeaders(); + } catch (e) { return null } + }, + + getResponseHeader: function(name) { + return this.transport.getResponseHeader(name); + }, + + getAllResponseHeaders: function() { + return this.transport.getAllResponseHeaders(); + }, + + _getHeaderJSON: function() { + var json = this.getHeader('X-JSON'); + if (!json) return null; + json = decodeURIComponent(escape(json)); + try { + return json.evalJSON(this.request.options.sanitizeJSON || + !this.request.isSameOrigin()); + } catch (e) { + this.request.dispatchException(e); + } + }, + + _getResponseJSON: function() { + var options = this.request.options; + if (!options.evalJSON || (options.evalJSON != 'force' && + !(this.getHeader('Content-type') || '').include('application/json')) || + this.responseText.blank()) + return null; + try { + return this.responseText.evalJSON(options.sanitizeJSON || + !this.request.isSameOrigin()); + } catch (e) { + this.request.dispatchException(e); + } + } +}); + +Ajax.Updater = Class.create(Ajax.Request, { + initialize: function($super, container, url, options) { + this.container = { + success: (container.success || container), + failure: (container.failure || (container.success ? null : container)) + }; + + options = Object.clone(options); + var onComplete = options.onComplete; + options.onComplete = (function(response, json) { + this.updateContent(response.responseText); + if (Object.isFunction(onComplete)) onComplete(response, json); + }).bind(this); + + $super(url, options); + }, + + updateContent: function(responseText) { + var receiver = this.container[this.success() ? 'success' : 'failure'], + options = this.options; + + if (!options.evalScripts) responseText = responseText.stripScripts(); + + if (receiver = $(receiver)) { + if (options.insertion) { + if (Object.isString(options.insertion)) { + var insertion = { }; insertion[options.insertion] = responseText; + receiver.insert(insertion); + } + else options.insertion(receiver, responseText); + } + else receiver.update(responseText); + } + } +}); + +Ajax.PeriodicalUpdater = Class.create(Ajax.Base, { + initialize: function($super, container, url, options) { + $super(options); + this.onComplete = this.options.onComplete; + + this.frequency = (this.options.frequency || 2); + this.decay = (this.options.decay || 1); + + this.updater = { }; + this.container = container; + this.url = url; + + this.start(); + }, + + start: function() { + this.options.onComplete = this.updateComplete.bind(this); + this.onTimerEvent(); + }, + + stop: function() { + this.updater.options.onComplete = undefined; + clearTimeout(this.timer); + (this.onComplete || Prototype.emptyFunction).apply(this, arguments); + }, + + updateComplete: function(response) { + if (this.options.decay) { + this.decay = (response.responseText == this.lastText ? + this.decay * this.options.decay : 1); + + this.lastText = response.responseText; + } + this.timer = this.onTimerEvent.bind(this).delay(this.decay * this.frequency); + }, + + onTimerEvent: function() { + this.updater = new Ajax.Updater(this.container, this.url, this.options); + } +}); +function $(element) { + if (arguments.length > 1) { + for (var i = 0, elements = [], length = arguments.length; i < length; i++) + elements.push($(arguments[i])); + return elements; + } + if (Object.isString(element)) + element = document.getElementById(element); + return Element.extend(element); +} + +if (Prototype.BrowserFeatures.XPath) { + document._getElementsByXPath = function(expression, parentElement) { + var results = []; + var query = document.evaluate(expression, $(parentElement) || document, + null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); + for (var i = 0, length = query.snapshotLength; i < length; i++) + results.push(Element.extend(query.snapshotItem(i))); + return results; + }; +} + +/*--------------------------------------------------------------------------*/ + +if (!window.Node) var Node = { }; + +if (!Node.ELEMENT_NODE) { + // DOM level 2 ECMAScript Language Binding + Object.extend(Node, { + ELEMENT_NODE: 1, + ATTRIBUTE_NODE: 2, + TEXT_NODE: 3, + CDATA_SECTION_NODE: 4, + ENTITY_REFERENCE_NODE: 5, + ENTITY_NODE: 6, + PROCESSING_INSTRUCTION_NODE: 7, + COMMENT_NODE: 8, + DOCUMENT_NODE: 9, + DOCUMENT_TYPE_NODE: 10, + DOCUMENT_FRAGMENT_NODE: 11, + NOTATION_NODE: 12 + }); +} + +(function() { + var element = this.Element; + this.Element = function(tagName, attributes) { + attributes = attributes || { }; + tagName = tagName.toLowerCase(); + var cache = Element.cache; + if (Prototype.Browser.IE && (attributes.name || attributes.type)) { + tagName = '<' + tagName + + (attributes.name ? ' name="' + attributes.name + '"' : '') + + (attributes.type ? ' type="' + attributes.type + '"' : '') + '>'; + delete attributes.name; delete attributes.type; + return Element.writeAttribute(document.createElement(tagName), attributes); + } + if (!cache[tagName]) cache[tagName] = Element.extend(document.createElement(tagName)); + return Element.writeAttribute(cache[tagName].cloneNode(false), attributes); + }; + Object.extend(this.Element, element || { }); + if (element) this.Element.prototype = element.prototype; +}).call(window); + +Element.cache = { }; + +Element.Methods = { + visible: function(element) { + return $(element).style.display != 'none'; + }, + + toggle: function(element) { + element = $(element); + Element[Element.visible(element) ? 'hide' : 'show'](element); + return element; + }, + + hide: function(element) { + element = $(element); + var originalDisplay = element.style.display; + if (originalDisplay && originalDisplay != 'none') + element._originalDisplay = originalDisplay; + element.style.display = 'none'; + return element; + }, + + show: function(element) { + element = $(element); + if (element._originalDisplay) { + element.style.display = element._originalDisplay; + element._originalDisplay = null; + } else element.style.display = ''; + return element; + }, + + remove: function(element) { + element = $(element); + element.parentNode.removeChild(element); + return element; + }, + + update: function(element, content) { + element = $(element); + if (content && content.toElement) content = content.toElement(); + if (Object.isElement(content)) return element.update().insert(content); + content = Object.toHTML(content); + element.innerHTML = content.stripScripts(); + content.evalScripts.bind(content).defer(); + return element; + }, + + replace: function(element, content) { + element = $(element); + if (content && content.toElement) content = content.toElement(); + else if (!Object.isElement(content)) { + content = Object.toHTML(content); + var range = element.ownerDocument.createRange(); + range.selectNode(element); + content.evalScripts.bind(content).defer(); + content = range.createContextualFragment(content.stripScripts()); + } + element.parentNode.replaceChild(content, element); + return element; + }, + + insert: function(element, insertions) { + element = $(element); + + if (Object.isString(insertions) || Object.isNumber(insertions) || + Object.isElement(insertions) || (insertions && (insertions.toElement || insertions.toHTML))) + insertions = {bottom:insertions}; + + var content, insert, tagName, childNodes; + + for (var position in insertions) { + content = insertions[position]; + position = position.toLowerCase(); + insert = Element._insertionTranslations[position]; + + if (content && content.toElement) content = content.toElement(); + if (Object.isElement(content)) { + insert(element, content); + continue; + } + + content = Object.toHTML(content); + + tagName = ((position == 'before' || position == 'after') + ? element.parentNode : element).tagName.toUpperCase(); + + childNodes = Element._getContentFromAnonymousElement(tagName, content.stripScripts()); + + if (position == 'top' || position == 'after') childNodes.reverse(); + childNodes.each(insert.curry(element)); + + content.evalScripts.bind(content).defer(); + } + + return element; + }, + + wrap: function(element, wrapper, attributes) { + element = $(element); + if (Object.isElement(wrapper)) + $(wrapper).writeAttribute(attributes || { }); + else if (Object.isString(wrapper)) wrapper = new Element(wrapper, attributes); + else wrapper = new Element('div', wrapper); + if (element.parentNode) + element.parentNode.replaceChild(wrapper, element); + wrapper.appendChild(element); + return wrapper; + }, + + inspect: function(element) { + element = $(element); + var result = '<' + element.tagName.toLowerCase(); + $H({'id': 'id', 'className': 'class'}).each(function(pair) { + var property = pair.first(), attribute = pair.last(); + var value = (element[property] || '').toString(); + if (value) result += ' ' + attribute + '=' + value.inspect(true); + }); + return result + '>'; + }, + + recursivelyCollect: function(element, property) { + element = $(element); + var elements = []; + while (element = element[property]) + if (element.nodeType == 1) + elements.push(Element.extend(element)); + return elements; + }, + + ancestors: function(element) { + return $(element).recursivelyCollect('parentNode'); + }, + + descendants: function(element) { + return $(element).select("*"); + }, + + firstDescendant: function(element) { + element = $(element).firstChild; + while (element && element.nodeType != 1) element = element.nextSibling; + return $(element); + }, + + immediateDescendants: function(element) { + if (!(element = $(element).firstChild)) return []; + while (element && element.nodeType != 1) element = element.nextSibling; + if (element) return [element].concat($(element).nextSiblings()); + return []; + }, + + previousSiblings: function(element) { + return $(element).recursivelyCollect('previousSibling'); + }, + + nextSiblings: function(element) { + return $(element).recursivelyCollect('nextSibling'); + }, + + siblings: function(element) { + element = $(element); + return element.previousSiblings().reverse().concat(element.nextSiblings()); + }, + + match: function(element, selector) { + if (Object.isString(selector)) + selector = new Selector(selector); + return selector.match($(element)); + }, + + up: function(element, expression, index) { + element = $(element); + if (arguments.length == 1) return $(element.parentNode); + var ancestors = element.ancestors(); + return Object.isNumber(expression) ? ancestors[expression] : + Selector.findElement(ancestors, expression, index); + }, + + down: function(element, expression, index) { + element = $(element); + if (arguments.length == 1) return element.firstDescendant(); + return Object.isNumber(expression) ? element.descendants()[expression] : + Element.select(element, expression)[index || 0]; + }, + + previous: function(element, expression, index) { + element = $(element); + if (arguments.length == 1) return $(Selector.handlers.previousElementSibling(element)); + var previousSiblings = element.previousSiblings(); + return Object.isNumber(expression) ? previousSiblings[expression] : + Selector.findElement(previousSiblings, expression, index); + }, + + next: function(element, expression, index) { + element = $(element); + if (arguments.length == 1) return $(Selector.handlers.nextElementSibling(element)); + var nextSiblings = element.nextSiblings(); + return Object.isNumber(expression) ? nextSiblings[expression] : + Selector.findElement(nextSiblings, expression, index); + }, + + select: function() { + var args = $A(arguments), element = $(args.shift()); + return Selector.findChildElements(element, args); + }, + + adjacent: function() { + var args = $A(arguments), element = $(args.shift()); + return Selector.findChildElements(element.parentNode, args).without(element); + }, + + identify: function(element) { + element = $(element); + var id = element.readAttribute('id'), self = arguments.callee; + if (id) return id; + do { id = 'anonymous_element_' + self.counter++ } while ($(id)); + element.writeAttribute('id', id); + return id; + }, + + readAttribute: function(element, name) { + element = $(element); + var t = Element._attributeTranslations.read; + if (t.names[name]) name = t.names[name]; + + if (Prototype.Browser.IE) { + // If we're reading from a form, avoid a conflict between an attribute + // and a child name. + if (element.tagName.toUpperCase() == 'FORM' && + !/^((child|parent)Node|(next|previous)Sibling)$/.test(name) && + element.children[name]){ + element = $(element.cloneNode(false)); + } + if (t.values[name]) return t.values[name](element, name); + if (name.include(':')) { + return (!element.attributes || !element.attributes[name]) ? null : + element.attributes[name].value; + } + } else if (t.values[name]) return t.values[name](element, name); + + return element.getAttribute(name); + }, + + writeAttribute: function(element, name, value) { + element = $(element); + var attributes = { }, t = Element._attributeTranslations.write; + + if (typeof name == 'object') attributes = name; + else attributes[name] = Object.isUndefined(value) ? true : value; + + for (var attr in attributes) { + name = t.names[attr] || attr; + value = attributes[attr]; + if (t.values[name]) name = t.values[name](element, value); + if (value === false || value === null) + element.removeAttribute(name); + else if (value === true) + element.setAttribute(name, name); + else element.setAttribute(name, value); + } + return element; + }, + + getHeight: function(element) { + return $(element).getDimensions().height; + }, + + getWidth: function(element) { + return $(element).getDimensions().width; + }, + + classNames: function(element) { + return new Element.ClassNames(element); + }, + + hasClassName: function(element, className) { + if (!(element = $(element))) return; + var elementClassName = element.className; + return (elementClassName.length > 0 && (elementClassName == className || + new RegExp("(^|\\s)" + className + "(\\s|$)").test(elementClassName))); + }, + + addClassName: function(element, className) { + if (!(element = $(element))) return; + if (!element.hasClassName(className)) + element.className += (element.className ? ' ' : '') + className; + return element; + }, + + removeClassName: function(element, className) { + if (!(element = $(element))) return; + element.className = element.className.replace( + new RegExp("(^|\\s+)" + className + "(\\s+|$)"), ' ').strip(); + return element; + }, + + toggleClassName: function(element, className) { + if (!(element = $(element))) return; + return element[element.hasClassName(className) ? + 'removeClassName' : 'addClassName'](className); + }, + + // removes whitespace-only text node children + cleanWhitespace: function(element) { + element = $(element); + var node = element.firstChild; + while (node) { + var nextNode = node.nextSibling; + if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) + element.removeChild(node); + node = nextNode; + } + return element; + }, + + empty: function(element) { + return $(element).innerHTML.blank(); + }, + + descendantOf: function(element, ancestor) { + element = $(element), ancestor = $(ancestor); + + if (element.compareDocumentPosition) + return (element.compareDocumentPosition(ancestor) & 8) === 8; + + if (ancestor.contains) + return ancestor.contains(element) && ancestor !== element; + + while (element = element.parentNode) + if (element == ancestor) return true; + + return false; + }, + + scrollTo: function(element) { + element = $(element); + var pos = element.cumulativeOffset(); + window.scrollTo(pos[0], pos[1]); + return element; + }, + + getStyle: function(element, style) { + element = $(element); + style = style == 'float' ? 'cssFloat' : style.camelize(); + var value = element.style[style]; + if (!value || value == 'auto') { + var css = document.defaultView.getComputedStyle(element, null); + value = css ? css[style] : null; + } + if (style == 'opacity') return value ? parseFloat(value) : 1.0; + return value == 'auto' ? null : value; + }, + + getOpacity: function(element) { + return $(element).getStyle('opacity'); + }, + + setStyle: function(element, styles) { + element = $(element); + var elementStyle = element.style, match; + if (Object.isString(styles)) { + element.style.cssText += ';' + styles; + return styles.include('opacity') ? + element.setOpacity(styles.match(/opacity:\s*(\d?\.?\d*)/)[1]) : element; + } + for (var property in styles) + if (property == 'opacity') element.setOpacity(styles[property]); + else + elementStyle[(property == 'float' || property == 'cssFloat') ? + (Object.isUndefined(elementStyle.styleFloat) ? 'cssFloat' : 'styleFloat') : + property] = styles[property]; + + return element; + }, + + setOpacity: function(element, value) { + element = $(element); + element.style.opacity = (value == 1 || value === '') ? '' : + (value < 0.00001) ? 0 : value; + return element; + }, + + getDimensions: function(element) { + element = $(element); + var display = element.getStyle('display'), + dimensions = { width: element.clientWidth, height: element.clientHeight }; + + // All *Width and *Height properties give 0 on elements with display: none, + // so enable the element temporarily + if (display === "none" || display === null) { + var els = element.style, + originalVisibility = els.visibility, + originalPosition = els.position, + originalDisplay = els.display; + + els.visibility = 'hidden'; + els.position = 'absolute'; + els.display = 'block'; + + dimensions = { width: element.clientWidth, height: element.clientHeight }; + + els.display = originalDisplay; + els.position = originalPosition; + els.visibility = originalVisibility; + } + + return dimensions; + }, + + makePositioned: function(element) { + element = $(element); + var pos = Element.getStyle(element, 'position'); + if (pos == 'static' || !pos) { + element._madePositioned = true; + element.style.position = 'relative'; + // Opera returns the offset relative to the positioning context, when an + // element is position relative but top and left have not been defined + if (window.opera) { + element.style.top = 0; + element.style.left = 0; + } + } + return element; + }, + + undoPositioned: function(element) { + element = $(element); + if (element._madePositioned) { + element._madePositioned = undefined; + element.style.position = + element.style.top = + element.style.left = + element.style.bottom = + element.style.right = ''; + } + return element; + }, + + makeClipping: function(element) { + element = $(element); + if (element._overflow) return element; + element._overflow = Element.getStyle(element, 'overflow') || 'auto'; + if (element._overflow !== 'hidden') + element.style.overflow = 'hidden'; + return element; + }, + + undoClipping: function(element) { + element = $(element); + if (!element._overflow) return element; + element.style.overflow = element._overflow == 'auto' ? '' : element._overflow; + element._overflow = null; + return element; + }, + + cumulativeOffset: function(element) { + element = $(element); + var valueT = 0, valueL = 0; + do { + valueT += element.offsetTop || 0; + valueL += element.offsetLeft || 0; + element = element.offsetParent; + } while (element); + return Element._returnOffset(valueL, valueT); + }, + + positionedOffset: function(element) { + element = $(element); + var valueT = 0, valueL = 0; + do { + valueT += element.offsetTop || 0; + valueL += element.offsetLeft || 0; + element = element.offsetParent; + if (element) { + if (element.tagName.toUpperCase() == 'BODY') break; + var p = Element.getStyle(element, 'position'); + if (p !== 'static') break; + } + } while (element); + return Element._returnOffset(valueL, valueT); + }, + + absolutize: function(element) { + element = $(element); + if (element.getStyle('position') == 'absolute') return element; + // Position.prepare(); // To be done manually by Scripty when it needs it. + + var offsets = element.positionedOffset(), + dimensions = element.getDimensions(), + top = offsets[1], + left = offsets[0], + width = dimensions.width, + height = dimensions.height; + + Object.extend(element, { + _originalLeft: left - parseFloat(element.style.left || 0), + _originalTop: top - parseFloat(element.style.top || 0), + _originalWidth: element.style.width, + _originalHeight: element.style.height + }); + + element.setStyle({ + position: 'absolute', + top: top + 'px', + left: left + 'px', + width: width + 'px', + height: height + 'px' + }); + + return element; + }, + + relativize: function(element) { + element = $(element); + if (element.getStyle('position') == 'relative') return element; + + if (!element._originalTop){ + /* fix bizarre IE position issue with empty elements */ + var isBuggy = element.outerHTML && element.innerHTML.blank(); + if (isBuggy) element.innerHTML = '\x00'; + + Object.extend(element, { + _originalTop: element.offsetTop, + _originalLeft: element.offsetLeft, + _originalWidth: Element.getStyle(element, 'width'), + _originalHeight: Element.getStyle(element, 'height'), + _originalMarginTop: Element.getStyle(element, 'marginTop'), + _originalMarginLeft: Element.getStyle(element, 'marginLeft') + }); + + if (isBuggy) element.innerHTML = ''; + } + + Element.setStyle(element, { + position: 'relative', + width: element._originalWidth, + height: element._originalHeight, + marginTop: element._originalMarginTop, + marginLeft: element._originalMarginLeft + }); + + var offsets = element.positionedOffset(), + top = element._originalTop - offsets.top, + left = element._originalLeft - offsets.left; + + var isAuto = /^(auto|)$/; + if (!isAuto.test(element.style.top)) top += element._originalTop; + if (!isAuto.test(element.style.left)) left += element._originalLeft; + + Element.setStyle(element, { + top: top + 'px', + left: left + 'px' + }); + + return element; + }, + + getOffsetParent: function(element) { + element = $(element); + var op = element.offsetParent, docElement = document.documentElement; + if (op && op != docElement) return $(op); + + while ((element = element.parentNode) && element !== docElement && + element !== document) { + if (Element.getStyle(element, 'position') != 'static') + return $(element); + } + + return $(document.body); + } +}; + +Object.extend(Element.Methods, (function() { + function getNumericStyle(element, style) { + return parseFloat(Element.getStyle(element, style)) || 0; + } + + function getStyleDiff(element, source, style) { + return getNumericStyle(source, style) - getNumericStyle(element, style); + } + + function cloneDimension(element, source, dimension) { + var d = Element.getDimensions(source), style = { }; + style[dimension] = d[dimension] + 'px'; + + var styles = $w('margin padding'); + var sides = (dimension === 'height') ? $w('top bottom') : + $w('left right'); + + var property; + for (var i = 0; i < 2; i++) { + for (var j = 0; j < 2; j++) { + property = styles[i] + sides[j].capitalize(); + style[property] = (getNumericStyle(element, property) + + getStyleDiff(element, source, property)) + 'px'; + } + } + Element.setStyle(element, style); + } + + return { + cumulativeScrollOffset: function(element) { + element = $(element); + var valueT = 0, valueL = 0, endElement = document; + var B = Prototype.Browser; + + // Safari and Opera need to stop at document.body or else they'll + // report inaccurate values. + if (B.WebKit || B.Opera && opera.version() < 9.5) { + if ([document, document.body, document.documentElement].include(element)) + return Element._returnOffset(0, 0); + + endElement = document.body; + } + + if (Element.getStyle(element, 'position') !== 'fixed') { + while ((element = element.parentNode) && element !== endElement) { + if (Element.getStyle(element, 'position') === 'fixed') break; + valueT += element.scrollTop || 0; + valueL += element.scrollLeft || 0; + } + } + return Element._returnOffset(valueL, valueT); + }, + + cumulativeOffset: function(element) { + element = $(element); + var valueT = 0, valueL = 0; + do { + valueT += (element.offsetTop || 0); + valueL += (element.offsetLeft || 0); + } while ((element = Element.getOffsetParent(element)) != document.body); + + return Element._returnOffset(valueL, valueT); + }, + + positionedOffset: function(element) { + element = $(element); + var valueT = 0, valueL = 0; + do { + valueT += (element.offsetTop || 0); + valueL += (element.offsetLeft || 0); + element = Element.getOffsetParent(element); + } while (element != document.body && + Element.getStyle(element, 'position') == 'static'); + + return Element._returnOffset(valueL, valueT); + }, + + viewportOffset: function(forElement) { + forElement = $(forElement); + var op, element = forElement, valueT = 0, valueL = 0; + + do { + valueT += (element.offsetTop || 0); + valueL += (element.offsetLeft || 0); + + // Safari fix + op = Element.getOffsetParent(element); + if (op == document.body && Element.getStyle(element, + 'position') == 'absolute') break; + } while ((element = op) != document.body); + + var scrollOffset = Element.cumulativeScrollOffset(forElement); + valueT -= scrollOffset.top; + valueL -= scrollOffset.left; + + return Element._returnOffset(valueL, valueT); + }, + + clonePosition: function(element, source) { + element = $(element); + source = $(source); + var options = Object.extend({ + setLeft: true, + setTop: true, + setWidth: true, + setHeight: true, + offsetTop: 0, + offsetLeft: 0 + }, arguments[2] || { }); + + // find coordinate system to use + // delta [0,0] will do fine with position: fixed elements; + // position: absolute needs offsetParent deltas + var parent, delta = [0, 0]; + if (Element.getStyle(element, 'position') == 'absolute') { + parent = Element.getOffsetParent(element); + delta = Element.viewportOffset(parent); + } + + // correct by body offsets (fixes Safari) + if (parent == document.body) { + delta[0] -= document.body.offsetLeft; + delta[1] -= document.body.offsetTop; + } + + // set dimensions + if (options.setWidth) cloneDimension(element, source, 'width'); + if (options.setHeight) cloneDimension(element, source, 'height'); + + // find page position of source + var p = Element.viewportOffset(source), + borderOffset = ['borderLeftWidth', 'borderTopWidth'].map( + function(style) { return getStyleDiff(element, source, style); }); + + if (options.setLeft) { + var left = p[0] - delta[0] + borderOffset[0]; + if (options.offsetLeft) + left += options.offsetLeft + getNumericStyle(element, 'paddingLeft'); + + element.style.left = left + 'px'; + } + if (options.setTop) { + var top = p[1] - delta[1] + borderOffset[1]; + if (options.offsetTop) + top += options.offsetTop + getNumericStyle(element, 'paddingTop'); + + element.style.top = top + 'px'; + } + return element; + } + }; +})()); + +Element.Methods.identify.counter = 1; + +Object.extend(Element.Methods, { + getElementsBySelector: Element.Methods.select, + childElements: Element.Methods.immediateDescendants +}); + +Element._attributeTranslations = { + write: { + names: { + className: 'class', + htmlFor: 'for' + }, + values: { } + }, + + read: { + names: { }, + values: { + _flag: function(element, attribute) { + return $(element).hasAttribute(attribute) ? attribute : null; + } + } + } +}; + +(function(v) { + Object.extend(v, { + disabled: v._flag, + checked: v._flag, + readonly: v._flag, + multiple: v._flag + }); +})(Element._attributeTranslations.read.values); + +if (Prototype.Browser.Opera) { + Element.Methods.getStyle = Element.Methods.getStyle.wrap( + function(proceed, element, style) { + switch (style) { + case 'left': case 'top': case 'right': case 'bottom': + if (proceed(element, 'position') === 'static') return null; + case 'height': case 'width': + // returns '0px' for hidden elements; we want it to return null + if (!Element.visible(element)) return null; + + // returns the border-box dimensions rather than the content-box + // dimensions, so we subtract padding and borders from the value + var dim = parseInt(proceed(element, style), 10); + + if (dim !== element['offset' + style.capitalize()]) + return dim + 'px'; + + var properties; + if (style === 'height') { + properties = ['border-top-width', 'padding-top', + 'padding-bottom', 'border-bottom-width']; + } + else { + properties = ['border-left-width', 'padding-left', + 'padding-right', 'border-right-width']; + } + return properties.inject(dim, function(memo, property) { + var val = proceed(element, property); + return val === null ? memo : memo - parseInt(val, 10); + }) + 'px'; + default: return proceed(element, style); + } + } + ); + + Element.Methods.readAttribute = Element.Methods.readAttribute.wrap( + function(proceed, element, attribute) { + if (attribute === 'title') return $(element).title; + return proceed(element, attribute); + } + ); +} + +else if (Prototype.Browser.IE) { + // IE doesn't report offsets correctly for static elements, so we change them + // to "relative" to get the values, then change them back. + Element.Methods.getOffsetParent = Element.Methods.getOffsetParent.wrap( + function(proceed, element) { + element = $(element); + // IE throws an error if element is not in document + try { element.offsetParent; } + catch(e) { return $(document.body); } + + var position = element.getStyle('position'); + if (position !== 'static') return proceed(element); + element.setStyle({ position: 'relative' }); + + var value = proceed(element); + element.setStyle({ position: position }); + return value; + } + ); + + $w('positionedOffset viewportOffset').each(function(method) { + Element.Methods[method] = Element.Methods[method].wrap( + function(proceed, element) { + element = $(element); + + var position = Element.getStyle(element, 'position'); + if (position !== 'static') return proceed(element); + + // Trigger hasLayout on the offset parent so that IE6 reports + // accurate offsetTop and offsetLeft values for position: fixed. + var offsetParent = Element.getOffsetParent(element), + style = { position: 'relative' }; + if (Element.getOffsetParent(offsetParent, 'position') === 'fixed') + style.zoom = '1'; + + Element.setStyle(element, style); + var value = proceed(element); + Element.setStyle(element, { position: position}); + return value; + } + ); + }); + + + Element.Methods.getStyle = function(element, style) { + element = $(element); + style = (style == 'float' || style == 'cssFloat') ? 'styleFloat' : style.camelize(); + var value = element.style[style]; + if (!value && element.currentStyle) value = element.currentStyle[style]; + + if (style == 'opacity') { + if (value = (element.getStyle('filter') || '').match(/alpha\(opacity=(.*)\)/)) + if (value[1]) return parseFloat(value[1]) / 100; + return 1.0; + } + + if (value == 'auto') { + if ((style == 'width' || style == 'height') && (element.getStyle('display') != 'none')) + return element['offset' + style.capitalize()] + 'px'; + return null; + } + return value; + }; + + Element.Methods.setOpacity = function(element, value) { + function stripAlpha(filter){ + return filter.replace(/alpha\([^\)]*\)/gi,''); + } + element = $(element); + var currentStyle = element.currentStyle; + if ((currentStyle && !currentStyle.hasLayout) || + (!currentStyle && element.style.zoom == 'normal')) + element.style.zoom = 1; + + var filter = element.getStyle('filter'), style = element.style; + if (value == 1 || value === '') { + (filter = stripAlpha(filter)) ? + style.filter = filter : style.removeAttribute('filter'); + return element; + } else if (value < 0.00001) value = 0; + style.filter = stripAlpha(filter) + + 'alpha(opacity=' + (value * 100) + ')'; + return element; + }; + + (function(t) { + t.has = { }; + t.write.names = { }; + + $w('cellPadding cellSpacing colSpan rowSpan vAlign dateTime accessKey ' + + 'tabIndex encType maxLength readOnly longDesc frameBorder').each(function(attr) { + var lower = attr.toLowerCase(); + t.has[lower] = attr; + t.read.names[lower] = attr; + t.write.names[lower] = attr; + }); + + [t.write.names, t.read.names].each(function(n) { + Object.extend(n, { + 'class': 'className', + 'for': 'htmlFor' + }); + }); + })(Element._attributeTranslations); + + Object.extend(Element._attributeTranslations.read.values, { + + _getAttr: function(element, attribute) { + return element.getAttribute(attribute, 2); + }, + + _getAttrNode: function(element, attribute) { + var node = element.getAttributeNode(attribute); + return node ? node.value : ""; + }, + + _getEv: function(element, attribute) { + attribute = element.getAttribute(attribute); + return attribute ? attribute.toString().slice(23, -2) : null; + }, + + style: function(element) { + return element.style.cssText.toLowerCase(); + }, + + title: function(element) { + return element.title; + } + }); + + Object.extend(Element._attributeTranslations.write.values, { + checked: function(element, value) { + element.checked = !!value; + }, + + encType: function(element, value) { + element.getAttributeNode('encType').value = value; + }, + + style: function(element, value) { + element.style.cssText = value ? value : ''; + } + }); + + (function(v) { + delete v.readonly; + Object.extend(v, { + href: v._getAttr, + src: v._getAttr, + type: v._getAttr, + action: v._getAttrNode, + onload: v._getEv, + onunload: v._getEv, + onclick: v._getEv, + ondblclick: v._getEv, + onmousedown: v._getEv, + onmouseup: v._getEv, + onmouseover: v._getEv, + onmousemove: v._getEv, + onmouseout: v._getEv, + onfocus: v._getEv, + onblur: v._getEv, + onkeypress: v._getEv, + onkeydown: v._getEv, + onkeyup: v._getEv, + onsubmit: v._getEv, + onreset: v._getEv, + onselect: v._getEv, + onchange: v._getEv, + readOnly: v._flag.wrap(function(proceed, element, attribute) { + attribute = proceed(element, attribute); + return attribute? 'readonly' : null; + }) + }); + })(Element._attributeTranslations.read.values); +} + +else if (Prototype.Browser.Gecko && /rv:1\.8\.0/.test(navigator.userAgent)) { + Element.Methods.setOpacity = function(element, value) { + element = $(element); + element.style.opacity = (value == 1) ? 0.999999 : + (value === '') ? '' : (value < 0.00001) ? 0 : value; + return element; + }; +} + +else if (Prototype.Browser.WebKit) { + Element.Methods.setOpacity = function(element, value) { + element = $(element); + element.style.opacity = (value == 1 || value === '') ? '' : + (value < 0.00001) ? 0 : value; + + if (value == 1) + if (element.tagName.toUpperCase() == 'IMG' && element.width) { + element.width++; element.width--; + } else try { + var n = document.createTextNode(' '); + element.appendChild(n); + element.removeChild(n); + } catch (e) { } + + return element; + }; + + // Safari returns margins on body which is incorrect if the child is absolutely + // positioned. For performance reasons, redefine Element#cumulativeOffset for + // KHTML/WebKit only. + Element.Methods.cumulativeOffset = function(element) { + element = $(element); + var valueT = 0, valueL = 0; + do { + valueT += element.offsetTop || 0; + valueL += element.offsetLeft || 0; + if (element.offsetParent == document.body) + if (Element.getStyle(element, 'position') == 'absolute') break; + + element = element.offsetParent; + } while (element); + + return Element._returnOffset(valueL, valueT); + }; +} + +if (Prototype.Browser.IE || Prototype.Browser.Opera) { + // IE and Opera are missing .innerHTML support for TABLE-related and SELECT elements + Element.Methods.update = function(element, content) { + element = $(element); + + if (content && content.toElement) content = content.toElement(); + if (Object.isElement(content)) return element.update().insert(content); + + content = Object.toHTML(content); + var tagName = element.tagName.toUpperCase(); + + if (tagName in Element._insertionTranslations.tags) { + $A(element.childNodes).each(function(node) { element.removeChild(node) }); + Element._getContentFromAnonymousElement(tagName, content.stripScripts()) + .each(function(node) { element.appendChild(node) }); + } + else element.innerHTML = content.stripScripts(); + + content.evalScripts.bind(content).defer(); + return element; + }; +} + +if (Prototype.Browser.IE) { + // Wrap Element#update to clean up event handlers on + // newly-removed elements. Prevents memory leaks in IE. + Element.Methods.update = Element.Methods.update.wrap( + function(proceed, element, contents) { + Element.select(element, '*').each(Event.stopObserving); + return proceed(element, contents); + } + ); +} + +if ('outerHTML' in document.createElement('div')) { + Element.Methods.replace = function(element, content) { + element = $(element); + + if (content && content.toElement) content = content.toElement(); + if (Object.isElement(content)) { + element.parentNode.replaceChild(content, element); + return element; + } + + content = Object.toHTML(content); + var parent = element.parentNode, tagName = parent.tagName.toUpperCase(); + + // Avoid outerHTML in IE because it incorrectly removes the replaced + // elements' child nodes. + if (Element._insertionTranslations.tags[tagName] || Prototype.Browser.IE) { + var nextSibling = element.next(); + var fragments = Element._getContentFromAnonymousElement(tagName, + content.stripScripts()); + parent.removeChild(element); + if (nextSibling) + fragments.each(function(node) { parent.insertBefore(node, nextSibling) }); + else + fragments.each(function(node) { parent.appendChild(node) }); + } + else element.outerHTML = content.stripScripts(); + + content.evalScripts.bind(content).defer(); + return element; + }; +} + +Element._returnOffset = function(l, t) { + var result = [l, t]; + result.left = l; + result.top = t; + return result; +}; + +Element._getContentFromAnonymousElement = function(tagName, html) { + var div = new Element('div'), t = Element._insertionTranslations.tags[tagName]; + if (t) { + div.innerHTML = t[0] + html + t[1]; + t[2].times(function() { div = div.firstChild }); + } else div.innerHTML = html; + return $A(div.childNodes); +}; + +Element._insertionTranslations = { + before: function(element, node) { + element.parentNode.insertBefore(node, element); + }, + top: function(element, node) { + element.insertBefore(node, element.firstChild); + }, + bottom: function(element, node) { + element.appendChild(node); + }, + after: function(element, node) { + element.parentNode.insertBefore(node, element.nextSibling); + }, + tags: { + TABLE: ['<table>', '</table>', 1], + TBODY: ['<table><tbody>', '</tbody></table>', 2], + TR: ['<table><tbody><tr>', '</tr></tbody></table>', 3], + TD: ['<table><tbody><tr><td>', '</td></tr></tbody></table>', 4], + SELECT: ['<select>', '</select>', 1] + } +}; + +(function() { + Object.extend(this.tags, { + THEAD: this.tags.TBODY, + TFOOT: this.tags.TBODY, + TH: this.tags.TD + }); +}).call(Element._insertionTranslations); + +Element.Methods.Simulated = { + // No use of $ in this function in order to keep things fast. + // Used by the Selector class. + hasAttribute: function(element, attribute) { + attribute = Element._attributeTranslations.has[attribute] || attribute; + var node = element.getAttributeNode(attribute); + return !!(node && node.specified); + } +}; + +Element.Methods.ByTag = { }; + +Object.extend(Element, Element.Methods); + +if (!Prototype.BrowserFeatures.ElementExtensions && + document.createElement('div')['__proto__']) { + window.HTMLElement = { }; + window.HTMLElement.prototype = document.createElement('div')['__proto__']; + Prototype.BrowserFeatures.ElementExtensions = true; +} + +Element.extend = (function() { + if (Prototype.BrowserFeatures.SpecificElementExtensions) + return Prototype.K; + + var Methods = { }, ByTag = Element.Methods.ByTag; + + var extend = Object.extend(function(element) { + if (!element || element._extendedByPrototype || + element.nodeType != 1 || element == window) return element; + + // Filter out XML nodes in IE. + if (!(element.ownerDocument || element).body) return element; + + var methods = Object.clone(Methods), + tagName = element.tagName.toUpperCase(), property, value; + + // extend methods for specific tags + if (ByTag[tagName]) Object.extend(methods, ByTag[tagName]); + + for (property in methods) { + value = methods[property]; + if (Object.isFunction(value) && !(property in element)) + element[property] = value.methodize(); + } + + element._extendedByPrototype = Prototype.emptyFunction; + return element; + + }, { + refresh: function() { + // extend methods for all tags (Safari doesn't need this) + if (!Prototype.BrowserFeatures.ElementExtensions) { + Object.extend(Methods, Element.Methods); + Object.extend(Methods, Element.Methods.Simulated); + } + } + }); + + extend.refresh(); + return extend; +})(); + + +// No use of $ in this function in order to keep things fast. +// Used by the Selector class. +Element.hasAttribute = function(element, attribute) { + if (element.hasAttribute) return element.hasAttribute(attribute); + return Element.Methods.Simulated.hasAttribute(element, attribute); +}; + +Element.addMethods = function(methods) { + var F = Prototype.BrowserFeatures, T = Element.Methods.ByTag; + + if (!methods) { + Object.extend(Form, Form.Methods); + Object.extend(Form.Element, Form.Element.Methods); + Object.extend(Element.Methods.ByTag, { + "BUTTON": Object.clone(Form.Element.Methods), + "FORM": Object.clone(Form.Methods), + "INPUT": Object.clone(Form.Element.Methods), + "SELECT": Object.clone(Form.Element.Methods), + "TEXTAREA": Object.clone(Form.Element.Methods) + }); + } + + if (arguments.length == 2) { + var tagName = methods; + methods = arguments[1]; + } + + if (!tagName) Object.extend(Element.Methods, methods || { }); + else { + if (Object.isArray(tagName)) tagName.each(extend); + else extend(tagName); + } + + function extend(tagName) { + tagName = tagName.toUpperCase(); + if (!Element.Methods.ByTag[tagName]) + Element.Methods.ByTag[tagName] = { }; + Object.extend(Element.Methods.ByTag[tagName], methods); + } + + function copy(methods, destination, onlyIfAbsent) { + onlyIfAbsent = onlyIfAbsent || false; + for (var property in methods) { + var value = methods[property]; + if (!Object.isFunction(value)) continue; + if (!onlyIfAbsent || !(property in destination)) + destination[property] = value.methodize(); + } + } + + function findDOMClass(tagName) { + var klass; + var trans = { + "OPTGROUP": "OptGroup", "TEXTAREA": "TextArea", "P": "Paragraph", + "FIELDSET": "FieldSet", "UL": "UList", "OL": "OList", "DL": "DList", + "DIR": "Directory", "H1": "Heading", "H2": "Heading", "H3": "Heading", + "H4": "Heading", "H5": "Heading", "H6": "Heading", "Q": "Quote", + "INS": "Mod", "DEL": "Mod", "A": "Anchor", "IMG": "Image", "CAPTION": + "TableCaption", "COL": "TableCol", "COLGROUP": "TableCol", "THEAD": + "TableSection", "TFOOT": "TableSection", "TBODY": "TableSection", "TR": + "TableRow", "TH": "TableCell", "TD": "TableCell", "FRAMESET": + "FrameSet", "IFRAME": "IFrame" + }; + if (trans[tagName]) klass = 'HTML' + trans[tagName] + 'Element'; + if (window[klass]) return window[klass]; + klass = 'HTML' + tagName + 'Element'; + if (window[klass]) return window[klass]; + klass = 'HTML' + tagName.capitalize() + 'Element'; + if (window[klass]) return window[klass]; + + window[klass] = { }; + window[klass].prototype = document.createElement(tagName)['__proto__']; + return window[klass]; + } + + if (F.ElementExtensions) { + copy(Element.Methods, HTMLElement.prototype); + copy(Element.Methods.Simulated, HTMLElement.prototype, true); + } + + if (F.SpecificElementExtensions) { + for (var tag in Element.Methods.ByTag) { + var klass = findDOMClass(tag); + if (Object.isUndefined(klass)) continue; + copy(T[tag], klass.prototype); + } + } + + Object.extend(Element, Element.Methods); + delete Element.ByTag; + + if (Element.extend.refresh) Element.extend.refresh(); + Element.cache = { }; +}; + +document.viewport = { + getDimensions: function() { + var dimensions = { }, B = Prototype.Browser; + $w('width height').each(function(d) { + var D = d.capitalize(); + dimensions[d] = (B.WebKit && !document.evaluate) ? self['inner' + D] : + (B.Opera && opera.version() < 9.5) ? document.body['client' + D] : document.documentElement['client' + D]; + }); + return dimensions; + }, + + getWidth: function() { + return this.getDimensions().width; + }, + + getHeight: function() { + return this.getDimensions().height; + }, + + getScrollOffsets: function() { + return Element._returnOffset( + window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft, + window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop); + } +}; +/* Portions of the Selector class are derived from Jack Slocum's DomQuery, + * part of YUI-Ext version 0.40, distributed under the terms of an MIT-style + * license. Please see http://www.yui-ext.com/ for more information. */ + +var Selector = Class.create({ + initialize: function(expression) { + this.expression = expression.strip(); + + if (this.shouldUseSelectorsAPI()) { + this.mode = 'selectorsAPI'; + } else if (this.shouldUseXPath()) { + this.mode = 'xpath'; + this.compileXPathMatcher(); + } else { + this.mode = "normal"; + this.compileMatcher(); + } + + }, + + shouldUseXPath: function() { + if (!Prototype.BrowserFeatures.XPath) return false; + + var e = this.expression; + + // Safari 3 chokes on :*-of-type and :empty + if (Prototype.Browser.WebKit && + (e.include("-of-type") || e.include(":empty"))) + return false; + + // XPath can't do namespaced attributes, nor can it read + // the "checked" property from DOM nodes + if ((/(\[[\w-]*?:|:checked)/).test(e)) + return false; + + return true; + }, + + shouldUseSelectorsAPI: function() { + if (!Prototype.BrowserFeatures.SelectorsAPI) return false; + + if (!Selector._div) Selector._div = new Element('div'); + + // Make sure the browser treats the selector as valid. Test on an + // isolated element to minimize cost of this check. + try { + Selector._div.querySelector(this.expression); + } catch(e) { + return false; + } + + return true; + }, + + compileMatcher: function() { + var e = this.expression, ps = Selector.patterns, h = Selector.handlers, + c = Selector.criteria, le, p, m; + + if (Selector._cache[e]) { + this.matcher = Selector._cache[e]; + return; + } + + this.matcher = ["this.matcher = function(root) {", + "var r = root, h = Selector.handlers, c = false, n;"]; + + while (e && le != e && (/\S/).test(e)) { + le = e; + for (var i in ps) { + p = ps[i]; + if (m = e.match(p)) { + this.matcher.push(Object.isFunction(c[i]) ? c[i](m) : + new Template(c[i]).evaluate(m)); + e = e.replace(m[0], ''); + break; + } + } + } + + this.matcher.push("return h.unique(n);\n}"); + eval(this.matcher.join('\n')); + Selector._cache[this.expression] = this.matcher; + }, + + compileXPathMatcher: function() { + var e = this.expression, ps = Selector.patterns, + x = Selector.xpath, le, m; + + if (Selector._cache[e]) { + this.xpath = Selector._cache[e]; return; + } + + this.matcher = ['.//*']; + while (e && le != e && (/\S/).test(e)) { + le = e; + for (var i in ps) { + if (m = e.match(ps[i])) { + this.matcher.push(Object.isFunction(x[i]) ? x[i](m) : + new Template(x[i]).evaluate(m)); + e = e.replace(m[0], ''); + break; + } + } + } + + this.xpath = this.matcher.join(''); + Selector._cache[this.expression] = this.xpath; + }, + + findElements: function(root) { + root = root || document; + var e = this.expression, results; + + switch (this.mode) { + case 'selectorsAPI': + // querySelectorAll queries document-wide, then filters to descendants + // of the context element. That's not what we want. + // Add an explicit context to the selector if necessary. + if (root !== document) { + var oldId = root.id, id = $(root).identify(); + e = "#" + id + " " + e; + } + + results = $A(root.querySelectorAll(e)).map(Element.extend); + root.id = oldId; + + return results; + case 'xpath': + return document._getElementsByXPath(this.xpath, root); + default: + return this.matcher(root); + } + }, + + match: function(element) { + this.tokens = []; + + var e = this.expression, ps = Selector.patterns, as = Selector.assertions; + var le, p, m; + + while (e && le !== e && (/\S/).test(e)) { + le = e; + for (var i in ps) { + p = ps[i]; + if (m = e.match(p)) { + // use the Selector.assertions methods unless the selector + // is too complex. + if (as[i]) { + this.tokens.push([i, Object.clone(m)]); + e = e.replace(m[0], ''); + } else { + // reluctantly do a document-wide search + // and look for a match in the array + return this.findElements(document).include(element); + } + } + } + } + + var match = true, name, matches; + for (var i = 0, token; token = this.tokens[i]; i++) { + name = token[0], matches = token[1]; + if (!Selector.assertions[name](element, matches)) { + match = false; break; + } + } + + return match; + }, + + toString: function() { + return this.expression; + }, + + inspect: function() { + return "#<Selector:" + this.expression.inspect() + ">"; + } +}); + +Object.extend(Selector, { + _cache: { }, + + xpath: { + descendant: "//*", + child: "/*", + adjacent: "/following-sibling::*[1]", + laterSibling: '/following-sibling::*', + tagName: function(m) { + if (m[1] == '*') return ''; + return "[local-name()='" + m[1].toLowerCase() + + "' or local-name()='" + m[1].toUpperCase() + "']"; + }, + className: "[contains(concat(' ', @class, ' '), ' #{1} ')]", + id: "[@id='#{1}']", + attrPresence: function(m) { + m[1] = m[1].toLowerCase(); + return new Template("[@#{1}]").evaluate(m); + }, + attr: function(m) { + m[1] = m[1].toLowerCase(); + m[3] = m[5] || m[6]; + return new Template(Selector.xpath.operators[m[2]]).evaluate(m); + }, + pseudo: function(m) { + var h = Selector.xpath.pseudos[m[1]]; + if (!h) return ''; + if (Object.isFunction(h)) return h(m); + return new Template(Selector.xpath.pseudos[m[1]]).evaluate(m); + }, + operators: { + '=': "[@#{1}='#{3}']", + '!=': "[@#{1}!='#{3}']", + '^=': "[starts-with(@#{1}, '#{3}')]", + '$=': "[substring(@#{1}, (string-length(@#{1}) - string-length('#{3}') + 1))='#{3}']", + '*=': "[contains(@#{1}, '#{3}')]", + '~=': "[contains(concat(' ', @#{1}, ' '), ' #{3} ')]", + '|=': "[contains(concat('-', @#{1}, '-'), '-#{3}-')]" + }, + pseudos: { + 'first-child': '[not(preceding-sibling::*)]', + 'last-child': '[not(following-sibling::*)]', + 'only-child': '[not(preceding-sibling::* or following-sibling::*)]', + 'empty': "[count(*) = 0 and (count(text()) = 0)]", + 'checked': "[@checked]", + 'disabled': "[(@disabled) and (@type!='hidden')]", + 'enabled': "[not(@disabled) and (@type!='hidden')]", + 'not': function(m) { + var e = m[6], p = Selector.patterns, + x = Selector.xpath, le, v; + + var exclusion = []; + while (e && le != e && (/\S/).test(e)) { + le = e; + for (var i in p) { + if (m = e.match(p[i])) { + v = Object.isFunction(x[i]) ? x[i](m) : new Template(x[i]).evaluate(m); + exclusion.push("(" + v.substring(1, v.length - 1) + ")"); + e = e.replace(m[0], ''); + break; + } + } + } + return "[not(" + exclusion.join(" and ") + ")]"; + }, + 'nth-child': function(m) { + return Selector.xpath.pseudos.nth("(count(./preceding-sibling::*) + 1) ", m); + }, + 'nth-last-child': function(m) { + return Selector.xpath.pseudos.nth("(count(./following-sibling::*) + 1) ", m); + }, + 'nth-of-type': function(m) { + return Selector.xpath.pseudos.nth("position() ", m); + }, + 'nth-last-of-type': function(m) { + return Selector.xpath.pseudos.nth("(last() + 1 - position()) ", m); + }, + 'first-of-type': function(m) { + m[6] = "1"; return Selector.xpath.pseudos['nth-of-type'](m); + }, + 'last-of-type': function(m) { + m[6] = "1"; return Selector.xpath.pseudos['nth-last-of-type'](m); + }, + 'only-of-type': function(m) { + var p = Selector.xpath.pseudos; return p['first-of-type'](m) + p['last-of-type'](m); + }, + nth: function(fragment, m) { + var mm, formula = m[6], predicate; + if (formula == 'even') formula = '2n+0'; + if (formula == 'odd') formula = '2n+1'; + if (mm = formula.match(/^(\d+)$/)) // digit only + return '[' + fragment + "= " + mm[1] + ']'; + if (mm = formula.match(/^(-?\d*)?n(([+-])(\d+))?/)) { // an+b + if (mm[1] == "-") mm[1] = -1; + var a = mm[1] ? Number(mm[1]) : 1; + var b = mm[2] ? Number(mm[2]) : 0; + predicate = "[((#{fragment} - #{b}) mod #{a} = 0) and " + + "((#{fragment} - #{b}) div #{a} >= 0)]"; + return new Template(predicate).evaluate({ + fragment: fragment, a: a, b: b }); + } + } + } + }, + + criteria: { + tagName: 'n = h.tagName(n, r, "#{1}", c); c = false;', + className: 'n = h.className(n, r, "#{1}", c); c = false;', + id: 'n = h.id(n, r, "#{1}", c); c = false;', + attrPresence: 'n = h.attrPresence(n, r, "#{1}", c); c = false;', + attr: function(m) { + m[3] = (m[5] || m[6]); + return new Template('n = h.attr(n, r, "#{1}", "#{3}", "#{2}", c); c = false;').evaluate(m); + }, + pseudo: function(m) { + if (m[6]) m[6] = m[6].replace(/"/g, '\\"'); + return new Template('n = h.pseudo(n, "#{1}", "#{6}", r, c); c = false;').evaluate(m); + }, + descendant: 'c = "descendant";', + child: 'c = "child";', + adjacent: 'c = "adjacent";', + laterSibling: 'c = "laterSibling";' + }, + + patterns: { + // combinators must be listed first + // (and descendant needs to be last combinator) + laterSibling: /^\s*~\s*/, + child: /^\s*>\s*/, + adjacent: /^\s*\+\s*/, + descendant: /^\s/, + + // selectors follow + tagName: /^\s*(\*|[\w\-]+)(\b|$)?/, + id: /^#([\w\-\*]+)(\b|$)/, + className: /^\.([\w\-\*]+)(\b|$)/, + pseudo: +/^:((first|last|nth|nth-last|only)(-child|-of-type)|empty|checked|(en|dis)abled|not)(\((.*?)\))?(\b|$|(?=\s|[:+~>]))/, + attrPresence: /^\[((?:[\w]+:)?[\w]+)\]/, + attr: /\[((?:[\w-]*:)?[\w-]+)\s*(?:([!^$*~|]?=)\s*((['"])([^\4]*?)\4|([^'"][^\]]*?)))?\]/ + }, + + // for Selector.match and Element#match + assertions: { + tagName: function(element, matches) { + return matches[1].toUpperCase() == element.tagName.toUpperCase(); + }, + + className: function(element, matches) { + return Element.hasClassName(element, matches[1]); + }, + + id: function(element, matches) { + return element.id === matches[1]; + }, + + attrPresence: function(element, matches) { + return Element.hasAttribute(element, matches[1]); + }, + + attr: function(element, matches) { + var nodeValue = Element.readAttribute(element, matches[1]); + return nodeValue && Selector.operators[matches[2]](nodeValue, matches[5] || matches[6]); + } + }, + + handlers: { + // UTILITY FUNCTIONS + // joins two collections + concat: function(a, b) { + for (var i = 0, node; node = b[i]; i++) + a.push(node); + return a; + }, + + // marks an array of nodes for counting + mark: function(nodes) { + var _true = Prototype.emptyFunction; + for (var i = 0, node; node = nodes[i]; i++) + node._countedByPrototype = _true; + return nodes; + }, + + unmark: function(nodes) { + for (var i = 0, node; node = nodes[i]; i++) + node._countedByPrototype = undefined; + return nodes; + }, + + // mark each child node with its position (for nth calls) + // "ofType" flag indicates whether we're indexing for nth-of-type + // rather than nth-child + index: function(parentNode, reverse, ofType) { + parentNode._countedByPrototype = Prototype.emptyFunction; + if (reverse) { + for (var nodes = parentNode.childNodes, i = nodes.length - 1, j = 1; i >= 0; i--) { + var node = nodes[i]; + if (node.nodeType == 1 && (!ofType || node._countedByPrototype)) node.nodeIndex = j++; + } + } else { + for (var i = 0, j = 1, nodes = parentNode.childNodes; node = nodes[i]; i++) + if (node.nodeType == 1 && (!ofType || node._countedByPrototype)) node.nodeIndex = j++; + } + }, + + // filters out duplicates and extends all nodes + unique: function(nodes) { + if (nodes.length == 0) return nodes; + var results = [], n; + for (var i = 0, l = nodes.length; i < l; i++) + if (!(n = nodes[i])._countedByPrototype) { + n._countedByPrototype = Prototype.emptyFunction; + results.push(Element.extend(n)); + } + return Selector.handlers.unmark(results); + }, + + // COMBINATOR FUNCTIONS + descendant: function(nodes) { + var h = Selector.handlers; + for (var i = 0, results = [], node; node = nodes[i]; i++) + h.concat(results, node.getElementsByTagName('*')); + return results; + }, + + child: function(nodes) { + var h = Selector.handlers; + for (var i = 0, results = [], node; node = nodes[i]; i++) { + for (var j = 0, child; child = node.childNodes[j]; j++) + if (child.nodeType == 1 && child.tagName != '!') results.push(child); + } + return results; + }, + + adjacent: function(nodes) { + for (var i = 0, results = [], node; node = nodes[i]; i++) { + var next = this.nextElementSibling(node); + if (next) results.push(next); + } + return results; + }, + + laterSibling: function(nodes) { + var h = Selector.handlers; + for (var i = 0, results = [], node; node = nodes[i]; i++) + h.concat(results, Element.nextSiblings(node)); + return results; + }, + + nextElementSibling: function(node) { + while (node = node.nextSibling) + if (node.nodeType == 1) return node; + return null; + }, + + previousElementSibling: function(node) { + while (node = node.previousSibling) + if (node.nodeType == 1) return node; + return null; + }, + + // TOKEN FUNCTIONS + tagName: function(nodes, root, tagName, combinator) { + var uTagName = tagName.toUpperCase(); + var results = [], h = Selector.handlers; + if (nodes) { + if (combinator) { + // fastlane for ordinary descendant combinators + if (combinator == "descendant") { + for (var i = 0, node; node = nodes[i]; i++) + h.concat(results, node.getElementsByTagName(tagName)); + return results; + } else nodes = this[combinator](nodes); + if (tagName == "*") return nodes; + } + for (var i = 0, node; node = nodes[i]; i++) + if (node.tagName.toUpperCase() === uTagName) results.push(node); + return results; + } else return root.getElementsByTagName(tagName); + }, + + id: function(nodes, root, id, combinator) { + var targetNode = $(id), h = Selector.handlers; + if (!targetNode) { + // IE doesn't find elements by ID if they're not attached to the + // document. + if (Prototype.Browser.IE && (root.sourceIndex < 1 || root === document)) { + var nodes = root.getElementsByTagName('*'); + for (var i = 0, node; node = nodes[i]; i++) { + if (node[id] = id) { + targetNode = node; break; + } + } if (!targetNode) return []; + } else return []; + } + + if (!nodes && root === document) return [targetNode]; + if (nodes) { + if (combinator) { + if (combinator == 'child') { + for (var i = 0, node; node = nodes[i]; i++) + if (targetNode.parentNode == node) return [targetNode]; + } else if (combinator == 'descendant') { + for (var i = 0, node; node = nodes[i]; i++) + if (Element.descendantOf(targetNode, node)) return [targetNode]; + } else if (combinator == 'adjacent') { + for (var i = 0, node; node = nodes[i]; i++) + if (Selector.handlers.previousElementSibling(targetNode) == node) + return [targetNode]; + } else nodes = h[combinator](nodes); + } + for (var i = 0, node; node = nodes[i]; i++) + if (node == targetNode) return [targetNode]; + return []; + } + return (targetNode && Element.descendantOf(targetNode, root)) ? [targetNode] : []; + }, + + className: function(nodes, root, className, combinator) { + if (nodes && combinator) nodes = this[combinator](nodes); + return Selector.handlers.byClassName(nodes, root, className); + }, + + byClassName: function(nodes, root, className) { + if (!nodes) nodes = Selector.handlers.descendant([root]); + var needle = ' ' + className + ' '; + for (var i = 0, results = [], node, nodeClassName; node = nodes[i]; i++) { + nodeClassName = node.className; + if (nodeClassName.length == 0) continue; + if (nodeClassName == className || (' ' + nodeClassName + ' ').include(needle)) + results.push(node); + } + return results; + }, + + attrPresence: function(nodes, root, attr, combinator) { + if (!nodes) nodes = root.getElementsByTagName("*"); + if (nodes && combinator) nodes = this[combinator](nodes); + var results = []; + for (var i = 0, node; node = nodes[i]; i++) + if (Element.hasAttribute(node, attr)) results.push(node); + return results; + }, + + attr: function(nodes, root, attr, value, operator, combinator) { + if (!nodes) nodes = root.getElementsByTagName("*"); + if (nodes && combinator) nodes = this[combinator](nodes); + var handler = Selector.operators[operator], results = []; + for (var i = 0, node; node = nodes[i]; i++) { + var nodeValue = Element.readAttribute(node, attr); + if (nodeValue === null) continue; + if (handler(nodeValue, value)) results.push(node); + } + return results; + }, + + pseudo: function(nodes, name, value, root, combinator) { + if (nodes && combinator) nodes = this[combinator](nodes); + if (!nodes) nodes = root.getElementsByTagName("*"); + return Selector.pseudos[name](nodes, value, root); + } + }, + + pseudos: { + 'first-child': function(nodes, value, root) { + for (var i = 0, results = [], node; node = nodes[i]; i++) { + if (Selector.handlers.previousElementSibling(node)) continue; + results.push(node); + } + return results; + }, + 'last-child': function(nodes, value, root) { + for (var i = 0, results = [], node; node = nodes[i]; i++) { + if (Selector.handlers.nextElementSibling(node)) continue; + results.push(node); + } + return results; + }, + 'only-child': function(nodes, value, root) { + var h = Selector.handlers; + for (var i = 0, results = [], node; node = nodes[i]; i++) + if (!h.previousElementSibling(node) && !h.nextElementSibling(node)) + results.push(node); + return results; + }, + 'nth-child': function(nodes, formula, root) { + return Selector.pseudos.nth(nodes, formula, root); + }, + 'nth-last-child': function(nodes, formula, root) { + return Selector.pseudos.nth(nodes, formula, root, true); + }, + 'nth-of-type': function(nodes, formula, root) { + return Selector.pseudos.nth(nodes, formula, root, false, true); + }, + 'nth-last-of-type': function(nodes, formula, root) { + return Selector.pseudos.nth(nodes, formula, root, true, true); + }, + 'first-of-type': function(nodes, formula, root) { + return Selector.pseudos.nth(nodes, "1", root, false, true); + }, + 'last-of-type': function(nodes, formula, root) { + return Selector.pseudos.nth(nodes, "1", root, true, true); + }, + 'only-of-type': function(nodes, formula, root) { + var p = Selector.pseudos; + return p['last-of-type'](p['first-of-type'](nodes, formula, root), formula, root); + }, + + // handles the an+b logic + getIndices: function(a, b, total) { + if (a == 0) return b > 0 ? [b] : []; + return $R(1, total).inject([], function(memo, i) { + if (0 == (i - b) % a && (i - b) / a >= 0) memo.push(i); + return memo; + }); + }, + + // handles nth(-last)-child, nth(-last)-of-type, and (first|last)-of-type + nth: function(nodes, formula, root, reverse, ofType) { + if (nodes.length == 0) return []; + if (formula == 'even') formula = '2n+0'; + if (formula == 'odd') formula = '2n+1'; + var h = Selector.handlers, results = [], indexed = [], m; + h.mark(nodes); + for (var i = 0, node; node = nodes[i]; i++) { + if (!node.parentNode._countedByPrototype) { + h.index(node.parentNode, reverse, ofType); + indexed.push(node.parentNode); + } + } + if (formula.match(/^\d+$/)) { // just a number + formula = Number(formula); + for (var i = 0, node; node = nodes[i]; i++) + if (node.nodeIndex == formula) results.push(node); + } else if (m = formula.match(/^(-?\d*)?n(([+-])(\d+))?/)) { // an+b + if (m[1] == "-") m[1] = -1; + var a = m[1] ? Number(m[1]) : 1; + var b = m[2] ? Number(m[2]) : 0; + var indices = Selector.pseudos.getIndices(a, b, nodes.length); + for (var i = 0, node, l = indices.length; node = nodes[i]; i++) { + for (var j = 0; j < l; j++) + if (node.nodeIndex == indices[j]) results.push(node); + } + } + h.unmark(nodes); + h.unmark(indexed); + return results; + }, + + 'empty': function(nodes, value, root) { + for (var i = 0, results = [], node; node = nodes[i]; i++) { + // IE treats comments as element nodes + if (node.tagName == '!' || node.firstChild) continue; + results.push(node); + } + return results; + }, + + 'not': function(nodes, selector, root) { + var h = Selector.handlers, selectorType, m; + var exclusions = new Selector(selector).findElements(root); + h.mark(exclusions); + for (var i = 0, results = [], node; node = nodes[i]; i++) + if (!node._countedByPrototype) results.push(node); + h.unmark(exclusions); + return results; + }, + + 'enabled': function(nodes, value, root) { + for (var i = 0, results = [], node; node = nodes[i]; i++) + if (!node.disabled && (!node.type || node.type !== 'hidden')) + results.push(node); + return results; + }, + + 'disabled': function(nodes, value, root) { + for (var i = 0, results = [], node; node = nodes[i]; i++) + if (node.disabled) results.push(node); + return results; + }, + + 'checked': function(nodes, value, root) { + for (var i = 0, results = [], node; node = nodes[i]; i++) + if (node.checked) results.push(node); + return results; + } + }, + + operators: { + '=': function(nv, v) { return nv == v; }, + '!=': function(nv, v) { return nv != v; }, + '^=': function(nv, v) { return nv == v || nv && nv.startsWith(v); }, + '$=': function(nv, v) { return nv == v || nv && nv.endsWith(v); }, + '*=': function(nv, v) { return nv == v || nv && nv.include(v); }, + '$=': function(nv, v) { return nv.endsWith(v); }, + '*=': function(nv, v) { return nv.include(v); }, + '~=': function(nv, v) { return (' ' + nv + ' ').include(' ' + v + ' '); }, + '|=': function(nv, v) { return ('-' + (nv || "").toUpperCase() + + '-').include('-' + (v || "").toUpperCase() + '-'); } + }, + + split: function(expression) { + var expressions = []; + expression.scan(/(([\w#:.~>+()\s-]+|\*|\[.*?\])+)\s*(,|$)/, function(m) { + expressions.push(m[1].strip()); + }); + return expressions; + }, + + matchElements: function(elements, expression) { + var matches = $$(expression), h = Selector.handlers; + h.mark(matches); + for (var i = 0, results = [], element; element = elements[i]; i++) + if (element._countedByPrototype) results.push(element); + h.unmark(matches); + return results; + }, + + findElement: function(elements, expression, index) { + if (Object.isNumber(expression)) { + index = expression; expression = false; + } + return Selector.matchElements(elements, expression || '*')[index || 0]; + }, + + findChildElements: function(element, expressions) { + expressions = Selector.split(expressions.join(',')); + var results = [], h = Selector.handlers; + for (var i = 0, l = expressions.length, selector; i < l; i++) { + selector = new Selector(expressions[i].strip()); + h.concat(results, selector.findElements(element)); + } + return (l > 1) ? h.unique(results) : results; + } +}); + +if (Prototype.Browser.IE) { + Object.extend(Selector.handlers, { + // IE returns comment nodes on getElementsByTagName("*"). + // Filter them out. + concat: function(a, b) { + for (var i = 0, node; node = b[i]; i++) + if (node.tagName !== "!") a.push(node); + return a; + }, + + // IE improperly serializes _countedByPrototype in (inner|outer)HTML. + unmark: function(nodes) { + for (var i = 0, node; node = nodes[i]; i++) + node.removeAttribute('_countedByPrototype'); + return nodes; + } + }); +} + +function $$() { + return Selector.findChildElements(document, $A(arguments)); +} +var Form = { + reset: function(form) { + $(form).reset(); + return form; + }, + + serializeElements: function(elements, options) { + if (typeof options !== 'object') options = { hash: !!options }; + else if (Object.isUndefined(options.hash)) options.hash = true; + + var key, value, type, isImageType, isSubmitButton, submitSerialized; + var submit = options.submit; + + var data = elements.inject({ }, function(result, element) { + element = $(element); + key = element.name; + value = element.getValue(); + type = element.type; + + isImageType = type === 'image'; + isSubmitButton = (type === 'submit' || isImageType); + + // Null values don't get serialized + if (value === null) return result; + // Disabled elements don't get serialized + if (element.disabled) return result; + // <input type="file|reset" /> doesn't get serialized + if (type === 'file' || type === 'reset') return result; + // Non-active submit buttons don't get serialized + if (isSubmitButton && + (submit === false || submitSerialized || + (submit && !(key === submit || element === submit)))) + return result; + + if (isSubmitButton) { + submitSerialized = true; + if (isImageType) { + var prefix = key ? key + '.' : '', + x = options.x || 0, y = options.y || 0; + + result[prefix + 'x'] = x; + result[prefix + 'y'] = y; + return result; + } + } + + else if (!key) return result; + + if (key in result) { + // a key is already present; construct an array of values + if (!Object.isArray(result[key])) result[key] = [result[key]]; + result[key].push(value); + } else result[key] = value; + + return result; + }); + + return options.hash ? data : Object.toQueryString(data); + } +}; + +Form.Methods = { + serialize: function(form, options) { + return Form.serializeElements(Form.getElements(form), options); + }, + + getElements: function(form) { + return $A($(form).getElementsByTagName('*')).inject([], + function(elements, child) { + if (Form.Element.Serializers[child.tagName.toLowerCase()]) + elements.push(Element.extend(child)); + return elements; + } + ); + }, + + getInputs: function(form, typeName, name) { + form = $(form); + var inputs = form.getElementsByTagName('input'); + + if (!typeName && !name) return $A(inputs).map(Element.extend); + + for (var i = 0, matchingInputs = [], length = inputs.length; i < length; i++) { + var input = inputs[i]; + if ((typeName && input.type != typeName) || (name && input.name != name)) + continue; + matchingInputs.push(Element.extend(input)); + } + + return matchingInputs; + }, + + disable: function(form) { + form = $(form); + Form.getElements(form).invoke('disable'); + return form; + }, + + enable: function(form) { + form = $(form); + Form.getElements(form).invoke('enable'); + return form; + }, + + findFirstElement: function(form) { + var elements = $(form).getElements().findAll(function(element) { + return 'hidden' != element.type && !element.disabled; + }); + var firstByIndex = elements.findAll(function(element) { + return element.hasAttribute('tabIndex') && element.tabIndex >= 0; + }).sortBy(function(element) { return element.tabIndex }).first(); + + return firstByIndex ? firstByIndex : elements.find(function(element) { + return ['button', 'input', 'select', 'textarea'].include(element.tagName.toLowerCase()); + }); + }, + + focusFirstElement: function(form) { + form = $(form); + form.findFirstElement().activate(); + return form; + }, + + request: function(form, options) { + form = $(form), options = Object.clone(options || { }); + + var params = options.parameters, action = form.readAttribute('action') || ''; + if (action.blank()) action = window.location.href; + options.parameters = form.serialize(true); + + if (params) { + if (Object.isString(params)) params = params.toQueryParams(); + Object.extend(options.parameters, params); + } + + if (form.hasAttribute('method') && !options.method) + options.method = form.method; + + return new Ajax.Request(action, options); + } +}; + +/*--------------------------------------------------------------------------*/ + +Form.Element = { + focus: function(element) { + $(element).focus(); + return element; + }, + + select: function(element) { + $(element).select(); + return element; + } +}; + +Form.Element.Methods = { + serialize: function(element) { + element = $(element); + if (!element.disabled && element.name) { + var value = element.getValue(); + if (value != undefined) { + var pair = { }; + pair[element.name] = value; + return Object.toQueryString(pair); + } + } + return ''; + }, + + getValue: function(element) { + if (!(element = $(element))) return null; + var method = element.tagName.toLowerCase(), s = Form.Element.Serializers; + return s[method]? s[method](element) : null; + }, + + setValue: function(element, value) { + if (!(element = $(element))) return null; + var method = element.tagName.toLowerCase(), s = Form.Element.Serializers; + if (s[method]) s[method](element, value); + return element; + }, + + clear: function(element) { + $(element).value = ''; + return element; + }, + + present: function(element) { + return $(element).value != ''; + }, + + activate: function(element) { + element = $(element); + try { + element.focus(); + if (element.select && (element.tagName.toLowerCase() != 'input' || + !['button', 'image', 'reset', 'submit'].include(element.type))) + element.select(); + } catch (e) { } + return element; + }, + + disable: function(element) { + element = $(element); + element.disabled = true; + return element; + }, + + enable: function(element) { + element = $(element); + element.disabled = false; + return element; + } +}; + +/*--------------------------------------------------------------------------*/ + +var Field = Form.Element; +var $F = Form.Element.Methods.getValue; + +/*--------------------------------------------------------------------------*/ + +Form.Element.Serializers = { + input: function(element, value) { + switch (element.type.toLowerCase()) { + case 'checkbox': + case 'radio': + return Form.Element.Serializers.inputSelector(element, value); + default: + return Form.Element.Serializers.textarea(element, value); + } + }, + + inputSelector: function(element, value) { + if (Object.isUndefined(value)) return element.checked ? element.value : null; + else element.checked = !!value; + }, + + button: function(element, value){ + if (Object.isUndefined(value)) return element.innerHTML; + else element.innerHTML = value; + }, + + textarea: function(element, value) { + if (Object.isUndefined(value)) return element.value; + else element.value = value; + }, + + select: function(element, index) { + if (Object.isUndefined(index)) + return this[element.type == 'select-one' ? + 'selectOne' : 'selectMany'](element); + else { + var opt, value, single = !Object.isArray(index); + for (var i = 0, length = element.length; i < length; i++) { + opt = element.options[i]; + value = this.optionValue(opt); + if (single) { + if (value == index) { + opt.selected = true; + return; + } + } + else opt.selected = index.include(value); + } + } + }, + + selectOne: function(element) { + var index = element.selectedIndex; + return index >= 0 ? this.optionValue(element.options[index]) : null; + }, + + selectMany: function(element) { + var values, length = element.length; + if (!length) return null; + + for (var i = 0, values = []; i < length; i++) { + var opt = element.options[i]; + if (opt.selected) values.push(this.optionValue(opt)); + } + return values; + }, + + optionValue: function(opt) { + // extend element because hasAttribute may not be native + return Element.extend(opt).hasAttribute('value') ? opt.value : opt.text; + } +}; + +/*--------------------------------------------------------------------------*/ + +Abstract.TimedObserver = Class.create(PeriodicalExecuter, { + initialize: function($super, element, frequency, callback) { + $super(callback, frequency); + this.element = $(element); + this.lastValue = this.getValue(); + }, + + execute: function() { + var value = this.getValue(); + if (Object.isString(this.lastValue) && Object.isString(value) ? + this.lastValue != value : String(this.lastValue) != String(value)) { + this.callback(this.element, value); + this.lastValue = value; + } + } +}); + +Form.Element.Observer = Class.create(Abstract.TimedObserver, { + getValue: function() { + return Form.Element.getValue(this.element); + } +}); + +Form.Observer = Class.create(Abstract.TimedObserver, { + getValue: function() { + return Form.serialize(this.element); + } +}); + +/*--------------------------------------------------------------------------*/ + +Abstract.EventObserver = Class.create({ + initialize: function(element, callback) { + this.element = $(element); + this.callback = callback; + + this.lastValue = this.getValue(); + if (this.element.tagName.toLowerCase() == 'form') + this.registerFormCallbacks(); + else + this.registerCallback(this.element); + }, + + onElementEvent: function() { + var value = this.getValue(); + if (this.lastValue != value) { + this.callback(this.element, value); + this.lastValue = value; + } + }, + + registerFormCallbacks: function() { + Form.getElements(this.element).each(this.registerCallback, this); + }, + + registerCallback: function(element) { + if (element.type) { + switch (element.type.toLowerCase()) { + case 'checkbox': + case 'radio': + Event.observe(element, 'click', this.onElementEvent.bind(this)); + break; + default: + Event.observe(element, 'change', this.onElementEvent.bind(this)); + break; + } + } + } +}); + +Form.Element.EventObserver = Class.create(Abstract.EventObserver, { + getValue: function() { + return Form.Element.getValue(this.element); + } +}); + +Form.EventObserver = Class.create(Abstract.EventObserver, { + getValue: function() { + return Form.serialize(this.element); + } +}); +if (!window.Event) var Event = { }; + +Object.extend(Event, { + KEY_BACKSPACE: 8, + KEY_TAB: 9, + KEY_RETURN: 13, + KEY_ESC: 27, + KEY_LEFT: 37, + KEY_UP: 38, + KEY_RIGHT: 39, + KEY_DOWN: 40, + KEY_DELETE: 46, + KEY_HOME: 36, + KEY_END: 35, + KEY_PAGEUP: 33, + KEY_PAGEDOWN: 34, + KEY_INSERT: 45, + + cache: { }, + + relatedTarget: function(event) { + var element; + switch(event.type) { + case 'mouseover': element = event.fromElement; break; + case 'mouseout': element = event.toElement; break; + default: return null; + } + return Element.extend(element); + } +}); + +Event.Methods = (function() { + var isButton; + + if (Prototype.Browser.IE) { + var buttonMap = { 0: 1, 1: 4, 2: 2 }; + isButton = function(event, code) { + return event.button == buttonMap[code]; + }; + + } else if (Prototype.Browser.WebKit) { + isButton = function(event, code) { + switch (code) { + case 0: return event.which == 1 && !event.metaKey; + case 1: return event.which == 1 && event.metaKey; + default: return false; + } + }; + + } else { + isButton = function(event, code) { + return event.which ? (event.which === code + 1) : (event.button === code); + }; + } + + return { + isLeftClick: function(event) { return isButton(event, 0) }, + isMiddleClick: function(event) { return isButton(event, 1) }, + isRightClick: function(event) { return isButton(event, 2) }, + + element: function(event) { + event = Event.extend(event); + var node = event.target, currentTarget = event.currentTarget, type = event.type; + + if (currentTarget && currentTarget.tagName) { + // Firefox screws up the "click" event when moving between radio buttons + // via arrow keys. It also screws up the "load" and "error" events on images, + // reporting the document as the target instead of the original image. + if (['load', 'error'].include(type) || + (currentTarget.tagName.toUpperCase() === "INPUT" && currentTarget.type === "radio" && type === "click")) + node = currentTarget; + } + + return Element.extend(node && node.nodeType == Node.TEXT_NODE ? + node.parentNode : node); + }, + + findElement: function(event, expression) { + var element = Event.element(event); + if (!expression) return element; + var elements = [element].concat(element.ancestors()); + return Selector.findElement(elements, expression, 0); + }, + + pointer: function(event) { + var docElement = document.documentElement, + body = document.body || { scrollLeft: 0, scrollTop: 0 }; + return { + x: event.pageX || (event.clientX + + (docElement.scrollLeft || body.scrollLeft) - + (docElement.clientLeft || 0)), + y: event.pageY || (event.clientY + + (docElement.scrollTop || body.scrollTop) - + (docElement.clientTop || 0)) + }; + }, + + pointerX: function(event) { return Event.pointer(event).x }, + pointerY: function(event) { return Event.pointer(event).y }, + + stop: function(event) { + Event.extend(event); + event.preventDefault(); + event.stopPropagation(); + event.stopped = true; + } + }; +})(); + +Event.extend = (function() { + var methods = Object.keys(Event.Methods).inject({ }, function(m, name) { + m[name] = Event.Methods[name].methodize(); + return m; + }); + + if (Prototype.Browser.IE) { + Object.extend(methods, { + stopPropagation: function() { this.cancelBubble = true }, + preventDefault: function() { this.returnValue = false }, + inspect: function() { return "[object Event]" } + }); + + return function(event) { + if (!event) return false; + if (event._extendedByPrototype) return event; + + var pointer = Event.pointer(event); + Object.extend(event, { + _extendedByPrototype: Prototype.emptyFunction, + target: Element.extend(event.srcElement), + relatedTarget: Event.relatedTarget(event), + pageX: pointer.x, + pageY: pointer.y + }); + return Object.extend(event, methods); + }; + + } else { + Event.prototype = Event.prototype || document.createEvent("HTMLEvents")['__proto__']; + Object.extend(Event.prototype, methods); + return Prototype.K; + } +})(); + +Object.extend(Event, (function() { + var cache = Event.cache; + + function getEventID(element) { + // Event ID is stored as the 0th index in a one-item array so that it + // won't get copied to a new node when cloneNode is called. + if (element === window) return 1; + if (element._prototypeEventID) return element._prototypeEventID[0]; + return element._prototypeEventID = [arguments.callee.id++]; + } + getEventID.id = 2; + + function getDOMEventName(eventName) { + if (eventName && eventName.include(':')) return "dataavailable"; + return eventName; + } + + function getCacheForID(id) { + return cache[id] = cache[id] || { }; + } + + function addEventDispatcher(element, eventName, dispatchWrapper) { + var id = getEventID(element), wrappers = getWrappersForEventName(id, eventName); + if (wrappers.dispatcher) return; + + wrappers.dispatcher = function(event) { + var w = getWrappersForEventName(id, eventName); + for(var i = 0, l = w.length; i < l; i++) w[i](event); // execute wrappers + }; + + if (dispatchWrapper) wrappers.dispatcher = wrappers.dispatcher.wrap(dispatchWrapper); + element.attachEvent("on" + getDOMEventName(eventName), wrappers.dispatcher); + } + + function getWrappersForEventName(id, eventName) { + var c = getCacheForID(id); + return c[eventName] = c[eventName] || []; + } + + function createWrapper(element, eventName, handler) { + var id = getEventID(element), c = getCacheForID(id); + + // Attach the element itself onto its cache entry so we can retrieve it for + // cleanup on page unload. + if (!c.element) c.element = element; + + var w = getWrappersForEventName(id, eventName); + if (w.pluck("handler").include(handler)) return false; + + var wrapper = function(event) { + if (!Event || !Event.extend || + (event.eventName && event.eventName != eventName)) + return false; + + handler.call(element, Event.extend(event)); + }; + + wrapper.handler = handler; + w.push(wrapper); + return wrapper; + } + + function findWrapper(id, eventName, handler) { + var w = getWrappersForEventName(id, eventName); + return w.find(function(wrapper) { return wrapper.handler == handler }); + } + + function destroyWrapper(id, eventName, handler) { + var c = getCacheForID(id); + if (!c[eventName]) return false; + var d = c[eventName].dispatcher; + c[eventName] = c[eventName].without(findWrapper(id, eventName, handler)); + c[eventName].dispatcher = d; + } + + // Loop through all elements and remove all handlers on page unload. IE + // needs this in order to prevent memory leaks. + function purgeListeners() { + var element, entry; + for (var i in Event.cache) { + entry = Event.cache[i]; + Event.stopObserving(entry.element); + entry.element = null; + } + } + + function onStop() { + document.detachEvent("onstop", onStop); + purgeListeners(); + } + + function onBeforeUnload() { + if (document.readyState === "interactive") { + document.attachEvent("onstop", onStop); + (function() { document.detachEvent("onstop", onStop); }).defer(); + } + } + + if (window.attachEvent && !window.addEventListener) { + // Internet Explorer needs to remove event handlers on page unload + // in order to avoid memory leaks. + window.attachEvent("onunload", purgeListeners); + + // IE also doesn't fire the unload event if the page is navigated away + // from before it's done loading. Workaround adapted from + // http://blog.moxiecode.com/2008/04/08/unload-event-never-fires-in-ie/. + window.attachEvent("onbeforeunload", onBeforeUnload); + + // Ensure window onload is fired after "dom:loaded" + addEventDispatcher(window, 'load', function(proceed, event) { + if (document.loaded) { + proceed(event); + } else { + arguments.callee.defer(proceed, event); + } + }); + + // Ensure window onresize is fired only once per resize + addEventDispatcher(window, 'resize', function(proceed, event) { + var callee = arguments.callee, dimensions = document.viewport.getDimensions(); + if (dimensions.width != callee.prevWidth || dimensions.height != callee.prevHeight) { + callee.prevWidth = dimensions.width; + callee.prevHeight = dimensions.height; + proceed(event); + } + }); + } + + // Safari has a dummy event handler on page unload so that it won't + // use its bfcache. Safari <= 3.1 has an issue with restoring the "document" + // object when page is returned to via the back button using its bfcache. + else if (Prototype.Browser.WebKit) { + window.addEventListener("unload", Prototype.emptyFunction, false); + } + + return { + observe: function(element, eventName, handler) { + element = $(element); + var name = getDOMEventName(eventName); + + var wrapper = createWrapper(element, eventName, handler); + if (!wrapper) return element; + + if (element.addEventListener) { + element.addEventListener(name, wrapper, false); + } else { + addEventDispatcher(element, eventName); + } + + return element; + }, + + stopObserving: function(element, eventName, handler) { + element = $(element); + eventName = Object.isString(eventName) ? eventName : null; + var id = getEventID(element), c = cache[id]; + + if (!c) { + return element; + } + else if (!handler && eventName) { + getWrappersForEventName(id, eventName).each(function(wrapper) { + Event.stopObserving(element, eventName, wrapper.handler); + }); + return element; + } + else if (!eventName) { + Object.keys(c).without("element").each(function(eventName) { + Event.stopObserving(element, eventName); + }); + return element; + } + + var wrapper = findWrapper(id, eventName, handler); + if (!wrapper) return element; + + var name = getDOMEventName(eventName); + if (element.removeEventListener) { + element.removeEventListener(name, wrapper, false); + destroyWrapper(id, eventName, handler); + } else { + destroyWrapper(id, eventName, handler); + var wrappers = getWrappersForEventName(id, eventName); + if (!wrappers.length) { + element.detachEvent("on" + name, wrappers.dispatcher); + wrappers.dispatcher = null; + } + } + + return element; + }, + + fire: function(element, eventName, memo) { + element = $(element); + if (element == document && document.createEvent && !element.dispatchEvent) + element = document.documentElement; + + var event; + if (document.createEvent) { + event = document.createEvent("HTMLEvents"); + event.initEvent("dataavailable", true, true); + } else { + event = document.createEventObject(); + event.eventType = "ondataavailable"; + } + + event.eventName = eventName; + event.memo = memo || { }; + + if (document.createEvent) { + element.dispatchEvent(event); + } else { + element.fireEvent(event.eventType, event); + } + + return Event.extend(event); + } + }; +})()); + +Object.extend(Event, Event.Methods); + +Element.addMethods({ + fire: Event.fire, + observe: Event.observe, + stopObserving: Event.stopObserving +}); + +Object.extend(document, { + fire: Element.Methods.fire.methodize(), + observe: Element.Methods.observe.methodize(), + stopObserving: Element.Methods.stopObserving.methodize(), + loaded: false +}); + +(function() { + /* Support for the DOMContentLoaded event is based on work by Dan Webb, + Matthias Miller, Dean Edwards, John Resig and Diego Perini. */ + + var timer; + + function fireContentLoadedEvent() { + if (document.loaded) return; + if (timer) window.clearInterval(timer); + document.loaded = true; + document.fire("dom:loaded"); + } + + if (document.addEventListener) { + document.addEventListener("DOMContentLoaded", fireContentLoadedEvent, false); + } else { + document.attachEvent("onreadystatechange", function() { + if (document.readyState == "complete") { + document.detachEvent("onreadystatechange", arguments.callee); + fireContentLoadedEvent(); + } + }); + + if (window == top) { + timer = setInterval(function() { + try { + document.documentElement.doScroll("left"); + } catch(e) { return } + fireContentLoadedEvent(); + }, 10); + } + } + + + // Safari <3.1 doesn't support DOMContentLoaded + if (Prototype.Browser.WebKit && (navigator.userAgent.match(/AppleWebKit\/(\d+)/)[1] < 525)) { + timer = setInterval(function() { + if (/loaded|complete/.test(document.readyState)) + fireContentLoadedEvent(); + }, 10); + } + + // Worst case fallback... + Event.observe(window, "load", fireContentLoadedEvent); +})(); +/*------------------------------- DEPRECATED -------------------------------*/ + +Hash.toQueryString = Object.toQueryString; + +var Toggle = { display: Element.toggle }; + +Element.Methods.childOf = Element.Methods.descendantOf; + +var Insertion = { + Before: function(element, content) { + return Element.insert(element, {before:content}); + }, + + Top: function(element, content) { + return Element.insert(element, {top:content}); + }, + + Bottom: function(element, content) { + return Element.insert(element, {bottom:content}); + }, + + After: function(element, content) { + return Element.insert(element, {after:content}); + } +}; + +var $continue = new Error('"throw $continue" is deprecated, use "return" instead'); + +// This should be moved to script.aculo.us; notice the deprecated methods +// further below, that map to the newer Element methods. +var Position = { + // set to true if needed, warning: firefox performance problems + // NOT neeeded for page scrolling, only if draggable contained in + // scrollable elements + includeScrollOffsets: false, + + // must be called before calling withinIncludingScrolloffset, every time the + // page is scrolled + prepare: function() { + this.deltaX = window.pageXOffset + || document.documentElement.scrollLeft + || document.body.scrollLeft + || 0; + this.deltaY = window.pageYOffset + || document.documentElement.scrollTop + || document.body.scrollTop + || 0; + }, + + // caches x/y coordinate pair to use with overlap + within: function(element, x, y) { + if (this.includeScrollOffsets) + return this.withinIncludingScrolloffsets(element, x, y); + + var dimensions = Element.getDimensions(element); + this.xcomp = x; + this.ycomp = y; + this.offset = Element.cumulativeOffset(element); + + return (y >= this.offset[1] && + y < this.offset[1] + dimensions.height && + x >= this.offset[0] && + x < this.offset[0] + dimensions.width); + }, + + withinIncludingScrolloffsets: function(element, x, y) { + var offsetcache = Element.cumulativeScrollOffset(element), + dimensions = Element.getDimensions(element); + + this.xcomp = x + offsetcache[0] - this.deltaX; + this.ycomp = y + offsetcache[1] - this.deltaY; + this.offset = Element.cumulativeOffset(element); + + return (this.ycomp >= this.offset[1] && + this.ycomp < this.offset[1] + dimensions.height && + this.xcomp >= this.offset[0] && + this.xcomp < this.offset[0] + dimensions.width); + }, + + // within must be called directly before + overlap: function(mode, element) { + var dimensions = Element.getDimensions(element); + if (!mode) return 0; + if (mode == 'vertical') + return ((this.offset[1] + dimensions.height) - this.ycomp) / + dimensions.height; + if (mode == 'horizontal') + return ((this.offset[0] + dimensions.width) - this.xcomp) / + dimensions.width; + }, + + // Deprecation layer -- use newer Element methods now (1.5.2). + + cumulativeOffset: Element.Methods.cumulativeOffset, + + positionedOffset: Element.Methods.positionedOffset, + + absolutize: function(element) { + Position.prepare(); + return Element.absolutize(element); + }, + + relativize: function(element) { + Position.prepare(); + return Element.relativize(element); + }, + + realOffset: Element.Methods.cumulativeScrollOffset, + + offsetParent: Element.Methods.getOffsetParent, + + page: Element.Methods.viewportOffset, + + clone: function(source, target, options) { + options = options || { }; + return Element.clonePosition(target, source, options); + } +}; + +/*--------------------------------------------------------------------------*/ + +if (!document.getElementsByClassName) document.getElementsByClassName = function(instanceMethods){ + function iter(name) { + return name.blank() ? null : "[contains(concat(' ', @class, ' '), ' " + name + " ')]"; + } + + instanceMethods.getElementsByClassName = Prototype.BrowserFeatures.XPath ? + function(element, className) { + className = className.toString().strip(); + var cond = /\s/.test(className) ? $w(className).map(iter).join('') : iter(className); + return cond ? document._getElementsByXPath('.//*' + cond, element) : []; + } : function(element, className) { + className = className.toString().strip(); + var elements = [], classNames = (/\s/.test(className) ? $w(className) : null); + if (!classNames && !className) return elements; + + var nodes = $(element).getElementsByTagName('*'); + className = ' ' + className + ' '; + + for (var i = 0, child, cn; child = nodes[i]; i++) { + if (child.className && (cn = ' ' + child.className + ' ') && (cn.include(className) || + (classNames && classNames.all(function(name) { + return !name.toString().blank() && cn.include(' ' + name + ' '); + })))) + elements.push(Element.extend(child)); + } + return elements; + }; + + return function(className, parentElement) { + return $(parentElement || document.body).getElementsByClassName(className); + }; +}(Element.Methods); + +/*--------------------------------------------------------------------------*/ + +Element.ClassNames = Class.create(); +Element.ClassNames.prototype = { + initialize: function(element) { + this.element = $(element); + }, + + _each: function(iterator) { + this.element.className.split(/\s+/).select(function(name) { + return name.length > 0; + })._each(iterator); + }, + + set: function(className) { + this.element.className = className; + }, + + add: function(classNameToAdd) { + if (this.include(classNameToAdd)) return; + this.set($A(this).concat(classNameToAdd).join(' ')); + }, + + remove: function(classNameToRemove) { + if (!this.include(classNameToRemove)) return; + this.set($A(this).without(classNameToRemove).join(' ')); + }, + + toString: function() { + return $A(this).join(' '); + } +}; + +Object.extend(Element.ClassNames.prototype, Enumerable); + +/*--------------------------------------------------------------------------*/ + +Element.addMethods();
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/dist/prototype_update_helper.js b/dom/tests/mochitest/ajax/prototype/dist/prototype_update_helper.js new file mode 100644 index 0000000000..0f4b949381 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/dist/prototype_update_helper.js @@ -0,0 +1,417 @@ +/* Update Helper (c) 2008 Tobie Langel + * + * Requires Prototype >= 1.6.0 + * + * Update Helper is distributable under the same terms as Prototype + * (MIT-style license). For details, see the Prototype web site: + * http://www.prototypejs.org/ + * + *--------------------------------------------------------------------------*/ + +var UpdateHelper = Class.create({ + logLevel: 0, + MessageTemplate: new Template('Update Helper: #{message}\n#{stack}'), + Regexp: new RegExp("@" + window.location.protocol + ".*?\\d+\\n", "g"), + + initialize: function(deprecatedMethods) { + var notify = function(message, type) { + this.notify(message, type); + }.bind(this); // Late binding to simplify testing. + + deprecatedMethods.each(function(d) { + var condition = d.condition, + type = d.type || 'info', + message = d.message, + namespace = d.namespace, + method = d.methodName; + + namespace[method] = (namespace[method] || function() {}).wrap(function(proceed) { + var arr = $A(arguments); + var args = arr.splice(1, arr.length); + if (!condition || condition.apply(this, args)) notify(message, type); + return proceed.apply(proceed, args); + }); + }); + Element.addMethods(); + }, + + notify: function(message, type) { + switch(type) { + case 'info': + if (this.logLevel > UpdateHelper.Info) return false; + case 'warn': + if (this.logLevel > UpdateHelper.Warn) return false; + default: + if (this.logLevel > UpdateHelper.Error) return false; + } + this.log(this.MessageTemplate.evaluate({ + message: message, + stack: this.getStack() + }), type); + return true; + }, + + getStack: function() { + try { + throw new Error("stack"); + } catch(e) { + return (e.stack || '').match(this.Regexp).reject(function(path) { + return /(prototype|unittest|update_helper)\.js/.test(path); + }).join("\n"); + } + }, + + log: function(message, type) { + if (type == 'error') console.error(message); + else if (type == 'warn') console.warn(message); + else console.log(message); + } +}); + +Object.extend(UpdateHelper, { + Info: 0, + Warn: 1, + Error: 2 +}); + +/* UpdateHelper for Prototype 1.6.0.2 (c) 2008 Tobie Langel + * + * UpdateHelper for Prototype is freely distributable under the same + * terms as Prototype (MIT-style license). + * For details, see the Prototype web site: http://www.prototypejs.org/ + * + * Include this file right below prototype.js. All messages + * will be logged to the console. + * + * Note: You can tune the level of warning by redefining + * prototypeUpdateHelper.logLevel with one of the appropriate constansts + * (UpdateHelper.Info, UpdateHelper.Warn or UpdateHelper.Error), e.g.: + * + * prototypeUpdateHelper.logLevel = UpdateHelper.Warn; + * + * This, for example, will prevent deprecation messages from being logged. + * + * THIS SCRIPT WORKS IN FIREFOX ONLY + *--------------------------------------------------------------------------*/ + +var prototypeUpdateHelper = new UpdateHelper([ + { + methodName: 'display', + namespace: Toggle, + message: 'Toggle.display has been deprecated, please use Element.toggle instead.' + }, + + { + methodName: 'show', + namespace: Element.Methods, + message: 'Passing an arbitrary number of elements to Element.show is no longer supported.\n' + + 'Use [id_1, id_2, ...].each(Element.show) or $(id_1, id_2, ...).invoke("show") instead.', + type: 'error', + condition: function() { return arguments.length > 1 && !Object.isNumber(arguments[1]) } + }, + + { + methodName: 'hide', + namespace: Element.Methods, + message: 'Passing an arbitrary number of elements to Element.hide is no longer supported.\n' + + 'Use [id_1, id_2, ...].each(Element.hide) or $(id_1, id_2, ...).invoke("hide") instead.', + type: 'error', + condition: function() { return arguments.length > 1 && !Object.isNumber(arguments[1]) } + }, + + { + methodName: 'toggle', + namespace: Element.Methods, + message: 'Passing an arbitrary number of elements to Element.toggle is no longer supported.\n' + + 'Use [id_1, id_2, ...].each(Element.toggle) or $(id_1, id_2, ...).invoke("toggle") instead.', + type: 'error', + condition: function() { return arguments.length > 1 && !Object.isNumber(arguments[1]) } + }, + + { + methodName: 'clear', + namespace: Form.Element.Methods, + message: 'Passing an arbitrary number of elements to Field.clear is no longer supported.\n' + + 'Use [id_1, id_2, ...].each(Form.Element.clear) or $(id_1, id_2, ...).invoke("clear") instead.', + type: 'error', + condition: function() { return arguments.length > 1 && !Object.isNumber(arguments[1]) } + }, + + { + methodName: 'present', + namespace: Form.Element.Methods, + message: 'Passing an arbitrary number of elements to Field.present is no longer supported.\n' + + 'Use [id_1, id_2, ...].each(Form.Element.present) or $(id_1, id_2, ...).invoke("present") instead.', + type: 'error', + condition: function() { return arguments.length > 1 && !Object.isNumber(arguments[1]) } + }, + + { + methodName: 'childOf', + namespace: Element.Methods, + message: 'Element#childOf has been deprecated, please use Element#descendantOf instead.' + }, + + { + methodName: 'Before', + namespace: Insertion, + message: 'Insertion.Before has been deprecated, please use Element#insert instead.' + }, + + { + methodName: 'Top', + namespace: Insertion, + message: 'Insertion.Top has been deprecated, please use Element#insert instead.' + }, + + { + methodName: 'Bottom', + namespace: Insertion, + message: 'Insertion.Bottom has been deprecated, please use Element#insert instead.' + }, + + { + methodName: 'After', + namespace: Insertion, + message: 'Insertion.After has been deprecated, please use Element#insert instead.' + }, + + { + methodName: 'prepare', + namespace: Position, + message: 'Position.prepare has been deprecated.' + }, + + { + methodName: 'within', + namespace: Position, + message: 'Position.within has been deprecated.' + }, + + { + methodName: 'withinIncludingScrolloffsets', + namespace: Position, + message: 'Position.withinIncludingScrolloffsets has been deprecated.' + }, + + { + methodName: 'overlap', + namespace: Position, + message: 'Position.overlap has been deprecated.' + }, + + { + methodName: 'cumulativeOffset', + namespace: Position, + message: 'Position.cumulativeOffset has been deprecated, please use Element#cumulativeOffset instead.' + }, + + { + methodName: 'positionedOffset', + namespace: Position, + message: 'Position.positionedOffset has been deprecated, please use Element#positionedOffset instead.' + }, + + { + methodName: 'absolutize', + namespace: Position, + message: 'Position.absolutize has been deprecated, please use Element#absolutize instead.' + }, + + { + methodName: 'relativize', + namespace: Position, + message: 'Position.relativize has been deprecated, please use Element#relativize instead.' + }, + + { + methodName: 'realOffset', + namespace: Position, + message: 'Position.realOffset has been deprecated, please use Element#cumulativeScrollOffset instead.' + }, + + { + methodName: 'offsetParent', + namespace: Position, + message: 'Position.offsetParent has been deprecated, please use Element#getOffsetParent instead.' + }, + + { + methodName: 'page', + namespace: Position, + message: 'Position.page has been deprecated, please use Element#viewportOffset instead.' + }, + + { + methodName: 'clone', + namespace: Position, + message: 'Position.clone has been deprecated, please use Element#clonePosition instead.' + }, + + { + methodName: 'initialize', + namespace: Element.ClassNames.prototype, + message: 'Element.ClassNames has been deprecated.' + }, + + { + methodName: 'classNames', + namespace: Element.Methods, + message: 'Element#classNames has been deprecated.\n' + + 'If you need to access CSS class names as an array, try: $w(element.classname).' + }, + + { + methodName: 'setStyle', + namespace: Element.Methods, + message: 'Use of uncamelized style-property names is no longer supported.\n' + + 'Use either camelized style-property names or a regular CSS string instead (see online documentation).', + type: 'error', + condition: function(element, style) { + return !Object.isString(style) && Object.keys(style).join('').include('-'); + } + }, + + { + methodName: 'getElementsByClassName', + namespace: document, + message: 'document.getElementsByClassName has been deprecated, please use $$ instead.' + }, + + { + methodName: 'getElementsByClassName', + namespace: Element.Methods, + message: 'Element#getElementsByClassName has been deprecated, please use Element#select instead.' + }, + + { + methodName: 'immediateDescendants', + namespace: Element.Methods, + message: 'Element#immediateDescendants has been deprecated, please use Element#childElements instead.' + }, + + { + methodName: 'getElementsBySelector', + namespace: Element.Methods, + message: 'Element#getElementsBySelector has been deprecated, please use Element#select instead.' + }, + + { + methodName: 'toQueryString', + namespace: Hash, + message: 'Hash.toQueryString has been deprecated.\n' + + 'Use the instance method Hash#toQueryString or Object.toQueryString instead.' + }, + + { + methodName: 'toJSON', + namespace: Hash, + message: 'Hash.toJSON has been removed.\n' + + 'Use the instance method Hash#toJSON or Object.toJSON instead.', + type: 'error' + }, + + { + methodName: 'remove', + namespace: Hash.prototype, + message: 'Hash#remove is no longer supported, use Hash#unset instead.\n' + + 'Please note that Hash#unset only accepts one argument.', + type: 'error' + }, + + { + methodName: 'merge', + namespace: Hash.prototype, + message: 'Hash#merge is no longer destructive and now operates on a clone of the Hash instance.\n' + + 'If you need a destructive merge, use Hash#update instead.', + type: 'warn' + }, + + { + methodName: 'unloadCache', + namespace: Event, + message: 'Event.unloadCache has been deprecated.', + type: 'error' + }, + + { + methodName: 'create', + namespace: Class, + message: 'The class API has been fully revised and now allows for mixins and inheritance.\n' + + 'You can find more about it here: http://prototypejs.org/learn/class-inheritance', + condition: function() { return !arguments.length } + } +]); + +// Special casing for Hash. + +(function() { + var __properties = Object.keys(Hash.prototype).concat(['_object', '__properties']); + + var messages = { + setting: new Template("Directly setting a property on an instance of Hash is no longer supported.\n" + + "Please use Hash#set('#{property}', #{value}) instead."), + getting: new Template("Directly accessing a property of an instance of Hash is no longer supported.\n" + + "Please use Hash#get('#{property}') instead.") + }; + + function notify(property, value) { + var message = messages[arguments.length == 1 ? 'getting' : 'setting'].evaluate({ + property: property, + value: Object.inspect(value) + }); + prototypeUpdateHelper.notify(message, 'error'); + } + + function defineSetters(obj, prop) { + if (obj.__properties.include(prop)) return; + obj.__properties.push(prop); + obj.__defineGetter__(prop, function() { + checkProperties(this); + notify(prop); + }); + obj.__defineSetter__(prop, function(value) { + checkProperties(this); + notify(prop, value); + }); + } + + function checkProperties(hash) { + var current = Object.keys(hash); + if (current.length == hash.__properties.length) + return; + current.each(function(prop) { + if (hash.__properties.include(prop)) return; + notify(prop, hash[prop]); + defineSetters(hash, prop); + }); + } + + Hash.prototype.set = Hash.prototype.set.wrap(function(proceed, property, value) { + defineSetters(this, property); + return proceed(property, value); + }); + + $w('merge update').each(function(name) { + Hash.prototype[name] = Hash.prototype[name].wrap(function(proceed, object) { + for (var prop in object) defineSetters(this, prop); + return proceed(object); + }); + }); + + $H(Hash.prototype).each(function(method) { + var key = method.key; + if (!Object.isFunction(method.value) || key == 'initialize') return; + Hash.prototype[key] = Hash.prototype[key].wrap(function(proceed) { + checkProperties(this); + var arr = $A(arguments); + return proceed.apply(proceed, arr.splice(1, arr.length)); + }); + }); + + Hash.prototype.initialize = Hash.prototype.initialize.wrap(function(proceed, object) { + this.__properties = __properties.clone(); + for (var prop in object) defineSetters(this, prop); + proceed(object); + }); +})(); diff --git a/dom/tests/mochitest/ajax/prototype/manifest.json b/dom/tests/mochitest/ajax/prototype/manifest.json new file mode 100644 index 0000000000..1beb8a7079 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/manifest.json @@ -0,0 +1 @@ +{testcases:["test/unit/tmp/ajax_test.html","test/unit/tmp/array_test.html","test/unit/tmp/base_test.html","test/unit/tmp/dom_test.html","test/unit/tmp/element_mixins_test.html","test/unit/tmp/enumerable_test.html","test/unit/tmp/event_test.html","test/unit/tmp/form_test.html","test/unit/tmp/hash_test.html","test/unit/tmp/number_test.html","test/unit/tmp/position_test.html","test/unit/tmp/range_test.html","test/unit/tmp/selector_test.html","test/unit/tmp/string_test.html", "test/unit/tmp/unit_test.html"]}
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/mochitest.ini b/dom/tests/mochitest/ajax/prototype/mochitest.ini new file mode 100644 index 0000000000..6af573fad6 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/mochitest.ini @@ -0,0 +1,67 @@ +[DEFAULT] +support-files = + manifest.json + dist/prototype.js + test/browser.html + test/console.html + test/test.css + test/functional/event.html + test/lib/unittest.js + test/unit/ajax_test.js + test/unit/array_test.js + test/unit/base_test.js + test/unit/dom_test.js + test/unit/element_mixins_test.js + test/unit/enumerable_test.js + test/unit/event_test.js + test/unit/form_test.js + test/unit/hash_test.js + test/unit/number_test.js + test/unit/position_test.js + test/unit/range_test.js + test/unit/selector_test.js + test/unit/string_test.js + test/unit/unittest_test.js + test/unit/fixtures/ajax.html + test/unit/fixtures/ajax.js + test/unit/fixtures/array.html + test/unit/fixtures/base.html + test/unit/fixtures/base.js + test/unit/fixtures/content.html + test/unit/fixtures/data.json + test/unit/fixtures/dom.css + test/unit/fixtures/dom.html + test/unit/fixtures/dom.js + test/unit/fixtures/element_mixins.html + test/unit/fixtures/element_mixins.js + test/unit/fixtures/empty.html + test/unit/fixtures/empty.js + test/unit/fixtures/enumerable.html + test/unit/fixtures/enumerable.js + test/unit/fixtures/event.html + test/unit/fixtures/form.html + test/unit/fixtures/hash.js + test/unit/fixtures/hello.js + test/unit/fixtures/logo.gif + test/unit/fixtures/position.html + test/unit/fixtures/selector.html + test/unit/fixtures/string.js + test/unit/fixtures/unittest.html + test/unit/tmp/ajax_test.html + test/unit/tmp/array_test.html + test/unit/tmp/base_test.html + test/unit/tmp/dom_test.html + test/unit/tmp/element_mixins_test.html + test/unit/tmp/enumerable_test.html + test/unit/tmp/event_test.html + test/unit/tmp/form_test.html + test/unit/tmp/hash_test.html + test/unit/tmp/number_test.html + test/unit/tmp/position_test.html + test/unit/tmp/range_test.html + test/unit/tmp/selector_test.html + test/unit/tmp/string_test.html + test/unit/tmp/unit_test.html + +[test_Prototype.html] +skip-if = toolkit == 'android' diff --git a/dom/tests/mochitest/ajax/prototype/test/browser.html b/dom/tests/mochitest/ajax/prototype/test/browser.html new file mode 100644 index 0000000000..ec699f9d45 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/browser.html @@ -0,0 +1,229 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html> + <head> + <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> + <title>Prototype object browser</title> + <style type="text/css" media="screen"> + body { + font-family: Lucida Grande, Verdana, sans-serif; + font-size: 13px; + } + + .inspector { + margin: 1%; + float: left; + width: 31%; + border: 1px solid #ccc; + background-color: white; + } + + .inspector h2 { + font-size: 13px; + margin: 0; + text-align: center; + padding: 5px; + background-color: #e6e6e6; + border-bottom: 1px solid #999; + } + + .inspector ul { + height: 200px; + overflow: auto; + margin: 0; + padding-left: 0; + } + + .inspector li { + cursor: pointer; + list-style-type: none; + padding: 2px 5px 2px 30px; + color: #333; + } + + .inspector li.selected { + background-color: #888; + color: #fff; + } + + .inspector.active li.selected { + background-color: #1a76fd; + color: #fff; + } + + #path, #value { + width: 97%; + margin: 1%; + } + + #path { + margin-bottom: 0; + border: 1px solid #ccc; + border-bottom: 1px solid #999; + background-color: #e6e6e6; + } + + #value { + margin-top: 0; + border: 1px solid #ccc; + border-top: none; + overflow: auto; + } + + #path_content, #value_content { + display: block; + padding: 15px 30px 15px 30px; + } + + </style> + <script type="text/javascript" src="../dist/prototype.js"></script> + <script type="text/javascript"> + var Browser = Class.create(); + Browser.prototype = { + initialize: function(element, name, value, options) { + this.element = $(element); + this.name = name; + this.value = value; + this.history = []; + Object.extend(this, options || {}); + this.reset(); + }, + + reset: function() { + this.go(this.name, this.value); + }, + + refresh: function() { + var children = $A(this.element.childNodes), + history = this.history.toArray(), + elements = history.slice(-3).pluck('element'); + + children.each(function(element) { + if (element && !elements.include(element)) + this.element.removeChild(element); + }.bind(this)); + + children = $A(this.element.childNodes); + + elements.each(function(element, index) { + Element.removeClassName(element, 'active'); + var child = children[index]; + if (!child) + this.element.appendChild(element); + else if (!element.parentNode) + this.element.insertBefore(element, child); + }.bind(this)); + + this.setTitle(); + this.setValue(); + }, + + setTitle: function() { + if (this.titleElement) + this.titleElement.innerHTML = + this.history.pluck('name').invoke('escapeHTML').join('.'); + }, + + setValue: function() { + if (this.valueElement) + this.valueElement.innerHTML = + this.currentValue().escapeHTML() + ' '; + }, + + currentValue: function() { + try { + return Object.inspect(this.current()); + } catch (e) { + return '(Internal Function)'; + } + }, + + current: function() { + return this.history.last().value; + }, + + go: function(name, value) { + var from = this.history.last(); + this.history.push(new Inspector(this, name, value)); + this.refresh(); + if (from) + Element.addClassName(from.element, 'active'); + } + } + + var Inspector = Class.create(); + Inspector.prototype = { + initialize: function(browser, name, value) { + this.browser = browser; + this.name = name; + this.value = value; + this.id = 'inspector_' + new Date().getTime(); + this.history = this.browser.history.toArray(); + this.history.push(this); + this.createElement(); + this.populate(); + }, + + properties: function() { + var properties = []; + for (var property in this.value) + properties.push(property); + properties.sort(); + return properties; + }, + + createElement: function() { + var element = document.createElement('div'); + element.className = 'inspector'; + element.id = this.id; + this.element = element; + + var title = document.createElement('h2'); + title.innerHTML = this.name.toString().escapeHTML(); + this.titleElement = title; + + var list = document.createElement('ul'); + this.listElement = list; + + element.appendChild(title); + element.appendChild(list); + }, + + populate: function() { + this.properties().each(function(property) { + var li = document.createElement('li'); + li.innerHTML = property.toString().escapeHTML(); + li.onclick = this.select.bind(this, li); + li._property = property; + this.listElement.appendChild(li); + }.bind(this)); + }, + + select: function(element) { + this.unselect(); + Element.addClassName(element, 'selected'); + this.selectedProperty = element; + this.browser.history = this.history.toArray(); + this.browser.go(element._property, this.value[element._property]); + }, + + unselect: function() { + if (this.selectedProperty) + Element.removeClassName(this.selectedProperty, 'selected'); + this.selectedProperty = null; + } + } + </script> + </head> + <body> + <div id="browser_wrapper"> + <div id="browser"></div> + <div style="clear: left"></div> + </div> + <h1 id="path"><span id="path_content"></span></h1> + <pre id="value"><div id="value_content"></div></pre> + <script type="text/javascript"> + new Browser('browser', 'window', window, {titleElement: $('path_content'), valueElement: $('value_content')}) + </script> + </body> +</html>
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/test/console.html b/dom/tests/mochitest/ajax/prototype/test/console.html new file mode 100644 index 0000000000..2b586ee426 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/console.html @@ -0,0 +1,110 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" + "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> + <head> + <title>Prototype Console</title> + <script src="../dist/prototype.js" type="text/javascript"></script> + <script type="text/javascript"> + Prototype.Console = Class.create(); + Prototype.Console.prototype = { + initialize: function(element, form, input) { + this.element = $(element); + this.form = $(form); + this.input = $(input); + this.context = window.eval.bind(window); + this.registerCallbacks(); + document.title = 'Prototype Console ' + Prototype.Version; + Field.activate(this.input); + }, + + registerCallbacks: function() { + Event.observe(this.form, 'submit', function(event) { + this.eval($F(this.input)); + this.input.value = ''; + Field.activate(this.input); + Event.stop(event); + }.bind(this)); + }, + + log: function(type, message) { + new Insertion.Bottom(this.element, + '<tr class="' + type + '"><td>' + + message.escapeHTML() + '</td></tr>'); + Element.scrollTo(this.form); + }, + + eval: function(expression) { + if (expression.match(/^\s*$/)) return; + try { + this.log('input', expression); + window.$_ = this.context.call(window, expression); + this.log('output', Object.inspect($_)); + } catch (e) { + this.log('error', e.toString()); + } + }, + + clear: function() { + this.element.innerHTML = ''; + } + } + </script> + <style type="text/css"> + body { + margin: 0; + padding: 0; + } + + .console { + width: 100%; + border-collapse: collapse; + margin-bottom: 50px; + } + + .console td { + padding: 5px; + font-family: monospace; + font-size: 14px; + } + + .console tr.input td { + background-color: #eee; + font-weight: bold; + } + + .console tr.error td, + .console tr.output td { + color: #333; + border-bottom: 1px solid #ccc; + } + + .console tr.error td { + color: #f00; + } + + #input-form { + width: 100%; + background-color: #f0f5b8; + border-top: 1px solid #333; + padding: 10px; + position: fixed; + height: 25px; + bottom: 0; + margin: 0; + } + </style> + </head> + <body> + <table class="console"> + <tbody id="console"> + </tbody> + </table> + <form id="input-form"> + <input type="text" size="60" id="input" /> + <input type="submit" value="Evaluate" /> + </form> + <script type="text/javascript"> + window.console = new Prototype.Console('console', 'input-form', 'input'); + </script> + </body> +</html> diff --git a/dom/tests/mochitest/ajax/prototype/test/functional/event.html b/dom/tests/mochitest/ajax/prototype/test/functional/event.html new file mode 100644 index 0000000000..526e711f9a --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/functional/event.html @@ -0,0 +1,243 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>Prototype functional test file</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script src="../../dist/prototype.js" type="text/javascript"></script> + + <style type="text/css" media="screen"> + /* <![CDATA[ */ + body { margin:1em 2em; padding:0; font-size:0.8em } + hr { width:31.2em; margin:1em 0; text-align:left } + p { width:30em; margin:0.5em 0; padding:0.3em 0.6em; color:#222; background:#eee; border:1px solid silver; } + .subtest { margin-top:-0.5em } + .passed { color:green; border-color:olive } + .failed { color:firebrick; border-color:firebrick } + .button { padding:0.2em 0.4em; background:#ccc; border:1px solid #aaa } + #log { position:absolute; left:35em; top:5em; width:20em; font-size:13px !important } + h2 { font:normal 1.1em Verdana,Arial,sans-serif; font-style:italic; color:gray; margin-top:-1.2em } + h2 *, h2 a:visited { color:#444 } + h2 a:hover { color:blue } + a:visited { color:blue } + a:hover { color:red } + /* ]]> */ + </style> + + <script type="text/javascript"> + Element.addMethods({ + passed: function(el, message) { + el = $(el); + el.className = 'passed'; + (el.down('span') || el).update(message || 'Test passed!'); + }, + + failed: function(el, message) { + el = $(el); + el.className = 'failed'; + (el.down('span') || el).update(message || 'Test failed'); + } + }); + + function log(obj) { + var line, all = []; + for (prop in obj) { + if (typeof obj[prop] == 'function' || /^[A-Z]|[XY]$/.test(prop)) continue; + line = prop + ": " + Object.inspect(obj[prop]); + all.push(line.escapeHTML()); + } + $('log').update(all.join('<br />')); + } + </script> +</head> +<body> + <h1>Prototype functional tests for the Event module</h1> + + <div id="log">log empty</div> + + <p id="basic">A basic event test - <strong>click here</strong></p> + <p id="basic_remove" class="subtest"><strong>click</strong> to stop observing the first test</p> + + <p id="inline_test" onclick="Event.stop(event); $(this).passed();"><strong>click</strong> to ensure generic Event methods work on inline handlers</p> + + <script type="text/javascript"> + var basic_callback = function(e){ + $('basic').passed(); + if ($('basic_remove')) $('basic_remove').show() + else $('basic').failed() + log(e); + } + $('basic').observe('click', basic_callback) + $('basic_remove').observe('click', function(e){ + el = $('basic') + el.passed('This test should now be inactive (try clicking)') + el.stopObserving('click') + $('basic_remove').remove() + log(e); + }).hide() + </script> + + <p id="basic2"><strong>Scope</strong> test - scope of the handler should be this element</p> + + <script type="text/javascript"> + $('basic2').observe('click', function(e) { + if (this === window) $('basic2').failed('Window scope! (needs scope correction)'); + else this.passed(); + log(e); + }); + </script> + + <p id="basic3"><strong>Event object</strong> test - should be present as a first argument</p> + + <script type="text/javascript"> + $('basic3').observe('click', function(evt) { + el = $('basic3'); + if (typeof evt != 'object') this.failed('Expected event object for first argument'); + else this.passed('Good first argument'); + log(evt); + }); + </script> + + <p><a href="#wrong" id="hijack">Hijack link test</a> (preventDefault)</p> + + <script type="text/javascript"> + $('hijack').observe('click', function(e){ + el = $(this.parentNode); + log(e); // this makes it fail?!? + e.preventDefault(); + + setTimeout(function() { + if (window.location.hash == '#wrong') el.failed('Hijack failed (<a href="' + + window.location.toString().replace(/#.+$/, '') + '">remove the fragment</a>)') + else el.passed(); + }, 50) + }) + </script> + + <hr /> + + + <p>Mouse click: + <span class="button" id="left">left</span> <span class="button" id="middle">middle</span> <span class="button" id="right">right</span></p> + + <script type="text/javascript"> + $w('left middle right').each(function(button) { + Event.observe(button, 'mousedown', function(e) { + if (Event['is' + this.id.capitalize() + 'Click'](e)) this.passed('Squeak!') + else this.failed('OH NO!'); + log(e); + }); + }); + </script> + + <p id="context">Context menu event (tries to prevent default)</p> + + <script type="text/javascript"> + $('context').observe('contextmenu', function(e){ + this.passed(); + Event.stop(e); + log(e); + }) + </script> + + <p id="target">Event.element() test</p> + + <script type="text/javascript"> + $('target').observe('click', function(e) { + if (e.element() == this && e.target == this) this.passed(); + else this.failed(); + log(e); + }); + </script> + + <p id="currentTarget"><span>Event.currentTarget test</span></p> + + <script type="text/javascript"> + $('currentTarget').observe('click', function(e){ + if (e.currentTarget !== this) this.failed(); + else this.passed(); + log(e); + }) + </script> + + <p id="findElement"><span>Event.findElement() test</span></p> + + <script type="text/javascript"> + $('findElement').observe('click', function(e){ + if (e.findElement('p') == this && e.findElement('body') == document.body && + e.findElement('foo') == null) this.passed(); + else this.failed(); + log(e); + }) + </script> + + <div id="container"><p id="stop"><strong>Stop propagation</strong> test (bubbling)</p></div> + + <script type="text/javascript"> + $('stop').observe('click', function(e){ + e.stop(); + this.passed(); + log(e); + }) + $('container').observe('click', function(e){ + $('stop').failed(); + log(e); + }) + </script> + + <div> + <p id="keyup_log"><strong>Keyup</strong> test - focus on the textarea and type</p> + <textarea id="keyup" class="subtest"></textarea> + </div> + + <script type="text/javascript"> + $('keyup').observe('keyup', function(e){ + el = $('keyup_log'); + el.passed('Key captured: the length is ' + $('keyup').value.length); + log(e); + }) + </script> + + <p id="bind"><code>bindAsEventListener()</code> test</p> + + <script type="text/javascript"> + $('bind').observe('click', function(e, str, arr){ + el = $('bind') + try { + if (arguments.length != 3) throw arguments.length + ' arguments: ' + $A(arguments).inspect() + if (str != 'foo') throw 'wrong string: ' + str + if (arr.constructor != Array) throw '3rd parameter is not an array' + el.passed(); + } + catch (err) { el.failed(err.toString()) } + log(e); + }.bindAsEventListener(document.body, 'foo', [1,2,3])) + </script> + + <p id="obj_inspect"><code>Object.inspect(event)</code> test</p> + + <script type="text/javascript"> + $('obj_inspect').observe('click', function(e){ + el = $('obj_inspect') + try { el.passed(Object.inspect(e)) } + catch (err) { el.failed('Failed! Error thrown') } + log(e); + }) + </script> + + <p id="addunload">Add unload events</p> + + <script type="text/javascript"> + $('addunload').observe('click', function(e){ + if (this._done) return + + window.onunload = function(){ alert('inline unload fired!') } + Event.observe(window, 'unload', function(){ alert('observed unload fired!') }) + + this.update('Registered two unload events, one inline ("onunload") and one regular - try to refresh, both should fire') + this._done = true + log(e); + }) + </script> +</body> +</html> diff --git a/dom/tests/mochitest/ajax/prototype/test/lib/unittest.js b/dom/tests/mochitest/ajax/prototype/test/lib/unittest.js new file mode 100644 index 0000000000..4f92d13dc0 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/lib/unittest.js @@ -0,0 +1,602 @@ +// Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) +// (c) 2005 Jon Tirsen (http://www.tirsen.com) +// (c) 2005 Michael Schuerig (http://www.schuerig.de/michael/) +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + +// experimental, Firefox-only +Event.simulateMouse = function(element, eventName) { + var options = Object.extend({ + pointerX: 0, + pointerY: 0, + buttons: 0 + }, arguments[2] || {}); + var oEvent = document.createEvent("MouseEvents"); + oEvent.initMouseEvent(eventName, true, true, document.defaultView, + options.buttons, options.pointerX, options.pointerY, options.pointerX, options.pointerY, + false, false, false, false, 0, $(element)); + + if (this.mark) Element.remove(this.mark); + + var style = 'position: absolute; width: 5px; height: 5px;' + + 'top: #{pointerY}px; left: #{pointerX}px;'.interpolate(options) + + 'border-top: 1px solid red; border-left: 1px solid red;' + + this.mark = new Element('div', { style: style }); + this.mark.appendChild(document.createTextNode(" ")); + document.body.appendChild(this.mark); + + if (this.step) + alert('['+new Date().getTime().toString()+'] '+eventName+'/'+Test.Unit.inspect(options)); + + $(element).dispatchEvent(oEvent); +}; + +// Note: Due to a fix in Firefox 1.0.5/6 that probably fixed "too much", this doesn't work in 1.0.6 or DP2. +// You need to downgrade to 1.0.4 for now to get this working +// See https://bugzilla.mozilla.org/show_bug.cgi?id=289940 for the fix that fixed too much +Event.simulateKey = function(element, eventName) { + var options = Object.extend({ + ctrlKey: false, + altKey: false, + shiftKey: false, + metaKey: false, + keyCode: 0, + charCode: 0 + }, arguments[2] || {}); + + var oEvent = document.createEvent("KeyboardEvent"); + oEvent.initKeyEvent(eventName, true, true, window, + options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, + options.keyCode, options.charCode ); + $(element).dispatchEvent(oEvent); +}; + +Event.simulateKeys = function(element, command) { + for (var i=0; i<command.length; i++) { + Event.simulateKey(element,'keypress',{charCode:command.charCodeAt(i)}); + } +}; + +var Test = { + Unit: { + inspect: Object.inspect // security exception workaround + } +}; + +Test.Unit.Logger = Class.create({ + initialize: function(element) { + this.element = $(element); + if (this.element) this._createLogTable(); + this.tbody = $(this.element.getElementsByTagName('tbody')[0]); + }, + + start: function(testName) { + this.testName = testName; + if (!this.element) return; + this.tbody.insert('<tr><td>' + testName + '</td><td></td><td></td></tr>'); + }, + + setStatus: function(status) { + this.getLastLogLine().addClassName(status); + $(this.getLastLogLine().getElementsByTagName('td')[1]).update(status); + }, + + finish: function(status, summary) { + if (!this.element) return; + this.setStatus(status); + this.message(summary); + }, + + message: function(message) { + if (!this.element) return; + this.getMessageCell().update(this._toHTML(message)); + }, + + summary: function(summary) { + if (!this.element) return; + var div = $(this.element.getElementsByTagName('div')[0]); + div.update(this._toHTML(summary)); + }, + + getLastLogLine: function() { + //return this.element.descendants('tr').last(); + var trs = this.element.getElementsByTagName('tr'); + return $(trs[trs.length - 1]); + }, + + getMessageCell: function() { + return this.getLastLogLine().down('td', 2); + var tds = this.getLastLogLine().getElementsByTagName('td'); + return $(tds[2]); + }, + + _createLogTable: function() { + var html = '<div class="logsummary">running...</div>' + + '<table class="logtable">' + + '<thead><tr><th>Status</th><th>Test</th><th>Message</th></tr></thead>' + + '<tbody class="loglines"></tbody>' + + '</table>'; + this.element.update(html); + + }, + + appendActionButtons: function(actions) { + actions = $H(actions); + if (!actions.any()) return; + var div = new Element("div", {className: 'action_buttons'}); + actions.inject(div, function(container, action) { + var button = new Element("input").setValue(action.key).observe("click", action.value); + button.type = "button"; + return container.insert(button); + }); + this.getMessageCell().insert(div); + }, + + _toHTML: function(txt) { + return txt.escapeHTML().replace(/\n/g,"<br />"); + } +}); + +Test.Unit.Runner = Class.create({ + initialize: function(testcases) { + var options = this.options = Object.extend({ + testLog: 'testlog' + }, arguments[1] || {}); + + options.resultsURL = this.queryParams.resultsURL; + + this.tests = this.getTests(testcases); + this.currentTest = 0; + + Event.observe(window, "load", function() { + this.logger = new Test.Unit.Logger($(options.testLog)); + this.runTests.bind(this).delay(0.1); + }.bind(this)); + }, + + queryParams: window.location.search.parseQuery(), + + getTests: function(testcases) { + var tests, options = this.options; + if (this.queryParams.tests) tests = this.queryParams.tests.split(','); + else if (options.tests) tests = options.tests; + else if (options.test) tests = [option.test]; + else tests = Object.keys(testcases).grep(/^test/); + + return tests.map(function(test) { + if (testcases[test]) + return new Test.Unit.Testcase(test, testcases[test], testcases.setup, testcases.teardown); + }).compact(); + }, + + getResult: function() { + var results = { + tests: this.tests.length, + assertions: 0, + failures: 0, + errors: 0 + }; + + return this.tests.inject(results, function(results, test) { + results.assertions += test.assertions; + results.failures += test.failures; + results.errors += test.errors; + return results; + }); + }, + + postResults: function() { + if (this.options.resultsURL) { + new Ajax.Request(this.options.resultsURL, + { method: 'get', parameters: this.getResult(), asynchronous: false }); + } + }, + + runTests: function() { + var test = this.tests[this.currentTest], actions; + + if (!test) return this.finish(); + if (test.timerID > 0) test.timerID = -1; + if (!test.isWaiting) this.logger.start(test.name); + test.run(); + if (test.isWaiting) { + if (test.timeToWait) { + this.logger.message("Waiting for " + test.timeToWait + "ms"); + test.timerID = setTimeout(this.runTests.bind(this), test.timeToWait); + } else { + this.logger.message("Waiting for finish"); + } + test.runner = this; + return; + } + + this.logger.finish(test.status(), test.summary()); + if (actions = test.actions) this.logger.appendActionButtons(actions); + this.currentTest++; + // tail recursive, hopefully the browser will skip the stackframe + this.runTests(); + }, + + finish: function() { + this.postResults(); + this.logger.summary(this.summary()); + }, + + summary: function() { + return '#{tests} tests, #{assertions} assertions, #{failures} failures, #{errors} errors' + .interpolate(this.getResult()); + } +}); + +Test.Unit.MessageTemplate = Class.create({ + initialize: function(string) { + var parts = []; + (string || '').scan(/(?=[^\\])\?|(?:\\\?|[^\?])+/, function(part) { + parts.push(part[0]); + }); + this.parts = parts; + }, + + evaluate: function(params) { + return this.parts.map(function(part) { + return part == '?' ? Test.Unit.inspect(params.shift()) : part.replace(/\\\?/, '?'); + }).join(''); + } +}); + +Test.Unit.Assertions = { + buildMessage: function(message, template) { + var args = $A(arguments).slice(2); + return (message ? message + '\n' : '') + new Test.Unit.MessageTemplate(template).evaluate(args); + }, + + flunk: function(message) { + this.assertBlock(message || 'Flunked', function() { return false }); + }, + + assertBlock: function(message, block) { + try { + block.call(this) ? this.pass() : this.fail(message); + } catch(e) { this.error(e) } + }, + + assert: function(expression, message) { + message = this.buildMessage(message || 'assert', 'got <?>', expression); + this.assertBlock(message, function() { return expression }); + }, + + assertEqual: function(expected, actual, message) { + message = this.buildMessage(message || 'assertEqual', 'expected <?>, actual: <?>', expected, actual); + this.assertBlock(message, function() { return expected == actual }); + }, + + assertNotEqual: function(expected, actual, message) { + message = this.buildMessage(message || 'assertNotEqual', 'expected <?>, actual: <?>', expected, actual); + this.assertBlock(message, function() { return expected != actual }); + }, + + assertEnumEqual: function(expected, actual, message) { + expected = $A(expected); + actual = $A(actual); + message = this.buildMessage(message || 'assertEnumEqual', 'expected <?>, actual: <?>', expected, actual); + this.assertBlock(message, function() { + return expected.length == actual.length && expected.zip(actual).all(function(pair) { return pair[0] == pair[1] }); + }); + }, + + assertEnumNotEqual: function(expected, actual, message) { + expected = $A(expected); + actual = $A(actual); + message = this.buildMessage(message || 'assertEnumNotEqual', '<?> was the same as <?>', expected, actual); + this.assertBlock(message, function() { + return expected.length != actual.length || expected.zip(actual).any(function(pair) { return pair[0] != pair[1] }); + }); + }, + + assertHashEqual: function(expected, actual, message) { + expected = $H(expected); + actual = $H(actual); + var expected_array = expected.toArray().sort(), actual_array = actual.toArray().sort(); + message = this.buildMessage(message || 'assertHashEqual', 'expected <?>, actual: <?>', expected, actual); + // from now we recursively zip & compare nested arrays + var block = function() { + return expected_array.length == actual_array.length && + expected_array.zip(actual_array).all(function(pair) { + return pair.all(Object.isArray) ? + pair[0].zip(pair[1]).all(arguments.callee) : pair[0] == pair[1]; + }); + }; + this.assertBlock(message, block); + }, + + assertHashNotEqual: function(expected, actual, message) { + expected = $H(expected); + actual = $H(actual); + var expected_array = expected.toArray().sort(), actual_array = actual.toArray().sort(); + message = this.buildMessage(message || 'assertHashNotEqual', '<?> was the same as <?>', expected, actual); + // from now we recursively zip & compare nested arrays + var block = function() { + return !(expected_array.length == actual_array.length && + expected_array.zip(actual_array).all(function(pair) { + return pair.all(Object.isArray) ? + pair[0].zip(pair[1]).all(arguments.callee) : pair[0] == pair[1]; + })); + }; + this.assertBlock(message, block); + }, + + assertIdentical: function(expected, actual, message) { + message = this.buildMessage(message || 'assertIdentical', 'expected <?>, actual: <?>', expected, actual); + this.assertBlock(message, function() { return expected === actual }); + }, + + assertNotIdentical: function(expected, actual, message) { + message = this.buildMessage(message || 'assertNotIdentical', 'expected <?>, actual: <?>', expected, actual); + this.assertBlock(message, function() { return expected !== actual }); + }, + + assertNull: function(obj, message) { + message = this.buildMessage(message || 'assertNull', 'got <?>', obj); + this.assertBlock(message, function() { return obj === null }); + }, + + assertNotNull: function(obj, message) { + message = this.buildMessage(message || 'assertNotNull', 'got <?>', obj); + this.assertBlock(message, function() { return obj !== null }); + }, + + assertUndefined: function(obj, message) { + message = this.buildMessage(message || 'assertUndefined', 'got <?>', obj); + this.assertBlock(message, function() { return typeof obj == "undefined" }); + }, + + assertNotUndefined: function(obj, message) { + message = this.buildMessage(message || 'assertNotUndefined', 'got <?>', obj); + this.assertBlock(message, function() { return typeof obj != "undefined" }); + }, + + assertNullOrUndefined: function(obj, message) { + message = this.buildMessage(message || 'assertNullOrUndefined', 'got <?>', obj); + this.assertBlock(message, function() { return obj == null }); + }, + + assertNotNullOrUndefined: function(obj, message) { + message = this.buildMessage(message || 'assertNotNullOrUndefined', 'got <?>', obj); + this.assertBlock(message, function() { return obj != null }); + }, + + assertMatch: function(expected, actual, message) { + message = this.buildMessage(message || 'assertMatch', 'regex <?> did not match <?>', expected, actual); + this.assertBlock(message, function() { return new RegExp(expected).exec(actual) }); + }, + + assertNoMatch: function(expected, actual, message) { + message = this.buildMessage(message || 'assertNoMatch', 'regex <?> matched <?>', expected, actual); + this.assertBlock(message, function() { return !(new RegExp(expected).exec(actual)) }); + }, + + assertHidden: function(element, message) { + message = this.buildMessage(message || 'assertHidden', '? isn\'t hidden.', element); + this.assertBlock(message, function() { return element.style.display == 'none' }); + }, + + assertInstanceOf: function(expected, actual, message) { + message = this.buildMessage(message || 'assertInstanceOf', '<?> was not an instance of the expected type', actual); + this.assertBlock(message, function() { return actual instanceof expected }); + }, + + assertNotInstanceOf: function(expected, actual, message) { + message = this.buildMessage(message || 'assertNotInstanceOf', '<?> was an instance of the expected type', actual); + this.assertBlock(message, function() { return !(actual instanceof expected) }); + }, + + assertRespondsTo: function(method, obj, message) { + message = this.buildMessage(message || 'assertRespondsTo', 'object doesn\'t respond to <?>', method); + this.assertBlock(message, function() { return (method in obj && typeof obj[method] == 'function') }); + }, + + assertRaise: function(exceptionName, method, message) { + message = this.buildMessage(message || 'assertRaise', '<?> exception expected but none was raised', exceptionName); + var block = function() { + try { + method(); + return false; + } catch(e) { + if (e.name == exceptionName) return true; + else throw e; + } + }; + this.assertBlock(message, block); + }, + + assertNothingRaised: function(method, message) { + try { + method(); + this.assert(true, "Expected nothing to be thrown"); + } catch(e) { + message = this.buildMessage(message || 'assertNothingRaised', '<?> was thrown when nothing was expected.', e); + this.flunk(message); + } + }, + + _isVisible: function(element) { + element = $(element); + if (!element.parentNode) return true; + this.assertNotNull(element); + if (element.style && Element.getStyle(element, 'display') == 'none') + return false; + + return arguments.callee.call(this, element.parentNode); + }, + + assertVisible: function(element, message) { + message = this.buildMessage(message, '? was not visible.', element); + this.assertBlock(message, function() { return this._isVisible(element) }); + }, + + assertNotVisible: function(element, message) { + message = this.buildMessage(message, '? was not hidden and didn\'t have a hidden parent either.', element); + this.assertBlock(message, function() { return !this._isVisible(element) }); + }, + + assertElementsMatch: function() { + var pass = true, expressions = $A(arguments), elements = $A(expressions.shift()); + if (elements.length != expressions.length) { + message = this.buildMessage('assertElementsMatch', 'size mismatch: ? elements, ? expressions (?).', elements.length, expressions.length, expressions); + this.flunk(message); + pass = false; + } + elements.zip(expressions).all(function(pair, index) { + var element = $(pair.first()), expression = pair.last(); + if (element.match(expression)) return true; + message = this.buildMessage('assertElementsMatch', 'In index <?>: expected <?> but got ?', index, expression, element); + this.flunk(message); + pass = false; + }.bind(this)) + + if (pass) this.assert(true, "Expected all elements to match."); + }, + + assertElementMatches: function(element, expression, message) { + this.assertElementsMatch([element], expression); + } +}; + +Test.Unit.Testcase = Class.create(Test.Unit.Assertions, { + initialize: function(name, test, setup, teardown) { + this.name = name; + this.test = test || Prototype.emptyFunction; + this.setup = setup || Prototype.emptyFunction; + this.teardown = teardown || Prototype.emptyFunction; + this.messages = []; + this.actions = {}; + }, + + isWaiting: false, + timeToWait: null, + timerID: -1, + runner: null, + assertions: 0, + failures: 0, + errors: 0, + isRunningFromRake: window.location.port == 4711, + + wait: function(time, nextPart) { + this.isWaiting = true; + this.test = nextPart; + this.timeToWait = time; + }, + + waitForFinish: function() { + this.isWaiting = true; + }, + + finish: function() { + if (this.timerID > 0) { + clearTimeout(this.timerID); + this.timerID = -1; + this.timeToWait = null; + } + this.test = function(){}; + // continue test + if (this.runner) + this.runner.runTests(); + }, + + run: function(rethrow) { + try { + try { + if (!this.isWaiting) this.setup(); + this.isWaiting = false; + this.test(); + } finally { + if (!this.isWaiting) { + this.teardown(); + } + } + } + catch(e) { + if (rethrow) throw e; + this.error(e, this); + } + }, + + summary: function() { + var msg = '#{assertions} assertions, #{failures} failures, #{errors} errors\n'; + return msg.interpolate(this) + this.messages.join("\n"); + }, + + pass: function() { + this.assertions++; + }, + + fail: function(message) { + this.failures++; + var line = ""; + try { + throw new Error("stack"); + } catch(e){ + match = /.*\/(.*_test\.js):(\d+)/.exec(e.stack || '') || ['','','']; + file = match[1]; + line = match[2]; + } + this.messages.push("Failure: " + message + (line ? " " + file + ": Line #" + line : "")); + }, + + info: function(message) { + this.messages.push("Info: " + message); + }, + + error: function(error, test) { + this.errors++; + this.actions['retry with throw'] = function() { test.run(true) }; + this.messages.push(error.name + ": "+ error.message + "(" + Test.Unit.inspect(error) + ")"); + }, + + status: function() { + if (this.failures > 0) return 'failed'; + if (this.errors > 0) return 'error'; + return 'passed'; + }, + + benchmark: function(operation, iterations) { + var startAt = new Date(); + (iterations || 1).times(operation); + var timeTaken = ((new Date())-startAt); + this.info((arguments[2] || 'Operation') + ' finished ' + + iterations + ' iterations in ' + (timeTaken/1000)+'s' ); + return timeTaken; + } +}); + +if ( parent.SimpleTest && parent.runAJAXTest ) (function(){ + var finish = Test.Unit.Logger.prototype.finish; + Test.Unit.Logger.prototype.finish = function(status,summary){ + parent.SimpleTest.ok( status == "passed", `${this.testName}: ${summary}` ); + return finish.apply( this, arguments ); + }; + + // Intentionally overwrite (to stop the Ajax request) + Test.Unit.Runner.prototype.postResults = parent.runAJAXTest; +})(); + + diff --git a/dom/tests/mochitest/ajax/prototype/test/test.css b/dom/tests/mochitest/ajax/prototype/test/test.css new file mode 100644 index 0000000000..6fe8f51c9b --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/test.css @@ -0,0 +1,50 @@ +body, div, p, h1, h2, h3, ul, ol, span, a, table, td, form, img, li { + font-family: sans-serif; +} + +body { + font-size:0.8em; +} + +#log { + padding-bottom: 1em; + border-bottom: 2px solid #000; + margin-bottom: 2em; +} + +.logsummary { + margin-top: 1em; + margin-bottom: 1em; + padding: 1ex; + border: 1px solid #000; + font-weight: bold; +} + +.logtable { + width:100%; + border-collapse: collapse; + border: 1px dotted #666; +} + +.logtable td, .logtable th { + text-align: left; + padding: 3px 8px; + border: 1px dotted #666; +} + +.logtable .passed { + background-color: #cfc; +} + +.logtable .failed, .logtable .error { + background-color: #fcc; +} + +.logtable td div.action_buttons { + display: inline; +} + +.logtable td div.action_buttons input { + margin: 0 5px; + font-size: 10px; +}
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/ajax_test.js b/dom/tests/mochitest/ajax/prototype/test/unit/ajax_test.js new file mode 100644 index 0000000000..9ceb985186 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/ajax_test.js @@ -0,0 +1,286 @@ +var extendDefault = function(options) { + return Object.extend({ + asynchronous: false, + method: 'get', + onException: function(request, e) { throw e } + }, options); +}; + +new Test.Unit.Runner({ + setup: function() { + $('content').update(''); + $('content2').update(''); + }, + + teardown: function() { + // hack to cleanup responders + Ajax.Responders.responders = [Ajax.Responders.responders[0]]; + }, + + testSynchronousRequest: function() { + this.assertEqual("", $("content").innerHTML); + + this.assertEqual(0, Ajax.activeRequestCount); + new Ajax.Request("../fixtures/hello.js", extendDefault({ + asynchronous: false, + method: 'GET', + evalJS: 'force' + })); + this.assertEqual(0, Ajax.activeRequestCount); + + var h2 = $("content").firstChild; + this.assertEqual("Hello world!", h2.innerHTML); + }, + + testUpdaterOptions: function() { + var options = { + method: 'get', + asynchronous: false, + evalJS: 'force', + onComplete: Prototype.emptyFunction + } + var request = new Ajax.Updater("content", "../fixtures/hello.js", options); + request.options.onComplete = function() {}; + this.assertIdentical(Prototype.emptyFunction, options.onComplete); + }, + + testEvalResponseShouldBeCalledBeforeOnComplete: function() { + if (this.isRunningFromRake) { + this.assertEqual("", $("content").innerHTML); + + this.assertEqual(0, Ajax.activeRequestCount); + new Ajax.Request("../fixtures/hello.js", extendDefault({ + onComplete: function(response) { this.assertNotEqual("", $("content").innerHTML) }.bind(this) + })); + this.assertEqual(0, Ajax.activeRequestCount); + + var h2 = $("content").firstChild; + this.assertEqual("Hello world!", h2.innerHTML); + } else { + this.info(message); + } + }, + + testContentTypeSetForSimulatedVerbs: function() { + if (this.isRunningFromRake) { + new Ajax.Request('/inspect', extendDefault({ + method: 'put', + contentType: 'application/bogus', + onComplete: function(response) { + this.assertEqual('application/bogus; charset=UTF-8', response.responseJSON.headers['content-type']); + }.bind(this) + })); + } else { + this.info(message); + } + }, + + testOnCreateCallback: function() { + new Ajax.Request("../fixtures/content.html", extendDefault({ + onCreate: function(transport) { this.assertEqual(0, transport.readyState) }.bind(this), + onComplete: function(transport) { this.assertNotEqual(0, transport.readyState) }.bind(this) + })); + }, + + testEvalJS: function() { + if (this.isRunningFromRake) { + + $('content').update(); + new Ajax.Request("/response", extendDefault({ + parameters: Fixtures.js, + onComplete: function(transport) { + var h2 = $("content").firstChild; + this.assertEqual("Hello world!", h2.innerHTML); + }.bind(this) + })); + + $('content').update(); + new Ajax.Request("/response", extendDefault({ + evalJS: false, + parameters: Fixtures.js, + onComplete: function(transport) { + this.assertEqual("", $("content").innerHTML); + }.bind(this) + })); + } else { + this.info(message); + } + + $('content').update(); + new Ajax.Request("../fixtures/hello.js", extendDefault({ + evalJS: 'force', + onComplete: function(transport) { + var h2 = $("content").firstChild; + this.assertEqual("Hello world!", h2.innerHTML); + }.bind(this) + })); + }, + + testCallbacks: function() { + var options = extendDefault({ + onCreate: function(transport) { this.assertInstanceOf(Ajax.Response, transport) }.bind(this) + }); + + Ajax.Request.Events.each(function(state){ + options['on' + state] = options.onCreate; + }); + + new Ajax.Request("../fixtures/content.html", options); + }, + + testResponseText: function() { + new Ajax.Request("../fixtures/empty.html", extendDefault({ + onComplete: function(transport) { this.assertEqual('', transport.responseText) }.bind(this) + })); + + new Ajax.Request("../fixtures/content.html", extendDefault({ + onComplete: function(transport) { this.assertEqual(sentence, transport.responseText.toLowerCase()) }.bind(this) + })); + }, + + testResponseXML: function() { + if (this.isRunningFromRake) { + new Ajax.Request("/response", extendDefault({ + parameters: Fixtures.xml, + onComplete: function(transport) { + this.assertEqual('foo', transport.responseXML.getElementsByTagName('name')[0].getAttribute('attr')) + }.bind(this) + })); + } else { + this.info(message); + } + }, + + testResponseJSON: function() { + if (this.isRunningFromRake) { + new Ajax.Request("/response", extendDefault({ + parameters: Fixtures.json, + onComplete: function(transport) { this.assertEqual(123, transport.responseJSON.test) }.bind(this) + })); + + new Ajax.Request("/response", extendDefault({ + parameters: { + 'Content-Length': 0, + 'Content-Type': 'application/json' + }, + onComplete: function(transport) { this.assertNull(transport.responseJSON) }.bind(this) + })); + + new Ajax.Request("/response", extendDefault({ + evalJSON: false, + parameters: Fixtures.json, + onComplete: function(transport) { this.assertNull(transport.responseJSON) }.bind(this) + })); + + new Ajax.Request("/response", extendDefault({ + parameters: Fixtures.jsonWithoutContentType, + onComplete: function(transport) { this.assertNull(transport.responseJSON) }.bind(this) + })); + + new Ajax.Request("/response", extendDefault({ + sanitizeJSON: true, + parameters: Fixtures.invalidJson, + onException: function(request, error) { + this.assert(error.message.include('Badly formed JSON string')); + this.assertInstanceOf(Ajax.Request, request); + }.bind(this) + })); + } else { + this.info(message); + } + + new Ajax.Request("../fixtures/data.json", extendDefault({ + evalJSON: 'force', + onComplete: function(transport) { this.assertEqual(123, transport.responseJSON.test) }.bind(this) + })); + }, + + testHeaderJSON: function() { + if (this.isRunningFromRake) { + new Ajax.Request("/response", extendDefault({ + parameters: Fixtures.headerJson, + onComplete: function(transport, json) { + this.assertEqual('hello #éà', transport.headerJSON.test); + this.assertEqual('hello #éà', json.test); + }.bind(this) + })); + + new Ajax.Request("/response", extendDefault({ + onComplete: function(transport, json) { + this.assertNull(transport.headerJSON) + this.assertNull(json) + }.bind(this) + })); + } else { + this.info(message); + } + }, + + testGetHeader: function() { + if (this.isRunningFromRake) { + new Ajax.Request("/response", extendDefault({ + parameters: { 'X-TEST': 'some value' }, + onComplete: function(transport) { + this.assertEqual('some value', transport.getHeader('X-Test')); + this.assertNull(transport.getHeader('X-Inexistant')); + }.bind(this) + })); + } else { + this.info(message); + } + }, + + testParametersCanBeHash: function() { + if (this.isRunningFromRake) { + new Ajax.Request("/response", extendDefault({ + parameters: $H({ "one": "two", "three": "four" }), + onComplete: function(transport) { + this.assertEqual("two", transport.getHeader("one")); + this.assertEqual("four", transport.getHeader("three")); + this.assertNull(transport.getHeader("toObject")); + }.bind(this) + })); + } else { + this.info(message); + } + }, + + testIsSameOriginMethod: function() { + var isSameOrigin = Ajax.Request.prototype.isSameOrigin; + this.assert(isSameOrigin.call({ url: '/foo/bar.html' }), '/foo/bar.html'); + this.assert(isSameOrigin.call({ url: window.location.toString() }), window.location); + this.assert(!isSameOrigin.call({ url: 'http://example.com' }), 'http://example.com'); + + if (this.isRunningFromRake) { + Ajax.Request.prototype.isSameOrigin = function() { + return false + }; + + $("content").update('same origin policy'); + new Ajax.Request("/response", extendDefault({ + parameters: Fixtures.js, + onComplete: function(transport) { + this.assertEqual("same origin policy", $("content").innerHTML); + }.bind(this) + })); + + new Ajax.Request("/response", extendDefault({ + parameters: Fixtures.invalidJson, + onException: function(request, error) { + this.assert(error.message.include('Badly formed JSON string')); + }.bind(this) + })); + + new Ajax.Request("/response", extendDefault({ + parameters: { 'X-JSON': '{});window.attacked = true;({}' }, + onException: function(request, error) { + this.assert(error.message.include('Badly formed JSON string')); + }.bind(this) + })); + + Ajax.Request.prototype.isSameOrigin = isSameOrigin; + } else { + this.info(message); + } + } +}); diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/array_test.js b/dom/tests/mochitest/ajax/prototype/test/unit/array_test.js new file mode 100644 index 0000000000..c3f9d41147 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/array_test.js @@ -0,0 +1,190 @@ +var globalArgsTest = 'nothing to see here'; + +new Test.Unit.Runner({ + test$A: function(){ + this.assertEnumEqual([], $A({})); + }, + + testToArrayOnArguments: function(){ + function toArrayOnArguments(){ + globalArgsTest = $A(arguments); + } + toArrayOnArguments(); + this.assertEnumEqual([], globalArgsTest); + toArrayOnArguments('foo'); + this.assertEnumEqual(['foo'], globalArgsTest); + toArrayOnArguments('foo','bar'); + this.assertEnumEqual(['foo','bar'], globalArgsTest); + }, + + testToArrayOnNodeList: function(){ + // direct HTML + this.assertEqual(3, $A($('test_node').childNodes).length); + + // DOM + var element = document.createElement('div'); + element.appendChild(document.createTextNode('22')); + (2).times(function(){ element.appendChild(document.createElement('span')) }); + this.assertEqual(3, $A(element.childNodes).length); + + // HTML String + element = document.createElement('div'); + $(element).update('22<span></span><span></span'); + this.assertEqual(3, $A(element.childNodes).length); + }, + + testClear: function(){ + this.assertEnumEqual([], [].clear()); + this.assertEnumEqual([], [1].clear()); + this.assertEnumEqual([], [1,2].clear()); + }, + + testClone: function(){ + this.assertEnumEqual([], [].clone()); + this.assertEnumEqual([1], [1].clone()); + this.assertEnumEqual([1,2], [1,2].clone()); + this.assertEnumEqual([0,1,2], [0,1,2].clone()); + var a = [0,1,2]; + var b = a; + this.assertIdentical(a, b); + b = a.clone(); + this.assertNotIdentical(a, b); + }, + + testFirst: function(){ + this.assertUndefined([].first()); + this.assertEqual(1, [1].first()); + this.assertEqual(1, [1,2].first()); + }, + + testLast: function(){ + this.assertUndefined([].last()); + this.assertEqual(1, [1].last()); + this.assertEqual(2, [1,2].last()); + }, + + testCompact: function(){ + this.assertEnumEqual([], [].compact()); + this.assertEnumEqual([1,2,3], [1,2,3].compact()); + this.assertEnumEqual([0,1,2,3], [0,null,1,2,undefined,3].compact()); + this.assertEnumEqual([1,2,3], [null,1,2,3,null].compact()); + }, + + testFlatten: function(){ + this.assertEnumEqual([], [].flatten()); + this.assertEnumEqual([1,2,3], [1,2,3].flatten()); + this.assertEnumEqual([1,2,3], [1,[[[2,3]]]].flatten()); + this.assertEnumEqual([1,2,3], [[1],[2],[3]].flatten()); + this.assertEnumEqual([1,2,3], [[[[[[[1]]]]]],2,3].flatten()); + }, + + testIndexOf: function(){ + this.assertEqual(-1, [].indexOf(1)); + this.assertEqual(-1, [0].indexOf(1)); + this.assertEqual(0, [1].indexOf(1)); + this.assertEqual(1, [0,1,2].indexOf(1)); + this.assertEqual(0, [1,2,1].indexOf(1)); + this.assertEqual(2, [1,2,1].indexOf(1, -1)); + this.assertEqual(1, [undefined,null].indexOf(null)); + }, + + testLastIndexOf: function(){ + this.assertEqual(-1,[].lastIndexOf(1)); + this.assertEqual(-1, [0].lastIndexOf(1)); + this.assertEqual(0, [1].lastIndexOf(1)); + this.assertEqual(2, [0,2,4,6].lastIndexOf(4)); + this.assertEqual(3, [4,4,2,4,6].lastIndexOf(4)); + this.assertEqual(3, [0,2,4,6].lastIndexOf(6,3)); + this.assertEqual(-1, [0,2,4,6].lastIndexOf(6,2)); + this.assertEqual(0, [6,2,4,6].lastIndexOf(6,2)); + + var fixture = [1,2,3,4,3]; + this.assertEqual(4, fixture.lastIndexOf(3)); + this.assertEnumEqual([1,2,3,4,3],fixture); + + //tests from http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:lastIndexOf + var array = [2, 5, 9, 2]; + this.assertEqual(3,array.lastIndexOf(2)); + this.assertEqual(-1,array.lastIndexOf(7)); + this.assertEqual(3,array.lastIndexOf(2,3)); + this.assertEqual(0,array.lastIndexOf(2,2)); + this.assertEqual(0,array.lastIndexOf(2,-2)); + this.assertEqual(3,array.lastIndexOf(2,-1)); + }, + + testInspect: function(){ + this.assertEqual('[]',[].inspect()); + this.assertEqual('[1]',[1].inspect()); + this.assertEqual('[\'a\']',['a'].inspect()); + this.assertEqual('[\'a\', 1]',['a',1].inspect()); + }, + + testIntersect: function(){ + this.assertEnumEqual([1,3], [1,1,3,5].intersect([1,2,3])); + this.assertEnumEqual([1], [1,1].intersect([1,1])); + this.assertEnumEqual([0], [0,2].intersect([1,0])); + this.assertEnumEqual([], [1,1,3,5].intersect([4])); + this.assertEnumEqual([], [1].intersect(['1'])); + + this.assertEnumEqual( + ['B','C','D'], + $R('A','Z').toArray().intersect($R('B','D').toArray()) + ); + }, + + testToJSON: function(){ + this.assertEqual('[]', [].toJSON()); + this.assertEqual('[\"a\"]', ['a'].toJSON()); + this.assertEqual('[\"a\", 1]', ['a', 1].toJSON()); + this.assertEqual('[\"a\", {\"b\": null}]', ['a', {'b': null}].toJSON()); + }, + + testReduce: function(){ + this.assertUndefined([].reduce()); + this.assertNull([null].reduce()); + this.assertEqual(1, [1].reduce()); + this.assertEnumEqual([1,2,3], [1,2,3].reduce()); + this.assertEnumEqual([1,null,3], [1,null,3].reduce()); + }, + + testReverse: function(){ + this.assertEnumEqual([], [].reverse()); + this.assertEnumEqual([1], [1].reverse()); + this.assertEnumEqual([2,1], [1,2].reverse()); + this.assertEnumEqual([3,2,1], [1,2,3].reverse()); + }, + + testSize: function(){ + this.assertEqual(4, [0, 1, 2, 3].size()); + this.assertEqual(0, [].size()); + }, + + testUniq: function(){ + this.assertEnumEqual([1], [1, 1, 1].uniq()); + this.assertEnumEqual([1], [1].uniq()); + this.assertEnumEqual([], [].uniq()); + this.assertEnumEqual([0, 1, 2, 3], [0, 1, 2, 2, 3, 0, 2].uniq()); + this.assertEnumEqual([0, 1, 2, 3], [0, 0, 1, 1, 2, 3, 3, 3].uniq(true)); + }, + + testWithout: function(){ + this.assertEnumEqual([], [].without(0)); + this.assertEnumEqual([], [0].without(0)); + this.assertEnumEqual([1], [0,1].without(0)); + this.assertEnumEqual([1,2], [0,1,2].without(0)); + }, + + test$w: function(){ + this.assertEnumEqual(['a', 'b', 'c', 'd'], $w('a b c d')); + this.assertEnumEqual([], $w(' ')); + this.assertEnumEqual([], $w('')); + this.assertEnumEqual([], $w(null)); + this.assertEnumEqual([], $w(undefined)); + this.assertEnumEqual([], $w()); + this.assertEnumEqual([], $w(10)); + this.assertEnumEqual(['a'], $w('a')); + this.assertEnumEqual(['a'], $w('a ')); + this.assertEnumEqual(['a'], $w(' a')); + this.assertEnumEqual(['a', 'b', 'c', 'd'], $w(' a b\nc\t\nd\n')); + } +});
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/base_test.js b/dom/tests/mochitest/ajax/prototype/test/unit/base_test.js new file mode 100644 index 0000000000..f2843128a0 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/base_test.js @@ -0,0 +1,511 @@ +new Test.Unit.Runner({ + testFunctionArgumentNames: function() { + this.assertEnumEqual([], (function() {}).argumentNames()); + this.assertEnumEqual(["one"], (function(one) {}).argumentNames()); + this.assertEnumEqual(["one", "two", "three"], (function(one, two, three) {}).argumentNames()); + this.assertEnumEqual(["one", "two", "three"], (function( one , two + , three ) {}).argumentNames()); + this.assertEqual("$super", (function($super) {}).argumentNames().first()); + + function named1() {}; + this.assertEnumEqual([], named1.argumentNames()); + function named2(one) {}; + this.assertEnumEqual(["one"], named2.argumentNames()); + function named3(one, two, three) {}; + this.assertEnumEqual(["one", "two", "three"], named3.argumentNames()); + }, + + testFunctionBind: function() { + function methodWithoutArguments() { return this.hi }; + function methodWithArguments() { return this.hi + ',' + $A(arguments).join(',') }; + var func = Prototype.emptyFunction; + + this.assertIdentical(func, func.bind()); + this.assertIdentical(func, func.bind(undefined)); + this.assertNotIdentical(func, func.bind(null)); + + this.assertEqual('without', methodWithoutArguments.bind({ hi: 'without' })()); + this.assertEqual('with,arg1,arg2', methodWithArguments.bind({ hi: 'with' })('arg1','arg2')); + this.assertEqual('withBindArgs,arg1,arg2', + methodWithArguments.bind({ hi: 'withBindArgs' }, 'arg1', 'arg2')()); + this.assertEqual('withBindArgsAndArgs,arg1,arg2,arg3,arg4', + methodWithArguments.bind({ hi: 'withBindArgsAndArgs' }, 'arg1', 'arg2')('arg3', 'arg4')); + }, + + testFunctionCurry: function() { + var split = function(delimiter, string) { return string.split(delimiter); }; + var splitOnColons = split.curry(":"); + this.assertNotIdentical(split, splitOnColons); + this.assertEnumEqual(split(":", "0:1:2:3:4:5"), splitOnColons("0:1:2:3:4:5")); + this.assertIdentical(split, split.curry()); + }, + + testFunctionDelay: function() { + window.delayed = undefined; + var delayedFunction = function() { window.delayed = true; }; + var delayedFunctionWithArgs = function() { window.delayedWithArgs = $A(arguments).join(' '); }; + delayedFunction.delay(0.8); + delayedFunctionWithArgs.delay(0.8, 'hello', 'world'); + this.assertUndefined(window.delayed); + this.wait(1000, function() { + this.assert(window.delayed); + this.assertEqual('hello world', window.delayedWithArgs); + }); + }, + + testFunctionWrap: function() { + function sayHello(){ + return 'hello world'; + }; + + this.assertEqual('HELLO WORLD', sayHello.wrap(function(proceed) { + return proceed().toUpperCase(); + })()); + + var temp = String.prototype.capitalize; + String.prototype.capitalize = String.prototype.capitalize.wrap(function(proceed, eachWord) { + if (eachWord && this.include(' ')) return this.split(' ').map(function(str){ + return str.capitalize(); + }).join(' '); + return proceed(); + }); + this.assertEqual('Hello world', 'hello world'.capitalize()); + this.assertEqual('Hello World', 'hello world'.capitalize(true)); + this.assertEqual('Hello', 'hello'.capitalize()); + String.prototype.capitalize = temp; + }, + + testFunctionDefer: function() { + window.deferred = undefined; + var deferredFunction = function() { window.deferred = true; }; + deferredFunction.defer(); + this.assertUndefined(window.deferred); + this.wait(50, function() { + this.assert(window.deferred); + + window.deferredValue = 0; + var deferredFunction2 = function(arg) { window.deferredValue = arg; }; + deferredFunction2.defer('test'); + this.wait(50, function() { + this.assertEqual('test', window.deferredValue); + }); + }); + }, + + testFunctionMethodize: function() { + var Foo = { bar: function(baz) { return baz } }; + var baz = { quux: Foo.bar.methodize() }; + + this.assertEqual(Foo.bar.methodize(), baz.quux); + this.assertEqual(baz, Foo.bar(baz)); + this.assertEqual(baz, baz.quux()); + }, + + testObjectExtend: function() { + var object = {foo: 'foo', bar: [1, 2, 3]}; + this.assertIdentical(object, Object.extend(object)); + this.assertHashEqual({foo: 'foo', bar: [1, 2, 3]}, object); + this.assertIdentical(object, Object.extend(object, {bla: 123})); + this.assertHashEqual({foo: 'foo', bar: [1, 2, 3], bla: 123}, object); + this.assertHashEqual({foo: 'foo', bar: [1, 2, 3], bla: null}, + Object.extend(object, {bla: null})); + }, + + testObjectToQueryString: function() { + this.assertEqual('a=A&b=B&c=C&d=D%23', Object.toQueryString({a: 'A', b: 'B', c: 'C', d: 'D#'})); + }, + + testObjectClone: function() { + var object = {foo: 'foo', bar: [1, 2, 3]}; + this.assertNotIdentical(object, Object.clone(object)); + this.assertHashEqual(object, Object.clone(object)); + this.assertHashEqual({}, Object.clone()); + var clone = Object.clone(object); + delete clone.bar; + this.assertHashEqual({foo: 'foo'}, clone, + "Optimizing Object.clone perf using prototyping doesn't allow properties to be deleted."); + }, + + testObjectInspect: function() { + this.assertEqual('undefined', Object.inspect()); + this.assertEqual('undefined', Object.inspect(undefined)); + this.assertEqual('null', Object.inspect(null)); + this.assertEqual("'foo\\\\b\\\'ar'", Object.inspect('foo\\b\'ar')); + this.assertEqual('[]', Object.inspect([])); + this.assertNothingRaised(function() { Object.inspect(window.Node) }); + }, + + testObjectToJSON: function() { + this.assertUndefined(Object.toJSON(undefined)); + this.assertUndefined(Object.toJSON(Prototype.K)); + this.assertEqual('\"\"', Object.toJSON('')); + this.assertEqual('[]', Object.toJSON([])); + this.assertEqual('[\"a\"]', Object.toJSON(['a'])); + this.assertEqual('[\"a\", 1]', Object.toJSON(['a', 1])); + this.assertEqual('[\"a\", {\"b\": null}]', Object.toJSON(['a', {'b': null}])); + this.assertEqual('{\"a\": \"hello!\"}', Object.toJSON({a: 'hello!'})); + this.assertEqual('{}', Object.toJSON({})); + this.assertEqual('{}', Object.toJSON({a: undefined, b: undefined, c: Prototype.K})); + this.assertEqual('{\"b\": [false, true], \"c\": {\"a\": \"hello!\"}}', + Object.toJSON({'b': [undefined, false, true, undefined], c: {a: 'hello!'}})); + this.assertEqual('{\"b\": [false, true], \"c\": {\"a\": \"hello!\"}}', + Object.toJSON($H({'b': [undefined, false, true, undefined], c: {a: 'hello!'}}))); + this.assertEqual('true', Object.toJSON(true)); + this.assertEqual('false', Object.toJSON(false)); + this.assertEqual('null', Object.toJSON(null)); + var sam = new Person('sam'); + this.assertEqual('-sam', Object.toJSON(sam)); + this.assertEqual('-sam', sam.toJSON()); + var element = $('test'); + this.assertUndefined(Object.toJSON(element)); + element.toJSON = function(){return 'I\'m a div with id test'}; + this.assertEqual('I\'m a div with id test', Object.toJSON(element)); + }, + + testObjectToHTML: function() { + this.assertIdentical('', Object.toHTML()); + this.assertIdentical('', Object.toHTML('')); + this.assertIdentical('', Object.toHTML(null)); + this.assertIdentical('0', Object.toHTML(0)); + this.assertIdentical('123', Object.toHTML(123)); + this.assertEqual('hello world', Object.toHTML('hello world')); + this.assertEqual('hello world', Object.toHTML({toHTML: function() { return 'hello world' }})); + }, + + testObjectIsArray: function() { + this.assert(Object.isArray([])); + this.assert(Object.isArray([0])); + this.assert(Object.isArray([0, 1])); + this.assert(!Object.isArray({})); + this.assert(!Object.isArray($('list').childNodes)); + this.assert(!Object.isArray()); + this.assert(!Object.isArray('')); + this.assert(!Object.isArray('foo')); + this.assert(!Object.isArray(0)); + this.assert(!Object.isArray(1)); + this.assert(!Object.isArray(null)); + this.assert(!Object.isArray(true)); + this.assert(!Object.isArray(false)); + this.assert(!Object.isArray(undefined)); + }, + + testObjectIsHash: function() { + this.assert(Object.isHash($H())); + this.assert(Object.isHash(new Hash())); + this.assert(!Object.isHash({})); + this.assert(!Object.isHash(null)); + this.assert(!Object.isHash()); + this.assert(!Object.isHash('')); + this.assert(!Object.isHash(2)); + this.assert(!Object.isHash(false)); + this.assert(!Object.isHash(true)); + this.assert(!Object.isHash([])); + }, + + testObjectIsElement: function() { + this.assert(Object.isElement(document.createElement('div'))); + this.assert(Object.isElement(new Element('div'))); + this.assert(Object.isElement($('testlog'))); + this.assert(!Object.isElement(document.createTextNode('bla'))); + + // falsy variables should not mess up return value type + this.assertIdentical(false, Object.isElement(0)); + this.assertIdentical(false, Object.isElement('')); + this.assertIdentical(false, Object.isElement(NaN)); + this.assertIdentical(false, Object.isElement(null)); + this.assertIdentical(false, Object.isElement(undefined)); + }, + + testObjectIsFunction: function() { + this.assert(Object.isFunction(function() { })); + this.assert(Object.isFunction(Class.create())); + this.assert(!Object.isFunction("a string")); + this.assert(!Object.isFunction($("testlog"))); + this.assert(!Object.isFunction([])); + this.assert(!Object.isFunction({})); + this.assert(!Object.isFunction(0)); + this.assert(!Object.isFunction(false)); + this.assert(!Object.isFunction(undefined)); + this.assert(!Object.isFunction(/foo/)); + this.assert(!Object.isFunction(document.getElementsByTagName('div'))); + }, + + testObjectIsString: function() { + this.assert(!Object.isString(function() { })); + this.assert(Object.isString("a string")); + this.assert(!Object.isString(0)); + this.assert(!Object.isString([])); + this.assert(!Object.isString({})); + this.assert(!Object.isString(false)); + this.assert(!Object.isString(undefined)); + }, + + testObjectIsNumber: function() { + this.assert(Object.isNumber(0)); + this.assert(Object.isNumber(1.0)); + this.assert(!Object.isNumber(function() { })); + this.assert(!Object.isNumber("a string")); + this.assert(!Object.isNumber([])); + this.assert(!Object.isNumber({})); + this.assert(!Object.isNumber(false)); + this.assert(!Object.isNumber(undefined)); + }, + + testObjectIsUndefined: function() { + this.assert(Object.isUndefined(undefined)); + this.assert(!Object.isUndefined(null)); + this.assert(!Object.isUndefined(false)); + this.assert(!Object.isUndefined(0)); + this.assert(!Object.isUndefined("")); + this.assert(!Object.isUndefined(function() { })); + this.assert(!Object.isUndefined([])); + this.assert(!Object.isUndefined({})); + }, + + // sanity check + testDoesntExtendObjectPrototype: function() { + // for-in is supported with objects + var iterations = 0, obj = { a: 1, b: 2, c: 3 }; + for (property in obj) iterations++; + this.assertEqual(3, iterations); + + // for-in is not supported with arrays + iterations = 0; + var arr = [1,2,3]; + for (property in arr) iterations++; + this.assert(iterations > 3); + }, + + + testBindAsEventListener: function() { + for ( var i = 0; i < 10; ++i ) { + var div = document.createElement('div'); + div.setAttribute('id','test-'+i); + document.body.appendChild(div); + var tobj = new TestObj(); + var eventTest = { test: true }; + var call = tobj.assertingEventHandler.bindAsEventListener(tobj, + this.assertEqual.bind(this, eventTest), + this.assertEqual.bind(this, arg1), + this.assertEqual.bind(this, arg2), + this.assertEqual.bind(this, arg3), arg1, arg2, arg3 ); + call(eventTest); + } + }, + + testDateToJSON: function() { + this.assertEqual('\"1970-01-01T00:00:00Z\"', new Date(Date.UTC(1970, 0, 1)).toJSON()); + }, + + testRegExpEscape: function() { + this.assertEqual('word', RegExp.escape('word')); + this.assertEqual('\\/slashes\\/', RegExp.escape('/slashes/')); + this.assertEqual('\\\\backslashes\\\\', RegExp.escape('\\backslashes\\')); + this.assertEqual('\\\\border of word', RegExp.escape('\\border of word')); + + this.assertEqual('\\(\\?\\:non-capturing\\)', RegExp.escape('(?:non-capturing)')); + this.assertEqual('non-capturing', new RegExp(RegExp.escape('(?:') + '([^)]+)').exec('(?:non-capturing)')[1]); + + this.assertEqual('\\(\\?\\=positive-lookahead\\)', RegExp.escape('(?=positive-lookahead)')); + this.assertEqual('positive-lookahead', new RegExp(RegExp.escape('(?=') + '([^)]+)').exec('(?=positive-lookahead)')[1]); + + this.assertEqual('\\(\\?<\\=positive-lookbehind\\)', RegExp.escape('(?<=positive-lookbehind)')); + this.assertEqual('positive-lookbehind', new RegExp(RegExp.escape('(?<=') + '([^)]+)').exec('(?<=positive-lookbehind)')[1]); + + this.assertEqual('\\(\\?\\!negative-lookahead\\)', RegExp.escape('(?!negative-lookahead)')); + this.assertEqual('negative-lookahead', new RegExp(RegExp.escape('(?!') + '([^)]+)').exec('(?!negative-lookahead)')[1]); + + this.assertEqual('\\(\\?<\\!negative-lookbehind\\)', RegExp.escape('(?<!negative-lookbehind)')); + this.assertEqual('negative-lookbehind', new RegExp(RegExp.escape('(?<!') + '([^)]+)').exec('(?<!negative-lookbehind)')[1]); + + this.assertEqual('\\[\\\\w\\]\\+', RegExp.escape('[\\w]+')); + this.assertEqual('character class', new RegExp(RegExp.escape('[') + '([^\\]]+)').exec('[character class]')[1]); + + this.assertEqual('<div>', new RegExp(RegExp.escape('<div>')).exec('<td><div></td>')[0]); + + this.assertEqual('false', RegExp.escape(false)); + this.assertEqual('undefined', RegExp.escape()); + this.assertEqual('null', RegExp.escape(null)); + this.assertEqual('42', RegExp.escape(42)); + + this.assertEqual('\\\\n\\\\r\\\\t', RegExp.escape('\\n\\r\\t')); + this.assertEqual('\n\r\t', RegExp.escape('\n\r\t')); + this.assertEqual('\\{5,2\\}', RegExp.escape('{5,2}')); + + this.assertEqual( + '\\/\\(\\[\\.\\*\\+\\?\\^\\=\\!\\:\\$\\{\\}\\(\\)\\|\\[\\\\\\]\\\\\\\/\\\\\\\\\\]\\)\\/g', + RegExp.escape('/([.*+?^=!:${}()|[\\]\\/\\\\])/g') + ); + }, + + testBrowserDetection: function() { + var results = $H(Prototype.Browser).map(function(engine){ + return engine; + }).partition(function(engine){ + return engine[1] === true + }); + var trues = results[0], falses = results[1]; + + this.info('User agent string is: ' + navigator.userAgent); + + this.assert(trues.size() == 0 || trues.size() == 1, + 'There should be only one or no browser detected.'); + + // we should have definite trues or falses here + trues.each(function(result) { + this.assert(result[1] === true); + }, this); + falses.each(function(result) { + this.assert(result[1] === false); + }, this); + + if (navigator.userAgent.indexOf('AppleWebKit/') > -1) { + this.info('Running on WebKit'); + this.assert(Prototype.Browser.WebKit); + } + + if (!!window.opera) { + this.info('Running on Opera'); + this.assert(Prototype.Browser.Opera); + } + + if (!!(window.attachEvent && !window.opera)) { + this.info('Running on IE'); + this.assert(Prototype.Browser.IE); + } + + if (navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1) { + this.info('Running on Gecko'); + this.assert(Prototype.Browser.Gecko); + } + }, + + testClassCreate: function() { + this.assert(Object.isFunction(Animal), 'Animal is not a constructor'); + this.assertEnumEqual([Cat, Mouse, Dog, Ox], Animal.subclasses); + Animal.subclasses.each(function(subclass) { + this.assertEqual(Animal, subclass.superclass); + }, this); + + var Bird = Class.create(Animal); + this.assertEqual(Bird, Animal.subclasses.last()); + // for..in loop (for some reason) doesn't iterate over the constructor property in top-level classes + this.assertEnumEqual(Object.keys(new Animal).sort(), Object.keys(new Bird).without('constructor').sort()); + }, + + testClassInstantiation: function() { + var pet = new Animal("Nibbles"); + this.assertEqual("Nibbles", pet.name, "property not initialized"); + this.assertEqual('Nibbles: Hi!', pet.say('Hi!')); + this.assertEqual(Animal, pet.constructor, "bad constructor reference"); + this.assertUndefined(pet.superclass); + + var Empty = Class.create(); + this.assert('object', typeof new Empty); + }, + + testInheritance: function() { + var tom = new Cat('Tom'); + this.assertEqual(Cat, tom.constructor, "bad constructor reference"); + this.assertEqual(Animal, tom.constructor.superclass, 'bad superclass reference'); + this.assertEqual('Tom', tom.name); + this.assertEqual('Tom: meow', tom.say('meow')); + this.assertEqual('Tom: Yuk! I only eat mice.', tom.eat(new Animal)); + }, + + testSuperclassMethodCall: function() { + var tom = new Cat('Tom'); + this.assertEqual('Tom: Yum!', tom.eat(new Mouse)); + + // augment the constructor and test + var Dodo = Class.create(Animal, { + initialize: function($super, name) { + $super(name); + this.extinct = true; + }, + + say: function($super, message) { + return $super(message) + " honk honk"; + } + }); + + var gonzo = new Dodo('Gonzo'); + this.assertEqual('Gonzo', gonzo.name); + this.assert(gonzo.extinct, 'Dodo birds should be extinct'); + this.assertEqual("Gonzo: hello honk honk", gonzo.say("hello")); + }, + + testClassAddMethods: function() { + var tom = new Cat('Tom'); + var jerry = new Mouse('Jerry'); + + Animal.addMethods({ + sleep: function() { + return this.say('ZZZ'); + } + }); + + Mouse.addMethods({ + sleep: function($super) { + return $super() + " ... no, can't sleep! Gotta steal cheese!"; + }, + escape: function(cat) { + return this.say('(from a mousehole) Take that, ' + cat.name + '!'); + } + }); + + this.assertEqual('Tom: ZZZ', tom.sleep(), "added instance method not available to subclass"); + this.assertEqual("Jerry: ZZZ ... no, can't sleep! Gotta steal cheese!", jerry.sleep()); + this.assertEqual("Jerry: (from a mousehole) Take that, Tom!", jerry.escape(tom)); + // insure that a method has not propagated *up* the prototype chain: + this.assertUndefined(tom.escape); + this.assertUndefined(new Animal().escape); + + Animal.addMethods({ + sleep: function() { + return this.say('zZzZ'); + } + }); + + this.assertEqual("Jerry: zZzZ ... no, can't sleep! Gotta steal cheese!", jerry.sleep()); + }, + + testBaseClassWithMixin: function() { + var grass = new Plant('grass', 3); + this.assertRespondsTo('getValue', grass); + this.assertEqual('#<Plant: grass>', grass.inspect()); + }, + + testSubclassWithMixin: function() { + var snoopy = new Dog('Snoopy', 12, 'male'); + this.assertRespondsTo('reproduce', snoopy); + }, + + testSubclassWithMixins: function() { + var cow = new Ox('cow', 400, 'female'); + this.assertEqual('#<Ox: cow>', cow.inspect()); + this.assertRespondsTo('reproduce', cow); + this.assertRespondsTo('getValue', cow); + }, + + testClassWithToStringAndValueOfMethods: function() { + var Foo = Class.create({ + toString: function() { return "toString" }, + valueOf: function() { return "valueOf" } + }); + + var Parent = Class.create({ + m1: function(){ return 'm1' }, + m2: function(){ return 'm2' } + }); + var Child = Class.create(Parent, { + m1: function($super) { return 'm1 child' }, + m2: function($super) { return 'm2 child' } + }); + + this.assert(new Child().m1.toString().indexOf('m1 child') > -1); + + this.assertEqual("toString", new Foo().toString()); + this.assertEqual("valueOf", new Foo().valueOf()); + } +});
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/dom_test.js b/dom/tests/mochitest/ajax/prototype/test/unit/dom_test.js new file mode 100644 index 0000000000..ebb3b383ec --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/dom_test.js @@ -0,0 +1,1404 @@ +var getInnerHTML = function(id) { + return $(id).innerHTML.toString().toLowerCase().gsub(/[\r\n\t]/, ''); +}; +var createParagraph = function(text) { + var p = document.createElement('p'); + p.appendChild(document.createTextNode(text)); + return p; +} + +new Test.Unit.Runner({ + setup: function() { + if (documentViewportProperties) return; + // Based on properties check from http://www.quirksmode.org/viewport/compatibility.html + documentViewportProperties = { + properties : [ + 'self.pageXOffset', 'self.pageYOffset', + 'self.screenX', 'self.screenY', + 'self.innerHeight', 'self.innerWidth', + 'self.outerHeight', 'self.outerWidth', + 'self.screen.height', 'self.screen.width', + 'self.screen.availHeight', 'self.screen.availWidth', + 'self.screen.availTop', 'self.screen.availLeft', + 'self.screen.Top', 'self.screen.Left', + 'self.screenTop', 'self.screenLeft', + 'document.body.clientHeight', 'document.body.clientWidth', + 'document.body.scrollHeight', 'document.body.scrollWidth', + 'document.body.scrollLeft', 'document.body.scrollTop', + 'document.body.offsetHeight', 'document.body.offsetWidth', + 'document.body.offsetTop', 'document.body.offsetLeft' + ].inject([], function(properties, prop) { + if (!self.screen && prop.include('self.screen')) return; + if (!document.body && prop.include('document.body')) return; + properties.push(prop); + if (prop.include('.body') && document.documentElement) + properties.push(prop.sub('.body', '.documentElement')); + return properties; + }), + + inspect : function() { + var props = []; + this.properties.each(function(prop) { + if (eval(prop)) props[prop] = eval(prop); + }, this); + return props; + } + }; + }, + + testDollarFunction: function() { + this.assertUndefined($()); + + this.assertNull(document.getElementById('noWayThisIDExists')); + this.assertNull($('noWayThisIDExists')); + + this.assertIdentical(document.getElementById('testdiv'), $('testdiv')); + this.assertEnumEqual([ $('testdiv'), $('container') ], $('testdiv', 'container')); + this.assertEnumEqual([ $('testdiv'), undefined, $('container') ], + $('testdiv', 'noWayThisIDExists', 'container')); + var elt = $('testdiv'); + this.assertIdentical(elt, $(elt)); + this.assertRespondsTo('hide', elt); + this.assertRespondsTo('childOf', elt); + }, + + testGetElementsByClassName: function() { + if (document.getElementsByClassName.toString().include('[native code]')) { + this.info("browser uses native getElementsByClassName; skipping tests"); + return; + } + + + var div = $('class_names'), list = $('class_names_ul'); + + this.assertElementsMatch(document.getElementsByClassName('A'), 'p.A', 'ul#class_names_ul.A', 'li.A.C'); + + if (Prototype.Browser.IE) + this.assertUndefined(document.getElementById('unextended').show); + + this.assertElementsMatch(div.getElementsByClassName('B'), 'ul#class_names_ul.A.B', 'div.B.C.D'); + this.assertElementsMatch(div.getElementsByClassName('D C B'), 'div.B.C.D'); + this.assertElementsMatch(div.getElementsByClassName(' D\nC\tB '), 'div.B.C.D'); + this.assertElementsMatch(div.getElementsByClassName($w('D C B'))); + this.assertElementsMatch(list.getElementsByClassName('A'), 'li.A.C'); + this.assertElementsMatch(list.getElementsByClassName(' A '), 'li.A.C'); + this.assertElementsMatch(list.getElementsByClassName('C A'), 'li.A.C'); + this.assertElementsMatch(list.getElementsByClassName("C\nA "), 'li.A.C'); + this.assertElementsMatch(list.getElementsByClassName('B')); + this.assertElementsMatch(list.getElementsByClassName('1'), 'li.1'); + this.assertElementsMatch(list.getElementsByClassName([1]), 'li.1'); + this.assertElementsMatch(list.getElementsByClassName(['1 junk'])); + this.assertElementsMatch(list.getElementsByClassName('')); + this.assertElementsMatch(list.getElementsByClassName(' ')); + this.assertElementsMatch(list.getElementsByClassName([''])); + this.assertElementsMatch(list.getElementsByClassName([' ', ''])); + this.assertElementsMatch(list.getElementsByClassName({})); + + // those lookups shouldn't have extended all nodes in document + if (Prototype.Browser.IE) this.assertUndefined(document.getElementById('unextended')['show']); + }, + + testElementInsertWithHTML: function() { + Element.insert('insertions-main', {before:'<p><em>before</em> text</p><p>more testing</p>'}); + this.assert(getInnerHTML('insertions-container').startsWith('<p><em>before</em> text</p><p>more testing</p>')); + Element.insert('insertions-main', {after:'<p><em>after</em> text</p><p>more testing</p>'}); + this.assert(getInnerHTML('insertions-container').endsWith('<p><em>after</em> text</p><p>more testing</p>')); + Element.insert('insertions-main', {top:'<p><em>top</em> text.</p><p>more testing</p>'}); + this.assert(getInnerHTML('insertions-main').startsWith('<p><em>top</em> text.</p><p>more testing</p>')); + Element.insert('insertions-main', {bottom:'<p><em>bottom</em> text.</p><p>more testing</p>'}); + this.assert(getInnerHTML('insertions-main').endsWith('<p><em>bottom</em> text.</p><p>more testing</p>')); + }, + + testElementInsertWithDOMNode: function() { + Element.insert('insertions-node-main', {before: createParagraph('node before')}); + this.assert(getInnerHTML('insertions-node-container').startsWith('<p>node before</p>')); + Element.insert('insertions-node-main', {after: createParagraph('node after')}); + this.assert(getInnerHTML('insertions-node-container').endsWith('<p>node after</p>')); + Element.insert('insertions-node-main', {top:createParagraph('node top')}); + this.assert(getInnerHTML('insertions-node-main').startsWith('<p>node top</p>')); + Element.insert('insertions-node-main', {bottom:createParagraph('node bottom')}); + this.assert(getInnerHTML('insertions-node-main').endsWith('<p>node bottom</p>')); + this.assertEqual($('insertions-node-main'), $('insertions-node-main').insert(document.createElement('p'))); + }, + + testElementInsertWithToElementMethod: function() { + Element.insert('insertions-node-main', {toElement: createParagraph.curry('toElement') }); + this.assert(getInnerHTML('insertions-node-main').endsWith('<p>toelement</p>')); + Element.insert('insertions-node-main', {bottom: {toElement: createParagraph.curry('bottom toElement') }}); + this.assert(getInnerHTML('insertions-node-main').endsWith('<p>bottom toelement</p>')); + }, + + testElementInsertWithToHTMLMethod: function() { + Element.insert('insertions-node-main', {toHTML: function() { return '<p>toHTML</p>'} }); + this.assert(getInnerHTML('insertions-node-main').endsWith('<p>tohtml</p>')); + Element.insert('insertions-node-main', {bottom: {toHTML: function() { return '<p>bottom toHTML</p>'} }}); + this.assert(getInnerHTML('insertions-node-main').endsWith('<p>bottom tohtml</p>')); + }, + + testElementInsertWithNonString: function() { + Element.insert('insertions-main', {bottom:3}); + this.assert(getInnerHTML('insertions-main').endsWith('3')); + }, + + testElementInsertInTables: function() { + Element.insert('second_row', {after:'<tr id="third_row"><td>Third Row</td></tr>'}); + this.assert($('second_row').descendantOf('table')); + + $('a_cell').insert({top:'hello world'}); + this.assert($('a_cell').innerHTML.startsWith('hello world')); + $('a_cell').insert({after:'<td>hi planet</td>'}); + this.assertEqual('hi planet', $('a_cell').next().innerHTML); + $('table_for_insertions').insert('<tr><td>a cell!</td></tr>'); + this.assert($('table_for_insertions').innerHTML.gsub('\r\n', '').toLowerCase().include('<tr><td>a cell!</td></tr>')); + $('row_1').insert({after:'<tr></tr><tr></tr><tr><td>last</td></tr>'}); + this.assertEqual('last', $A($('table_for_row_insertions').getElementsByTagName('tr')).last().lastChild.innerHTML); + }, + + testElementInsertInSelect: function() { + var selectTop = $('select_for_insert_top'), selectBottom = $('select_for_insert_bottom'); + selectBottom.insert('<option value="33">option 33</option><option selected="selected">option 45</option>'); + this.assertEqual('option 45', selectBottom.getValue()); + selectTop.insert({top:'<option value="A">option A</option><option value="B" selected="selected">option B</option>'}); + this.assertEqual(4, selectTop.options.length); + }, + + testElementMethodInsert: function() { + $('element-insertions-main').insert({before:'some text before'}); + this.assert(getInnerHTML('element-insertions-container').startsWith('some text before')); + $('element-insertions-main').insert({after:'some text after'}); + this.assert(getInnerHTML('element-insertions-container').endsWith('some text after')); + $('element-insertions-main').insert({top:'some text top'}); + this.assert(getInnerHTML('element-insertions-main').startsWith('some text top')); + $('element-insertions-main').insert({bottom:'some text bottom'}); + this.assert(getInnerHTML('element-insertions-main').endsWith('some text bottom')); + + $('element-insertions-main').insert('some more text at the bottom'); + this.assert(getInnerHTML('element-insertions-main').endsWith('some more text at the bottom')); + + $('element-insertions-main').insert({TOP:'some text uppercase top'}); + this.assert(getInnerHTML('element-insertions-main').startsWith('some text uppercase top')); + + $('element-insertions-multiple-main').insert({ + top:'1', bottom:2, before: new Element('p').update('3'), after:'4' + }); + this.assert(getInnerHTML('element-insertions-multiple-main').startsWith('1')); + this.assert(getInnerHTML('element-insertions-multiple-main').endsWith('2')); + this.assert(getInnerHTML('element-insertions-multiple-container').startsWith('<p>3</p>')); + this.assert(getInnerHTML('element-insertions-multiple-container').endsWith('4')); + + $('element-insertions-main').update('test'); + $('element-insertions-main').insert(null); + $('element-insertions-main').insert({bottom:null}); + this.assertEqual('test', getInnerHTML('element-insertions-main')); + $('element-insertions-main').insert(1337); + this.assertEqual('test1337', getInnerHTML('element-insertions-main')); + }, + + testNewElementInsert: function() { + var container = new Element('div'); + element = new Element('div'); + container.insert(element); + + element.insert({ before: '<p>a paragraph</p>' }); + this.assertEqual('<p>a paragraph</p><div></div>', getInnerHTML(container)); + element.insert({ after: 'some text' }); + this.assertEqual('<p>a paragraph</p><div></div>some text', getInnerHTML(container)); + + element.insert({ top: '<p>a paragraph</p>' }); + this.assertEqual('<p>a paragraph</p>', getInnerHTML(element)); + element.insert('some text'); + this.assertEqual('<p>a paragraph</p>some text', getInnerHTML(element)); + }, + + testInsertionBackwardsCompatibility: function() { + new Insertion.Before('element-insertions-main', 'some backward-compatibility testing before'); + this.assert(getInnerHTML('element-insertions-container').include('some backward-compatibility testing before')); + new Insertion.After('element-insertions-main', 'some backward-compatibility testing after'); + this.assert(getInnerHTML('element-insertions-container').include('some backward-compatibility testing after')); + new Insertion.Top('element-insertions-main', 'some backward-compatibility testing top'); + this.assert(getInnerHTML('element-insertions-main').startsWith('some backward-compatibility testing top')); + new Insertion.Bottom('element-insertions-main', 'some backward-compatibility testing bottom'); + this.assert(getInnerHTML('element-insertions-main').endsWith('some backward-compatibility testing bottom')); + }, + + testElementWrap: function() { + var element = $('wrap'), parent = document.createElement('div'); + element.wrap(); + this.assert(getInnerHTML('wrap-container').startsWith('<div><p')); + element.wrap('div'); + this.assert(getInnerHTML('wrap-container').startsWith('<div><div><p')); + + element.wrap(parent); + this.assert(Object.isFunction(parent.setStyle)); + this.assert(getInnerHTML('wrap-container').startsWith('<div><div><div><p')); + + element.wrap('div', {className: 'wrapper'}); + this.assert(element.up().hasClassName('wrapper')); + element.wrap({className: 'other-wrapper'}); + this.assert(element.up().hasClassName('other-wrapper')); + element.wrap(new Element('div'), {className: 'yet-other-wrapper'}); + this.assert(element.up().hasClassName('yet-other-wrapper')); + + var orphan = new Element('p'), div = new Element('div'); + orphan.wrap(div); + this.assertEqual(orphan.parentNode, div); + }, + + testElementWrapReturnsWrapper: function() { + var element = new Element("div"); + var wrapper = element.wrap("div"); + this.assertNotEqual(element, wrapper); + this.assertEqual(element.up(), wrapper); + }, + + testElementVisible: function(){ + this.assertNotEqual('none', $('test-visible').style.display); + this.assertEqual('none', $('test-hidden').style.display); + }, + + testElementToggle: function(){ + $('test-toggle-visible').toggle(); + this.assert(!$('test-toggle-visible').visible()); + $('test-toggle-visible').toggle(); + this.assert($('test-toggle-visible').visible()); + $('test-toggle-hidden').toggle(); + this.assert($('test-toggle-hidden').visible()); + $('test-toggle-hidden').toggle(); + this.assert(!$('test-toggle-hidden').visible()); + }, + + testElementShow: function(){ + $('test-show-visible').show(); + this.assert($('test-show-visible').visible()); + $('test-show-hidden').show(); + this.assert($('test-show-hidden').visible()); + }, + + testElementHide: function(){ + $('test-hide-visible').hide(); + this.assert(!$('test-hide-visible').visible()); + $('test-hide-hidden').hide(); + this.assert(!$('test-hide-hidden').visible()); + }, + + testElementRemove: function(){ + $('removable').remove(); + this.assert($('removable-container').empty()); + }, + + testElementUpdate: function() { + $('testdiv').update('hello from div!'); + this.assertEqual('hello from div!', $('testdiv').innerHTML); + + Element.update('testdiv', 'another hello from div!'); + this.assertEqual('another hello from div!', $('testdiv').innerHTML); + + Element.update('testdiv', 123); + this.assertEqual('123', $('testdiv').innerHTML); + + Element.update('testdiv'); + this.assertEqual('', $('testdiv').innerHTML); + + Element.update('testdiv', ' '); + this.assert(!$('testdiv').innerHTML.empty()); + }, + + testElementUpdateWithScript: function() { + $('testdiv').update('hello from div!<script>\ntestVar="hello!";\n</'+'script>'); + this.assertEqual('hello from div!',$('testdiv').innerHTML); + this.wait(100,function(){ + this.assertEqual('hello!',testVar); + + Element.update('testdiv','another hello from div!\n<script>testVar="another hello!"</'+'script>\nhere it goes'); + + // note: IE normalizes whitespace (like line breaks) to single spaces, thus the match test + this.assertMatch(/^another hello from div!\s+here it goes$/,$('testdiv').innerHTML); + this.wait(100,function(){ + this.assertEqual('another hello!',testVar); + + Element.update('testdiv','a\n<script>testVar="a"\ntestVar="b"</'+'script>'); + this.wait(100,function(){ + this.assertEqual('b', testVar); + + Element.update('testdiv', + 'x<script>testVar2="a"</'+'script>\nblah\n'+ + 'x<script>testVar2="b"</'+'script>'); + this.wait(100,function(){ + this.assertEqual('b', testVar2); + }); + }); + }); + }); + }, + + testElementUpdateInTableRow: function() { + $('second_row').update('<td id="i_am_a_td">test</td>'); + this.assertEqual('test',$('i_am_a_td').innerHTML); + + Element.update('second_row','<td id="i_am_a_td">another <span>test</span></td>'); + this.assertEqual('another <span>test</span>',$('i_am_a_td').innerHTML.toLowerCase()); + }, + + testElementUpdateInTableCell: function() { + Element.update('a_cell','another <span>test</span>'); + this.assertEqual('another <span>test</span>',$('a_cell').innerHTML.toLowerCase()); + }, + + testElementUpdateInTable: function() { + Element.update('table','<tr><td>boo!</td></tr>'); + this.assertMatch(/^<tr>\s*<td>boo!<\/td><\/tr>$/,$('table').innerHTML.toLowerCase()); + }, + + testElementUpdateInSelect: function() { + var select = $('select_for_update'); + select.update('<option value="3">option 3</option><option selected="selected">option 4</option>'); + this.assertEqual('option 4', select.getValue()); + }, + + testElementUpdateWithDOMNode: function() { + $('testdiv').update(new Element('div').insert('bla')); + this.assertEqual('<div>bla</div>', getInnerHTML('testdiv')); + }, + + testElementUpdateWithToElementMethod: function() { + $('testdiv').update({toElement: createParagraph.curry('foo')}); + this.assertEqual('<p>foo</p>', getInnerHTML('testdiv')); + }, + + testElementUpdateWithToHTMLMethod: function() { + $('testdiv').update({toHTML: function() { return 'hello world' }}); + this.assertEqual('hello world', getInnerHTML('testdiv')); + }, + + testElementReplace: function() { + $('testdiv-replace-1').replace('hello from div!'); + this.assertEqual('hello from div!', $('testdiv-replace-container-1').innerHTML); + + $('testdiv-replace-2').replace(123); + this.assertEqual('123', $('testdiv-replace-container-2').innerHTML); + + $('testdiv-replace-3').replace(); + this.assertEqual('', $('testdiv-replace-container-3').innerHTML); + + $('testrow-replace').replace('<tr><td>hello</td></tr>'); + this.assert(getInnerHTML('testrow-replace-container').include('<tr><td>hello</td></tr>')); + + $('testoption-replace').replace('<option>hello</option>'); + this.assert($('testoption-replace-container').innerHTML.include('hello')); + + Element.replace('testinput-replace', '<p>hello world</p>'); + this.assertEqual('<p>hello world</p>', getInnerHTML('testform-replace')); + + Element.replace('testform-replace', '<form></form>'); + this.assertEqual('<p>some text</p><form></form><p>some text</p>', getInnerHTML('testform-replace-container')); + }, + + testElementReplaceWithScript: function() { + $('testdiv-replace-4').replace('hello from div!<script>testVarReplace="hello!"</'+'script>'); + this.assertEqual('hello from div!', $('testdiv-replace-container-4').innerHTML); + this.wait(100,function(){ + this.assertEqual('hello!',testVarReplace); + + $('testdiv-replace-5').replace('another hello from div!\n<script>testVarReplace="another hello!"</'+'script>\nhere it goes'); + + // note: IE normalizes whitespace (like line breaks) to single spaces, thus the match test + this.assertMatch(/^another hello from div!\s+here it goes$/,$('testdiv-replace-container-5').innerHTML); + this.wait(100,function(){ + this.assertEqual('another hello!',testVarReplace); + }); + }); + }, + + testElementReplaceWithDOMNode: function() { + $('testdiv-replace-element').replace(createParagraph('hello')); + this.assertEqual('<p>hello</p>', getInnerHTML('testdiv-replace-container-element')); + }, + + testElementReplaceWithToElementMethod: function() { + $('testdiv-replace-toelement').replace({toElement: createParagraph.curry('hello')}); + this.assertEqual('<p>hello</p>', getInnerHTML('testdiv-replace-container-toelement')); + }, + + testElementReplaceWithToHTMLMethod: function() { + $('testdiv-replace-tohtml').replace({toHTML: function() { return 'hello' }}); + this.assertEqual('hello', getInnerHTML('testdiv-replace-container-tohtml')); + }, + + testElementSelectorMethod: function() { + ['getElementsBySelector','select'].each(function(method) { + var testSelector = $('container')[method]('p.test'); + this.assertEqual(testSelector.length, 4); + this.assertEqual(testSelector[0], $('intended')); + this.assertEqual(testSelector[0], $$('#container p.test')[0]); + }, this); + }, + + testElementAdjacent: function() { + var elements = $('intended').adjacent('p'); + this.assertEqual(elements.length, 3); + elements.each(function(element){ + this.assert(element != $('intended')); + }, this); + }, + + testElementIdentify: function() { + var parent = $('identification'); + this.assertEqual(parent.down().identify(), 'predefined_id'); + this.assert(parent.down(1).identify().startsWith('anonymous_element_')); + this.assert(parent.down(2).identify().startsWith('anonymous_element_')); + this.assert(parent.down(3).identify().startsWith('anonymous_element_')); + + this.assert(parent.down(3).id !== parent.down(2).id); + }, + + testElementClassNameMethod: function() { + var testClassNames = $('container').getElementsByClassName('test'); + var testSelector = $('container').getElementsBySelector('p.test'); + this.assertEqual(testClassNames[0], $('intended')); + this.assertEqual(testClassNames.length, 4); + this.assertEqual(testSelector[3], testClassNames[3]); + this.assertEqual(testClassNames.length, testSelector.length); + }, + + testElementAncestors: function() { + var ancestors = $('navigation_test_f').ancestors(); + this.assertElementsMatch(ancestors, 'ul', 'li', 'ul#navigation_test', + 'div#nav_tests_isolator', 'body', 'html'); + this.assertElementsMatch(ancestors.last().ancestors()); + + var dummy = $(document.createElement('DIV')); + dummy.innerHTML = '<div></div>'.times(3); + this.assert(typeof $(dummy.childNodes[0]).ancestors()[0]['setStyle'] == 'function'); + }, + + testElementDescendants: function() { + this.assertElementsMatch($('navigation_test').descendants(), + 'li', 'em', 'li', 'em.dim', 'li', 'em', 'ul', 'li', + 'em.dim', 'li#navigation_test_f', 'em', 'li', 'em'); + this.assertElementsMatch($('navigation_test_f').descendants(), 'em'); + + var dummy = $(document.createElement('DIV')); + dummy.innerHTML = '<div></div>'.times(3); + this.assert(typeof dummy.descendants()[0].setStyle == 'function'); + }, + + testElementFirstDescendant: function() { + this.assertElementMatches($('navigation_test').firstDescendant(), 'li.first'); + this.assertNull($('navigation_test_next_sibling').firstDescendant()); + }, + + testElementChildElements: function() { + this.assertElementsMatch($('navigation_test').childElements(), + 'li.first', 'li', 'li#navigation_test_c', 'li.last'); + this.assertNotEqual(0, $('navigation_test_next_sibling').childNodes.length); + this.assertEnumEqual([], $('navigation_test_next_sibling').childElements()); + + var dummy = $(document.createElement('DIV')); + dummy.innerHTML = '<div></div>'.times(3); + this.assert(typeof dummy.childElements()[0].setStyle == 'function'); + }, + + testElementImmediateDescendants: function() { + this.assertIdentical(Element.Methods.childElements, Element.Methods.immediateDescendants); + }, + + testElementPreviousSiblings: function() { + this.assertElementsMatch($('navigation_test').previousSiblings(), + 'span#nav_test_prev_sibling', 'p.test', 'div', 'div#nav_test_first_sibling'); + this.assertElementsMatch($('navigation_test_f').previousSiblings(), 'li'); + + var dummy = $(document.createElement('DIV')); + dummy.innerHTML = '<div></div>'.times(3); + this.assert(typeof $(dummy.childNodes[1]).previousSiblings()[0].setStyle == 'function'); + }, + + testElementNextSiblings: function() { + this.assertElementsMatch($('navigation_test').nextSiblings(), + 'div#navigation_test_next_sibling', 'p'); + this.assertElementsMatch($('navigation_test_f').nextSiblings()); + + var dummy = $(document.createElement('DIV')); + dummy.innerHTML = '<div></div>'.times(3); + this.assert(typeof $(dummy.childNodes[0]).nextSiblings()[0].setStyle == 'function'); + }, + + testElementSiblings: function() { + this.assertElementsMatch($('navigation_test').siblings(), + 'div#nav_test_first_sibling', 'div', 'p.test', + 'span#nav_test_prev_sibling', 'div#navigation_test_next_sibling', 'p'); + + var dummy = $(document.createElement('DIV')); + dummy.innerHTML = '<div></div>'.times(3); + this.assert(typeof $(dummy.childNodes[0]).siblings()[0].setStyle == 'function'); + }, + + testElementUp: function() { + var element = $('navigation_test_f'); + this.assertElementMatches(element.up(), 'ul'); + this.assertElementMatches(element.up(0), 'ul'); + this.assertElementMatches(element.up(1), 'li'); + this.assertElementMatches(element.up(2), 'ul#navigation_test'); + this.assertElementsMatch(element.up('li').siblings(), 'li.first', 'li', 'li.last'); + this.assertElementMatches(element.up('ul', 1), 'ul#navigation_test'); + this.assertEqual(undefined, element.up('garbage')); + this.assertEqual(undefined, element.up(6)); + this.assertElementMatches(element.up('.non-existant, ul'), 'ul'); + + var dummy = $(document.createElement('DIV')); + dummy.innerHTML = '<div></div>'.times(3); + this.assert(typeof $(dummy.childNodes[0]).up().setStyle == 'function'); + }, + + testElementDown: function() { + var element = $('navigation_test'); + this.assertElementMatches(element.down(), 'li.first'); + this.assertElementMatches(element.down(0), 'li.first'); + this.assertElementMatches(element.down(1), 'em'); + this.assertElementMatches(element.down('li', 5), 'li.last'); + this.assertElementMatches(element.down('ul').down('li', 1), 'li#navigation_test_f'); + this.assertElementMatches(element.down('.non-existant, .first'), 'li.first'); + + var dummy = $(document.createElement('DIV')); + dummy.innerHTML = '<div></div>'.times(3); + this.assert(typeof dummy.down().setStyle == 'function'); + + var input = $$('input')[0]; + this.assertNothingRaised(function(){ input.down('span') }); + this.assertUndefined(input.down('span')); + }, + + testElementPrevious: function() { + var element = $('navigation_test').down('li.last'); + this.assertElementMatches(element.previous(), 'li#navigation_test_c'); + this.assertElementMatches(element.previous(1), 'li'); + this.assertElementMatches(element.previous('.first'), 'li.first'); + this.assertEqual(undefined, element.previous(3)); + this.assertEqual(undefined, $('navigation_test').down().previous()); + this.assertElementMatches(element.previous('.non-existant, .first'), 'li.first'); + + var dummy = $(document.createElement('DIV')); + dummy.innerHTML = '<div></div>'.times(3); + this.assert(typeof $(dummy.childNodes[1]).previous().setStyle == 'function'); + }, + + testElementNext: function() { + var element = $('navigation_test').down('li.first'); + this.assertElementMatches(element.next(), 'li'); + this.assertElementMatches(element.next(1), 'li#navigation_test_c'); + this.assertElementMatches(element.next(2), 'li.last'); + this.assertElementMatches(element.next('.last'), 'li.last'); + this.assertEqual(undefined, element.next(3)); + this.assertEqual(undefined, element.next(2).next()); + this.assertElementMatches(element.next('.non-existant, .last'), 'li.last'); + + var dummy = $(document.createElement('DIV')); + dummy.innerHTML = '<div></div>'.times(3); + this.assert(typeof $(dummy.childNodes[0]).next().setStyle == 'function'); + }, + + testElementInspect: function() { + this.assertEqual('<ul id="navigation_test">', $('navigation_test').inspect()); + this.assertEqual('<li class="first">', $('navigation_test').down().inspect()); + this.assertEqual('<em>', $('navigation_test').down(1).inspect()); + }, + + testElementMakeClipping: function() { + var chained = Element.extend(document.createElement('DIV')); + this.assertEqual(chained, chained.makeClipping()); + this.assertEqual(chained, chained.makeClipping()); + this.assertEqual(chained, chained.makeClipping().makeClipping()); + + this.assertEqual(chained, chained.undoClipping()); + this.assertEqual(chained, chained.undoClipping()); + this.assertEqual(chained, chained.undoClipping().makeClipping()); + + ['hidden','visible','scroll'].each( function(overflowValue) { + var element = $('element_with_'+overflowValue+'_overflow'); + + this.assertEqual(overflowValue, element.getStyle('overflow')); + element.makeClipping(); + this.assertEqual('hidden', element.getStyle('overflow')); + element.undoClipping(); + this.assertEqual(overflowValue, element.getStyle('overflow')); + }, this); + }, + + testElementExtend: function() { + var element = $('element_extend_test'); + this.assertRespondsTo('show', element); + + var XHTML_TAGS = $w( + 'a abbr acronym address applet area '+ + 'b bdo big blockquote br button caption '+ + 'cite code col colgroup dd del dfn div dl dt '+ + 'em fieldset form h1 h2 h3 h4 h5 h6 hr '+ + 'i iframe img input ins kbd label legend li '+ + 'map object ol optgroup option p param pre q samp '+ + 'script select small span strong style sub sup '+ + 'table tbody td textarea tfoot th thead tr tt ul var'); + + XHTML_TAGS.each(function(tag) { + var element = document.createElement(tag); + this.assertEqual(element, Element.extend(element)); + this.assertRespondsTo('show', element); + }, this); + + [null,'','a','aa'].each(function(content) { + var textnode = document.createTextNode(content); + this.assertEqual(textnode, Element.extend(textnode)); + this.assert(typeof textnode['show'] == 'undefined'); + }, this); + }, + + testElementExtendReextendsDiscardedNodes: function() { + this.assertRespondsTo('show', $('discard_1')); + $('element_reextend_test').innerHTML += '<div id="discard_2"></div>'; + this.assertRespondsTo('show', $('discard_1')); + }, + + testElementCleanWhitespace: function() { + Element.cleanWhitespace("test_whitespace"); + this.assertEqual(3, $("test_whitespace").childNodes.length); + + this.assertEqual(1, $("test_whitespace").firstChild.nodeType); + this.assertEqual('SPAN', $("test_whitespace").firstChild.tagName); + + this.assertEqual(1, $("test_whitespace").firstChild.nextSibling.nodeType); + this.assertEqual('DIV', $("test_whitespace").firstChild.nextSibling.tagName); + + this.assertEqual(1, $("test_whitespace").firstChild.nextSibling.nextSibling.nodeType); + this.assertEqual('SPAN', $("test_whitespace").firstChild.nextSibling.nextSibling.tagName); + + var element = document.createElement('DIV'); + element.appendChild(document.createTextNode('')); + element.appendChild(document.createTextNode('')); + this.assertEqual(2, element.childNodes.length); + Element.cleanWhitespace(element); + this.assertEqual(0, element.childNodes.length); + }, + + testElementEmpty: function() { + this.assert($('test-empty').empty()); + this.assert($('test-empty-but-contains-whitespace').empty()); + this.assert(!$('test-full').empty()); + }, + + testDescendantOf: function() { + this.assert($('child').descendantOf('ancestor')); + this.assert($('child').descendantOf($('ancestor'))); + + this.assert(!$('ancestor').descendantOf($('child'))); + + this.assert($('great-grand-child').descendantOf('ancestor'), 'great-grand-child < ancestor'); + this.assert($('grand-child').descendantOf('ancestor'), 'grand-child < ancestor'); + this.assert($('great-grand-child').descendantOf('grand-child'), 'great-grand-child < grand-child'); + this.assert($('grand-child').descendantOf('child'), 'grand-child < child'); + this.assert($('great-grand-child').descendantOf('child'), 'great-grand-child < child'); + + this.assert($('sibling').descendantOf('ancestor'), 'sibling < ancestor'); + this.assert($('grand-sibling').descendantOf('sibling'), 'grand-sibling < sibling'); + this.assert($('grand-sibling').descendantOf('ancestor'), 'grand-sibling < ancestor'); + + this.assert($('grand-sibling').descendantOf(document.body), 'grand-sibling < body'); + + this.assert(!$('great-grand-child').descendantOf('great-grand-child'), 'great-grand-child < great-grand-child'); + this.assert(!$('great-grand-child').descendantOf('sibling'), 'great-grand-child < sibling'); + this.assert(!$('sibling').descendantOf('child'), 'sibling < child'); + this.assert(!$('great-grand-child').descendantOf('not-in-the-family'), 'great-grand-child < not-in-the-family'); + this.assert(!$('child').descendantOf('not-in-the-family'), 'child < not-in-the-family'); + + this.assert(!$(document.body).descendantOf('great-grand-child')); + + // dynamically-created elements + $('ancestor').insert(new Element('div', { id: 'weird-uncle' })); + this.assert($('weird-uncle').descendantOf('ancestor')); + + $(document.body).insert(new Element('div', { id: 'impostor' })); + this.assert(!$('impostor').descendantOf('ancestor')); + + // test descendantOf document + this.assert($(document.body).descendantOf(document)); + this.assert($(document.documentElement).descendantOf(document)); + }, + + testChildOf: function() { + this.assert($('child').childOf('ancestor')); + this.assert($('child').childOf($('ancestor'))); + this.assert($('great-grand-child').childOf('ancestor')); + this.assert(!$('great-grand-child').childOf('not-in-the-family')); + this.assertIdentical(Element.Methods.childOf, Element.Methods.descendantOf); + }, + + testElementSetStyle: function() { + Element.setStyle('style_test_3',{ 'left': '2px' }); + this.assertEqual('2px', $('style_test_3').style.left); + + Element.setStyle('style_test_3',{ marginTop: '1px' }); + this.assertEqual('1px', $('style_test_3').style.marginTop); + + $('style_test_3').setStyle({ marginTop: '2px', left: '-1px' }); + this.assertEqual('-1px', $('style_test_3').style.left); + this.assertEqual('2px', $('style_test_3').style.marginTop); + + this.assertEqual('none', $('style_test_3').getStyle('float')); + $('style_test_3').setStyle({ 'float': 'left' }); + this.assertEqual('left', $('style_test_3').getStyle('float')); + + $('style_test_3').setStyle({ cssFloat: 'none' }); + this.assertEqual('none', $('style_test_3').getStyle('float')); + + this.assertEqual(1, $('style_test_3').getStyle('opacity')); + + $('style_test_3').setStyle({ opacity: 0.5 }); + this.assertEqual(0.5, $('style_test_3').getStyle('opacity')); + + $('style_test_3').setStyle({ opacity: '' }); + this.assertEqual(1, $('style_test_3').getStyle('opacity')); + + $('style_test_3').setStyle({ opacity: 0 }); + this.assertEqual(0, $('style_test_3').getStyle('opacity')); + + $('test_csstext_1').setStyle('font-size: 15px'); + this.assertEqual('15px', $('test_csstext_1').getStyle('font-size')); + + $('test_csstext_2').setStyle({height: '40px'}); + $('test_csstext_2').setStyle('font-size: 15px'); + this.assertEqual('15px', $('test_csstext_2').getStyle('font-size')); + this.assertEqual('40px', $('test_csstext_2').getStyle('height')); + + $('test_csstext_3').setStyle('font-size: 15px'); + this.assertEqual('15px', $('test_csstext_3').getStyle('font-size')); + this.assertEqual('1px', $('test_csstext_3').getStyle('border-top-width')); + + $('test_csstext_4').setStyle('font-size: 15px'); + this.assertEqual('15px', $('test_csstext_4').getStyle('font-size')); + + $('test_csstext_4').setStyle('float: right; font-size: 10px'); + this.assertEqual('right', $('test_csstext_4').getStyle('float')); + this.assertEqual('10px', $('test_csstext_4').getStyle('font-size')); + + $('test_csstext_5').setStyle('float: left; opacity: .5; font-size: 10px'); + this.assertEqual(parseFloat('0.5'), parseFloat($('test_csstext_5').getStyle('opacity'))); + }, + + testElementSetStyleCamelized: function() { + this.assertNotEqual('30px', $('style_test_3').style.marginTop); + $('style_test_3').setStyle({ marginTop: '30px'}, true); + this.assertEqual('30px', $('style_test_3').style.marginTop); + }, + + testElementSetOpacity: function() { + [0,0.1,0.5,0.999].each(function(opacity){ + $('style_test_3').setOpacity(opacity); + + // b/c of rounding issues on IE special case + var realOpacity = $('style_test_3').getStyle('opacity'); + + // opera rounds off to two significant digits, so we check for a + // ballpark figure + this.assert((Number(realOpacity) - opacity) <= 0.002, 'setting opacity to ' + opacity); + }, this); + + this.assertEqual(0, + $('style_test_3').setOpacity(0.0000001).getStyle('opacity')); + + // for Firefox, we don't set to 1, because of flickering + this.assert( + $('style_test_3').setOpacity(0.9999999).getStyle('opacity') > 0.999 + ); + if (Prototype.Browser.IE) { + this.assert($('style_test_4').setOpacity(0.5).currentStyle.hasLayout); + this.assert(2, $('style_test_5').setOpacity(0.5).getStyle('zoom')); + this.assert(0.5, new Element('div').setOpacity(0.5).getOpacity()); + this.assert(2, new Element('div').setOpacity(0.5).setStyle('zoom: 2;').getStyle('zoom')); + this.assert(2, new Element('div').setStyle('zoom: 2;').setOpacity(0.5).getStyle('zoom')); + } + }, + + testElementGetStyle: function() { + this.assertEqual("none", + $('style_test_1').getStyle('display')); + + // not displayed, so "null" ("auto" is tranlated to "null") + this.assertNull(Element.getStyle('style_test_1', 'width'), 'elements that are hidden should return null on getStyle("width")'); + + $('style_test_1').show(); + + // from id rule + this.assertEqual("pointer", + Element.getStyle('style_test_1','cursor')); + + this.assertEqual("block", + Element.getStyle('style_test_2','display')); + + // we should always get something for width (if displayed) + // firefox and safari automatically send the correct value, + // IE is special-cased to do the same + this.assertEqual($('style_test_2').offsetWidth+'px', Element.getStyle('style_test_2','width')); + + this.assertEqual("static",Element.getStyle('style_test_1','position')); + // from style + this.assertEqual("11px", + Element.getStyle('style_test_2','font-size')); + // from class + this.assertEqual("1px", + Element.getStyle('style_test_2','margin-left')); + + ['not_floating_none','not_floating_style','not_floating_inline'].each(function(element) { + this.assertEqual('none', $(element).getStyle('float')); + this.assertEqual('none', $(element).getStyle('cssFloat')); + }, this); + + ['floating_style','floating_inline'].each(function(element) { + this.assertEqual('left', $(element).getStyle('float')); + this.assertEqual('left', $(element).getStyle('cssFloat')); + }, this); + + this.assertEqual(0.5, $('op1').getStyle('opacity')); + this.assertEqual(0.5, $('op2').getStyle('opacity')); + this.assertEqual(1.0, $('op3').getStyle('opacity')); + + $('op1').setStyle({opacity: '0.3'}); + $('op2').setStyle({opacity: '0.3'}); + $('op3').setStyle({opacity: '0.3'}); + + this.assertEqual(0.3, $('op1').getStyle('opacity')); + this.assertEqual(0.3, $('op2').getStyle('opacity')); + this.assertEqual(0.3, $('op3').getStyle('opacity')); + + $('op3').setStyle({opacity: 0}); + this.assertEqual(0, $('op3').getStyle('opacity')); + + if (navigator.appVersion.match(/MSIE/)) { + this.assertEqual('alpha(opacity=30)', $('op1').getStyle('filter')); + this.assertEqual('progid:DXImageTransform.Microsoft.Blur(strength=10)alpha(opacity=30)', $('op2').getStyle('filter')); + $('op2').setStyle({opacity:''}); + this.assertEqual('progid:DXImageTransform.Microsoft.Blur(strength=10)', $('op2').getStyle('filter')); + this.assertEqual('alpha(opacity=0)', $('op3').getStyle('filter')); + this.assertEqual(0.3, $('op4-ie').getStyle('opacity')); + } + // verify that value is still found when using camelized + // strings (function previously used getPropertyValue() + // which expected non-camelized strings) + this.assertEqual("12px", $('style_test_1').getStyle('fontSize')); + + // getStyle on width/height should return values according to + // the CSS box-model, which doesn't include + // margin, padding, or borders + // TODO: This test fails on IE because there seems to be no way + // to calculate this properly (clientWidth/Height returns 0) + if (!navigator.appVersion.match(/MSIE/)) { + this.assertEqual("14px", $('style_test_dimensions').getStyle('width')); + this.assertEqual("17px", $('style_test_dimensions').getStyle('height')); + } + + // height/width could always be calculated if it's set to "auto" (Firefox) + this.assertNotNull($('auto_dimensions').getStyle('height')); + this.assertNotNull($('auto_dimensions').getStyle('width')); + }, + + testElementGetOpacity: function() { + this.assertEqual(0.45, $('op1').setOpacity(0.45).getOpacity()); + }, + + testElementReadAttribute: function() { + var attribFormIssues = $('attributes_with_issues_form'); + this.assertEqual('blah-class', attribFormIssues.readAttribute('class')); + this.assertEqual('post', attribFormIssues.readAttribute('method')); + this.assertEqual('string', typeof(attribFormIssues.readAttribute('action'))); + this.assertEqual('string', typeof(attribFormIssues.readAttribute('id'))); + + $(document.body).insert('<div id="ie_href_test_div"></div>'); + $('ie_href_test_div').insert('<p>blah blah</p><a id="ie_href_test" href="test.html">blah</a>'); + this.assertEqual('test.html', $('ie_href_test').readAttribute('href')); + + this.assertEqual('test.html' , $('attributes_with_issues_1').readAttribute('href')); + this.assertEqual('L' , $('attributes_with_issues_1').readAttribute('accesskey')); + this.assertEqual('50' , $('attributes_with_issues_1').readAttribute('tabindex')); + this.assertEqual('a link' , $('attributes_with_issues_1').readAttribute('title')); + + $('cloned_element_attributes_issue').readAttribute('foo') + var clone = $('cloned_element_attributes_issue').cloneNode(true); + clone.writeAttribute('foo', 'cloned'); + this.assertEqual('cloned', clone.readAttribute('foo')); + this.assertEqual('original', $('cloned_element_attributes_issue').readAttribute('foo')); + + ['href', 'accesskey', 'accesskey', 'title'].each(function(attr) { + this.assertEqual('' , $('attributes_with_issues_2').readAttribute(attr)); + }, this); + + ['checked','disabled','readonly','multiple'].each(function(attr) { + this.assertEqual(attr, $('attributes_with_issues_'+attr).readAttribute(attr)); + }, this); + + this.assertEqual("alert('hello world');", $('attributes_with_issues_1').readAttribute('onclick')); + this.assertNull($('attributes_with_issues_1').readAttribute('onmouseover')); + + this.assertEqual('date', $('attributes_with_issues_type').readAttribute('type')); + this.assertEqual('text', $('attributes_with_issues_readonly').readAttribute('type')); + + var elements = $('custom_attributes').immediateDescendants(); + this.assertEnumEqual(['1', '2'], elements.invoke('readAttribute', 'foo')); + this.assertEnumEqual(['2', null], elements.invoke('readAttribute', 'bar')); + + var table = $('write_attribute_table'); + this.assertEqual('4', table.readAttribute('cellspacing')); + this.assertEqual('6', table.readAttribute('cellpadding')); + + // test for consistent flag value across browsers + ["true", true, " ", 'rEadOnLy'].each(function(value) { + $('attributes_with_issues_readonly').writeAttribute('readonly', value); + this.assertEqual('readonly', $('attributes_with_issues_readonly').readAttribute('readonly')); + }, this); + }, + + testElementWriteAttribute: function() { + var element = Element.extend(document.body.appendChild(document.createElement('p'))); + this.assertRespondsTo('writeAttribute', element); + this.assertEqual(element, element.writeAttribute('id', 'write_attribute_test')); + this.assertEqual('write_attribute_test', element.id); + this.assertEqual('http://prototypejs.org/', $('write_attribute_link'). + writeAttribute({href: 'http://prototypejs.org/', title: 'Home of Prototype'}).href); + this.assertEqual('Home of Prototype', $('write_attribute_link').title); + + var element2 = Element.extend(document.createElement('p')); + element2.writeAttribute('id', 'write_attribute_without_hash'); + this.assertEqual('write_attribute_without_hash', element2.id); + element2.writeAttribute('animal', 'cat'); + this.assertEqual('cat', element2.readAttribute('animal')); + }, + + testElementWriteAttributeWithBooleans: function() { + var input = $('write_attribute_input'), + select = $('write_attribute_select'), + checkbox = $('write_attribute_checkbox'), + checkedCheckbox = $('write_attribute_checked_checkbox'); + this.assert( input. writeAttribute('readonly'). hasAttribute('readonly')); + this.assert(!input. writeAttribute('readonly', false). hasAttribute('readonly')); + this.assert( input. writeAttribute('readonly', true). hasAttribute('readonly')); + this.assert(!input. writeAttribute('readonly', null). hasAttribute('readonly')); + this.assert( input. writeAttribute('readonly', 'readonly').hasAttribute('readonly')); + this.assert( select. writeAttribute('multiple'). hasAttribute('multiple')); + this.assert( input. writeAttribute('disabled'). hasAttribute('disabled')); + this.assert( checkbox. writeAttribute('checked'). checked); + this.assert(!checkedCheckbox.writeAttribute('checked', false). checked); + }, + + testElementWriteAttributeWithIssues: function() { + var input = $('write_attribute_input').writeAttribute({maxlength: 90, minlength:80, tabindex: 10}), + td = $('write_attribute_td').writeAttribute({valign: 'bottom', colspan: 2, rowspan: 2}); + this.assertEqual(80, input.readAttribute('minlength')); + this.assertEqual(90, input.readAttribute('maxlength')); + this.assertEqual(10, input.readAttribute('tabindex')); + this.assertEqual(2, td.readAttribute('colspan')); + this.assertEqual(2, td.readAttribute('rowspan')); + this.assertEqual('bottom', td.readAttribute('valign')); + + var p = $('write_attribute_para'), label = $('write_attribute_label'); + this.assertEqual('some-class', p. writeAttribute({'class': 'some-class'}). readAttribute('class')); + this.assertEqual('some-className', p. writeAttribute({className: 'some-className'}).readAttribute('class')); + this.assertEqual('some-id', label.writeAttribute({'for': 'some-id'}). readAttribute('for')); + this.assertEqual('some-other-id', label.writeAttribute({htmlFor: 'some-other-id'}). readAttribute('for')); + + this.assert(p.writeAttribute({style: 'width: 5px;'}).readAttribute('style').toLowerCase().include('width')); + + var table = $('write_attribute_table'); + table.writeAttribute('cellspacing', '2') + table.writeAttribute('cellpadding', '3') + this.assertEqual('2', table.readAttribute('cellspacing')); + this.assertEqual('3', table.readAttribute('cellpadding')); + + var iframe = new Element('iframe', { frameborder: 0 }); + this.assertIdentical(0, parseInt(iframe.readAttribute('frameborder'))); + }, + + testElementWriteAttributeWithCustom: function() { + var p = $('write_attribute_para').writeAttribute({name: 'martin', location: 'stockholm', age: 26}); + this.assertEqual('martin', p.readAttribute('name')); + this.assertEqual('stockholm', p.readAttribute('location')); + this.assertEqual('26', p.readAttribute('age')); + }, + + testElementHasAttribute: function() { + var label = $('write_attribute_label'); + this.assertIdentical(true, label.hasAttribute('for')); + this.assertIdentical(false, label.hasAttribute('htmlFor')); + this.assertIdentical(false, label.hasAttribute('className')); + this.assertIdentical(false, label.hasAttribute('rainbows')); + + var input = $('write_attribute_input'); + this.assertNotIdentical(null, input.hasAttribute('readonly')); + this.assertNotIdentical(null, input.hasAttribute('readOnly')); + }, + + testNewElement: function() { + this.assert(new Element('h1')); + + var XHTML_TAGS = $w( + 'a abbr acronym address area '+ + 'b bdo big blockquote br button caption '+ + 'cite code col colgroup dd del dfn div dl dt '+ + 'em fieldset form h1 h2 h3 h4 h5 h6 hr '+ + 'i iframe img input ins kbd label legend li '+ + 'map object ol optgroup option p param pre q samp '+ + 'script select small span strong style sub sup '+ + 'table tbody td textarea tfoot th thead tr tt ul var'); + + XHTML_TAGS.each(function(tag, index) { + var id = tag + '_' + index, element = document.body.appendChild(new Element(tag, {id: id})); + this.assertEqual(tag, element.tagName.toLowerCase()); + this.assertEqual(element, document.body.lastChild); + this.assertEqual(id, element.id); + }, this); + + + this.assertRespondsTo('update', new Element('div')); + Element.addMethods({ + cheeseCake: function(){ + return 'Cheese cake'; + } + }); + + this.assertRespondsTo('cheeseCake', new Element('div')); + + /* window.ElementOld = function(tagName, attributes) { + if (Prototype.Browser.IE && attributes && attributes.name) { + tagName = '<' + tagName + ' name="' + attributes.name + '">'; + delete attributes.name; + } + return Element.extend(document.createElement(tagName)).writeAttribute(attributes || {}); + }; + + this.benchmark(function(){ + XHTML_TAGS.each(function(tagName) { new Element(tagName) }); + }, 5); + + this.benchmark(function(){ + XHTML_TAGS.each(function(tagName) { new ElementOld(tagName) }); + }, 5); */ + + this.assertEqual('foobar', new Element('a', {custom: 'foobar'}).readAttribute('custom')); + var input = document.body.appendChild(new Element('input', + {id: 'my_input_field_id', name: 'my_input_field'})); + this.assertEqual(input, document.body.lastChild); + this.assertEqual('my_input_field', $(document.body.lastChild).name); + if (Prototype.Browser.IE) + this.assertMatch(/name=["']?my_input_field["']?/, $('my_input_field').outerHTML); + + if (originalElement && Prototype.BrowserFeatures.ElementExtensions) { + Element.prototype.fooBar = Prototype.emptyFunction + this.assertRespondsTo('fooBar', new Element('div')); + } + + //test IE setting "type" property of newly created button element + var button = new Element('button', {id:'button_type_test',type: 'reset'}); + var form = $('attributes_with_issues_form'); + var input = $('attributes_with_issues_regular'); + + form.insert(button); + input.value = 1; + button.click(); + + this.assertEqual('0', input.value); + button.remove(); + }, + + testElementGetHeight: function() { + this.assertIdentical(100, $('dimensions-visible').getHeight()); + this.assertIdentical(100, $('dimensions-display-none').getHeight()); + }, + + testElementGetWidth: function() { + this.assertIdentical(200, $('dimensions-visible').getWidth()); + this.assertIdentical(200, $('dimensions-display-none').getWidth()); + }, + + testElementGetDimensions: function() { + this.assertIdentical(100, $('dimensions-visible').getDimensions().height); + this.assertIdentical(200, $('dimensions-visible').getDimensions().width); + this.assertIdentical(100, $('dimensions-display-none').getDimensions().height); + this.assertIdentical(200, $('dimensions-display-none').getDimensions().width); + + this.assertIdentical(100, $('dimensions-visible-pos-rel').getDimensions().height); + this.assertIdentical(200, $('dimensions-visible-pos-rel').getDimensions().width); + this.assertIdentical(100, $('dimensions-display-none-pos-rel').getDimensions().height); + this.assertIdentical(200, $('dimensions-display-none-pos-rel').getDimensions().width); + + this.assertIdentical(100, $('dimensions-visible-pos-abs').getDimensions().height); + this.assertIdentical(200, $('dimensions-visible-pos-abs').getDimensions().width); + this.assertIdentical(100, $('dimensions-display-none-pos-abs').getDimensions().height); + this.assertIdentical(200, $('dimensions-display-none-pos-abs').getDimensions().width); + + // known failing issue + // this.assert($('dimensions-nestee').getDimensions().width <= 500, 'check for proper dimensions of hidden child elements'); + + $('dimensions-td').hide(); + this.assertIdentical(100, $('dimensions-td').getDimensions().height); + this.assertIdentical(200, $('dimensions-td').getDimensions().width); + $('dimensions-td').show(); + + $('dimensions-tr').hide(); + this.assertIdentical(100, $('dimensions-tr').getDimensions().height); + this.assertIdentical(200, $('dimensions-tr').getDimensions().width); + $('dimensions-tr').show(); + + $('dimensions-table').hide(); + this.assertIdentical(100, $('dimensions-table').getDimensions().height); + this.assertIdentical(200, $('dimensions-table').getDimensions().width); + + }, + + testDOMAttributesHavePrecedenceOverExtendedElementMethods: function() { + this.assertNothingRaised(function() { $('dom_attribute_precedence').down('form') }); + this.assertEqual($('dom_attribute_precedence').down('input'), $('dom_attribute_precedence').down('form').update); + }, + + testClassNames: function() { + this.assertEnumEqual([], $('class_names').classNames()); + this.assertEnumEqual(['A'], $('class_names').down().classNames()); + this.assertEnumEqual(['A', 'B'], $('class_names_ul').classNames()); + }, + + testHasClassName: function() { + this.assertIdentical(false, $('class_names').hasClassName('does_not_exist')); + this.assertIdentical(true, $('class_names').down().hasClassName('A')); + this.assertIdentical(false, $('class_names').down().hasClassName('does_not_exist')); + this.assertIdentical(true, $('class_names_ul').hasClassName('A')); + this.assertIdentical(true, $('class_names_ul').hasClassName('B')); + this.assertIdentical(false, $('class_names_ul').hasClassName('does_not_exist')); + }, + + testAddClassName: function() { + $('class_names').addClassName('added_className'); + this.assertEnumEqual(['added_className'], $('class_names').classNames()); + + $('class_names').addClassName('added_className'); // verify that className cannot be added twice. + this.assertEnumEqual(['added_className'], $('class_names').classNames()); + + $('class_names').addClassName('another_added_className'); + this.assertEnumEqual(['added_className', 'another_added_className'], $('class_names').classNames()); + }, + + testRemoveClassName: function() { + $('class_names').removeClassName('added_className'); + this.assertEnumEqual(['another_added_className'], $('class_names').classNames()); + + $('class_names').removeClassName('added_className'); // verify that removing a non existent className is safe. + this.assertEnumEqual(['another_added_className'], $('class_names').classNames()); + + $('class_names').removeClassName('another_added_className'); + this.assertEnumEqual([], $('class_names').classNames()); + }, + + testToggleClassName: function() { + $('class_names').toggleClassName('toggled_className'); + this.assertEnumEqual(['toggled_className'], $('class_names').classNames()); + + $('class_names').toggleClassName('toggled_className'); + this.assertEnumEqual([], $('class_names').classNames()); + + $('class_names_ul').toggleClassName('toggled_className'); + this.assertEnumEqual(['A', 'B', 'toggled_className'], $('class_names_ul').classNames()); + + $('class_names_ul').toggleClassName('toggled_className'); + this.assertEnumEqual(['A', 'B'], $('class_names_ul').classNames()); + }, + + testElementScrollTo: function() { + var elem = $('scroll_test_2'); + Element.scrollTo('scroll_test_2'); + this.assertEqual(Position.page(elem)[1], 0); + window.scrollTo(0, 0); + + elem.scrollTo(); + this.assertEqual(Position.page(elem)[1], 0); + window.scrollTo(0, 0); + }, + + testCustomElementMethods: function() { + var elem = $('navigation_test_f'); + this.assertRespondsTo('hashBrowns', elem); + this.assertEqual('hash browns', elem.hashBrowns()); + + this.assertRespondsTo('hashBrowns', Element); + this.assertEqual('hash browns', Element.hashBrowns(elem)); + }, + + testSpecificCustomElementMethods: function() { + var elem = $('navigation_test_f'); + + this.assert(Element.Methods.ByTag[elem.tagName]); + this.assertRespondsTo('pancakes', elem); + this.assertEqual("pancakes", elem.pancakes()); + + var elem2 = $('test-visible'); + + this.assert(Element.Methods.ByTag[elem2.tagName]); + this.assertUndefined(elem2.pancakes); + this.assertRespondsTo('waffles', elem2); + this.assertEqual("waffles", elem2.waffles()); + + this.assertRespondsTo('orangeJuice', elem); + this.assertRespondsTo('orangeJuice', elem2); + this.assertEqual("orange juice", elem.orangeJuice()); + this.assertEqual("orange juice", elem2.orangeJuice()); + + this.assert(typeof Element.orangeJuice == 'undefined'); + this.assert(typeof Element.pancakes == 'undefined'); + this.assert(typeof Element.waffles == 'undefined'); + + }, + + testScriptFragment: function() { + var element = document.createElement('div'); + // tests an issue with Safari 2.0 crashing when the ScriptFragment + // regular expression is using a pipe-based approach for + // matching any character + ['\r','\n',' '].each(function(character){ + $(element).update("<script>"+character.times(10000)+"</scr"+"ipt>"); + this.assertEqual('', element.innerHTML); + }, this); + $(element).update("<script>var blah='"+'\\'.times(10000)+"'</scr"+"ipt>"); + this.assertEqual('', element.innerHTML); + }, + + testPositionedOffset: function() { + this.assertEnumEqual([10,10], + $('body_absolute').positionedOffset()); + this.assertEnumEqual([10,10], + $('absolute_absolute').positionedOffset()); + this.assertEnumEqual([10,10], + $('absolute_relative').positionedOffset()); + this.assertEnumEqual([0,10], + $('absolute_relative_undefined').positionedOffset()); + this.assertEnumEqual([10,10], + $('absolute_fixed_absolute').positionedOffset()); + + var afu = $('absolute_fixed_undefined'); + this.assertEnumEqual([afu.offsetLeft, afu.offsetTop], + afu.positionedOffset()); + + var element = new Element('div'), offset = element.positionedOffset(); + this.assertEnumEqual([0,0], offset); + this.assertIdentical(0, offset.top); + this.assertIdentical(0, offset.left); + }, + + testCumulativeOffset: function() { + var element = new Element('div'), offset = element.cumulativeOffset(); + this.assertEnumEqual([0,0], offset); + this.assertIdentical(0, offset.top); + this.assertIdentical(0, offset.left); + }, + + testViewportOffset: function() { + this.assertEnumEqual([10,10], + $('body_absolute').viewportOffset()); + this.assertEnumEqual([20,20], + $('absolute_absolute').viewportOffset()); + this.assertEnumEqual([20,20], + $('absolute_relative').viewportOffset()); + this.assertEnumEqual([20,30], + $('absolute_relative_undefined').viewportOffset()); + var element = new Element('div'), offset = element.viewportOffset(); + this.assertEnumEqual([0,0], offset); + this.assertIdentical(0, offset.top); + this.assertIdentical(0, offset.left); + }, + + testOffsetParent: function() { + this.assertEqual('body_absolute', $('absolute_absolute').getOffsetParent().id); + this.assertEqual('body_absolute', $('absolute_relative').getOffsetParent().id); + this.assertEqual('absolute_relative', $('inline').getOffsetParent().id); + this.assertEqual('absolute_relative', $('absolute_relative_undefined').getOffsetParent().id); + + this.assertEqual(document.body, new Element('div').getOffsetParent()); + }, + + testAbsolutize: function() { + $('notInlineAbsoluted', 'inlineAbsoluted').each(function(elt) { + if ('_originalLeft' in elt) delete elt._originalLeft; + elt.absolutize(); + this.assertUndefined(elt._originalLeft, 'absolutize() did not detect absolute positioning'); + }, this); + // invoking on "absolute" positioned element should return element + var element = $('absolute_fixed_undefined').setStyle({position: 'absolute'}); + this.assertEqual(element, element.absolutize()); + + // test relatively positioned element with no height specified for IE7 + var element = $('absolute_relative'), dimensions = element.getDimensions(); + element.absolutize(); + this.assertIdentical(dimensions.width, element.getDimensions().width); + this.assertIdentical(dimensions.height, element.getDimensions().height); + }, + + testRelativize: function() { + // invoking on "relative" positioned element should return element + var element = $('absolute_fixed_undefined').setStyle({position: 'relative'}); + this.assertEqual(element, element.relativize()); + + var assertPositionEqual = function(modifier, element) { + element = $(element); + var offsets = element.cumulativeOffset(); + Element[modifier](element); + this.assertEnumEqual(offsets, element.cumulativeOffset()); + }.bind(this); + + var testRelativize = assertPositionEqual.curry('relativize'); + testRelativize('notInlineAbsoluted'); + testRelativize('inlineAbsoluted'); + testRelativize('absolute_absolute'); + }, + + testElementToViewportDimensionsDoesNotAffectDocumentProperties: function() { + // No properties on the document should be affected when resizing + // an absolute positioned(0,0) element to viewport dimensions + var vd = document.viewport.getDimensions(); + + var before = documentViewportProperties.inspect(); + $('elementToViewportDimensions').setStyle({ height: vd.height + 'px', width: vd.width + 'px' }).show(); + var after = documentViewportProperties.inspect(); + $('elementToViewportDimensions').hide(); + + documentViewportProperties.properties.each(function(prop) { + this.assertEqual(before[prop], after[prop], prop + ' was affected'); + }, this); + }, + + testNodeConstants: function() { + this.assert(window.Node, 'window.Node is unavailable'); + + var constants = $H({ + ELEMENT_NODE: 1, + ATTRIBUTE_NODE: 2, + TEXT_NODE: 3, + CDATA_SECTION_NODE: 4, + ENTITY_REFERENCE_NODE: 5, + ENTITY_NODE: 6, + PROCESSING_INSTRUCTION_NODE: 7, + COMMENT_NODE: 8, + DOCUMENT_NODE: 9, + DOCUMENT_TYPE_NODE: 10, + DOCUMENT_FRAGMENT_NODE: 11, + NOTATION_NODE: 12 + }); + + constants.each(function(pair) { + this.assertEqual(Node[pair.key], pair.value); + }, this); + } +}); + +function preservingBrowserDimensions(callback) { + var original = document.viewport.getDimensions(); + window.resizeTo(640, 480); + var resized = document.viewport.getDimensions(); + original.width += 640 - resized.width, original.height += 480 - resized.height; + + try { + window.resizeTo(original.width, original.height); + callback(); + } finally { + window.resizeTo(original.width, original.height); + } +} diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/element_mixins_test.js b/dom/tests/mochitest/ajax/prototype/test/unit/element_mixins_test.js new file mode 100644 index 0000000000..239179375c --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/element_mixins_test.js @@ -0,0 +1,32 @@ +new Test.Unit.Runner({ + testInput: function() { + this.assert($("input").present != null); + this.assert(typeof $("input").present == 'function'); + this.assert($("input").select != null); + this.assertRespondsTo('present', Form.Element); + this.assertRespondsTo('present', Form.Element.Methods); + this.assertRespondsTo('coffee', $('input')); + this.assertIdentical(Prototype.K, Form.Element.coffee); + this.assertIdentical(Prototype.K, Form.Element.Methods.coffee); + }, + + testForm: function() { + this.assert($("form").reset != null); + this.assert($("form").getInputs().length == 2); + }, + + testEvent: function() { + this.assert($("form").observe != null) + // Can't really test this one with TestUnit... + $('form').observe("submit", function(e) { + alert("yeah!"); + Event.stop(e); + }); + }, + + testCollections: function() { + this.assert($$("input").all(function(input) { + return (input.focus != null); + })); + } +});
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/enumerable_test.js b/dom/tests/mochitest/ajax/prototype/test/unit/enumerable_test.js new file mode 100644 index 0000000000..fdc7c019d0 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/enumerable_test.js @@ -0,0 +1,263 @@ +function prime(value) { + for (var i = 2; i < value; i++) + if (value % i == 0) return false; + return true; +} + +new Test.Unit.Runner({ + testEachBreak: function() { + var result = 0; + Fixtures.Basic.each(function(value) { + if ((result = value) == 2) throw $break; + }); + + this.assertEqual(2, result); + }, + + testEachReturnActsAsContinue: function() { + var results = []; + Fixtures.Basic.each(function(value) { + if (value == 2) return; + results.push(value); + }); + + this.assertEqual('1, 3', results.join(', ')); + }, + + testEachChaining: function() { + this.assertEqual(Fixtures.Primes, Fixtures.Primes.each(Prototype.emptyFunction)); + this.assertEqual(3, Fixtures.Basic.each(Prototype.emptyFunction).length); + }, + + testEnumContext: function() { + var results = []; + Fixtures.Basic.each(function(value) { + results.push(value * this.i); + }, { i: 2 }); + + this.assertEqual('2 4 6', results.join(' ')); + + this.assert(Fixtures.Basic.all(function(value){ + return value >= this.min && value <= this.max; + }, { min: 1, max: 3 })); + this.assert(!Fixtures.Basic.all(function(value){ + return value >= this.min && value <= this.max; + })); + this.assert(Fixtures.Basic.any(function(value){ + return value == this.target_value; + }, { target_value: 2 })); + }, + + testAny: function() { + this.assert(!([].any())); + + this.assert([true, true, true].any()); + this.assert([true, false, false].any()); + this.assert(![false, false, false].any()); + + this.assert(Fixtures.Basic.any(function(value) { + return value > 2; + })); + this.assert(!Fixtures.Basic.any(function(value) { + return value > 5; + })); + }, + + testAll: function() { + this.assert([].all()); + + this.assert([true, true, true].all()); + this.assert(![true, false, false].all()); + this.assert(![false, false, false].all()); + + this.assert(Fixtures.Basic.all(function(value) { + return value > 0; + })); + this.assert(!Fixtures.Basic.all(function(value) { + return value > 1; + })); + }, + + testCollect: function() { + this.assertEqual(Fixtures.Nicknames.join(', '), + Fixtures.People.collect(function(person) { + return person.nickname; + }).join(", ")); + + this.assertEqual(26, Fixtures.Primes.map().length); + }, + + testDetect: function() { + this.assertEqual('Marcel Molina Jr.', + Fixtures.People.detect(function(person) { + return person.nickname.match(/no/); + }).name); + }, + + testEachSlice: function() { + this.assertEnumEqual([], [].eachSlice(2)); + this.assertEqual(1, [1].eachSlice(1).length); + this.assertEnumEqual([1], [1].eachSlice(1)[0]); + this.assertEqual(2, Fixtures.Basic.eachSlice(2).length); + this.assertEnumEqual( + [3, 2, 1, 11, 7, 5, 19, 17, 13, 31, 29, 23, 43, 41, 37, 59, 53, 47, 71, 67, 61, 83, 79, 73, 97, 89], + Fixtures.Primes.eachSlice( 3, function(slice){ return slice.reverse() }).flatten() + ); + this.assertEnumEqual(Fixtures.Basic, Fixtures.Basic.eachSlice(-10)); + this.assertEnumEqual(Fixtures.Basic, Fixtures.Basic.eachSlice(0)); + this.assertNotIdentical(Fixtures.Basic, Fixtures.Basic.eachSlice(0)); + }, + + testEachWithIndex: function() { + var nicknames = [], indexes = []; + Fixtures.People.each(function(person, index) { + nicknames.push(person.nickname); + indexes.push(index); + }); + + this.assertEqual(Fixtures.Nicknames.join(', '), + nicknames.join(', ')); + this.assertEqual('0, 1, 2, 3', indexes.join(', ')); + }, + + testFindAll: function() { + this.assertEqual(Fixtures.Primes.join(', '), + Fixtures.Z.findAll(prime).join(', ')); + }, + + testGrep: function() { + this.assertEqual('noradio, htonl', + Fixtures.Nicknames.grep(/o/).join(", ")); + + this.assertEqual('NORADIO, HTONL', + Fixtures.Nicknames.grep(/o/, function(nickname) { + return nickname.toUpperCase(); + }).join(", ")) + + this.assertEnumEqual($('grepHeader', 'grepCell'), + $('grepTable', 'grepTBody', 'grepRow', 'grepHeader', 'grepCell').grep(new Selector('.cell'))); + }, + + testInclude: function() { + this.assert(Fixtures.Nicknames.include('sam-')); + this.assert(Fixtures.Nicknames.include('noradio')); + this.assert(!Fixtures.Nicknames.include('gmosx')); + this.assert(Fixtures.Basic.include(2)); + this.assert(Fixtures.Basic.include('2')); + this.assert(!Fixtures.Basic.include('4')); + }, + + testInGroupsOf: function() { + this.assertEnumEqual([], [].inGroupsOf(3)); + + var arr = [1, 2, 3, 4, 5, 6].inGroupsOf(3); + this.assertEqual(2, arr.length); + this.assertEnumEqual([1, 2, 3], arr[0]); + this.assertEnumEqual([4, 5, 6], arr[1]); + + arr = [1, 2, 3, 4, 5, 6].inGroupsOf(4); + this.assertEqual(2, arr.length); + this.assertEnumEqual([1, 2, 3, 4], arr[0]); + this.assertEnumEqual([5, 6, null, null], arr[1]); + + var basic = Fixtures.Basic + + arr = basic.inGroupsOf(4,'x'); + this.assertEqual(1, arr.length); + this.assertEnumEqual([1, 2, 3, 'x'], arr[0]); + + this.assertEnumEqual([1,2,3,'a'], basic.inGroupsOf(2, 'a').flatten()); + + arr = basic.inGroupsOf(5, ''); + this.assertEqual(1, arr.length); + this.assertEnumEqual([1, 2, 3, '', ''], arr[0]); + + this.assertEnumEqual([1,2,3,0], basic.inGroupsOf(2, 0).flatten()); + this.assertEnumEqual([1,2,3,false], basic.inGroupsOf(2, false).flatten()); + }, + + testInject: function() { + this.assertEqual(1061, + Fixtures.Primes.inject(0, function(sum, value) { + return sum + value; + })); + }, + + testInvoke: function() { + var result = [[2, 1, 3], [6, 5, 4]].invoke('sort'); + this.assertEqual(2, result.length); + this.assertEqual('1, 2, 3', result[0].join(', ')); + this.assertEqual('4, 5, 6', result[1].join(', ')); + + result = result.invoke('invoke', 'toString', 2); + this.assertEqual('1, 10, 11', result[0].join(', ')); + this.assertEqual('100, 101, 110', result[1].join(', ')); + }, + + testMax: function() { + this.assertEqual(100, Fixtures.Z.max()); + this.assertEqual(97, Fixtures.Primes.max()); + this.assertEqual(2, [ -9, -8, -7, -6, -4, -3, -2, 0, -1, 2 ].max()); + this.assertEqual('sam-', Fixtures.Nicknames.max()); // ?s > ?U + }, + + testMin: function() { + this.assertEqual(1, Fixtures.Z.min()); + this.assertEqual(0, [ 1, 2, 3, 4, 5, 6, 7, 8, 0, 9 ].min()); + this.assertEqual('Ulysses', Fixtures.Nicknames.min()); // ?U < ?h + }, + + testPartition: function() { + var result = Fixtures.People.partition(function(person) { + return person.name.length < 15; + }).invoke('pluck', 'nickname'); + + this.assertEqual(2, result.length); + this.assertEqual('sam-, htonl', result[0].join(', ')); + this.assertEqual('noradio, Ulysses', result[1].join(', ')); + }, + + testPluck: function() { + this.assertEqual(Fixtures.Nicknames.join(', '), + Fixtures.People.pluck('nickname').join(', ')); + }, + + testReject: function() { + this.assertEqual(0, + Fixtures.Nicknames.reject(Prototype.K).length); + + this.assertEqual('sam-, noradio, htonl', + Fixtures.Nicknames.reject(function(nickname) { + return nickname != nickname.toLowerCase(); + }).join(', ')); + }, + + testSortBy: function() { + this.assertEqual('htonl, noradio, sam-, Ulysses', + Fixtures.People.sortBy(function(value) { + return value.nickname.toLowerCase(); + }).pluck('nickname').join(', ')); + }, + + testToArray: function() { + var result = Fixtures.People.toArray(); + this.assert(result != Fixtures.People); // they're different objects... + this.assertEqual(Fixtures.Nicknames.join(', '), + result.pluck('nickname').join(', ')); // but the values are the same + }, + + testZip: function() { + var result = [1, 2, 3].zip([4, 5, 6], [7, 8, 9]); + this.assertEqual('[[1, 4, 7], [2, 5, 8], [3, 6, 9]]', result.inspect()); + + result = [1, 2, 3].zip([4, 5, 6], [7, 8, 9], function(array) { return array.reverse() }); + this.assertEqual('[[7, 4, 1], [8, 5, 2], [9, 6, 3]]', result.inspect()); + }, + + testSize: function() { + this.assertEqual(4, Fixtures.People.size()); + this.assertEqual(4, Fixtures.Nicknames.size()); + this.assertEqual(26, Fixtures.Primes.size()); + this.assertEqual(0, [].size()); + } +});
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/event_test.js b/dom/tests/mochitest/ajax/prototype/test/unit/event_test.js new file mode 100644 index 0000000000..ae10a77a7f --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/event_test.js @@ -0,0 +1,286 @@ +var documentLoaded = document.loaded; + +new Test.Unit.Runner({ + + // test firing an event and observing it on the element it's fired from + testCustomEventFiring: function() { + var span = $("span"), fired = false, observer = function(event) { + this.assertEqual(span, event.element()); + this.assertEqual(1, event.memo.index); + fired = true; + }.bind(this); + + span.observe("test:somethingHappened", observer); + span.fire("test:somethingHappened", { index: 1 }); + this.assert(fired); + + fired = false; + span.fire("test:somethingElseHappened"); + this.assert(!fired); + + span.stopObserving("test:somethingHappened", observer); + span.fire("test:somethingHappened"); + this.assert(!fired); + }, + + // test firing an event and observing it on a containing element + testCustomEventBubbling: function() { + var span = $("span"), outer = $("outer"), fired = false, observer = function(event) { + this.assertEqual(span, event.element()); + fired = true; + }.bind(this); + + outer.observe("test:somethingHappened", observer); + span.fire("test:somethingHappened"); + this.assert(fired); + + fired = false; + span.fire("test:somethingElseHappened"); + this.assert(!fired); + + outer.stopObserving("test:somethingHappened", observer); + span.fire("test:somethingHappened"); + this.assert(!fired); + }, + + testCustomEventCanceling: function() { + var span = $("span"), outer = $("outer"), inner = $("inner"); + var fired = false, stopped = false; + + function outerObserver(event) { + fired = span == event.element(); + } + + function innerObserver(event) { + event.stop(); + stopped = true; + } + + inner.observe("test:somethingHappened", innerObserver); + outer.observe("test:somethingHappened", outerObserver); + span.fire("test:somethingHappened"); + this.assert(stopped); + this.assert(!fired); + + fired = stopped = false; + inner.stopObserving("test:somethingHappened", innerObserver); + span.fire("test:somethingHappened"); + this.assert(!stopped); + this.assert(fired); + + outer.stopObserving("test:somethingHappened", outerObserver); + }, + + testEventObjectIsExtended: function() { + var span = $("span"), event, observedEvent, observer = function(e) { observedEvent = e }; + span.observe("test:somethingHappened", observer); + event = span.fire("test:somethingHappened"); + this.assertEqual(event, observedEvent); + this.assertEqual(Event.Methods.stop.methodize(), event.stop); + span.stopObserving("test:somethingHappened", observer); + + event = span.fire("test:somethingHappenedButNoOneIsListening"); + this.assertEqual(Event.Methods.stop.methodize(), event.stop); + }, + + testEventObserversAreBoundToTheObservedElement: function() { + var span = $("span"), target, observer = function() { target = this }; + + span.observe("test:somethingHappened", observer); + span.fire("test:somethingHappened"); + span.stopObserving("test:somethingHappened", observer); + this.assertEqual(span, target); + target = null; + + var outer = $("outer"); + outer.observe("test:somethingHappened", observer); + span.fire("test:somethingHappened"); + outer.stopObserving("test:somethingHappened", observer); + this.assertEqual(outer, target); + }, + + testMultipleCustomEventObserversWithTheSameHandler: function() { + var span = $("span"), count = 0, observer = function() { count++ }; + + span.observe("test:somethingHappened", observer); + span.observe("test:somethingElseHappened", observer); + span.fire("test:somethingHappened"); + this.assertEqual(1, count); + span.fire("test:somethingElseHappened"); + this.assertEqual(2, count); + span.stopObserving("test:somethingHappened", observer); + span.stopObserving("test:somethingElseHappened", observer); + }, + + testStopObservingWithoutArguments: function() { + var span = $("span"), count = 0, observer = function() { count++ }; + + span.observe("test:somethingHappened", observer); + span.observe("test:somethingElseHappened", observer); + span.stopObserving(); + span.fire("test:somethingHappened"); + this.assertEqual(0, count); + span.fire("test:somethingElseHappened"); + this.assertEqual(0, count); + + this.assertEqual(window, Event.stopObserving(window)); + + // test element with no observers + this.assertNothingRaised(function() { $(document.body).stopObserving() }); + }, + + testStopObservingWithoutHandlerArgument: function() { + var span = $("span"), count = 0, observer = function() { count++ }; + + span.observe("test:somethingHappened", observer); + span.observe("test:somethingElseHappened", observer); + span.stopObserving("test:somethingHappened"); + span.fire("test:somethingHappened"); + this.assertEqual(0, count); + span.fire("test:somethingElseHappened"); + this.assertEqual(1, count); + span.stopObserving("test:somethingElseHappened"); + span.fire("test:somethingElseHappened"); + this.assertEqual(1, count); + + // test element with no observers + this.assertNothingRaised(function() { $(document.body).stopObserving("test:somethingHappened") }); + }, + + testStopObservingRemovesHandlerFromCache: function() { + var span = $("span"), observer = function() { }, eventID; + + span.observe("test:somethingHappened", observer); + eventID = span._prototypeEventID; + + this.assert(Event.cache[eventID]); + this.assert(Object.isArray(Event.cache[eventID]["test:somethingHappened"])); + this.assertEqual(1, Event.cache[eventID]["test:somethingHappened"].length); + + span.stopObserving("test:somethingHappened", observer); + this.assert(Event.cache[eventID]); + this.assert(Object.isArray(Event.cache[eventID]["test:somethingHappened"])); + this.assertEqual(0, Event.cache[eventID]["test:somethingHappened"].length); + }, + + testObserveAndStopObservingAreChainable: function() { + var span = $("span"), observer = function() { }; + + this.assertEqual(span, span.observe("test:somethingHappened", observer)); + this.assertEqual(span, span.stopObserving("test:somethingHappened", observer)); + + span.observe("test:somethingHappened", observer); + this.assertEqual(span, span.stopObserving("test:somethingHappened")); + + span.observe("test:somethingHappened", observer); + this.assertEqual(span, span.stopObserving()); + this.assertEqual(span, span.stopObserving()); // assert it again, after there are no observers + + span.observe("test:somethingHappened", observer); + this.assertEqual(span, span.observe("test:somethingHappened", observer)); // try to reuse the same observer + span.stopObserving(); + }, + + testObserveInsideHandlers: function() { + var fired = false, observer = function(event) { + fired = true; + }; + + document.observe("test:somethingHappened", function() { + document.observe("test:somethingHappened", observer); + }); + + document.fire("test:somethingHappened"); + this.assert(!fired); + + document.fire("test:somethingHappened"); + this.assert(fired); + document.stopObserving("test:somethingHappened"); + }, + + testStopObservingInsideHandlers: function() { + var fired = false, observer = function(event) { + fired = true; + }; + + document.observe("test:somethingHappened", observer); + document.observe("test:somethingHappened", function() { + document.stopObserving("test:somethingHappened", observer); + }); + + document.fire("test:somethingHappened"); + this.assert(fired); + + fired = false; + document.fire("test:somethingHappened"); + document.stopObserving("test:somethingHappened"); + this.assert(!fired); + }, + + testDocumentLoaded: function() { + this.assert(!documentLoaded); + this.assert(document.loaded); + }, + + testDocumentContentLoadedEventFiresBeforeWindowLoad: function() { + this.assert(eventResults.contentLoaded, "contentLoaded"); + this.assert(eventResults.contentLoaded.endOfDocument, "contentLoaded.endOfDocument"); + this.assert(!eventResults.contentLoaded.windowLoad, "!contentLoaded.windowLoad"); + this.assert(eventResults.windowLoad, "windowLoad"); + this.assert(eventResults.windowLoad.endOfDocument, "windowLoad.endOfDocument"); + this.assert(eventResults.windowLoad.contentLoaded, "windowLoad.contentLoaded"); + }, + + testEventStopped: function() { + var span = $("span"), event; + + span.observe("test:somethingHappened", function() { }); + event = span.fire("test:somethingHappened"); + this.assert(!event.stopped, "event.stopped should be false with an empty observer"); + span.stopObserving("test:somethingHappened"); + + span.observe("test:somethingHappened", function(e) { e.stop() }); + event = span.fire("test:somethingHappened"); + this.assert(event.stopped, "event.stopped should be true for an observer that calls stop"); + span.stopObserving("test:somethingHappened"); + }, + + testEventFindElement: function() { + var span = $("span"), event; + event = span.fire("test:somethingHappened"); + this.assertElementMatches(event.findElement(), 'span#span'); + this.assertElementMatches(event.findElement('span'), 'span#span'); + this.assertElementMatches(event.findElement('p'), 'p#inner'); + this.assertEqual(null, event.findElement('div.does_not_exist')); + this.assertElementMatches(event.findElement('.does_not_exist, span'), 'span#span'); + }, + + testEventIDDuplication: function() { + $('container').down().observe("test:somethingHappened", Prototype.emptyFunction); + $('container').innerHTML += $('container').innerHTML; + this.assertUndefined($('container').down(1)._prototypeEventID); + }, + + testHandlerCallsPreserveOrder: function() { + var span = $("span"), order = [ ]; + span.observe("test:somethingHappened", function(e) { order.push('first') }); + span.observe("test:somethingHappened", function(e) { order.push('second') }); + span.fire("test:somethingHappened"); + this.assertEnumEqual(['first', 'second'], order); + span.stopObserving("test:somethingHappened"); + } +}); + +document.observe("dom:loaded", function(event) { + eventResults.contentLoaded = { + endOfDocument: eventResults.endOfDocument, + windowLoad: eventResults.windowLoad + }; +}); + +Event.observe(window, "load", function(event) { + eventResults.windowLoad = { + endOfDocument: eventResults.endOfDocument, + contentLoaded: eventResults.contentLoaded + }; +}); diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/ajax.html b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/ajax.html new file mode 100644 index 0000000000..4af5e267ac --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/ajax.html @@ -0,0 +1,2 @@ +<div id="content"></div> +<div id="content2" style="color:red"></div> diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/ajax.js b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/ajax.js new file mode 100644 index 0000000000..8487943f73 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/ajax.js @@ -0,0 +1,42 @@ +var Fixtures = { + js: { + responseBody: '$("content").update("<H2>Hello world!</H2>");', + 'Content-Type': ' text/javascript ' + }, + + html: { + responseBody: "Pack my box with <em>five dozen</em> liquor jugs! " + + "Oh, how <strong>quickly</strong> daft jumping zebras vex..." + }, + + xml: { + responseBody: '<?xml version="1.0" encoding="UTF-8" ?><name attr="foo">bar</name>', + 'Content-Type': 'application/xml' + }, + + json: { + responseBody: '{\n\r"test": 123}', + 'Content-Type': 'application/json' + }, + + jsonWithoutContentType: { + responseBody: '{"test": 123}' + }, + + invalidJson: { + responseBody: '{});window.attacked = true;({}', + 'Content-Type': 'application/json' + }, + + headerJson: { + 'X-JSON': '{"test": "hello #éà"}' + } +}; + +var responderCounter = 0; + +// lowercase comparison because of MSIE which presents HTML tags in uppercase +var sentence = ("Pack my box with <em>five dozen</em> liquor jugs! " + +"Oh, how <strong>quickly</strong> daft jumping zebras vex...").toLowerCase(); + +var message = 'You must be running your tests from rake to test this feature.';
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/array.html b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/array.html new file mode 100644 index 0000000000..8f091878c8 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/array.html @@ -0,0 +1 @@ +<div id="test_node">22<span id="span_1"></span><span id="span_2"></span></div> diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/base.html b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/base.html new file mode 100644 index 0000000000..5a08fbd554 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/base.html @@ -0,0 +1,6 @@ +<div id="test"></div> +<ul id="list"> + <li></li> + <li></li> + <li></li> +</ul>
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/base.js b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/base.js new file mode 100644 index 0000000000..8e9e00bd54 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/base.js @@ -0,0 +1,106 @@ +var Person = function(name){ + this.name = name; +}; + +Person.prototype.toJSON = function() { + return '-' + this.name; +}; + +var arg1 = 1; +var arg2 = 2; +var arg3 = 3; +function TestObj() { }; +TestObj.prototype.assertingEventHandler = + function(event, assertEvent, assert1, assert2, assert3, a1, a2, a3) { + assertEvent(event); + assert1(a1); + assert2(a2); + assert3(a3); + }; + +var globalBindTest = null; + + +// base class +var Animal = Class.create({ + initialize: function(name) { + this.name = name; + }, + name: "", + eat: function() { + return this.say("Yum!"); + }, + say: function(message) { + return this.name + ": " + message; + } +}); + +// subclass that augments a method +var Cat = Class.create(Animal, { + eat: function($super, food) { + if (food instanceof Mouse) return $super(); + else return this.say("Yuk! I only eat mice."); + } +}); + +// empty subclass +var Mouse = Class.create(Animal, {}); + +//mixins +var Sellable = { + getValue: function(pricePerKilo) { + return this.weight * pricePerKilo; + }, + + inspect: function() { + return '#<Sellable: #{weight}kg>'.interpolate(this); + } +}; + +var Reproduceable = { + reproduce: function(partner) { + if (partner.constructor != this.constructor || partner.sex == this.sex) + return null; + var weight = this.weight / 10, sex = Math.random(1).round() ? 'male' : 'female'; + return new this.constructor('baby', weight, sex); + } +}; + +// base class with mixin +var Plant = Class.create(Sellable, { + initialize: function(name, weight) { + this.name = name; + this.weight = weight; + }, + + inspect: function() { + return '#<Plant: #{name}>'.interpolate(this); + } +}); + +// subclass with mixin +var Dog = Class.create(Animal, Reproduceable, { + initialize: function($super, name, weight, sex) { + this.weight = weight; + this.sex = sex; + $super(name); + } +}); + +// subclass with mixins +var Ox = Class.create(Animal, Sellable, Reproduceable, { + initialize: function($super, name, weight, sex) { + this.weight = weight; + this.sex = sex; + $super(name); + }, + + eat: function(food) { + if (food instanceof Plant) + this.weight += food.weight; + }, + + inspect: function() { + return '#<Ox: #{name}>'.interpolate(this); + } +});
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/content.html b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/content.html new file mode 100644 index 0000000000..ee3701d3c3 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/content.html @@ -0,0 +1 @@ +Pack my box with <em>five dozen</em> liquor jugs! Oh, how <strong>quickly</strong> daft jumping zebras vex...
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/data.json b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/data.json new file mode 100644 index 0000000000..85391eb939 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/data.json @@ -0,0 +1 @@ +{test: 123}
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/dom.css b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/dom.css new file mode 100644 index 0000000000..453a569c26 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/dom.css @@ -0,0 +1,85 @@ +#style_test_1 { cursor: pointer; font-size:12px;} +div.style-test { margin-left: 1px } + +#style_test_dimensions_container { + position: absolute; + top: 0; + left: 500px; + width: 20px; + height: 30px; + margin: 10px; + padding: 10px; + border: 3px solid red; +} + +#not_floating_style { float: none } +#floating_style { float: left } +#op2 { opacity:0.5;filter:alpha(opacity=50)progid:DXImageTransform.Microsoft.Blur(strength=10);} + +#scroll_test_1 { + margin: 10px; + padding: 10px; + position: relative; +} + +#scroll_test_2 { + position: absolute; + left: 10px; + top: 10px; +} + +#dimensions-visible, +#dimensions-display-none, +#dimensions-visible-pos-rel, +#dimensions-display-none-pos-rel, +#dimensions-visible-pos-abs, +#dimensions-display-none-pos-abs { + font-size: 10px; + height: 10em; + width: 20em; +} + +#dimensions-visible-pos-abs, +#dimensions-display-none-pos-abs { + position: absolute; + top: 15px; + left: 15px; +} + +#dimensions-visible-pos-rel, +#dimensions-display-none-pos-rel { + position: relative; + top: 15px; + left: 15px; +} + +#dimensions-display-none, #dimensions-display-none-pos-rel, #dimensions-display-none-pos-abs { + display: none; +} + +#dimensions-table, #dimensions-tbody, #dimensions-tr, #dimensions-td { + font-size: 10px; + margin: 0; + padding: 0; + border: 0; + border-spacing: 0; + height: 10em; + width: 20em; +} + + +#notInlineAbsoluted { position: absolute; } + +#elementToViewportDimensions { + position: absolute; + top: 0; + left: 0; + height: 10px; + width: 10px; + background: #000; +} + +/* for scroll test on really big screens */ +body { + height: 40000px; +}
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/dom.html b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/dom.html new file mode 100644 index 0000000000..1273098e6d --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/dom.html @@ -0,0 +1,285 @@ +<div id="scroll_test_1"> + <p id="scroll_test_2">Scroll test</p> +</div> + +<div id="test-visible">visible</div> +<div id="test-hidden" style="display:none;">hidden</div> +<div id="test-toggle-visible">visible</div> +<div id="test-toggle-hidden" style="display:none;">hidden</div> +<div id="test-hide-visible">visible</div> +<div id="test-hide-hidden" style="display:none;">hidden</div> +<div id="test-show-visible">visible</div> +<div id="test-show-hidden" style="display:none;">hidden</div> +<div id="removable-container"><div id="removable"></div></div> + +<div> + <table> + <tbody id="table"> + <tr> + <td>Data</td> + </tr> + <tr> + <td id="a_cell">First Row</td> + </tr> + <tr id="second_row"> + <td>Second Row</td> + </tr> + </tbody> + </table> +</div> + +<div id="table-container-to-replace"> + <table> + <tbody id="table-to-replace"> + <tr> + <td>Data</td> + </tr> + <tr> + <td id="a_cell-to-replace">First Row</td> + </tr> + <tr id="second_row-to-replace"> + <td>Second Row</td> + </tr> + </tbody> + </table> +</div> + +<p class="test">Test paragraph outside of container</p> + +<div id="container"> + <p class="test" id="intended">Test paragraph 1 inside of container</p> + <p class="test">Test paragraph 2 inside of container</p> + <p class="test">Test paragraph 3 inside of container</p> + <p class="test">Test paragraph 4 inside of container</p> +</div> + +<div id="testdiv">to be updated</div> +<div id="testdiv-replace-container-1"><div id="testdiv-replace-1"></div></div> +<div id="testdiv-replace-container-2"><div id="testdiv-replace-2"></div></div> +<div id="testdiv-replace-container-3"><div id="testdiv-replace-3"></div></div> +<div id="testdiv-replace-container-4"><div id="testdiv-replace-4"></div></div> +<div id="testdiv-replace-container-5"><div id="testdiv-replace-5"></div></div> +<div id="testdiv-replace-container-element"><div id="testdiv-replace-element"></div></div> +<div id="testdiv-replace-container-toelement"><div id="testdiv-replace-toelement"></div></div> +<div id="testdiv-replace-container-tohtml"><div id="testdiv-replace-tohtml"></div></div> +<div id="testtable-replace-container"><table id="testtable-replace"></table></div> +<table id="testrow-replace-container"><tr id="testrow-replace"></tr></table> +<select id="testoption-replace-container"><option id="testoption-replace"></option><option>stays</option></select> +<div id="testform-replace-container"><p>some text</p><form id="testform-replace"><input id="testinput-replace" type="text" /></form><p>some text</p></div> + +<div id="element_with_visible_overflow" style="overflow:visible">V</div> +<div id="element_with_hidden_overflow" style="overflow:hidden">H</div> +<div id="element_with_scroll_overflow" style="overflow:scroll">S</div> + +<div id="element_extend_test"> </div> + +<div id="element_reextend_test"><div id="discard_1"></div></div> + +<div id="test_whitespace"> <span> </span> + + +<div><div></div> </div><span> </span> +</div> + + +<div id="nav_tests_isolator"> + <div id="nav_test_first_sibling"></div> + <div></div> + <p id="nav_test_p" class="test"></p> + <span id="nav_test_prev_sibling"></span> + + <ul id="navigation_test" style="display: none"> + <!-- comment node to screw things up --> + <li class="first"><em>A</em></li> + <li><em class="dim">B</em></li> + <li id="navigation_test_c"> + <em>C</em> + <ul> + <li><em class="dim">E</em></li> + <li id="navigation_test_f"><em>F</em></li> + </ul> + </li> + <li class="last"><em>D</em></li> + </ul> + + <div id="navigation_test_next_sibling"> + <!-- --> + </div> + + <p></p> +</div> + +<div id="class_names"> + <p class="A"></p> + <ul class="A B" id="class_names_ul"> + <li class="C"></li> + <li class="A C"></li> + <li class="1"></li> + </ul> + <div class="B C D"></div> + <div id="unextended"></div> +</div> + +<div id="style_test_1" style="display:none;"></div> +<div id="style_test_2" class="style-test" style="font-size:11px;"></div> + +<div id="style_test_3">blah</div> +<span id="style_test_4">blah</span> +<span id="style_test_5">blah</span> + +<div id="style_test_dimensions_container"> + <div id="style_test_dimensions" style="background:#ddd;padding:1px;margin:1px;border:1px solid #00f"><div style="height:5px;background:#eee;width:5px;padding:2px;margin:2px;border:2px solid #0f0"> </div> + </div> +</div> + +<div id="test_csstext_1">test_csstext_1</div> +<div id="test_csstext_2">test_csstext_2</div> +<div id="test_csstext_3" style="border: 1px solid red">test_csstext_3</div> +<div id="test_csstext_4" style="font-size: 20px">test_csstext_4</div> +<div id="test_csstext_5">test_csstext_5</div> + +<div id="custom_attributes"> + <div foo="1" bar="2"></div> + <div foo="2"></div> +</div> + +<div id="cloned_element_attributes_issue" foo="original"></div> +<a id="attributes_with_issues_1" href="test.html" accesskey="L" tabindex="50" title="a link" onclick="alert('hello world');"></a> +<a id="attributes_with_issues_2" href="" accesskey="" tabindex="" title=""></a> +<a id="attributes_with_issues_3"></a> +<form id="attributes_with_issues_form" method="post" action="blah" class="blah-class"> + <input type="hidden" id="id" /> + <input type="hidden" name="id" /> + <input type="checkbox" id="action" /> + <input type="checkbox" name="action" /> + <input type="text" id="method" /> + <input type="hidden" name="class" /> + <input type="checkbox" id="attributes_with_issues_checked" name="a" checked="checked"/> + <input type="checkbox" id="attributes_with_issues_disabled" name="b" checked="checked" disabled="disabled"/> + <input type="text" id="attributes_with_issues_readonly" name="c" readonly="readonly" value="blech"/> + <input type="text" id="attributes_with_issues_regular" name="d" value="0"/> + <input type="date" id="attributes_with_issues_type" value="blech" /> + <select id="attributes_with_issues_multiple" name="e" multiple="multiple"> + <option>blech</option> + <option>blah</option> + </select> +</form> + +<!-- writeAttributes --> +<p id="write_attribute_para"></p> +<a id="write_attribute_link" href="test.html"></a> +<form action="/dev/null" id="write_attribute_form" method="get" accept-charset="utf-8"> + <label id="write_attribute_label"></label> + <input type="checkbox" name="write_attribute_checkbox" value="" id="write_attribute_checkbox"> + <input type="checkbox" checked="checked" name="write_attribute_checked_checkbox" value="" id="write_attribute_checked_checkbox"> + <input type="text" name="write_attribute_input" value="" id="write_attribute_input"> + <select id="write_attribute_select"> + <option>Cat</option> + <option>Dog</option> + </select> +</form> + +<table id="write_attribute_table" cellpadding="6" cellspacing="4"> + <tr><td id="write_attribute_td">A</td><td>B</td></tr> + <tr><td>C</td></tr> + <tr><td>D</td><td>E</td><td>F</td></tr> +</table> + +<div id="dom_attribute_precedence"> + <form action="blech" method="post"> + <input type="submit" id="update" /> + </form> +</div> + +<div id="not_floating_none">NFN</div> +<div id="not_floating_inline" style="float:none">NFI</div> +<div id="not_floating_style">NFS</div> + +<div id="floating_inline" style="float:left">FI</div> +<div id="floating_style">FS</div> +<!-- Test Element opacity functions --> +<img id="op1" alt="op2" src="fixtures/logo.gif" style="opacity:0.5;filter:alpha(opacity=50)" /> +<img id="op2" alt="op2" src="fixtures/logo.gif"/> +<img id="op3" alt="op3" src="fixtures/logo.gif"/> +<img id="op4-ie" alt="op3" src="fixtures/logo.gif" style="filter:alpha(opacity=30)" /> +<div id="dimensions-visible"></div> +<div id="dimensions-display-none"></div> +<div id="dimensions-visible-pos-rel"></div> +<div id="dimensions-display-none-pos-rel"></div> +<div id="dimensions-visible-pos-abs"></div> +<div id="dimensions-display-none-pos-abs"></div> +<table border="0" cellspacing="0" cellpadding="0" id="dimensions-table"> + <tbody id="dimensions-tbody"> + <tr id="dimensions-tr"> + <td id="dimensions-td">Data</td> + </tr> + </tbody> +</table> + +<div id="dimensions-nester" style="width: 500px;"> + <div id="dimensions-nestee" style="display: none">This is a nested DIV. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div> +</div> + + +<p id="test-empty"></p> +<p id="test-empty-but-contains-whitespace"> + + +</p> +<p id="test-full">content</p> +<div id="ancestor"> + <div id="child"> + <div id="grand-child"> + <div id="great-grand-child"></div> + </div></div><!-- intentional formatting; don't change this line --> + <div id="sibling"><div id="grand-sibling"></div></div> +</div> +<div id="not-in-the-family"></div> + +<div id="insertions-container"><div id="insertions-main"><p>some content.</p></div></div> +<div id="insertions-node-container"><div id="insertions-node-main"><p>some content.</p></div></div> +<div id="element-insertions-container"><div id="element-insertions-main"><p>some content.</p></div></div> +<div id="element-insertions-multiple-container"><div id="element-insertions-multiple-main"><p>some content.</p></div></div> +<table id="table_for_insertions"></table> +<table id="table_for_row_insertions"><tr id="row_1"></tr></table> +<form method="post" action="blah"> + <select id="select_for_update" name="select_for_update"> + <option>option 1</option> + <option>option 2</option> + </select> + <select id="select_for_insert_bottom" name="select_for_insert_bottom"> + <option>option 1</option> + <option>option 2</option> + </select> + <select id="select_for_insert_top" name="select_for_insert_top"> + <option>option 1</option> + <option>option 2</option> + </select> +</form> +<div id="wrap-container"><p id="wrap"></p></div> + +<!-- Positioning methods bench --> +<div id="body_absolute" style="position: absolute; top: 10px; left: 10px"> + <div id="absolute_absolute" style="position: absolute; top: 10px; left:10px"> </div> + <div id="absolute_relative" style="position: relative; top: 10px; left:10px"> + <div style="height:10px;font-size:2px">test<span id="inline">test</span></div> + <div id="absolute_relative_undefined">XYZ</div> + </div> + <div id="absolute_fixed" style="position: fixed; top: 10px; left: 10px"> + <span id="absolute_fixed_absolute" style="position: absolute; top: 10px; left: 10px">foo</span> + <span id="absolute_fixed_undefined" style="display:block">bar</span> + </div></div> +<div id="notInlineAbsoluted"></div> +<div id="inlineAbsoluted" style="position: absolute"></div> + +<div id="unextended"></div> +<div id="identification"> + <div id="predefined_id"></div> + <div></div> + <div></div> + <div></div> + <div id="anonymous_element_3"></div> +</div> + +<div id='elementToViewportDimensions' style='display: none'></div> +<div id="auto_dimensions" style="height:auto"></div>
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/dom.js b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/dom.js new file mode 100644 index 0000000000..18f62f354c --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/dom.js @@ -0,0 +1,17 @@ +var testVar = 'to be updated', testVar2 = '', documentViewportProperties; + +Element.addMethods({ + hashBrowns: function(element) { return 'hash browns'; } +}); + +Element.addMethods("LI", { + pancakes: function(element) { return "pancakes"; } +}); + +Element.addMethods("DIV", { + waffles: function(element) { return "waffles"; } +}); + +Element.addMethods($w("li div"), { + orangeJuice: function(element) { return "orange juice"; } +});
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/element_mixins.html b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/element_mixins.html new file mode 100644 index 0000000000..583d0e5f30 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/element_mixins.html @@ -0,0 +1,4 @@ +<form id="form"> + <input type="text" id="input" value="4" /> + <input type="submit" /> +</form>
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/element_mixins.js b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/element_mixins.js new file mode 100644 index 0000000000..22b1c3fd14 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/element_mixins.js @@ -0,0 +1,2 @@ +Form.Element.Methods.coffee = Prototype.K; +Element.addMethods();
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/empty.html b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/empty.html new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/empty.html diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/empty.js b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/empty.js new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/empty.js @@ -0,0 +1 @@ + diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/enumerable.html b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/enumerable.html new file mode 100644 index 0000000000..5e7e8f3b3c --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/enumerable.html @@ -0,0 +1,8 @@ +<table id="grepTable"> +<tbody id="grepTBody"> + <tr id="grepRow"> + <th id="grepHeader" class="cell"></th> + <td id="grepCell" class="cell"></td> + </tr> +</tbody> +</table> diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/enumerable.js b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/enumerable.js new file mode 100644 index 0000000000..5fae7662dd --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/enumerable.js @@ -0,0 +1,23 @@ +var Fixtures = { + People: [ + {name: 'Sam Stephenson', nickname: 'sam-'}, + {name: 'Marcel Molina Jr.', nickname: 'noradio'}, + {name: 'Scott Barron', nickname: 'htonl'}, + {name: 'Nicholas Seckar', nickname: 'Ulysses'} + ], + + Nicknames: $w('sam- noradio htonl Ulysses'), + + Basic: [1, 2, 3], + + Primes: [ + 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, + 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, + 71, 73, 79, 83, 89, 97 + ], + + Z: [] +}; + +for (var i = 1; i <= 100; i++) + Fixtures.Z.push(i); diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/event.html b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/event.html new file mode 100644 index 0000000000..baa88a7d10 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/event.html @@ -0,0 +1,4 @@ +<div id="outer" style="display: none"> + <p id="inner">One two three <span id="span">four</span></p> +</div> +<div id="container"><div></div></div> diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/form.html b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/form.html new file mode 100644 index 0000000000..dc7f171bf2 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/form.html @@ -0,0 +1,112 @@ +<form id="form" method="get" action="fixtures/empty.js"> + <input type="text" name="val1" id="input_enabled" value="4" /> + <div>This is not a form element</div> + <input type="text" name="val2" id="input_disabled" disabled="disabled" value="5" /> + <input type="submit" name="first_submit" value="Commit it!" /> + <input type="submit" name="second_submit" value="Delete it!" /> + <input type="text" name="action" value="blah" /> +</form> + +<form id="bigform" method="get" action="fixtures/empty.js"> + <div id="inputs"> + <input type="text" name="dummy" id="dummy_disabled" disabled="disabled"/> + <input type="submit" name="commit" id="submit" /> + <input type="button" name="clicky" value="click me" /> + <input type="reset" name="revert" /> + <input type="text" name="greeting" id="focus_text" value="Hello" /> + </div> + + <div id="buttons"> + <button type="button" id="button_type_button" name="button">Click Me</button> + </div>div> + + <!-- some edge cases in serialization --> + <div id="value_checks"> + <input name="twin" type="text" value="" /> + <input name="twin" type="text" value="siamese" /> + <!-- Rails checkbox hack with hidden input: --> + <input name="checky" type="checkbox" id="checkbox_hack" value="1" /> + <input name="checky" type="hidden" value="0" /> + </div> + + <!-- all variations of SELECT controls --> + <div id="selects_wrapper"> + <select name="vu"> + <option value="1" selected="selected">One</option> + <option value="2">Two</option> + <option value="3">Three</option> + </select> + <select id="multiSel1" name="vm[]" multiple="multiple"> + <option id="multiSel1_opt1" value="1" selected="selected">One</option> + <option id="multiSel1_opt2" value="2">Two</option> + <option id="multiSel1_opt3" value="3" selected="selected">Three</option> + </select> + <select name="nvu"> + <option selected="selected">One</option> + <option value="2">Two</option> + <option value="3">Three</option> + </select> + + <fieldset id="selects_fieldset"> + <select name="nvm[]" multiple="multiple"> + <option selected="selected">One</option> + <option>Two</option> + <option selected="selected">Three</option> + </select> + <select name="evu"> + <option value="" selected="selected">One</option> + <option value="2">Two</option> + <option value="3">Three</option> + </select> + <select name="evm[]" multiple="multiple"> + <option value="" selected="selected">One</option> + <option>Two</option> + <option selected="selected">Three</option> + </select> + </fieldset> + </div> + + <div id="various"> + <select name="tf_selectOne"><option selected="selected"></option><option>1</option></select> + <textarea name="tf_textarea"></textarea> + <input type="checkbox" name="tf_checkbox" value="on" /> + <select name="tf_selectMany" multiple="multiple"></select> + <input type="text" name="tf_text" /> + <div>This is not a form element</div> + <input type="radio" name="tf_radio" value="on" /> + <input type="hidden" name="tf_hidden" /> + <input type="password" name="tf_password" /> + </div> +</form> + +<form id="form_focus_hidden" style="display: none"> + <input type="text" /> +</form> + +<form id="form_with_file_input"> + <input type="file" name="file_name" value="foo" /> +</form> + +<!-- tabindexed forms --> +<div id="tabindex"> + <form id="ffe"> + <p><input type="text" disabled="disabled" id="ffe_disabled" /></p> + <input type="hidden" id="ffe_hidden" /> + <input type="checkbox" id="ffe_checkbox" /> + </form> + + <form id="ffe_ti"> + <p><input type="text" disabled="disabled" id="ffe_ti_disabled" /></p> + <input type="hidden" id="ffe_ti_hidden" /> + <input type="checkbox" id="ffe_ti_checkbox" /> + <input type="submit" id="ffe_ti_submit" tabindex="1" /> + </form> + + <form id="ffe_ti2"> + <p><input type="text" disabled="disabled" id="ffe_ti2_disabled" /></p> + <input type="hidden" id="ffe_ti2_hidden" /> + <input type="checkbox" id="ffe_ti2_checkbox" tabindex="0" /> + <input type="submit" id="ffe_ti2_submit" tabindex="1" /> + </form> + +</div> diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/hash.js b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/hash.js new file mode 100644 index 0000000000..be5ccfa7d9 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/hash.js @@ -0,0 +1,25 @@ +var Fixtures = { + one: { a: 'A#' }, + + many: { + a: 'A', + b: 'B', + c: 'C', + d: 'D#' + }, + + functions: { + quad: function(n) { return n*n }, + plus: function(n) { return n+n } + }, + + multiple: { color: $w('r g b') }, + multiple_nil: { color: ['r', null, 'g', undefined, 0] }, + multiple_all_nil: { color: [null, undefined] }, + multiple_empty: { color: [] }, + multiple_special: { 'stuff[]': $w('$ a ;') }, + + value_undefined: { a:"b", c:undefined }, + value_null: { a:"b", c:null }, + value_zero: { a:"b", c:0 } +}; diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/hello.js b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/hello.js new file mode 100644 index 0000000000..63d81117bf --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/hello.js @@ -0,0 +1 @@ +$("content").update("<H2>Hello world!</H2>"); diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/logo.gif b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/logo.gif Binary files differnew file mode 100644 index 0000000000..71574abd76 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/logo.gif diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/position.html b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/position.html new file mode 100644 index 0000000000..5fd7bdf4b6 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/position.html @@ -0,0 +1,9 @@ +<div id="ensure_scrollbars" style="width:10000px; height:10000px; position:absolute" > </div> + +<div id="body_absolute" style="position: absolute; top: 10px; left: 10px"> + <div id="absolute_absolute" style="position: absolute; top: 10px; left:10px"> </div> + <div id="absolute_relative" style="position: relative; top: 10px; left:10px"> + <div style="height:10px;font-size:2px">test<span id="inline">test</span></div> + <div id="absolute_relative_undefined"> </div> + </div> +</div> diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/selector.html b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/selector.html new file mode 100644 index 0000000000..d745c8fd55 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/selector.html @@ -0,0 +1,69 @@ +<div id="fixtures" style="display: none"> + <h1 class="title">Some title <span>here</span></h1> + <p id="p" class="first summary"> + <strong id="strong">This</strong> is a short blurb + <a id="link_1" class="first internal" rel="external nofollow" href="#">with a <em id="em2">link</em></a> or + <a id="link_2" class="internal highlight" href="#"><em id="em">two</em></a>. + Or <cite id="with_title" title="hello world!">a citation</cite>. + </p> + <ul id="list"> + <li id="item_1" class="first"><a id="link_3" href="#" class="external"><span id="span">Another link</span></a></li> + <li id="item_2">Some text</li> + <li id="item_3" xml:lang="es-us" class="">Otra cosa</li> + </ul> + + <!-- this form has a field with the name 'id', + therefore its ID property won't be 'troubleForm': --> + <form id="troubleForm"> + <input type="hidden" name="id" id="hidden" /> + <input type="text" name="disabled_text_field" id="disabled_text_field" disabled="disabled" /> + <input type="text" name="enabled_text_field" id="enabled_text_field" /> + <input type="checkbox" name="checkboxes" id="checked_box" checked="checked" value="Checked" /> + <input type="checkbox" name="checkboxes" id="unchecked_box" value="Unchecked"/> + <input type="radio" name="radiobuttons" id="checked_radio" checked="checked" value="Checked" /> + <input type="radio" name="radiobuttons" id="unchecked_radio" value="Unchecked" /> + </form> + + <form id="troubleForm2"> + <input type="checkbox" name="brackets[5][]" id="chk_1" checked="checked" value="1" /> + <input type="checkbox" name="brackets[5][]" id="chk_2" value="2" /> + </form> + + <div id="level1"> + <span id="level2_1"> + <span id="level3_1"></span> + <!-- This comment should be ignored by the adjacent selector --> + <span id="level3_2"></span> + </span> + <span id="level2_2"> + <em id="level_only_child"> + </em> + </span> + <div id="level2_3"></div> + </div> <!-- #level1 --> + + <div id="dupContainer"> + <span id="dupL1" class="span_foo span_bar"> + <span id="dupL2"> + <span id="dupL3"> + <span id="dupL4"> + <span id="dupL5"></span> + </span> + </span> + </span> + </span> + </div> <!-- #dupContainer --> + + <div id="grandfather"> grandfather + <div id="father" class="brothers men"> father + <div id="son"> son </div> + </div> + <div id="uncle" class="brothers men"> uncle </div> + </div> + + <form id="commaParent" title="commas,are,good"> + <input type="hidden" id="commaChild" name="foo" value="#commaOne,#commaTwo" /> + <input type="hidden" id="commaTwo" name="foo2" value="oops" /> + </form> + <div id="counted_container"><div class="is_counted"></div></div> +</div> diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/string.js b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/string.js new file mode 100644 index 0000000000..e4483a5b61 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/string.js @@ -0,0 +1,8 @@ +var attackTarget; +var evalScriptsCounter = 0, + largeTextEscaped = '<span>test</span>', + largeTextUnescaped = '<span>test</span>'; +(2048).times(function(){ + largeTextEscaped += ' ABC'; + largeTextUnescaped += ' ABC'; +}); diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/unittest.html b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/unittest.html new file mode 100644 index 0000000000..15d303aa3d --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/fixtures/unittest.html @@ -0,0 +1,18 @@ +<div id="testlog_2"> </div> + +<!-- Test elements follow --> +<div id="test_1" class="a bbbbbbbbbbbb cccccccccc dddd"> </div> + +<div id="test_2"> <span> </span> + + + +<div><div></div> </div><span> </span> +</div> + +<ul id="tlist"><li id="tlist_1">x1</li><li id="tlist_2">x2</li></ul> +<ul id="tlist2"><li class="a" id="tlist2_1">x1</li><li id="tlist2_2">x2</li></ul> + +<div id="testmoveby" style="background-color:#333;width:100px;">XXXX</div> + +<div id="testcss1">testcss1<span id="testcss1_span" style="display:none;">blah</span></div><div id="testcss2">testcss1</div> diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/form_test.js b/dom/tests/mochitest/ajax/prototype/test/unit/form_test.js new file mode 100644 index 0000000000..4cda87d81b --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/form_test.js @@ -0,0 +1,384 @@ +// sweet sweet additional assertions +Object.extend(Test.Unit.Testcase.prototype, { + assertEnabled: function() { + for (var i = 0, element; element = arguments[i]; i++) { + this.assert(!$(element).disabled, Test.Unit.inspect(element) + ' was disabled'); + } + }, + assertDisabled: function() { + for (var i = 0, element; element = arguments[i]; i++) { + this.assert($(element).disabled, Test.Unit.inspect(element) + ' was enabled'); + } + } +}); + +new Test.Unit.Runner({ + + // Make sure to set defaults in the test forms, as some browsers override this + // with previously entered values on page reload + setup: function(){ + $$('form').each(function(f){ f.reset() }); + // hidden value does not reset (for some reason) + $('bigform')['tf_hidden'].value = ''; + }, + + testDollarF: function(){ + this.assertEqual("4", $F("input_enabled")); + }, + + testFormElementEventObserver: function(){ + var callbackCounter = 0; + var observer = new Form.Element.EventObserver('input_enabled', function(){ + callbackCounter++; + }); + + this.assertEqual(0, callbackCounter); + $('input_enabled').value = 'boo!'; + observer.onElementEvent(); // can't test the event directly, simulating + this.assertEqual(1, callbackCounter); + }, + + testFormElementObserver: function(){ + var timedCounter = 0; + // First part: regular field + var observer = new Form.Element.Observer('input_enabled', 0.5, function() { + ++timedCounter; + }); + + // Test it's unchanged yet + this.assertEqual(0, timedCounter); + // Test it doesn't change on first check + this.wait(550, function() { + this.assertEqual(0, timedCounter); + // Change, test it doesn't immediately change + $('input_enabled').value = 'yowza!'; + this.assertEqual(0, timedCounter); + // Test it changes on next check, but not again on the next + this.wait(550, function() { + this.assertEqual(1, timedCounter); + this.wait(550, function() { + this.assertEqual(1, timedCounter); + observer.stop(); + }); + }); + }); + + // Second part: multiple-select + [1, 2, 3].each(function(index) { + $('multiSel1_opt' + index).selected = (1 == index); + }); + timedCounter = 0; + observer = new Form.Element.Observer('multiSel1', 0.5, function() { + ++timedCounter; + }); + + // Test it's unchanged yet + this.assertEqual(0, timedCounter); + // Test it doesn't change on first check + this.wait(550, function() { + this.assertEqual(0, timedCounter); + // Change, test it doesn't immediately change + // NOTE: it is important that the 3rd be re-selected, for the + // serialize form to obtain the expected value :-) + $('multiSel1_opt3').selected = true; + this.assertEqual(0, timedCounter); + // Test it changes on next check, but not again on the next + this.wait(550, function() { + this.assertEqual(1, timedCounter); + this.wait(550, function() { + this.assertEqual(1, timedCounter); + observer.stop(); + }); + }); + }); + }, + + testFormObserver: function(){ + var timedCounter = 0; + // should work the same way was Form.Element.Observer + var observer = new Form.Observer('form', 0.5, function(form, value) { + ++timedCounter; + }); + + // Test it's unchanged yet + this.assertEqual(0, timedCounter); + // Test it doesn't change on first check + this.wait(550, function() { + this.assertEqual(0, timedCounter); + // Change, test it doesn't immediately change + $('input_enabled').value = 'yowza!'; + this.assertEqual(0, timedCounter); + // Test it changes on next check, but not again on the next + this.wait(550, function() { + this.assertEqual(1, timedCounter); + this.wait(550, function() { + this.assertEqual(1, timedCounter); + observer.stop(); + }); + }); + }); + }, + + testFormEnabling: function(){ + var form = $('bigform') + var input1 = $('dummy_disabled'); + var input2 = $('focus_text'); + + this.assertDisabled(input1); + this.assertEnabled(input2); + + form.disable(); + this.assertDisabled(input1, input2); + form.enable(); + this.assertEnabled(input1, input2); + input1.disable(); + this.assertDisabled(input1); + + // non-form elements: + var fieldset = $('selects_fieldset'); + var fields = fieldset.immediateDescendants(); + fields.each(function(select) { this.assertEnabled(select) }, this); + + Form.disable(fieldset) + fields.each(function(select) { this.assertDisabled(select) }, this); + + Form.enable(fieldset) + fields.each(function(select) { this.assertEnabled(select) }, this); + }, + + testFormElementEnabling: function(){ + var field = $('input_disabled'); + field.enable(); + this.assertEnabled(field); + field.disable(); + this.assertDisabled(field); + + var field = $('input_enabled'); + this.assertEnabled(field); + field.disable(); + this.assertDisabled(field); + field.enable(); + this.assertEnabled(field); + }, + + // due to the lack of a DOM hasFocus() API method, + // we're simulating things here a little bit + testFormActivating: function(){ + // Firefox, IE, and Safari 2+ + function getSelection(element){ + try { + if (typeof element.selectionStart == 'number') { + return element.value.substring(element.selectionStart, element.selectionEnd); + } else if (document.selection && document.selection.createRange) { + return document.selection.createRange().text; + } + } + catch(e){ return null } + } + + // Form.focusFirstElement shouldn't focus disabled elements + var element = Form.findFirstElement('bigform'); + this.assertEqual('submit', element.id); + + // Test IE doesn't select text on buttons + Form.focusFirstElement('bigform'); + if (document.selection) this.assertEqual('', getSelection(element)); + + // Form.Element.activate shouldn't select text on buttons + element = $('focus_text'); + this.assertEqual('', getSelection(element)); + + // Form.Element.activate should select text on text input elements + element.activate(); + this.assertEqual('Hello', getSelection(element)); + + // Form.Element.activate shouldn't raise an exception when the form or field is hidden + this.assertNothingRaised(function() { + $('form_focus_hidden').focusFirstElement(); + }); + }, + + testFormGetElements: function() { + var elements = Form.getElements('various'), + names = $w('tf_selectOne tf_textarea tf_checkbox tf_selectMany tf_text tf_radio tf_hidden tf_password'); + this.assertEnumEqual(names, elements.pluck('name')) + }, + + testFormGetInputs: function() { + var form = $('form'); + [form.getInputs(), Form.getInputs(form)].each(function(inputs){ + this.assertEqual(inputs.length, 5); + this.assert(inputs instanceof Array); + this.assert(inputs.all(function(input) { return (input.tagName == "INPUT"); })); + }, this); + }, + + testFormFindFirstElement: function() { + this.assertEqual($('ffe_checkbox'), $('ffe').findFirstElement()); + this.assertEqual($('ffe_ti_submit'), $('ffe_ti').findFirstElement()); + this.assertEqual($('ffe_ti2_checkbox'), $('ffe_ti2').findFirstElement()); + }, + + testFormSerialize: function() { + // form is initially empty + var form = $('bigform'); + var expected = { tf_selectOne:'', tf_textarea:'', tf_text:'', tf_hidden:'', tf_password:'' }; + this.assertHashEqual(expected, Form.serialize('various', true)); + + // set up some stuff + form['tf_selectOne'].selectedIndex = 1; + form['tf_textarea'].value = "boo hoo!"; + form['tf_text'].value = "123öäü"; + form['tf_hidden'].value = "moo%hoo&test"; + form['tf_password'].value = 'sekrit code'; + form['tf_checkbox'].checked = true; + form['tf_radio'].checked = true; + var expected = { tf_selectOne:1, tf_textarea:"boo hoo!", tf_text:"123öäü", + tf_hidden:"moo%hoo&test", tf_password:'sekrit code', tf_checkbox:'on', tf_radio:'on' } + + // return params + this.assertHashEqual(expected, Form.serialize('various', true)); + // return string + this.assertEnumEqual(Object.toQueryString(expected).split('&').sort(), + Form.serialize('various').split('&').sort()); + this.assertEqual('string', typeof $('form').serialize({ hash:false })); + + // Checks that disabled element is not included in serialized form. + $('input_enabled').enable(); + this.assertHashEqual({ val1:4, action:'blah', first_submit:'Commit it!' }, + $('form').serialize(true)); + + // should not eat empty values for duplicate names + $('checkbox_hack').checked = false; + var data = Form.serialize('value_checks', true); + this.assertEnumEqual(['', 'siamese'], data['twin']); + this.assertEqual('0', data['checky']); + + $('checkbox_hack').checked = true; + this.assertEnumEqual($w('1 0'), Form.serialize('value_checks', true)['checky']); + + // all kinds of SELECT controls + var params = Form.serialize('selects_fieldset', true); + var expected = { 'nvm[]':['One', 'Three'], evu:'', 'evm[]':['', 'Three'] }; + this.assertHashEqual(expected, params); + params = Form.serialize('selects_wrapper', true); + this.assertHashEqual(Object.extend(expected, { vu:1, 'vm[]':[1, 3], nvu:'One' }), params); + + // explicit submit button + this.assertHashEqual({ val1:4, action:'blah', second_submit:'Delete it!' }, + $('form').serialize({ submit: 'second_submit' })); + this.assertHashEqual({ val1:4, action:'blah' }, + $('form').serialize({ submit: false })); + this.assertHashEqual({ val1:4, action:'blah' }, + $('form').serialize({ submit: 'inexistent' })); + + // file input should not be serialized + this.assertEqual('', $('form_with_file_input').serialize()); + }, + + testFormMethodsOnExtendedElements: function() { + var form = $('form'); + this.assertEqual(Form.serialize('form'), form.serialize()); + this.assertEqual(Form.Element.serialize('input_enabled'), $('input_enabled').serialize()); + this.assertNotEqual(form.serialize, $('input_enabled').serialize); + + Element.addMethods('INPUT', { anInputMethod: function(input) { return 'input' } }); + Element.addMethods('SELECT', { aSelectMethod: function(select) { return 'select' } }); + + form = $('bigform'); + var input = form['tf_text'], select = form['tf_selectOne']; + input._extendedByPrototype = select._extendedByPrototype = false; + + this.assert($(input).anInputMethod); + this.assert(!input.aSelectMethod); + this.assertEqual('input', input.anInputMethod()); + + this.assert($(select).aSelectMethod); + this.assert(!select.anInputMethod); + this.assertEqual('select', select.aSelectMethod()); + }, + + testFormRequest: function() { + request = $("form").request(); + this.assert($("form").hasAttribute("method")); + this.assert(request.url.include("fixtures/empty.js?val1=4")); + this.assertEqual("get", request.method); + + request = $("form").request({ method: "put", parameters: {val2: "hello"} }); + var url = request.url; + + this.assert(url.startsWith("fixtures/empty.js")); + this.assert(url.endsWith("_method=put") || url.endsWith("_method=put&_=")); + this.assertEqual(4, request.options.parameters['val1']); + this.assertEqual('hello', request.options.parameters['val2']); + this.assertEqual("post", request.method); + this.assertEqual("put", request.parameters['_method']); + + // with empty action attribute + request = $("ffe").request({ method: 'post' }); + this.assert(request.url.include("unit/tmp/form_test.html"), + 'wrong default action for form element with empty action attribute'); + }, + + testFormElementMethodsChaining: function(){ + var methods = $w('clear activate disable enable'), + formElements = $('form').getElements(); + methods.each(function(method){ + formElements.each(function(element){ + var returned = element[method](); + this.assertIdentical(element, returned); + }, this); + }, this); + }, + + testGetValue: function() { + this.assertEqual('Click Me', Form.Element.getValue('button_type_button')); // Button element + this.assertEqual(null, Form.Element.getValue('someBorkedId')); // Unknown + this.assertEqual(null, Form.Element.getValue('form')); // Unsupported + this.assertEqual(null, Form.Element.getValue('form')); // Unsupported + }, + + testSetValue: function(){ + // unkown element + this.assertEqual(null, Form.Element.setValue('someBorkedId', 'blah')); // Unknown + + // test button element + var button = $('button_type_button'); + button.setValue('I Changed'); + this.assertEqual('I Changed', button.getValue()); + + // text input + var input = $('input_enabled'), oldValue = input.getValue(); + this.assertEqual(input, input.setValue('foo'), 'setValue chaining is broken'); + this.assertEqual('foo', input.getValue(), 'value improperly set'); + input.setValue(oldValue); + this.assertEqual(oldValue, input.getValue(), 'value improperly restored to original'); + + // checkbox + input = $('checkbox_hack'); + input.setValue(false); + this.assertEqual(null, input.getValue(), 'checkbox should be unchecked'); + input.setValue(true); + this.assertEqual("1", input.getValue(), 'checkbox should be checked'); + + // selectbox + input = $('bigform')['vu']; + input.setValue('3'); + this.assertEqual('3', input.getValue(), 'single select option improperly set'); + input.setValue('1'); + this.assertEqual('1', input.getValue()); + + // multiple select + input = $('bigform')['vm[]']; + input.setValue(['2', '3']); + this.assertEnumEqual(['2', '3'], input.getValue(), + 'multiple select options improperly set'); + input.setValue(['1', '3']); + this.assertEnumEqual(['1', '3'], input.getValue()); + + // test chainability + var input = $('input_enabled'); + this.assert(Object.isElement(button.setValue('New Value'))); + this.assert(Object.isElement(input.setValue('New Value'))); + } +});
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/hash_test.js b/dom/tests/mochitest/ajax/prototype/test/unit/hash_test.js new file mode 100644 index 0000000000..cc3d083135 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/hash_test.js @@ -0,0 +1,178 @@ +new Test.Unit.Runner({ + testSet: function() { + var h = $H({a: 'A'}) + + this.assertEqual('B', h.set('b', 'B')); + this.assertHashEqual({a: 'A', b: 'B'}, h); + + this.assertUndefined(h.set('c')); + this.assertHashEqual({a: 'A', b: 'B', c: undefined}, h); + }, + + testGet: function() { + var h = $H({a: 'A'}); + this.assertEqual('A', h.get('a')); + this.assertUndefined(h.a); + this.assertUndefined($H({}).get('a')); + + this.assertUndefined($H({}).get('toString')); + this.assertUndefined($H({}).get('constructor')); + }, + + testUnset: function() { + var hash = $H(Fixtures.many); + this.assertEqual('B', hash.unset('b')); + this.assertHashEqual({a:'A', c: 'C', d:'D#'}, hash); + this.assertUndefined(hash.unset('z')); + this.assertHashEqual({a:'A', c: 'C', d:'D#'}, hash); + // not equivalent to Hash#remove + this.assertEqual('A', hash.unset('a', 'c')); + this.assertHashEqual({c: 'C', d:'D#'}, hash); + }, + + testToObject: function() { + var hash = $H(Fixtures.many), object = hash.toObject(); + this.assertInstanceOf(Object, object); + this.assertHashEqual(Fixtures.many, object); + this.assertNotIdentical(Fixtures.many, object); + hash.set('foo', 'bar'); + this.assertHashNotEqual(object, hash.toObject()); + }, + + testConstruct: function() { + var object = Object.clone(Fixtures.one); + var h = new Hash(object), h2 = $H(object); + this.assertInstanceOf(Hash, h); + this.assertInstanceOf(Hash, h2); + + this.assertHashEqual({}, new Hash()); + this.assertHashEqual(object, h); + this.assertHashEqual(object, h2); + + h.set('foo', 'bar'); + this.assertHashNotEqual(object, h); + + var clone = $H(h); + this.assertInstanceOf(Hash, clone); + this.assertHashEqual(h, clone); + h.set('foo', 'foo'); + this.assertHashNotEqual(h, clone); + this.assertIdentical($H, Hash.from); + }, + + testKeys: function() { + this.assertEnumEqual([], $H({}).keys()); + this.assertEnumEqual(['a'], $H(Fixtures.one).keys()); + this.assertEnumEqual($w('a b c d'), $H(Fixtures.many).keys().sort()); + this.assertEnumEqual($w('plus quad'), $H(Fixtures.functions).keys().sort()); + }, + + testValues: function() { + this.assertEnumEqual([], $H({}).values()); + this.assertEnumEqual(['A#'], $H(Fixtures.one).values()); + this.assertEnumEqual($w('A B C D#'), $H(Fixtures.many).values().sort()); + this.assertEnumEqual($w('function function'), + $H(Fixtures.functions).values().map(function(i){ return typeof i })); + this.assertEqual(9, $H(Fixtures.functions).get('quad')(3)); + this.assertEqual(6, $H(Fixtures.functions).get('plus')(3)); + }, + + testIndex: function() { + this.assertUndefined($H().index('foo')); + + this.assert('a', $H(Fixtures.one).index('A#')); + this.assert('a', $H(Fixtures.many).index('A')); + this.assertUndefined($H(Fixtures.many).index('Z')) + + var hash = $H({a:1,b:'2',c:1}); + this.assert(['a','c'].include(hash.index(1))); + this.assertUndefined(hash.index('1')); + }, + + testMerge: function() { + var h = $H(Fixtures.many); + this.assertNotIdentical(h, h.merge()); + this.assertNotIdentical(h, h.merge({})); + this.assertInstanceOf(Hash, h.merge()); + this.assertInstanceOf(Hash, h.merge({})); + this.assertHashEqual(h, h.merge()); + this.assertHashEqual(h, h.merge({})); + this.assertHashEqual(h, h.merge($H())); + this.assertHashEqual({a:'A', b:'B', c:'C', d:'D#', aaa:'AAA' }, h.merge({aaa: 'AAA'})); + this.assertHashEqual({a:'A#', b:'B', c:'C', d:'D#' }, h.merge(Fixtures.one)); + }, + + testUpdate: function() { + var h = $H(Fixtures.many); + this.assertIdentical(h, h.update()); + this.assertIdentical(h, h.update({})); + this.assertHashEqual(h, h.update()); + this.assertHashEqual(h, h.update({})); + this.assertHashEqual(h, h.update($H())); + this.assertHashEqual({a:'A', b:'B', c:'C', d:'D#', aaa:'AAA' }, h.update({aaa: 'AAA'})); + this.assertHashEqual({a:'A#', b:'B', c:'C', d:'D#', aaa:'AAA' }, h.update(Fixtures.one)); + }, + + testToQueryString: function() { + this.assertEqual('', $H({}).toQueryString()); + this.assertEqual('a%23=A', $H({'a#': 'A'}).toQueryString()); + this.assertEqual('a=A%23', $H(Fixtures.one).toQueryString()); + this.assertEqual('a=A&b=B&c=C&d=D%23', $H(Fixtures.many).toQueryString()); + this.assertEqual("a=b&c", $H(Fixtures.value_undefined).toQueryString()); + this.assertEqual("a=b&c", $H("a=b&c".toQueryParams()).toQueryString()); + this.assertEqual("a=b&c=", $H(Fixtures.value_null).toQueryString()); + this.assertEqual("a=b&c=0", $H(Fixtures.value_zero).toQueryString()); + this.assertEqual("color=r&color=g&color=b", $H(Fixtures.multiple).toQueryString()); + this.assertEqual("color=r&color=&color=g&color&color=0", $H(Fixtures.multiple_nil).toQueryString()); + this.assertEqual("color=&color", $H(Fixtures.multiple_all_nil).toQueryString()); + this.assertEqual("", $H(Fixtures.multiple_empty).toQueryString()); + this.assertEqual("", $H({foo: {}, bar: {}}).toQueryString()); + this.assertEqual("stuff%5B%5D=%24&stuff%5B%5D=a&stuff%5B%5D=%3B", $H(Fixtures.multiple_special).toQueryString()); + this.assertHashEqual(Fixtures.multiple_special, $H(Fixtures.multiple_special).toQueryString().toQueryParams()); + this.assertIdentical(Object.toQueryString, Hash.toQueryString); + }, + + testInspect: function() { + this.assertEqual('#<Hash:{}>', $H({}).inspect()); + this.assertEqual("#<Hash:{'a': 'A#'}>", $H(Fixtures.one).inspect()); + this.assertEqual("#<Hash:{'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D#'}>", $H(Fixtures.many).inspect()); + }, + + testClone: function() { + var h = $H(Fixtures.many); + this.assertHashEqual(h, h.clone()); + this.assertInstanceOf(Hash, h.clone()); + this.assertNotIdentical(h, h.clone()); + }, + + testToJSON: function() { + this.assertEqual('{\"b\": [false, true], \"c\": {\"a\": \"hello!\"}}', + $H({'b': [undefined, false, true, undefined], c: {a: 'hello!'}}).toJSON()); + }, + + testAbilityToContainAnyKey: function() { + var h = $H({ _each: 'E', map: 'M', keys: 'K', pluck: 'P', unset: 'U' }); + this.assertEnumEqual($w('_each keys map pluck unset'), h.keys().sort()); + this.assertEqual('U', h.unset('unset')); + this.assertHashEqual({ _each: 'E', map: 'M', keys: 'K', pluck: 'P' }, h); + }, + + testHashToTemplateReplacements: function() { + var template = new Template("#{a} #{b}"), hash = $H({ a: "hello", b: "world" }); + this.assertEqual("hello world", template.evaluate(hash.toObject())); + this.assertEqual("hello world", template.evaluate(hash)); + this.assertEqual("hello", "#{a}".interpolate(hash)); + }, + + testPreventIterationOverShadowedProperties: function() { + // redundant now that object is systematically cloned. + var FooMaker = function(value) { + this.key = value; + }; + FooMaker.prototype.key = 'foo'; + var foo = new FooMaker('bar'); + this.assertEqual("key=bar", new Hash(foo).toQueryString()); + this.assertEqual("key=bar", new Hash(new Hash(foo)).toQueryString()); + } + +});
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/number_test.js b/dom/tests/mochitest/ajax/prototype/test/unit/number_test.js new file mode 100644 index 0000000000..4e09529b2c --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/number_test.js @@ -0,0 +1,44 @@ +new Test.Unit.Runner({ + + testNumberMathMethods: function() { + this.assertEqual(1, (0.9).round()); + this.assertEqual(-2, (-1.9).floor()); + this.assertEqual(-1, (-1.9).ceil()); + + $w('abs floor round ceil').each(function(method) { + this.assertEqual(Math[method](Math.PI), Math.PI[method]()); + }, this); + }, + + testNumberToColorPart: function() { + this.assertEqual('00', (0).toColorPart()); + this.assertEqual('0a', (10).toColorPart()); + this.assertEqual('ff', (255).toColorPart()); + }, + + testNumberToPaddedString: function() { + this.assertEqual('00', (0).toPaddedString(2, 16)); + this.assertEqual('0a', (10).toPaddedString(2, 16)); + this.assertEqual('ff', (255).toPaddedString(2, 16)); + this.assertEqual('000', (0).toPaddedString(3)); + this.assertEqual('010', (10).toPaddedString(3)); + this.assertEqual('100', (100).toPaddedString(3)); + this.assertEqual('1000', (1000).toPaddedString(3)); + }, + + testNumberToJSON: function() { + this.assertEqual('null', Number.NaN.toJSON()); + this.assertEqual('0', (0).toJSON()); + this.assertEqual('-293', (-293).toJSON()); + }, + + testNumberTimes: function() { + var results = []; + (5).times(function(i) { results.push(i) }); + this.assertEnumEqual($R(0, 4), results); + + results = []; + (5).times(function(i) { results.push(i * this.i) }, { i: 2 }); + this.assertEnumEqual([0, 2, 4, 6, 8], results); + } +});
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/position_test.js b/dom/tests/mochitest/ajax/prototype/test/unit/position_test.js new file mode 100644 index 0000000000..5cea791b1f --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/position_test.js @@ -0,0 +1,44 @@ +var testVar = 'to be updated'; + +new Test.Unit.Runner({ + + setup: function() { + scrollTo(0,0); + Position.prepare(); + Position.includeScrollOffsets = false; + }, + + teardown: function() { + scrollTo(0,0); + Position.prepare(); + Position.includeScrollOffsets = false; + }, + + testPrepare: function() { + Position.prepare(); + this.assertEqual(0, Position.deltaX); + this.assertEqual(0, Position.deltaY); + scrollTo(20,30); + Position.prepare(); + this.assertEqual(20, Position.deltaX); + this.assertEqual(30, Position.deltaY); + }, + + testWithin: function() { + [true, false].each(function(withScrollOffsets) { + Position.includeScrollOffsets = withScrollOffsets; + this.assert(!Position.within($('body_absolute'), 9, 9), 'outside left/top'); + this.assert(Position.within($('body_absolute'), 10, 10), 'left/top corner'); + this.assert(Position.within($('body_absolute'), 10, 19), 'left/bottom corner'); + this.assert(!Position.within($('body_absolute'), 10, 20), 'outside bottom'); + }, this); + + scrollTo(20,30); + Position.prepare(); + Position.includeScrollOffsets = true; + this.assert(!Position.within($('body_absolute'), 9, 9), 'outside left/top'); + this.assert(Position.within($('body_absolute'), 10, 10), 'left/top corner'); + this.assert(Position.within($('body_absolute'), 10, 19), 'left/bottom corner'); + this.assert(!Position.within($('body_absolute'), 10, 20), 'outside bottom'); + } +});
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/range_test.js b/dom/tests/mochitest/ajax/prototype/test/unit/range_test.js new file mode 100644 index 0000000000..bcf5acb0b5 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/range_test.js @@ -0,0 +1,58 @@ +new Test.Unit.Runner({ + + testInclude: function() { + this.assert(!$R(0, 0, true).include(0)); + this.assert($R(0, 0, false).include(0)); + + this.assert($R(0, 5, true).include(0)); + this.assert($R(0, 5, true).include(4)); + this.assert(!$R(0, 5, true).include(5)); + + this.assert($R(0, 5, false).include(0)); + this.assert($R(0, 5, false).include(5)); + this.assert(!$R(0, 5, false).include(6)); + }, + + testEach: function() { + var results = []; + $R(0, 0, true).each(function(value) { + results.push(value); + }); + + this.assertEnumEqual([], results); + + results = []; + $R(0, 3, false).each(function(value) { + results.push(value); + }); + + this.assertEnumEqual([0, 1, 2, 3], results); + }, + + testAny: function() { + this.assert(!$R(1, 1, true).any()); + this.assert($R(0, 3, false).any(function(value) { + return value == 3; + })); + }, + + testAll: function() { + this.assert($R(1, 1, true).all()); + this.assert($R(0, 3, false).all(function(value) { + return value <= 3; + })); + }, + + testToArray: function() { + this.assertEnumEqual([], $R(0, 0, true).toArray()); + this.assertEnumEqual([0], $R(0, 0, false).toArray()); + this.assertEnumEqual([0], $R(0, 1, true).toArray()); + this.assertEnumEqual([0, 1], $R(0, 1, false).toArray()); + this.assertEnumEqual([-3, -2, -1, 0, 1, 2], $R(-3, 3, true).toArray()); + this.assertEnumEqual([-3, -2, -1, 0, 1, 2, 3], $R(-3, 3, false).toArray()); + }, + + testDefaultsToNotExclusive: function() { + this.assertEnumEqual($R(-3,3), $R(-3,3,false)); + } +});
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/selector_test.js b/dom/tests/mochitest/ajax/prototype/test/unit/selector_test.js new file mode 100644 index 0000000000..72506fc17d --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/selector_test.js @@ -0,0 +1,377 @@ +var $RunBenchmarks = false; + +new Test.Unit.Runner({ + + testSelectorWithTagName: function() { + this.assertEnumEqual($A(document.getElementsByTagName('li')), $$('li')); + this.assertEnumEqual([$('strong')], $$('strong')); + this.assertEnumEqual([], $$('nonexistent')); + + var allNodes = $A(document.getElementsByTagName('*')).select( function(node) { + return node.tagName !== '!'; + }); + this.assertEnumEqual(allNodes, $$('*')); + }, + + testSelectorWithId: function() { + this.assertEnumEqual([$('fixtures')], $$('#fixtures')); + this.assertEnumEqual([], $$('#nonexistent')); + this.assertEnumEqual([$('troubleForm')], $$('#troubleForm')); + }, + + testSelectorWithClassName: function() { + this.assertEnumEqual($('p', 'link_1', 'item_1'), $$('.first')); + this.assertEnumEqual([], $$('.second')); + }, + + testSelectorWithTagNameAndId: function() { + this.assertEnumEqual([$('strong')], $$('strong#strong')); + this.assertEnumEqual([], $$('p#strong')); + }, + + testSelectorWithTagNameAndClassName: function() { + this.assertEnumEqual($('link_1', 'link_2'), $$('a.internal')); + this.assertEnumEqual([$('link_2')], $$('a.internal.highlight')); + this.assertEnumEqual([$('link_2')], $$('a.highlight.internal')); + this.assertEnumEqual([], $$('a.highlight.internal.nonexistent')); + }, + + testSelectorWithIdAndClassName: function() { + this.assertEnumEqual([$('link_2')], $$('#link_2.internal')); + this.assertEnumEqual([$('link_2')], $$('.internal#link_2')); + this.assertEnumEqual([$('link_2')], $$('#link_2.internal.highlight')); + this.assertEnumEqual([], $$('#link_2.internal.nonexistent')); + }, + + testSelectorWithTagNameAndIdAndClassName: function() { + this.assertEnumEqual([$('link_2')], $$('a#link_2.internal')); + this.assertEnumEqual([$('link_2')], $$('a.internal#link_2')); + this.assertEnumEqual([$('item_1')], $$('li#item_1.first')); + this.assertEnumEqual([], $$('li#item_1.nonexistent')); + this.assertEnumEqual([], $$('li#item_1.first.nonexistent')); + }, + + test$$MatchesAncestryWithTokensSeparatedByWhitespace: function() { + this.assertEnumEqual($('em2', 'em', 'span'), $$('#fixtures a *')); + this.assertEnumEqual([$('p')], $$('div#fixtures p')); + }, + + test$$CombinesResultsWhenMultipleExpressionsArePassed: function() { + this.assertEnumEqual($('link_1', 'link_2', 'item_1', 'item_2', 'item_3'), $$('#p a', ' ul#list li ')); + }, + + testSelectorWithTagNameAndAttributeExistence: function() { + this.assertEnumEqual($$('#fixtures h1'), $$('h1[class]'), 'h1[class]'); + this.assertEnumEqual($$('#fixtures h1'), $$('h1[CLASS]'), 'h1[CLASS]'); + this.assertEnumEqual([$('item_3')], $$('li#item_3[class]'), 'li#item_3[class]'); + }, + + testSelectorWithTagNameAndSpecificAttributeValue: function() { + this.assertEnumEqual($('link_1', 'link_2', 'link_3'), $$('a[href="#"]')); + this.assertEnumEqual($('link_1', 'link_2', 'link_3'), $$('a[href=#]')); + }, + + testSelectorWithTagNameAndWhitespaceTokenizedAttributeValue: function() { + this.assertEnumEqual($('link_1', 'link_2'), $$('a[class~="internal"]'), "a[class~=\"internal\"]"); + this.assertEnumEqual($('link_1', 'link_2'), $$('a[class~=internal]'), "a[class~=internal]"); + }, + + testSelectorWithAttributeAndNoTagName: function() { + this.assertEnumEqual($(document.body).select('a[href]'), $(document.body).select('[href]')); + this.assertEnumEqual($$('a[class~="internal"]'), $$('[class~=internal]')); + this.assertEnumEqual($$('*[id]'), $$('[id]')); + this.assertEnumEqual($('checked_radio', 'unchecked_radio'), $$('[type=radio]')); + this.assertEnumEqual($$('*[type=checkbox]'), $$('[type=checkbox]')); + this.assertEnumEqual($('with_title', 'commaParent'), $$('[title]')); + this.assertEnumEqual($$('#troubleForm *[type=radio]'), $$('#troubleForm [type=radio]')); + this.assertEnumEqual($$('#troubleForm *[type]'), $$('#troubleForm [type]')); + }, + + testSelectorWithUniversalAndHyphenTokenizedAttributeValue: function() { + this.assertEnumEqual([$('item_3')], $$('*[xml:lang|="es"]')); + this.assertEnumEqual([$('item_3')], $$('*[xml:lang|="ES"]')); + }, + + testSelectorWithTagNameAndNegatedAttributeValue: function() { + this.assertEnumEqual([], $$('a[href!="#"]')); + }, + + testSelectorWithBracketAttributeValue: function() { + this.assertEnumEqual($('chk_1', 'chk_2'), $$('#troubleForm2 input[name="brackets[5][]"]')); + this.assertEnumEqual([$('chk_1')], $$('#troubleForm2 input[name="brackets[5][]"]:checked')); + this.assertEnumEqual([$('chk_2')], $$('#troubleForm2 input[name="brackets[5][]"][value=2]')); + this.assertEnumEqual([], $$('#troubleForm2 input[name=brackets[5][]]')); + }, + + test$$WithNestedAttributeSelectors: function() { + this.assertEnumEqual([$('strong')], $$('div[style] p[id] strong'), 'div[style] p[id] strong'); + }, + + testSelectorWithMultipleConditions: function() { + this.assertEnumEqual([$('link_3')], $$('a[class~=external][href="#"]'), + 'a[class~=external][href="#"]'); + this.assertEnumEqual([], $$('a[class~=external][href!="#"]'), + 'a[class~=external][href!="#"]'); + }, + + testSelectorMatchElements: function() { + this.assertElementsMatch(Selector.matchElements($('list').descendants(), 'li'), '#item_1', '#item_2', '#item_3'); + this.assertElementsMatch(Selector.matchElements($('fixtures').descendants(), 'a.internal'), '#link_1', '#link_2'); + this.assertEnumEqual([], Selector.matchElements($('fixtures').descendants(), 'p.last')); + this.assertElementsMatch(Selector.matchElements($('fixtures').descendants(), '.inexistant, a.internal'), '#link_1', '#link_2'); + }, + + testSelectorFindElement: function() { + this.assertElementMatches(Selector.findElement($('list').descendants(), 'li'), 'li#item_1.first'); + this.assertElementMatches(Selector.findElement($('list').descendants(), 'li', 1), 'li#item_2'); + this.assertElementMatches(Selector.findElement($('list').descendants(), 'li#item_3'), 'li'); + this.assertEqual(undefined, Selector.findElement($('list').descendants(), 'em')); + }, + + testElementMatch: function() { + var span = $('dupL1'); + + // tests that should pass + this.assert(span.match('span')); + this.assert(span.match('span#dupL1')); + this.assert(span.match('div > span'), 'child combinator'); + this.assert(span.match('#dupContainer span'), 'descendant combinator'); + this.assert(span.match('#dupL1'), 'ID only'); + this.assert(span.match('span.span_foo'), 'class name 1'); + this.assert(span.match('span.span_bar'), 'class name 2'); + this.assert(span.match('span:first-child'), 'first-child pseudoclass'); + + this.assert(!span.match('span.span_wtf'), 'bogus class name'); + this.assert(!span.match('#dupL2'), 'different ID'); + this.assert(!span.match('div'), 'different tag name'); + this.assert(!span.match('span span'), 'different ancestry'); + this.assert(!span.match('span > span'), 'different parent'); + this.assert(!span.match('span:nth-child(5)'), 'different pseudoclass'); + + this.assert(!$('link_2').match('a[rel^=external]')); + this.assert($('link_1').match('a[rel^=external]')); + this.assert($('link_1').match('a[rel^="external"]')); + this.assert($('link_1').match("a[rel^='external']")); + + this.assert(span.match({ match: function(element) { return true }}), 'custom selector'); + this.assert(!span.match({ match: function(element) { return false }}), 'custom selector'); + }, + + testSelectorWithSpaceInAttributeValue: function() { + this.assertEnumEqual([$('with_title')], $$('cite[title="hello world!"]')); + }, + + // AND NOW COME THOSE NEW TESTS AFTER ANDREW'S REWRITE! + + testSelectorWithNamespacedAttributes: function() { + if (Prototype.BrowserFeatures.XPath) { + this.assertUndefined(new Selector('html[xml:lang]').xpath); + this.assertUndefined(new Selector('body p[xml:lang]').xpath); + } else + this.info("Could not test XPath bypass: no XPath to begin with!"); + + this.assertElementsMatch($$('[xml:lang]'), 'html', '#item_3'); + this.assertElementsMatch($$('*[xml:lang]'), 'html', '#item_3'); + }, + + testSelectorWithChild: function() { + this.assertEnumEqual($('link_1', 'link_2'), $$('p.first > a')); + this.assertEnumEqual($('father', 'uncle'), $$('div#grandfather > div')); + this.assertEnumEqual($('level2_1', 'level2_2'), $$('#level1>span')); + this.assertEnumEqual($('level2_1', 'level2_2'), $$('#level1 > span')); + this.assertEnumEqual($('level3_1', 'level3_2'), $$('#level2_1 > *')); + this.assertEnumEqual([], $$('div > #nonexistent')); + $RunBenchmarks && this.wait(500, function() { + this.benchmark(function() { $$('#level1 > span') }, 1000); + }); + }, + + testSelectorWithAdjacence: function() { + this.assertEnumEqual([$('uncle')], $$('div.brothers + div.brothers')); + this.assertEnumEqual([$('uncle')], $$('div.brothers + div')); + this.assertEqual($('level2_2'), $$('#level2_1+span').reduce()); + this.assertEqual($('level2_2'), $$('#level2_1 + span').reduce()); + this.assertEqual($('level2_2'), $$('#level2_1 + *').reduce()); + this.assertEnumEqual([], $$('#level2_2 + span')); + this.assertEqual($('level3_2'), $$('#level3_1 + span').reduce()); + this.assertEqual($('level3_2'), $$('#level3_1 + *').reduce()); + this.assertEnumEqual([], $$('#level3_2 + *')); + this.assertEnumEqual([], $$('#level3_1 + em')); + $RunBenchmarks && this.wait(500, function() { + this.benchmark(function() { $$('#level3_1 + span') }, 1000); + }); + }, + + testSelectorWithLaterSibling: function() { + this.assertEnumEqual([$('list')], $$('h1 ~ ul')); + this.assertEqual($('level2_2'), $$('#level2_1 ~ span').reduce()); + this.assertEnumEqual($('level2_2', 'level2_3'), $$('#level2_1 ~ *').reduce()); + this.assertEnumEqual([], $$('#level2_2 ~ span')); + this.assertEnumEqual([], $$('#level3_2 ~ *')); + this.assertEnumEqual([], $$('#level3_1 ~ em')); + this.assertEnumEqual([$('level3_2')], $$('#level3_1 ~ #level3_2')); + this.assertEnumEqual([$('level3_2')], $$('span ~ #level3_2')); + this.assertEnumEqual([], $$('div ~ #level3_2')); + this.assertEnumEqual([], $$('div ~ #level2_3')); + $RunBenchmarks && this.wait(500, function() { + this.benchmark(function() { $$('#level2_1 ~ span') }, 1000); + }); + }, + + testSelectorWithNewAttributeOperators: function() { + this.assertEnumEqual($('father', 'uncle'), $$('div[class^=bro]'), 'matching beginning of string'); + this.assertEnumEqual($('father', 'uncle'), $$('div[class$=men]'), 'matching end of string'); + this.assertEnumEqual($('father', 'uncle'), $$('div[class*="ers m"]'), 'matching substring') + this.assertEnumEqual($('level2_1', 'level2_2', 'level2_3'), $$('#level1 *[id^="level2_"]')); + this.assertEnumEqual($('level2_1', 'level2_2', 'level2_3'), $$('#level1 *[id^=level2_]')); + this.assertEnumEqual($('level2_1', 'level3_1'), $$('#level1 *[id$="_1"]')); + this.assertEnumEqual($('level2_1', 'level3_1'), $$('#level1 *[id$=_1]')); + this.assertEnumEqual($('level2_1', 'level3_2', 'level2_2', 'level2_3'), $$('#level1 *[id*="2"]')); + this.assertEnumEqual($('level2_1', 'level3_2', 'level2_2', 'level2_3'), $$('#level1 *[id*=2]')); + $RunBenchmarks && this.wait(500, function() { + this.benchmark(function() { $$('#level1 *[id^=level2_]') }, 1000, '[^=]'); + this.benchmark(function() { $$('#level1 *[id$=_1]') }, 1000, '[$=]'); + this.benchmark(function() { $$('#level1 *[id*=_2]') }, 1000, '[*=]'); + }); + }, + + testSelectorWithDuplicates: function() { + this.assertEnumEqual($$('div div'), $$('div div').uniq()); + this.assertEnumEqual($('dupL2', 'dupL3', 'dupL4', 'dupL5'), $$('#dupContainer span span')); + $RunBenchmarks && this.wait(500, function() { + this.benchmark(function() { $$('#dupContainer span span') }, 1000); + }); + }, + + testSelectorWithFirstLastOnlyNthNthLastChild: function() { + this.assertEnumEqual([$('level2_1')], $$('#level1>*:first-child')); + this.assertEnumEqual($('level2_1', 'level3_1', 'level_only_child'), $$('#level1 *:first-child')); + this.assertEnumEqual([$('level2_3')], $$('#level1>*:last-child')); + this.assertEnumEqual($('level3_2', 'level_only_child', 'level2_3'), $$('#level1 *:last-child')); + this.assertEnumEqual([$('level2_3')], $$('#level1>div:last-child')); + this.assertEnumEqual([$('level2_3')], $$('#level1 div:last-child')); + this.assertEnumEqual([], $$('#level1>div:first-child')); + this.assertEnumEqual([], $$('#level1>span:last-child')); + this.assertEnumEqual($('level2_1', 'level3_1'), $$('#level1 span:first-child')); + this.assertEnumEqual([], $$('#level1:first-child')); + this.assertEnumEqual([], $$('#level1>*:only-child')); + this.assertEnumEqual([$('level_only_child')], $$('#level1 *:only-child')); + this.assertEnumEqual([], $$('#level1:only-child')); + this.assertEnumEqual([$('link_2')], $$('#p *:nth-last-child(2)'), 'nth-last-child'); + this.assertEnumEqual([$('link_2')], $$('#p *:nth-child(3)'), 'nth-child'); + this.assertEnumEqual([$('link_2')], $$('#p a:nth-child(3)'), 'nth-child'); + this.assertEnumEqual($('item_2', 'item_3'), $$('#list > li:nth-child(n+2)')); + this.assertEnumEqual($('item_1', 'item_2'), $$('#list > li:nth-child(-n+2)')); + $RunBenchmarks && this.wait(500, function() { + this.benchmark(function() { $$('#level1 *:first-child') }, 1000, ':first-child'); + this.benchmark(function() { $$('#level1 *:last-child') }, 1000, ':last-child'); + this.benchmark(function() { $$('#level1 *:only-child') }, 1000, ':only-child'); + }); + }, + + testSelectorWithFirstLastNthNthLastOfType: function() { + this.assertEnumEqual([$('link_2')], $$('#p a:nth-of-type(2)'), 'nth-of-type'); + this.assertEnumEqual([$('link_1')], $$('#p a:nth-of-type(1)'), 'nth-of-type'); + this.assertEnumEqual([$('link_2')], $$('#p a:nth-last-of-type(1)'), 'nth-last-of-type'); + this.assertEnumEqual([$('link_1')], $$('#p a:first-of-type'), 'first-of-type'); + this.assertEnumEqual([$('link_2')], $$('#p a:last-of-type'), 'last-of-type'); + }, + + testSelectorWithNot: function() { + this.assertEnumEqual([$('link_2')], $$('#p a:not(a:first-of-type)'), 'first-of-type'); + this.assertEnumEqual([$('link_1')], $$('#p a:not(a:last-of-type)'), 'last-of-type'); + this.assertEnumEqual([$('link_2')], $$('#p a:not(a:nth-of-type(1))'), 'nth-of-type'); + this.assertEnumEqual([$('link_1')], $$('#p a:not(a:nth-last-of-type(1))'), 'nth-last-of-type'); + this.assertEnumEqual([$('link_2')], $$('#p a:not([rel~=nofollow])'), 'attribute 1'); + this.assertEnumEqual([$('link_2')], $$('#p a:not(a[rel^=external])'), 'attribute 2'); + this.assertEnumEqual([$('link_2')], $$('#p a:not(a[rel$=nofollow])'), 'attribute 3'); + this.assertEnumEqual([$('em')], $$('#p a:not(a[rel$="nofollow"]) > em'), 'attribute 4') + this.assertEnumEqual([$('item_2')], $$('#list li:not(#item_1):not(#item_3)'), 'adjacent :not clauses'); + this.assertEnumEqual([$('son')], $$('#grandfather > div:not(#uncle) #son')); + this.assertEnumEqual([$('em')], $$('#p a:not(a[rel$="nofollow"]) em'), 'attribute 4 + all descendants'); + this.assertEnumEqual([$('em')], $$('#p a:not(a[rel$="nofollow"])>em'), 'attribute 4 (without whitespace)'); + }, + + testSelectorWithEnabledDisabledChecked: function() { + this.assertEnumEqual([$('disabled_text_field')], $$('#troubleForm > *:disabled')); + // bug 452708 this.assertEnumEqual($('troubleForm').getInputs().without($('disabled_text_field'), $('hidden')), $$('#troubleForm > *:enabled')); + this.assertEnumEqual($('checked_box', 'checked_radio'), $$('#troubleForm *:checked')); + }, + + testSelectorWithEmpty: function() { + $('level3_1').innerHTML = ""; + this.assertEnumEqual($('level3_1', 'level3_2', 'level2_3'), + $$('#level1 *:empty'), '#level1 *:empty'); + this.assertEnumEqual([], $$('#level_only_child:empty'), 'newlines count as content!'); + }, + + testIdenticalResultsFromEquivalentSelectors: function() { + this.assertEnumEqual($$('div.brothers'), $$('div[class~=brothers]')); + this.assertEnumEqual($$('div.brothers'), $$('div[class~=brothers].brothers')); + this.assertEnumEqual($$('div:not(.brothers)'), $$('div:not([class~=brothers])')); + this.assertEnumEqual($$('li ~ li'), $$('li:not(:first-child)')); + this.assertEnumEqual($$('ul > li'), $$('ul > li:nth-child(n)')); + this.assertEnumEqual($$('ul > li:nth-child(even)'), $$('ul > li:nth-child(2n)')); + this.assertEnumEqual($$('ul > li:nth-child(odd)'), $$('ul > li:nth-child(2n+1)')); + this.assertEnumEqual($$('ul > li:first-child'), $$('ul > li:nth-child(1)')); + this.assertEnumEqual($$('ul > li:last-child'), $$('ul > li:nth-last-child(1)')); + this.assertEnumEqual($$('ul > li:nth-child(n-999)'), $$('ul > li')); + this.assertEnumEqual($$('ul>li'), $$('ul > li')); + this.assertEnumEqual($$('#p a:not(a[rel$="nofollow"])>em'), $$('#p a:not(a[rel$="nofollow"]) > em')) + }, + + testSelectorsThatShouldReturnNothing: function() { + this.assertEnumEqual([], $$('span:empty > *')); + this.assertEnumEqual([], $$('div.brothers:not(.brothers)')); + this.assertEnumEqual([], $$('#level2_2 :only-child:not(:last-child)')); + this.assertEnumEqual([], $$('#level2_2 :only-child:not(:first-child)')); + }, + + testCommasFor$$: function() { + this.assertEnumEqual($('list', 'p', 'link_1', 'item_1', 'item_3', 'troubleForm'), $$('#list, .first,*[xml:lang="es-us"] , #troubleForm')); + this.assertEnumEqual($('list', 'p', 'link_1', 'item_1', 'item_3', 'troubleForm'), $$('#list, .first,', '*[xml:lang="es-us"] , #troubleForm')); + this.assertEnumEqual($('commaParent', 'commaChild'), $$('form[title*="commas,"], input[value="#commaOne,#commaTwo"]')); + this.assertEnumEqual($('commaParent', 'commaChild'), $$('form[title*="commas,"]', 'input[value="#commaOne,#commaTwo"]')); + }, + + testSelectorExtendsAllNodes: function(){ + var element = document.createElement('div'); + (3).times(function(){ + element.appendChild(document.createElement('div')); + }); + element.setAttribute('id','scratch_element'); + $$('body')[0].appendChild(element); + + var results = $$('#scratch_element div'); + this.assert(typeof results[0].show == 'function'); + this.assert(typeof results[1].show == 'function'); + this.assert(typeof results[2].show == 'function'); + }, + + testCountedIsNotAnAttribute: function() { + var el = $('list'); + Selector.handlers.mark([el]); + this.assert(!el.innerHTML.include("_counted")); + Selector.handlers.unmark([el]); + this.assert(!el.innerHTML.include("_counted")); + }, + + testCopiedNodesGetIncluded: function() { + this.assertElementsMatch( + Selector.matchElements($('counted_container').descendants(), 'div'), + 'div.is_counted' + ); + $('counted_container').innerHTML += $('counted_container').innerHTML; + this.assertElementsMatch( + Selector.matchElements($('counted_container').descendants(), 'div'), 'div.is_counted', + 'div.is_counted' + ); + }, + + testElementDown: function() { + var a = $('dupL4'); + var b = $('dupContainer').down('#dupL4'); + + this.assertEqual(a, b); + } +}); diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/string_test.js b/dom/tests/mochitest/ajax/prototype/test/unit/string_test.js new file mode 100644 index 0000000000..c4e21f095b --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/string_test.js @@ -0,0 +1,540 @@ +new Test.Unit.Runner({ + testInterpret: function(){ + this.assertIdentical('true', String.interpret(true)); + this.assertIdentical('123', String.interpret(123)); + this.assertIdentical('foo bar', String.interpret('foo bar')); + this.assertIdentical( + 'object string', + String.interpret({ toString: function(){ return 'object string' } })); + + this.assertIdentical('0', String.interpret(0)); + this.assertIdentical('false', String.interpret(false)); + this.assertIdentical('', String.interpret(undefined)); + this.assertIdentical('', String.interpret(null)); + this.assertIdentical('', String.interpret('')); + }, + + testGsubWithReplacementFunction: function() { + var source = 'foo boo boz'; + + this.assertEqual('Foo Boo BoZ', + source.gsub(/[^o]+/, function(match) { + return match[0].toUpperCase() + })); + this.assertEqual('f2 b2 b1z', + source.gsub(/o+/, function(match) { + return match[0].length; + })); + this.assertEqual('f0 b0 b1z', + source.gsub(/o+/, function(match) { + return match[0].length % 2; + })); + + }, + + testGsubWithReplacementString: function() { + var source = 'foo boo boz'; + + this.assertEqual('foobooboz', + source.gsub(/\s+/, '')); + this.assertEqual(' z', + source.gsub(/(.)(o+)/, '')); + + this.assertEqual('ウィメンズ2007<br/>クルーズコレクション', + 'ウィメンズ2007\nクルーズコレクション'.gsub(/\n/,'<br/>')); + this.assertEqual('ウィメンズ2007<br/>クルーズコレクション', + 'ウィメンズ2007\nクルーズコレクション'.gsub('\n','<br/>')); + }, + + testGsubWithReplacementTemplateString: function() { + var source = 'foo boo boz'; + + this.assertEqual('-oo-#{1}- -oo-#{1}- -o-#{1}-z', + source.gsub(/(.)(o+)/, '-#{2}-\\#{1}-')); + this.assertEqual('-foo-f- -boo-b- -bo-b-z', + source.gsub(/(.)(o+)/, '-#{0}-#{1}-')); + this.assertEqual('-oo-f- -oo-b- -o-b-z', + source.gsub(/(.)(o+)/, '-#{2}-#{1}-')); + this.assertEqual(' z', + source.gsub(/(.)(o+)/, '#{3}')); + }, + + testSubWithReplacementFunction: function() { + var source = 'foo boo boz'; + + this.assertEqual('Foo boo boz', + source.sub(/[^o]+/, function(match) { + return match[0].toUpperCase() + }), 1); + this.assertEqual('Foo Boo boz', + source.sub(/[^o]+/, function(match) { + return match[0].toUpperCase() + }, 2), 2); + this.assertEqual(source, + source.sub(/[^o]+/, function(match) { + return match[0].toUpperCase() + }, 0), 0); + this.assertEqual(source, + source.sub(/[^o]+/, function(match) { + return match[0].toUpperCase() + }, -1), -1); + }, + + testSubWithReplacementString: function() { + var source = 'foo boo boz'; + + this.assertEqual('oo boo boz', + source.sub(/[^o]+/, '')); + this.assertEqual('oooo boz', + source.sub(/[^o]+/, '', 2)); + this.assertEqual('-f-oo boo boz', + source.sub(/[^o]+/, '-#{0}-')); + this.assertEqual('-f-oo- b-oo boz', + source.sub(/[^o]+/, '-#{0}-', 2)); + }, + + testScan: function() { + var source = 'foo boo boz', results = []; + var str = source.scan(/[o]+/, function(match) { + results.push(match[0].length); + }); + this.assertEnumEqual([2, 2, 1], results); + this.assertEqual(source, source.scan(/x/, this.fail)); + this.assert(typeof str == 'string'); + }, + + testToArray: function() { + this.assertEnumEqual([],''.toArray()); + this.assertEnumEqual(['a'],'a'.toArray()); + this.assertEnumEqual(['a','b'],'ab'.toArray()); + this.assertEnumEqual(['f','o','o'],'foo'.toArray()); + }, + + /* + Note that camelize() differs from its Rails counterpart, + as it is optimized for dealing with JavaScript object + properties in conjunction with CSS property names: + - Looks for dashes, not underscores + - CamelCases first word if there is a front dash + */ + testCamelize: function() { + this.assertEqual('', ''.camelize()); + this.assertEqual('', '-'.camelize()); + this.assertEqual('foo', 'foo'.camelize()); + this.assertEqual('foo_bar', 'foo_bar'.camelize()); + this.assertEqual('FooBar', '-foo-bar'.camelize()); + this.assertEqual('FooBar', 'FooBar'.camelize()); + + this.assertEqual('fooBar', 'foo-bar'.camelize()); + this.assertEqual('borderBottomWidth', 'border-bottom-width'.camelize()); + + this.assertEqual('classNameTest','class-name-test'.camelize()); + this.assertEqual('classNameTest','className-test'.camelize()); + this.assertEqual('classNameTest','class-nameTest'.camelize()); + + /* this.benchmark(function(){ + 'class-name-test'.camelize(); + },10000); */ + }, + + testCapitalize: function() { + this.assertEqual('',''.capitalize()); + this.assertEqual('Ä','ä'.capitalize()); + this.assertEqual('A','A'.capitalize()); + this.assertEqual('Hello','hello'.capitalize()); + this.assertEqual('Hello','HELLO'.capitalize()); + this.assertEqual('Hello','Hello'.capitalize()); + this.assertEqual('Hello world','hello WORLD'.capitalize()); + }, + + testUnderscore: function() { + this.assertEqual('', ''.underscore()); + this.assertEqual('_', '-'.underscore()); + this.assertEqual('foo', 'foo'.underscore()); + this.assertEqual('foo', 'Foo'.underscore()); + this.assertEqual('foo_bar', 'foo_bar'.underscore()); + this.assertEqual('border_bottom', 'borderBottom'.underscore()); + this.assertEqual('border_bottom_width', 'borderBottomWidth'.underscore()); + this.assertEqual('border_bottom_width', 'border-Bottom-Width'.underscore()); + }, + + testDasherize: function() { + this.assertEqual('', ''.dasherize()); + this.assertEqual('foo', 'foo'.dasherize()); + this.assertEqual('Foo', 'Foo'.dasherize()); + this.assertEqual('foo-bar', 'foo-bar'.dasherize()); + this.assertEqual('border-bottom-width', 'border_bottom_width'.dasherize()); + }, + + testTruncate: function() { + var source = 'foo boo boz foo boo boz foo boo boz foo boo boz'; + this.assertEqual(source, source.truncate(source.length)); + this.assertEqual('foo boo boz foo boo boz foo...', source.truncate(0)); + this.assertEqual('fo...', source.truncate(5)); + this.assertEqual('foo b', source.truncate(5, '')); + + this.assert(typeof 'foo'.truncate(5) == 'string'); + this.assert(typeof 'foo bar baz'.truncate(5) == 'string'); + }, + + testStrip: function() { + this.assertEqual('hello world', ' hello world '.strip()); + this.assertEqual('hello world', 'hello world'.strip()); + this.assertEqual('hello \n world', ' hello \n world '.strip()); + this.assertEqual('', ' '.strip()); + }, + + testStripTags: function() { + this.assertEqual('hello world', 'hello world'.stripTags()); + this.assertEqual('hello world', 'hello <span>world</span>'.stripTags()); + this.assertEqual('hello world', '<a href="#" onclick="moo!">hello</a> world'.stripTags()); + this.assertEqual('hello world', 'h<b><em>e</em></b>l<i>l</i>o w<span class="moo" id="x"><b>o</b></span>rld'.stripTags()); + this.assertEqual('1\n2', '1\n2'.stripTags()); + }, + + testStripScripts: function() { + this.assertEqual('foo bar', 'foo bar'.stripScripts()); + this.assertEqual('foo bar', ('foo <script>boo();<'+'/script>bar').stripScripts()); + this.assertEqual('foo bar', ('foo <script type="text/javascript">boo();\nmoo();<'+'/script>bar').stripScripts()); + }, + + testExtractScripts: function() { + this.assertEnumEqual([], 'foo bar'.extractScripts()); + this.assertEnumEqual(['boo();'], ('foo <script>boo();<'+'/script>bar').extractScripts()); + this.assertEnumEqual(['boo();','boo();\nmoo();'], + ('foo <script>boo();<'+'/script><script type="text/javascript">boo();\nmoo();<'+'/script>bar').extractScripts()); + this.assertEnumEqual(['boo();','boo();\nmoo();'], + ('foo <script>boo();<'+'/script>blub\nblub<script type="text/javascript">boo();\nmoo();<'+'/script>bar').extractScripts()); + + var russianChars = '//кПЌеМÑаÑОй\n'; + var longComment = '//' + Array(7000).join('.') + '\n'; + var longScript = '\nvar foo = 1;\n' + russianChars + longComment; + var longString = '<script type="text/javascript">'+ longScript + '<'+'/script>'; + this.assertEnumEqual([longScript], longString.extractScripts()); + + this.assertEnumEqual([], ('<!--\n<script>boo();<'+'/script>\n-->').extractScripts()); + }, + + testEvalScripts: function() { + this.assertEqual(0, evalScriptsCounter); + + ('foo <script>evalScriptsCounter++<'+'/script>bar').evalScripts(); + this.assertEqual(1, evalScriptsCounter); + + var stringWithScripts = ''; + (3).times(function(){ stringWithScripts += 'foo <script>evalScriptsCounter++<'+'/script>bar' }); + stringWithScripts.evalScripts(); + this.assertEqual(4, evalScriptsCounter); + }, + + testEscapeHTML: function() { + this.assertEqual('foo bar', 'foo bar'.escapeHTML()); + this.assertEqual('foo <span>bar</span>', 'foo <span>bar</span>'.escapeHTML()); + this.assertEqual('foo ß bar', 'foo ß bar'.escapeHTML()); + + this.assertEqual('ウィメンズ2007\nクルーズコレクション', + 'ウィメンズ2007\nクルーズコレクション'.escapeHTML()); + + this.assertEqual('a<a href="blah">blub</a>b<span><div></div></span>cdef<strong>!!!!</strong>g', + 'a<a href="blah">blub</a>b<span><div></div></span>cdef<strong>!!!!</strong>g'.escapeHTML()); + + this.assertEqual(largeTextEscaped, largeTextUnescaped.escapeHTML()); + + this.assertEqual('1\n2', '1\n2'.escapeHTML()); + + this.benchmark(function() { largeTextUnescaped.escapeHTML() }, 1000); + }, + + testUnescapeHTML: function() { + this.assertEqual('foo bar', 'foo bar'.unescapeHTML()); + this.assertEqual('foo <span>bar</span>', 'foo <span>bar</span>'.unescapeHTML()); + this.assertEqual('foo ß bar', 'foo ß bar'.unescapeHTML()); + + this.assertEqual('a<a href="blah">blub</a>b<span><div></div></span>cdef<strong>!!!!</strong>g', + 'a<a href="blah">blub</a>b<span><div></div></span>cdef<strong>!!!!</strong>g'.unescapeHTML()); + + this.assertEqual(largeTextUnescaped, largeTextEscaped.unescapeHTML()); + + this.assertEqual('test \xfa', 'test ú'.unescapeHTML()); + this.assertEqual('1\n2', '1\n2'.unescapeHTML()); + this.assertEqual('Pride & Prejudice', '<h1>Pride & Prejudice</h1>'.unescapeHTML()); + + var escapedTest = '"<" means "<" in HTML'; + this.assertEqual(escapedTest, escapedTest.escapeHTML().unescapeHTML()); + + this.benchmark(function() { largeTextEscaped.unescapeHTML() }, 1000); + + }, + + testTemplateEvaluation: function() { + var source = '<tr><td>#{name}</td><td>#{age}</td></tr>'; + var person = {name: 'Sam', age: 21}; + var template = new Template(source); + + this.assertEqual('<tr><td>Sam</td><td>21</td></tr>', + template.evaluate(person)); + this.assertEqual('<tr><td></td><td></td></tr>', + template.evaluate({})); + }, + + testTemplateEvaluationWithEmptyReplacement: function() { + var template = new Template('##{}'); + this.assertEqual('#', template.evaluate({})); + this.assertEqual('#', template.evaluate({foo: 'bar'})); + + template = new Template('#{}'); + this.assertEqual('', template.evaluate({})); + }, + + testTemplateEvaluationWithFalses: function() { + var source = '<tr><td>#{zero}</td><td>#{false_}</td><td>#{undef}</td><td>#{null_}</td><td>#{empty}</td></tr>'; + var falses = {zero:0, false_:false, undef:undefined, null_:null, empty:""}; + var template = new Template(source); + + this.assertEqual('<tr><td>0</td><td>false</td><td></td><td></td><td></td></tr>', + template.evaluate(falses)); + }, + + testTemplateEvaluationWithNested: function() { + var source = '#{name} #{manager.name} #{manager.age} #{manager.undef} #{manager.age.undef} #{colleagues.first.name}'; + var subject = { manager: { name: 'John', age: 29 }, name: 'Stephan', age: 22, colleagues: { first: { name: 'Mark' }} }; + this.assertEqual('Stephan', new Template('#{name}').evaluate(subject)); + this.assertEqual('John', new Template('#{manager.name}').evaluate(subject)); + this.assertEqual('29', new Template('#{manager.age}').evaluate(subject)); + this.assertEqual('', new Template('#{manager.undef}').evaluate(subject)); + this.assertEqual('', new Template('#{manager.age.undef}').evaluate(subject)); + this.assertEqual('Mark', new Template('#{colleagues.first.name}').evaluate(subject)); + this.assertEqual('Stephan John 29 Mark', new Template(source).evaluate(subject)); + }, + + testTemplateEvaluationWithIndexing: function() { + var source = '#{0} = #{[0]} - #{1} = #{[1]} - #{[2][0]} - #{[2].name} - #{first[0]} - #{[first][0]} - #{[\\]]} - #{first[\\]]}'; + var subject = [ 'zero', 'one', [ 'two-zero' ] ]; + subject[2].name = 'two-zero-name'; + subject.first = subject[2]; + subject[']'] = '\\'; + subject.first[']'] = 'first\\'; + this.assertEqual('zero', new Template('#{[0]}').evaluate(subject)); + this.assertEqual('one', new Template('#{[1]}').evaluate(subject)); + this.assertEqual('two-zero', new Template('#{[2][0]}').evaluate(subject)); + this.assertEqual('two-zero-name', new Template('#{[2].name}').evaluate(subject)); + this.assertEqual('two-zero', new Template('#{first[0]}').evaluate(subject)); + this.assertEqual('\\', new Template('#{[\\]]}').evaluate(subject)); + this.assertEqual('first\\', new Template('#{first[\\]]}').evaluate(subject)); + this.assertEqual('empty - empty2', new Template('#{[]} - #{m[]}').evaluate({ '': 'empty', m: {'': 'empty2'}})); + this.assertEqual('zero = zero - one = one - two-zero - two-zero-name - two-zero - two-zero - \\ - first\\', new Template(source).evaluate(subject)); + }, + + testTemplateToTemplateReplacements: function() { + var source = 'My name is #{name}, my job is #{job}'; + var subject = { + name: 'Stephan', + getJob: function() { return 'Web developer'; }, + toTemplateReplacements: function() { return { name: this.name, job: this.getJob() } } + }; + this.assertEqual('My name is Stephan, my job is Web developer', new Template(source).evaluate(subject)); + }, + + testTemplateEvaluationCombined: function() { + var source = '#{name} is #{age} years old, managed by #{manager.name}, #{manager.age}.\n' + + 'Colleagues include #{colleagues[0].name} and #{colleagues[1].name}.'; + var subject = { + name: 'Stephan', age: 22, + manager: { name: 'John', age: 29 }, + colleagues: [ { name: 'Mark' }, { name: 'Indy' } ] + }; + this.assertEqual('Stephan is 22 years old, managed by John, 29.\n' + + 'Colleagues include Mark and Indy.', + new Template(source).evaluate(subject)); + }, + + testInterpolate: function() { + var subject = { name: 'Stephan' }; + var pattern = /(^|.|\r|\n)(#\((.*?)\))/; + this.assertEqual('#{name}: Stephan', '\\#{name}: #{name}'.interpolate(subject)); + this.assertEqual('#(name): Stephan', '\\#(name): #(name)'.interpolate(subject, pattern)); + }, + + testToQueryParams: function() { + // only the query part + var result = {a:undefined, b:'c'}; + this.assertHashEqual({}, ''.toQueryParams(), 'empty query'); + this.assertHashEqual({}, 'foo?'.toQueryParams(), 'empty query with URL'); + this.assertHashEqual(result, 'foo?a&b=c'.toQueryParams(), 'query with URL'); + this.assertHashEqual(result, 'foo?a&b=c#fragment'.toQueryParams(), 'query with URL and fragment'); + this.assertHashEqual(result, 'a;b=c'.toQueryParams(';'), 'custom delimiter'); + + this.assertHashEqual({a:undefined}, 'a'.toQueryParams(), 'key without value'); + this.assertHashEqual({a:'b'}, 'a=b&=c'.toQueryParams(), 'empty key'); + this.assertHashEqual({a:'b', c:''}, 'a=b&c='.toQueryParams(), 'empty value'); + + this.assertHashEqual({'a b':'c', d:'e f', g:'h'}, + 'a%20b=c&d=e%20f&g=h'.toQueryParams(), 'proper decoding'); + this.assertHashEqual({a:'b=c=d'}, 'a=b=c=d'.toQueryParams(), 'multiple equal signs'); + this.assertHashEqual({a:'b', c:'d'}, '&a=b&&&c=d'.toQueryParams(), 'proper splitting'); + + this.assertEnumEqual($w('r g b'), 'col=r&col=g&col=b'.toQueryParams()['col'], + 'collection without square brackets'); + var msg = 'empty values inside collection'; + this.assertEnumEqual(['r', '', 'b'], 'c=r&c=&c=b'.toQueryParams()['c'], msg); + this.assertEnumEqual(['', 'blue'], 'c=&c=blue'.toQueryParams()['c'], msg); + this.assertEnumEqual(['blue', ''], 'c=blue&c='.toQueryParams()['c'], msg); + }, + + testInspect: function() { + this.assertEqual('\'\'', ''.inspect()); + this.assertEqual('\'test\'', 'test'.inspect()); + this.assertEqual('\'test \\\'test\\\' "test"\'', 'test \'test\' "test"'.inspect()); + this.assertEqual('\"test \'test\' \\"test\\"\"', 'test \'test\' "test"'.inspect(true)); + this.assertEqual('\'\\b\\t\\n\\f\\r"\\\\\'', '\b\t\n\f\r"\\'.inspect()); + this.assertEqual('\"\\b\\t\\n\\f\\r\\"\\\\\"', '\b\t\n\f\r"\\'.inspect(true)); + this.assertEqual('\'\\b\\t\\n\\f\\r\'', '\x08\x09\x0a\x0c\x0d'.inspect()); + this.assertEqual('\'\\u001a\'', '\x1a'.inspect()); + }, + + testInclude: function() { + this.assert('hello world'.include('h')); + this.assert('hello world'.include('hello')); + this.assert('hello world'.include('llo w')); + this.assert('hello world'.include('world')); + this.assert(!'hello world'.include('bye')); + this.assert(!''.include('bye')); + }, + + testStartsWith: function() { + this.assert('hello world'.startsWith('h')); + this.assert('hello world'.startsWith('hello')); + this.assert(!'hello world'.startsWith('bye')); + this.assert(!''.startsWith('bye')); + this.assert(!'hell'.startsWith('hello')); + }, + + testEndsWith: function() { + this.assert('hello world'.endsWith('d')); + this.assert('hello world'.endsWith(' world')); + this.assert(!'hello world'.endsWith('planet')); + this.assert(!''.endsWith('planet')); + this.assert('hello world world'.endsWith(' world')); + this.assert(!'z'.endsWith('az')); + }, + + testBlank: function() { + this.assert(''.blank()); + this.assert(' '.blank()); + this.assert('\t\r\n '.blank()); + this.assert(!'a'.blank()); + this.assert(!'\t y \n'.blank()); + }, + + testEmpty: function() { + this.assert(''.empty()); + this.assert(!' '.empty()); + this.assert(!'\t\r\n '.empty()); + this.assert(!'a'.empty()); + this.assert(!'\t y \n'.empty()); + }, + + testSucc: function() { + this.assertEqual('b', 'a'.succ()); + this.assertEqual('B', 'A'.succ()); + this.assertEqual('1', '0'.succ()); + this.assertEqual('abce', 'abcd'.succ()); + this.assertEqual('{', 'z'.succ()); + this.assertEqual(':', '9'.succ()); + }, + + testTimes: function() { + + this.assertEqual('', ''.times(0)); + this.assertEqual('', ''.times(5)); + this.assertEqual('', 'a'.times(-1)); + this.assertEqual('', 'a'.times(0)); + this.assertEqual('a', 'a'.times(1)); + this.assertEqual('aa', 'a'.times(2)); + this.assertEqual('aaaaa', 'a'.times(5)); + this.assertEqual('foofoofoofoofoo', 'foo'.times(5)); + this.assertEqual('', 'foo'.times(-5)); + + /*window.String.prototype.oldTimes = function(count) { + var result = ''; + for (var i = 0; i < count; i++) result += this; + return result; + }; + + this.benchmark(function() { + 'foo'.times(15); + }, 1000, 'new: '); + + this.benchmark(function() { + 'foo'.oldTimes(15); + }, 1000, 'previous: ');*/ + }, + + testToJSON: function() { + this.assertEqual('\"\"', ''.toJSON()); + this.assertEqual('\"test\"', 'test'.toJSON()); + }, + + testIsJSON: function() { + this.assert(!''.isJSON()); + this.assert(!' '.isJSON()); + this.assert('""'.isJSON()); + this.assert('"foo"'.isJSON()); + this.assert('{}'.isJSON()); + this.assert('[]'.isJSON()); + this.assert('null'.isJSON()); + this.assert('123'.isJSON()); + this.assert('true'.isJSON()); + this.assert('false'.isJSON()); + this.assert('"\\""'.isJSON()); + this.assert(!'\\"'.isJSON()); + this.assert(!'new'.isJSON()); + this.assert(!'\u0028\u0029'.isJSON()); + // we use '@' as a placeholder for characters authorized only inside brackets, + // so this tests make sure it is not considered authorized elsewhere. + this.assert(!'@'.isJSON()); + }, + + testEvalJSON: function() { + var valid = '{"test": \n\r"hello world!"}'; + var invalid = '{"test": "hello world!"'; + var dangerous = '{});attackTarget = "attack succeeded!";({}'; + + // use smaller huge string size for KHTML + var size = navigator.userAgent.include('KHTML') ? 20 : 100; + var longString = '"' + '123456789\\"'.times(size * 10) + '"'; + var object = '{' + longString + ': ' + longString + '},'; + var huge = '[' + object.times(size) + '{"test": 123}]'; + + this.assertEqual('hello world!', valid.evalJSON().test); + this.assertEqual('hello world!', valid.evalJSON(true).test); + this.assertRaise('SyntaxError', function() { invalid.evalJSON() }); + this.assertRaise('SyntaxError', function() { invalid.evalJSON(true) }); + + attackTarget = "scared"; + dangerous.evalJSON(); + this.assertEqual("attack succeeded!", attackTarget); + + attackTarget = "Not scared!"; + this.assertRaise('SyntaxError', function(){dangerous.evalJSON(true)}); + this.assertEqual("Not scared!", attackTarget); + + this.assertEqual('hello world!', ('/*-secure- \r \n ' + valid + ' \n */').evalJSON().test); + var temp = Prototype.JSONFilter; + Prototype.JSONFilter = /^\/\*([\s\S]*)\*\/$/; // test custom delimiters. + this.assertEqual('hello world!', ('/*' + valid + '*/').evalJSON().test); + Prototype.JSONFilter = temp; + + this.assertMatch(123, huge.evalJSON(true).last().test); + + this.assertEqual('', '""'.evalJSON()); + this.assertEqual('foo', '"foo"'.evalJSON()); + this.assert('object', typeof '{}'.evalJSON()); + this.assert(Object.isArray('[]'.evalJSON())); + this.assertNull('null'.evalJSON()); + this.assert(123, '123'.evalJSON()); + this.assertIdentical(true, 'true'.evalJSON()); + this.assertIdentical(false, 'false'.evalJSON()); + this.assertEqual('"', '"\\""'.evalJSON()); + } +});
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/tmp/ajax_test.html b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/ajax_test.html new file mode 100644 index 0000000000..b38fd306ca --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/ajax_test.html @@ -0,0 +1,42 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>Prototype Unit test file | Ajax</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script type="text/javascript" charset="utf-8"> + var eventResults = {}; + var originalElement = window.Element; + </script> + <script src="../../../dist/prototype.js" type="text/javascript"></script> + <script src="../../lib/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../../test.css" type="text/css" /> + + + + + <script src="../fixtures/ajax.js" type="text/javascript" charset="utf-8"></script> + + + <script src="../ajax_test.js" type="text/javascript"></script> +</head> +<body> +<h1>Prototype Unit test file</h1> +<h2>Ajax</h2> + +<!-- This file is programmatically generated. Do not attempt to modify it. Instead, modify --> + +<!-- Log output start --> +<div id="testlog"></div> +<!-- Log output end --> + +<!-- HTML Fixtures start --> +<div id="content"></div> +<div id="content2" style="color:red"></div> + +<!-- HTML Fixtures end --> +</body> +</html> +<script type="text/javascript" charset="utf-8"> + eventResults.endOfDocument = true; +</script> diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/tmp/array_test.html b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/array_test.html new file mode 100644 index 0000000000..cc3b9ef629 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/array_test.html @@ -0,0 +1,39 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>Prototype Unit test file | Array</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script type="text/javascript" charset="utf-8"> + var eventResults = {}; + var originalElement = window.Element; + </script> + <script src="../../../dist/prototype.js" type="text/javascript"></script> + <script src="../../lib/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../../test.css" type="text/css" /> + + + + + + <script src="../array_test.js" type="text/javascript"></script> +</head> +<body> +<h1>Prototype Unit test file</h1> +<h2>Array</h2> + +<!-- This file is programmatically generated. Do not attempt to modify it. Instead, modify --> + +<!-- Log output start --> +<div id="testlog"></div> +<!-- Log output end --> + +<!-- HTML Fixtures start --> +<div id="test_node">22<span id="span_1"></span><span id="span_2"></span></div> + +<!-- HTML Fixtures end --> +</body> +</html> +<script type="text/javascript" charset="utf-8"> + eventResults.endOfDocument = true; +</script> diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/tmp/base_test.html b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/base_test.html new file mode 100644 index 0000000000..bcd29f91df --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/base_test.html @@ -0,0 +1,45 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>Prototype Unit test file | Base</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script type="text/javascript" charset="utf-8"> + var eventResults = {}; + var originalElement = window.Element; + </script> + <script src="../../../dist/prototype.js" type="text/javascript"></script> + <script src="../../lib/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../../test.css" type="text/css" /> + + + + + <script src="../fixtures/base.js" type="text/javascript" charset="utf-8"></script> + + + <script src="../base_test.js" type="text/javascript"></script> +</head> +<body> +<h1>Prototype Unit test file</h1> +<h2>Base</h2> + +<!-- This file is programmatically generated. Do not attempt to modify it. Instead, modify --> + +<!-- Log output start --> +<div id="testlog"></div> +<!-- Log output end --> + +<!-- HTML Fixtures start --> +<div id="test"></div> +<ul id="list"> + <li></li> + <li></li> + <li></li> +</ul> +<!-- HTML Fixtures end --> +</body> +</html> +<script type="text/javascript" charset="utf-8"> + eventResults.endOfDocument = true; +</script> diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/tmp/dom_test.html b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/dom_test.html new file mode 100644 index 0000000000..c1ffb7536f --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/dom_test.html @@ -0,0 +1,326 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>Prototype Unit test file | Dom</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script type="text/javascript" charset="utf-8"> + var eventResults = {}; + var originalElement = window.Element; + </script> + <script src="../../../dist/prototype.js" type="text/javascript"></script> + <script src="../../lib/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../../test.css" type="text/css" /> + + + <link rel="stylesheet" href="../fixtures/dom.css" type="text/css" charset="utf-8" /> + + + + <script src="../fixtures/dom.js" type="text/javascript" charset="utf-8"></script> + + + <script src="../dom_test.js" type="text/javascript"></script> +</head> +<body> +<h1>Prototype Unit test file</h1> +<h2>Dom</h2> + +<!-- This file is programmatically generated. Do not attempt to modify it. Instead, modify --> + +<!-- Log output start --> +<div id="testlog"></div> +<!-- Log output end --> + +<!-- HTML Fixtures start --> +<div id="scroll_test_1"> + <p id="scroll_test_2">Scroll test</p> +</div> + +<div id="test-visible">visible</div> +<div id="test-hidden" style="display:none;">hidden</div> +<div id="test-toggle-visible">visible</div> +<div id="test-toggle-hidden" style="display:none;">hidden</div> +<div id="test-hide-visible">visible</div> +<div id="test-hide-hidden" style="display:none;">hidden</div> +<div id="test-show-visible">visible</div> +<div id="test-show-hidden" style="display:none;">hidden</div> +<div id="removable-container"><div id="removable"></div></div> + +<div> + <table> + <tbody id="table"> + <tr> + <td>Data</td> + </tr> + <tr> + <td id="a_cell">First Row</td> + </tr> + <tr id="second_row"> + <td>Second Row</td> + </tr> + </tbody> + </table> +</div> + +<div id="table-container-to-replace"> + <table> + <tbody id="table-to-replace"> + <tr> + <td>Data</td> + </tr> + <tr> + <td id="a_cell-to-replace">First Row</td> + </tr> + <tr id="second_row-to-replace"> + <td>Second Row</td> + </tr> + </tbody> + </table> +</div> + +<p class="test">Test paragraph outside of container</p> + +<div id="container"> + <p class="test" id="intended">Test paragraph 1 inside of container</p> + <p class="test">Test paragraph 2 inside of container</p> + <p class="test">Test paragraph 3 inside of container</p> + <p class="test">Test paragraph 4 inside of container</p> +</div> + +<div id="testdiv">to be updated</div> +<div id="testdiv-replace-container-1"><div id="testdiv-replace-1"></div></div> +<div id="testdiv-replace-container-2"><div id="testdiv-replace-2"></div></div> +<div id="testdiv-replace-container-3"><div id="testdiv-replace-3"></div></div> +<div id="testdiv-replace-container-4"><div id="testdiv-replace-4"></div></div> +<div id="testdiv-replace-container-5"><div id="testdiv-replace-5"></div></div> +<div id="testdiv-replace-container-element"><div id="testdiv-replace-element"></div></div> +<div id="testdiv-replace-container-toelement"><div id="testdiv-replace-toelement"></div></div> +<div id="testdiv-replace-container-tohtml"><div id="testdiv-replace-tohtml"></div></div> +<div id="testtable-replace-container"><table id="testtable-replace"></table></div> +<table id="testrow-replace-container"><tr id="testrow-replace"></tr></table> +<select id="testoption-replace-container"><option id="testoption-replace"></option><option>stays</option></select> +<div id="testform-replace-container"><p>some text</p><form id="testform-replace"><input id="testinput-replace" type="text" /></form><p>some text</p></div> + +<div id="element_with_visible_overflow" style="overflow:visible">V</div> +<div id="element_with_hidden_overflow" style="overflow:hidden">H</div> +<div id="element_with_scroll_overflow" style="overflow:scroll">S</div> + +<div id="element_extend_test"> </div> + +<div id="element_reextend_test"><div id="discard_1"></div></div> + +<div id="test_whitespace"> <span> </span> + + +<div><div></div> </div><span> </span> +</div> + + +<div id="nav_tests_isolator"> + <div id="nav_test_first_sibling"></div> + <div></div> + <p id="nav_test_p" class="test"></p> + <span id="nav_test_prev_sibling"></span> + + <ul id="navigation_test" style="display: none"> + <!-- comment node to screw things up --> + <li class="first"><em>A</em></li> + <li><em class="dim">B</em></li> + <li id="navigation_test_c"> + <em>C</em> + <ul> + <li><em class="dim">E</em></li> + <li id="navigation_test_f"><em>F</em></li> + </ul> + </li> + <li class="last"><em>D</em></li> + </ul> + + <div id="navigation_test_next_sibling"> + <!-- --> + </div> + + <p></p> +</div> + +<div id="class_names"> + <p class="A"></p> + <ul class="A B" id="class_names_ul"> + <li class="C"></li> + <li class="A C"></li> + <li class="1"></li> + </ul> + <div class="B C D"></div> + <div id="unextended"></div> +</div> + +<div id="style_test_1" style="display:none;"></div> +<div id="style_test_2" class="style-test" style="font-size:11px;"></div> + +<div id="style_test_3">blah</div> +<span id="style_test_4">blah</span> +<span id="style_test_5">blah</span> + +<div id="style_test_dimensions_container"> + <div id="style_test_dimensions" style="background:#ddd;padding:1px;margin:1px;border:1px solid #00f"><div style="height:5px;background:#eee;width:5px;padding:2px;margin:2px;border:2px solid #0f0"> </div> + </div> +</div> + +<div id="test_csstext_1">test_csstext_1</div> +<div id="test_csstext_2">test_csstext_2</div> +<div id="test_csstext_3" style="border: 1px solid red">test_csstext_3</div> +<div id="test_csstext_4" style="font-size: 20px">test_csstext_4</div> +<div id="test_csstext_5">test_csstext_5</div> + +<div id="custom_attributes"> + <div foo="1" bar="2"></div> + <div foo="2"></div> +</div> + +<div id="cloned_element_attributes_issue" foo="original"></div> +<a id="attributes_with_issues_1" href="test.html" accesskey="L" tabindex="50" title="a link" onclick="alert('hello world');"></a> +<a id="attributes_with_issues_2" href="" accesskey="" tabindex="" title=""></a> +<a id="attributes_with_issues_3"></a> +<form id="attributes_with_issues_form" method="post" action="blah" class="blah-class"> + <input type="hidden" id="id" /> + <input type="hidden" name="id" /> + <input type="checkbox" id="action" /> + <input type="checkbox" name="action" /> + <input type="text" id="method" /> + <input type="hidden" name="class" /> + <input type="checkbox" id="attributes_with_issues_checked" name="a" checked="checked"/> + <input type="checkbox" id="attributes_with_issues_disabled" name="b" checked="checked" disabled="disabled"/> + <input type="text" id="attributes_with_issues_readonly" name="c" readonly="readonly" value="blech"/> + <input type="text" id="attributes_with_issues_regular" name="d" value="0"/> + <input type="date" id="attributes_with_issues_type" value="blech" /> + <select id="attributes_with_issues_multiple" name="e" multiple="multiple"> + <option>blech</option> + <option>blah</option> + </select> +</form> + +<!-- writeAttributes --> +<p id="write_attribute_para"></p> +<a id="write_attribute_link" href="test.html"></a> +<form action="/dev/null" id="write_attribute_form" method="get" accept-charset="utf-8"> + <label id="write_attribute_label"></label> + <input type="checkbox" name="write_attribute_checkbox" value="" id="write_attribute_checkbox"> + <input type="checkbox" checked="checked" name="write_attribute_checked_checkbox" value="" id="write_attribute_checked_checkbox"> + <input type="text" name="write_attribute_input" value="" id="write_attribute_input"> + <select id="write_attribute_select"> + <option>Cat</option> + <option>Dog</option> + </select> +</form> + +<table id="write_attribute_table" cellpadding="6" cellspacing="4"> + <tr><td id="write_attribute_td">A</td><td>B</td></tr> + <tr><td>C</td></tr> + <tr><td>D</td><td>E</td><td>F</td></tr> +</table> + +<div id="dom_attribute_precedence"> + <form action="blech" method="post"> + <input type="submit" id="update" /> + </form> +</div> + +<div id="not_floating_none">NFN</div> +<div id="not_floating_inline" style="float:none">NFI</div> +<div id="not_floating_style">NFS</div> + +<div id="floating_inline" style="float:left">FI</div> +<div id="floating_style">FS</div> +<!-- Test Element opacity functions --> +<img id="op1" alt="op2" src="fixtures/logo.gif" style="opacity:0.5;filter:alpha(opacity=50)" /> +<img id="op2" alt="op2" src="fixtures/logo.gif"/> +<img id="op3" alt="op3" src="fixtures/logo.gif"/> +<img id="op4-ie" alt="op3" src="fixtures/logo.gif" style="filter:alpha(opacity=30)" /> +<div id="dimensions-visible"></div> +<div id="dimensions-display-none"></div> +<div id="dimensions-visible-pos-rel"></div> +<div id="dimensions-display-none-pos-rel"></div> +<div id="dimensions-visible-pos-abs"></div> +<div id="dimensions-display-none-pos-abs"></div> +<table border="0" cellspacing="0" cellpadding="0" id="dimensions-table"> + <tbody id="dimensions-tbody"> + <tr id="dimensions-tr"> + <td id="dimensions-td">Data</td> + </tr> + </tbody> +</table> + +<div id="dimensions-nester" style="width: 500px;"> + <div id="dimensions-nestee" style="display: none">This is a nested DIV. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div> +</div> + + +<p id="test-empty"></p> +<p id="test-empty-but-contains-whitespace"> + + +</p> +<p id="test-full">content</p> +<div id="ancestor"> + <div id="child"> + <div id="grand-child"> + <div id="great-grand-child"></div> + </div></div><!-- intentional formatting; don't change this line --> + <div id="sibling"><div id="grand-sibling"></div></div> +</div> +<div id="not-in-the-family"></div> + +<div id="insertions-container"><div id="insertions-main"><p>some content.</p></div></div> +<div id="insertions-node-container"><div id="insertions-node-main"><p>some content.</p></div></div> +<div id="element-insertions-container"><div id="element-insertions-main"><p>some content.</p></div></div> +<div id="element-insertions-multiple-container"><div id="element-insertions-multiple-main"><p>some content.</p></div></div> +<table id="table_for_insertions"></table> +<table id="table_for_row_insertions"><tr id="row_1"></tr></table> +<form method="post" action="blah"> + <select id="select_for_update" name="select_for_update"> + <option>option 1</option> + <option>option 2</option> + </select> + <select id="select_for_insert_bottom" name="select_for_insert_bottom"> + <option>option 1</option> + <option>option 2</option> + </select> + <select id="select_for_insert_top" name="select_for_insert_top"> + <option>option 1</option> + <option>option 2</option> + </select> +</form> +<div id="wrap-container"><p id="wrap"></p></div> + +<!-- Positioning methods bench --> +<div id="body_absolute" style="position: absolute; top: 10px; left: 10px"> + <div id="absolute_absolute" style="position: absolute; top: 10px; left:10px"> </div> + <div id="absolute_relative" style="position: relative; top: 10px; left:10px"> + <div style="height:10px;font-size:2px">test<span id="inline">test</span></div> + <div id="absolute_relative_undefined">XYZ</div> + </div> + <div id="absolute_fixed" style="position: fixed; top: 10px; left: 10px"> + <span id="absolute_fixed_absolute" style="position: absolute; top: 10px; left: 10px">foo</span> + <span id="absolute_fixed_undefined" style="display:block">bar</span> + </div></div> +<div id="notInlineAbsoluted"></div> +<div id="inlineAbsoluted" style="position: absolute"></div> + +<div id="unextended"></div> +<div id="identification"> + <div id="predefined_id"></div> + <div></div> + <div></div> + <div></div> + <div id="anonymous_element_3"></div> +</div> + +<div id='elementToViewportDimensions' style='display: none'></div> +<div id="auto_dimensions" style="height:auto"></div> +<!-- HTML Fixtures end --> +</body> +</html> +<script type="text/javascript" charset="utf-8"> + eventResults.endOfDocument = true; +</script> diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/tmp/element_mixins_test.html b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/element_mixins_test.html new file mode 100644 index 0000000000..51a789e79b --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/element_mixins_test.html @@ -0,0 +1,43 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>Prototype Unit test file | Element mixins</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script type="text/javascript" charset="utf-8"> + var eventResults = {}; + var originalElement = window.Element; + </script> + <script src="../../../dist/prototype.js" type="text/javascript"></script> + <script src="../../lib/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../../test.css" type="text/css" /> + + + + + <script src="../fixtures/element_mixins.js" type="text/javascript" charset="utf-8"></script> + + + <script src="../element_mixins_test.js" type="text/javascript"></script> +</head> +<body> +<h1>Prototype Unit test file</h1> +<h2>Element mixins</h2> + +<!-- This file is programmatically generated. Do not attempt to modify it. Instead, modify --> + +<!-- Log output start --> +<div id="testlog"></div> +<!-- Log output end --> + +<!-- HTML Fixtures start --> +<form id="form"> + <input type="text" id="input" value="4" /> + <input type="submit" /> +</form> +<!-- HTML Fixtures end --> +</body> +</html> +<script type="text/javascript" charset="utf-8"> + eventResults.endOfDocument = true; +</script> diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/tmp/enumerable_test.html b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/enumerable_test.html new file mode 100644 index 0000000000..8f462b6395 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/enumerable_test.html @@ -0,0 +1,48 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>Prototype Unit test file | Enumerable</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script type="text/javascript" charset="utf-8"> + var eventResults = {}; + var originalElement = window.Element; + </script> + <script src="../../../dist/prototype.js" type="text/javascript"></script> + <script src="../../lib/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../../test.css" type="text/css" /> + + + + + <script src="../fixtures/enumerable.js" type="text/javascript" charset="utf-8"></script> + + + <script src="../enumerable_test.js" type="text/javascript"></script> +</head> +<body> +<h1>Prototype Unit test file</h1> +<h2>Enumerable</h2> + +<!-- This file is programmatically generated. Do not attempt to modify it. Instead, modify --> + +<!-- Log output start --> +<div id="testlog"></div> +<!-- Log output end --> + +<!-- HTML Fixtures start --> +<table id="grepTable"> +<tbody id="grepTBody"> + <tr id="grepRow"> + <th id="grepHeader" class="cell"></th> + <td id="grepCell" class="cell"></td> + </tr> +</tbody> +</table> + +<!-- HTML Fixtures end --> +</body> +</html> +<script type="text/javascript" charset="utf-8"> + eventResults.endOfDocument = true; +</script> diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/tmp/event_test.html b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/event_test.html new file mode 100644 index 0000000000..12accf239f --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/event_test.html @@ -0,0 +1,42 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>Prototype Unit test file | Event</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script type="text/javascript" charset="utf-8"> + var eventResults = {}; + var originalElement = window.Element; + </script> + <script src="../../../dist/prototype.js" type="text/javascript"></script> + <script src="../../lib/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../../test.css" type="text/css" /> + + + + + + <script src="../event_test.js" type="text/javascript"></script> +</head> +<body> +<h1>Prototype Unit test file</h1> +<h2>Event</h2> + +<!-- This file is programmatically generated. Do not attempt to modify it. Instead, modify --> + +<!-- Log output start --> +<div id="testlog"></div> +<!-- Log output end --> + +<!-- HTML Fixtures start --> +<div id="outer" style="display: none"> + <p id="inner">One two three <span id="span">four</span></p> +</div> +<div id="container"><div></div></div> + +<!-- HTML Fixtures end --> +</body> +</html> +<script type="text/javascript" charset="utf-8"> + eventResults.endOfDocument = true; +</script> diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/tmp/form_test.html b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/form_test.html new file mode 100644 index 0000000000..14349eb511 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/form_test.html @@ -0,0 +1,150 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>Prototype Unit test file | Form</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script type="text/javascript" charset="utf-8"> + var eventResults = {}; + var originalElement = window.Element; + </script> + <script src="../../../dist/prototype.js" type="text/javascript"></script> + <script src="../../lib/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../../test.css" type="text/css" /> + + + + + + <script src="../form_test.js" type="text/javascript"></script> +</head> +<body> +<h1>Prototype Unit test file</h1> +<h2>Form</h2> + +<!-- This file is programmatically generated. Do not attempt to modify it. Instead, modify --> + +<!-- Log output start --> +<div id="testlog"></div> +<!-- Log output end --> + +<!-- HTML Fixtures start --> +<form id="form" method="get" action="fixtures/empty.js"> + <input type="text" name="val1" id="input_enabled" value="4" /> + <div>This is not a form element</div> + <input type="text" name="val2" id="input_disabled" disabled="disabled" value="5" /> + <input type="submit" name="first_submit" value="Commit it!" /> + <input type="submit" name="second_submit" value="Delete it!" /> + <input type="text" name="action" value="blah" /> +</form> + +<form id="bigform" method="get" action="fixtures/empty.js"> + <div id="inputs"> + <input type="text" name="dummy" id="dummy_disabled" disabled="disabled"/> + <input type="submit" name="commit" id="submit" /> + <input type="button" name="clicky" value="click me" /> + <input type="reset" name="revert" /> + <input type="text" name="greeting" id="focus_text" value="Hello" /> + </div> + + <div id="buttons"> + <button type="button" id="button_type_button" name="button">Click Me</button> + </div>div> + + <!-- some edge cases in serialization --> + <div id="value_checks"> + <input name="twin" type="text" value="" /> + <input name="twin" type="text" value="siamese" /> + <!-- Rails checkbox hack with hidden input: --> + <input name="checky" type="checkbox" id="checkbox_hack" value="1" /> + <input name="checky" type="hidden" value="0" /> + </div> + + <!-- all variations of SELECT controls --> + <div id="selects_wrapper"> + <select name="vu"> + <option value="1" selected="selected">One</option> + <option value="2">Two</option> + <option value="3">Three</option> + </select> + <select id="multiSel1" name="vm[]" multiple="multiple"> + <option id="multiSel1_opt1" value="1" selected="selected">One</option> + <option id="multiSel1_opt2" value="2">Two</option> + <option id="multiSel1_opt3" value="3" selected="selected">Three</option> + </select> + <select name="nvu"> + <option selected="selected">One</option> + <option value="2">Two</option> + <option value="3">Three</option> + </select> + + <fieldset id="selects_fieldset"> + <select name="nvm[]" multiple="multiple"> + <option selected="selected">One</option> + <option>Two</option> + <option selected="selected">Three</option> + </select> + <select name="evu"> + <option value="" selected="selected">One</option> + <option value="2">Two</option> + <option value="3">Three</option> + </select> + <select name="evm[]" multiple="multiple"> + <option value="" selected="selected">One</option> + <option>Two</option> + <option selected="selected">Three</option> + </select> + </fieldset> + </div> + + <div id="various"> + <select name="tf_selectOne"><option selected="selected"></option><option>1</option></select> + <textarea name="tf_textarea"></textarea> + <input type="checkbox" name="tf_checkbox" value="on" /> + <select name="tf_selectMany" multiple="multiple"></select> + <input type="text" name="tf_text" /> + <div>This is not a form element</div> + <input type="radio" name="tf_radio" value="on" /> + <input type="hidden" name="tf_hidden" /> + <input type="password" name="tf_password" /> + </div> +</form> + +<form id="form_focus_hidden" style="display: none"> + <input type="text" /> +</form> + +<form id="form_with_file_input"> + <input type="file" name="file_name" value="foo" /> +</form> + +<!-- tabindexed forms --> +<div id="tabindex"> + <form id="ffe"> + <p><input type="text" disabled="disabled" id="ffe_disabled" /></p> + <input type="hidden" id="ffe_hidden" /> + <input type="checkbox" id="ffe_checkbox" /> + </form> + + <form id="ffe_ti"> + <p><input type="text" disabled="disabled" id="ffe_ti_disabled" /></p> + <input type="hidden" id="ffe_ti_hidden" /> + <input type="checkbox" id="ffe_ti_checkbox" /> + <input type="submit" id="ffe_ti_submit" tabindex="1" /> + </form> + + <form id="ffe_ti2"> + <p><input type="text" disabled="disabled" id="ffe_ti2_disabled" /></p> + <input type="hidden" id="ffe_ti2_hidden" /> + <input type="checkbox" id="ffe_ti2_checkbox" tabindex="0" /> + <input type="submit" id="ffe_ti2_submit" tabindex="1" /> + </form> + +</div> + +<!-- HTML Fixtures end --> +</body> +</html> +<script type="text/javascript" charset="utf-8"> + eventResults.endOfDocument = true; +</script> diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/tmp/hash_test.html b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/hash_test.html new file mode 100644 index 0000000000..4e4f0be75d --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/hash_test.html @@ -0,0 +1,40 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>Prototype Unit test file | Hash</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script type="text/javascript" charset="utf-8"> + var eventResults = {}; + var originalElement = window.Element; + </script> + <script src="../../../dist/prototype.js" type="text/javascript"></script> + <script src="../../lib/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../../test.css" type="text/css" /> + + + + + <script src="../fixtures/hash.js" type="text/javascript" charset="utf-8"></script> + + + <script src="../hash_test.js" type="text/javascript"></script> +</head> +<body> +<h1>Prototype Unit test file</h1> +<h2>Hash</h2> + +<!-- This file is programmatically generated. Do not attempt to modify it. Instead, modify --> + +<!-- Log output start --> +<div id="testlog"></div> +<!-- Log output end --> + +<!-- HTML Fixtures start --> + +<!-- HTML Fixtures end --> +</body> +</html> +<script type="text/javascript" charset="utf-8"> + eventResults.endOfDocument = true; +</script> diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/tmp/number_test.html b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/number_test.html new file mode 100644 index 0000000000..85eed3191b --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/number_test.html @@ -0,0 +1,38 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>Prototype Unit test file | Number</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script type="text/javascript" charset="utf-8"> + var eventResults = {}; + var originalElement = window.Element; + </script> + <script src="../../../dist/prototype.js" type="text/javascript"></script> + <script src="../../lib/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../../test.css" type="text/css" /> + + + + + + <script src="../number_test.js" type="text/javascript"></script> +</head> +<body> +<h1>Prototype Unit test file</h1> +<h2>Number</h2> + +<!-- This file is programmatically generated. Do not attempt to modify it. Instead, modify --> + +<!-- Log output start --> +<div id="testlog"></div> +<!-- Log output end --> + +<!-- HTML Fixtures start --> + +<!-- HTML Fixtures end --> +</body> +</html> +<script type="text/javascript" charset="utf-8"> + eventResults.endOfDocument = true; +</script> diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/tmp/position_test.html b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/position_test.html new file mode 100644 index 0000000000..78ed551d0e --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/position_test.html @@ -0,0 +1,47 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>Prototype Unit test file | Position</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script type="text/javascript" charset="utf-8"> + var eventResults = {}; + var originalElement = window.Element; + </script> + <script src="../../../dist/prototype.js" type="text/javascript"></script> + <script src="../../lib/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../../test.css" type="text/css" /> + + + + + + <script src="../position_test.js" type="text/javascript"></script> +</head> +<body> +<h1>Prototype Unit test file</h1> +<h2>Position</h2> + +<!-- This file is programmatically generated. Do not attempt to modify it. Instead, modify --> + +<!-- Log output start --> +<div id="testlog"></div> +<!-- Log output end --> + +<!-- HTML Fixtures start --> +<div id="ensure_scrollbars" style="width:10000px; height:10000px; position:absolute" > </div> + +<div id="body_absolute" style="position: absolute; top: 10px; left: 10px"> + <div id="absolute_absolute" style="position: absolute; top: 10px; left:10px"> </div> + <div id="absolute_relative" style="position: relative; top: 10px; left:10px"> + <div style="height:10px;font-size:2px">test<span id="inline">test</span></div> + <div id="absolute_relative_undefined"> </div> + </div> +</div> + +<!-- HTML Fixtures end --> +</body> +</html> +<script type="text/javascript" charset="utf-8"> + eventResults.endOfDocument = true; +</script> diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/tmp/range_test.html b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/range_test.html new file mode 100644 index 0000000000..cbc6f828b6 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/range_test.html @@ -0,0 +1,38 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>Prototype Unit test file | Range</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script type="text/javascript" charset="utf-8"> + var eventResults = {}; + var originalElement = window.Element; + </script> + <script src="../../../dist/prototype.js" type="text/javascript"></script> + <script src="../../lib/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../../test.css" type="text/css" /> + + + + + + <script src="../range_test.js" type="text/javascript"></script> +</head> +<body> +<h1>Prototype Unit test file</h1> +<h2>Range</h2> + +<!-- This file is programmatically generated. Do not attempt to modify it. Instead, modify --> + +<!-- Log output start --> +<div id="testlog"></div> +<!-- Log output end --> + +<!-- HTML Fixtures start --> + +<!-- HTML Fixtures end --> +</body> +</html> +<script type="text/javascript" charset="utf-8"> + eventResults.endOfDocument = true; +</script> diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/tmp/selector_test.html b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/selector_test.html new file mode 100644 index 0000000000..84bde55e50 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/selector_test.html @@ -0,0 +1,107 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>Prototype Unit test file | Selector</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script type="text/javascript" charset="utf-8"> + var eventResults = {}; + var originalElement = window.Element; + </script> + <script src="../../../dist/prototype.js" type="text/javascript"></script> + <script src="../../lib/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../../test.css" type="text/css" /> + + + + + + <script src="../selector_test.js" type="text/javascript"></script> +</head> +<body> +<h1>Prototype Unit test file</h1> +<h2>Selector</h2> + +<!-- This file is programmatically generated. Do not attempt to modify it. Instead, modify --> + +<!-- Log output start --> +<div id="testlog"></div> +<!-- Log output end --> + +<!-- HTML Fixtures start --> +<div id="fixtures" style="display: none"> + <h1 class="title">Some title <span>here</span></h1> + <p id="p" class="first summary"> + <strong id="strong">This</strong> is a short blurb + <a id="link_1" class="first internal" rel="external nofollow" href="#">with a <em id="em2">link</em></a> or + <a id="link_2" class="internal highlight" href="#"><em id="em">two</em></a>. + Or <cite id="with_title" title="hello world!">a citation</cite>. + </p> + <ul id="list"> + <li id="item_1" class="first"><a id="link_3" href="#" class="external"><span id="span">Another link</span></a></li> + <li id="item_2">Some text</li> + <li id="item_3" xml:lang="es-us" class="">Otra cosa</li> + </ul> + + <!-- this form has a field with the name 'id', + therefore its ID property won't be 'troubleForm': --> + <form id="troubleForm"> + <input type="hidden" name="id" id="hidden" /> + <input type="text" name="disabled_text_field" id="disabled_text_field" disabled="disabled" /> + <input type="text" name="enabled_text_field" id="enabled_text_field" /> + <input type="checkbox" name="checkboxes" id="checked_box" checked="checked" value="Checked" /> + <input type="checkbox" name="checkboxes" id="unchecked_box" value="Unchecked"/> + <input type="radio" name="radiobuttons" id="checked_radio" checked="checked" value="Checked" /> + <input type="radio" name="radiobuttons" id="unchecked_radio" value="Unchecked" /> + </form> + + <form id="troubleForm2"> + <input type="checkbox" name="brackets[5][]" id="chk_1" checked="checked" value="1" /> + <input type="checkbox" name="brackets[5][]" id="chk_2" value="2" /> + </form> + + <div id="level1"> + <span id="level2_1"> + <span id="level3_1"></span> + <!-- This comment should be ignored by the adjacent selector --> + <span id="level3_2"></span> + </span> + <span id="level2_2"> + <em id="level_only_child"> + </em> + </span> + <div id="level2_3"></div> + </div> <!-- #level1 --> + + <div id="dupContainer"> + <span id="dupL1" class="span_foo span_bar"> + <span id="dupL2"> + <span id="dupL3"> + <span id="dupL4"> + <span id="dupL5"></span> + </span> + </span> + </span> + </span> + </div> <!-- #dupContainer --> + + <div id="grandfather"> grandfather + <div id="father" class="brothers men"> father + <div id="son"> son </div> + </div> + <div id="uncle" class="brothers men"> uncle </div> + </div> + + <form id="commaParent" title="commas,are,good"> + <input type="hidden" id="commaChild" name="foo" value="#commaOne,#commaTwo" /> + <input type="hidden" id="commaTwo" name="foo2" value="oops" /> + </form> + <div id="counted_container"><div class="is_counted"></div></div> +</div> + +<!-- HTML Fixtures end --> +</body> +</html> +<script type="text/javascript" charset="utf-8"> + eventResults.endOfDocument = true; +</script> diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/tmp/string_test.html b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/string_test.html new file mode 100644 index 0000000000..74564fc0ce --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/string_test.html @@ -0,0 +1,40 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>Prototype Unit test file | String</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script type="text/javascript" charset="utf-8"> + var eventResults = {}; + var originalElement = window.Element; + </script> + <script src="../../../dist/prototype.js" type="text/javascript"></script> + <script src="../../lib/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../../test.css" type="text/css" /> + + + + + <script src="../fixtures/string.js" type="text/javascript" charset="utf-8"></script> + + + <script src="../string_test.js" type="text/javascript"></script> +</head> +<body> +<h1>Prototype Unit test file</h1> +<h2>String</h2> + +<!-- This file is programmatically generated. Do not attempt to modify it. Instead, modify --> + +<!-- Log output start --> +<div id="testlog"></div> +<!-- Log output end --> + +<!-- HTML Fixtures start --> + +<!-- HTML Fixtures end --> +</body> +</html> +<script type="text/javascript" charset="utf-8"> + eventResults.endOfDocument = true; +</script> diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/tmp/unit_test.html b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/unit_test.html new file mode 100644 index 0000000000..b684225ba5 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/tmp/unit_test.html @@ -0,0 +1,56 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>Prototype Unit test file | Unittest</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script type="text/javascript" charset="utf-8"> + var eventResults = {}; + var originalElement = window.Element; + </script> + <script src="../../../dist/prototype.js" type="text/javascript"></script> + <script src="../../lib/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../../test.css" type="text/css" /> + + + + + + <script src="../unittest_test.js" type="text/javascript"></script> +</head> +<body> +<h1>Prototype Unit test file</h1> +<h2>Unittest</h2> + +<!-- This file is programmatically generated. Do not attempt to modify it. Instead, modify --> + +<!-- Log output start --> +<div id="testlog"></div> +<!-- Log output end --> + +<!-- HTML Fixtures start --> +<div id="testlog_2"> </div> + +<!-- Test elements follow --> +<div id="test_1" class="a bbbbbbbbbbbb cccccccccc dddd"> </div> + +<div id="test_2"> <span> </span> + + + +<div><div></div> </div><span> </span> +</div> + +<ul id="tlist"><li id="tlist_1">x1</li><li id="tlist_2">x2</li></ul> +<ul id="tlist2"><li class="a" id="tlist2_1">x1</li><li id="tlist2_2">x2</li></ul> + +<div id="testmoveby" style="background-color:#333;width:100px;">XXXX</div> + +<div id="testcss1">testcss1<span id="testcss1_span" style="display:none;">blah</span></div><div id="testcss2">testcss1</div> + +<!-- HTML Fixtures end --> +</body> +</html> +<script type="text/javascript" charset="utf-8"> + eventResults.endOfDocument = true; +</script> diff --git a/dom/tests/mochitest/ajax/prototype/test/unit/unittest_test.js b/dom/tests/mochitest/ajax/prototype/test/unit/unittest_test.js new file mode 100644 index 0000000000..e7afdd60bf --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test/unit/unittest_test.js @@ -0,0 +1,143 @@ +var testObj = { + isNice: function() { + return true; + }, + isBroken: function() { + return false; + } +} + +new Test.Unit.Runner({ + + testBuildMessage: function() { + this.assertEqual("'foo' 'bar'", this.buildMessage('', '? ?', 'foo', 'bar')) + }, + + testAssertEqual: function() { + this.assertEqual(0, 0); + this.assertEqual(0, 0, "test"); + + this.assertEqual(0,'0'); + this.assertEqual(65.0, 65); + + this.assertEqual("a", "a"); + this.assertEqual("a", "a", "test"); + + this.assertNotEqual(0, 1); + this.assertNotEqual("a","b"); + this.assertNotEqual({},{}); + this.assertNotEqual([],[]); + this.assertNotEqual([],{}); + }, + + testAssertEnumEqual: function() { + this.assertEnumEqual([], []); + this.assertEnumEqual(['a', 'b'], ['a', 'b']); + this.assertEnumEqual(['1', '2'], [1, 2]); + this.assertEnumNotEqual(['1', '2'], [1, 2, 3]); + }, + + testAssertHashEqual: function() { + this.assertHashEqual({}, {}); + this.assertHashEqual({a:'b'}, {a:'b'}); + this.assertHashEqual({a:'b', c:'d'}, {c:'d', a:'b'}); + this.assertHashNotEqual({a:'b', c:'d'}, {c:'d', a:'boo!'}); + }, + + testAssertRespondsTo: function() { + this.assertRespondsTo('isNice', testObj); + this.assertRespondsTo('isBroken', testObj); + }, + + testAssertIdentical: function() { + this.assertIdentical(0, 0); + this.assertIdentical(0, 0, "test"); + this.assertIdentical(1, 1); + this.assertIdentical('a', 'a'); + this.assertIdentical('a', 'a', "test"); + this.assertIdentical('', ''); + this.assertIdentical(undefined, undefined); + this.assertIdentical(null, null); + this.assertIdentical(true, true); + this.assertIdentical(false, false); + + var obj = {a:'b'}; + this.assertIdentical(obj, obj); + + this.assertNotIdentical({1:2,3:4},{1:2,3:4}); + + this.assertIdentical(1, 1.0); // both are typeof == 'number' + + this.assertNotIdentical(1, '1'); + this.assertNotIdentical(1, '1.0'); + }, + + testAssertNullAndAssertUndefined: function() { + this.assertNull(null); + this.assertNotNull(undefined); + this.assertNotNull(0); + this.assertNotNull(''); + this.assertNotUndefined(null); + this.assertUndefined(undefined); + this.assertNotUndefined(0); + this.assertNotUndefined(''); + this.assertNullOrUndefined(null); + this.assertNullOrUndefined(undefined); + this.assertNotNullOrUndefined(0); + this.assertNotNullOrUndefined(''); + }, + + testAssertMatch: function() { + this.assertMatch(/knowmad.jpg$/, 'http://script.aculo.us/images/knowmad.jpg'); + this.assertMatch(/Fuc/, 'Thomas Fuchs'); + this.assertMatch(/^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$/, '$19.95'); + this.assertMatch(/(\d{3}\) ?)|(\d{3}[- \.])?\d{3}[- \.]\d{4}(\s(x\d+)?){0,1}$/, '704-343-9330'); + this.assertMatch(/^(?:(?:(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(\/|-|\.)(?:0?2\1(?:29)))|(?:(?:(?:1[6-9]|[2-9]\d)?\d{2})(\/|-|\.)(?:(?:(?:0?[13578]|1[02])\2(?:31))|(?:(?:0?[1,3-9]|1[0-2])\2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))\2(?:0?[1-9]|1\d|2[0-8]))))$/, '2001-06-16'); + this.assertMatch(/^((0?[123456789])|(1[012]))\s*:\s*([012345]\d)(\s*:\s*([012345]\d))?\s*[ap]m\s*-\s*((0?[123456789])|(1[012]))\s*:\s*([012345]\d)(\s*:\s*([012345]\d))?\s*[ap]m$/i, '2:00PM-2:15PM'); + this.assertNoMatch(/zubar/, 'foo bar'); + }, + + testAssertInstanceOf: function() { + this.assertInstanceOf(String, new String); + this.assertInstanceOf(RegExp, /foo/); + this.assertNotInstanceOf(String, {}); + }, + + testAssertVisible: function() { + this.assertVisible('testcss1'); + this.assertNotVisible('testcss1_span'); + //this.assertNotVisible('testcss2', "Due to a Safari bug, this test fails in Safari."); + + Element.hide('testcss1'); + this.assertNotVisible('testcss1'); + this.assertNotVisible('testcss1_span'); + Element.show('testcss1'); + this.assertVisible('testcss1'); + this.assertNotVisible('testcss1_span'); + + Element.show('testcss1_span'); + this.assertVisible('testcss1_span'); + Element.hide('testcss1'); + this.assertNotVisible('testcss1_span'); // hidden by parent + }, + + testAssertElementsMatch: function() { + this.assertElementsMatch($$('#tlist'), '#tlist'); + this.assertElementMatches($('tlist'), '#tlist'); + } +}); + +/* This test was disabled in bug 486256, because we don't support having two + * Runners in one file. + */ +/* +new Test.Unit.Runner({ + testDummy: function() { + this.assert(true); + }, + + testMultipleTestRunner: function() { + this.assertEqual('passed', $('testlog_2').down('td', 1).innerHTML); + } +}, {testLog: 'testlog_2'}); +*/ diff --git a/dom/tests/mochitest/ajax/prototype/test_Prototype.html b/dom/tests/mochitest/ajax/prototype/test_Prototype.html new file mode 100644 index 0000000000..a650949955 --- /dev/null +++ b/dom/tests/mochitest/ajax/prototype/test_Prototype.html @@ -0,0 +1,15 @@ +<!DOCTYPE HTML> +<html> +<head> + <title>Test for Prototype</title> + <script src="/MochiKit/Base.js"></script> + <script src="/MochiKit/Async.js"></script> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="../lib/AJAX_setup.js"></script> + <link rel="stylesheet" type="text/css" href="../lib/test.css" /> +</head> +<body> + <iframe width="100%" height="500" id="testframe" src=""></iframe> +</body> +</html> + diff --git a/dom/tests/mochitest/ajax/scriptaculous/lib/prototype.js b/dom/tests/mochitest/ajax/scriptaculous/lib/prototype.js new file mode 100644 index 0000000000..1beb8a89ab --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/lib/prototype.js @@ -0,0 +1,3271 @@ +/* Prototype JavaScript framework, version 1.5.1_rc4 + * (c) 2005-2007 Sam Stephenson + * + * Prototype is freely distributable under the terms of an MIT-style license. + * For details, see the Prototype web site: http://www.prototypejs.org/ + * +/*--------------------------------------------------------------------------*/ + +var Prototype = { + Version: '1.5.1_rc4', + + Browser: { + IE: !!(window.attachEvent && !window.opera), + Opera: !!window.opera, + WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1, + Gecko: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1 + }, + + BrowserFeatures: { + XPath: !!document.evaluate, + ElementExtensions: !!window.HTMLElement, + SpecificElementExtensions: + (document.createElement('div').__proto__ !== + document.createElement('form').__proto__) + }, + + ScriptFragment: '<script[^>]*>([\u0001-\uFFFF]*?)</script>', + JSONFilter: /^\/\*-secure-\s*(.*)\s*\*\/\s*$/, + + emptyFunction: function() { }, + K: function(x) { return x } +} + +var Class = { + create: function() { + return function() { + this.initialize.apply(this, arguments); + } + } +} + +var Abstract = new Object(); + +Object.extend = function(destination, source) { + for (var property in source) { + destination[property] = source[property]; + } + return destination; +} + +Object.extend(Object, { + inspect: function(object) { + try { + if (object === undefined) return 'undefined'; + if (object === null) return 'null'; + return object.inspect ? object.inspect() : object.toString(); + } catch (e) { + if (e instanceof RangeError) return '...'; + throw e; + } + }, + + toJSON: function(object) { + var type = typeof object; + switch(type) { + case 'undefined': + case 'function': + case 'unknown': return; + case 'boolean': return object.toString(); + } + if (object === null) return 'null'; + if (object.toJSON) return object.toJSON(); + if (object.ownerDocument === document) return; + var results = []; + for (var property in object) { + var value = Object.toJSON(object[property]); + if (value !== undefined) + results.push(property.toJSON() + ': ' + value); + } + return '{' + results.join(', ') + '}'; + }, + + keys: function(object) { + var keys = []; + for (var property in object) + keys.push(property); + return keys; + }, + + values: function(object) { + var values = []; + for (var property in object) + values.push(object[property]); + return values; + }, + + clone: function(object) { + return Object.extend({}, object); + } +}); + +Function.prototype.bind = function() { + var __method = this, args = $A(arguments), object = args.shift(); + return function() { + return __method.apply(object, args.concat($A(arguments))); + } +} + +Function.prototype.bindAsEventListener = function(object) { + var __method = this, args = $A(arguments), object = args.shift(); + return function(event) { + return __method.apply(object, [( event || window.event)].concat(args).concat($A(arguments))); + } +} + +Object.extend(Number.prototype, { + toColorPart: function() { + return this.toPaddedString(2, 16); + }, + + succ: function() { + return this + 1; + }, + + times: function(iterator) { + $R(0, this, true).each(iterator); + return this; + }, + + toPaddedString: function(length, radix) { + var string = this.toString(radix || 10); + return '0'.times(length - string.length) + string; + }, + + toJSON: function() { + return isFinite(this) ? this.toString() : 'null'; + } +}); + +Date.prototype.toJSON = function() { + return '"' + this.getFullYear() + '-' + + (this.getMonth() + 1).toPaddedString(2) + '-' + + this.getDate().toPaddedString(2) + 'T' + + this.getHours().toPaddedString(2) + ':' + + this.getMinutes().toPaddedString(2) + ':' + + this.getSeconds().toPaddedString(2) + '"'; +}; + +var Try = { + these: function() { + var returnValue; + + for (var i = 0, length = arguments.length; i < length; i++) { + var lambda = arguments[i]; + try { + returnValue = lambda(); + break; + } catch (e) {} + } + + return returnValue; + } +} + +/*--------------------------------------------------------------------------*/ + +var PeriodicalExecuter = Class.create(); +PeriodicalExecuter.prototype = { + initialize: function(callback, frequency) { + this.callback = callback; + this.frequency = frequency; + this.currentlyExecuting = false; + + this.registerCallback(); + }, + + registerCallback: function() { + this.timer = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000); + }, + + stop: function() { + if (!this.timer) return; + clearInterval(this.timer); + this.timer = null; + }, + + onTimerEvent: function() { + if (!this.currentlyExecuting) { + try { + this.currentlyExecuting = true; + this.callback(this); + } finally { + this.currentlyExecuting = false; + } + } + } +} +Object.extend(String, { + interpret: function(value) { + return value == null ? '' : String(value); + }, + specialChar: { + '\b': '\\b', + '\t': '\\t', + '\n': '\\n', + '\f': '\\f', + '\r': '\\r', + '\\': '\\\\' + } +}); + +Object.extend(String.prototype, { + gsub: function(pattern, replacement) { + var result = '', source = this, match; + replacement = arguments.callee.prepareReplacement(replacement); + + while (source.length > 0) { + if (match = source.match(pattern)) { + result += source.slice(0, match.index); + result += String.interpret(replacement(match)); + source = source.slice(match.index + match[0].length); + } else { + result += source, source = ''; + } + } + return result; + }, + + sub: function(pattern, replacement, count) { + replacement = this.gsub.prepareReplacement(replacement); + count = count === undefined ? 1 : count; + + return this.gsub(pattern, function(match) { + if (--count < 0) return match[0]; + return replacement(match); + }); + }, + + scan: function(pattern, iterator) { + this.gsub(pattern, iterator); + return this; + }, + + truncate: function(length, truncation) { + length = length || 30; + truncation = truncation === undefined ? '...' : truncation; + return this.length > length ? + this.slice(0, length - truncation.length) + truncation : this; + }, + + strip: function() { + return this.replace(/^\s+/, '').replace(/\s+$/, ''); + }, + + stripTags: function() { + return this.replace(/<\/?[^>]+>/gi, ''); + }, + + stripScripts: function() { + return this.replace(new RegExp(Prototype.ScriptFragment, 'img'), ''); + }, + + extractScripts: function() { + var matchAll = new RegExp(Prototype.ScriptFragment, 'img'); + var matchOne = new RegExp(Prototype.ScriptFragment, 'im'); + return (this.match(matchAll) || []).map(function(scriptTag) { + return (scriptTag.match(matchOne) || ['', ''])[1]; + }); + }, + + evalScripts: function() { + return this.extractScripts().map(function(script) { return eval(script) }); + }, + + escapeHTML: function() { + var self = arguments.callee; + self.text.data = this; + return self.div.innerHTML; + }, + + unescapeHTML: function() { + var div = document.createElement('div'); + div.innerHTML = this.stripTags(); + return div.childNodes[0] ? (div.childNodes.length > 1 ? + $A(div.childNodes).inject('', function(memo, node) { return memo+node.nodeValue }) : + div.childNodes[0].nodeValue) : ''; + }, + + toQueryParams: function(separator) { + var match = this.strip().match(/([^?#]*)(#.*)?$/); + if (!match) return {}; + + return match[1].split(separator || '&').inject({}, function(hash, pair) { + if ((pair = pair.split('='))[0]) { + var key = decodeURIComponent(pair.shift()); + var value = pair.length > 1 ? pair.join('=') : pair[0]; + if (value != undefined) value = decodeURIComponent(value); + + if (key in hash) { + if (hash[key].constructor != Array) hash[key] = [hash[key]]; + hash[key].push(value); + } + else hash[key] = value; + } + return hash; + }); + }, + + toArray: function() { + return this.split(''); + }, + + succ: function() { + return this.slice(0, this.length - 1) + + String.fromCharCode(this.charCodeAt(this.length - 1) + 1); + }, + + times: function(count) { + var result = ''; + for (var i = 0; i < count; i++) result += this; + return result; + }, + + camelize: function() { + var parts = this.split('-'), len = parts.length; + if (len == 1) return parts[0]; + + var camelized = this.charAt(0) == '-' + ? parts[0].charAt(0).toUpperCase() + parts[0].substring(1) + : parts[0]; + + for (var i = 1; i < len; i++) + camelized += parts[i].charAt(0).toUpperCase() + parts[i].substring(1); + + return camelized; + }, + + capitalize: function() { + return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase(); + }, + + underscore: function() { + return this.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'#{1}_#{2}').gsub(/([a-z\d])([A-Z])/,'#{1}_#{2}').gsub(/-/,'_').toLowerCase(); + }, + + dasherize: function() { + return this.gsub(/_/,'-'); + }, + + inspect: function(useDoubleQuotes) { + var escapedString = this.gsub(/[\x00-\x1f\\]/, function(match) { + var character = String.specialChar[match[0]]; + return character ? character : '\\u00' + match[0].charCodeAt().toPaddedString(2, 16); + }); + if (useDoubleQuotes) return '"' + escapedString.replace(/"/g, '\\"') + '"'; + return "'" + escapedString.replace(/'/g, '\\\'') + "'"; + }, + + toJSON: function() { + return this.inspect(true); + }, + + unfilterJSON: function(filter) { + return this.sub(filter || Prototype.JSONFilter, '#{1}'); + }, + + evalJSON: function(sanitize) { + var json = this.unfilterJSON(); + try { + if (!sanitize || (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(json))) + return eval('(' + json + ')'); + } catch (e) { } + throw new SyntaxError('Badly formed JSON string: ' + this.inspect()); + }, + + include: function(pattern) { + return this.indexOf(pattern) > -1; + }, + + startsWith: function(pattern) { + return this.indexOf(pattern) === 0; + }, + + endsWith: function(pattern) { + var d = this.length - pattern.length; + return d >= 0 && this.lastIndexOf(pattern) === d; + }, + + empty: function() { + return this == ''; + }, + + blank: function() { + return /^\s*$/.test(this); + } +}); + +if (Prototype.Browser.WebKit || Prototype.Browser.IE) Object.extend(String.prototype, { + escapeHTML: function() { + return this.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>'); + }, + unescapeHTML: function() { + return this.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>'); + } +}); + +String.prototype.gsub.prepareReplacement = function(replacement) { + if (typeof replacement == 'function') return replacement; + var template = new Template(replacement); + return function(match) { return template.evaluate(match) }; +} + +String.prototype.parseQuery = String.prototype.toQueryParams; + +Object.extend(String.prototype.escapeHTML, { + div: document.createElement('div'), + text: document.createTextNode('') +}); + +with (String.prototype.escapeHTML) div.appendChild(text); + +var Template = Class.create(); +Template.Pattern = /(^|.|\r|\n)(#\{(.*?)\})/; +Template.prototype = { + initialize: function(template, pattern) { + this.template = template.toString(); + this.pattern = pattern || Template.Pattern; + }, + + evaluate: function(object) { + return this.template.gsub(this.pattern, function(match) { + var before = match[1]; + if (before == '\\') return match[2]; + return before + String.interpret(object[match[3]]); + }); + } +} + +var $break = {}, $continue = new Error('"throw $continue" is deprecated, use "return" instead'); + +var Enumerable = { + each: function(iterator) { + var index = 0; + try { + this._each(function(value) { + iterator(value, index++); + }); + } catch (e) { + if (e != $break) throw e; + } + return this; + }, + + eachSlice: function(number, iterator) { + var index = -number, slices = [], array = this.toArray(); + while ((index += number) < array.length) + slices.push(array.slice(index, index+number)); + return slices.map(iterator); + }, + + all: function(iterator) { + var result = true; + this.each(function(value, index) { + result = result && !!(iterator || Prototype.K)(value, index); + if (!result) throw $break; + }); + return result; + }, + + any: function(iterator) { + var result = false; + this.each(function(value, index) { + if (result = !!(iterator || Prototype.K)(value, index)) + throw $break; + }); + return result; + }, + + collect: function(iterator) { + var results = []; + this.each(function(value, index) { + results.push((iterator || Prototype.K)(value, index)); + }); + return results; + }, + + detect: function(iterator) { + var result; + this.each(function(value, index) { + if (iterator(value, index)) { + result = value; + throw $break; + } + }); + return result; + }, + + findAll: function(iterator) { + var results = []; + this.each(function(value, index) { + if (iterator(value, index)) + results.push(value); + }); + return results; + }, + + grep: function(pattern, iterator) { + var results = []; + this.each(function(value, index) { + var stringValue = value.toString(); + if (stringValue.match(pattern)) + results.push((iterator || Prototype.K)(value, index)); + }) + return results; + }, + + include: function(object) { + var found = false; + this.each(function(value) { + if (value == object) { + found = true; + throw $break; + } + }); + return found; + }, + + inGroupsOf: function(number, fillWith) { + fillWith = fillWith === undefined ? null : fillWith; + return this.eachSlice(number, function(slice) { + while(slice.length < number) slice.push(fillWith); + return slice; + }); + }, + + inject: function(memo, iterator) { + this.each(function(value, index) { + memo = iterator(memo, value, index); + }); + return memo; + }, + + invoke: function(method) { + var args = $A(arguments).slice(1); + return this.map(function(value) { + return value[method].apply(value, args); + }); + }, + + max: function(iterator) { + var result; + this.each(function(value, index) { + value = (iterator || Prototype.K)(value, index); + if (result == undefined || value >= result) + result = value; + }); + return result; + }, + + min: function(iterator) { + var result; + this.each(function(value, index) { + value = (iterator || Prototype.K)(value, index); + if (result == undefined || value < result) + result = value; + }); + return result; + }, + + partition: function(iterator) { + var trues = [], falses = []; + this.each(function(value, index) { + ((iterator || Prototype.K)(value, index) ? + trues : falses).push(value); + }); + return [trues, falses]; + }, + + pluck: function(property) { + var results = []; + this.each(function(value, index) { + results.push(value[property]); + }); + return results; + }, + + reject: function(iterator) { + var results = []; + this.each(function(value, index) { + if (!iterator(value, index)) + results.push(value); + }); + return results; + }, + + sortBy: function(iterator) { + return this.map(function(value, index) { + return {value: value, criteria: iterator(value, index)}; + }).sort(function(left, right) { + var a = left.criteria, b = right.criteria; + return a < b ? -1 : a > b ? 1 : 0; + }).pluck('value'); + }, + + toArray: function() { + return this.map(); + }, + + zip: function() { + var iterator = Prototype.K, args = $A(arguments); + if (typeof args.last() == 'function') + iterator = args.pop(); + + var collections = [this].concat(args).map($A); + return this.map(function(value, index) { + return iterator(collections.pluck(index)); + }); + }, + + size: function() { + return this.toArray().length; + }, + + inspect: function() { + return '#<Enumerable:' + this.toArray().inspect() + '>'; + } +} + +Object.extend(Enumerable, { + map: Enumerable.collect, + find: Enumerable.detect, + select: Enumerable.findAll, + member: Enumerable.include, + entries: Enumerable.toArray +}); +var $A = Array.from = function(iterable) { + if (!iterable) return []; + if (iterable.toArray) { + return iterable.toArray(); + } else { + var results = []; + for (var i = 0, length = iterable.length; i < length; i++) + results.push(iterable[i]); + return results; + } +} + +if (Prototype.Browser.WebKit) { + $A = Array.from = function(iterable) { + if (!iterable) return []; + if (!(typeof iterable == 'function' && iterable == '[object NodeList]') && + iterable.toArray) { + return iterable.toArray(); + } else { + var results = []; + for (var i = 0, length = iterable.length; i < length; i++) + results.push(iterable[i]); + return results; + } + } +} + +Object.extend(Array.prototype, Enumerable); + +if (!Array.prototype._reverse) + Array.prototype._reverse = Array.prototype.reverse; + +Object.extend(Array.prototype, { + _each: function(iterator) { + for (var i = 0, length = this.length; i < length; i++) + iterator(this[i]); + }, + + clear: function() { + this.length = 0; + return this; + }, + + first: function() { + return this[0]; + }, + + last: function() { + return this[this.length - 1]; + }, + + compact: function() { + return this.select(function(value) { + return value != null; + }); + }, + + flatten: function() { + return this.inject([], function(array, value) { + return array.concat(value && value.constructor == Array ? + value.flatten() : [value]); + }); + }, + + without: function() { + var values = $A(arguments); + return this.select(function(value) { + return !values.include(value); + }); + }, + + indexOf: function(object) { + for (var i = 0, length = this.length; i < length; i++) + if (this[i] == object) return i; + return -1; + }, + + reverse: function(inline) { + return (inline !== false ? this : this.toArray())._reverse(); + }, + + reduce: function() { + return this.length > 1 ? this : this[0]; + }, + + uniq: function(sorted) { + return this.inject([], function(array, value, index) { + if (0 == index || (sorted ? array.last() != value : !array.include(value))) + array.push(value); + return array; + }); + }, + + clone: function() { + return [].concat(this); + }, + + size: function() { + return this.length; + }, + + inspect: function() { + return '[' + this.map(Object.inspect).join(', ') + ']'; + }, + + toJSON: function() { + var results = []; + this.each(function(object) { + var value = Object.toJSON(object); + if (value !== undefined) results.push(value); + }); + return '[' + results.join(', ') + ']'; + } +}); + +Array.prototype.toArray = Array.prototype.clone; + +function $w(string) { + string = string.strip(); + return string ? string.split(/\s+/) : []; +} + +if (Prototype.Browser.Opera){ + Array.prototype.concat = function() { + var array = []; + for (var i = 0, length = this.length; i < length; i++) array.push(this[i]); + for (var i = 0, length = arguments.length; i < length; i++) { + if (arguments[i].constructor == Array) { + for (var j = 0, arrayLength = arguments[i].length; j < arrayLength; j++) + array.push(arguments[i][j]); + } else { + array.push(arguments[i]); + } + } + return array; + } +} +var Hash = function(object) { + if (object instanceof Hash) this.merge(object); + else Object.extend(this, object || {}); +}; + +Object.extend(Hash, { + toQueryString: function(obj) { + var parts = []; + parts.add = arguments.callee.addPair; + + this.prototype._each.call(obj, function(pair) { + if (!pair.key) return; + var value = pair.value; + + if (value && typeof value == 'object') { + if (value.constructor == Array) value.each(function(value) { + parts.add(pair.key, value); + }); + return; + } + parts.add(pair.key, value); + }); + + return parts.join('&'); + }, + + toJSON: function(object) { + var results = []; + this.prototype._each.call(object, function(pair) { + var value = Object.toJSON(pair.value); + if (value !== undefined) results.push(pair.key.toJSON() + ': ' + value); + }); + return '{' + results.join(', ') + '}'; + } +}); + +Hash.toQueryString.addPair = function(key, value, prefix) { + key = encodeURIComponent(key); + if (value === undefined) this.push(key); + else this.push(key + '=' + (value == null ? '' : encodeURIComponent(value))); +} + +Object.extend(Hash.prototype, Enumerable); +Object.extend(Hash.prototype, { + _each: function(iterator) { + for (var key in this) { + var value = this[key]; + if (value && value == Hash.prototype[key]) continue; + + var pair = [key, value]; + pair.key = key; + pair.value = value; + iterator(pair); + } + }, + + keys: function() { + return this.pluck('key'); + }, + + values: function() { + return this.pluck('value'); + }, + + merge: function(hash) { + return $H(hash).inject(this, function(mergedHash, pair) { + mergedHash[pair.key] = pair.value; + return mergedHash; + }); + }, + + remove: function() { + var result; + for(var i = 0, length = arguments.length; i < length; i++) { + var value = this[arguments[i]]; + if (value !== undefined){ + if (result === undefined) result = value; + else { + if (result.constructor != Array) result = [result]; + result.push(value) + } + } + delete this[arguments[i]]; + } + return result; + }, + + toQueryString: function() { + return Hash.toQueryString(this); + }, + + inspect: function() { + return '#<Hash:{' + this.map(function(pair) { + return pair.map(Object.inspect).join(': '); + }).join(', ') + '}>'; + }, + + toJSON: function() { + return Hash.toJSON(this); + } +}); + +function $H(object) { + if (object instanceof Hash) return object; + return new Hash(object); +}; + +// Safari iterates over shadowed properties +if (function() { + var i = 0, Test = function(value) { this.key = value }; + Test.prototype.key = 'foo'; + for (var property in new Test('bar')) i++; + return i > 1; +}()) Hash.prototype._each = function(iterator) { + var cache = []; + for (var key in this) { + var value = this[key]; + if ((value && value == Hash.prototype[key]) || cache.include(key)) continue; + cache.push(key); + var pair = [key, value]; + pair.key = key; + pair.value = value; + iterator(pair); + } +}; +ObjectRange = Class.create(); +Object.extend(ObjectRange.prototype, Enumerable); +Object.extend(ObjectRange.prototype, { + initialize: function(start, end, exclusive) { + this.start = start; + this.end = end; + this.exclusive = exclusive; + }, + + _each: function(iterator) { + var value = this.start; + while (this.include(value)) { + iterator(value); + value = value.succ(); + } + }, + + include: function(value) { + if (value < this.start) + return false; + if (this.exclusive) + return value < this.end; + return value <= this.end; + } +}); + +var $R = function(start, end, exclusive) { + return new ObjectRange(start, end, exclusive); +} + +var Ajax = { + getTransport: function() { + return Try.these( + function() {return new XMLHttpRequest()}, + function() {return new ActiveXObject('Msxml2.XMLHTTP')}, + function() {return new ActiveXObject('Microsoft.XMLHTTP')} + ) || false; + }, + + activeRequestCount: 0 +} + +Ajax.Responders = { + responders: [], + + _each: function(iterator) { + this.responders._each(iterator); + }, + + register: function(responder) { + if (!this.include(responder)) + this.responders.push(responder); + }, + + unregister: function(responder) { + this.responders = this.responders.without(responder); + }, + + dispatch: function(callback, request, transport, json) { + this.each(function(responder) { + if (typeof responder[callback] == 'function') { + try { + responder[callback].apply(responder, [request, transport, json]); + } catch (e) {} + } + }); + } +}; + +Object.extend(Ajax.Responders, Enumerable); + +Ajax.Responders.register({ + onCreate: function() { + Ajax.activeRequestCount++; + }, + onComplete: function() { + Ajax.activeRequestCount--; + } +}); + +Ajax.Base = function() {}; +Ajax.Base.prototype = { + setOptions: function(options) { + this.options = { + method: 'post', + asynchronous: true, + contentType: 'application/x-www-form-urlencoded', + encoding: 'UTF-8', + parameters: '' + } + Object.extend(this.options, options || {}); + + this.options.method = this.options.method.toLowerCase(); + if (typeof this.options.parameters == 'string') + this.options.parameters = this.options.parameters.toQueryParams(); + } +} + +Ajax.Request = Class.create(); +Ajax.Request.Events = + ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete']; + +Ajax.Request.prototype = Object.extend(new Ajax.Base(), { + _complete: false, + + initialize: function(url, options) { + this.transport = Ajax.getTransport(); + this.setOptions(options); + this.request(url); + }, + + request: function(url) { + this.url = url; + this.method = this.options.method; + var params = Object.clone(this.options.parameters); + + if (!['get', 'post'].include(this.method)) { + // simulate other verbs over post + params['_method'] = this.method; + this.method = 'post'; + } + + this.parameters = params; + + if (params = Hash.toQueryString(params)) { + // when GET, append parameters to URL + if (this.method == 'get') + this.url += (this.url.include('?') ? '&' : '?') + params; + else if (/Konqueror|Safari|KHTML/.test(navigator.userAgent)) + params += '&_='; + } + + try { + if (this.options.onCreate) this.options.onCreate(this.transport); + Ajax.Responders.dispatch('onCreate', this, this.transport); + + this.transport.open(this.method.toUpperCase(), this.url, + this.options.asynchronous); + + if (this.options.asynchronous) + setTimeout(function() { this.respondToReadyState(1) }.bind(this), 10); + + this.transport.onreadystatechange = this.onStateChange.bind(this); + this.setRequestHeaders(); + + this.body = this.method == 'post' ? (this.options.postBody || params) : null; + this.transport.send(this.body); + + /* Force Firefox to handle ready state 4 for synchronous requests */ + if (!this.options.asynchronous && this.transport.overrideMimeType) + this.onStateChange(); + + } + catch (e) { + this.dispatchException(e); + } + }, + + onStateChange: function() { + var readyState = this.transport.readyState; + if (readyState > 1 && !((readyState == 4) && this._complete)) + this.respondToReadyState(this.transport.readyState); + }, + + setRequestHeaders: function() { + var headers = { + 'X-Requested-With': 'XMLHttpRequest', + 'X-Prototype-Version': Prototype.Version, + 'Accept': 'text/javascript, text/html, application/xml, text/xml, */*' + }; + + if (this.method == 'post') { + headers['Content-type'] = this.options.contentType + + (this.options.encoding ? '; charset=' + this.options.encoding : ''); + + /* Force "Connection: close" for older Mozilla browsers to work + * around a bug where XMLHttpRequest sends an incorrect + * Content-length header. See Mozilla Bugzilla #246651. + */ + if (this.transport.overrideMimeType && + (navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005])[1] < 2005) + headers['Connection'] = 'close'; + } + + // user-defined headers + if (typeof this.options.requestHeaders == 'object') { + var extras = this.options.requestHeaders; + + if (typeof extras.push == 'function') + for (var i = 0, length = extras.length; i < length; i += 2) + headers[extras[i]] = extras[i+1]; + else + $H(extras).each(function(pair) { headers[pair.key] = pair.value }); + } + + for (var name in headers) + this.transport.setRequestHeader(name, headers[name]); + }, + + success: function() { + return !this.transport.status + || (this.transport.status >= 200 && this.transport.status < 300); + }, + + respondToReadyState: function(readyState) { + var state = Ajax.Request.Events[readyState]; + var transport = this.transport, json = this.evalJSON(); + + if (state == 'Complete') { + try { + this._complete = true; + (this.options['on' + this.transport.status] + || this.options['on' + (this.success() ? 'Success' : 'Failure')] + || Prototype.emptyFunction)(transport, json); + } catch (e) { + this.dispatchException(e); + } + + var contentType = this.getHeader('Content-type'); + if (contentType && contentType.strip(). + match(/^(text|application)\/(x-)?(java|ecma)script(;.*)?$/i)) + this.evalResponse(); + } + + try { + (this.options['on' + state] || Prototype.emptyFunction)(transport, json); + Ajax.Responders.dispatch('on' + state, this, transport, json); + } catch (e) { + this.dispatchException(e); + } + + if (state == 'Complete') { + // avoid memory leak in MSIE: clean up + this.transport.onreadystatechange = Prototype.emptyFunction; + } + }, + + getHeader: function(name) { + try { + return this.transport.getResponseHeader(name); + } catch (e) { return null } + }, + + evalJSON: function() { + try { + var json = this.getHeader('X-JSON'); + return json ? json.evalJSON() : null; + } catch (e) { return null } + }, + + evalResponse: function() { + try { + return eval((this.transport.responseText || '').unfilterJSON()); + } catch (e) { + this.dispatchException(e); + } + }, + + dispatchException: function(exception) { + (this.options.onException || Prototype.emptyFunction)(this, exception); + Ajax.Responders.dispatch('onException', this, exception); + } +}); + +Ajax.Updater = Class.create(); + +Object.extend(Object.extend(Ajax.Updater.prototype, Ajax.Request.prototype), { + initialize: function(container, url, options) { + this.container = { + success: (container.success || container), + failure: (container.failure || (container.success ? null : container)) + } + + this.transport = Ajax.getTransport(); + this.setOptions(options); + + var onComplete = this.options.onComplete || Prototype.emptyFunction; + this.options.onComplete = (function(transport, param) { + this.updateContent(); + onComplete(transport, param); + }).bind(this); + + this.request(url); + }, + + updateContent: function() { + var receiver = this.container[this.success() ? 'success' : 'failure']; + var response = this.transport.responseText; + + if (!this.options.evalScripts) response = response.stripScripts(); + + if (receiver = $(receiver)) { + if (this.options.insertion) + new this.options.insertion(receiver, response); + else + receiver.update(response); + } + + if (this.success()) { + if (this.onComplete) + setTimeout(this.onComplete.bind(this), 10); + } + } +}); + +Ajax.PeriodicalUpdater = Class.create(); +Ajax.PeriodicalUpdater.prototype = Object.extend(new Ajax.Base(), { + initialize: function(container, url, options) { + this.setOptions(options); + this.onComplete = this.options.onComplete; + + this.frequency = (this.options.frequency || 2); + this.decay = (this.options.decay || 1); + + this.updater = {}; + this.container = container; + this.url = url; + + this.start(); + }, + + start: function() { + this.options.onComplete = this.updateComplete.bind(this); + this.onTimerEvent(); + }, + + stop: function() { + this.updater.options.onComplete = undefined; + clearTimeout(this.timer); + (this.onComplete || Prototype.emptyFunction).apply(this, arguments); + }, + + updateComplete: function(request) { + if (this.options.decay) { + this.decay = (request.responseText == this.lastText ? + this.decay * this.options.decay : 1); + + this.lastText = request.responseText; + } + this.timer = setTimeout(this.onTimerEvent.bind(this), + this.decay * this.frequency * 1000); + }, + + onTimerEvent: function() { + this.updater = new Ajax.Updater(this.container, this.url, this.options); + } +}); +function $(element) { + if (arguments.length > 1) { + for (var i = 0, elements = [], length = arguments.length; i < length; i++) + elements.push($(arguments[i])); + return elements; + } + if (typeof element == 'string') + element = document.getElementById(element); + return Element.extend(element); +} + +if (Prototype.BrowserFeatures.XPath) { + document._getElementsByXPath = function(expression, parentElement) { + var results = []; + var query = document.evaluate(expression, $(parentElement) || document, + null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); + for (var i = 0, length = query.snapshotLength; i < length; i++) + results.push(query.snapshotItem(i)); + return results; + }; + + document.getElementsByClassName = function(className, parentElement) { + var q = ".//*[contains(concat(' ', @class, ' '), ' " + className + " ')]"; + return document._getElementsByXPath(q, parentElement); + } + +} else document.getElementsByClassName = function(className, parentElement) { + var children = ($(parentElement) || document.body).getElementsByTagName('*'); + var elements = [], child; + for (var i = 0, length = children.length; i < length; i++) { + child = children[i]; + if (Element.hasClassName(child, className)) + elements.push(Element.extend(child)); + } + return elements; +}; + +/*--------------------------------------------------------------------------*/ + +if (!window.Element) var Element = {}; + +Element.extend = function(element) { + var F = Prototype.BrowserFeatures; + if (!element || !element.tagName || element.nodeType == 3 || + element._extended || F.SpecificElementExtensions || element == window) + return element; + + var methods = {}, tagName = element.tagName, cache = Element.extend.cache, + T = Element.Methods.ByTag; + + // extend methods for all tags (Safari doesn't need this) + if (!F.ElementExtensions) { + Object.extend(methods, Element.Methods), + Object.extend(methods, Element.Methods.Simulated); + } + + // extend methods for specific tags + if (T[tagName]) Object.extend(methods, T[tagName]); + + for (var property in methods) { + var value = methods[property]; + if (typeof value == 'function' && !(property in element)) + element[property] = cache.findOrStore(value); + } + + element._extended = Prototype.emptyFunction; + return element; +}; + +Element.extend.cache = { + findOrStore: function(value) { + return this[value] = this[value] || function() { + return value.apply(null, [this].concat($A(arguments))); + } + } +}; + +Element.Methods = { + visible: function(element) { + return $(element).style.display != 'none'; + }, + + toggle: function(element) { + element = $(element); + Element[Element.visible(element) ? 'hide' : 'show'](element); + return element; + }, + + hide: function(element) { + $(element).style.display = 'none'; + return element; + }, + + show: function(element) { + $(element).style.display = ''; + return element; + }, + + remove: function(element) { + element = $(element); + element.parentNode.removeChild(element); + return element; + }, + + update: function(element, html) { + html = typeof html == 'undefined' ? '' : html.toString(); + $(element).innerHTML = html.stripScripts(); + setTimeout(function() {html.evalScripts()}, 10); + return element; + }, + + replace: function(element, html) { + element = $(element); + html = typeof html == 'undefined' ? '' : html.toString(); + if (element.outerHTML) { + element.outerHTML = html.stripScripts(); + } else { + var range = element.ownerDocument.createRange(); + range.selectNodeContents(element); + element.parentNode.replaceChild( + range.createContextualFragment(html.stripScripts()), element); + } + setTimeout(function() {html.evalScripts()}, 10); + return element; + }, + + inspect: function(element) { + element = $(element); + var result = '<' + element.tagName.toLowerCase(); + $H({'id': 'id', 'className': 'class'}).each(function(pair) { + var property = pair.first(), attribute = pair.last(); + var value = (element[property] || '').toString(); + if (value) result += ' ' + attribute + '=' + value.inspect(true); + }); + return result + '>'; + }, + + recursivelyCollect: function(element, property) { + element = $(element); + var elements = []; + while (element = element[property]) + if (element.nodeType == 1) + elements.push(Element.extend(element)); + return elements; + }, + + ancestors: function(element) { + return $(element).recursivelyCollect('parentNode'); + }, + + descendants: function(element) { + return $A($(element).getElementsByTagName('*')).each(Element.extend); + }, + + firstDescendant: function(element) { + element = $(element).firstChild; + while (element && element.nodeType != 1) element = element.nextSibling; + return $(element); + }, + + immediateDescendants: function(element) { + if (!(element = $(element).firstChild)) return []; + while (element && element.nodeType != 1) element = element.nextSibling; + if (element) return [element].concat($(element).nextSiblings()); + return []; + }, + + previousSiblings: function(element) { + return $(element).recursivelyCollect('previousSibling'); + }, + + nextSiblings: function(element) { + return $(element).recursivelyCollect('nextSibling'); + }, + + siblings: function(element) { + element = $(element); + return element.previousSiblings().reverse().concat(element.nextSiblings()); + }, + + match: function(element, selector) { + if (typeof selector == 'string') + selector = new Selector(selector); + return selector.match($(element)); + }, + + up: function(element, expression, index) { + element = $(element); + if (arguments.length == 1) return $(element.parentNode); + var ancestors = element.ancestors(); + return expression ? Selector.findElement(ancestors, expression, index) : + ancestors[index || 0]; + }, + + down: function(element, expression, index) { + element = $(element); + if (arguments.length == 1) return element.firstDescendant(); + var descendants = element.descendants(); + return expression ? Selector.findElement(descendants, expression, index) : + descendants[index || 0]; + }, + + previous: function(element, expression, index) { + element = $(element); + if (arguments.length == 1) return $(Selector.handlers.previousElementSibling(element)); + var previousSiblings = element.previousSiblings(); + return expression ? Selector.findElement(previousSiblings, expression, index) : + previousSiblings[index || 0]; + }, + + next: function(element, expression, index) { + element = $(element); + if (arguments.length == 1) return $(Selector.handlers.nextElementSibling(element)); + var nextSiblings = element.nextSiblings(); + return expression ? Selector.findElement(nextSiblings, expression, index) : + nextSiblings[index || 0]; + }, + + getElementsBySelector: function() { + var args = $A(arguments), element = $(args.shift()); + return Selector.findChildElements(element, args); + }, + + getElementsByClassName: function(element, className) { + return document.getElementsByClassName(className, element); + }, + + readAttribute: function(element, name) { + element = $(element); + if (Prototype.Browser.IE) { + if (!element.attributes) return null; + var t = Element._attributeTranslations; + if (t.values[name]) return t.values[name](element, name); + if (t.names[name]) name = t.names[name]; + var attribute = element.attributes[name]; + return attribute ? attribute.nodeValue : null; + } + return element.getAttribute(name); + }, + + getHeight: function(element) { + return $(element).getDimensions().height; + }, + + getWidth: function(element) { + return $(element).getDimensions().width; + }, + + classNames: function(element) { + return new Element.ClassNames(element); + }, + + hasClassName: function(element, className) { + if (!(element = $(element))) return; + var elementClassName = element.className; + if (elementClassName.length == 0) return false; + if (elementClassName == className || + elementClassName.match(new RegExp("(^|\\s)" + className + "(\\s|$)"))) + return true; + return false; + }, + + addClassName: function(element, className) { + if (!(element = $(element))) return; + Element.classNames(element).add(className); + return element; + }, + + removeClassName: function(element, className) { + if (!(element = $(element))) return; + Element.classNames(element).remove(className); + return element; + }, + + toggleClassName: function(element, className) { + if (!(element = $(element))) return; + Element.classNames(element)[element.hasClassName(className) ? 'remove' : 'add'](className); + return element; + }, + + observe: function() { + Event.observe.apply(Event, arguments); + return $A(arguments).first(); + }, + + stopObserving: function() { + Event.stopObserving.apply(Event, arguments); + return $A(arguments).first(); + }, + + // removes whitespace-only text node children + cleanWhitespace: function(element) { + element = $(element); + var node = element.firstChild; + while (node) { + var nextNode = node.nextSibling; + if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) + element.removeChild(node); + node = nextNode; + } + return element; + }, + + empty: function(element) { + return $(element).innerHTML.blank(); + }, + + descendantOf: function(element, ancestor) { + element = $(element), ancestor = $(ancestor); + while (element = element.parentNode) + if (element == ancestor) return true; + return false; + }, + + scrollTo: function(element) { + element = $(element); + var pos = Position.cumulativeOffset(element); + window.scrollTo(pos[0], pos[1]); + return element; + }, + + getStyle: function(element, style) { + element = $(element); + style = style == 'float' ? 'cssFloat' : style.camelize(); + var value = element.style[style]; + if (!value) { + var css = document.defaultView.getComputedStyle(element, null); + value = css ? css[style] : null; + } + if (style == 'opacity') return value ? parseFloat(value) : 1.0; + return value == 'auto' ? null : value; + }, + + getOpacity: function(element) { + return $(element).getStyle('opacity'); + }, + + setStyle: function(element, styles, camelized) { + element = $(element); + var elementStyle = element.style; + + for (var property in styles) + if (property == 'opacity') element.setOpacity(styles[property]) + else + elementStyle[(property == 'float' || property == 'cssFloat') ? + (elementStyle.styleFloat === undefined ? 'cssFloat' : 'styleFloat') : + (camelized ? property : property.camelize())] = styles[property]; + + return element; + }, + + setOpacity: function(element, value) { + element = $(element); + element.style.opacity = (value == 1 || value === '') ? '' : + (value < 0.00001) ? 0 : value; + return element; + }, + + getDimensions: function(element) { + element = $(element); + var display = $(element).getStyle('display'); + if (display != 'none' && display != null) // Safari bug + return {width: element.offsetWidth, height: element.offsetHeight}; + + // All *Width and *Height properties give 0 on elements with display none, + // so enable the element temporarily + var els = element.style; + var originalVisibility = els.visibility; + var originalPosition = els.position; + var originalDisplay = els.display; + els.visibility = 'hidden'; + els.position = 'absolute'; + els.display = 'block'; + var originalWidth = element.clientWidth; + var originalHeight = element.clientHeight; + els.display = originalDisplay; + els.position = originalPosition; + els.visibility = originalVisibility; + return {width: originalWidth, height: originalHeight}; + }, + + makePositioned: function(element) { + element = $(element); + var pos = Element.getStyle(element, 'position'); + if (pos == 'static' || !pos) { + element._madePositioned = true; + element.style.position = 'relative'; + // Opera returns the offset relative to the positioning context, when an + // element is position relative but top and left have not been defined + if (window.opera) { + element.style.top = 0; + element.style.left = 0; + } + } + return element; + }, + + undoPositioned: function(element) { + element = $(element); + if (element._madePositioned) { + element._madePositioned = undefined; + element.style.position = + element.style.top = + element.style.left = + element.style.bottom = + element.style.right = ''; + } + return element; + }, + + makeClipping: function(element) { + element = $(element); + if (element._overflow) return element; + element._overflow = element.style.overflow || 'auto'; + if ((Element.getStyle(element, 'overflow') || 'visible') != 'hidden') + element.style.overflow = 'hidden'; + return element; + }, + + undoClipping: function(element) { + element = $(element); + if (!element._overflow) return element; + element.style.overflow = element._overflow == 'auto' ? '' : element._overflow; + element._overflow = null; + return element; + } +}; + +Object.extend(Element.Methods, { + childOf: Element.Methods.descendantOf, + childElements: Element.Methods.immediateDescendants +}); + +if (Prototype.Browser.Opera) { + Element.Methods._getStyle = Element.Methods.getStyle; + Element.Methods.getStyle = function(element, style) { + switch(style) { + case 'left': + case 'top': + case 'right': + case 'bottom': + if (Element._getStyle(element, 'position') == 'static') return null; + default: return Element._getStyle(element, style); + } + }; +} +else if (Prototype.Browser.IE) { + Element.Methods.getStyle = function(element, style) { + element = $(element); + style = (style == 'float' || style == 'cssFloat') ? 'styleFloat' : style.camelize(); + var value = element.style[style]; + if (!value && element.currentStyle) value = element.currentStyle[style]; + + if (style == 'opacity') { + if (value = (element.getStyle('filter') || '').match(/alpha\(opacity=(.*)\)/)) + if (value[1]) return parseFloat(value[1]) / 100; + return 1.0; + } + + if (value == 'auto') { + if ((style == 'width' || style == 'height') && (element.getStyle('display') != 'none')) + return element['offset'+style.capitalize()] + 'px'; + return null; + } + return value; + }; + + Element.Methods.setOpacity = function(element, value) { + element = $(element); + var filter = element.getStyle('filter'), style = element.style; + if (value == 1 || value === '') { + style.filter = filter.replace(/alpha\([^\)]*\)/gi,''); + return element; + } else if (value < 0.00001) value = 0; + style.filter = filter.replace(/alpha\([^\)]*\)/gi, '') + + 'alpha(opacity=' + (value * 100) + ')'; + return element; + }; + + // IE is missing .innerHTML support for TABLE-related elements + Element.Methods.update = function(element, html) { + element = $(element); + html = typeof html == 'undefined' ? '' : html.toString(); + var tagName = element.tagName.toUpperCase(); + if (['THEAD','TBODY','TR','TD'].include(tagName)) { + var div = document.createElement('div'); + switch (tagName) { + case 'THEAD': + case 'TBODY': + div.innerHTML = '<table><tbody>' + html.stripScripts() + '</tbody></table>'; + depth = 2; + break; + case 'TR': + div.innerHTML = '<table><tbody><tr>' + html.stripScripts() + '</tr></tbody></table>'; + depth = 3; + break; + case 'TD': + div.innerHTML = '<table><tbody><tr><td>' + html.stripScripts() + '</td></tr></tbody></table>'; + depth = 4; + } + $A(element.childNodes).each(function(node) { element.removeChild(node) }); + depth.times(function() { div = div.firstChild }); + $A(div.childNodes).each(function(node) { element.appendChild(node) }); + } else { + element.innerHTML = html.stripScripts(); + } + setTimeout(function() { html.evalScripts() }, 10); + return element; + } +} +else if (Prototype.Browser.Gecko) { + Element.Methods.setOpacity = function(element, value) { + element = $(element); + element.style.opacity = (value == 1) ? 0.999999 : + (value === '') ? '' : (value < 0.00001) ? 0 : value; + return element; + }; +} + +Element._attributeTranslations = { + names: { + colspan: "colSpan", + rowspan: "rowSpan", + valign: "vAlign", + datetime: "dateTime", + accesskey: "accessKey", + tabindex: "tabIndex", + enctype: "encType", + maxlength: "maxLength", + readonly: "readOnly", + longdesc: "longDesc" + }, + values: { + _getAttr: function(element, attribute) { + return element.getAttribute(attribute, 2); + }, + _flag: function(element, attribute) { + return $(element).hasAttribute(attribute) ? attribute : null; + }, + style: function(element) { + return element.style.cssText.toLowerCase(); + }, + title: function(element) { + var node = element.getAttributeNode('title'); + return node.specified ? node.nodeValue : null; + } + } +}; + +(function() { + Object.extend(this, { + href: this._getAttr, + src: this._getAttr, + type: this._getAttr, + disabled: this._flag, + checked: this._flag, + readonly: this._flag, + multiple: this._flag + }); +}).call(Element._attributeTranslations.values); + +Element.Methods.Simulated = { + hasAttribute: function(element, attribute) { + var t = Element._attributeTranslations, node; + attribute = t.names[attribute] || attribute; + node = $(element).getAttributeNode(attribute); + return node && node.specified; + } +}; + +Element.Methods.ByTag = {}; + +Object.extend(Element, Element.Methods); + +if (!Prototype.BrowserFeatures.ElementExtensions && + document.createElement('div').__proto__) { + window.HTMLElement = {}; + window.HTMLElement.prototype = document.createElement('div').__proto__; + Prototype.BrowserFeatures.ElementExtensions = true; +} + +Element.hasAttribute = function(element, attribute) { + if (element.hasAttribute) return element.hasAttribute(attribute); + return Element.Methods.Simulated.hasAttribute(element, attribute); +}; + +Element.addMethods = function(methods) { + var F = Prototype.BrowserFeatures, T = Element.Methods.ByTag; + + if (!methods) { + Object.extend(Form, Form.Methods); + Object.extend(Form.Element, Form.Element.Methods); + Object.extend(Element.Methods.ByTag, { + "FORM": Object.clone(Form.Methods), + "INPUT": Object.clone(Form.Element.Methods), + "SELECT": Object.clone(Form.Element.Methods), + "TEXTAREA": Object.clone(Form.Element.Methods) + }); + } + + if (arguments.length == 2) { + var tagName = methods; + methods = arguments[1]; + } + + if (!tagName) Object.extend(Element.Methods, methods || {}); + else { + if (tagName.constructor == Array) tagName.each(extend); + else extend(tagName); + } + + function extend(tagName) { + tagName = tagName.toUpperCase(); + if (!Element.Methods.ByTag[tagName]) + Element.Methods.ByTag[tagName] = {}; + Object.extend(Element.Methods.ByTag[tagName], methods); + } + + function copy(methods, destination, onlyIfAbsent) { + onlyIfAbsent = onlyIfAbsent || false; + var cache = Element.extend.cache; + for (var property in methods) { + var value = methods[property]; + if (!onlyIfAbsent || !(property in destination)) + destination[property] = cache.findOrStore(value); + } + } + + function findDOMClass(tagName) { + var klass; + var trans = { + "OPTGROUP": "OptGroup", "TEXTAREA": "TextArea", "P": "Paragraph", + "FIELDSET": "FieldSet", "UL": "UList", "OL": "OList", "DL": "DList", + "DIR": "Directory", "H1": "Heading", "H2": "Heading", "H3": "Heading", + "H4": "Heading", "H5": "Heading", "H6": "Heading", "Q": "Quote", + "INS": "Mod", "DEL": "Mod", "A": "Anchor", "IMG": "Image", "CAPTION": + "TableCaption", "COL": "TableCol", "COLGROUP": "TableCol", "THEAD": + "TableSection", "TFOOT": "TableSection", "TBODY": "TableSection", "TR": + "TableRow", "TH": "TableCell", "TD": "TableCell", "FRAMESET": + "FrameSet", "IFRAME": "IFrame" + }; + if (trans[tagName]) klass = 'HTML' + trans[tagName] + 'Element'; + if (window[klass]) return window[klass]; + klass = 'HTML' + tagName + 'Element'; + if (window[klass]) return window[klass]; + klass = 'HTML' + tagName.capitalize() + 'Element'; + if (window[klass]) return window[klass]; + + window[klass] = {}; + window[klass].prototype = document.createElement(tagName).__proto__; + return window[klass]; + } + + if (F.ElementExtensions) { + copy(Element.Methods, HTMLElement.prototype); + copy(Element.Methods.Simulated, HTMLElement.prototype, true); + } + + if (F.SpecificElementExtensions) { + for (var tag in Element.Methods.ByTag) { + var klass = findDOMClass(tag); + if (typeof klass == "undefined") continue; + copy(T[tag], klass.prototype); + } + } + + Object.extend(Element, Element.Methods); + delete Element.ByTag; +}; + +var Toggle = { display: Element.toggle }; + +/*--------------------------------------------------------------------------*/ + +Abstract.Insertion = function(adjacency) { + this.adjacency = adjacency; +} + +Abstract.Insertion.prototype = { + initialize: function(element, content) { + this.element = $(element); + this.content = content.stripScripts(); + + if (this.adjacency && this.element.insertAdjacentHTML) { + try { + this.element.insertAdjacentHTML(this.adjacency, this.content); + } catch (e) { + var tagName = this.element.tagName.toUpperCase(); + if (['TBODY', 'TR'].include(tagName)) { + this.insertContent(this.contentFromAnonymousTable()); + } else { + throw e; + } + } + } else { + this.range = this.element.ownerDocument.createRange(); + if (this.initializeRange) this.initializeRange(); + this.insertContent([this.range.createContextualFragment(this.content)]); + } + + setTimeout(function() {content.evalScripts()}, 10); + }, + + contentFromAnonymousTable: function() { + var div = document.createElement('div'); + div.innerHTML = '<table><tbody>' + this.content + '</tbody></table>'; + return $A(div.childNodes[0].childNodes[0].childNodes); + } +} + +var Insertion = new Object(); + +Insertion.Before = Class.create(); +Insertion.Before.prototype = Object.extend(new Abstract.Insertion('beforeBegin'), { + initializeRange: function() { + this.range.setStartBefore(this.element); + }, + + insertContent: function(fragments) { + fragments.each((function(fragment) { + this.element.parentNode.insertBefore(fragment, this.element); + }).bind(this)); + } +}); + +Insertion.Top = Class.create(); +Insertion.Top.prototype = Object.extend(new Abstract.Insertion('afterBegin'), { + initializeRange: function() { + this.range.selectNodeContents(this.element); + this.range.collapse(true); + }, + + insertContent: function(fragments) { + fragments.reverse(false).each((function(fragment) { + this.element.insertBefore(fragment, this.element.firstChild); + }).bind(this)); + } +}); + +Insertion.Bottom = Class.create(); +Insertion.Bottom.prototype = Object.extend(new Abstract.Insertion('beforeEnd'), { + initializeRange: function() { + this.range.selectNodeContents(this.element); + this.range.collapse(this.element); + }, + + insertContent: function(fragments) { + fragments.each((function(fragment) { + this.element.appendChild(fragment); + }).bind(this)); + } +}); + +Insertion.After = Class.create(); +Insertion.After.prototype = Object.extend(new Abstract.Insertion('afterEnd'), { + initializeRange: function() { + this.range.setStartAfter(this.element); + }, + + insertContent: function(fragments) { + fragments.each((function(fragment) { + this.element.parentNode.insertBefore(fragment, + this.element.nextSibling); + }).bind(this)); + } +}); + +/*--------------------------------------------------------------------------*/ + +Element.ClassNames = Class.create(); +Element.ClassNames.prototype = { + initialize: function(element) { + this.element = $(element); + }, + + _each: function(iterator) { + this.element.className.split(/\s+/).select(function(name) { + return name.length > 0; + })._each(iterator); + }, + + set: function(className) { + this.element.className = className; + }, + + add: function(classNameToAdd) { + if (this.include(classNameToAdd)) return; + this.set($A(this).concat(classNameToAdd).join(' ')); + }, + + remove: function(classNameToRemove) { + if (!this.include(classNameToRemove)) return; + this.set($A(this).without(classNameToRemove).join(' ')); + }, + + toString: function() { + return $A(this).join(' '); + } +}; + +Object.extend(Element.ClassNames.prototype, Enumerable); +/* Portions of the Selector class are derived from Jack Slocum’s DomQuery, + * part of YUI-Ext version 0.40, distributed under the terms of an MIT-style + * license. Please see http://www.yui-ext.com/ for more information. */ + +var Selector = Class.create(); + +Selector.prototype = { + initialize: function(expression) { + this.expression = expression.strip(); + this.compileMatcher(); + }, + + compileMatcher: function() { + // Selectors with namespaced attributes can't use the XPath version + if (Prototype.BrowserFeatures.XPath && !(/\[[\w-]*?:/).test(this.expression)) + return this.compileXPathMatcher(); + + var e = this.expression, ps = Selector.patterns, h = Selector.handlers, + c = Selector.criteria, le, p, m; + + if (Selector._cache[e]) { + this.matcher = Selector._cache[e]; return; + } + this.matcher = ["this.matcher = function(root) {", + "var r = root, h = Selector.handlers, c = false, n;"]; + + while (e && le != e && (/\S/).test(e)) { + le = e; + for (var i in ps) { + p = ps[i]; + if (m = e.match(p)) { + this.matcher.push(typeof c[i] == 'function' ? c[i](m) : + new Template(c[i]).evaluate(m)); + e = e.replace(m[0], ''); + break; + } + } + } + + this.matcher.push("return h.unique(n);\n}"); + eval(this.matcher.join('\n')); + Selector._cache[this.expression] = this.matcher; + }, + + compileXPathMatcher: function() { + var e = this.expression, ps = Selector.patterns, + x = Selector.xpath, le, m; + + if (Selector._cache[e]) { + this.xpath = Selector._cache[e]; return; + } + + this.matcher = ['.//*']; + while (e && le != e && (/\S/).test(e)) { + le = e; + for (var i in ps) { + if (m = e.match(ps[i])) { + this.matcher.push(typeof x[i] == 'function' ? x[i](m) : + new Template(x[i]).evaluate(m)); + e = e.replace(m[0], ''); + break; + } + } + } + + this.xpath = this.matcher.join(''); + Selector._cache[this.expression] = this.xpath; + }, + + findElements: function(root) { + root = root || document; + if (this.xpath) return document._getElementsByXPath(this.xpath, root); + return this.matcher(root); + }, + + match: function(element) { + return this.findElements(document).include(element); + }, + + toString: function() { + return this.expression; + }, + + inspect: function() { + return "#<Selector:" + this.expression.inspect() + ">"; + } +}; + +Object.extend(Selector, { + _cache: {}, + + xpath: { + descendant: "//*", + child: "/*", + adjacent: "/following-sibling::*[1]", + laterSibling: '/following-sibling::*', + tagName: function(m) { + if (m[1] == '*') return ''; + return "[local-name()='" + m[1].toLowerCase() + + "' or local-name()='" + m[1].toUpperCase() + "']"; + }, + className: "[contains(concat(' ', @class, ' '), ' #{1} ')]", + id: "[@id='#{1}']", + attrPresence: "[@#{1}]", + attr: function(m) { + m[3] = m[5] || m[6]; + return new Template(Selector.xpath.operators[m[2]]).evaluate(m); + }, + pseudo: function(m) { + var h = Selector.xpath.pseudos[m[1]]; + if (!h) return ''; + if (typeof h === 'function') return h(m); + return new Template(Selector.xpath.pseudos[m[1]]).evaluate(m); + }, + operators: { + '=': "[@#{1}='#{3}']", + '!=': "[@#{1}!='#{3}']", + '^=': "[starts-with(@#{1}, '#{3}')]", + '$=': "[substring(@#{1}, (string-length(@#{1}) - string-length('#{3}') + 1))='#{3}']", + '*=': "[contains(@#{1}, '#{3}')]", + '~=': "[contains(concat(' ', @#{1}, ' '), ' #{3} ')]", + '|=': "[contains(concat('-', @#{1}, '-'), '-#{3}-')]" + }, + pseudos: { + 'first-child': '[not(preceding-sibling::*)]', + 'last-child': '[not(following-sibling::*)]', + 'only-child': '[not(preceding-sibling::* or following-sibling::*)]', + 'empty': "[count(*) = 0 and (count(text()) = 0 or translate(text(), ' \t\r\n', '') = '')]", + 'checked': "[@checked]", + 'disabled': "[@disabled]", + 'enabled': "[not(@disabled)]", + 'not': function(m) { + var e = m[6], p = Selector.patterns, + x = Selector.xpath, le, m, v; + + var exclusion = []; + while (e && le != e && (/\S/).test(e)) { + le = e; + for (var i in p) { + if (m = e.match(p[i])) { + v = typeof x[i] == 'function' ? x[i](m) : new Template(x[i]).evaluate(m); + exclusion.push("(" + v.substring(1, v.length - 1) + ")"); + e = e.replace(m[0], ''); + break; + } + } + } + return "[not(" + exclusion.join(" and ") + ")]"; + }, + 'nth-child': function(m) { + return Selector.xpath.pseudos.nth("(count(./preceding-sibling::*) + 1) ", m); + }, + 'nth-last-child': function(m) { + return Selector.xpath.pseudos.nth("(count(./following-sibling::*) + 1) ", m); + }, + 'nth-of-type': function(m) { + return Selector.xpath.pseudos.nth("position() ", m); + }, + 'nth-last-of-type': function(m) { + return Selector.xpath.pseudos.nth("(last() + 1 - position()) ", m); + }, + 'first-of-type': function(m) { + m[6] = "1"; return Selector.xpath.pseudos['nth-of-type'](m); + }, + 'last-of-type': function(m) { + m[6] = "1"; return Selector.xpath.pseudos['nth-last-of-type'](m); + }, + 'only-of-type': function(m) { + var p = Selector.xpath.pseudos; return p['first-of-type'](m) + p['last-of-type'](m); + }, + nth: function(fragment, m) { + var mm, formula = m[6], predicate; + if (formula == 'even') formula = '2n+0'; + if (formula == 'odd') formula = '2n+1'; + if (mm = formula.match(/^(\d+)$/)) // digit only + return '[' + fragment + "= " + mm[1] + ']'; + if (mm = formula.match(/^(-?\d*)?n(([+-])(\d+))?/)) { // an+b + if (mm[1] == "-") mm[1] = -1; + var a = mm[1] ? Number(mm[1]) : 1; + var b = mm[2] ? Number(mm[2]) : 0; + predicate = "[((#{fragment} - #{b}) mod #{a} = 0) and " + + "((#{fragment} - #{b}) div #{a} >= 0)]"; + return new Template(predicate).evaluate({ + fragment: fragment, a: a, b: b }); + } + } + } + }, + + criteria: { + tagName: 'n = h.tagName(n, r, "#{1}", c); c = false;', + className: 'n = h.className(n, r, "#{1}", c); c = false;', + id: 'n = h.id(n, r, "#{1}", c); c = false;', + attrPresence: 'n = h.attrPresence(n, r, "#{1}"); c = false;', + attr: function(m) { + m[3] = (m[5] || m[6]); + return new Template('n = h.attr(n, r, "#{1}", "#{3}", "#{2}"); c = false;').evaluate(m); + }, + pseudo: function(m) { + if (m[6]) m[6] = m[6].replace(/"/g, '\\"'); + return new Template('n = h.pseudo(n, "#{1}", "#{6}", r, c); c = false;').evaluate(m); + }, + descendant: 'c = "descendant";', + child: 'c = "child";', + adjacent: 'c = "adjacent";', + laterSibling: 'c = "laterSibling";' + }, + + patterns: { + // combinators must be listed first + // (and descendant needs to be last combinator) + laterSibling: /^\s*~\s*/, + child: /^\s*>\s*/, + adjacent: /^\s*\+\s*/, + descendant: /^\s/, + + // selectors follow + tagName: /^\s*(\*|[\w\-]+)(\b|$)?/, + id: /^#([\w\-\*]+)(\b|$)/, + className: /^\.([\w\-\*]+)(\b|$)/, + pseudo: /^:((first|last|nth|nth-last|only)(-child|-of-type)|empty|checked|(en|dis)abled|not)(\((.*?)\))?(\b|$|\s)/, + attrPresence: /^\[([\w]+)\]/, + attr: /\[((?:[\w-]*:)?[\w-]+)\s*(?:([!^$*~|]?=)\s*((['"])([^\]]*?)\4|([^'"][^\]]*?)))?\]/ + }, + + handlers: { + // UTILITY FUNCTIONS + // joins two collections + concat: function(a, b) { + for (var i = 0, node; node = b[i]; i++) + a.push(node); + return a; + }, + + // marks an array of nodes for counting + mark: function(nodes) { + for (var i = 0, node; node = nodes[i]; i++) + node._counted = true; + return nodes; + }, + + unmark: function(nodes) { + for (var i = 0, node; node = nodes[i]; i++) + node._counted = undefined; + return nodes; + }, + + // mark each child node with its position (for nth calls) + // "ofType" flag indicates whether we're indexing for nth-of-type + // rather than nth-child + index: function(parentNode, reverse, ofType) { + parentNode._counted = true; + if (reverse) { + for (var nodes = parentNode.childNodes, i = nodes.length - 1, j = 1; i >= 0; i--) { + node = nodes[i]; + if (node.nodeType == 1 && (!ofType || node._counted)) node.nodeIndex = j++; + } + } else { + for (var i = 0, j = 1, nodes = parentNode.childNodes; node = nodes[i]; i++) + if (node.nodeType == 1 && (!ofType || node._counted)) node.nodeIndex = j++; + } + }, + + // filters out duplicates and extends all nodes + unique: function(nodes) { + if (nodes.length == 0) return nodes; + var results = [], n; + for (var i = 0, l = nodes.length; i < l; i++) + if (!(n = nodes[i])._counted) { + n._counted = true; + results.push(Element.extend(n)); + } + return Selector.handlers.unmark(results); + }, + + // COMBINATOR FUNCTIONS + descendant: function(nodes) { + var h = Selector.handlers; + for (var i = 0, results = [], node; node = nodes[i]; i++) + h.concat(results, node.getElementsByTagName('*')); + return results; + }, + + child: function(nodes) { + var h = Selector.handlers; + for (var i = 0, results = [], node; node = nodes[i]; i++) { + for (var j = 0, children = [], child; child = node.childNodes[j]; j++) + if (child.nodeType == 1 && child.tagName != '!') results.push(child); + } + return results; + }, + + adjacent: function(nodes) { + for (var i = 0, results = [], node; node = nodes[i]; i++) { + var next = this.nextElementSibling(node); + if (next) results.push(next); + } + return results; + }, + + laterSibling: function(nodes) { + var h = Selector.handlers; + for (var i = 0, results = [], node; node = nodes[i]; i++) + h.concat(results, Element.nextSiblings(node)); + return results; + }, + + nextElementSibling: function(node) { + while (node = node.nextSibling) + if (node.nodeType == 1) return node; + return null; + }, + + previousElementSibling: function(node) { + while (node = node.previousSibling) + if (node.nodeType == 1) return node; + return null; + }, + + // TOKEN FUNCTIONS + tagName: function(nodes, root, tagName, combinator) { + tagName = tagName.toUpperCase(); + var results = [], h = Selector.handlers; + if (nodes) { + if (combinator) { + // fastlane for ordinary descendant combinators + if (combinator == "descendant") { + for (var i = 0, node; node = nodes[i]; i++) + h.concat(results, node.getElementsByTagName(tagName)); + return results; + } else nodes = this[combinator](nodes); + if (tagName == "*") return nodes; + } + for (var i = 0, node; node = nodes[i]; i++) + if (node.tagName.toUpperCase() == tagName) results.push(node); + return results; + } else return root.getElementsByTagName(tagName); + }, + + id: function(nodes, root, id, combinator) { + var targetNode = $(id), h = Selector.handlers; + if (!nodes && root == document) return targetNode ? [targetNode] : []; + if (nodes) { + if (combinator) { + if (combinator == 'child') { + for (var i = 0, node; node = nodes[i]; i++) + if (targetNode.parentNode == node) return [targetNode]; + } else if (combinator == 'descendant') { + for (var i = 0, node; node = nodes[i]; i++) + if (Element.descendantOf(targetNode, node)) return [targetNode]; + } else if (combinator == 'adjacent') { + for (var i = 0, node; node = nodes[i]; i++) + if (Selector.handlers.previousElementSibling(targetNode) == node) + return [targetNode]; + } else nodes = h[combinator](nodes); + } + for (var i = 0, node; node = nodes[i]; i++) + if (node == targetNode) return [targetNode]; + return []; + } + return (targetNode && Element.descendantOf(targetNode, root)) ? [targetNode] : []; + }, + + className: function(nodes, root, className, combinator) { + if (nodes && combinator) nodes = this[combinator](nodes); + return Selector.handlers.byClassName(nodes, root, className); + }, + + byClassName: function(nodes, root, className) { + if (!nodes) nodes = Selector.handlers.descendant([root]); + var needle = ' ' + className + ' '; + for (var i = 0, results = [], node, nodeClassName; node = nodes[i]; i++) { + nodeClassName = node.className; + if (nodeClassName.length == 0) continue; + if (nodeClassName == className || (' ' + nodeClassName + ' ').include(needle)) + results.push(node); + } + return results; + }, + + attrPresence: function(nodes, root, attr) { + var results = []; + for (var i = 0, node; node = nodes[i]; i++) + if (Element.hasAttribute(node, attr)) results.push(node); + return results; + }, + + attr: function(nodes, root, attr, value, operator) { + if (!nodes) nodes = root.getElementsByTagName("*"); + var handler = Selector.operators[operator], results = []; + for (var i = 0, node; node = nodes[i]; i++) { + var nodeValue = Element.readAttribute(node, attr); + if (nodeValue === null) continue; + if (handler(nodeValue, value)) results.push(node); + } + return results; + }, + + pseudo: function(nodes, name, value, root, combinator) { + if (nodes && combinator) nodes = this[combinator](nodes); + if (!nodes) nodes = root.getElementsByTagName("*"); + return Selector.pseudos[name](nodes, value, root); + } + }, + + pseudos: { + 'first-child': function(nodes, value, root) { + for (var i = 0, results = [], node; node = nodes[i]; i++) { + if (Selector.handlers.previousElementSibling(node)) continue; + results.push(node); + } + return results; + }, + 'last-child': function(nodes, value, root) { + for (var i = 0, results = [], node; node = nodes[i]; i++) { + if (Selector.handlers.nextElementSibling(node)) continue; + results.push(node); + } + return results; + }, + 'only-child': function(nodes, value, root) { + var h = Selector.handlers; + for (var i = 0, results = [], node; node = nodes[i]; i++) + if (!h.previousElementSibling(node) && !h.nextElementSibling(node)) + results.push(node); + return results; + }, + 'nth-child': function(nodes, formula, root) { + return Selector.pseudos.nth(nodes, formula, root); + }, + 'nth-last-child': function(nodes, formula, root) { + return Selector.pseudos.nth(nodes, formula, root, true); + }, + 'nth-of-type': function(nodes, formula, root) { + return Selector.pseudos.nth(nodes, formula, root, false, true); + }, + 'nth-last-of-type': function(nodes, formula, root) { + return Selector.pseudos.nth(nodes, formula, root, true, true); + }, + 'first-of-type': function(nodes, formula, root) { + return Selector.pseudos.nth(nodes, "1", root, false, true); + }, + 'last-of-type': function(nodes, formula, root) { + return Selector.pseudos.nth(nodes, "1", root, true, true); + }, + 'only-of-type': function(nodes, formula, root) { + var p = Selector.pseudos; + return p['last-of-type'](p['first-of-type'](nodes, formula, root), formula, root); + }, + + // handles the an+b logic + getIndices: function(a, b, total) { + if (a == 0) return b > 0 ? [b] : []; + return $R(1, total).inject([], function(memo, i) { + if (0 == (i - b) % a && (i - b) / a >= 0) memo.push(i); + return memo; + }); + }, + + // handles nth(-last)-child, nth(-last)-of-type, and (first|last)-of-type + nth: function(nodes, formula, root, reverse, ofType) { + if (nodes.length == 0) return []; + if (formula == 'even') formula = '2n+0'; + if (formula == 'odd') formula = '2n+1'; + var h = Selector.handlers, results = [], indexed = [], m; + h.mark(nodes); + for (var i = 0, node; node = nodes[i]; i++) { + if (!node.parentNode._counted) { + h.index(node.parentNode, reverse, ofType); + indexed.push(node.parentNode); + } + } + if (formula.match(/^\d+$/)) { // just a number + formula = Number(formula); + for (var i = 0, node; node = nodes[i]; i++) + if (node.nodeIndex == formula) results.push(node); + } else if (m = formula.match(/^(-?\d*)?n(([+-])(\d+))?/)) { // an+b + if (m[1] == "-") m[1] = -1; + var a = m[1] ? Number(m[1]) : 1; + var b = m[2] ? Number(m[2]) : 0; + var indices = Selector.pseudos.getIndices(a, b, nodes.length); + for (var i = 0, node, l = indices.length; node = nodes[i]; i++) { + for (var j = 0; j < l; j++) + if (node.nodeIndex == indices[j]) results.push(node); + } + } + h.unmark(nodes); + h.unmark(indexed); + return results; + }, + + 'empty': function(nodes, value, root) { + for (var i = 0, results = [], node; node = nodes[i]; i++) { + // IE treats comments as element nodes + if (node.tagName == '!' || (node.firstChild && !node.innerHTML.match(/^\s*$/))) continue; + results.push(node); + } + return results; + }, + + 'not': function(nodes, selector, root) { + var h = Selector.handlers, selectorType, m; + var exclusions = new Selector(selector).findElements(root); + h.mark(exclusions); + for (var i = 0, results = [], node; node = nodes[i]; i++) + if (!node._counted) results.push(node); + h.unmark(exclusions); + return results; + }, + + 'enabled': function(nodes, value, root) { + for (var i = 0, results = [], node; node = nodes[i]; i++) + if (!node.disabled) results.push(node); + return results; + }, + + 'disabled': function(nodes, value, root) { + for (var i = 0, results = [], node; node = nodes[i]; i++) + if (node.disabled) results.push(node); + return results; + }, + + 'checked': function(nodes, value, root) { + for (var i = 0, results = [], node; node = nodes[i]; i++) + if (node.checked) results.push(node); + return results; + } + }, + + operators: { + '=': function(nv, v) { return nv == v; }, + '!=': function(nv, v) { return nv != v; }, + '^=': function(nv, v) { return nv.startsWith(v); }, + '$=': function(nv, v) { return nv.endsWith(v); }, + '*=': function(nv, v) { return nv.include(v); }, + '~=': function(nv, v) { return (' ' + nv + ' ').include(' ' + v + ' '); }, + '|=': function(nv, v) { return ('-' + nv.toUpperCase() + '-').include('-' + v.toUpperCase() + '-'); } + }, + + matchElements: function(elements, expression) { + var matches = new Selector(expression).findElements(), h = Selector.handlers; + h.mark(matches); + for (var i = 0, results = [], element; element = elements[i]; i++) + if (element._counted) results.push(element); + h.unmark(matches); + return results; + }, + + findElement: function(elements, expression, index) { + if (typeof expression == 'number') { + index = expression; expression = false; + } + return Selector.matchElements(elements, expression || '*')[index || 0]; + }, + + findChildElements: function(element, expressions) { + var exprs = expressions.join(','), expressions = []; + exprs.scan(/(([\w#:.~>+()\s-]+|\*|\[.*?\])+)\s*(,|$)/, function(m) { + expressions.push(m[1].strip()); + }); + var results = [], h = Selector.handlers; + for (var i = 0, l = expressions.length, selector; i < l; i++) { + selector = new Selector(expressions[i].strip()); + h.concat(results, selector.findElements(element)); + } + return (l > 1) ? h.unique(results) : results; + } +}); + +function $$() { + return Selector.findChildElements(document, $A(arguments)); +} +var Form = { + reset: function(form) { + $(form).reset(); + return form; + }, + + serializeElements: function(elements, getHash) { + var data = elements.inject({}, function(result, element) { + if (!element.disabled && element.name) { + var key = element.name, value = $(element).getValue(); + if (value != null) { + if (key in result) { + if (result[key].constructor != Array) result[key] = [result[key]]; + result[key].push(value); + } + else result[key] = value; + } + } + return result; + }); + + return getHash ? data : Hash.toQueryString(data); + } +}; + +Form.Methods = { + serialize: function(form, getHash) { + return Form.serializeElements(Form.getElements(form), getHash); + }, + + getElements: function(form) { + return $A($(form).getElementsByTagName('*')).inject([], + function(elements, child) { + if (Form.Element.Serializers[child.tagName.toLowerCase()]) + elements.push(Element.extend(child)); + return elements; + } + ); + }, + + getInputs: function(form, typeName, name) { + form = $(form); + var inputs = form.getElementsByTagName('input'); + + if (!typeName && !name) return $A(inputs).map(Element.extend); + + for (var i = 0, matchingInputs = [], length = inputs.length; i < length; i++) { + var input = inputs[i]; + if ((typeName && input.type != typeName) || (name && input.name != name)) + continue; + matchingInputs.push(Element.extend(input)); + } + + return matchingInputs; + }, + + disable: function(form) { + form = $(form); + Form.getElements(form).invoke('disable'); + return form; + }, + + enable: function(form) { + form = $(form); + Form.getElements(form).invoke('enable'); + return form; + }, + + findFirstElement: function(form) { + return $(form).getElements().find(function(element) { + return element.type != 'hidden' && !element.disabled && + ['input', 'select', 'textarea'].include(element.tagName.toLowerCase()); + }); + }, + + focusFirstElement: function(form) { + form = $(form); + form.findFirstElement().activate(); + return form; + }, + + request: function(form, options) { + form = $(form), options = Object.clone(options || {}); + + var params = options.parameters; + options.parameters = form.serialize(true); + + if (params) { + if (typeof params == 'string') params = params.toQueryParams(); + Object.extend(options.parameters, params); + } + + if (form.hasAttribute('method') && !options.method) + options.method = form.method; + + return new Ajax.Request(form.readAttribute('action'), options); + } +} + +/*--------------------------------------------------------------------------*/ + +Form.Element = { + focus: function(element) { + $(element).focus(); + return element; + }, + + select: function(element) { + $(element).select(); + return element; + } +} + +Form.Element.Methods = { + serialize: function(element) { + element = $(element); + if (!element.disabled && element.name) { + var value = element.getValue(); + if (value != undefined) { + var pair = {}; + pair[element.name] = value; + return Hash.toQueryString(pair); + } + } + return ''; + }, + + getValue: function(element) { + element = $(element); + var method = element.tagName.toLowerCase(); + return Form.Element.Serializers[method](element); + }, + + clear: function(element) { + $(element).value = ''; + return element; + }, + + present: function(element) { + return $(element).value != ''; + }, + + activate: function(element) { + element = $(element); + try { + element.focus(); + if (element.select && (element.tagName.toLowerCase() != 'input' || + !['button', 'reset', 'submit'].include(element.type))) + element.select(); + } catch (e) {} + return element; + }, + + disable: function(element) { + element = $(element); + element.blur(); + element.disabled = true; + return element; + }, + + enable: function(element) { + element = $(element); + element.disabled = false; + return element; + } +} + +/*--------------------------------------------------------------------------*/ + +var Field = Form.Element; +var $F = Form.Element.Methods.getValue; + +/*--------------------------------------------------------------------------*/ + +Form.Element.Serializers = { + input: function(element) { + switch (element.type.toLowerCase()) { + case 'checkbox': + case 'radio': + return Form.Element.Serializers.inputSelector(element); + default: + return Form.Element.Serializers.textarea(element); + } + }, + + inputSelector: function(element) { + return element.checked ? element.value : null; + }, + + textarea: function(element) { + return element.value; + }, + + select: function(element) { + return this[element.type == 'select-one' ? + 'selectOne' : 'selectMany'](element); + }, + + selectOne: function(element) { + var index = element.selectedIndex; + return index >= 0 ? this.optionValue(element.options[index]) : null; + }, + + selectMany: function(element) { + var values, length = element.length; + if (!length) return null; + + for (var i = 0, values = []; i < length; i++) { + var opt = element.options[i]; + if (opt.selected) values.push(this.optionValue(opt)); + } + return values; + }, + + optionValue: function(opt) { + // extend element because hasAttribute may not be native + return Element.extend(opt).hasAttribute('value') ? opt.value : opt.text; + } +} + +/*--------------------------------------------------------------------------*/ + +Abstract.TimedObserver = function() {} +Abstract.TimedObserver.prototype = { + initialize: function(element, frequency, callback) { + this.frequency = frequency; + this.element = $(element); + this.callback = callback; + + this.lastValue = this.getValue(); + this.registerCallback(); + }, + + registerCallback: function() { + setInterval(this.onTimerEvent.bind(this), this.frequency * 1000); + }, + + onTimerEvent: function() { + var value = this.getValue(); + var changed = ('string' == typeof this.lastValue && 'string' == typeof value + ? this.lastValue != value : String(this.lastValue) != String(value)); + if (changed) { + this.callback(this.element, value); + this.lastValue = value; + } + } +} + +Form.Element.Observer = Class.create(); +Form.Element.Observer.prototype = Object.extend(new Abstract.TimedObserver(), { + getValue: function() { + return Form.Element.getValue(this.element); + } +}); + +Form.Observer = Class.create(); +Form.Observer.prototype = Object.extend(new Abstract.TimedObserver(), { + getValue: function() { + return Form.serialize(this.element); + } +}); + +/*--------------------------------------------------------------------------*/ + +Abstract.EventObserver = function() {} +Abstract.EventObserver.prototype = { + initialize: function(element, callback) { + this.element = $(element); + this.callback = callback; + + this.lastValue = this.getValue(); + if (this.element.tagName.toLowerCase() == 'form') + this.registerFormCallbacks(); + else + this.registerCallback(this.element); + }, + + onElementEvent: function() { + var value = this.getValue(); + if (this.lastValue != value) { + this.callback(this.element, value); + this.lastValue = value; + } + }, + + registerFormCallbacks: function() { + Form.getElements(this.element).each(this.registerCallback.bind(this)); + }, + + registerCallback: function(element) { + if (element.type) { + switch (element.type.toLowerCase()) { + case 'checkbox': + case 'radio': + Event.observe(element, 'click', this.onElementEvent.bind(this)); + break; + default: + Event.observe(element, 'change', this.onElementEvent.bind(this)); + break; + } + } + } +} + +Form.Element.EventObserver = Class.create(); +Form.Element.EventObserver.prototype = Object.extend(new Abstract.EventObserver(), { + getValue: function() { + return Form.Element.getValue(this.element); + } +}); + +Form.EventObserver = Class.create(); +Form.EventObserver.prototype = Object.extend(new Abstract.EventObserver(), { + getValue: function() { + return Form.serialize(this.element); + } +}); +if (!window.Event) { + var Event = new Object(); +} + +Object.extend(Event, { + KEY_BACKSPACE: 8, + KEY_TAB: 9, + KEY_RETURN: 13, + KEY_ESC: 27, + KEY_LEFT: 37, + KEY_UP: 38, + KEY_RIGHT: 39, + KEY_DOWN: 40, + KEY_DELETE: 46, + KEY_HOME: 36, + KEY_END: 35, + KEY_PAGEUP: 33, + KEY_PAGEDOWN: 34, + + element: function(event) { + return $(event.target || event.srcElement); + }, + + isLeftClick: function(event) { + return (((event.which) && (event.which == 1)) || + ((event.button) && (event.button == 1))); + }, + + pointerX: function(event) { + return event.pageX || (event.clientX + + (document.documentElement.scrollLeft || document.body.scrollLeft)); + }, + + pointerY: function(event) { + return event.pageY || (event.clientY + + (document.documentElement.scrollTop || document.body.scrollTop)); + }, + + stop: function(event) { + if (event.preventDefault) { + event.preventDefault(); + event.stopPropagation(); + } else { + event.returnValue = false; + event.cancelBubble = true; + } + }, + + // find the first node with the given tagName, starting from the + // node the event was triggered on; traverses the DOM upwards + findElement: function(event, tagName) { + var element = Event.element(event); + while (element.parentNode && (!element.tagName || + (element.tagName.toUpperCase() != tagName.toUpperCase()))) + element = element.parentNode; + return element; + }, + + observers: false, + + _observeAndCache: function(element, name, observer, useCapture) { + if (!this.observers) this.observers = []; + if (element.addEventListener) { + this.observers.push([element, name, observer, useCapture]); + element.addEventListener(name, observer, useCapture); + } else if (element.attachEvent) { + this.observers.push([element, name, observer, useCapture]); + element.attachEvent('on' + name, observer); + } + }, + + unloadCache: function() { + if (!Event.observers) return; + for (var i = 0, length = Event.observers.length; i < length; i++) { + Event.stopObserving.apply(this, Event.observers[i]); + Event.observers[i][0] = null; + } + Event.observers = false; + }, + + observe: function(element, name, observer, useCapture) { + element = $(element); + useCapture = useCapture || false; + + if (name == 'keypress' && + (Prototype.Browser.WebKit || element.attachEvent)) + name = 'keydown'; + + Event._observeAndCache(element, name, observer, useCapture); + }, + + stopObserving: function(element, name, observer, useCapture) { + element = $(element); + useCapture = useCapture || false; + + if (name == 'keypress' && + (Prototype.Browser.WebKit || element.attachEvent)) + name = 'keydown'; + + if (element.removeEventListener) { + element.removeEventListener(name, observer, useCapture); + } else if (element.detachEvent) { + try { + element.detachEvent('on' + name, observer); + } catch (e) {} + } + } +}); + +/* prevent memory leaks in IE */ +if (Prototype.Browser.IE) + Event.observe(window, 'unload', Event.unloadCache, false); +var Position = { + // set to true if needed, warning: firefox performance problems + // NOT neeeded for page scrolling, only if draggable contained in + // scrollable elements + includeScrollOffsets: false, + + // must be called before calling withinIncludingScrolloffset, every time the + // page is scrolled + prepare: function() { + this.deltaX = window.pageXOffset + || document.documentElement.scrollLeft + || document.body.scrollLeft + || 0; + this.deltaY = window.pageYOffset + || document.documentElement.scrollTop + || document.body.scrollTop + || 0; + }, + + realOffset: function(element) { + var valueT = 0, valueL = 0; + do { + valueT += element.scrollTop || 0; + valueL += element.scrollLeft || 0; + element = element.parentNode; + } while (element); + return [valueL, valueT]; + }, + + cumulativeOffset: function(element) { + var valueT = 0, valueL = 0; + do { + valueT += element.offsetTop || 0; + valueL += element.offsetLeft || 0; + element = element.offsetParent; + } while (element); + return [valueL, valueT]; + }, + + positionedOffset: function(element) { + var valueT = 0, valueL = 0; + do { + valueT += element.offsetTop || 0; + valueL += element.offsetLeft || 0; + element = element.offsetParent; + if (element) { + if(element.tagName=='BODY') break; + var p = Element.getStyle(element, 'position'); + if (p == 'relative' || p == 'absolute') break; + } + } while (element); + return [valueL, valueT]; + }, + + offsetParent: function(element) { + if (element.offsetParent) return element.offsetParent; + if (element == document.body) return element; + + while ((element = element.parentNode) && element != document.body) + if (Element.getStyle(element, 'position') != 'static') + return element; + + return document.body; + }, + + // caches x/y coordinate pair to use with overlap + within: function(element, x, y) { + if (this.includeScrollOffsets) + return this.withinIncludingScrolloffsets(element, x, y); + this.xcomp = x; + this.ycomp = y; + this.offset = this.cumulativeOffset(element); + + return (y >= this.offset[1] && + y < this.offset[1] + element.offsetHeight && + x >= this.offset[0] && + x < this.offset[0] + element.offsetWidth); + }, + + withinIncludingScrolloffsets: function(element, x, y) { + var offsetcache = this.realOffset(element); + + this.xcomp = x + offsetcache[0] - this.deltaX; + this.ycomp = y + offsetcache[1] - this.deltaY; + this.offset = this.cumulativeOffset(element); + + return (this.ycomp >= this.offset[1] && + this.ycomp < this.offset[1] + element.offsetHeight && + this.xcomp >= this.offset[0] && + this.xcomp < this.offset[0] + element.offsetWidth); + }, + + // within must be called directly before + overlap: function(mode, element) { + if (!mode) return 0; + if (mode == 'vertical') + return ((this.offset[1] + element.offsetHeight) - this.ycomp) / + element.offsetHeight; + if (mode == 'horizontal') + return ((this.offset[0] + element.offsetWidth) - this.xcomp) / + element.offsetWidth; + }, + + page: function(forElement) { + var valueT = 0, valueL = 0; + + var element = forElement; + do { + valueT += element.offsetTop || 0; + valueL += element.offsetLeft || 0; + + // Safari fix + if (element.offsetParent == document.body) + if (Element.getStyle(element,'position')=='absolute') break; + + } while (element = element.offsetParent); + + element = forElement; + do { + if (!window.opera || element.tagName=='BODY') { + valueT -= element.scrollTop || 0; + valueL -= element.scrollLeft || 0; + } + } while (element = element.parentNode); + + return [valueL, valueT]; + }, + + clone: function(source, target) { + var options = Object.extend({ + setLeft: true, + setTop: true, + setWidth: true, + setHeight: true, + offsetTop: 0, + offsetLeft: 0 + }, arguments[2] || {}) + + // find page position of source + source = $(source); + var p = Position.page(source); + + // find coordinate system to use + target = $(target); + var delta = [0, 0]; + var parent = null; + // delta [0,0] will do fine with position: fixed elements, + // position:absolute needs offsetParent deltas + if (Element.getStyle(target,'position') == 'absolute') { + parent = Position.offsetParent(target); + delta = Position.page(parent); + } + + // correct by body offsets (fixes Safari) + if (parent == document.body) { + delta[0] -= document.body.offsetLeft; + delta[1] -= document.body.offsetTop; + } + + // set position + if(options.setLeft) target.style.left = (p[0] - delta[0] + options.offsetLeft) + 'px'; + if(options.setTop) target.style.top = (p[1] - delta[1] + options.offsetTop) + 'px'; + if(options.setWidth) target.style.width = source.offsetWidth + 'px'; + if(options.setHeight) target.style.height = source.offsetHeight + 'px'; + }, + + absolutize: function(element) { + element = $(element); + if (element.style.position == 'absolute') return; + Position.prepare(); + + var offsets = Position.positionedOffset(element); + var top = offsets[1]; + var left = offsets[0]; + var width = element.clientWidth; + var height = element.clientHeight; + + element._originalLeft = left - parseFloat(element.style.left || 0); + element._originalTop = top - parseFloat(element.style.top || 0); + element._originalWidth = element.style.width; + element._originalHeight = element.style.height; + + element.style.position = 'absolute'; + element.style.top = top + 'px'; + element.style.left = left + 'px'; + element.style.width = width + 'px'; + element.style.height = height + 'px'; + }, + + relativize: function(element) { + element = $(element); + if (element.style.position == 'relative') return; + Position.prepare(); + + element.style.position = 'relative'; + var top = parseFloat(element.style.top || 0) - (element._originalTop || 0); + var left = parseFloat(element.style.left || 0) - (element._originalLeft || 0); + + element.style.top = top + 'px'; + element.style.left = left + 'px'; + element.style.height = element._originalHeight; + element.style.width = element._originalWidth; + } +} + +// Safari returns margins on body which is incorrect if the child is absolutely +// positioned. For performance reasons, redefine Position.cumulativeOffset for +// KHTML/WebKit only. +if (Prototype.Browser.WebKit) { + Position.cumulativeOffset = function(element) { + var valueT = 0, valueL = 0; + do { + valueT += element.offsetTop || 0; + valueL += element.offsetLeft || 0; + if (element.offsetParent == document.body) + if (Element.getStyle(element, 'position') == 'absolute') break; + + element = element.offsetParent; + } while (element); + + return [valueL, valueT]; + } +} + +Element.addMethods();
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/scriptaculous/manifest.json b/dom/tests/mochitest/ajax/scriptaculous/manifest.json new file mode 100644 index 0000000000..b0f6322df9 --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/manifest.json @@ -0,0 +1 @@ +{testcases:["test/unit/ajax_inplaceeditor_test.html","test/unit/builder_test.html","test/unit/dragdrop_test.html","test/unit/effects_test.html","test/unit/element_test.html","test/unit/loading_test.html","test/unit/position_clone_test.html","test/unit/sortable_test.html","test/unit/string_test.html","test/unit/unit_test.html"]} diff --git a/dom/tests/mochitest/ajax/scriptaculous/mochitest.ini b/dom/tests/mochitest/ajax/scriptaculous/mochitest.ini new file mode 100644 index 0000000000..66a19ed2c3 --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/mochitest.ini @@ -0,0 +1,33 @@ +[DEFAULT] +support-files = + manifest.json + lib/prototype.js + src/builder.js + src/controls.js + src/dragdrop.js + src/effects.js + src/scriptaculous.js + src/slider.js + src/sound.js + src/unittest.js + test/unit/_ajax_inplaceeditor_result.html + test/unit/_ajax_inplaceeditor_text.html + test/unit/_ajax_updater_result.html + test/unit/_autocomplete_result.html + test/unit/_autocomplete_result_nobr.html + test/unit/ajax_autocompleter_test.html + test/unit/ajax_inplaceeditor_test.html + test/unit/bdd_test.html + test/unit/builder_test.html + test/unit/dragdrop_test.html + test/unit/effects_test.html + test/unit/element_test.html + test/unit/icon.png + test/unit/loading_test.html + test/unit/position_clone_test.html + test/unit/slider_test.html + test/unit/sortable_test.html + test/unit/string_test.html + test/unit/unit_test.html + +[test_Scriptaculous.html] diff --git a/dom/tests/mochitest/ajax/scriptaculous/src/builder.js b/dom/tests/mochitest/ajax/scriptaculous/src/builder.js new file mode 100644 index 0000000000..0cf631b19d --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/src/builder.js @@ -0,0 +1,136 @@ +// script.aculo.us builder.js v1.7.1_beta2, Tue May 15 15:15:45 EDT 2007 + +// Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) +// +// script.aculo.us is freely distributable under the terms of an MIT-style license. +// For details, see the script.aculo.us web site: http://script.aculo.us/ + +var Builder = { + NODEMAP: { + AREA: 'map', + CAPTION: 'table', + COL: 'table', + COLGROUP: 'table', + LEGEND: 'fieldset', + OPTGROUP: 'select', + OPTION: 'select', + PARAM: 'object', + TBODY: 'table', + TD: 'table', + TFOOT: 'table', + TH: 'table', + THEAD: 'table', + TR: 'table' + }, + // note: For Firefox < 1.5, OPTION and OPTGROUP tags are currently broken, + // due to a Firefox bug + node: function(elementName) { + elementName = elementName.toUpperCase(); + + // try innerHTML approach + var parentTag = this.NODEMAP[elementName] || 'div'; + var parentElement = document.createElement(parentTag); + try { // prevent IE "feature": http://dev.rubyonrails.org/ticket/2707 + parentElement.innerHTML = "<" + elementName + "></" + elementName + ">"; + } catch(e) {} + var element = parentElement.firstChild || null; + + // see if browser added wrapping tags + if(element && (element.tagName.toUpperCase() != elementName)) + element = element.getElementsByTagName(elementName)[0]; + + // fallback to createElement approach + if(!element) element = document.createElement(elementName); + + // abort if nothing could be created + if(!element) return; + + // attributes (or text) + if(arguments[1]) + if(this._isStringOrNumber(arguments[1]) || + (arguments[1] instanceof Array) || + arguments[1].tagName) { + this._children(element, arguments[1]); + } else { + var attrs = this._attributes(arguments[1]); + if(attrs.length) { + try { // prevent IE "feature": http://dev.rubyonrails.org/ticket/2707 + parentElement.innerHTML = "<" +elementName + " " + + attrs + "></" + elementName + ">"; + } catch(e) {} + element = parentElement.firstChild || null; + // workaround firefox 1.0.X bug + if(!element) { + element = document.createElement(elementName); + for(attr in arguments[1]) + element[attr == 'class' ? 'className' : attr] = arguments[1][attr]; + } + if(element.tagName.toUpperCase() != elementName) + element = parentElement.getElementsByTagName(elementName)[0]; + } + } + + // text, or array of children + if(arguments[2]) + this._children(element, arguments[2]); + + return element; + }, + _text: function(text) { + return document.createTextNode(text); + }, + + ATTR_MAP: { + 'className': 'class', + 'htmlFor': 'for' + }, + + _attributes: function(attributes) { + var attrs = []; + for(attribute in attributes) + attrs.push((attribute in this.ATTR_MAP ? this.ATTR_MAP[attribute] : attribute) + + '="' + attributes[attribute].toString().escapeHTML().gsub(/"/,'"') + '"'); + return attrs.join(" "); + }, + _children: function(element, children) { + if(children.tagName) { + element.appendChild(children); + return; + } + if(typeof children=='object') { // array can hold nodes and text + children.flatten().each( function(e) { + if(typeof e=='object') + element.appendChild(e) + else + if(Builder._isStringOrNumber(e)) + element.appendChild(Builder._text(e)); + }); + } else + if(Builder._isStringOrNumber(children)) + element.appendChild(Builder._text(children)); + }, + _isStringOrNumber: function(param) { + return(typeof param=='string' || typeof param=='number'); + }, + build: function(html) { + var element = this.node('div'); + $(element).update(html.strip()); + return element.down(); + }, + dump: function(scope) { + if(typeof scope != 'object' && typeof scope != 'function') scope = window; //global scope + + var tags = ("A ABBR ACRONYM ADDRESS APPLET AREA B BASE BASEFONT BDO BIG BLOCKQUOTE BODY " + + "BR BUTTON CAPTION CENTER CITE CODE COL COLGROUP DD DEL DFN DIR DIV DL DT EM FIELDSET " + + "FONT FORM FRAME FRAMESET H1 H2 H3 H4 H5 H6 HEAD HR HTML I IFRAME IMG INPUT INS ISINDEX "+ + "KBD LABEL LEGEND LI LINK MAP MENU META NOFRAMES NOSCRIPT OBJECT OL OPTGROUP OPTION P "+ + "PARAM PRE Q S SAMP SCRIPT SELECT SMALL SPAN STRIKE STRONG STYLE SUB SUP TABLE TBODY TD "+ + "TEXTAREA TFOOT TH THEAD TITLE TR TT U UL VAR").split(/\s+/); + + tags.each( function(tag){ + scope[tag] = function() { + return Builder.node.apply(Builder, [tag].concat($A(arguments))); + } + }); + } +} diff --git a/dom/tests/mochitest/ajax/scriptaculous/src/controls.js b/dom/tests/mochitest/ajax/scriptaculous/src/controls.js new file mode 100644 index 0000000000..7d4f16ece0 --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/src/controls.js @@ -0,0 +1,875 @@ +// script.aculo.us controls.js v1.7.1_beta2, Tue May 15 15:15:45 EDT 2007 + +// Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) +// (c) 2005-2007 Ivan Krstic (http://blogs.law.harvard.edu/ivan) +// (c) 2005-2007 Jon Tirsen (http://www.tirsen.com) +// Contributors: +// Richard Livsey +// Rahul Bhargava +// Rob Wills +// +// script.aculo.us is freely distributable under the terms of an MIT-style license. +// For details, see the script.aculo.us web site: http://script.aculo.us/ + +// Autocompleter.Base handles all the autocompletion functionality +// that's independent of the data source for autocompletion. This +// includes drawing the autocompletion menu, observing keyboard +// and mouse events, and similar. +// +// Specific autocompleters need to provide, at the very least, +// a getUpdatedChoices function that will be invoked every time +// the text inside the monitored textbox changes. This method +// should get the text for which to provide autocompletion by +// invoking this.getToken(), NOT by directly accessing +// this.element.value. This is to allow incremental tokenized +// autocompletion. Specific auto-completion logic (AJAX, etc) +// belongs in getUpdatedChoices. +// +// Tokenized incremental autocompletion is enabled automatically +// when an autocompleter is instantiated with the 'tokens' option +// in the options parameter, e.g.: +// new Ajax.Autocompleter('id','upd', '/url/', { tokens: ',' }); +// will incrementally autocomplete with a comma as the token. +// Additionally, ',' in the above example can be replaced with +// a token array, e.g. { tokens: [',', '\n'] } which +// enables autocompletion on multiple tokens. This is most +// useful when one of the tokens is \n (a newline), as it +// allows smart autocompletion after linebreaks. + +if(typeof Effect == 'undefined') + throw("controls.js requires including script.aculo.us' effects.js library"); + +var Autocompleter = {} +Autocompleter.Base = function() {}; +Autocompleter.Base.prototype = { + baseInitialize: function(element, update, options) { + element = $(element) + this.element = element; + this.update = $(update); + this.hasFocus = false; + this.changed = false; + this.active = false; + this.index = 0; + this.entryCount = 0; + + if(this.setOptions) + this.setOptions(options); + else + this.options = options || {}; + + this.options.paramName = this.options.paramName || this.element.name; + this.options.tokens = this.options.tokens || []; + this.options.frequency = this.options.frequency || 0.4; + this.options.minChars = this.options.minChars || 1; + this.options.onShow = this.options.onShow || + function(element, update){ + if(!update.style.position || update.style.position=='absolute') { + update.style.position = 'absolute'; + Position.clone(element, update, { + setHeight: false, + offsetTop: element.offsetHeight + }); + } + Effect.Appear(update,{duration:0.15}); + }; + this.options.onHide = this.options.onHide || + function(element, update){ new Effect.Fade(update,{duration:0.15}) }; + + if(typeof(this.options.tokens) == 'string') + this.options.tokens = new Array(this.options.tokens); + + this.observer = null; + + this.element.setAttribute('autocomplete','off'); + + Element.hide(this.update); + + Event.observe(this.element, 'blur', this.onBlur.bindAsEventListener(this)); + Event.observe(this.element, 'keypress', this.onKeyPress.bindAsEventListener(this)); + + // Turn autocomplete back on when the user leaves the page, so that the + // field's value will be remembered on Mozilla-based browsers. + Event.observe(window, 'beforeunload', function(){ + element.setAttribute('autocomplete', 'on'); + }); + }, + + show: function() { + if(Element.getStyle(this.update, 'display')=='none') this.options.onShow(this.element, this.update); + if(!this.iefix && + (Prototype.Browser.IE) && + (Element.getStyle(this.update, 'position')=='absolute')) { + new Insertion.After(this.update, + '<iframe id="' + this.update.id + '_iefix" '+ + 'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' + + 'src="javascript:false;" frameborder="0" scrolling="no"></iframe>'); + this.iefix = $(this.update.id+'_iefix'); + } + if(this.iefix) setTimeout(this.fixIEOverlapping.bind(this), 50); + }, + + fixIEOverlapping: function() { + Position.clone(this.update, this.iefix, {setTop:(!this.update.style.height)}); + this.iefix.style.zIndex = 1; + this.update.style.zIndex = 2; + Element.show(this.iefix); + }, + + hide: function() { + this.stopIndicator(); + if(Element.getStyle(this.update, 'display')!='none') this.options.onHide(this.element, this.update); + if(this.iefix) Element.hide(this.iefix); + }, + + startIndicator: function() { + if(this.options.indicator) Element.show(this.options.indicator); + }, + + stopIndicator: function() { + if(this.options.indicator) Element.hide(this.options.indicator); + }, + + onKeyPress: function(event) { + if(this.active) + switch(event.keyCode) { + case Event.KEY_TAB: + case Event.KEY_RETURN: + this.selectEntry(); + Event.stop(event); + case Event.KEY_ESC: + this.hide(); + this.active = false; + Event.stop(event); + return; + case Event.KEY_LEFT: + case Event.KEY_RIGHT: + return; + case Event.KEY_UP: + this.markPrevious(); + this.render(); + if(Prototype.Browser.WebKit) Event.stop(event); + return; + case Event.KEY_DOWN: + this.markNext(); + this.render(); + if(Prototype.Browser.WebKit) Event.stop(event); + return; + } + else + if(event.keyCode==Event.KEY_TAB || event.keyCode==Event.KEY_RETURN || + (Prototype.Browser.WebKit > 0 && event.keyCode == 0)) return; + + this.changed = true; + this.hasFocus = true; + + if(this.observer) clearTimeout(this.observer); + this.observer = + setTimeout(this.onObserverEvent.bind(this), this.options.frequency*1000); + }, + + activate: function() { + this.changed = false; + this.hasFocus = true; + this.getUpdatedChoices(); + }, + + onHover: function(event) { + var element = Event.findElement(event, 'LI'); + if(this.index != element.autocompleteIndex) + { + this.index = element.autocompleteIndex; + this.render(); + } + Event.stop(event); + }, + + onClick: function(event) { + var element = Event.findElement(event, 'LI'); + this.index = element.autocompleteIndex; + this.selectEntry(); + this.hide(); + }, + + onBlur: function(event) { + // needed to make click events working + setTimeout(this.hide.bind(this), 250); + this.hasFocus = false; + this.active = false; + }, + + render: function() { + if(this.entryCount > 0) { + for (var i = 0; i < this.entryCount; i++) + this.index==i ? + Element.addClassName(this.getEntry(i),"selected") : + Element.removeClassName(this.getEntry(i),"selected"); + if(this.hasFocus) { + this.show(); + this.active = true; + } + } else { + this.active = false; + this.hide(); + } + }, + + markPrevious: function() { + if(this.index > 0) this.index-- + else this.index = this.entryCount-1; + this.getEntry(this.index).scrollIntoView(true); + }, + + markNext: function() { + if(this.index < this.entryCount-1) this.index++ + else this.index = 0; + this.getEntry(this.index).scrollIntoView(false); + }, + + getEntry: function(index) { + return this.update.firstChild.childNodes[index]; + }, + + getCurrentEntry: function() { + return this.getEntry(this.index); + }, + + selectEntry: function() { + this.active = false; + this.updateElement(this.getCurrentEntry()); + }, + + updateElement: function(selectedElement) { + if (this.options.updateElement) { + this.options.updateElement(selectedElement); + return; + } + var value = ''; + if (this.options.select) { + var nodes = document.getElementsByClassName(this.options.select, selectedElement) || []; + if(nodes.length>0) value = Element.collectTextNodes(nodes[0], this.options.select); + } else + value = Element.collectTextNodesIgnoreClass(selectedElement, 'informal'); + + var lastTokenPos = this.findLastToken(); + if (lastTokenPos != -1) { + var newValue = this.element.value.substr(0, lastTokenPos + 1); + var whitespace = this.element.value.substr(lastTokenPos + 1).match(/^\s+/); + if (whitespace) + newValue += whitespace[0]; + this.element.value = newValue + value; + } else { + this.element.value = value; + } + this.element.focus(); + + if (this.options.afterUpdateElement) + this.options.afterUpdateElement(this.element, selectedElement); + }, + + updateChoices: function(choices) { + if(!this.changed && this.hasFocus) { + this.update.innerHTML = choices; + Element.cleanWhitespace(this.update); + Element.cleanWhitespace(this.update.down()); + + if(this.update.firstChild && this.update.down().childNodes) { + this.entryCount = + this.update.down().childNodes.length; + for (var i = 0; i < this.entryCount; i++) { + var entry = this.getEntry(i); + entry.autocompleteIndex = i; + this.addObservers(entry); + } + } else { + this.entryCount = 0; + } + + this.stopIndicator(); + this.index = 0; + + if(this.entryCount==1 && this.options.autoSelect) { + this.selectEntry(); + this.hide(); + } else { + this.render(); + } + } + }, + + addObservers: function(element) { + Event.observe(element, "mouseover", this.onHover.bindAsEventListener(this)); + Event.observe(element, "click", this.onClick.bindAsEventListener(this)); + }, + + onObserverEvent: function() { + this.changed = false; + if(this.getToken().length>=this.options.minChars) { + this.getUpdatedChoices(); + } else { + this.active = false; + this.hide(); + } + }, + + getToken: function() { + var tokenPos = this.findLastToken(); + if (tokenPos != -1) + var ret = this.element.value.substr(tokenPos + 1).replace(/^\s+/,'').replace(/\s+$/,''); + else + var ret = this.element.value; + + return /\n/.test(ret) ? '' : ret; + }, + + findLastToken: function() { + var lastTokenPos = -1; + + for (var i=0; i<this.options.tokens.length; i++) { + var thisTokenPos = this.element.value.lastIndexOf(this.options.tokens[i]); + if (thisTokenPos > lastTokenPos) + lastTokenPos = thisTokenPos; + } + return lastTokenPos; + } +} + +Ajax.Autocompleter = Class.create(); +Object.extend(Object.extend(Ajax.Autocompleter.prototype, Autocompleter.Base.prototype), { + initialize: function(element, update, url, options) { + this.baseInitialize(element, update, options); + this.options.asynchronous = true; + this.options.onComplete = this.onComplete.bind(this); + this.options.defaultParams = this.options.parameters || null; + this.url = url; + }, + + getUpdatedChoices: function() { + this.startIndicator(); + + var entry = encodeURIComponent(this.options.paramName) + '=' + + encodeURIComponent(this.getToken()); + + this.options.parameters = this.options.callback ? + this.options.callback(this.element, entry) : entry; + + if(this.options.defaultParams) + this.options.parameters += '&' + this.options.defaultParams; + + new Ajax.Request(this.url, this.options); + }, + + onComplete: function(request) { + this.updateChoices(request.responseText); + } + +}); + +// The local array autocompleter. Used when you'd prefer to +// inject an array of autocompletion options into the page, rather +// than sending out Ajax queries, which can be quite slow sometimes. +// +// The constructor takes four parameters. The first two are, as usual, +// the id of the monitored textbox, and id of the autocompletion menu. +// The third is the array you want to autocomplete from, and the fourth +// is the options block. +// +// Extra local autocompletion options: +// - choices - How many autocompletion choices to offer +// +// - partialSearch - If false, the autocompleter will match entered +// text only at the beginning of strings in the +// autocomplete array. Defaults to true, which will +// match text at the beginning of any *word* in the +// strings in the autocomplete array. If you want to +// search anywhere in the string, additionally set +// the option fullSearch to true (default: off). +// +// - fullSsearch - Search anywhere in autocomplete array strings. +// +// - partialChars - How many characters to enter before triggering +// a partial match (unlike minChars, which defines +// how many characters are required to do any match +// at all). Defaults to 2. +// +// - ignoreCase - Whether to ignore case when autocompleting. +// Defaults to true. +// +// It's possible to pass in a custom function as the 'selector' +// option, if you prefer to write your own autocompletion logic. +// In that case, the other options above will not apply unless +// you support them. + +Autocompleter.Local = Class.create(); +Autocompleter.Local.prototype = Object.extend(new Autocompleter.Base(), { + initialize: function(element, update, array, options) { + this.baseInitialize(element, update, options); + this.options.array = array; + }, + + getUpdatedChoices: function() { + this.updateChoices(this.options.selector(this)); + }, + + setOptions: function(options) { + this.options = Object.extend({ + choices: 10, + partialSearch: true, + partialChars: 2, + ignoreCase: true, + fullSearch: false, + selector: function(instance) { + var ret = []; // Beginning matches + var partial = []; // Inside matches + var entry = instance.getToken(); + var count = 0; + + for (var i = 0; i < instance.options.array.length && + ret.length < instance.options.choices ; i++) { + + var elem = instance.options.array[i]; + var foundPos = instance.options.ignoreCase ? + elem.toLowerCase().indexOf(entry.toLowerCase()) : + elem.indexOf(entry); + + while (foundPos != -1) { + if (foundPos == 0 && elem.length != entry.length) { + ret.push("<li><strong>" + elem.substr(0, entry.length) + "</strong>" + + elem.substr(entry.length) + "</li>"); + break; + } else if (entry.length >= instance.options.partialChars && + instance.options.partialSearch && foundPos != -1) { + if (instance.options.fullSearch || /\s/.test(elem.substr(foundPos-1,1))) { + partial.push("<li>" + elem.substr(0, foundPos) + "<strong>" + + elem.substr(foundPos, entry.length) + "</strong>" + elem.substr( + foundPos + entry.length) + "</li>"); + break; + } + } + + foundPos = instance.options.ignoreCase ? + elem.toLowerCase().indexOf(entry.toLowerCase(), foundPos + 1) : + elem.indexOf(entry, foundPos + 1); + + } + } + if (partial.length) + ret = ret.concat(partial.slice(0, instance.options.choices - ret.length)) + return "<ul>" + ret.join('') + "</ul>"; + } + }, options || {}); + } +}); + +// AJAX in-place editor +// +// see documentation on http://wiki.script.aculo.us/scriptaculous/show/Ajax.InPlaceEditor + +// Use this if you notice weird scrolling problems on some browsers, +// the DOM might be a bit confused when this gets called so do this +// waits 1 ms (with setTimeout) until it does the activation +Field.scrollFreeActivate = function(field) { + setTimeout(function() { + Field.activate(field); + }, 1); +} + +Ajax.InPlaceEditor = Class.create(); +Ajax.InPlaceEditor.defaultHighlightColor = "#FFFF99"; +Ajax.InPlaceEditor.prototype = { + initialize: function(element, url, options) { + this.url = url; + this.element = $(element); + + this.options = Object.extend({ + paramName: "value", + okButton: true, + okLink: false, + okText: "ok", + cancelButton: false, + cancelLink: true, + cancelText: "cancel", + textBeforeControls: '', + textBetweenControls: '', + textAfterControls: '', + savingText: "Saving...", + clickToEditText: "Click to edit", + okText: "ok", + rows: 1, + onComplete: function(transport, element) { + new Effect.Highlight(element, {startcolor: this.options.highlightcolor}); + }, + onFailure: function(transport) { + alert("Error communicating with the server: " + transport.responseText.stripTags()); + }, + callback: function(form) { + return Form.serialize(form); + }, + handleLineBreaks: true, + loadingText: 'Loading...', + savingClassName: 'inplaceeditor-saving', + loadingClassName: 'inplaceeditor-loading', + formClassName: 'inplaceeditor-form', + highlightcolor: Ajax.InPlaceEditor.defaultHighlightColor, + highlightendcolor: "#FFFFFF", + externalControl: null, + submitOnBlur: false, + ajaxOptions: {}, + evalScripts: false + }, options || {}); + + if(!this.options.formId && this.element.id) { + this.options.formId = this.element.id + "-inplaceeditor"; + if ($(this.options.formId)) { + // there's already a form with that name, don't specify an id + this.options.formId = null; + } + } + + if (this.options.externalControl) { + this.options.externalControl = $(this.options.externalControl); + } + + this.originalBackground = Element.getStyle(this.element, 'background-color'); + if (!this.originalBackground) { + this.originalBackground = "transparent"; + } + + this.element.title = this.options.clickToEditText; + + this.onclickListener = this.enterEditMode.bindAsEventListener(this); + this.mouseoverListener = this.enterHover.bindAsEventListener(this); + this.mouseoutListener = this.leaveHover.bindAsEventListener(this); + Event.observe(this.element, 'click', this.onclickListener); + Event.observe(this.element, 'mouseover', this.mouseoverListener); + Event.observe(this.element, 'mouseout', this.mouseoutListener); + if (this.options.externalControl) { + Event.observe(this.options.externalControl, 'click', this.onclickListener); + Event.observe(this.options.externalControl, 'mouseover', this.mouseoverListener); + Event.observe(this.options.externalControl, 'mouseout', this.mouseoutListener); + } + }, + enterEditMode: function(evt) { + if (this.saving) return; + if (this.editing) return; + this.editing = true; + this.onEnterEditMode(); + if (this.options.externalControl) { + Element.hide(this.options.externalControl); + } + Element.hide(this.element); + this.createForm(); + this.element.parentNode.insertBefore(this.form, this.element); + if (!this.options.loadTextURL) Field.scrollFreeActivate(this.editField); + // stop the event to avoid a page refresh in Safari + if (evt) { + Event.stop(evt); + } + return false; + }, + createForm: function() { + this.form = document.createElement("form"); + this.form.id = this.options.formId; + Element.addClassName(this.form, this.options.formClassName) + this.form.onsubmit = this.onSubmit.bind(this); + + this.createEditField(); + + if (this.options.textarea) { + var br = document.createElement("br"); + this.form.appendChild(br); + } + + if (this.options.textBeforeControls) + this.form.appendChild(document.createTextNode(this.options.textBeforeControls)); + + if (this.options.okButton) { + var okButton = document.createElement("input"); + okButton.type = "submit"; + okButton.value = this.options.okText; + okButton.className = 'editor_ok_button'; + this.form.appendChild(okButton); + } + + if (this.options.okLink) { + var okLink = document.createElement("a"); + okLink.href = "#"; + okLink.appendChild(document.createTextNode(this.options.okText)); + okLink.onclick = this.onSubmit.bind(this); + okLink.className = 'editor_ok_link'; + this.form.appendChild(okLink); + } + + if (this.options.textBetweenControls && + (this.options.okLink || this.options.okButton) && + (this.options.cancelLink || this.options.cancelButton)) + this.form.appendChild(document.createTextNode(this.options.textBetweenControls)); + + if (this.options.cancelButton) { + var cancelButton = document.createElement("input"); + cancelButton.type = "submit"; + cancelButton.value = this.options.cancelText; + cancelButton.onclick = this.onclickCancel.bind(this); + cancelButton.className = 'editor_cancel_button'; + this.form.appendChild(cancelButton); + } + + if (this.options.cancelLink) { + var cancelLink = document.createElement("a"); + cancelLink.href = "#"; + cancelLink.appendChild(document.createTextNode(this.options.cancelText)); + cancelLink.onclick = this.onclickCancel.bind(this); + cancelLink.className = 'editor_cancel editor_cancel_link'; + this.form.appendChild(cancelLink); + } + + if (this.options.textAfterControls) + this.form.appendChild(document.createTextNode(this.options.textAfterControls)); + }, + hasHTMLLineBreaks: function(string) { + if (!this.options.handleLineBreaks) return false; + return string.match(/<br/i) || string.match(/<p>/i); + }, + convertHTMLLineBreaks: function(string) { + return string.replace(/<br>/gi, "\n").replace(/<br\/>/gi, "\n").replace(/<\/p>/gi, "\n").replace(/<p>/gi, ""); + }, + createEditField: function() { + var text; + if(this.options.loadTextURL) { + text = this.options.loadingText; + } else { + text = this.getText(); + } + + var obj = this; + + if (this.options.rows == 1 && !this.hasHTMLLineBreaks(text)) { + this.options.textarea = false; + var textField = document.createElement("input"); + textField.obj = this; + textField.type = "text"; + textField.name = this.options.paramName; + textField.value = text; + textField.style.backgroundColor = this.options.highlightcolor; + textField.className = 'editor_field'; + var size = this.options.size || this.options.cols || 0; + if (size != 0) textField.size = size; + if (this.options.submitOnBlur) + textField.onblur = this.onSubmit.bind(this); + this.editField = textField; + } else { + this.options.textarea = true; + var textArea = document.createElement("textarea"); + textArea.obj = this; + textArea.name = this.options.paramName; + textArea.value = this.convertHTMLLineBreaks(text); + textArea.rows = this.options.rows; + textArea.cols = this.options.cols || 40; + textArea.className = 'editor_field'; + if (this.options.submitOnBlur) + textArea.onblur = this.onSubmit.bind(this); + this.editField = textArea; + } + + if(this.options.loadTextURL) { + this.loadExternalText(); + } + this.form.appendChild(this.editField); + }, + getText: function() { + return this.element.innerHTML; + }, + loadExternalText: function() { + Element.addClassName(this.form, this.options.loadingClassName); + this.editField.disabled = true; + new Ajax.Request( + this.options.loadTextURL, + Object.extend({ + asynchronous: true, + onComplete: this.onLoadedExternalText.bind(this) + }, this.options.ajaxOptions) + ); + }, + onLoadedExternalText: function(transport) { + Element.removeClassName(this.form, this.options.loadingClassName); + this.editField.disabled = false; + this.editField.value = transport.responseText.stripTags(); + Field.scrollFreeActivate(this.editField); + }, + onclickCancel: function() { + this.onComplete(); + this.leaveEditMode(); + return false; + }, + onFailure: function(transport) { + this.options.onFailure(transport); + if (this.oldInnerHTML) { + this.element.innerHTML = this.oldInnerHTML; + this.oldInnerHTML = null; + } + return false; + }, + onSubmit: function() { + // onLoading resets these so we need to save them away for the Ajax call + var form = this.form; + var value = this.editField.value; + + // do this first, sometimes the ajax call returns before we get a chance to switch on Saving... + // which means this will actually switch on Saving... *after* we've left edit mode causing Saving... + // to be displayed indefinitely + this.onLoading(); + + if (this.options.evalScripts) { + new Ajax.Request( + this.url, Object.extend({ + parameters: this.options.callback(form, value), + onComplete: this.onComplete.bind(this), + onFailure: this.onFailure.bind(this), + asynchronous:true, + evalScripts:true + }, this.options.ajaxOptions)); + } else { + new Ajax.Updater( + { success: this.element, + // don't update on failure (this could be an option) + failure: null }, + this.url, Object.extend({ + parameters: this.options.callback(form, value), + onComplete: this.onComplete.bind(this), + onFailure: this.onFailure.bind(this) + }, this.options.ajaxOptions)); + } + // stop the event to avoid a page refresh in Safari + if (arguments.length > 1) { + Event.stop(arguments[0]); + } + return false; + }, + onLoading: function() { + this.saving = true; + this.removeForm(); + this.leaveHover(); + this.showSaving(); + }, + showSaving: function() { + this.oldInnerHTML = this.element.innerHTML; + this.element.innerHTML = this.options.savingText; + Element.addClassName(this.element, this.options.savingClassName); + this.element.style.backgroundColor = this.originalBackground; + Element.show(this.element); + }, + removeForm: function() { + if(this.form) { + if (this.form.parentNode) Element.remove(this.form); + this.form = null; + } + }, + enterHover: function() { + if (this.saving) return; + this.element.style.backgroundColor = this.options.highlightcolor; + if (this.effect) { + this.effect.cancel(); + } + Element.addClassName(this.element, this.options.hoverClassName) + }, + leaveHover: function() { + if (this.options.backgroundColor) { + this.element.style.backgroundColor = this.oldBackground; + } + Element.removeClassName(this.element, this.options.hoverClassName) + if (this.saving) return; + this.effect = new Effect.Highlight(this.element, { + startcolor: this.options.highlightcolor, + endcolor: this.options.highlightendcolor, + restorecolor: this.originalBackground + }); + }, + leaveEditMode: function() { + Element.removeClassName(this.element, this.options.savingClassName); + this.removeForm(); + this.leaveHover(); + this.element.style.backgroundColor = this.originalBackground; + Element.show(this.element); + if (this.options.externalControl) { + Element.show(this.options.externalControl); + } + this.editing = false; + this.saving = false; + this.oldInnerHTML = null; + this.onLeaveEditMode(); + }, + onComplete: function(transport) { + this.leaveEditMode(); + this.options.onComplete.bind(this)(transport, this.element); + }, + onEnterEditMode: function() {}, + onLeaveEditMode: function() {}, + dispose: function() { + if (this.oldInnerHTML) { + this.element.innerHTML = this.oldInnerHTML; + } + this.leaveEditMode(); + Event.stopObserving(this.element, 'click', this.onclickListener); + Event.stopObserving(this.element, 'mouseover', this.mouseoverListener); + Event.stopObserving(this.element, 'mouseout', this.mouseoutListener); + if (this.options.externalControl) { + Event.stopObserving(this.options.externalControl, 'click', this.onclickListener); + Event.stopObserving(this.options.externalControl, 'mouseover', this.mouseoverListener); + Event.stopObserving(this.options.externalControl, 'mouseout', this.mouseoutListener); + } + } +}; + +Ajax.InPlaceCollectionEditor = Class.create(); +Object.extend(Ajax.InPlaceCollectionEditor.prototype, Ajax.InPlaceEditor.prototype); +Object.extend(Ajax.InPlaceCollectionEditor.prototype, { + createEditField: function() { + if (!this.cached_selectTag) { + var selectTag = document.createElement("select"); + var collection = this.options.collection || []; + var optionTag; + collection.each(function(e,i) { + optionTag = document.createElement("option"); + optionTag.value = (e instanceof Array) ? e[0] : e; + if((typeof this.options.value == 'undefined') && + ((e instanceof Array) ? this.element.innerHTML == e[1] : e == optionTag.value)) optionTag.selected = true; + if(this.options.value==optionTag.value) optionTag.selected = true; + optionTag.appendChild(document.createTextNode((e instanceof Array) ? e[1] : e)); + selectTag.appendChild(optionTag); + }.bind(this)); + this.cached_selectTag = selectTag; + } + + this.editField = this.cached_selectTag; + if(this.options.loadTextURL) this.loadExternalText(); + this.form.appendChild(this.editField); + this.options.callback = function(form, value) { + return "value=" + encodeURIComponent(value); + } + } +}); + +// Delayed observer, like Form.Element.Observer, +// but waits for delay after last key input +// Ideal for live-search fields + +Form.Element.DelayedObserver = Class.create(); +Form.Element.DelayedObserver.prototype = { + initialize: function(element, delay, callback) { + this.delay = delay || 0.5; + this.element = $(element); + this.callback = callback; + this.timer = null; + this.lastValue = $F(this.element); + Event.observe(this.element,'keyup',this.delayedListener.bindAsEventListener(this)); + }, + delayedListener: function(event) { + if(this.lastValue == $F(this.element)) return; + if(this.timer) clearTimeout(this.timer); + this.timer = setTimeout(this.onTimerEvent.bind(this), this.delay * 1000); + this.lastValue = $F(this.element); + }, + onTimerEvent: function() { + this.timer = null; + this.callback(this.element, $F(this.element)); + } +}; diff --git a/dom/tests/mochitest/ajax/scriptaculous/src/dragdrop.js b/dom/tests/mochitest/ajax/scriptaculous/src/dragdrop.js new file mode 100644 index 0000000000..10c7c93264 --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/src/dragdrop.js @@ -0,0 +1,970 @@ +// script.aculo.us dragdrop.js v1.7.1_beta2, Tue May 15 15:15:45 EDT 2007 + +// Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) +// (c) 2005-2007 Sammi Williams (http://www.oriontransfer.co.nz, sammi@oriontransfer.co.nz) +// +// script.aculo.us is freely distributable under the terms of an MIT-style license. +// For details, see the script.aculo.us web site: http://script.aculo.us/ + +if(typeof Effect == 'undefined') + throw("dragdrop.js requires including script.aculo.us' effects.js library"); + +var Droppables = { + drops: [], + + remove: function(element) { + this.drops = this.drops.reject(function(d) { return d.element==$(element) }); + }, + + add: function(element) { + element = $(element); + var options = Object.extend({ + greedy: true, + hoverclass: null, + tree: false + }, arguments[1] || {}); + + // cache containers + if(options.containment) { + options._containers = []; + var containment = options.containment; + if((typeof containment == 'object') && + (containment.constructor == Array)) { + containment.each( function(c) { options._containers.push($(c)) }); + } else { + options._containers.push($(containment)); + } + } + + if(options.accept) options.accept = [options.accept].flatten(); + + Element.makePositioned(element); // fix IE + options.element = element; + + this.drops.push(options); + }, + + findDeepestChild: function(drops) { + deepest = drops[0]; + + for (i = 1; i < drops.length; ++i) + if (Element.isParent(drops[i].element, deepest.element)) + deepest = drops[i]; + + return deepest; + }, + + isContained: function(element, drop) { + var containmentNode; + if(drop.tree) { + containmentNode = element.treeNode; + } else { + containmentNode = element.parentNode; + } + return drop._containers.detect(function(c) { return containmentNode == c }); + }, + + isAffected: function(point, element, drop) { + return ( + (drop.element!=element) && + ((!drop._containers) || + this.isContained(element, drop)) && + ((!drop.accept) || + (Element.classNames(element).detect( + function(v) { return drop.accept.include(v) } ) )) && + Position.within(drop.element, point[0], point[1]) ); + }, + + deactivate: function(drop) { + if(drop.hoverclass) + Element.removeClassName(drop.element, drop.hoverclass); + this.last_active = null; + }, + + activate: function(drop) { + if(drop.hoverclass) + Element.addClassName(drop.element, drop.hoverclass); + this.last_active = drop; + }, + + show: function(point, element) { + if(!this.drops.length) return; + var affected = []; + + if(this.last_active) this.deactivate(this.last_active); + this.drops.each( function(drop) { + if(Droppables.isAffected(point, element, drop)) + affected.push(drop); + }); + + if(affected.length>0) { + drop = Droppables.findDeepestChild(affected); + Position.within(drop.element, point[0], point[1]); + if(drop.onHover) + drop.onHover(element, drop.element, Position.overlap(drop.overlap, drop.element)); + + Droppables.activate(drop); + } + }, + + fire: function(event, element) { + if(!this.last_active) return; + Position.prepare(); + + if (this.isAffected([Event.pointerX(event), Event.pointerY(event)], element, this.last_active)) + if (this.last_active.onDrop) { + this.last_active.onDrop(element, this.last_active.element, event); + return true; + } + }, + + reset: function() { + if(this.last_active) + this.deactivate(this.last_active); + } +} + +var Draggables = { + drags: [], + observers: [], + + register: function(draggable) { + if(this.drags.length == 0) { + this.eventMouseUp = this.endDrag.bindAsEventListener(this); + this.eventMouseMove = this.updateDrag.bindAsEventListener(this); + this.eventKeypress = this.keyPress.bindAsEventListener(this); + + Event.observe(document, "mouseup", this.eventMouseUp); + Event.observe(document, "mousemove", this.eventMouseMove); + Event.observe(document, "keypress", this.eventKeypress); + } + this.drags.push(draggable); + }, + + unregister: function(draggable) { + this.drags = this.drags.reject(function(d) { return d==draggable }); + if(this.drags.length == 0) { + Event.stopObserving(document, "mouseup", this.eventMouseUp); + Event.stopObserving(document, "mousemove", this.eventMouseMove); + Event.stopObserving(document, "keypress", this.eventKeypress); + } + }, + + activate: function(draggable) { + if(draggable.options.delay) { + this._timeout = setTimeout(function() { + Draggables._timeout = null; + window.focus(); + Draggables.activeDraggable = draggable; + }.bind(this), draggable.options.delay); + } else { + window.focus(); // allows keypress events if window isn't currently focused, fails for Safari + this.activeDraggable = draggable; + } + }, + + deactivate: function() { + this.activeDraggable = null; + }, + + updateDrag: function(event) { + if(!this.activeDraggable) return; + var pointer = [Event.pointerX(event), Event.pointerY(event)]; + // Mozilla-based browsers fire successive mousemove events with + // the same coordinates, prevent needless redrawing (moz bug?) + if(this._lastPointer && (this._lastPointer.inspect() == pointer.inspect())) return; + this._lastPointer = pointer; + + this.activeDraggable.updateDrag(event, pointer); + }, + + endDrag: function(event) { + if(this._timeout) { + clearTimeout(this._timeout); + this._timeout = null; + } + if(!this.activeDraggable) return; + this._lastPointer = null; + this.activeDraggable.endDrag(event); + this.activeDraggable = null; + }, + + keyPress: function(event) { + if(this.activeDraggable) + this.activeDraggable.keyPress(event); + }, + + addObserver: function(observer) { + this.observers.push(observer); + this._cacheObserverCallbacks(); + }, + + removeObserver: function(element) { // element instead of observer fixes mem leaks + this.observers = this.observers.reject( function(o) { return o.element==element }); + this._cacheObserverCallbacks(); + }, + + notify: function(eventName, draggable, event) { // 'onStart', 'onEnd', 'onDrag' + if(this[eventName+'Count'] > 0) + this.observers.each( function(o) { + if(o[eventName]) o[eventName](eventName, draggable, event); + }); + if(draggable.options[eventName]) draggable.options[eventName](draggable, event); + }, + + _cacheObserverCallbacks: function() { + ['onStart','onEnd','onDrag'].each( function(eventName) { + Draggables[eventName+'Count'] = Draggables.observers.select( + function(o) { return o[eventName]; } + ).length; + }); + } +} + +/*--------------------------------------------------------------------------*/ + +var Draggable = Class.create(); +Draggable._dragging = {}; + +Draggable.prototype = { + initialize: function(element) { + var defaults = { + handle: false, + reverteffect: function(element, top_offset, left_offset) { + var dur = Math.sqrt(Math.abs(top_offset^2)+Math.abs(left_offset^2))*0.02; + new Effect.Move(element, { x: -left_offset, y: -top_offset, duration: dur, + queue: {scope:'_draggable', position:'end'} + }); + }, + endeffect: function(element) { + var toOpacity = typeof element._opacity == 'number' ? element._opacity : 1.0; + new Effect.Opacity(element, {duration:0.2, from:0.7, to:toOpacity, + queue: {scope:'_draggable', position:'end'}, + afterFinish: function(){ + Draggable._dragging[element] = false + } + }); + }, + zindex: 1000, + revert: false, + quiet: false, + scroll: false, + scrollSensitivity: 20, + scrollSpeed: 15, + snap: false, // false, or xy or [x,y] or function(x,y){ return [x,y] } + delay: 0 + }; + + if(!arguments[1] || typeof arguments[1].endeffect == 'undefined') + Object.extend(defaults, { + starteffect: function(element) { + element._opacity = Element.getOpacity(element); + Draggable._dragging[element] = true; + new Effect.Opacity(element, {duration:0.2, from:element._opacity, to:0.7}); + } + }); + + var options = Object.extend(defaults, arguments[1] || {}); + + this.element = $(element); + + if(options.handle && (typeof options.handle == 'string')) + this.handle = this.element.down('.'+options.handle, 0); + + if(!this.handle) this.handle = $(options.handle); + if(!this.handle) this.handle = this.element; + + if(options.scroll && !options.scroll.scrollTo && !options.scroll.outerHTML) { + options.scroll = $(options.scroll); + this._isScrollChild = Element.childOf(this.element, options.scroll); + } + + Element.makePositioned(this.element); // fix IE + + this.delta = this.currentDelta(); + this.options = options; + this.dragging = false; + + this.eventMouseDown = this.initDrag.bindAsEventListener(this); + Event.observe(this.handle, "mousedown", this.eventMouseDown); + + Draggables.register(this); + }, + + destroy: function() { + Event.stopObserving(this.handle, "mousedown", this.eventMouseDown); + Draggables.unregister(this); + }, + + currentDelta: function() { + return([ + parseInt(Element.getStyle(this.element,'left') || '0'), + parseInt(Element.getStyle(this.element,'top') || '0')]); + }, + + initDrag: function(event) { + if(typeof Draggable._dragging[this.element] != 'undefined' && + Draggable._dragging[this.element]) return; + if(Event.isLeftClick(event)) { + // abort on form elements, fixes a Firefox issue + var src = Event.element(event); + if((tag_name = src.tagName.toUpperCase()) && ( + tag_name=='INPUT' || + tag_name=='SELECT' || + tag_name=='OPTION' || + tag_name=='BUTTON' || + tag_name=='TEXTAREA')) return; + + var pointer = [Event.pointerX(event), Event.pointerY(event)]; + var pos = Position.cumulativeOffset(this.element); + this.offset = [0,1].map( function(i) { return (pointer[i] - pos[i]) }); + + Draggables.activate(this); + Event.stop(event); + } + }, + + startDrag: function(event) { + this.dragging = true; + + if(this.options.zindex) { + this.originalZ = parseInt(Element.getStyle(this.element,'z-index') || 0); + this.element.style.zIndex = this.options.zindex; + } + + if(this.options.ghosting) { + this._clone = this.element.cloneNode(true); + Position.absolutize(this.element); + this.element.parentNode.insertBefore(this._clone, this.element); + } + + if(this.options.scroll) { + if (this.options.scroll == window) { + var where = this._getWindowScroll(this.options.scroll); + this.originalScrollLeft = where.left; + this.originalScrollTop = where.top; + } else { + this.originalScrollLeft = this.options.scroll.scrollLeft; + this.originalScrollTop = this.options.scroll.scrollTop; + } + } + + Draggables.notify('onStart', this, event); + + if(this.options.starteffect) this.options.starteffect(this.element); + }, + + updateDrag: function(event, pointer) { + if(!this.dragging) this.startDrag(event); + + if(!this.options.quiet){ + Position.prepare(); + Droppables.show(pointer, this.element); + } + + Draggables.notify('onDrag', this, event); + + this.draw(pointer); + if(this.options.change) this.options.change(this); + + if(this.options.scroll) { + this.stopScrolling(); + + var p; + if (this.options.scroll == window) { + with(this._getWindowScroll(this.options.scroll)) { p = [ left, top, left+width, top+height ]; } + } else { + p = Position.page(this.options.scroll); + p[0] += this.options.scroll.scrollLeft + Position.deltaX; + p[1] += this.options.scroll.scrollTop + Position.deltaY; + p.push(p[0]+this.options.scroll.offsetWidth); + p.push(p[1]+this.options.scroll.offsetHeight); + } + var speed = [0,0]; + if(pointer[0] < (p[0]+this.options.scrollSensitivity)) speed[0] = pointer[0]-(p[0]+this.options.scrollSensitivity); + if(pointer[1] < (p[1]+this.options.scrollSensitivity)) speed[1] = pointer[1]-(p[1]+this.options.scrollSensitivity); + if(pointer[0] > (p[2]-this.options.scrollSensitivity)) speed[0] = pointer[0]-(p[2]-this.options.scrollSensitivity); + if(pointer[1] > (p[3]-this.options.scrollSensitivity)) speed[1] = pointer[1]-(p[3]-this.options.scrollSensitivity); + this.startScrolling(speed); + } + + // fix AppleWebKit rendering + if(Prototype.Browser.WebKit) window.scrollBy(0,0); + + Event.stop(event); + }, + + finishDrag: function(event, success) { + this.dragging = false; + + if(this.options.quiet){ + Position.prepare(); + var pointer = [Event.pointerX(event), Event.pointerY(event)]; + Droppables.show(pointer, this.element); + } + + if(this.options.ghosting) { + Position.relativize(this.element); + Element.remove(this._clone); + this._clone = null; + } + + var dropped = false; + if(success) { + dropped = Droppables.fire(event, this.element); + if (!dropped) dropped = false; + } + if(dropped && this.options.onDropped) this.options.onDropped(this.element); + Draggables.notify('onEnd', this, event); + + var revert = this.options.revert; + if(revert && typeof revert == 'function') revert = revert(this.element); + + var d = this.currentDelta(); + if(revert && this.options.reverteffect) { + if (dropped == 0 || revert != 'failure') + this.options.reverteffect(this.element, + d[1]-this.delta[1], d[0]-this.delta[0]); + } else { + this.delta = d; + } + + if(this.options.zindex) + this.element.style.zIndex = this.originalZ; + + if(this.options.endeffect) + this.options.endeffect(this.element); + + Draggables.deactivate(this); + Droppables.reset(); + }, + + keyPress: function(event) { + if(event.keyCode!=Event.KEY_ESC) return; + this.finishDrag(event, false); + Event.stop(event); + }, + + endDrag: function(event) { + if(!this.dragging) return; + this.stopScrolling(); + this.finishDrag(event, true); + Event.stop(event); + }, + + draw: function(point) { + var pos = Position.cumulativeOffset(this.element); + if(this.options.ghosting) { + var r = Position.realOffset(this.element); + pos[0] += r[0] - Position.deltaX; pos[1] += r[1] - Position.deltaY; + } + + var d = this.currentDelta(); + pos[0] -= d[0]; pos[1] -= d[1]; + + if(this.options.scroll && (this.options.scroll != window && this._isScrollChild)) { + pos[0] -= this.options.scroll.scrollLeft-this.originalScrollLeft; + pos[1] -= this.options.scroll.scrollTop-this.originalScrollTop; + } + + var p = [0,1].map(function(i){ + return (point[i]-pos[i]-this.offset[i]) + }.bind(this)); + + if(this.options.snap) { + if(typeof this.options.snap == 'function') { + p = this.options.snap(p[0],p[1],this); + } else { + if(this.options.snap instanceof Array) { + p = p.map( function(v, i) { + return Math.round(v/this.options.snap[i])*this.options.snap[i] }.bind(this)) + } else { + p = p.map( function(v) { + return Math.round(v/this.options.snap)*this.options.snap }.bind(this)) + } + }} + + var style = this.element.style; + if((!this.options.constraint) || (this.options.constraint=='horizontal')) + style.left = p[0] + "px"; + if((!this.options.constraint) || (this.options.constraint=='vertical')) + style.top = p[1] + "px"; + + if(style.visibility=="hidden") style.visibility = ""; // fix gecko rendering + }, + + stopScrolling: function() { + if(this.scrollInterval) { + clearInterval(this.scrollInterval); + this.scrollInterval = null; + Draggables._lastScrollPointer = null; + } + }, + + startScrolling: function(speed) { + if(!(speed[0] || speed[1])) return; + this.scrollSpeed = [speed[0]*this.options.scrollSpeed,speed[1]*this.options.scrollSpeed]; + this.lastScrolled = new Date(); + this.scrollInterval = setInterval(this.scroll.bind(this), 10); + }, + + scroll: function() { + var current = new Date(); + var delta = current - this.lastScrolled; + this.lastScrolled = current; + if(this.options.scroll == window) { + with (this._getWindowScroll(this.options.scroll)) { + if (this.scrollSpeed[0] || this.scrollSpeed[1]) { + var d = delta / 1000; + this.options.scroll.scrollTo( left + d*this.scrollSpeed[0], top + d*this.scrollSpeed[1] ); + } + } + } else { + this.options.scroll.scrollLeft += this.scrollSpeed[0] * delta / 1000; + this.options.scroll.scrollTop += this.scrollSpeed[1] * delta / 1000; + } + + Position.prepare(); + Droppables.show(Draggables._lastPointer, this.element); + Draggables.notify('onDrag', this); + if (this._isScrollChild) { + Draggables._lastScrollPointer = Draggables._lastScrollPointer || $A(Draggables._lastPointer); + Draggables._lastScrollPointer[0] += this.scrollSpeed[0] * delta / 1000; + Draggables._lastScrollPointer[1] += this.scrollSpeed[1] * delta / 1000; + if (Draggables._lastScrollPointer[0] < 0) + Draggables._lastScrollPointer[0] = 0; + if (Draggables._lastScrollPointer[1] < 0) + Draggables._lastScrollPointer[1] = 0; + this.draw(Draggables._lastScrollPointer); + } + + if(this.options.change) this.options.change(this); + }, + + _getWindowScroll: function(w) { + var T, L, W, H; + with (w.document) { + if (w.document.documentElement && documentElement.scrollTop) { + T = documentElement.scrollTop; + L = documentElement.scrollLeft; + } else if (w.document.body) { + T = body.scrollTop; + L = body.scrollLeft; + } + if (w.innerWidth) { + W = w.innerWidth; + H = w.innerHeight; + } else if (w.document.documentElement && documentElement.clientWidth) { + W = documentElement.clientWidth; + H = documentElement.clientHeight; + } else { + W = body.offsetWidth; + H = body.offsetHeight + } + } + return { top: T, left: L, width: W, height: H }; + } +} + +/*--------------------------------------------------------------------------*/ + +var SortableObserver = Class.create(); +SortableObserver.prototype = { + initialize: function(element, observer) { + this.element = $(element); + this.observer = observer; + this.lastValue = Sortable.serialize(this.element); + }, + + onStart: function() { + this.lastValue = Sortable.serialize(this.element); + }, + + onEnd: function() { + Sortable.unmark(); + if(this.lastValue != Sortable.serialize(this.element)) + this.observer(this.element) + } +} + +var Sortable = { + SERIALIZE_RULE: /^[^_\-](?:[A-Za-z0-9\-\_]*)[_](.*)$/, + + sortables: {}, + + _findRootElement: function(element) { + while (element.tagName.toUpperCase() != "BODY") { + if(element.id && Sortable.sortables[element.id]) return element; + element = element.parentNode; + } + }, + + options: function(element) { + element = Sortable._findRootElement($(element)); + if(!element) return; + return Sortable.sortables[element.id]; + }, + + destroy: function(element){ + var s = Sortable.options(element); + + if(s) { + Draggables.removeObserver(s.element); + s.droppables.each(function(d){ Droppables.remove(d) }); + s.draggables.invoke('destroy'); + + delete Sortable.sortables[s.element.id]; + } + }, + + create: function(element) { + element = $(element); + var options = Object.extend({ + element: element, + tag: 'li', // assumes li children, override with tag: 'tagname' + dropOnEmpty: false, + tree: false, + treeTag: 'ul', + overlap: 'vertical', // one of 'vertical', 'horizontal' + constraint: 'vertical', // one of 'vertical', 'horizontal', false + containment: element, // also takes array of elements (or id's); or false + handle: false, // or a CSS class + only: false, + delay: 0, + hoverclass: null, + ghosting: false, + quiet: false, + scroll: false, + scrollSensitivity: 20, + scrollSpeed: 15, + format: this.SERIALIZE_RULE, + + // these take arrays of elements or ids and can be + // used for better initialization performance + elements: false, + handles: false, + + onChange: Prototype.emptyFunction, + onUpdate: Prototype.emptyFunction + }, arguments[1] || {}); + + // clear any old sortable with same element + this.destroy(element); + + // build options for the draggables + var options_for_draggable = { + revert: true, + quiet: options.quiet, + scroll: options.scroll, + scrollSpeed: options.scrollSpeed, + scrollSensitivity: options.scrollSensitivity, + delay: options.delay, + ghosting: options.ghosting, + constraint: options.constraint, + handle: options.handle }; + + if(options.starteffect) + options_for_draggable.starteffect = options.starteffect; + + if(options.reverteffect) + options_for_draggable.reverteffect = options.reverteffect; + else + if(options.ghosting) options_for_draggable.reverteffect = function(element) { + element.style.top = 0; + element.style.left = 0; + }; + + if(options.endeffect) + options_for_draggable.endeffect = options.endeffect; + + if(options.zindex) + options_for_draggable.zindex = options.zindex; + + // build options for the droppables + var options_for_droppable = { + overlap: options.overlap, + containment: options.containment, + tree: options.tree, + hoverclass: options.hoverclass, + onHover: Sortable.onHover + } + + var options_for_tree = { + onHover: Sortable.onEmptyHover, + overlap: options.overlap, + containment: options.containment, + hoverclass: options.hoverclass + } + + // fix for gecko engine + Element.cleanWhitespace(element); + + options.draggables = []; + options.droppables = []; + + // drop on empty handling + if(options.dropOnEmpty || options.tree) { + Droppables.add(element, options_for_tree); + options.droppables.push(element); + } + + (options.elements || this.findElements(element, options) || []).each( function(e,i) { + var handle = options.handles ? $(options.handles[i]) : + (options.handle ? $(e).getElementsByClassName(options.handle)[0] : e); + options.draggables.push( + new Draggable(e, Object.extend(options_for_draggable, { handle: handle }))); + Droppables.add(e, options_for_droppable); + if(options.tree) e.treeNode = element; + options.droppables.push(e); + }); + + if(options.tree) { + (Sortable.findTreeElements(element, options) || []).each( function(e) { + Droppables.add(e, options_for_tree); + e.treeNode = element; + options.droppables.push(e); + }); + } + + // keep reference + this.sortables[element.id] = options; + + // for onupdate + Draggables.addObserver(new SortableObserver(element, options.onUpdate)); + + }, + + // return all suitable-for-sortable elements in a guaranteed order + findElements: function(element, options) { + return Element.findChildren( + element, options.only, options.tree ? true : false, options.tag); + }, + + findTreeElements: function(element, options) { + return Element.findChildren( + element, options.only, options.tree ? true : false, options.treeTag); + }, + + onHover: function(element, dropon, overlap) { + if(Element.isParent(dropon, element)) return; + + if(overlap > .33 && overlap < .66 && Sortable.options(dropon).tree) { + return; + } else if(overlap>0.5) { + Sortable.mark(dropon, 'before'); + if(dropon.previousSibling != element) { + var oldParentNode = element.parentNode; + element.style.visibility = "hidden"; // fix gecko rendering + dropon.parentNode.insertBefore(element, dropon); + if(dropon.parentNode!=oldParentNode) + Sortable.options(oldParentNode).onChange(element); + Sortable.options(dropon.parentNode).onChange(element); + } + } else { + Sortable.mark(dropon, 'after'); + var nextElement = dropon.nextSibling || null; + if(nextElement != element) { + var oldParentNode = element.parentNode; + element.style.visibility = "hidden"; // fix gecko rendering + dropon.parentNode.insertBefore(element, nextElement); + if(dropon.parentNode!=oldParentNode) + Sortable.options(oldParentNode).onChange(element); + Sortable.options(dropon.parentNode).onChange(element); + } + } + }, + + onEmptyHover: function(element, dropon, overlap) { + var oldParentNode = element.parentNode; + var droponOptions = Sortable.options(dropon); + + if(!Element.isParent(dropon, element)) { + var index; + + var children = Sortable.findElements(dropon, {tag: droponOptions.tag, only: droponOptions.only}); + var child = null; + + if(children) { + var offset = Element.offsetSize(dropon, droponOptions.overlap) * (1.0 - overlap); + + for (index = 0; index < children.length; index += 1) { + if (offset - Element.offsetSize (children[index], droponOptions.overlap) >= 0) { + offset -= Element.offsetSize (children[index], droponOptions.overlap); + } else if (offset - (Element.offsetSize (children[index], droponOptions.overlap) / 2) >= 0) { + child = index + 1 < children.length ? children[index + 1] : null; + break; + } else { + child = children[index]; + break; + } + } + } + + dropon.insertBefore(element, child); + + Sortable.options(oldParentNode).onChange(element); + droponOptions.onChange(element); + } + }, + + unmark: function() { + if(Sortable._marker) Sortable._marker.hide(); + }, + + mark: function(dropon, position) { + // mark on ghosting only + var sortable = Sortable.options(dropon.parentNode); + if(sortable && !sortable.ghosting) return; + + if(!Sortable._marker) { + Sortable._marker = + ($('dropmarker') || Element.extend(document.createElement('DIV'))). + hide().addClassName('dropmarker').setStyle({position:'absolute'}); + document.getElementsByTagName("body").item(0).appendChild(Sortable._marker); + } + var offsets = Position.cumulativeOffset(dropon); + Sortable._marker.setStyle({left: offsets[0]+'px', top: offsets[1] + 'px'}); + + if(position=='after') + if(sortable.overlap == 'horizontal') + Sortable._marker.setStyle({left: (offsets[0]+dropon.clientWidth) + 'px'}); + else + Sortable._marker.setStyle({top: (offsets[1]+dropon.clientHeight) + 'px'}); + + Sortable._marker.show(); + }, + + _tree: function(element, options, parent) { + var children = Sortable.findElements(element, options) || []; + + for (var i = 0; i < children.length; ++i) { + var match = children[i].id.match(options.format); + + if (!match) continue; + + var child = { + id: encodeURIComponent(match ? match[1] : null), + element: element, + parent: parent, + children: [], + position: parent.children.length, + container: $(children[i]).down(options.treeTag) + } + + /* Get the element containing the children and recurse over it */ + if (child.container) + this._tree(child.container, options, child) + + parent.children.push (child); + } + + return parent; + }, + + tree: function(element) { + element = $(element); + var sortableOptions = this.options(element); + var options = Object.extend({ + tag: sortableOptions.tag, + treeTag: sortableOptions.treeTag, + only: sortableOptions.only, + name: element.id, + format: sortableOptions.format + }, arguments[1] || {}); + + var root = { + id: null, + parent: null, + children: [], + container: element, + position: 0 + } + + return Sortable._tree(element, options, root); + }, + + /* Construct a [i] index for a particular node */ + _constructIndex: function(node) { + var index = ''; + do { + if (node.id) index = '[' + node.position + ']' + index; + } while ((node = node.parent) != null); + return index; + }, + + sequence: function(element) { + element = $(element); + var options = Object.extend(this.options(element), arguments[1] || {}); + + return $(this.findElements(element, options) || []).map( function(item) { + return item.id.match(options.format) ? item.id.match(options.format)[1] : ''; + }); + }, + + setSequence: function(element, new_sequence) { + element = $(element); + var options = Object.extend(this.options(element), arguments[2] || {}); + + var nodeMap = {}; + this.findElements(element, options).each( function(n) { + if (n.id.match(options.format)) + nodeMap[n.id.match(options.format)[1]] = [n, n.parentNode]; + n.parentNode.removeChild(n); + }); + + new_sequence.each(function(ident) { + var n = nodeMap[ident]; + if (n) { + n[1].appendChild(n[0]); + delete nodeMap[ident]; + } + }); + }, + + serialize: function(element) { + element = $(element); + var options = Object.extend(Sortable.options(element), arguments[1] || {}); + var name = encodeURIComponent( + (arguments[1] && arguments[1].name) ? arguments[1].name : element.id); + + if (options.tree) { + return Sortable.tree(element, arguments[1]).children.map( function (item) { + return [name + Sortable._constructIndex(item) + "[id]=" + + encodeURIComponent(item.id)].concat(item.children.map(arguments.callee)); + }).flatten().join('&'); + } else { + return Sortable.sequence(element, arguments[1]).map( function(item) { + return name + "[]=" + encodeURIComponent(item); + }).join('&'); + } + } +} + +// Returns true if child is contained within element +Element.isParent = function(child, element) { + if (!child.parentNode || child == element) return false; + if (child.parentNode == element) return true; + return Element.isParent(child.parentNode, element); +} + +Element.findChildren = function(element, only, recursive, tagName) { + if(!element.hasChildNodes()) return null; + tagName = tagName.toUpperCase(); + if(only) only = [only].flatten(); + var elements = []; + $A(element.childNodes).each( function(e) { + if(e.tagName && e.tagName.toUpperCase()==tagName && + (!only || (Element.classNames(e).detect(function(v) { return only.include(v) })))) + elements.push(e); + if(recursive) { + var grandchildren = Element.findChildren(e, only, recursive, tagName); + if(grandchildren) elements.push(grandchildren); + } + }); + + return (elements.length>0 ? elements.flatten() : []); +} + +Element.offsetSize = function (element, type) { + return element['offset' + ((type=='vertical' || type=='height') ? 'Height' : 'Width')]; +} diff --git a/dom/tests/mochitest/ajax/scriptaculous/src/effects.js b/dom/tests/mochitest/ajax/scriptaculous/src/effects.js new file mode 100644 index 0000000000..eb69587e60 --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/src/effects.js @@ -0,0 +1,1094 @@ +// script.aculo.us effects.js v1.7.1_beta2, Tue May 15 15:15:45 EDT 2007 + +// Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) +// Contributors: +// Justin Palmer (http://encytemedia.com/) +// Mark Pilgrim (http://diveintomark.org/) +// Martin Bialasinki +// +// script.aculo.us is freely distributable under the terms of an MIT-style license. +// For details, see the script.aculo.us web site: http://script.aculo.us/ + +// converts rgb() and #xxx to #xxxxxx format, +// returns self (or first argument) if not convertable +String.prototype.parseColor = function() { + var color = '#'; + if(this.slice(0,4) == 'rgb(') { + var cols = this.slice(4,this.length-1).split(','); + var i=0; do { color += parseInt(cols[i]).toColorPart() } while (++i<3); + } else { + if(this.slice(0,1) == '#') { + if(this.length==4) for(var i=1;i<4;i++) color += (this.charAt(i) + this.charAt(i)).toLowerCase(); + if(this.length==7) color = this.toLowerCase(); + } + } + return(color.length==7 ? color : (arguments[0] || this)); +} + +/*--------------------------------------------------------------------------*/ + +Element.collectTextNodes = function(element) { + return $A($(element).childNodes).collect( function(node) { + return (node.nodeType==3 ? node.nodeValue : + (node.hasChildNodes() ? Element.collectTextNodes(node) : '')); + }).flatten().join(''); +} + +Element.collectTextNodesIgnoreClass = function(element, className) { + return $A($(element).childNodes).collect( function(node) { + return (node.nodeType==3 ? node.nodeValue : + ((node.hasChildNodes() && !Element.hasClassName(node,className)) ? + Element.collectTextNodesIgnoreClass(node, className) : '')); + }).flatten().join(''); +} + +Element.setContentZoom = function(element, percent) { + element = $(element); + element.setStyle({fontSize: (percent/100) + 'em'}); + if(Prototype.Browser.WebKit) window.scrollBy(0,0); + return element; +} + +Element.getInlineOpacity = function(element){ + return $(element).style.opacity || ''; +} + +Element.forceRerendering = function(element) { + try { + element = $(element); + var n = document.createTextNode(' '); + element.appendChild(n); + element.removeChild(n); + } catch(e) { } +}; + +/*--------------------------------------------------------------------------*/ + +Array.prototype.call = function() { + var args = arguments; + this.each(function(f){ f.apply(this, args) }); +} + +/*--------------------------------------------------------------------------*/ + +var Effect = { + _elementDoesNotExistError: { + name: 'ElementDoesNotExistError', + message: 'The specified DOM element does not exist, but is required for this effect to operate' + }, + tagifyText: function(element) { + if(typeof Builder == 'undefined') + throw("Effect.tagifyText requires including script.aculo.us' builder.js library"); + + var tagifyStyle = 'position:relative'; + if(Prototype.Browser.IE) tagifyStyle += ';zoom:1'; + + element = $(element); + $A(element.childNodes).each( function(child) { + if(child.nodeType==3) { + child.nodeValue.toArray().each( function(character) { + element.insertBefore( + Builder.node('span',{style: tagifyStyle}, + character == ' ' ? String.fromCharCode(160) : character), + child); + }); + Element.remove(child); + } + }); + }, + multiple: function(element, effect) { + var elements; + if(((typeof element == 'object') || + (typeof element == 'function')) && + (element.length)) + elements = element; + else + elements = $(element).childNodes; + + var options = Object.extend({ + speed: 0.1, + delay: 0.0 + }, arguments[2] || {}); + var masterDelay = options.delay; + + $A(elements).each( function(element, index) { + new effect(element, Object.extend(options, { delay: index * options.speed + masterDelay })); + }); + }, + PAIRS: { + 'slide': ['SlideDown','SlideUp'], + 'blind': ['BlindDown','BlindUp'], + 'appear': ['Appear','Fade'] + }, + toggle: function(element, effect) { + element = $(element); + effect = (effect || 'appear').toLowerCase(); + var options = Object.extend({ + queue: { position:'end', scope:(element.id || 'global'), limit: 1 } + }, arguments[2] || {}); + Effect[element.visible() ? + Effect.PAIRS[effect][1] : Effect.PAIRS[effect][0]](element, options); + } +}; + +var Effect2 = Effect; // deprecated + +/* ------------- transitions ------------- */ + +Effect.Transitions = { + linear: Prototype.K, + sinoidal: function(pos) { + return (-Math.cos(pos*Math.PI)/2) + 0.5; + }, + reverse: function(pos) { + return 1-pos; + }, + flicker: function(pos) { + var pos = ((-Math.cos(pos*Math.PI)/4) + 0.75) + Math.random()/4; + return (pos > 1 ? 1 : pos); + }, + wobble: function(pos) { + return (-Math.cos(pos*Math.PI*(9*pos))/2) + 0.5; + }, + pulse: function(pos, pulses) { + pulses = pulses || 5; + return ( + Math.round((pos % (1/pulses)) * pulses) == 0 ? + ((pos * pulses * 2) - Math.floor(pos * pulses * 2)) : + 1 - ((pos * pulses * 2) - Math.floor(pos * pulses * 2)) + ); + }, + none: function(pos) { + return 0; + }, + full: function(pos) { + return 1; + } +}; + +/* ------------- core effects ------------- */ + +Effect.ScopedQueue = Class.create(); +Object.extend(Object.extend(Effect.ScopedQueue.prototype, Enumerable), { + initialize: function() { + this.effects = []; + this.interval = null; + }, + _each: function(iterator) { + this.effects._each(iterator); + }, + add: function(effect) { + var timestamp = new Date().getTime(); + + var position = (typeof effect.options.queue == 'string') ? + effect.options.queue : effect.options.queue.position; + + switch(position) { + case 'front': + // move unstarted effects after this effect + this.effects.findAll(function(e){ return e.state=='idle' }).each( function(e) { + e.startOn += effect.finishOn; + e.finishOn += effect.finishOn; + }); + break; + case 'with-last': + timestamp = this.effects.pluck('startOn').max() || timestamp; + break; + case 'end': + // start effect after last queued effect has finished + timestamp = this.effects.pluck('finishOn').max() || timestamp; + break; + } + + effect.startOn += timestamp; + effect.finishOn += timestamp; + + if(!effect.options.queue.limit || (this.effects.length < effect.options.queue.limit)) + this.effects.push(effect); + + if(!this.interval) + this.interval = setInterval(this.loop.bind(this), 15); + }, + remove: function(effect) { + this.effects = this.effects.reject(function(e) { return e==effect }); + if(this.effects.length == 0) { + clearInterval(this.interval); + this.interval = null; + } + }, + loop: function() { + var timePos = new Date().getTime(); + for(var i=0, len=this.effects.length;i<len;i++) + this.effects[i] && this.effects[i].loop(timePos); + } +}); + +Effect.Queues = { + instances: $H(), + get: function(queueName) { + if(typeof queueName != 'string') return queueName; + + if(!this.instances[queueName]) + this.instances[queueName] = new Effect.ScopedQueue(); + + return this.instances[queueName]; + } +} +Effect.Queue = Effect.Queues.get('global'); + +Effect.DefaultOptions = { + transition: Effect.Transitions.sinoidal, + duration: 1.0, // seconds + fps: 100, // 100= assume 66fps max. + sync: false, // true for combining + from: 0.0, + to: 1.0, + delay: 0.0, + queue: 'parallel' +} + +Effect.Base = function() {}; +Effect.Base.prototype = { + position: null, + start: function(options) { + function codeForEvent(options,eventName){ + return ( + (options[eventName+'Internal'] ? 'this.options.'+eventName+'Internal(this);' : '') + + (options[eventName] ? 'this.options.'+eventName+'(this);' : '') + ); + } + if(options.transition === false) options.transition = Effect.Transitions.linear; + this.options = Object.extend(Object.extend({},Effect.DefaultOptions), options || {}); + this.currentFrame = 0; + this.state = 'idle'; + this.startOn = this.options.delay*1000; + this.finishOn = this.startOn+(this.options.duration*1000); + this.fromToDelta = this.options.to-this.options.from; + this.totalTime = this.finishOn-this.startOn; + this.totalFrames = this.options.fps*this.options.duration; + + eval('this.render = function(pos){ '+ + 'if(this.state=="idle"){this.state="running";'+ + codeForEvent(options,'beforeSetup')+ + (this.setup ? 'this.setup();':'')+ + codeForEvent(options,'afterSetup')+ + '};if(this.state=="running"){'+ + 'pos=this.options.transition(pos)*'+this.fromToDelta+'+'+this.options.from+';'+ + 'this.position=pos;'+ + codeForEvent(options,'beforeUpdate')+ + (this.update ? 'this.update(pos);':'')+ + codeForEvent(options,'afterUpdate')+ + '}}'); + + this.event('beforeStart'); + if(!this.options.sync) + Effect.Queues.get(typeof this.options.queue == 'string' ? + 'global' : this.options.queue.scope).add(this); + }, + loop: function(timePos) { + if(timePos >= this.startOn) { + if(timePos >= this.finishOn) { + this.render(1.0); + this.cancel(); + this.event('beforeFinish'); + if(this.finish) this.finish(); + this.event('afterFinish'); + return; + } + var pos = (timePos - this.startOn) / this.totalTime, + frame = Math.round(pos * this.totalFrames); + if(frame > this.currentFrame) { + this.render(pos); + this.currentFrame = frame; + } + } + }, + cancel: function() { + if(!this.options.sync) + Effect.Queues.get(typeof this.options.queue == 'string' ? + 'global' : this.options.queue.scope).remove(this); + this.state = 'finished'; + }, + event: function(eventName) { + if(this.options[eventName + 'Internal']) this.options[eventName + 'Internal'](this); + if(this.options[eventName]) this.options[eventName](this); + }, + inspect: function() { + var data = $H(); + for(property in this) + if(typeof this[property] != 'function') data[property] = this[property]; + return '#<Effect:' + data.inspect() + ',options:' + $H(this.options).inspect() + '>'; + } +} + +Effect.Parallel = Class.create(); +Object.extend(Object.extend(Effect.Parallel.prototype, Effect.Base.prototype), { + initialize: function(effects) { + this.effects = effects || []; + this.start(arguments[1]); + }, + update: function(position) { + this.effects.invoke('render', position); + }, + finish: function(position) { + this.effects.each( function(effect) { + effect.render(1.0); + effect.cancel(); + effect.event('beforeFinish'); + if(effect.finish) effect.finish(position); + effect.event('afterFinish'); + }); + } +}); + +Effect.Event = Class.create(); +Object.extend(Object.extend(Effect.Event.prototype, Effect.Base.prototype), { + initialize: function() { + var options = Object.extend({ + duration: 0 + }, arguments[0] || {}); + this.start(options); + }, + update: Prototype.emptyFunction +}); + +Effect.Opacity = Class.create(); +Object.extend(Object.extend(Effect.Opacity.prototype, Effect.Base.prototype), { + initialize: function(element) { + this.element = $(element); + if(!this.element) throw(Effect._elementDoesNotExistError); + // make this work on IE on elements without 'layout' + if(Prototype.Browser.IE && (!this.element.currentStyle.hasLayout)) + this.element.setStyle({zoom: 1}); + var options = Object.extend({ + from: this.element.getOpacity() || 0.0, + to: 1.0 + }, arguments[1] || {}); + this.start(options); + }, + update: function(position) { + this.element.setOpacity(position); + } +}); + +Effect.Move = Class.create(); +Object.extend(Object.extend(Effect.Move.prototype, Effect.Base.prototype), { + initialize: function(element) { + this.element = $(element); + if(!this.element) throw(Effect._elementDoesNotExistError); + var options = Object.extend({ + x: 0, + y: 0, + mode: 'relative' + }, arguments[1] || {}); + this.start(options); + }, + setup: function() { + // Bug in Opera: Opera returns the "real" position of a static element or + // relative element that does not have top/left explicitly set. + // ==> Always set top and left for position relative elements in your stylesheets + // (to 0 if you do not need them) + this.element.makePositioned(); + this.originalLeft = parseFloat(this.element.getStyle('left') || '0'); + this.originalTop = parseFloat(this.element.getStyle('top') || '0'); + if(this.options.mode == 'absolute') { + // absolute movement, so we need to calc deltaX and deltaY + this.options.x = this.options.x - this.originalLeft; + this.options.y = this.options.y - this.originalTop; + } + }, + update: function(position) { + this.element.setStyle({ + left: Math.round(this.options.x * position + this.originalLeft) + 'px', + top: Math.round(this.options.y * position + this.originalTop) + 'px' + }); + } +}); + +// for backwards compatibility +Effect.MoveBy = function(element, toTop, toLeft) { + return new Effect.Move(element, + Object.extend({ x: toLeft, y: toTop }, arguments[3] || {})); +}; + +Effect.Scale = Class.create(); +Object.extend(Object.extend(Effect.Scale.prototype, Effect.Base.prototype), { + initialize: function(element, percent) { + this.element = $(element); + if(!this.element) throw(Effect._elementDoesNotExistError); + var options = Object.extend({ + scaleX: true, + scaleY: true, + scaleContent: true, + scaleFromCenter: false, + scaleMode: 'box', // 'box' or 'contents' or {} with provided values + scaleFrom: 100.0, + scaleTo: percent + }, arguments[2] || {}); + this.start(options); + }, + setup: function() { + this.restoreAfterFinish = this.options.restoreAfterFinish || false; + this.elementPositioning = this.element.getStyle('position'); + + this.originalStyle = {}; + ['top','left','width','height','fontSize'].each( function(k) { + this.originalStyle[k] = this.element.style[k]; + }.bind(this)); + + this.originalTop = this.element.offsetTop; + this.originalLeft = this.element.offsetLeft; + + var fontSize = this.element.getStyle('font-size') || '100%'; + ['em','px','%','pt'].each( function(fontSizeType) { + if(fontSize.indexOf(fontSizeType)>0) { + this.fontSize = parseFloat(fontSize); + this.fontSizeType = fontSizeType; + } + }.bind(this)); + + this.factor = (this.options.scaleTo - this.options.scaleFrom)/100; + + this.dims = null; + if(this.options.scaleMode=='box') + this.dims = [this.element.offsetHeight, this.element.offsetWidth]; + if(/^content/.test(this.options.scaleMode)) + this.dims = [this.element.scrollHeight, this.element.scrollWidth]; + if(!this.dims) + this.dims = [this.options.scaleMode.originalHeight, + this.options.scaleMode.originalWidth]; + }, + update: function(position) { + var currentScale = (this.options.scaleFrom/100.0) + (this.factor * position); + if(this.options.scaleContent && this.fontSize) + this.element.setStyle({fontSize: this.fontSize * currentScale + this.fontSizeType }); + this.setDimensions(this.dims[0] * currentScale, this.dims[1] * currentScale); + }, + finish: function(position) { + if(this.restoreAfterFinish) this.element.setStyle(this.originalStyle); + }, + setDimensions: function(height, width) { + var d = {}; + if(this.options.scaleX) d.width = Math.round(width) + 'px'; + if(this.options.scaleY) d.height = Math.round(height) + 'px'; + if(this.options.scaleFromCenter) { + var topd = (height - this.dims[0])/2; + var leftd = (width - this.dims[1])/2; + if(this.elementPositioning == 'absolute') { + if(this.options.scaleY) d.top = this.originalTop-topd + 'px'; + if(this.options.scaleX) d.left = this.originalLeft-leftd + 'px'; + } else { + if(this.options.scaleY) d.top = -topd + 'px'; + if(this.options.scaleX) d.left = -leftd + 'px'; + } + } + this.element.setStyle(d); + } +}); + +Effect.Highlight = Class.create(); +Object.extend(Object.extend(Effect.Highlight.prototype, Effect.Base.prototype), { + initialize: function(element) { + this.element = $(element); + if(!this.element) throw(Effect._elementDoesNotExistError); + var options = Object.extend({ startcolor: '#ffff99' }, arguments[1] || {}); + this.start(options); + }, + setup: function() { + // Prevent executing on elements not in the layout flow + if(this.element.getStyle('display')=='none') { this.cancel(); return; } + // Disable background image during the effect + this.oldStyle = {}; + if (!this.options.keepBackgroundImage) { + this.oldStyle.backgroundImage = this.element.getStyle('background-image'); + this.element.setStyle({backgroundImage: 'none'}); + } + if(!this.options.endcolor) + this.options.endcolor = this.element.getStyle('background-color').parseColor('#ffffff'); + if(!this.options.restorecolor) + this.options.restorecolor = this.element.getStyle('background-color'); + // init color calculations + this._base = $R(0,2).map(function(i){ return parseInt(this.options.startcolor.slice(i*2+1,i*2+3),16) }.bind(this)); + this._delta = $R(0,2).map(function(i){ return parseInt(this.options.endcolor.slice(i*2+1,i*2+3),16)-this._base[i] }.bind(this)); + }, + update: function(position) { + this.element.setStyle({backgroundColor: $R(0,2).inject('#',function(m,v,i){ + return m+(Math.round(this._base[i]+(this._delta[i]*position)).toColorPart()); }.bind(this)) }); + }, + finish: function() { + this.element.setStyle(Object.extend(this.oldStyle, { + backgroundColor: this.options.restorecolor + })); + } +}); + +Effect.ScrollTo = Class.create(); +Object.extend(Object.extend(Effect.ScrollTo.prototype, Effect.Base.prototype), { + initialize: function(element) { + this.element = $(element); + this.start(arguments[1] || {}); + }, + setup: function() { + Position.prepare(); + var offsets = Position.cumulativeOffset(this.element); + if(this.options.offset) offsets[1] += this.options.offset; + var max = window.innerHeight ? + window.height - window.innerHeight : + document.body.scrollHeight - + (document.documentElement.clientHeight ? + document.documentElement.clientHeight : document.body.clientHeight); + this.scrollStart = Position.deltaY; + this.delta = (offsets[1] > max ? max : offsets[1]) - this.scrollStart; + }, + update: function(position) { + Position.prepare(); + window.scrollTo(Position.deltaX, + this.scrollStart + (position*this.delta)); + } +}); + +/* ------------- combination effects ------------- */ + +Effect.Fade = function(element) { + element = $(element); + var oldOpacity = element.getInlineOpacity(); + var options = Object.extend({ + from: element.getOpacity() || 1.0, + to: 0.0, + afterFinishInternal: function(effect) { + if(effect.options.to!=0) return; + effect.element.hide().setStyle({opacity: oldOpacity}); + }}, arguments[1] || {}); + return new Effect.Opacity(element,options); +} + +Effect.Appear = function(element) { + element = $(element); + var options = Object.extend({ + from: (element.getStyle('display') == 'none' ? 0.0 : element.getOpacity() || 0.0), + to: 1.0, + // force Safari to render floated elements properly + afterFinishInternal: function(effect) { + effect.element.forceRerendering(); + }, + beforeSetup: function(effect) { + effect.element.setOpacity(effect.options.from).show(); + }}, arguments[1] || {}); + return new Effect.Opacity(element,options); +} + +Effect.Puff = function(element) { + element = $(element); + var oldStyle = { + opacity: element.getInlineOpacity(), + position: element.getStyle('position'), + top: element.style.top, + left: element.style.left, + width: element.style.width, + height: element.style.height + }; + return new Effect.Parallel( + [ new Effect.Scale(element, 200, + { sync: true, scaleFromCenter: true, scaleContent: true, restoreAfterFinish: true }), + new Effect.Opacity(element, { sync: true, to: 0.0 } ) ], + Object.extend({ duration: 1.0, + beforeSetupInternal: function(effect) { + Position.absolutize(effect.effects[0].element) + }, + afterFinishInternal: function(effect) { + effect.effects[0].element.hide().setStyle(oldStyle); } + }, arguments[1] || {}) + ); +} + +Effect.BlindUp = function(element) { + element = $(element); + element.makeClipping(); + return new Effect.Scale(element, 0, + Object.extend({ scaleContent: false, + scaleX: false, + restoreAfterFinish: true, + afterFinishInternal: function(effect) { + effect.element.hide().undoClipping(); + } + }, arguments[1] || {}) + ); +} + +Effect.BlindDown = function(element) { + element = $(element); + var elementDimensions = element.getDimensions(); + return new Effect.Scale(element, 100, Object.extend({ + scaleContent: false, + scaleX: false, + scaleFrom: 0, + scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width}, + restoreAfterFinish: true, + afterSetup: function(effect) { + effect.element.makeClipping().setStyle({height: '0px'}).show(); + }, + afterFinishInternal: function(effect) { + effect.element.undoClipping(); + } + }, arguments[1] || {})); +} + +Effect.SwitchOff = function(element) { + element = $(element); + var oldOpacity = element.getInlineOpacity(); + return new Effect.Appear(element, Object.extend({ + duration: 0.4, + from: 0, + transition: Effect.Transitions.flicker, + afterFinishInternal: function(effect) { + new Effect.Scale(effect.element, 1, { + duration: 0.3, scaleFromCenter: true, + scaleX: false, scaleContent: false, restoreAfterFinish: true, + beforeSetup: function(effect) { + effect.element.makePositioned().makeClipping(); + }, + afterFinishInternal: function(effect) { + effect.element.hide().undoClipping().undoPositioned().setStyle({opacity: oldOpacity}); + } + }) + } + }, arguments[1] || {})); +} + +Effect.DropOut = function(element) { + element = $(element); + var oldStyle = { + top: element.getStyle('top'), + left: element.getStyle('left'), + opacity: element.getInlineOpacity() }; + return new Effect.Parallel( + [ new Effect.Move(element, {x: 0, y: 100, sync: true }), + new Effect.Opacity(element, { sync: true, to: 0.0 }) ], + Object.extend( + { duration: 0.5, + beforeSetup: function(effect) { + effect.effects[0].element.makePositioned(); + }, + afterFinishInternal: function(effect) { + effect.effects[0].element.hide().undoPositioned().setStyle(oldStyle); + } + }, arguments[1] || {})); +} + +Effect.Shake = function(element) { + element = $(element); + var oldStyle = { + top: element.getStyle('top'), + left: element.getStyle('left') }; + return new Effect.Move(element, + { x: 20, y: 0, duration: 0.05, afterFinishInternal: function(effect) { + new Effect.Move(effect.element, + { x: -40, y: 0, duration: 0.1, afterFinishInternal: function(effect) { + new Effect.Move(effect.element, + { x: 40, y: 0, duration: 0.1, afterFinishInternal: function(effect) { + new Effect.Move(effect.element, + { x: -40, y: 0, duration: 0.1, afterFinishInternal: function(effect) { + new Effect.Move(effect.element, + { x: 40, y: 0, duration: 0.1, afterFinishInternal: function(effect) { + new Effect.Move(effect.element, + { x: -20, y: 0, duration: 0.05, afterFinishInternal: function(effect) { + effect.element.undoPositioned().setStyle(oldStyle); + }}) }}) }}) }}) }}) }}); +} + +Effect.SlideDown = function(element) { + element = $(element).cleanWhitespace(); + // SlideDown need to have the content of the element wrapped in a container element with fixed height! + var oldInnerBottom = element.down().getStyle('bottom'); + var elementDimensions = element.getDimensions(); + return new Effect.Scale(element, 100, Object.extend({ + scaleContent: false, + scaleX: false, + scaleFrom: window.opera ? 0 : 1, + scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width}, + restoreAfterFinish: true, + afterSetup: function(effect) { + effect.element.makePositioned(); + effect.element.down().makePositioned(); + if(window.opera) effect.element.setStyle({top: ''}); + effect.element.makeClipping().setStyle({height: '0px'}).show(); + }, + afterUpdateInternal: function(effect) { + effect.element.down().setStyle({bottom: + (effect.dims[0] - effect.element.clientHeight) + 'px' }); + }, + afterFinishInternal: function(effect) { + effect.element.undoClipping().undoPositioned(); + effect.element.down().undoPositioned().setStyle({bottom: oldInnerBottom}); } + }, arguments[1] || {}) + ); +} + +Effect.SlideUp = function(element) { + element = $(element).cleanWhitespace(); + var oldInnerBottom = element.down().getStyle('bottom'); + return new Effect.Scale(element, window.opera ? 0 : 1, + Object.extend({ scaleContent: false, + scaleX: false, + scaleMode: 'box', + scaleFrom: 100, + restoreAfterFinish: true, + beforeStartInternal: function(effect) { + effect.element.makePositioned(); + effect.element.down().makePositioned(); + if(window.opera) effect.element.setStyle({top: ''}); + effect.element.makeClipping().show(); + }, + afterUpdateInternal: function(effect) { + effect.element.down().setStyle({bottom: + (effect.dims[0] - effect.element.clientHeight) + 'px' }); + }, + afterFinishInternal: function(effect) { + effect.element.hide().undoClipping().undoPositioned().setStyle({bottom: oldInnerBottom}); + effect.element.down().undoPositioned(); + } + }, arguments[1] || {}) + ); +} + +// Bug in opera makes the TD containing this element expand for a instance after finish +Effect.Squish = function(element) { + return new Effect.Scale(element, window.opera ? 1 : 0, { + restoreAfterFinish: true, + beforeSetup: function(effect) { + effect.element.makeClipping(); + }, + afterFinishInternal: function(effect) { + effect.element.hide().undoClipping(); + } + }); +} + +Effect.Grow = function(element) { + element = $(element); + var options = Object.extend({ + direction: 'center', + moveTransition: Effect.Transitions.sinoidal, + scaleTransition: Effect.Transitions.sinoidal, + opacityTransition: Effect.Transitions.full + }, arguments[1] || {}); + var oldStyle = { + top: element.style.top, + left: element.style.left, + height: element.style.height, + width: element.style.width, + opacity: element.getInlineOpacity() }; + + var dims = element.getDimensions(); + var initialMoveX, initialMoveY; + var moveX, moveY; + + switch (options.direction) { + case 'top-left': + initialMoveX = initialMoveY = moveX = moveY = 0; + break; + case 'top-right': + initialMoveX = dims.width; + initialMoveY = moveY = 0; + moveX = -dims.width; + break; + case 'bottom-left': + initialMoveX = moveX = 0; + initialMoveY = dims.height; + moveY = -dims.height; + break; + case 'bottom-right': + initialMoveX = dims.width; + initialMoveY = dims.height; + moveX = -dims.width; + moveY = -dims.height; + break; + case 'center': + initialMoveX = dims.width / 2; + initialMoveY = dims.height / 2; + moveX = -dims.width / 2; + moveY = -dims.height / 2; + break; + } + + return new Effect.Move(element, { + x: initialMoveX, + y: initialMoveY, + duration: 0.01, + beforeSetup: function(effect) { + effect.element.hide().makeClipping().makePositioned(); + }, + afterFinishInternal: function(effect) { + new Effect.Parallel( + [ new Effect.Opacity(effect.element, { sync: true, to: 1.0, from: 0.0, transition: options.opacityTransition }), + new Effect.Move(effect.element, { x: moveX, y: moveY, sync: true, transition: options.moveTransition }), + new Effect.Scale(effect.element, 100, { + scaleMode: { originalHeight: dims.height, originalWidth: dims.width }, + sync: true, scaleFrom: window.opera ? 1 : 0, transition: options.scaleTransition, restoreAfterFinish: true}) + ], Object.extend({ + beforeSetup: function(effect) { + effect.effects[0].element.setStyle({height: '0px'}).show(); + }, + afterFinishInternal: function(effect) { + effect.effects[0].element.undoClipping().undoPositioned().setStyle(oldStyle); + } + }, options) + ) + } + }); +} + +Effect.Shrink = function(element) { + element = $(element); + var options = Object.extend({ + direction: 'center', + moveTransition: Effect.Transitions.sinoidal, + scaleTransition: Effect.Transitions.sinoidal, + opacityTransition: Effect.Transitions.none + }, arguments[1] || {}); + var oldStyle = { + top: element.style.top, + left: element.style.left, + height: element.style.height, + width: element.style.width, + opacity: element.getInlineOpacity() }; + + var dims = element.getDimensions(); + var moveX, moveY; + + switch (options.direction) { + case 'top-left': + moveX = moveY = 0; + break; + case 'top-right': + moveX = dims.width; + moveY = 0; + break; + case 'bottom-left': + moveX = 0; + moveY = dims.height; + break; + case 'bottom-right': + moveX = dims.width; + moveY = dims.height; + break; + case 'center': + moveX = dims.width / 2; + moveY = dims.height / 2; + break; + } + + return new Effect.Parallel( + [ new Effect.Opacity(element, { sync: true, to: 0.0, from: 1.0, transition: options.opacityTransition }), + new Effect.Scale(element, window.opera ? 1 : 0, { sync: true, transition: options.scaleTransition, restoreAfterFinish: true}), + new Effect.Move(element, { x: moveX, y: moveY, sync: true, transition: options.moveTransition }) + ], Object.extend({ + beforeStartInternal: function(effect) { + effect.effects[0].element.makePositioned().makeClipping(); + }, + afterFinishInternal: function(effect) { + effect.effects[0].element.hide().undoClipping().undoPositioned().setStyle(oldStyle); } + }, options) + ); +} + +Effect.Pulsate = function(element) { + element = $(element); + var options = arguments[1] || {}; + var oldOpacity = element.getInlineOpacity(); + var transition = options.transition || Effect.Transitions.sinoidal; + var reverser = function(pos){ return transition(1-Effect.Transitions.pulse(pos, options.pulses)) }; + reverser.bind(transition); + return new Effect.Opacity(element, + Object.extend(Object.extend({ duration: 2.0, from: 0, + afterFinishInternal: function(effect) { effect.element.setStyle({opacity: oldOpacity}); } + }, options), {transition: reverser})); +} + +Effect.Fold = function(element) { + element = $(element); + var oldStyle = { + top: element.style.top, + left: element.style.left, + width: element.style.width, + height: element.style.height }; + element.makeClipping(); + return new Effect.Scale(element, 5, Object.extend({ + scaleContent: false, + scaleX: false, + afterFinishInternal: function(effect) { + new Effect.Scale(element, 1, { + scaleContent: false, + scaleY: false, + afterFinishInternal: function(effect) { + effect.element.hide().undoClipping().setStyle(oldStyle); + } }); + }}, arguments[1] || {})); +}; + +Effect.Morph = Class.create(); +Object.extend(Object.extend(Effect.Morph.prototype, Effect.Base.prototype), { + initialize: function(element) { + this.element = $(element); + if(!this.element) throw(Effect._elementDoesNotExistError); + var options = Object.extend({ + style: {} + }, arguments[1] || {}); + if (typeof options.style == 'string') { + if(options.style.indexOf(':') == -1) { + var cssText = '', selector = '.' + options.style; + $A(document.styleSheets).reverse().each(function(styleSheet) { + if (styleSheet.cssRules) cssRules = styleSheet.cssRules; + else if (styleSheet.rules) cssRules = styleSheet.rules; + $A(cssRules).reverse().each(function(rule) { + if (selector == rule.selectorText) { + cssText = rule.style.cssText; + throw $break; + } + }); + if (cssText) throw $break; + }); + this.style = cssText.parseStyle(); + options.afterFinishInternal = function(effect){ + effect.element.addClassName(effect.options.style); + effect.transforms.each(function(transform) { + if(transform.style != 'opacity') + effect.element.style[transform.style] = ''; + }); + } + } else this.style = options.style.parseStyle(); + } else this.style = $H(options.style) + this.start(options); + }, + setup: function(){ + function parseColor(color){ + if(!color || ['rgba(0, 0, 0, 0)','transparent'].include(color)) color = '#ffffff'; + color = color.parseColor(); + return $R(0,2).map(function(i){ + return parseInt( color.slice(i*2+1,i*2+3), 16 ) + }); + } + this.transforms = this.style.map(function(pair){ + var property = pair[0], value = pair[1], unit = null; + + if(value.parseColor('#zzzzzz') != '#zzzzzz') { + value = value.parseColor(); + unit = 'color'; + } else if(property == 'opacity') { + value = parseFloat(value); + if(Prototype.Browser.IE && (!this.element.currentStyle.hasLayout)) + this.element.setStyle({zoom: 1}); + } else if(Element.CSS_LENGTH.test(value)) { + var components = value.match(/^([\+\-]?[0-9\.]+)(.*)$/); + value = parseFloat(components[1]); + unit = (components.length == 3) ? components[2] : null; + } + + var originalValue = this.element.getStyle(property); + return { + style: property.camelize(), + originalValue: unit=='color' ? parseColor(originalValue) : parseFloat(originalValue || 0), + targetValue: unit=='color' ? parseColor(value) : value, + unit: unit + }; + }.bind(this)).reject(function(transform){ + return ( + (transform.originalValue == transform.targetValue) || + ( + transform.unit != 'color' && + (isNaN(transform.originalValue) || isNaN(transform.targetValue)) + ) + ) + }); + }, + update: function(position) { + var style = {}, transform, i = this.transforms.length; + while(i--) + style[(transform = this.transforms[i]).style] = + transform.unit=='color' ? '#'+ + (Math.round(transform.originalValue[0]+ + (transform.targetValue[0]-transform.originalValue[0])*position)).toColorPart() + + (Math.round(transform.originalValue[1]+ + (transform.targetValue[1]-transform.originalValue[1])*position)).toColorPart() + + (Math.round(transform.originalValue[2]+ + (transform.targetValue[2]-transform.originalValue[2])*position)).toColorPart() : + transform.originalValue + Math.round( + ((transform.targetValue - transform.originalValue) * position) * 1000)/1000 + transform.unit; + this.element.setStyle(style, true); + } +}); + +Effect.Transform = Class.create(); +Object.extend(Effect.Transform.prototype, { + initialize: function(tracks){ + this.tracks = []; + this.options = arguments[1] || {}; + this.addTracks(tracks); + }, + addTracks: function(tracks){ + tracks.each(function(track){ + var data = $H(track).values().first(); + this.tracks.push($H({ + ids: $H(track).keys().first(), + effect: Effect.Morph, + options: { style: data } + })); + }.bind(this)); + return this; + }, + play: function(){ + return new Effect.Parallel( + this.tracks.map(function(track){ + var elements = [$(track.ids) || $$(track.ids)].flatten(); + return elements.map(function(e){ return new track.effect(e, Object.extend({ sync:true }, track.options)) }); + }).flatten(), + this.options + ); + } +}); + +Element.CSS_PROPERTIES = $w( + 'backgroundColor backgroundPosition borderBottomColor borderBottomStyle ' + + 'borderBottomWidth borderLeftColor borderLeftStyle borderLeftWidth ' + + 'borderRightColor borderRightStyle borderRightWidth borderSpacing ' + + 'borderTopColor borderTopStyle borderTopWidth bottom clip color ' + + 'fontSize fontWeight height left letterSpacing lineHeight ' + + 'marginBottom marginLeft marginRight marginTop maxHeight '+ + 'maxWidth minHeight minWidth opacity outlineColor outlineOffset ' + + 'outlineWidth paddingBottom paddingLeft paddingRight paddingTop ' + + 'right textIndent top width wordSpacing zIndex'); + +Element.CSS_LENGTH = /^(([\+\-]?[0-9\.]+)(em|ex|px|in|cm|mm|pt|pc|\%))|0$/; + +String.prototype.parseStyle = function(){ + var element = document.createElement('div'); + element.innerHTML = '<div style="' + this + '"></div>'; + var style = element.childNodes[0].style, styleRules = $H(); + + Element.CSS_PROPERTIES.each(function(property){ + if(style[property]) styleRules[property] = style[property]; + }); + if(Prototype.Browser.IE && this.indexOf('opacity') > -1) { + styleRules.opacity = this.match(/opacity:\s*((?:0|1)?(?:\.\d*)?)/)[1]; + } + return styleRules; +}; + +Element.morph = function(element, style) { + new Effect.Morph(element, Object.extend({ style: style }, arguments[2] || {})); + return element; +}; + +['getInlineOpacity','forceRerendering','setContentZoom', + 'collectTextNodes','collectTextNodesIgnoreClass','morph'].each( + function(f) { Element.Methods[f] = Element[f]; } +); + +Element.Methods.visualEffect = function(element, effect, options) { + s = effect.dasherize().camelize(); + effect_class = s.charAt(0).toUpperCase() + s.substring(1); + new Effect[effect_class](element, options); + return $(element); +}; + +Element.addMethods();
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/scriptaculous/src/scriptaculous.js b/dom/tests/mochitest/ajax/scriptaculous/src/scriptaculous.js new file mode 100644 index 0000000000..13f91cd6fb --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/src/scriptaculous.js @@ -0,0 +1,58 @@ +// script.aculo.us scriptaculous.js v1.7.1_beta2, Tue May 15 15:15:45 EDT 2007 + +// Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// +// For details, see the script.aculo.us web site: http://script.aculo.us/ + +var Scriptaculous = { + Version: '1.7.1_beta2', + require: function(libraryName) { + // inserting via DOM fails in Safari 2.0, so brute force approach + document.write('<script type="text/javascript" src="'+libraryName+'"></script>'); + }, + REQUIRED_PROTOTYPE: '1.5.1', + load: function() { + function convertVersionString(versionString){ + var r = versionString.split('.'); + return parseInt(r[0])*100000 + parseInt(r[1])*1000 + parseInt(r[2]); + } + + if((typeof Prototype=='undefined') || + (typeof Element == 'undefined') || + (typeof Element.Methods=='undefined') || + (convertVersionString(Prototype.Version) < + convertVersionString(Scriptaculous.REQUIRED_PROTOTYPE))) + throw("script.aculo.us requires the Prototype JavaScript framework >= " + + Scriptaculous.REQUIRED_PROTOTYPE); + + $A(document.getElementsByTagName("script")).findAll( function(s) { + return (s.src && s.src.match(/scriptaculous\.js(\?.*)?$/)) + }).each( function(s) { + var path = s.src.replace(/scriptaculous\.js(\?.*)?$/,''); + var includes = s.src.match(/\?.*load=([a-z,]*)/); + (includes ? includes[1] : 'builder,effects,dragdrop,controls,slider,sound').split(',').each( + function(include) { Scriptaculous.require(path+include+'.js') }); + }); + } +} + +Scriptaculous.load();
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/scriptaculous/src/slider.js b/dom/tests/mochitest/ajax/scriptaculous/src/slider.js new file mode 100644 index 0000000000..b9c96bfbd6 --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/src/slider.js @@ -0,0 +1,277 @@ +// script.aculo.us slider.js v1.7.1_beta2, Tue May 15 15:15:45 EDT 2007 + +// Copyright (c) 2005-2007 Marty Haught, Thomas Fuchs +// +// script.aculo.us is freely distributable under the terms of an MIT-style license. +// For details, see the script.aculo.us web site: http://script.aculo.us/ + +if(!Control) var Control = {}; +Control.Slider = Class.create(); + +// options: +// axis: 'vertical', or 'horizontal' (default) +// +// callbacks: +// onChange(value) +// onSlide(value) +Control.Slider.prototype = { + initialize: function(handle, track, options) { + var slider = this; + + if(handle instanceof Array) { + this.handles = handle.collect( function(e) { return $(e) }); + } else { + this.handles = [$(handle)]; + } + + this.track = $(track); + this.options = options || {}; + + this.axis = this.options.axis || 'horizontal'; + this.increment = this.options.increment || 1; + this.step = parseInt(this.options.step || '1'); + this.range = this.options.range || $R(0,1); + + this.value = 0; // assure backwards compat + this.values = this.handles.map( function() { return 0 }); + this.spans = this.options.spans ? this.options.spans.map(function(s){ return $(s) }) : false; + this.options.startSpan = $(this.options.startSpan || null); + this.options.endSpan = $(this.options.endSpan || null); + + this.restricted = this.options.restricted || false; + + this.maximum = this.options.maximum || this.range.end; + this.minimum = this.options.minimum || this.range.start; + + // Will be used to align the handle onto the track, if necessary + this.alignX = parseInt(this.options.alignX || '0'); + this.alignY = parseInt(this.options.alignY || '0'); + + this.trackLength = this.maximumOffset() - this.minimumOffset(); + + this.handleLength = this.isVertical() ? + (this.handles[0].offsetHeight != 0 ? + this.handles[0].offsetHeight : this.handles[0].style.height.replace(/px$/,"")) : + (this.handles[0].offsetWidth != 0 ? this.handles[0].offsetWidth : + this.handles[0].style.width.replace(/px$/,"")); + + this.active = false; + this.dragging = false; + this.disabled = false; + + if(this.options.disabled) this.setDisabled(); + + // Allowed values array + this.allowedValues = this.options.values ? this.options.values.sortBy(Prototype.K) : false; + if(this.allowedValues) { + this.minimum = this.allowedValues.min(); + this.maximum = this.allowedValues.max(); + } + + this.eventMouseDown = this.startDrag.bindAsEventListener(this); + this.eventMouseUp = this.endDrag.bindAsEventListener(this); + this.eventMouseMove = this.update.bindAsEventListener(this); + + // Initialize handles in reverse (make sure first handle is active) + this.handles.each( function(h,i) { + i = slider.handles.length-1-i; + slider.setValue(parseFloat( + (slider.options.sliderValue instanceof Array ? + slider.options.sliderValue[i] : slider.options.sliderValue) || + slider.range.start), i); + Element.makePositioned(h); // fix IE + Event.observe(h, "mousedown", slider.eventMouseDown); + }); + + Event.observe(this.track, "mousedown", this.eventMouseDown); + Event.observe(document, "mouseup", this.eventMouseUp); + Event.observe(document, "mousemove", this.eventMouseMove); + + this.initialized = true; + }, + dispose: function() { + var slider = this; + Event.stopObserving(this.track, "mousedown", this.eventMouseDown); + Event.stopObserving(document, "mouseup", this.eventMouseUp); + Event.stopObserving(document, "mousemove", this.eventMouseMove); + this.handles.each( function(h) { + Event.stopObserving(h, "mousedown", slider.eventMouseDown); + }); + }, + setDisabled: function(){ + this.disabled = true; + }, + setEnabled: function(){ + this.disabled = false; + }, + getNearestValue: function(value){ + if(this.allowedValues){ + if(value >= this.allowedValues.max()) return(this.allowedValues.max()); + if(value <= this.allowedValues.min()) return(this.allowedValues.min()); + + var offset = Math.abs(this.allowedValues[0] - value); + var newValue = this.allowedValues[0]; + this.allowedValues.each( function(v) { + var currentOffset = Math.abs(v - value); + if(currentOffset <= offset){ + newValue = v; + offset = currentOffset; + } + }); + return newValue; + } + if(value > this.range.end) return this.range.end; + if(value < this.range.start) return this.range.start; + return value; + }, + setValue: function(sliderValue, handleIdx){ + if(!this.active) { + this.activeHandleIdx = handleIdx || 0; + this.activeHandle = this.handles[this.activeHandleIdx]; + this.updateStyles(); + } + handleIdx = handleIdx || this.activeHandleIdx || 0; + if(this.initialized && this.restricted) { + if((handleIdx>0) && (sliderValue<this.values[handleIdx-1])) + sliderValue = this.values[handleIdx-1]; + if((handleIdx < (this.handles.length-1)) && (sliderValue>this.values[handleIdx+1])) + sliderValue = this.values[handleIdx+1]; + } + sliderValue = this.getNearestValue(sliderValue); + this.values[handleIdx] = sliderValue; + this.value = this.values[0]; // assure backwards compat + + this.handles[handleIdx].style[this.isVertical() ? 'top' : 'left'] = + this.translateToPx(sliderValue); + + this.drawSpans(); + if(!this.dragging || !this.event) this.updateFinished(); + }, + setValueBy: function(delta, handleIdx) { + this.setValue(this.values[handleIdx || this.activeHandleIdx || 0] + delta, + handleIdx || this.activeHandleIdx || 0); + }, + translateToPx: function(value) { + return Math.round( + ((this.trackLength-this.handleLength)/(this.range.end-this.range.start)) * + (value - this.range.start)) + "px"; + }, + translateToValue: function(offset) { + return ((offset/(this.trackLength-this.handleLength) * + (this.range.end-this.range.start)) + this.range.start); + }, + getRange: function(range) { + var v = this.values.sortBy(Prototype.K); + range = range || 0; + return $R(v[range],v[range+1]); + }, + minimumOffset: function(){ + return(this.isVertical() ? this.alignY : this.alignX); + }, + maximumOffset: function(){ + return(this.isVertical() ? + (this.track.offsetHeight != 0 ? this.track.offsetHeight : + this.track.style.height.replace(/px$/,"")) - this.alignY : + (this.track.offsetWidth != 0 ? this.track.offsetWidth : + this.track.style.width.replace(/px$/,"")) - this.alignY); + }, + isVertical: function(){ + return (this.axis == 'vertical'); + }, + drawSpans: function() { + var slider = this; + if(this.spans) + $R(0, this.spans.length-1).each(function(r) { slider.setSpan(slider.spans[r], slider.getRange(r)) }); + if(this.options.startSpan) + this.setSpan(this.options.startSpan, + $R(0, this.values.length>1 ? this.getRange(0).min() : this.value )); + if(this.options.endSpan) + this.setSpan(this.options.endSpan, + $R(this.values.length>1 ? this.getRange(this.spans.length-1).max() : this.value, this.maximum)); + }, + setSpan: function(span, range) { + if(this.isVertical()) { + span.style.top = this.translateToPx(range.start); + span.style.height = this.translateToPx(range.end - range.start + this.range.start); + } else { + span.style.left = this.translateToPx(range.start); + span.style.width = this.translateToPx(range.end - range.start + this.range.start); + } + }, + updateStyles: function() { + this.handles.each( function(h){ Element.removeClassName(h, 'selected') }); + Element.addClassName(this.activeHandle, 'selected'); + }, + startDrag: function(event) { + if(Event.isLeftClick(event)) { + if(!this.disabled){ + this.active = true; + + var handle = Event.element(event); + var pointer = [Event.pointerX(event), Event.pointerY(event)]; + var track = handle; + if(track==this.track) { + var offsets = Position.cumulativeOffset(this.track); + this.event = event; + this.setValue(this.translateToValue( + (this.isVertical() ? pointer[1]-offsets[1] : pointer[0]-offsets[0])-(this.handleLength/2) + )); + var offsets = Position.cumulativeOffset(this.activeHandle); + this.offsetX = (pointer[0] - offsets[0]); + this.offsetY = (pointer[1] - offsets[1]); + } else { + // find the handle (prevents issues with Safari) + while((this.handles.indexOf(handle) == -1) && handle.parentNode) + handle = handle.parentNode; + + if(this.handles.indexOf(handle)!=-1) { + this.activeHandle = handle; + this.activeHandleIdx = this.handles.indexOf(this.activeHandle); + this.updateStyles(); + + var offsets = Position.cumulativeOffset(this.activeHandle); + this.offsetX = (pointer[0] - offsets[0]); + this.offsetY = (pointer[1] - offsets[1]); + } + } + } + Event.stop(event); + } + }, + update: function(event) { + if(this.active) { + if(!this.dragging) this.dragging = true; + this.draw(event); + if(Prototype.Browser.WebKit) window.scrollBy(0,0); + Event.stop(event); + } + }, + draw: function(event) { + var pointer = [Event.pointerX(event), Event.pointerY(event)]; + var offsets = Position.cumulativeOffset(this.track); + pointer[0] -= this.offsetX + offsets[0]; + pointer[1] -= this.offsetY + offsets[1]; + this.event = event; + this.setValue(this.translateToValue( this.isVertical() ? pointer[1] : pointer[0] )); + if(this.initialized && this.options.onSlide) + this.options.onSlide(this.values.length>1 ? this.values : this.value, this); + }, + endDrag: function(event) { + if(this.active && this.dragging) { + this.finishDrag(event, true); + Event.stop(event); + } + this.active = false; + this.dragging = false; + }, + finishDrag: function(event, success) { + this.active = false; + this.dragging = false; + this.updateFinished(); + }, + updateFinished: function() { + if(this.initialized && this.options.onChange) + this.options.onChange(this.values.length>1 ? this.values : this.value, this); + this.event = null; + } +}
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/scriptaculous/src/sound.js b/dom/tests/mochitest/ajax/scriptaculous/src/sound.js new file mode 100644 index 0000000000..46618d9012 --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/src/sound.js @@ -0,0 +1,60 @@ +// script.aculo.us sound.js v1.7.1_beta2, Tue May 15 15:15:45 EDT 2007 + +// Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) +// +// Based on code created by Jules Gravinese (http://www.webveteran.com/) +// +// script.aculo.us is freely distributable under the terms of an MIT-style license. +// For details, see the script.aculo.us web site: http://script.aculo.us/ + +Sound = { + tracks: {}, + _enabled: true, + template: + new Template('<embed style="height:0" id="sound_#{track}_#{id}" src="#{url}" loop="false" autostart="true" hidden="true"/>'), + enable: function(){ + Sound._enabled = true; + }, + disable: function(){ + Sound._enabled = false; + }, + play: function(url){ + if(!Sound._enabled) return; + var options = Object.extend({ + track: 'global', url: url, replace: false + }, arguments[1] || {}); + + if(options.replace && this.tracks[options.track]) { + $R(0, this.tracks[options.track].id).each(function(id){ + var sound = $('sound_'+options.track+'_'+id); + sound.Stop && sound.Stop(); + sound.remove(); + }) + this.tracks[options.track] = null; + } + + if(!this.tracks[options.track]) + this.tracks[options.track] = { id: 0 } + else + this.tracks[options.track].id++; + + options.id = this.tracks[options.track].id; + if (Prototype.Browser.IE) { + var sound = document.createElement('bgsound'); + sound.setAttribute('id','sound_'+options.track+'_'+options.id); + sound.setAttribute('src',options.url); + sound.setAttribute('loop','1'); + sound.setAttribute('autostart','true'); + $$('body')[0].appendChild(sound); + } + else + new Insertion.Bottom($$('body')[0], Sound.template.evaluate(options)); + } +}; + +if(Prototype.Browser.Gecko && navigator.userAgent.indexOf("Win") > 0){ + if(navigator.plugins && $A(navigator.plugins).detect(function(p){ return p.name.indexOf('QuickTime') != -1 })) + Sound.template = new Template('<object id="sound_#{track}_#{id}" width="0" height="0" type="audio/mpeg" data="#{url}"/>') + else + Sound.play = function(){} +} diff --git a/dom/tests/mochitest/ajax/scriptaculous/src/unittest.js b/dom/tests/mochitest/ajax/scriptaculous/src/unittest.js new file mode 100644 index 0000000000..9844b1ce8a --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/src/unittest.js @@ -0,0 +1,586 @@ +// script.aculo.us unittest.js v1.7.1_beta2, Tue May 15 15:15:45 EDT 2007 + +// Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) +// (c) 2005-2007 Jon Tirsen (http://www.tirsen.com) +// (c) 2005-2007 Michael Schuerig (http://www.schuerig.de/michael/) +// +// script.aculo.us is freely distributable under the terms of an MIT-style license. +// For details, see the script.aculo.us web site: http://script.aculo.us/ + +// experimental, Firefox-only +Event.simulateMouse = function(element, eventName) { + var options = Object.extend({ + pointerX: 0, + pointerY: 0, + buttons: 0, + ctrlKey: false, + altKey: false, + shiftKey: false, + metaKey: false + }, arguments[2] || {}); + var oEvent = document.createEvent("MouseEvents"); + oEvent.initMouseEvent(eventName, true, true, document.defaultView, + options.buttons, options.pointerX, options.pointerY, options.pointerX, options.pointerY, + options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, 0, $(element)); + + if(this.mark) Element.remove(this.mark); + this.mark = document.createElement('div'); + this.mark.appendChild(document.createTextNode(" ")); + document.body.appendChild(this.mark); + this.mark.style.position = 'absolute'; + this.mark.style.top = options.pointerY + "px"; + this.mark.style.left = options.pointerX + "px"; + this.mark.style.width = "5px"; + this.mark.style.height = "5px;"; + this.mark.style.borderTop = "1px solid red;" + this.mark.style.borderLeft = "1px solid red;" + + if(this.step) + alert('['+new Date().getTime().toString()+'] '+eventName+'/'+Test.Unit.inspect(options)); + + $(element).dispatchEvent(oEvent); +}; + +// Note: Due to a fix in Firefox 1.0.5/6 that probably fixed "too much", this doesn't work in 1.0.6 or DP2. +// You need to downgrade to 1.0.4 for now to get this working +// See https://bugzilla.mozilla.org/show_bug.cgi?id=289940 for the fix that fixed too much +Event.simulateKey = function(element, eventName) { + var options = Object.extend({ + ctrlKey: false, + altKey: false, + shiftKey: false, + metaKey: false, + keyCode: 0, + charCode: 0 + }, arguments[2] || {}); + + var oEvent = document.createEvent("KeyboardEvent"); + oEvent.initKeyEvent(eventName, true, true, window, + options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, + options.keyCode, options.charCode ); + $(element).dispatchEvent(oEvent); +}; + +Event.simulateKeys = function(element, command) { + for(var i=0; i<command.length; i++) { + Event.simulateKey(element,'keypress',{charCode:command.charCodeAt(i)}); + } +}; + +var Test = {} +Test.Unit = {}; + +// security exception workaround +Test.Unit.inspect = Object.inspect; + +Test.Unit.Logger = Class.create(); +Test.Unit.Logger.prototype = { + initialize: function(log) { + this.log = $(log); + if (this.log) { + this._createLogTable(); + } + }, + start: function(testName) { + if (!this.log) return; + this.testName = testName; + this.lastLogLine = document.createElement('tr'); + this.statusCell = document.createElement('td'); + this.nameCell = document.createElement('td'); + this.nameCell.className = "nameCell"; + this.nameCell.appendChild(document.createTextNode(testName)); + this.messageCell = document.createElement('td'); + this.lastLogLine.appendChild(this.statusCell); + this.lastLogLine.appendChild(this.nameCell); + this.lastLogLine.appendChild(this.messageCell); + this.loglines.appendChild(this.lastLogLine); + }, + finish: function(status, summary) { + if (!this.log) return; + this.lastLogLine.className = status; + this.statusCell.innerHTML = status; + this.messageCell.innerHTML = this._toHTML(summary); + this.addLinksToResults(); + }, + message: function(message) { + if (!this.log) return; + this.messageCell.innerHTML = this._toHTML(message); + }, + summary: function(summary) { + if (!this.log) return; + this.logsummary.innerHTML = this._toHTML(summary); + }, + _createLogTable: function() { + this.log.innerHTML = + '<div id="logsummary"></div>' + + '<table id="logtable">' + + '<thead><tr><th>Status</th><th>Test</th><th>Message</th></tr></thead>' + + '<tbody id="loglines"></tbody>' + + '</table>'; + this.logsummary = $('logsummary') + this.loglines = $('loglines'); + }, + _toHTML: function(txt) { + return txt.escapeHTML().replace(/\n/g,"<br/>"); + }, + addLinksToResults: function(){ + $$("tr.failed .nameCell").each( function(td){ // todo: limit to children of this.log + td.title = "Run only this test" + Event.observe(td, 'click', function(){ window.location.search = "?tests=" + td.innerHTML;}); + }); + $$("tr.passed .nameCell").each( function(td){ // todo: limit to children of this.log + td.title = "Run all tests" + Event.observe(td, 'click', function(){ window.location.search = "";}); + }); + } +} + +Test.Unit.Runner = Class.create(); +Test.Unit.Runner.prototype = { + initialize: function(testcases) { + this.options = Object.extend({ + testLog: 'testlog' + }, arguments[1] || {}); + this.options.resultsURL = this.parseResultsURLQueryParameter(); + this.options.tests = this.parseTestsQueryParameter(); + if (this.options.testLog) { + this.options.testLog = $(this.options.testLog) || null; + } + if(this.options.tests) { + this.tests = []; + for(var i = 0; i < this.options.tests.length; i++) { + if(/^test/.test(this.options.tests[i])) { + this.tests.push(new Test.Unit.Testcase(this.options.tests[i], testcases[this.options.tests[i]], testcases["setup"], testcases["teardown"])); + } + } + } else { + if (this.options.test) { + this.tests = [new Test.Unit.Testcase(this.options.test, testcases[this.options.test], testcases["setup"], testcases["teardown"])]; + } else { + this.tests = []; + for(var testcase in testcases) { + if(/^test/.test(testcase)) { + this.tests.push( + new Test.Unit.Testcase( + this.options.context ? ' -> ' + this.options.titles[testcase] : testcase, + testcases[testcase], testcases["setup"], testcases["teardown"] + )); + } + } + } + } + this.currentTest = 0; + this.logger = new Test.Unit.Logger(this.options.testLog); + setTimeout(this.runTests.bind(this), 1000); + }, + parseResultsURLQueryParameter: function() { + return window.location.search.parseQuery()["resultsURL"]; + }, + parseTestsQueryParameter: function(){ + if (window.location.search.parseQuery()["tests"]){ + return window.location.search.parseQuery()["tests"].split(','); + }; + }, + // Returns: + // "ERROR" if there was an error, + // "FAILURE" if there was a failure, or + // "SUCCESS" if there was neither + getResult: function() { + var hasFailure = false; + for(var i=0;i<this.tests.length;i++) { + if (this.tests[i].errors > 0) { + return "ERROR"; + } + if (this.tests[i].failures > 0) { + hasFailure = true; + } + } + if (hasFailure) { + return "FAILURE"; + } else { + return "SUCCESS"; + } + }, + postResults: function() { + if (this.options.resultsURL) { + new Ajax.Request(this.options.resultsURL, + { method: 'get', parameters: 'result=' + this.getResult(), asynchronous: false }); + } + }, + runTests: function() { + var test = this.tests[this.currentTest]; + if (!test) { + // finished! + this.postResults(); + this.logger.summary(this.summary()); + return; + } + if(!test.isWaiting) { + this.logger.start(test.name); + } + test.run(); + if(test.isWaiting) { + this.logger.message("Waiting for " + test.timeToWait + "ms"); + setTimeout(this.runTests.bind(this), test.timeToWait || 1000); + } else { + this.logger.finish(test.status(), test.summary()); + this.currentTest++; + // tail recursive, hopefully the browser will skip the stackframe + this.runTests(); + } + }, + summary: function() { + var assertions = 0; + var failures = 0; + var errors = 0; + var messages = []; + for(var i=0;i<this.tests.length;i++) { + assertions += this.tests[i].assertions; + failures += this.tests[i].failures; + errors += this.tests[i].errors; + } + return ( + (this.options.context ? this.options.context + ': ': '') + + this.tests.length + " tests, " + + assertions + " assertions, " + + failures + " failures, " + + errors + " errors"); + } +} + +Test.Unit.Assertions = Class.create(); +Test.Unit.Assertions.prototype = { + initialize: function() { + this.assertions = 0; + this.failures = 0; + this.errors = 0; + this.messages = []; + }, + summary: function() { + return ( + this.assertions + " assertions, " + + this.failures + " failures, " + + this.errors + " errors" + "\n" + + this.messages.join("\n")); + }, + pass: function() { + this.assertions++; + }, + fail: function(message) { + this.failures++; + this.messages.push("Failure: " + message); + }, + info: function(message) { + this.messages.push("Info: " + message); + }, + error: function(error) { + this.errors++; + this.messages.push(error.name + ": "+ error.message + "(" + Test.Unit.inspect(error) +")"); + }, + status: function() { + if (this.failures > 0) return 'failed'; + if (this.errors > 0) return 'error'; + return 'passed'; + }, + assert: function(expression) { + var message = arguments[1] || 'assert: got "' + Test.Unit.inspect(expression) + '"'; + try { expression ? this.pass() : + this.fail(message); } + catch(e) { this.error(e); } + }, + assertEqual: function(expected, actual) { + var message = arguments[2] || "assertEqual"; + try { (expected == actual) ? this.pass() : + this.fail(message + ': expected "' + Test.Unit.inspect(expected) + + '", actual "' + Test.Unit.inspect(actual) + '"'); } + catch(e) { this.error(e); } + }, + assertInspect: function(expected, actual) { + var message = arguments[2] || "assertInspect"; + try { (expected == actual.inspect()) ? this.pass() : + this.fail(message + ': expected "' + Test.Unit.inspect(expected) + + '", actual "' + Test.Unit.inspect(actual) + '"'); } + catch(e) { this.error(e); } + }, + assertEnumEqual: function(expected, actual) { + var message = arguments[2] || "assertEnumEqual"; + try { $A(expected).length == $A(actual).length && + expected.zip(actual).all(function(pair) { return pair[0] == pair[1] }) ? + this.pass() : this.fail(message + ': expected ' + Test.Unit.inspect(expected) + + ', actual ' + Test.Unit.inspect(actual)); } + catch(e) { this.error(e); } + }, + assertNotEqual: function(expected, actual) { + var message = arguments[2] || "assertNotEqual"; + try { (expected != actual) ? this.pass() : + this.fail(message + ': got "' + Test.Unit.inspect(actual) + '"'); } + catch(e) { this.error(e); } + }, + assertIdentical: function(expected, actual) { + var message = arguments[2] || "assertIdentical"; + try { (expected === actual) ? this.pass() : + this.fail(message + ': expected "' + Test.Unit.inspect(expected) + + '", actual "' + Test.Unit.inspect(actual) + '"'); } + catch(e) { this.error(e); } + }, + assertNotIdentical: function(expected, actual) { + var message = arguments[2] || "assertNotIdentical"; + try { !(expected === actual) ? this.pass() : + this.fail(message + ': expected "' + Test.Unit.inspect(expected) + + '", actual "' + Test.Unit.inspect(actual) + '"'); } + catch(e) { this.error(e); } + }, + assertNull: function(obj) { + var message = arguments[1] || 'assertNull' + try { (obj==null) ? this.pass() : + this.fail(message + ': got "' + Test.Unit.inspect(obj) + '"'); } + catch(e) { this.error(e); } + }, + assertMatch: function(expected, actual) { + var message = arguments[2] || 'assertMatch'; + var regex = new RegExp(expected); + try { (regex.exec(actual)) ? this.pass() : + this.fail(message + ' : regex: "' + Test.Unit.inspect(expected) + ' did not match: ' + Test.Unit.inspect(actual) + '"'); } + catch(e) { this.error(e); } + }, + assertHidden: function(element) { + var message = arguments[1] || 'assertHidden'; + this.assertEqual("none", element.style.display, message); + }, + assertNotNull: function(object) { + var message = arguments[1] || 'assertNotNull'; + this.assert(object != null, message); + }, + assertType: function(expected, actual) { + var message = arguments[2] || 'assertType'; + try { + (actual.constructor == expected) ? this.pass() : + this.fail(message + ': expected "' + Test.Unit.inspect(expected) + + '", actual "' + (actual.constructor) + '"'); } + catch(e) { this.error(e); } + }, + assertNotOfType: function(expected, actual) { + var message = arguments[2] || 'assertNotOfType'; + try { + (actual.constructor != expected) ? this.pass() : + this.fail(message + ': expected "' + Test.Unit.inspect(expected) + + '", actual "' + (actual.constructor) + '"'); } + catch(e) { this.error(e); } + }, + assertInstanceOf: function(expected, actual) { + var message = arguments[2] || 'assertInstanceOf'; + try { + (actual instanceof expected) ? this.pass() : + this.fail(message + ": object was not an instance of the expected type"); } + catch(e) { this.error(e); } + }, + assertNotInstanceOf: function(expected, actual) { + var message = arguments[2] || 'assertNotInstanceOf'; + try { + !(actual instanceof expected) ? this.pass() : + this.fail(message + ": object was an instance of the not expected type"); } + catch(e) { this.error(e); } + }, + assertRespondsTo: function(method, obj) { + var message = arguments[2] || 'assertRespondsTo'; + try { + (obj[method] && typeof obj[method] == 'function') ? this.pass() : + this.fail(message + ": object doesn't respond to [" + method + "]"); } + catch(e) { this.error(e); } + }, + assertReturnsTrue: function(method, obj) { + var message = arguments[2] || 'assertReturnsTrue'; + try { + var m = obj[method]; + if(!m) m = obj['is'+method.charAt(0).toUpperCase()+method.slice(1)]; + m() ? this.pass() : + this.fail(message + ": method returned false"); } + catch(e) { this.error(e); } + }, + assertReturnsFalse: function(method, obj) { + var message = arguments[2] || 'assertReturnsFalse'; + try { + var m = obj[method]; + if(!m) m = obj['is'+method.charAt(0).toUpperCase()+method.slice(1)]; + !m() ? this.pass() : + this.fail(message + ": method returned true"); } + catch(e) { this.error(e); } + }, + assertRaise: function(exceptionName, method) { + var message = arguments[2] || 'assertRaise'; + try { + method(); + this.fail(message + ": exception expected but none was raised"); } + catch(e) { + ((exceptionName == null) || (e.name==exceptionName)) ? this.pass() : this.error(e); + } + }, + assertElementsMatch: function() { + var expressions = $A(arguments), elements = $A(expressions.shift()); + if (elements.length != expressions.length) { + this.fail('assertElementsMatch: size mismatch: ' + elements.length + ' elements, ' + expressions.length + ' expressions'); + return false; + } + elements.zip(expressions).all(function(pair, index) { + var element = $(pair.first()), expression = pair.last(); + if (element.match(expression)) return true; + this.fail('assertElementsMatch: (in index ' + index + ') expected ' + expression.inspect() + ' but got ' + element.inspect()); + }.bind(this)) && this.pass(); + }, + assertElementMatches: function(element, expression) { + this.assertElementsMatch([element], expression); + }, + benchmark: function(operation, iterations) { + var startAt = new Date(); + (iterations || 1).times(operation); + var timeTaken = ((new Date())-startAt); + this.info((arguments[2] || 'Operation') + ' finished ' + + iterations + ' iterations in ' + (timeTaken/1000)+'s' ); + return timeTaken; + }, + _isVisible: function(element) { + element = $(element); + if(!element.parentNode) return true; + this.assertNotNull(element); + if(element.style && Element.getStyle(element, 'display') == 'none') + return false; + + return this._isVisible(element.parentNode); + }, + assertNotVisible: function(element) { + this.assert(!this._isVisible(element), Test.Unit.inspect(element) + " was not hidden and didn't have a hidden parent either. " + ("" || arguments[1])); + }, + assertVisible: function(element) { + this.assert(this._isVisible(element), Test.Unit.inspect(element) + " was not visible. " + ("" || arguments[1])); + }, + benchmark: function(operation, iterations) { + var startAt = new Date(); + (iterations || 1).times(operation); + var timeTaken = ((new Date())-startAt); + this.info((arguments[2] || 'Operation') + ' finished ' + + iterations + ' iterations in ' + (timeTaken/1000)+'s' ); + return timeTaken; + } +} + +Test.Unit.Testcase = Class.create(); +Object.extend(Object.extend(Test.Unit.Testcase.prototype, Test.Unit.Assertions.prototype), { + initialize: function(name, test, setup, teardown) { + Test.Unit.Assertions.prototype.initialize.bind(this)(); + this.name = name; + + if(typeof test == 'string') { + test = test.gsub(/(\.should[^\(]+\()/,'#{0}this,'); + test = test.gsub(/(\.should[^\(]+)\(this,\)/,'#{1}(this)'); + this.test = function() { + eval('with(this){'+test+'}'); + } + } else { + this.test = test || function() {}; + } + + this.setup = setup || function() {}; + this.teardown = teardown || function() {}; + this.isWaiting = false; + this.timeToWait = 1000; + }, + wait: function(time, nextPart) { + this.isWaiting = true; + this.test = nextPart; + this.timeToWait = time; + }, + run: function() { + try { + try { + if (!this.isWaiting) this.setup.bind(this)(); + this.isWaiting = false; + this.test.bind(this)(); + } finally { + if(!this.isWaiting) { + this.teardown.bind(this)(); + } + } + } + catch(e) { this.error(e); } + } +}); + +// *EXPERIMENTAL* BDD-style testing to please non-technical folk +// This draws many ideas from RSpec http://rspec.rubyforge.org/ + +Test.setupBDDExtensionMethods = function(){ + var METHODMAP = { + shouldEqual: 'assertEqual', + shouldNotEqual: 'assertNotEqual', + shouldEqualEnum: 'assertEnumEqual', + shouldBeA: 'assertType', + shouldNotBeA: 'assertNotOfType', + shouldBeAn: 'assertType', + shouldNotBeAn: 'assertNotOfType', + shouldBeNull: 'assertNull', + shouldNotBeNull: 'assertNotNull', + + shouldBe: 'assertReturnsTrue', + shouldNotBe: 'assertReturnsFalse', + shouldRespondTo: 'assertRespondsTo' + }; + Test.BDDMethods = {}; + for(m in METHODMAP) { + Test.BDDMethods[m] = eval( + 'function(){'+ + 'var args = $A(arguments);'+ + 'var scope = args.shift();'+ + 'scope.'+METHODMAP[m]+'.apply(scope,(args || []).concat([this])); }'); + } + [Array.prototype, String.prototype, Number.prototype].each( + function(p){ Object.extend(p, Test.BDDMethods) } + ); +} + +Test.context = function(name, spec, log){ + Test.setupBDDExtensionMethods(); + + var compiledSpec = {}; + var titles = {}; + for(specName in spec) { + switch(specName){ + case "setup": + case "teardown": + compiledSpec[specName] = spec[specName]; + break; + default: + var testName = 'test'+specName.gsub(/\s+/,'-').camelize(); + var body = spec[specName].toString().split('\n').slice(1); + if(/^\{/.test(body[0])) body = body.slice(1); + body.pop(); + body = body.map(function(statement){ + return statement.strip() + }); + compiledSpec[testName] = body.join('\n'); + titles[testName] = specName; + } + } + new Test.Unit.Runner(compiledSpec, { titles: titles, testLog: log || 'testlog', context: name }); +}; + +if ( parent.SimpleTest && parent.runAJAXTest ) (function(){ + var finish = Test.Unit.Logger.prototype.finish; + Test.Unit.Logger.prototype.finish = function(status,summary){ + var match = summary.match(/(\d+).*(\d+).*(\d+)/) + var fail = parseInt(match[2]) + parseInt(match[3]); + var pass = match[1] - fail; + + for ( var i = 0; i < pass; i++ ) + parent.SimpleTest.ok( true, `${this.testName}: ${summary}` ); + + for ( var i = 0; i < fail; i++ ) + parent.SimpleTest.ok( false, `${this.testName}: ${summary}` ); + + return finish.apply( this, arguments ); + }; + + // Intentionally overwrite (to stop the Ajax request) + Test.Unit.Runner.prototype.postResults = parent.runAJAXTest; + + Ajax.InPlaceEditor.prototype.onFailure = function(){}; +})(); diff --git a/dom/tests/mochitest/ajax/scriptaculous/test/unit/_ajax_inplaceeditor_result.html b/dom/tests/mochitest/ajax/scriptaculous/test/unit/_ajax_inplaceeditor_result.html new file mode 100644 index 0000000000..55dcbeae1c --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/test/unit/_ajax_inplaceeditor_result.html @@ -0,0 +1 @@ +Server received: To be edited
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/scriptaculous/test/unit/_ajax_inplaceeditor_text.html b/dom/tests/mochitest/ajax/scriptaculous/test/unit/_ajax_inplaceeditor_text.html new file mode 100644 index 0000000000..a6f35d1324 --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/test/unit/_ajax_inplaceeditor_text.html @@ -0,0 +1 @@ +Text from server
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/scriptaculous/test/unit/_ajax_updater_result.html b/dom/tests/mochitest/ajax/scriptaculous/test/unit/_ajax_updater_result.html new file mode 100644 index 0000000000..5f11f56ce8 --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/test/unit/_ajax_updater_result.html @@ -0,0 +1,20 @@ +Test text + +<script type="text/javascript" language="javascript" charset="utf-8"> +// <![CDATA[ + alert('fragment1 hit!'); +// ]]> +</script> + +more test text + + +<script>alert('fragment2 hit!')</script> + + +even more test text + +<script type="text/javascript"> + alert('fragment3 hit!'); +</script> some other test text + diff --git a/dom/tests/mochitest/ajax/scriptaculous/test/unit/_autocomplete_result.html b/dom/tests/mochitest/ajax/scriptaculous/test/unit/_autocomplete_result.html new file mode 100644 index 0000000000..f03b34ffc7 --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/test/unit/_autocomplete_result.html @@ -0,0 +1,11 @@ +<ul> + <li>test1</li><li>test2</li> + <li>test3</li> +<li><b>BOLD</b></li> + + <li><span class="informal">(GET ME NOT)</span>(GET <ME> INSTEAD)</li> + + <li>Here we have <a href="_autocomplete_result.html">a link</a> which should work</li> + + <li>Here we have some international ©∏Á®Äç†∑rß</li> +</ul>
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/scriptaculous/test/unit/_autocomplete_result_nobr.html b/dom/tests/mochitest/ajax/scriptaculous/test/unit/_autocomplete_result_nobr.html new file mode 100644 index 0000000000..742ff18f1c --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/test/unit/_autocomplete_result_nobr.html @@ -0,0 +1 @@ +<ul><li>test1</li><li>test2</li><li>test3</li><li><b>BOLD</b></li><li><span class="informal">(GET ME NOT)</span>(GET <ME> INSTEAD)</li><li>Here we have <a href="_autocomplete_result.html">a link</a> which should work</li><li>Here we have some international ©∏Á®Äç†∑rß</li></ul>
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/scriptaculous/test/unit/ajax_autocompleter_test.html b/dom/tests/mochitest/ajax/scriptaculous/test/unit/ajax_autocompleter_test.html new file mode 100644 index 0000000000..69c1fadc78 --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/test/unit/ajax_autocompleter_test.html @@ -0,0 +1,232 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>script.aculo.us Unit test file</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script src="../../lib/prototype.js" type="text/javascript"></script> + <script src="../../src/scriptaculous.js" type="text/javascript"></script> + <script src="../../src/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../test.css" type="text/css" /> + <style> + .selected { background-color: #888; } + </style> +</head> +<body> +<h1>script.aculo.us Unit test file</h1> +<p> + Tests for Ajax.Autocompleter in controls.js. +</p> + +<!-- Log output --> +<div id="testlog"> </div> + +<input id="ac_input" type="text" autocomplete="off" /> +<div id="ac_update" style="display:none;border:1px solid black;background-color:white;position:relative;"></div> + +<input id="ac_input_br" type="text" autocomplete="off" /> +<div id="ac_update_br" style="display:none;border:1px solid black;background-color:white;position:relative;"></div> + +<input id="ac2_input" type="text" autocomplete="off" /> +<div id="ac2_update" style="display:none;border:1px solid black;background-color:white;position:relative;"></div> + +<input id="actoken_input" type="text" autocomplete="off" /> +<div id="actoken_update" style="display:none;border:1px solid black;background-color:white;position:relative;"></div> + +<input id="dummy_element" type="text" autocomplete="off" /> + +<!-- Tests follow --> +<script type="text/javascript" language="javascript" charset="utf-8"> +// <![CDATA[ + + + new Test.Unit.Runner({ + + // Integration test, tests the entire cycle + testAjaxAutocompleter: function() { with(this) { + var ac = new Ajax.Autocompleter('ac_input','ac_update','_autocomplete_result.html', + { method: 'get' }); //override so we can use a static for the result + assertInstanceOf(Ajax.Autocompleter, ac); + + // box not visible + assertNotVisible('ac_update'); + + // focus, but box not visible + Event.simulateMouse('ac_input', 'click'); + assertNotVisible('ac_update'); + + Event.simulateKeys('ac_input','abcdefg'); + assertEqual('abcdefg', $('ac_input').value); + + // check box popping up on input + wait(1000, function() { with(this) { + assertVisible('ac_update'); + assertEqual('test1', $('ac_update').firstChild.firstChild.innerHTML); + assertEqual('test2', $('ac_update').firstChild.firstChild.nextSibling.innerHTML); + + // intl. characters return (UTF-8) + assertEqual('Here we have some international ©∏Á®Äç†∑rß', $('ac_update').firstChild.lastChild.innerHTML); + + // first entry should be selected + assert(Element.hasClassName($('ac_update').firstChild.firstChild, 'selected'),'Selected item should have a className of: selected'); + + Event.simulateKey('ac_input','keypress',{keyCode:Event.KEY_DOWN}); + + // second entry should be selected + assert(!Element.hasClassName($('ac_update').firstChild.firstChild),'Item shouldn\'t have a className of: selected'); + assert(Element.hasClassName($('ac_update').firstChild.firstChild.nextSibling, 'selected'),'Second entry should have a className of: selected'); + + // check selecting with <TAB> + Event.simulateKey('ac_input','keypress',{keyCode:Event.KEY_TAB}); + assertEqual('test2',$('ac_input').value); + + // check box going away + wait(500, function() { with(this) { + assertNotVisible('ac_update'); + + // check selecting with mouse click + Event.simulateKeys('ac_input','3'); + assertEqual('test23', $('ac_input').value); + wait(1000, function() { with(this) { + assertVisible('ac_update'); + Event.simulateMouse($('ac_update').firstChild.childNodes[4],'click'); + + wait(1000, function() { with(this) { + // tests if removal of 'informal' nodes and HTML escaping works + assertEqual('(GET <ME> INSTEAD)',$('ac_input').value); + assertNotVisible('ac_update'); + + // check cancelling with <ESC> + Event.simulateKeys('ac_input','abcdefg'); + + wait(1000, function() { with(this) { + assertVisible('ac_update'); + assertEqual('(GET <ME> INSTEAD)abcdefg', $('ac_input').value); + + Event.simulateKey('ac_input','keypress',{keyCode:Event.KEY_DOWN}); + Event.simulateKey('ac_input','keypress',{keyCode:Event.KEY_ESC}); + + assertEqual('(GET <ME> INSTEAD)abcdefg', $('ac_input').value); + }}); + }}); + }}); + }}); + }}); + }}, + + testAfterUpdateElement: function() { with(this) { + var ac = new Ajax.Autocompleter('ac2_input','ac2_update','_autocomplete_result.html', + { method: 'get', + afterUpdateElement: function(element,selectedElement) { + element.value = 'afterupdate:' + selectedElement.tagName; + } + }); + assertInstanceOf(Ajax.Autocompleter, ac); + + Event.simulateMouse('ac2_input', 'click'); + Event.simulateKeys('ac2_input','abcdefg'); + + wait(1000, function() { with(this) { + assertVisible('ac2_update'); + Event.simulateKey('ac2_input','keypress',{keyCode:Event.KEY_TAB}); + + assertEqual('afterupdate:LI',$('ac2_input').value); + }}); + }}, + + testTokenizing: function() { with(this) { + var actoken = new Ajax.Autocompleter('actoken_input','ac_update','_autocomplete_result.html', + { tokens:',', method: 'get' }); + assertInstanceOf(Ajax.Autocompleter, actoken); + + Event.simulateKeys('actoken_input','abc'); + + wait(1000, function() { with(this) { + Event.simulateKey('actoken_input','keypress',{keyCode:Event.KEY_TAB}); + assertEqual('test1',$('actoken_input').value); + Event.simulateKeys('actoken_input',',abc'); + wait(1000, function() { with(this) { + Event.simulateKey('actoken_input','keypress',{keyCode:Event.KEY_DOWN}); + Event.simulateKey('actoken_input','keypress',{keyCode:Event.KEY_TAB}); + assertEqual('test1,test2',$('actoken_input').value); + }}); + }}); + }}, + + // Same integration test, results has no linebreaks + testAjaxAutocompleterNoLinebreaksInResult: function() { with(this) { + var ac = new Ajax.Autocompleter('ac_input_br','ac_update_br','_autocomplete_result_nobr.html', + { method: 'get' }); //override so we can use a static for the result + assertInstanceOf(Ajax.Autocompleter, ac); + + // box not visible + assertNotVisible('ac_update_br'); + + // focus, but box not visible + Event.simulateMouse('ac_input_br', 'click'); + assertNotVisible('ac_update_br'); + + Event.simulateKeys('ac_input_br','abcdefg'); + assertEqual('abcdefg', $('ac_input_br').value); + + // check box popping up on input + wait(1000, function() { with(this) { + assertVisible('ac_update_br'); + assertEqual('test1', $('ac_update_br').firstChild.firstChild.innerHTML); + assertEqual('test2', $('ac_update_br').firstChild.firstChild.nextSibling.innerHTML); + + // intl. characters return (UTF-8) + assertEqual('Here we have some international ©∏Á®Äç†∑rß', $('ac_update_br').firstChild.lastChild.innerHTML); + + // first entry should be selected + assert(Element.hasClassName($('ac_update_br').firstChild.firstChild, 'selected'),'Selected item should have a className of: selected'); + + Event.simulateKey('ac_input_br','keypress',{keyCode:Event.KEY_DOWN}); + + // second entry should be selected + assert(!Element.hasClassName($('ac_update_br').firstChild.firstChild),'Item shouldn\'t have a className of: selected'); + assert(Element.hasClassName($('ac_update_br').firstChild.firstChild.nextSibling, 'selected'),'Second entry should have a className of: selected'); + + // check selecting with <TAB> + Event.simulateKey('ac_input_br','keypress',{keyCode:Event.KEY_TAB}); + assertEqual('test2',$('ac_input_br').value); + + // check box going away + wait(500, function() { with(this) { + assertNotVisible('ac_update_br'); + + // check selecting with mouse click + Event.simulateKeys('ac_input_br','3'); + assertEqual('test23', $('ac_input_br').value); + wait(1000, function() { with(this) { + assertVisible('ac_update_br'); + Event.simulateMouse($('ac_update_br').firstChild.childNodes[4],'click'); + + wait(1000, function() { with(this) { + // tests if removal of 'informal' nodes and HTML escaping works + assertEqual('(GET <ME> INSTEAD)',$('ac_input_br').value); + assertNotVisible('ac_update_br'); + + // check cancelling with <ESC> + Event.simulateKeys('ac_input_br','abcdefg'); + + wait(1000, function() { with(this) { + assertVisible('ac_update_br'); + assertEqual('(GET <ME> INSTEAD)abcdefg', $('ac_input_br').value); + + Event.simulateKey('ac_input_br','keypress',{keyCode:Event.KEY_DOWN}); + Event.simulateKey('ac_input_br','keypress',{keyCode:Event.KEY_ESC}); + + assertEqual('(GET <ME> INSTEAD)abcdefg', $('ac_input_br').value); + }}); + }}); + }}); + }}); + }}); + }} + + }); +// ]]> +</script> +</body> +</html>
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/scriptaculous/test/unit/ajax_inplaceeditor_test.html b/dom/tests/mochitest/ajax/scriptaculous/test/unit/ajax_inplaceeditor_test.html new file mode 100644 index 0000000000..4104d9959d --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/test/unit/ajax_inplaceeditor_test.html @@ -0,0 +1,152 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>script.aculo.us Unit test file</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script src="../../lib/prototype.js" type="text/javascript"></script> + <script src="../../src/scriptaculous.js" type="text/javascript"></script> + <script src="../../src/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../test.css" type="text/css" /> +</head> +<body> +<h1>script.aculo.us Unit test file</h1> +<p> + Tests for Ajax.InPlaceEditor in controls.js +</p> + +<!-- Log output --> +<div id="testlog"> </div> + +<h1 id="tobeedited">To be edited</h1> +<a id="tobeeditedEditControl" href="#">edit</a> + +<div id="tobeeditedMultiLine">First line<br/>Second line<br/>Third line</div> + +<!-- Tests follow --> +<script type="text/javascript" language="javascript" charset="utf-8"> +// <![CDATA[ + + new Test.Unit.Runner({ + + setup: function() { with(this) { + inPlaceEditor = new Ajax.InPlaceEditor($('tobeedited'), '_ajax_inplaceeditor_result.html', { + externalControl: $('tobeeditedEditControl'), + ajaxOptions: {method: 'get'} //override so we can use a static for the result + }); + inPlaceEditorMultiLine = new Ajax.InPlaceEditor($('tobeeditedMultiLine'), '_ajax_inplaceeditor_result.html', { + ajaxOptions: {method: 'get'} //override so we can use a static for the result + }); + }}, + + teardown: function() { with(this) { + inPlaceEditor.dispose(); + }}, + + testDisposesProperly: function() { with(this) { + assertEqual("rgba(0, 0, 0, 0)", Element.getStyle('tobeedited','background-color')); + inPlaceEditor.dispose(); + assertEqual("rgba(0, 0, 0, 0)", Element.getStyle('tobeedited','background-color')); + assertVisible($('tobeedited')); + Event.simulateMouse('tobeedited','click'); + assertVisible($('tobeedited')); + }}, + + testUsesTextAreaWhenMoreThanOneRows: function() { with(this) { + inPlaceEditor.options.rows = 5; + inPlaceEditor.enterEditMode(); + assertEqual("TEXTAREA", document.forms[0].firstChild.tagName); + assertEqual("BR", document.forms[0].childNodes[1].tagName); + }}, + + testCanSpecifyAllTextsThroughOptions: function() { with(this) { + // swedish translation ;-) + inPlaceEditor.options.okText = "spara"; + inPlaceEditor.options.cancelText = "avbryt"; + inPlaceEditor.options.savingText = "Sparar..."; + inPlaceEditor.enterEditMode(); + assertEqual("spara", document.forms[0].lastChild.previousSibling.value); + assertEqual("avbryt", document.forms[0].lastChild.innerHTML); + inPlaceEditor.showSaving(); + assertEqual("Sparar...", $('tobeedited').innerHTML); + }}, + + testCanSpecifyFormIdThroughOptions: function() { with(this) { + inPlaceEditor.enterEditMode(); + // default form id + assertEqual("tobeedited-inplaceeditor", document.forms[0].id); + inPlaceEditor.leaveEditMode(); + inPlaceEditor.options.formId = "myFormId"; + inPlaceEditor.enterEditMode(); + assertEqual("myFormId", document.forms[0].id); + }}, + + testCantEditWhileSaving: function() { with(this) { + inPlaceEditor.onLoading(); + Event.simulateMouse('tobeedited','mouseover'); + assertEqual("rgba(0, 0, 0, 0)", Element.getStyle('tobeedited','background-color')); + Event.simulateMouse('tobeedited','click'); + assertVisible($('tobeedited')); + }}, + + testCallbackFunctionGetsCalled: function() { with(this) { + called = false; + inPlaceEditor.options.callback = function(form) { + called = true; + } + Event.simulateMouse('tobeedited','click'); + Event.simulateMouse(document.forms[0].childNodes[1],'click'); + assert(called, "callback was not called"); + }}, + + testCanUseExternalElementToGoIntoEditMode: function() { with(this) { + Event.simulateMouse('tobeeditedEditControl','click'); + assertNotNull(document.forms[0], "external control didn't work"); + // doesn't work if you click it again while in edit mode + Event.simulateMouse('tobeeditedEditControl','click'); + assertNull(document.forms[1], "external control created two forms"); + assertNotVisible($('tobeeditedEditControl')); + Event.simulateMouse(document.forms[0].childNodes[2],'click'); + assertVisible($('tobeeditedEditControl')); + }}, + + testHasLineBreaksDetectsHTMLLineBreaks: function() { with(this) { + assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("Line 1<br/>Line 2")); + assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("Line 1<br>Line 2")); + assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("<p>Line 1</p>Line 2")); + assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("Line 1<BR/>Line 2")); + assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("Line 1<BR>Line 2")); + assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("<P>Line 1</P>Line 2")); + assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("<P>Line 1<P>Line 2")); + assert(!inPlaceEditorMultiLine.hasHTMLLineBreaks("One line")); + }}, + + testConvertsHTMLLineBreaksIntoNewLines: function() { with(this) { + assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("Line 1<br/>Line 2")); + assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("Line 1<br>Line 2")); + // <p> not supported (too hard) + //assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("<p>Line 1</p>Line 2")); + assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("Line 1<BR/>Line 2")); + assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("Line 1<BR>Line 2")); + // <p> not supported (too hard) + //assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("<P>Line 1</P>Line 2")); + // unclosed <P>s not supported yet + //assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("<P>Line 1<P>Line 2")); + // <p> not supported (too hard) + //assertEqual("Line 1\nLine 2\nLine 3\nLine 4", inPlaceEditorMultiLine.convertHTMLLineBreaks("<P>Line 1</P>Line 2<br>Line 3<p>Line 4</P>")); + assertEqual("One line", inPlaceEditorMultiLine.convertHTMLLineBreaks("One line")); + }}, + + testConvertsParagraphsAndBRsIntoLineBreaks: function() { with(this) { + inPlaceEditorMultiLine.enterEditMode(); + assertEqual("TEXTAREA", document.forms[0].firstChild.tagName); + assertEqual("First line\nSecond line\nThird line", document.forms[0].firstChild.value) + // doesn't automatically determine size yet + //assertEqual(3, document.forms[0].firstChild.rows); + }} + + }); +// ]]> +</script> +</body> +</html> diff --git a/dom/tests/mochitest/ajax/scriptaculous/test/unit/bdd_test.html b/dom/tests/mochitest/ajax/scriptaculous/test/unit/bdd_test.html new file mode 100644 index 0000000000..02cb10bff7 --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/test/unit/bdd_test.html @@ -0,0 +1,138 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>script.aculo.us Unit test file</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script src="../../lib/prototype.js" type="text/javascript"></script> + <script src="../../src/scriptaculous.js" type="text/javascript"></script> + <script src="../../src/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../test.css" type="text/css" /> +</head> +<body> +<h1>script.aculo.us Unit test file</h1> + +<!-- Log output --> +<div id="testlog"> </div> + +<div id="d">initial</div> + +<!-- Tests follow --> +<script type="text/javascript" language="javascript" charset="utf-8"> +// <![CDATA[ +var moo = 0; + +var assertMethods = []; +for(method in Test.Unit.Assertions.prototype) { + if(/^assert/.test(method)) assertMethods.push(method); +} + +var testObj = { + isNice: function(){ + return true; + }, + isBroken: function(){ + return false; + } +} + +Test.context("BDD-style testing",{ + + setup: function() { + $('d').update('setup!'); + moo++; + }, + + teardown: function() { + moo--; + }, + + 'should run setup before each specification': function(){ + assert($('d').innerHTML == 'setup!'); + assert(moo == 1); + }, + + 'should run teardown after each specification': function(){ + assert(moo == 1); + }, + + 'should provide extensions to tie in isSomething and respondsTo object methods': function(){ + Object.extend(testObj, Test.BDDMethods); + + testObj.shouldBe('nice'); + testObj.shouldNotBe('broken'); + + testObj.shouldRespondTo('isNice'); + testObj.shouldRespondTo('isBroken'); + }, + + 'should automatically add extensions to strings': function(){ + 'a'.shouldEqual('a'); + 'a'.shouldNotEqual('b'); + 'a'.shouldNotBeNull(); + 'a'.shouldBeA(String); + + var aString = 'boo!'; + aString.shouldEqual('boo!'); + aString.shouldBeA(String); + aString.shouldNotBeA(Number); + }, + + 'should automatically add extensions to numbers': function(){ + var n = 123; + n.shouldEqual(123); + n.shouldNotEqual(4); + + n.shouldBeA(Number); + n.shouldNotBeA(Test); + }, + + 'should automatically add extensions to arrays': function(){ + ['a'].shouldNotBeA(String); + [1,2,3].shouldBeAn(Array); + [1,2,3].shouldEqualEnum([1,2,3]); + }, + + 'should support the eval() method': function(){ + eval('2*2').shouldEqual(4); + }, + + 'should support equality assertion': function(){ + assertEqual(1, 1); + assertEqual('a', 'a'); + assertEqual(1, '1'); + + var x = 1; + var y = 1; + assertEqual(1, x) + assertEqual(x, y); + }, + + 'should provide all assertions': function(){ + assertMethods.each(function(m){ + assert(typeof this[m] == 'function'); + }.bind(this)); + }, + + 'should support deferred execution': function(){ + wait(10,function(){ + 'a'.shouldEqual('a'); + }); + + wait(10,function(){ + 'a'.shouldEqual('a'); + wait(10,function(){ + 'a'.shouldEqual('a'); + wait(10,function(){ + 'a'.shouldEqual('a'); + }); + }); + }); + } + +}); + +// ]]> +</script> +</body> +</html>
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/scriptaculous/test/unit/builder_test.html b/dom/tests/mochitest/ajax/scriptaculous/test/unit/builder_test.html new file mode 100644 index 0000000000..3475198385 --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/test/unit/builder_test.html @@ -0,0 +1,258 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>script.aculo.us Unit test file</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script src="../../lib/prototype.js" type="text/javascript"></script> + <script src="../../src/scriptaculous.js" type="text/javascript"></script> + <script src="../../src/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../test.css" type="text/css" /> +</head> +<body> +<h1>script.aculo.us Unit test file</h1> +<p> + Tests for builder.js +</p> + +<!-- Log output --> +<div id="testlog"> </div> + +<div id="result"></div> + +<!-- Tests follow --> +<script type="text/javascript" language="javascript" charset="utf-8"> +// <![CDATA[ + + // Serializes a node and its contents to plain old HTML + // IMPORTANT: style attributes can't be correctly serialized cross-browser wise, + // so the contents of style attributes must match what IE thinks is correct + function serializeNode(node){ + if(node.nodeType == 3) return node.nodeValue; + node = $(node); + var tag = node.tagName.toLowerCase(); + return '<' + ([tag].concat($A(node.attributes).map(function(attr){ + // Filter out stuff that we don't need + if(attr.nodeName == '_extended' || attr.nodeName == '_counted' || + typeof attr.nodeValue == 'function' ||!Element.hasAttribute(node, attr.nodeName)) return; + // remove trailing ; in style attributes on Firefox + var value = node.readAttribute(attr.nodeName); + if(attr.nodeName == 'style' && value.endsWith(';')) + value = value.substr(0, value.length-1); + return attr.nodeName + '="' + value + '"' + }).compact().sort())).join(' ') + '>' + $A(node.childNodes).map(serializeNode).join('') + + '</' + tag + '>'; + } + + new Test.Unit.Runner({ + + setup: function() { + $('result').innerHTML = ''; + }, + + testBuilderBasics: function() { with(this) { + var element = Builder.node('div'); + assertEqual('DIV', element.nodeName); + + var element = Builder.node('div',{id:'mydiv'}) + assertEqual('mydiv', element.id); + + var element = Builder.node('div',{id:'mydiv',className:'one two'}) + assertEqual('mydiv', element.id); + assertEqual('one two', element.className); + + var element = Builder.node('span','text 123 <blah>'); + assertEqual('SPAN', element.nodeName); + assertEqual('text 123 <blah>', element.innerHTML); + + var element = Builder.node('span',123); + assertEqual('SPAN', element.nodeName); + assertEqual('123', element.innerHTML); + + var element = Builder.node('span',['test']); + assertEqual('SPAN', element.nodeName); + assertEqual('test', element.innerHTML); + + var element = Builder.node('span',['test',123]); + assertEqual('SPAN', element.nodeName); + assertEqual('test123', element.innerHTML); + + var element = Builder.node('span',{},['test',123]); + assertEqual('SPAN', element.nodeName); + assertEqual('test123', element.innerHTML); + + var element = Builder.node('span',{id:'myspan'},['test',123]); + assertEqual('SPAN', element.nodeName); + assertEqual('myspan', element.id); + assertEqual('test123', element.innerHTML); + + var element = Builder.node('div',[1,2,[3],[[[4],5],6],7,8,[[[[9]]],0]]); + assertEqual('1234567890',element.innerHTML); + + var element = Builder.node('div',[1,'2',['3'],[[[4],'5'],6],7,'8',[[[['9']]],0]]); + assertEqual('1234567890',element.innerHTML); + + var element = Builder.node('div',{id:'mydiv'},[1,2,[3],[[[4],5],6],7,8,[[[[9]]],0]]); + assertEqual('1234567890',element.innerHTML); + + var element = Builder.node('div',{id:'mydiv'},[1,'2',['3'],[[[4],'5'],6],7,'8',[[[['9']]],0]]); + assertEqual('1234567890',element.innerHTML); + assertEqual(10, element.childNodes.length); + + var element = Builder.node('div', Builder.node('span')); + assertEqual(1, element.childNodes.length); + assertEqual('SPAN', element.childNodes[0].tagName); + + var element = Builder.node('div', {id:'mydiv'}, Builder.node('span')); + assertEqual(1, element.childNodes.length); + assertEqual('mydiv', element.id); + assertEqual('SPAN', element.childNodes[0].tagName); + }}, + + testBuilderClassAndFor: function() { with(this) { + var elt = Builder.node('div', { className: 'demoClass' }); + assertEqual('demoClass', elt.className); + var elt = Builder.node('label', { htmlFor: 'mydiv' }); + assertEqual('mydiv', elt.htmlFor); + }}, + + testBuilderAllXHTMLTags: function() { with(this) { + var XHTML_TAGS = [ + 'a','abbr','acronym','address','applet','area', + 'b','bdo','big','blockquote','br','button', + 'caption','cite','code','col','colgroup', + 'dd','del','dfn','div','dl','dt', + 'em', + 'fieldset','form', + 'h1','h2','h3','h4','h5','h6','hr', + 'i','iframe','img','input','ins', + 'kbd', + 'label','legend','li', + 'map', + 'object','ol','optgroup','option', + 'p','param','pre', + 'q', + 'samp','script','select','small','span','strong','style','sub','sup', + 'table','tbody','td','textarea','tfoot','th','thead','tr','tt', + 'ul','var'] + + XHTML_TAGS.each(function(tag) { + try { + var element = Builder.node(tag); + assertNotNull(element, 'Tag "'+tag+'" expected, but was null.'); + assertEqual(tag.toUpperCase(), element.nodeName); + + var element = Builder.node(tag,{id:'tag_'+tag+'_test_id'}); + assertEqual(tag.toUpperCase(), element.nodeName); + assertEqual('tag_'+tag+'_test_id', element.id, 'Setting id attribute for "'+tag+'" failed!'); + } catch(e) { + assert(false, 'Error while creating node of type '+tag+'. Note: Firefox bug in 1.0.X on option and optgroup, fixed in 1.5b1. Internet Explorer 6 doesn\'t support the ABBR tag and has no standard DOM implementation for tables.'); + } + }); + }}, + + // special case, because requires workarounds on IE and Firefox < 1.5 + testBuilderOptionTag: function() { with(this) { + assertEqual('', Builder.node('option').innerHTML); + assertEqual('test', Builder.node('option','test').innerHTML); + assertEqual('', Builder.node('option',{className:'test'}).innerHTML); + assertEqual('test', Builder.node('option',{className:'test'},'test').innerHTML); + assertEqual('test', Builder.node('option',{},'test').innerHTML); + + var selectElement = Builder.node('select'); + var optionElement = Builder.node('option',{className:'test',id:'option_123'},123); + selectElement.appendChild(optionElement); + document.body.appendChild(selectElement); + assertEqual('123', $('option_123').innerHTML); + }}, + + testBuilderContatenation: function() { with(this) { + var element = Builder.node('div', [Builder.node('span')]); + assertEqual('DIV', element.nodeName); + assertEqual(1, element.childNodes.length); + assertEqual('SPAN', element.firstChild.nodeName); + + var element = Builder.node('div', [Builder.node('span'),'text']); + assertEqual(2, element.childNodes.length); + assertEqual(0, element.firstChild.childNodes.length); + assertEqual('DIV', element.nodeName); + assertEqual('SPAN', element.firstChild.nodeName); + assertEqual(3, element.firstChild.nextSibling.nodeType); + + var element = Builder.node('div', [Builder.node('span',[Builder.node('strong','blah')]),'text']); + assertEqual(2, element.childNodes.length); + assertEqual(1, element.firstChild.childNodes.length); + assertEqual('DIV', element.nodeName); + assertEqual('SPAN', element.firstChild.nodeName); + assertEqual('STRONG', element.firstChild.firstChild.nodeName); + assertEqual('blah', element.firstChild.firstChild.innerHTML); + assertEqual(3, element.firstChild.nextSibling.nodeType); + }}, + + testBuilderComplexExample: function() { with(this) { + var element = Builder.node('div',{id:'ghosttrain'},[ + Builder.node('div',{style:'font-weight: bold; font-size: 11px'},[ + Builder.node('h1','Ghost Train'), + "testtext", 2, 3, 4, + Builder.node('ul',[ + Builder.node('li',{onclick:'alert(\'test\')'},'click me') + ]), + ]), + ]); + assertEqual('DIV', element.nodeName); + + $('result').appendChild(element); + + // browsers aren't sure about upper and lower case on elements + assertEqual( + '<div id="ghosttrain"><div style="font-weight: bold; font-size: 11px">' + + '<h1>Ghost Train</h1>testtext234<ul><li onclick="alert(\'test\')">click me</li></ul></div></div>', + serializeNode($('result').childNodes[0])); + }}, + + testBuilderShortcuts: function() { with(this) { + Builder.dump(); + + var element = DIV(SPAN()); + assertEqual('SPAN', element.childNodes[0].tagName); + + var element = DIV({id:'test'},SPAN()); + assertEqual('SPAN', element.childNodes[0].tagName); + + var element = DIV({id:'ghosttrain'},[ + DIV({style:'font-weight: bold; font-size: 11px'},[ + H1('Ghost Train'), + "testtext", 2, 3, 4, + UL([ + LI({onclick:'alert(\'test\')'},'click me') + ]), + ]), + ]); + assertEqual('DIV', element.nodeName); + + $('result').appendChild(element); + + assertEqual( + '<div id="ghosttrain"><div style="font-weight: bold; font-size: 11px">' + + '<h1>Ghost Train</h1>testtext234<ul><li onclick="alert(\'test\')">click me</li></ul></div></div>', + serializeNode($('result').childNodes[0])); + }}, + + testBuilderBuild: function() { with(this) { + ['<span>this is <b>neat!</b></span>',' \n<span>this is <b>neat!</b></span>\n '].each( + function(html){ + var node = Builder.build(html); + assertEqual('<span>this is <b>neat!</b></span>', serializeNode(node)); + }); + }}, + + testBuilderAttributeEscaping: function() { with(this) { + var element = Builder.node('div',{blah:"<foo'bar&baz\"bat>"}); + assertEqual("<foo'bar&baz\"bat>", $(element).readAttribute('blah')); + }} + + }); +// ]]> +</script> +</body> +</html> diff --git a/dom/tests/mochitest/ajax/scriptaculous/test/unit/dragdrop_test.html b/dom/tests/mochitest/ajax/scriptaculous/test/unit/dragdrop_test.html new file mode 100644 index 0000000000..686bd0f6f9 --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/test/unit/dragdrop_test.html @@ -0,0 +1,131 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>script.aculo.us Unit test file</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script src="../../lib/prototype.js" type="text/javascript"></script> + <script src="../../src/scriptaculous.js" type="text/javascript"></script> + <script src="../../src/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../test.css" type="text/css" /> + <style type="text/css" media="screen"> + /* <![CDATA[ */ + #div_absolute_test { position: absolute } + /* ]]> */ + </style> + +</head> +<body> +<h1>script.aculo.us Unit test file</h1> +<p> + Test of drag & drop functions in dragdrop.js +</p> + +<!-- Log output --> +<div id="testlog"> </div> + +<p id="p_test">p_test</p> +<p id="p_test2">p_test2</p> +<p id="p_test3">p_test3</p> +<img id="img_test" src="icon.png" alt="img_text"/> +<div id="droppable_test">droppable_test</div> + +<div id="div_test">div_test</div> +<div id="div_absolute_test">div_absolute_test</div> +<div id="div_absolute_inline_test" style="position:absolute">div_absolute_inline_test</div> + +<div id="droppable_container"> + <div id="d1">droppable_test</div> + <div id="d2">droppable_test</div> +</div> + +<div id="droppable_container_2"> + <div id="d3">droppable_test</div> +</div> + +<!-- Tests follow --> +<script type="text/javascript" language="javascript" charset="utf-8"> +// <![CDATA[ + + new Test.Unit.Runner({ + + testDraggableBasics: function() { with(this) { + var d = new Draggable('p_test'); + assertInstanceOf(Draggable, d); + }}, + + testDraggableStartEffect: function() { with(this) { + var d = new Draggable('p_test2'); + assert(d.options.starteffect, 'There should be a default start effect.'); + d = new Draggable('p_test3', { endeffect: Prototype.EmptyFunction }); + assert(undefined === d.options.startEffect, 'There should be no default start effect.'); + }}, + + testAutoPositioning: function() { with(this) { + assertEqual('static', Element.getStyle('div_test','position')); + new Draggable('div_test'); + new Draggable('div_absolute_test'); + new Draggable('div_absolute_inline_test'); + assertEqual('relative', Element.getStyle('div_test','position')); + assertEqual('absolute', Element.getStyle('div_absolute_test','position')); + assertEqual('absolute', Element.getStyle('div_absolute_inline_test','position')); + }}, + + testDroppbalesBasics: function() { with(this) { + assertEqual(0, Droppables.drops.length); + assertEqual('static', Element.getStyle('droppable_test','position')); + + Droppables.add('droppable_test'); + assertEqual(1, Droppables.drops.length); + assertEqual('relative', Element.getStyle('droppable_test','position')); + + Droppables.remove('droppable_test'); + assertEqual(0, Droppables.drops.length); + + // accept option should take strings or array of strings + Droppables.add('droppable_test',{accept:'document'}); + assertEqual(['document'].inspect(), Droppables.drops[0].accept.inspect()); + Droppables.remove('droppable_test'); + + Droppables.add('droppable_test',{accept:['document','image']}); + assertEqual(['document','image'].inspect(), Droppables.drops[0].accept.inspect()); + Droppables.remove('droppable_test'); + }}, + + testDroppableContainment: function() { with(this) { + // Droppable containers should be cached + Droppables.add('droppable_test', { + containment:'droppable_container' }); + assertEqual(1, Droppables.drops[0]._containers.length); + assertEqual($('droppable_container'), Droppables.drops[0]._containers[0]); + assert(Droppables.isContained($('d1'), Droppables.drops[0])); + assert(Droppables.isContained($('d2'), Droppables.drops[0])); + assert(!Droppables.isContained($('d3'), Droppables.drops[0])); + Droppables.remove('droppable_test'); + + Droppables.add('droppable_test', { + containment:['droppable_container','droppable_container_2'] }); + assertEqual(2, Droppables.drops[0]._containers.length); + assertEqual($('droppable_container'), Droppables.drops[0]._containers[0]); + assertEqual($('droppable_container_2'), Droppables.drops[0]._containers[1]); + assert(Droppables.isContained($('d1'), Droppables.drops[0])); + assert(Droppables.isContained($('d2'), Droppables.drops[0])); + assert(Droppables.isContained($('d3'), Droppables.drops[0])); + Droppables.remove('droppable_test'); + }}, + + testDroppablesIsAffected: function() { with(this) { + Droppables.add('droppable_test'); + + Position.prepare(); + assert(!Droppables.isAffected([-10, -10], null, Droppables.drops[0])); + + var p = Position.page($('droppable_test')); + assert(Droppables.isAffected(p, null, Droppables.drops[0])); + }} + + }, "testlog"); +// ]]> +</script> +</body> +</html> diff --git a/dom/tests/mochitest/ajax/scriptaculous/test/unit/effects_test.html b/dom/tests/mochitest/ajax/scriptaculous/test/unit/effects_test.html new file mode 100644 index 0000000000..4fb6296589 --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/test/unit/effects_test.html @@ -0,0 +1,250 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>script.aculo.us Unit test file</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script src="../../lib/prototype.js" type="text/javascript"></script> + <script src="../../src/scriptaculous.js" type="text/javascript"></script> + <script src="../../src/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../test.css" type="text/css" /> + <style type="text/css" media="screen"> + #rotfl { + color: red; + font-family: serif; + font-style: italic; + font-size: 40px; + background: #fed; + padding: 1em; + width: 400px; + } + .final { + color: #fff; + font-style: italic; + font-size: 20px; + background: #000; + opacity: 0.5; + } + </style> +</head> +<body> +<h1>script.aculo.us Unit test file</h1> +<p> + Tests for effects.js +</p> + +<!-- generated elements go in here --> +<div id="sandbox"></div> + +<!-- Log output --> +<div id="testlog"> </div> + +<div class="morphing blub" style="font-size:25px;color:#f00">Well</div> +<div class="morphing">You know</div> +<div id="blah" style="border:1px solid black;width:100px">Whoo-hoo!</div> + +<div id="error_message">ERROR MESSAGE</div> +<div id="error_message_2">SECOND ERROR MESSAGE</div> +<div id="error_message_3" style="border:1px solid red; width:100px; color: #f00">THIRD ERROR MESSAGE</div> + +<ul class="error-list" id="error_test_ul"> + <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit,</li> + <li>sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</li> + <li>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris</li> + <li>nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in</li> + <li>reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</li> +</ul> + +<div id="rotfl">ROTFL</div> + +<!-- Tests follow --> +<script type="text/javascript" language="javascript" charset="utf-8"> +// <![CDATA[ + + var TAGS = + ['div','span','ol','ul','table','p','h1','h2','h3','h4','h5','h6']; + + var COMBINED_EFFECTS = + ['Fade','Appear','BlindUp','BlindDown','Puff','SwitchOff','DropOut','Shake', + 'SlideUp','SlideDown','Pulsate','Squish','Fold','Grow','Shrink']; + + var COMBINED_RJS_EFFECTS = $w('fade appear blind_up blind_down puff switch_off '+ + 'drop_out shake slide_up slide_down pulsate squish fold grow shrink'); + + var tmp, tmp2; + + new Test.Unit.Runner({ + + setup: function() { with (this) { + $('sandbox').innerHTML = ""; + }}, + + teardown: function() { with(this) { + // remove all queued effects + Effect.Queue.each(function(e) { e.cancel() }); + }}, + + testBackwardsCompat: function() { with(this) { + assertInstanceOf(Effect.Opacity, new Effect2.Fade('sandbox')); + }}, + + testExceptionOnNonExistingElement: function() { with(this) { + assertRaise('ElementDoesNotExistError', + function(){new Effect.Opacity('nothing-to-see-here')}); + assertRaise('ElementDoesNotExistError', + function(){new Effect.Move('nothing-to-see-here')}); + assertRaise('ElementDoesNotExistError', + function(){new Effect.Scale('nothing-to-see-here')}); + assertRaise('ElementDoesNotExistError', + function(){new Effect.Highlight('nothing-to-see-here')}); + }}, + + testEffectsQueue: function() { with(this) { + var e1 = new Effect.Highlight('sandbox'); + var e2 = new Effect.Appear('sandbox'); + + assertEqual(2, Effect.Queue.effects.length); + + tmp = 0; + Effect.Queue.each(function(e) { tmp++ }); + assertEqual(2, tmp); + + // the internal interval timer should be active + assertNotNull(Effect.Queue.interval); + e1.cancel(); + e2.cancel(); + assertEqual(0, Effect.Queue.effects.length); + + // should be inactive after all effects are removed from queue + assertNull(Effect.Queue.interval); + + // should be in e3,e1,e2 order + var e1 = new Effect.Highlight('sandbox'); + var e2 = new Effect.Appear('sandbox',{queue:'end'}); + var e3 = new Effect.Fade('sandbox',{queue:'front'}); + assert(e2.startOn > e1.startOn); + assert(e3.startOn < e1.startOn); + assert(e3.startOn < e2.startOn); + assertEqual(3, Effect.Queue.effects.length); + + Effect.Queue.each(function(e) { e.cancel() }); + assertEqual(0, Effect.Queue.effects.length); + }}, + + testScopedEffectsQueue: function() { with(this) { + var e1 = new Effect.Highlight('sandbox', {queue: {scope:'myscope'} } ); + var e2 = new Effect.Appear('sandbox', {queue: {scope:'myscope'} } ); + var e3 = new Effect.Highlight('sandbox', {queue: {scope:'secondscope'} } ); + var e4 = new Effect.Appear('sandbox'); + + assertEqual(2, Effect.Queues.get('myscope').effects.length); + assertEqual(1, Effect.Queues.get('secondscope').effects.length); + assertEqual(1, Effect.Queues.get('global').effects.length); + assertEqual(Effect.Queue.effects.length, Effect.Queues.get('global').effects.length); + + var tmp = 0; + Effect.Queues.get('myscope').effects.each(function(e) { tmp++ }); + assertEqual(2, tmp); + + // the internal interval timer should be active + assertNotNull(Effect.Queues.get('myscope').interval); + assertNotNull(Effect.Queues.get('secondscope').interval); + assertNotNull(Effect.Queues.get('global').interval); + + e1.cancel(); e2.cancel(); e3.cancel(); e4.cancel(); + + assertEqual(0, Effect.Queues.get('myscope').effects.length); + assertEqual(0, Effect.Queues.get('secondscope').effects.length); + assertEqual(0, Effect.Queues.get('global').effects.length); + + // should be inactive after all effects are removed from queues + assertNull(Effect.Queues.get('myscope').interval); + assertNull(Effect.Queues.get('secondscope').interval); + assertNull(Effect.Queues.get('global').interval); + + // should be in e3 and e4 together and then e1,e2 order + var e1 = new Effect.Highlight('sandbox', {queue: {scope:'myscope'} } ); + var e2 = new Effect.Appear('sandbox', {queue: {position: 'end', scope:'myscope'} } ); + var e3 = new Effect.Fade('sandbox', {queue: {position: 'front', scope:'myscope'} } ); + var e4 = new Effect.Appear('sandbox'); + assert(e2.startOn > e1.startOn); + assert(e3.startOn < e1.startOn); + assert(e3.startOn < e2.startOn); + assert(e3.startOn = e4.startOn); + assertEqual(3, Effect.Queues.get('myscope').effects.length); + + Effect.Queues.get('myscope').each(function(e) { e.cancel() }); + assertEqual(0, Effect.Queues.get('myscope').effects.length); + + Effect.Queues.get('global').each(function(e) { e.cancel() }); + assertEqual(0, Effect.Queues.get('global').effects.length); + + // should only allow the first two effects and ignore the third + var e1 = new Effect.Highlight('sandbox', {queue: {scope:'myscope', limit: 2} } ); + var e2 = new Effect.Appear('sandbox', {queue: {position: 'end', scope:'myscope', limit: 2} } ); + var e3 = new Effect.Fade('sandbox', {queue: {position: 'front', scope:'myscope', limit: 2} } ); + + assertEqual(2, Effect.Queues.get('myscope').effects.length); + }}, + + testEffectMultiple: function() { with(this) { + $('sandbox').appendChild(Builder.node('div',{id:'test_1'})); + $('sandbox').appendChild(Builder.node('div',{id:'test_2'},[Builder.node('div',{id:'test_2a'})])); + $('sandbox').appendChild(Builder.node('div',{id:'test_3'})); + + // only direct child elements + Effect.multiple('sandbox',Effect.Fade); + assertEqual(3, Effect.Queue.effects.length); + + Effect.Queue.each(function(e) { e.cancel() }); + assertEqual(0, Effect.Queue.effects.length); + + // call with array + Effect.multiple(['test_1','test_3'],Effect.Puff); + assertEqual(2, Effect.Queue.effects.length); + }}, + + testEffectTagifyText: function() { with(this) { + $('sandbox').innerHTML = "Blah<strong>bleb</strong> Blub"; + assertEqual(3, $('sandbox').childNodes.length); + Effect.tagifyText('sandbox'); + assertEqual(10, $('sandbox').childNodes.length); + + Effect.multiple('sandbox', Effect.Fade); + assertEqual(10, Effect.Queue.effects.length); + }}, + + // test if all combined effects correctly initialize themselves + testCombinedEffectsInitialize: function() { with(this) { + COMBINED_EFFECTS.each(function(fx,idx){ + info('Effect.'+fx); + $('sandbox').innerHTML = ""; + $('sandbox').appendChild( + Builder.node('div',{id:'test_element'}, + Builder.node('span','test'))); //some effects require a child element + + // should work with new Effect.Blah syntax + var effect = new Effect[fx]('test_element'); + assertEqual(0, effect.currentFrame); + + // and without the 'new' + var effect = Effect[fx]('test_element'); + assertEqual(0, effect.currentFrame); + + // and, for visualEffect + assert($('test_element') == $('test_element').visualEffect(COMBINED_RJS_EFFECTS[idx])); + + // options parsing (shake, squish and grow are special here) + if(!['Shake','Squish','Grow'].include(fx)) { + var effect = Effect[fx]('test_element',{duration:2.0}); + assertEqual(2.0, effect.options.duration, fx); + } + }); + }}, + + }); + +// ]]> +</script> +</body> +</html> diff --git a/dom/tests/mochitest/ajax/scriptaculous/test/unit/element_test.html b/dom/tests/mochitest/ajax/scriptaculous/test/unit/element_test.html new file mode 100644 index 0000000000..88fcb3f417 --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/test/unit/element_test.html @@ -0,0 +1,104 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>script.aculo.us Unit test file</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script src="../../lib/prototype.js" type="text/javascript"></script> + <script src="../../src/scriptaculous.js" type="text/javascript"></script> + <script src="../../src/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../test.css" type="text/css" /> + <style type="text/css" media="screen"> + #style_test_1 { color:rgb(0, 0, 255); background-color: rgb(0, 0, 255); } + blah { color:rgb(0, 255, 0); } + #op2 { opacity:0.5;filter:alpha(opacity=50)progid:DXImageTransform.Microsoft.Blur(strength=10);} + </style> +</head> +<body> +<h1>script.aculo.us Unit test file</h1> +<p> + Tests for Element extensions in effects.js +</p> + +<!-- Log output --> +<div id="testlog"> </div> + +<!-- Test elements follow --> +<div id="test_1" class="a bbbbbbbbbbbb cccccccccc dddd"> </div> + +<div id="test_2" class="classA-foobar classB-foobar"> </div> <span> </span> + +<div id="style_test_1" style="display:none;"></div> +<div id="style_test_2" class="blah" style="font-size:11px;"></div> + +<div id="style_test_3">blah</div> + +<div id="test_whitespace"> <span> </span> + + + +<div><div></div> </div><span> </span> +</div> + +<!-- Test Element opacity functions --> +<img id="op1" alt="op2" src="icon.png" style="opacity:0.5;filter:alpha(opacity=50)" /> +<img id="op2" alt="op2" src="icon.png"/> +<img id="op3" alt="op3" src="icon.png"/> +<img id="op4-ie" alt="op3" src="icon.png" style="filter:alpha(opacity=30)" /> + +<!-- Test Element.childrenWithClassName --> +<div id="Container" class="moo hoo"> + <span id="1" class="firstClass">First class</span> + <span id="2" class="secondClass">Second class</span> + <span id="3" class="firstClass secondClass">First and Second class</span> + <span id="4" class="thirdClass">Third class <span id="5" class="firstClass">Nested First class</span></span> + + <div id="collect">1<span class="ignore"><span class="someclass">2</span>3</span><ul><li class="ignore">4</li></ul></div> + +</div> + +<div id="perftest1"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></div> + +<!-- Tests follow --> +<script type="text/javascript" language="javascript" charset="utf-8"> +// <![CDATA[ + + new Test.Unit.Runner({ + + testElementCollectTextNodes: function() { with(this) { + assertEqual('1234', Element.collectTextNodes('collect')); + assert(benchmark(function(){ + Element.collectTextNodes('collect') + },50) < 1000); + + benchmark(function(){ + Element.collectTextNodes('collect') + },10,'Element.collectTextNodes'); + + assertEqual('1234', Element.collectTextNodesIgnoreClass('collect', 'somethingcompletelydifferent')); + assertEqual('1', $('collect').collectTextNodesIgnoreClass('ignore')); + benchmark(function(){ + Element.collectTextNodesIgnoreClass('collect','ignore') + },10,'Element.collectTextNodesIgnoreClass'); + + assertEqual('134', Element.collectTextNodesIgnoreClass('collect', 'someclass')); + }}, + + testVisualEffect: function() { with(this) { + assert($('style_test_3') == $('style_test_3').visualEffect('fade')); + wait(1500,function(){ + assert(!$('style_test_3').visible()) + }); + }}, + + testParseStylePerformance: function() { with(this) { + benchmark(function(){ + "font:12px/15pt Verdana;opacity:0.4;border:4px dotted red".parseStyle(); + },100); + }} + + }, "testlog"); +// ]]> +</script> +</body> +</html>
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/scriptaculous/test/unit/icon.png b/dom/tests/mochitest/ajax/scriptaculous/test/unit/icon.png Binary files differnew file mode 100644 index 0000000000..f752986b7e --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/test/unit/icon.png diff --git a/dom/tests/mochitest/ajax/scriptaculous/test/unit/loading_test.html b/dom/tests/mochitest/ajax/scriptaculous/test/unit/loading_test.html new file mode 100644 index 0000000000..d36f96ccc1 --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/test/unit/loading_test.html @@ -0,0 +1,41 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>script.aculo.us Unit test file</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script src="../../lib/prototype.js" type="text/javascript"></script> + <script src="../../src/scriptaculous.js?load=effects,dragdrop" type="text/javascript"></script> + <script src="../../src/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../test.css" type="text/css" /> +</head> +<body> +<h1>script.aculo.us Unit test file</h1> +<p> + Test dynamic loading in scriptaculous.js +</p> + +<!-- Log output --> +<div id="testlog"> </div> + +<!-- Tests follow --> +<script type="text/javascript" language="javascript" charset="utf-8"> +// <![CDATA[ + + new Test.Unit.Runner({ + + testDynamicLoading: function() { with(this) { + + // not loaded: controls + assertNull(Ajax.Autocompleter || null); + assertNull(Form.Element.DelayedObserver || null); + + // we loading dragdrop + assertNotNull(Draggable || null); + }} + + }, "testlog"); +// ]]> +</script> +</body> +</html>
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/scriptaculous/test/unit/position_clone_test.html b/dom/tests/mochitest/ajax/scriptaculous/test/unit/position_clone_test.html new file mode 100644 index 0000000000..0644c631b5 --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/test/unit/position_clone_test.html @@ -0,0 +1,312 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>script.aculo.us Unit test file</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script src="../../lib/prototype.js" type="text/javascript"></script> + <script src="../../src/scriptaculous.js" type="text/javascript"></script> + <script src="../../src/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../test.css" type="text/css" /> +</head> +<body> +<h1>script.aculo.us Unit test file</h1> +<p> + Tests for Postion.clone (to be moved to Prototype) +</p> + +<!-- Log output --> +<div id="testlog"> </div> + +<!-- Tests follow --> +<script type="text/javascript" language="javascript" charset="utf-8"> +// <![CDATA[ + + function prepareTarget(contained, position1, position2) { + var target; + if($('target_div')) Element.remove('target_div'); + if($('container_div')) Element.remove('container_div'); + if(contained) { + target = Builder.node('div', + {id: 'container_div', style: 'position:' + position1}, + [Builder.node('div', {id: 'target_div', style: 'position: ' +position2})]); + } else { + target = Builder.node('div', + {id: 'target_div', style: 'position:' + position1}, '456'); + } + document.body.appendChild(target); + Position.clone($('source_div'),$('target_div')); + } + + function prepareTargetHidden(contained, position1, position2) { + var target; + if($('target_div')) Element.remove('target_div'); + if($('container_div')) Element.remove('container_div'); + if(contained) { + target = Builder.node('div', + {id: 'container_div', style: 'position:' + position1}, + [Builder.node('div', {id: 'target_div', style: 'display:none; position: ' +position2})]); + } else { + target = Builder.node('div', + {id: 'target_div', style: 'display:none; position:' + position1}, '456'); + } + document.body.appendChild(target); + Position.clone($('source_div'),$('target_div')); + Element.show($('target_div')); + } + + new Test.Unit.Runner({ + + teardown: function() { + Element.remove($('source_div')); + }, + + testPositionCloneFromAbsolute: function() { with(this) { + var source = Builder.node('div', + {id: 'source_div', style: 'position:absolute; top:20px; left:120px; width:100px; height:50px;'}, '123'); + document.body.appendChild(source); + var expected = Object.inspect([120, 20]); + assertEqual(expected, Object.inspect(Position.page($('source_div')))); + + prepareTarget(false, 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute BODY child"); + + prepareTarget(false, 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed BODY child"); + + prepareTarget(true, 'absolute', 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute child of absolute BODY child"); + + prepareTarget(true, 'absolute', 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed child of absolute BODY child"); + + prepareTarget(true, 'relative', 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute child of relative BODY child"); + + prepareTarget(true, 'relative', 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed child of relative BODY child"); + + prepareTarget(true, 'static', 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute child of static BODY child"); + + prepareTarget(true, 'static', 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed child of static BODY child"); + }}, + + testPositionCloneFromRelative: function() { with(this) { + var source = Builder.node('div', + {id: 'source_div', style: 'position:relative; top:20px; left:120px; width:100px; height:50px;'}, '123'); + document.body.appendChild(source); + var expected = Object.inspect(Position.page($('source_div'))); + assertEqual(expected, Object.inspect(Position.page($('source_div')))); + + prepareTarget(false, 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute BODY child"); + + prepareTarget(false, 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed BODY child"); + + prepareTarget(true, 'absolute', 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute child of absolute BODY child"); + + prepareTarget(true, 'absolute', 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed child of absolute BODY child"); + + prepareTarget(true, 'relative', 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute child of relative BODY child"); + + prepareTarget(true, 'relative', 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed child of relative BODY child"); + + prepareTarget(true, 'static', 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute child of static BODY child"); + + prepareTarget(true, 'static', 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed child of static BODY child"); + }}, + + testPositionCloneFromStatic: function() { with(this) { + var source = Builder.node('div', + {id: 'source_div', style: 'top:20px; left:120px; width:100px; height:50px;'}, '123'); + document.body.appendChild(source); + var expected = Object.inspect(Position.page($('source_div'))); + assertEqual(expected, Object.inspect(Position.page($('source_div')))); + + prepareTarget(false, 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute BODY child"); + + prepareTarget(false, 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed BODY child"); + + prepareTarget(true, 'absolute', 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute child of absolute BODY child"); + + prepareTarget(true, 'absolute', 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed child of absolute BODY child"); + + prepareTarget(true, 'relative', 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute child of relative BODY child"); + + prepareTarget(true, 'relative', 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed child of relative BODY child"); + + prepareTarget(true, 'static', 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute child of static BODY child"); + + prepareTarget(true, 'static', 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed child of static BODY child"); + }}, + + testPositionCloneFromAbsoluteWithHiddenTarget: function() { with(this) { + var source = Builder.node('div', + {id: 'source_div', style: 'position:absolute; top:20px; left:120px; width:100px; height:50px;'}, '123'); + document.body.appendChild(source); + var expected = Object.inspect([120, 20]); + assertEqual(expected, Object.inspect(Position.page($('source_div')))); + + prepareTargetHidden(false, 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute BODY child"); + + prepareTargetHidden(false, 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed BODY child"); + + prepareTargetHidden(true, 'absolute', 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute child of absolute BODY child"); + + prepareTargetHidden(true, 'absolute', 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed child of absolute BODY child"); + + prepareTargetHidden(true, 'relative', 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute child of relative BODY child"); + + prepareTargetHidden(true, 'relative', 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed child of relative BODY child"); + + prepareTargetHidden(true, 'static', 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute child of static BODY child"); + + prepareTargetHidden(true, 'static', 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed child of static BODY child"); + }}, + + testPositionCloneFromRelativeWithHiddenTarget: function() { with(this) { + var source = Builder.node('div', + {id: 'source_div', style: 'position:relative; top:20px; left:120px; width:100px; height:50px;'}, '123'); + document.body.appendChild(source); + var expected = Object.inspect(Position.page($('source_div'))); + assertEqual(expected, Object.inspect(Position.page($('source_div')))); + + prepareTargetHidden(false, 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute BODY child"); + + prepareTargetHidden(false, 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed BODY child"); + + prepareTargetHidden(true, 'absolute', 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute child of absolute BODY child"); + + prepareTargetHidden(true, 'absolute', 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed child of absolute BODY child"); + + prepareTargetHidden(true, 'relative', 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute child of relative BODY child"); + + prepareTargetHidden(true, 'relative', 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed child of relative BODY child"); + + prepareTargetHidden(true, 'static', 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute child of static BODY child"); + + prepareTargetHidden(true, 'static', 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed child of static BODY child"); + }}, + + testPositionCloneFromStaticWithHiddenTarget: function() { with(this) { + var source = Builder.node('div', + {id: 'source_div', style: 'top:20px; left:120px; width:100px; height:50px;'}, '123'); + document.body.appendChild(source); + var expected = Object.inspect(Position.page($('source_div'))); + assertEqual(expected, Object.inspect(Position.page($('source_div')))); + + prepareTargetHidden(false, 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute BODY child"); + + prepareTargetHidden(false, 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed BODY child"); + + prepareTargetHidden(true, 'absolute', 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute child of absolute BODY child"); + + prepareTargetHidden(true, 'absolute', 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed child of absolute BODY child"); + + prepareTargetHidden(true, 'relative', 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute child of relative BODY child"); + + prepareTargetHidden(true, 'relative', 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed child of relative BODY child"); + + prepareTargetHidden(true, 'static', 'absolute'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to absolute child of static BODY child"); + + prepareTargetHidden(true, 'static', 'fixed'); + assertEqual(expected, Object.inspect(Position.page($('target_div'))), + "Clone to fixed child of static BODY child"); + }} + + }); + +// ]]> +</script> + +<!-- Test elements will be inserted after this --> + +</body> +</html>
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/scriptaculous/test/unit/slider_test.html b/dom/tests/mochitest/ajax/scriptaculous/test/unit/slider_test.html new file mode 100644 index 0000000000..018b45d078 --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/test/unit/slider_test.html @@ -0,0 +1,438 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>script.aculo.us Unit test file</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script src="../../lib/prototype.js" type="text/javascript"></script> + <script src="../../src/scriptaculous.js" type="text/javascript"></script> + <script src="../../src/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../test.css" type="text/css" /> +</head> +<body> +<h1>script.aculo.us Unit test file</h1> +<p> + Tests for slider.js +</p> + +<div id="track1" style="width:200px;background-color:#aaa;height:5px;"> + <div id="handle1" style="width:15px;height:25px;background-color:#f00;"> </div> +</div> + +<div id="track2-vertical" style="height:100px;background-color:#aaa;width:5px;"> + <div id="handle2-vertical" style="width:25px;height:10px;background-color:#f00;"> </div> +</div> + +<div id="track2-horizontal" style="height:5px;background-color:#aaa;width:100px;"> + <div id="handle2-horizontal" style="width:10px;height:25px;background-color:#f00;"> </div> +</div> + +<div id="track3" style="width:300px;background-color:#cfb;height:30px;"> + <span id="handle3-1">1</span> + <span id="handle3-2">2</span> + <span id="handle3-3">3</span> +</div> + +<div id="track4" style="width:300px;position:relative;background-color:#cbf;height:30px;"> + <span id="handle4-1" style="top:0;left:0;position:absolute;background-color:#f00;">1</span> + <span id="handle4-2" style="top:0;left:0;position:absolute;background-color:#0f0;">2</span> + <span id="handle4-3" style="top:0;left:0;position:absolute;background-color:#00f;">3</span> +</div> + +<div id="track5" style="width:300px;background-color:#cbf;height:30px;position:relative;z-index:0;"> + <div id="handle5-1" style="top:0;left:0;position:absolute;background-color:#f00;z-index:2">1</div> + <div id="handle5-2" style="top:0;left:0;position:absolute;background-color:#0f0;z-index:2">2</div> + <div id="handle5-3" style="top:0;left:0;position:absolute;background-color:#00f;z-index:2">3</div> + + <div id="span5-1" style="top:0;left:0;position:absolute;background-color:#000;height:20px;z-index:1;"> </div> + <div id="span5-2" style="top:0;left:0;position:absolute;background-color:#444;height:20px;z-index:1;"> </div> +</div> + +<div id="track6" style="width:20px;background-color:#cbf;height:100px;position:relative;z-index:0;"> + <div id="handle6-1" style="top:0;left:0;height:13px;position:absolute;background-color:#f00;z-index:2">1</div> + <div id="handle6-2" style="top:0;left:0;height:13px;position:absolute;background-color:#0f0;z-index:2">2</div> + <div id="handle6-3" style="top:0;left:0;height:13px;position:absolute;background-color:#00f;z-index:2">3</div> + + <div id="span6-1" style="top:0;left:0;position:absolute;background-color:#000;width:20px;z-index:1;"> </div> + <div id="span6-2" style="top:0;left:0;position:absolute;background-color:#444;width:20px;z-index:1;"> </div> +</div> + +<div id="track7" style="width:200px;background-color:#cbf;height:30px;position:relative;z-index:0;"> + <div id="handle7-1" style="top:0;left:0;position:absolute;background-color:#f88;z-index:2">1</div> + <div id="handle7-2" style="top:0;left:0;position:absolute;background-color:#8f8;z-index:2">2</div> + <div id="handle7-3" style="top:0;left:0;position:absolute;background-color:#88f;z-index:2">3</div> + + <div id="span7-1" style="top:0;left:0;position:absolute;background-color:#000;height:20px;z-index:1;"> </div> + <div id="span7-2" style="top:0;left:0;position:absolute;background-color:#444;height:20px;z-index:1;"> </div> + + <div id="span7-start" style="top:0;left:0;position:absolute;background-color:#f00;height:20px;z-index:1;"> </div> + <div id="span7-end" style="top:0;left:0;position:absolute;background-color:#00f;height:20px;z-index:1;"> </div> +</div> + + +<div id="debug"> </div> + +<!-- Log output --> +<div id="testlog"> </div> + +<!-- Tests follow --> +<script type="text/javascript" language="javascript" charset="utf-8"> +// <![CDATA[ + var globalValue; + + new Test.Unit.Runner({ + + testSliderBasics: function() { with(this) { + var slider = new Control.Slider('handle1', 'track1'); + assertInstanceOf(Control.Slider, slider); + assertEqual(Prototype.Browser.IE ? 5 : 4, Event.observers.length); + + assertEqual('horizontal', slider.axis); + assertEqual(false, slider.disabled); + assertEqual(0, slider.value); + + slider.dispose(); + }}, + + testSliderValues: function() { with(this) { + ['horizontal', 'vertical'].each( function(axis) { + var slider = new Control.Slider('handle2-'+axis, 'track2-'+axis, {values:[0.2,0.4,0.6,0.8,1],axis:axis}); + assertEqual(axis, slider.axis); + assertEqual(0.2, slider.value); + + slider.setValue(0.35); + assertEqual(0.4, slider.value); + + slider.setValueBy(0.1); + assertEqual(0.6, slider.value); + slider.setValueBy(-0.6); + assertEqual(0.2, slider.value); + + slider.setValue(1); + assertEqual(1, slider.value); + + slider.setValue(-2); + assertEqual(0.2, slider.value); + + slider.setValue(55555); + assertEqual(1, slider.value); + + // leave active to be able to play around with the sliders + // slider.dispose(); + }); + assertEqual("90px", $('handle2-horizontal').style.left); + assertEqual("90px", $('handle2-vertical').style.top); + }}, + + testSliderInitialValues: function() { with(this) { + var slider = new Control.Slider('handle1', 'track1',{sliderValue:0.5}); + assertEqual(0.5, slider.value); + + var slider = new Control.Slider(['handle4-1','handle4-2','handle4-3'], 'track4', { + sliderValue:[50,145,170], + values:[50,150,160,170,180], + range:$R(50,180) + }); + assertEqual(50, slider.value); + assertEqual(50, slider.values[0]); + assertEqual(150, slider.values[1]); + assertEqual(170, slider.values[2]); + slider.dispose(); + + var slider = new Control.Slider(['handle4-1','handle4-2','handle4-3'], 'track4', { + sliderValue:[50,145,170], + values:[50,150,160,170,180] + }); + assertEqual(50, slider.value); + assertEqual(50, slider.values[0]); + assertEqual(150, slider.values[1]); + assertEqual(170, slider.values[2]); + slider.dispose(); + + var slider = new Control.Slider(['handle4-1','handle4-2','handle4-3'], 'track4', { + restricted:true, + sliderValue:[50,145,170], + values:[50,150,160,170,180] + }); + assertEqual(50, slider.value); + assertEqual(50, slider.values[0]); + assertEqual(150, slider.values[1]); + assertEqual(170, slider.values[2]); + slider.dispose(); + }}, + + testSliderOnChange: function() { with(this) { + var slider = new Control.Slider('handle1', 'track1', { onChange:function(v){ globalValue = v; } }); + slider.setValue(1); + assert(1, globalValue); + assert(1, slider.value); + + slider.setDisabled(); + slider.setValue(0.5); + assert(1, globalValue); + assert(1, slider.value); + + slider.setEnabled(); + slider.setValue(0.2); + assert(0.2, globalValue); + assert(0.2, slider.value); + + // s.event should be null if setValue is called from script + var slider = new Control.Slider(['handle3-1','handle3-2','handle3-3'], 'track3', { + onChange:function(v, s){ if(!s.event) globalValue = v; } }); + + slider.setValue(0.5,1); + assertEqual([0,0.5,0].inspect(), globalValue.inspect()); + assert(!slider.event); + + slider.dispose(); + }}, + + testMultipleHandles: function() { with(this) { + var slider = new Control.Slider(['handle3-1','handle3-2','handle3-3'], 'track3', {range:$R(0,300)}); + assertInstanceOf(Control.Slider, slider); + + slider.setValue(20, 0); + slider.setValue(50, 1); + slider.setValue(70, 2); + assertEqual(20, slider.values[0]); + assertEqual(50, slider.values[1]); + assertEqual(70, slider.values[2]); + assertEqual("20px", slider.handles[0].style.left); + assertEqual("49px", slider.handles[1].style.left); + assertEqual("68px", slider.handles[2].style.left); + + // should change last manipulated handled by -10, + // so check for handle with idx 2 + slider.setValueBy(-10); + assertEqual(60, slider.values[2]); + + slider.setValueBy(10, 0); + assertEqual(20, slider.values[0]); + slider.setValueBy(10, 1); + assertEqual(60, slider.values[1]); + slider.setValueBy(20, slider.activeHandleIdx); + assertEqual(80, slider.values[1]); + }}, + + testMultipleHandlesValues: function() { with(this) { + var slider = new Control.Slider(['handle4-1','handle4-2','handle4-3'], 'track4', {values:[50,150,160,170,180],range:$R(50,180)}); + assertInstanceOf(Control.Slider, slider); + + slider.setValue(20, 0); + slider.setValue(150, 2); + slider.setValue(179, 1); + + assertEqual(50, slider.values[0]); + assertEqual(150, slider.values[2]); + assertEqual(180, slider.values[1]); + + assertEqual("0px", slider.handles[0].style.left); + assertEqual("225px", slider.handles[2].style.left); + assertEqual("293px", slider.handles[1].style.left); + + assertEqual($R(50,150).inspect(), slider.getRange().inspect()); + assertEqual(30, slider.getRange(1).end-slider.getRange(1).start); + }}, + + testMultipleHandlesSpans: function() { with(this) { + var slider = new Control.Slider(['handle5-1','handle5-2','handle5-3'], 'track5', + {spans:['span5-1','span5-2'],range:$R(0,300)}); + assertInstanceOf(Control.Slider, slider); + + slider.setValue(20, 0); + slider.setValue(100, 1); + slider.setValue(150, 2); + + assertEqual("20px", $('span5-1').style.left); + assertEqual("78px", $('span5-1').style.width); + assertEqual("98px", $('span5-2').style.left); + assertEqual("49px", $('span5-2').style.width); + + slider.setValue(30, 0); + slider.setValue(110, 1); + slider.setValue(90, 2); + + assertEqual("29px", $('span5-1').style.left); + assertEqual("59px", $('span5-1').style.width); + assertEqual("88px", $('span5-2').style.left); + assertEqual("20px", $('span5-2').style.width); + + slider.dispose(); + }}, + + testMultipleHandlesSpansStartEnd: function() { with(this) { + var slider = new Control.Slider(['handle7-1','handle7-2','handle7-3'], 'track7', + { spans:['span7-1','span7-2'], + startSpan:'span7-start', + endSpan:'span7-end', + range:$R(0,200) }); + assertInstanceOf(Control.Slider, slider); + + slider.setValue(20, 0); + slider.setValue(100, 1); + slider.setValue(150, 2); + assertEqual("0px", $('span7-start').style.left); + assertEqual("19px", $('span7-start').style.width); + assertEqual("145px", $('span7-end').style.left); + assertEqual("48px", $('span7-end').style.width); + + slider.dispose(); + }}, + + testSingleHandleSpansStartEnd: function() { with(this) { + var slider = new Control.Slider('handle7-1', 'track7', + { spans:['span7-1','span7-2'], + startSpan:'span7-start', + endSpan:'span7-end', + range:$R(0,200) }); + assertInstanceOf(Control.Slider, slider); + + slider.setValue(20, 0); + assertEqual("0px", $('span7-start').style.left); + assertEqual("19px", $('span7-start').style.width); + assertEqual("19px", $('span7-end').style.left); + assertEqual("174px", $('span7-end').style.width); + + slider.dispose(); + }}, + + testMultipleHandlesStyles: function() { with(this) { + var slider = new Control.Slider(['handle7-1','handle7-2','handle7-3'], 'track7', + { spans:['span7-1','span7-2'], + startSpan:'span7-start', + endSpan:'span7-end', + range:$R(0,200) }); + assertInstanceOf(Control.Slider, slider); + assert(Element.hasClassName('handle7-1','selected')); + assert(!Element.hasClassName('handle7-2','selected')); + assert(!Element.hasClassName('handle7-3','selected')); + + slider.setValue(20, 0); + assert(Element.hasClassName('handle7-1','selected')); + assert(!Element.hasClassName('handle7-2','selected')); + assert(!Element.hasClassName('handle7-3','selected')); + + slider.setValue(100, 1); + assert(!Element.hasClassName('handle7-1','selected')); + assert(Element.hasClassName('handle7-2','selected')); + assert(!Element.hasClassName('handle7-3','selected')); + + slider.setValue(150, 2); + assert(!Element.hasClassName('handle7-1','selected')); + assert(!Element.hasClassName('handle7-2','selected')); + assert(Element.hasClassName('handle7-3','selected')); + + slider.dispose(); + }}, + + testMultipleHandlesSpansRestricted: function() { with(this) { + var slider = new Control.Slider(['handle5-1','handle5-2','handle5-3'], 'track5', + {restricted:true,spans:['span5-1','span5-2'],range:$R(0,300)}); + assertInstanceOf(Control.Slider, slider); + + slider.setValue(20, 0); + slider.setValue(100, 1); + slider.setValue(150, 2); + assertEqual(0, slider.values[0]); + assertEqual(0, slider.values[1]); + assertEqual(150, slider.values[2]); + + slider.setValue(150, 2); + slider.setValue(100, 1); + slider.setValue(20, 0); + assertEqual(20, slider.values[0]); + assertEqual(100, slider.values[1]); + assertEqual(150, slider.values[2]); + assertEqual("20px", $('span5-1').style.left); + assertEqual("78px", $('span5-1').style.width); + assertEqual("98px", $('span5-2').style.left); + assertEqual("49px", $('span5-2').style.width); + + slider.setValue(30, 0); + slider.setValue(110, 1); + slider.setValue(90, 2); + assertEqual(30, slider.values[0]); + assertEqual(110, slider.values[1]); + assertEqual(110, slider.values[2], '???'); + + assertEqual("29px", $('span5-1').style.left); + assertEqual("78px", $('span5-1').style.width); + assertEqual("107px", $('span5-2').style.left); + assertEqual("0px", $('span5-2').style.width); + + slider.dispose(); + }}, + + testMultipleHandlesSpansVertical: function() { with(this) { + var slider = new Control.Slider(['handle6-1','handle6-2','handle6-3'], 'track6', {axis:'vertical',spans:['span6-1','span6-2'],range:$R(0,100)}); + assertInstanceOf(Control.Slider, slider); + + slider.setValue(20, 0); + slider.setValue(80, 1); + slider.setValue(90, 2); + + assertEqual("17px", $('span6-1').style.top); + assertEqual("52px", $('span6-1').style.height); + assertEqual("70px", $('span6-2').style.top); + assertEqual("9px", $('span6-2').style.height); + + slider.setValue(30, 0); + slider.setValue(20, 1); + slider.setValue(95, 2); + + assertEqual("17px", $('span6-1').style.top); + assertEqual("9px", $('span6-1').style.height); + assertEqual("26px", $('span6-2').style.top); + assertEqual("57px", $('span6-2').style.height); + }}, + + testRange: function() { with(this) { + var slider = new Control.Slider('handle1','track1'); + assertEqual(0, slider.value); + slider.setValue(1); + assertEqual("185px", $('handle1').style.left); + slider.dispose(); + + var slider = new Control.Slider('handle1','track1',{range:$R(10,20)}); + assertEqual(10, slider.value); + assertEqual("0px", $('handle1').style.left); + slider.setValue(15); + assertEqual("93px", $('handle1').style.left); + slider.setValue(20); + assertEqual("185px", $('handle1').style.left); + slider.dispose(); + }}, + + // test for #3732 + testRangeValues: function() { with(this) { + // test for non-zero starting range + var slider = new Control.Slider('handle1','track1',{ + range:$R(1,3), values:[1,2,3] + }); + assertEqual(1, slider.value); + assertEqual("0px", $('handle1').style.left); + slider.setValue(2); + assertEqual("93px", $('handle1').style.left); + slider.setValue(3); + assertEqual("185px", $('handle1').style.left); + slider.dispose(); + + // test zero-starting range + var slider = new Control.Slider('handle1','track1',{ + range:$R(0,2), values:[0,1,2] + }); + assertEqual(0, slider.value); + assertEqual("0px", $('handle1').style.left); + slider.setValue(1); + assertEqual("93px", $('handle1').style.left); + slider.setValue(2); + assertEqual("185px", $('handle1').style.left); + slider.dispose(); + }} + + }, "testlog"); +// ]]> +</script> +</body> +</html>
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/scriptaculous/test/unit/sortable_test.html b/dom/tests/mochitest/ajax/scriptaculous/test/unit/sortable_test.html new file mode 100644 index 0000000000..c2bafd0b9d --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/test/unit/sortable_test.html @@ -0,0 +1,205 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>script.aculo.us Unit test file</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script src="../../lib/prototype.js" type="text/javascript"></script> + <script src="../../src/scriptaculous.js" type="text/javascript"></script> + <script src="../../src/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../test.css" type="text/css" /> +</head> +<body> +<h1>script.aculo.us Unit test file</h1> +<p> + Test of sortable functions in dragdrop.js +</p> + +<!-- Log output --> +<div id="testlog"> </div> + + +<ul id="sortable1"> + <li id="item_1" class="a">item 1</li> + <li id="item_2" class="c b">item 1<ul><li id="item_99">!!!</li></ul></li> + <li id="item_3" class="b">item 1</li> + <li id="item_xy" class="x y">item 1</li> + <!-- a comment --> +</ul> + +<div id="sortable2"> + <div id="item_4">item 4</div> <div id="item_5">item 5</div> + <img src="icon.png" alt="img"/> + <!-- a comment --> +</div> + +<div id="sortable3"> + +</div> + +<!-- + By default, _ is the only valid seperator + for the DOM ids. Complex element ids as in + the form of "some_element_id_1" should also + be parsed correctly (only the last part should + be serialized) +--> +<ul id="sortable_complex"> + <li id="a_b_item_1" class="a">item 1</li> + <li id="ab_item_2" class="c b">item 1 + <ul> + <li id="item_99">!!!</li> + </ul> + </li> + <li id="a-b-item_3z_33" class="b">item 1</li> + <li id="a-item-xy" class="x y">item 1</li> + <!-- a comment --> +</ul> + + +<ul id="sortable_specialcreate"> + <li id="y1item">item 1</li> + <li id="y2item">item 1<ul><li id="yyyy9928282hjhd">!!!</li></ul></li> +</ul> + +<ul id="sortable_specialformat"> + <li id="x1item">item 1</li> + <li id="x2item">item 1<ul><li id="xxxxx88888item">!!!</li></ul></li> +</ul> + +<!-- Tests follow --> +<script type="text/javascript" language="javascript" charset="utf-8"> +// <![CDATA[ + + new Test.Unit.Runner({ + + setup: function() { with(this) { + Sortable.create('sortable1',{only:['a','b']}); + Sortable.create('sortable2',{tag:'div'}); + Sortable.create('sortable3'); + Sortable.create('sortable_specialcreate',{ format:/(\d+)/ }); + Sortable.create('sortable_specialformat'); + Sortable.create('sortable_complex'); + }}, + + teardown: function() { with(this) { + Sortable.destroy('sortable1'); + Sortable.destroy('sortable2'); + Sortable.destroy('sortable3'); + Sortable.destroy('sortable_specialformat'); + Sortable.destroy('sortable_specialcreate'); + Sortable.destroy('sortable_complex'); + }}, + + testSortableSerializeSequenceBasics: function() { with(this) { + assertEqual('sortable1[]=1&sortable1[]=2&sortable1[]=3', Sortable.serialize('sortable1')); + + // test empty sortable + assertEqual('', Sortable.serialize('sortable3')); + assertEnumEqual([], Sortable.sequence('sortable3')); + + Element.remove('item_4'); + assertEqual('sortable2[]=5', Sortable.serialize('sortable2')); + assertEnumEqual(['5'], Sortable.sequence('sortable2')); + }}, + + testSortableSerializeFormat: function() { with(this) { + // should correctly serialize from option given to Sortable.create() + assertEqual('sortable_specialcreate[]=1&sortable_specialcreate[]=2', + Sortable.serialize('sortable_specialcreate')); + + benchmark(function(){ + Sortable.serialize('sortable_specialcreate') + },1,'Sortable.serialize'); + + // test special format given explicitly + assertEqual('sortable_specialformat[]=1&sortable_specialformat[]=2', + Sortable.serialize('sortable_specialformat', {format:/(\d+)/})); + + // return full id + assertEqual('sortable_specialformat[]=x1item&sortable_specialformat[]=x2item', + Sortable.serialize('sortable_specialformat', {format:/(.*)/})); + + // test default format given explicitly + assertEqual('sortable1[]=1&sortable1[]=2&sortable1[]=3', + Sortable.serialize('sortable1',{format:/^[^_]*_(.*)$/})); + + // Ensure default options.format handles longer, more complex list + // item IDs + assertEqual('sortable_complex[]=1&sortable_complex[]=2&sortable_complex[]=33&sortable_complex[]=', + Sortable.serialize('sortable_complex')); + }}, + + testSortableSerializeRule: function() { with(this) { + var ids = ['x','x-y','test_test','x_y_z','x_y-x_z']; + ids.each(function(id){ + assertEqual('1', + (id+'_1').match(Sortable.SERIALIZE_RULE)[1]); + }); + + assertNull('x'.match(Sortable.SERIALIZE_RULE)); + }}, + + testSortableSerializeName: function() { with(this) { + assertEqual('dumdidu[]=1&dumdidu[]=2', + Sortable.serialize('sortable_specialcreate',{name:'dumdidu'})); + }}, + + testSortableSequenceFormat: function() { with(this) { + // shauld correctly serialize from option given to Sortable.create() + assertEnumEqual(['1','2'], + Sortable.sequence('sortable_specialcreate')); + + // test special format given explicitly + assertEnumEqual(['1','2'], + Sortable.sequence('sortable_specialformat', {format:/(\d+)/})); + + // return full id + assertEnumEqual(['x1item','x2item'], + Sortable.sequence('sortable_specialformat', {format:/(.*)/})); + + // test default format given explicitly + assertEnumEqual(['1','2','3'], + Sortable.sequence('sortable1',{format:/^[^_]*_(.*)$/})); + }}, + + testSortableFindElements: function() { with(this) { + assertEqual(3, Sortable.findElements($('sortable1'),{tag:'li',only:['a','b']}).length); + benchmark(function(){ + Sortable.findElements($('sortable1'),{tag:'li',only:['a','b']}) + },1,'Sortable.findElements/1'); + + assertEqual(1, Sortable.findElements($('sortable1'),{tag:'li',only:['x']}).length); + assertEqual(1, Sortable.findElements($('sortable1'),{tag:'li',only:'a'}).length); + assertEqual(2, Sortable.findElements($('sortable1'),{tag:'li',only:'b'}).length); + assertEqual(4, Sortable.findElements($('sortable1'),{tag:'li',only:['a','b','x']}).length); + }}, + + testSortableSetSequence: function() { with(this) { + // make sure assigning current sequence is a no-op + var o = Sortable.sequence('sortable1'); + Sortable.setSequence('sortable1', ['1','2','3']); + assertEnumEqual(o, Sortable.sequence('sortable1')); + + // check new sequence + Sortable.setSequence('sortable1', ['3','2','1']); + assertEnumEqual(['3','2','1'], Sortable.sequence('sortable1')); + + // non-default format + Sortable.setSequence('sortable_specialformat', ['2','1'], { format:/(\d+)/ }); + assertEnumEqual(['2','1'], Sortable.sequence('sortable_specialformat')); + + // invalid sequence ids should be ignored + Sortable.setSequence('sortable1', ['x', '1', 'y', '2', '3', 'z']); + assertEnumEqual(['1', '2', '3'], Sortable.sequence('sortable1')); + + // elements omitted in new sequence should be cropped + Sortable.setSequence('sortable1', ['1', '3']); + assertEnumEqual(['1', '3'], Sortable.sequence('sortable1')); + }} + + }, "testlog"); +// ]]> +</script> +</body> +</html> diff --git a/dom/tests/mochitest/ajax/scriptaculous/test/unit/string_test.html b/dom/tests/mochitest/ajax/scriptaculous/test/unit/string_test.html new file mode 100644 index 0000000000..995dba496c --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/test/unit/string_test.html @@ -0,0 +1,67 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>script.aculo.us Unit test file</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script src="../../lib/prototype.js" type="text/javascript"></script> + <script src="../../src/scriptaculous.js" type="text/javascript"></script> + <script src="../../src/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../test.css" type="text/css" /> +</head> +<body> +<h1>script.aculo.us Unit test file</h1> +<p> + Tests for String.prototype extensions in effects.js +</p> + +<!-- Log output --> +<div id="testlog"> </div> + +<!-- Tests follow --> +<script type="text/javascript" language="javascript" charset="utf-8"> +// <![CDATA[ + + new Test.Unit.Runner({ + + testStringParseColor: function() { with(this) { + assertEqual('#000000', "#000000".parseColor()); + assertEqual('#000000', "rgb(0,0,0)".parseColor()); + assertEqual('#000000', "rgb(0, 0, 0)".parseColor()); + assertEqual('#000000', "#000".parseColor()); + + assertEqual('#1', "#1".parseColor()); + assertEqual('#12', "#12".parseColor()); + assertEqual('#112233', "#123".parseColor()); + assertEqual('#1234', "#1234".parseColor()); + assertEqual('#12345', "#12345".parseColor()); + assertEqual('#123456', "#123456".parseColor()); + + assertEqual('#abcdef', "#aBcDeF".parseColor()); + assertEqual('#aabbcc', "#aBc".parseColor()); + + assertEqual('white', "white".parseColor()); + assertEqual('#123456', "#123456".parseColor('#000000')); // default to #000000 if not parseable + assertEqual('#000000', "white".parseColor('#000000')); // default to #000000 if not parseable + + assertEqual('#ffffff', "rgb(255,255,255)".parseColor()); + assertEqual('#ff0000', "rgb(255,0,0)".parseColor()); + assertEqual('#00ff00', "rgb(0,255,0)".parseColor()); + assertEqual('#0000ff', "rgb(0,0,255)".parseColor()); + }}, + + testStringParseStyle: function() { with(this) { + var expected = "#<Hash:{'fontSize': '11px'}>"; + + assertInspect(expected, "font-size:11px".parseStyle()); + assertInspect(expected, "font-SIZE: 11px".parseStyle()); + assertInspect(expected, "font-size:11px ".parseStyle()); + assertInspect(expected, " Font-size: 11px ".parseStyle()); + }} + + }); + +// ]]> +</script> +</body> +</html>
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/scriptaculous/test/unit/unit_test.html b/dom/tests/mochitest/ajax/scriptaculous/test/unit/unit_test.html new file mode 100644 index 0000000000..39c078959c --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/test/unit/unit_test.html @@ -0,0 +1,154 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>script.aculo.us Unit test file</title> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <script src="../../lib/prototype.js" type="text/javascript"></script> + <script src="../../src/scriptaculous.js" type="text/javascript"></script> + <script src="../../src/unittest.js" type="text/javascript"></script> + <link rel="stylesheet" href="../test.css" type="text/css" /> + <style type="text/css" media="screen"> + /* <![CDATA[ */ + #testcss1 { font-size:11px; color: #f00; } + #testcss2 { font-size:12px; color: #0f0; display: none; } + /* ]]> */ + </style> +</head> +<body> +<h1>script.aculo.us Unit test file</h1> +<p> + This is a preliminary version mostly for testing the unittest library. +</p> + +<!-- Log output --> +<div id="testlog"> </div> + +<!-- Test elements follow --> +<div id="test_1" class="a bbbbbbbbbbbb cccccccccc dddd"> </div> + +<div id="test_2"> <span> </span> + + + +<div><div></div> </div><span> </span> +</div> + +<ul id="tlist"><li id="tlist_1">x1</li><li id="tlist_2">x2</li></ul> +<ul id="tlist2"><li class="a" id="tlist2_1">x1</li><li id="tlist2_2">x2</li></ul> + +<div id="testmoveby" style="background-color:#333;width:100px;">XXXX</div> + +<div id="testcss1">testcss1<span id="testcss1_span" style="display:none;">blah</span></div><div id="testcss2">testcss1</div> + +<!-- Tests follow --> +<script type="text/javascript" language="javascript" charset="utf-8"> +// <![CDATA[ + + var testObj = { + isNice: function(){ + return true; + }, + isBroken: function(){ + return false; + } + } + + new Test.Unit.Runner({ + + testAssertEqual: function() { with(this) { + assertEqual(0, 0); + assertEqual(0, 0, "test"); + + assertEqual(0,'0'); + assertEqual(65.0, 65); + + assertEqual("a", "a"); + assertEqual("a", "a", "test"); + + assertNotEqual(0, 1); + assertNotEqual("a","b"); + assertNotEqual({},{}); + assertNotEqual([],[]); + assertNotEqual([],{}); + }}, + + testAssertRespondsTo: function() { with(this) { + assertRespondsTo('isNice', testObj); + assertRespondsTo('isBroken', testObj); + }}, + + testAssertIndentical: function() { with(this) { + assertIdentical(0, 0); + assertIdentical(0, 0, "test"); + assertIdentical(1, 1); + assertIdentical('a', 'a'); + assertIdentical('a', 'a', "test"); + assertIdentical('', ''); + assertIdentical(undefined, undefined); + assertIdentical(null, null); + assertIdentical(true, true); + assertIdentical(false, false); + + var obj = {a:'b'}; + assertIdentical(obj, obj); + + assertNotIdentical({1:2,3:4},{1:2,3:4}); + + assertIdentical(1, 1.0); // both are typeof == 'number' + + assertNotIdentical(1, '1'); + assertNotIdentical(1, '1.0'); + }}, + + testAssertMatch: function() { with(this) { + assertMatch(/knowmad.jpg$/, 'http://script.aculo.us/images/knowmad.jpg'); + assertMatch(/Fuc/, 'Thomas Fuchs'); + assertMatch(/^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$/, '$19.95'); + assertMatch(/(\d{3}\) ?)|(\d{3}[- \.])?\d{3}[- \.]\d{4}(\s(x\d+)?){0,1}$/, '704-343-9330'); + assertMatch(/^(?:(?:(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(\/|-|\.)(?:0?2\1(?:29)))|(?:(?:(?:1[6-9]|[2-9]\d)?\d{2})(\/|-|\.)(?:(?:(?:0?[13578]|1[02])\2(?:31))|(?:(?:0?[1,3-9]|1[0-2])\2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))\2(?:0?[1-9]|1\d|2[0-8]))))$/, '2001-06-16'); + assertMatch(/^((0?[123456789])|(1[012]))\s*:\s*([012345]\d)(\s*:\s*([012345]\d))?\s*[ap]m\s*-\s*((0?[123456789])|(1[012]))\s*:\s*([012345]\d)(\s*:\s*([012345]\d))?\s*[ap]m$/i, '2:00PM-2:15PM'); + + }}, + + testAssertInstanceOf: function() { with(this) { + assertInstanceOf(Effect.Opacity, new Effect.Opacity('testcss1',{sync:true})); + assertNotInstanceOf(String, new Effect.Opacity('testcss1',{sync:true})); + + // note: fails with firefox 1.0.X (bug, fixed in Deer Park) + assertNotInstanceOf(Effect.Parallel, new Effect.Opacity('testcss1',{sync:true}), "(note: fails with firefox 1.0.X, fixed in Deer Park)"); + }}, + + testAssertReturns: function() { with(this) { + + assertReturnsTrue('isNice',testObj); + assertReturnsFalse('isBroken',testObj); + + assertReturnsTrue('nice',testObj); + assertReturnsFalse('broken',testObj); + + }}, + + testAssertVisible: function() { with(this) { + assertVisible('testcss1'); + assertNotVisible('testcss1_span'); + assertNotVisible('testcss2', "Due to a Safari bug, this test fails in Safari."); + + Element.hide('testcss1'); + assertNotVisible('testcss1'); + assertNotVisible('testcss1_span'); + Element.show('testcss1'); + assertVisible('testcss1'); + assertNotVisible('testcss1_span'); + + Element.show('testcss1_span'); + assertVisible('testcss1_span'); + Element.hide('testcss1'); + assertNotVisible('testcss1_span'); // hidden by parent + }} + + }, "testlog"); +// ]]> +</script> +</body> +</html>
\ No newline at end of file diff --git a/dom/tests/mochitest/ajax/scriptaculous/test_Scriptaculous.html b/dom/tests/mochitest/ajax/scriptaculous/test_Scriptaculous.html new file mode 100644 index 0000000000..f0bf2cbe65 --- /dev/null +++ b/dom/tests/mochitest/ajax/scriptaculous/test_Scriptaculous.html @@ -0,0 +1,15 @@ +<!DOCTYPE HTML> +<html> +<head> + <title>Test for Scriptaculous</title> + <script src="/MochiKit/Base.js"></script> + <script src="/MochiKit/Async.js"></script> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="../lib/AJAX_setup.js"></script> + <link rel="stylesheet" type="text/css" href="../lib/test.css" /> +</head> +<body> + <iframe width="100%" height="500" id="testframe" src=""></iframe> +</body> +</html> + |