1 /*
  2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
  3 For licensing, see LICENSE.html or http://ckeditor.com/license
  4 */
  5
  6 /**
  7  * @fileOverview Defines the {@link CKEDITOR.test} object, which contains
  8  *		functions used at our testing environment.
  9  */
 10
 11 /*jsl:import ../tests/yuitest.js*/
 12
 13 /**
 14  * Contains functions used at our testing environment. Currently,
 15  * our testing system is based on the
 16  * <a href="http://developer.yahoo.com/yui/yuitest/">YUI Test</a>.
 17  * @namespace
 18  * @example
 19  */
 20 CKEDITOR.test =
 21 {
 22 	/**
 23 	 * The assertion namespace, containing all assertion functions. Currently,
 24 	 * this is an alias for
 25 	 * <a href="http://developer.yahoo.com/yui/docs/YAHOO.util.Assert.html">YAHOO.util.Assert</a>.
 26 	 * @example
 27 	 * <b>CKEDITOR.test.assert</b>.areEqual( '10', 10 );        // "true"
 28 	 * <b>CKEDITOR.test.assert</b>.areSame( '10', 10 );         // "false"
 29 	 * <b>CKEDITOR.test.assert</b>.isUndefined( window.test );  // "true"
 30 	 */
 31 	assert : YAHOO.util.Assert,
 32
 33 	/**
 34 	 * Adds a test case to the test runner.
 35 	 * @param {Object} testCase The test case object. See other tests for
 36 	 *		examples.
 37 	 * @example
 38 	 * <b>CKEDITOR.test.addTestCase</b>((function()
 39 	 * {
 40 	 *     // Local reference to the "assert" object.
 41 	 *     var assert = CKEDITOR.test.assert;
 42 	 *
 43 	 *     return {
 44 	 *         test_example : function()
 45 	 *         {
 46 	 *             assert.areSame( '10', 10 );  // FAIL
 47 	 *         }
 48 	 *      };
 49 	 * })());
 50 	 */
 51 	addTestCase : function( testCase )
 52 	{
 53 		YAHOO.tool.TestRunner.add( new YAHOO.tool.TestCase( testCase ) );
 54 	},
 55
 56 	/**
 57 	 * Gets the inner HTML of an element, for testing purposes.
 58 	 */
 59 	getInnerHtml : function( elementOrId )
 60 	{
 61 		var html = ( elementOrId.nodeType ? elementOrId : document.getElementById( elementOrId ) ).innerHTML;
 62 		html = html.toLowerCase();
 63 		html = html.replace( /[\n\r]/g, '' );
 64
 65 		html = html.replace( /<\w[^>]*/g, function( match )
 66 			{
 67 				var attribs = [];
 68 				var hasClass;
 69
 70 				match = match.replace( /\s([^\s=]+)=((?:"[^"]*")|(?:'[^']*')|(?:[^\s]+))/g, function( match, attName, attValue )
 71 					{
 72 						if ( attName == 'style' )
 73 						{
 74 							// IE doesn't add the final ";"
 75 							attValue = attValue.replace( /([^"';\s])\s*(["']?)$/, '$1;$2' );
 76
 77 							// Safari adds some extra space to the end.
 78 							attValue = attValue.replace( /\s+(["']?)$/, '$1' );
 79 						}
 80
 81 						// IE may have 'class' more than once.
 82 						if ( attName == 'class' )
 83 						{
 84 							if ( hasClass )
 85 								return '';
 86
 87 							hasClass = true;
 88 						}
 89
 90 						if ( attName != '_cke_expando' )
 91 							attribs.push( [ attName, attValue ] );
 92
 93 						return '';
 94 					} );
 95
 96 				attribs.sort( function( a, b )
 97 					{
 98 						var nameA = a[ 0 ];
 99 						var nameB = b[ 0 ];
100 						return nameA < nameB ? -1 : nameA > nameB ? 1 : 0;
101 					} );
102
103 				var ret = match.replace( /\s{2,}/g, ' ' );
104
105 				for ( var i = 0 ; i < attribs.length ; i++ )
106 				{
107 					ret += ' ' + attribs[i][0] + '=';
108 					ret += (/^["']/).test( attribs[i][1] ) ? attribs[i][1] : '"' + attribs[i][1] + '"';
109 				}
110
111 				return ret;
112 			} );
113
114 		return html;
115 	}
116 };
117