2 Distributed under both the W3C Test Suite License [1] and the W3C
3 3-clause BSD License [2]. To contribute to a W3C Test Suite, see the
4 policies and contribution forms [3].
6 [1] http://www.w3.org/Consortium/Legal/2008/04-testsuite-license
7 [2] http://www.w3.org/Consortium/Legal/2008/03-bsd-license
8 [3] http://www.w3.org/2004/10/27-testcases
14 * This file provides a framework for writing testcases. It is intended to
15 * provide a convenient API for making common assertions, and to work both
16 * for testing synchronous and asynchronous DOM features in a way that
17 * promotes clear, robust, tests.
21 * To use this file, import the script and the testharnessreport script into
23 * <script src="/resources/testharness.js"></script>
24 * <script src="/resources/testharnessreport.js"></script>
26 * Within each file one may define one or more tests. Each test is atomic
27 * in the sense that a single test has a single result (pass/fail/timeout).
28 * Within each test one may have a number of asserts. The test fails at the
29 * first failing assert, and the remainder of the test is (typically) not run.
31 * If the file containing the tests is a HTML file with an element of id "log"
32 * this will be populated with a table containing the test results after all
35 * NOTE: By default tests must be created before the load event fires. For ways
36 * to create tests after the load event, see "Determining when all tests
37 * are complete", below
39 * == Synchronous Tests ==
41 * To create a synchronous test use the test() function:
43 * test(test_function, name, properties)
45 * test_function is a function that contains the code to test. For example a
46 * trivial passing test would be:
48 * test(function() {assert_true(true)}, "assert_true with true")
50 * The function passed in is run in the test() call.
52 * properties is an object that overrides default test properties. The
53 * recognised properties are:
54 * timeout - the test timeout in ms
57 * test(test_function, "Sample test", {timeout:1000})
59 * would run test_function with a timeout of 1s.
61 * Additionally, test-specific metadata can be passed in the properties. These
62 * are used when the individual test has different metadata from that stored
64 * The recognized metadata properties are:
66 * help - The url of the part of the specification being tested
68 * assert - A human readable description of what the test is attempting
71 * author - Name and contact information for the author of the test in the
72 * format: "Name <email_addr>" or "Name http://contact/url"
74 * == Asynchronous Tests ==
76 * Testing asynchronous features is somewhat more complex since the result of
77 * a test may depend on one or more events or other callbacks. The API provided
78 * for testing these features is indended to be rather low-level but hopefully
79 * applicable to many situations.
81 * To create a test, one starts by getting a Test object using async_test:
83 * async_test(name, properties)
86 * var t = async_test("Simple async test")
88 * Assertions can be added to the test by calling the step method of the test
89 * object with a function containing the test assertions:
91 * t.step(function() {assert_true(true)});
93 * When all the steps are complete, the done() method must be called:
97 * As a convenience, async_test can also takes a function as first argument.
98 * This function is called with the test object as both its `this` object and
99 * first argument. The above example can be rewritten as:
101 * async_test(function(t) {
102 * object.some_event = function() {
103 * t.step(function (){assert_true(true); t.done();});
105 * }, "Simple async test");
107 * which avoids cluttering the global scope with references to async
110 * The properties argument is identical to that for test().
112 * In many cases it is convenient to run a step in response to an event or a
113 * callback. A convenient method of doing this is through the step_func method
114 * which returns a function that, when called runs a test step. For example
116 * object.some_event = t.step_func(function(e) {assert_true(e.a)});
118 * == Making assertions ==
120 * Functions for making assertions start assert_
121 * The best way to get a list is to look in this file for functions names
122 * matching that pattern. The general signature is
124 * assert_something(actual, expected, description)
126 * although not all assertions precisely match this pattern e.g. assert_true
127 * only takes actual and description as arguments.
129 * The description parameter is used to present more useful error messages when
132 * NOTE: All asserts must be located in a test() or a step of an async_test().
133 * asserts outside these places won't be detected correctly by the harness
134 * and may cause a file to stop testing.
136 * == Harness Timeout ==
138 * The overall harness admits two timeout values "normal" (the
139 * default) and "long", used for tests which have an unusually long
140 * runtime. After the timeout is reached, the harness will stop
141 * waiting for further async tests to complete. By default the
142 * timeouts are set to 10s and 60s, respectively, but may be changed
143 * when the test is run on hardware with different performance
144 * characteristics to a common desktop computer. In order to opt-in
145 * to the longer test timeout, the test must specify a meta element:
146 * <meta name="timeout" content="long">
150 * Sometimes tests require non-trivial setup that may fail. For this purpose
151 * there is a setup() function, that may be called with one or two arguments.
152 * The two argument version is:
154 * setup(func, properties)
156 * The one argument versions may omit either argument.
157 * func is a function to be run synchronously. setup() becomes a no-op once
158 * any tests have returned results. Properties are global properties of the test
159 * harness. Currently recognised properties are:
162 * explicit_done - Wait for an explicit call to done() before declaring all
163 * tests complete (see below)
165 * output_document - The document to which results should be logged. By default
166 * this is the current document but could be an ancestor
167 * document in some cases e.g. a SVG test loaded in an HTML
170 * explicit_timeout - disable file timeout; only stop waiting for results
171 * when the timeout() function is called (typically for
172 * use when integrating with some existing test framework
173 * that has its own timeout mechanism).
175 * allow_uncaught_exception - don't treat an uncaught exception as an error;
176 * needed when e.g. testing the window.onerror
179 * timeout_multiplier - Multiplier to apply to per-test timeouts.
181 * == Determining when all tests are complete ==
183 * By default the test harness will assume there are no more results to come
185 * 1) There are no Test objects that have been created but not completed
186 * 2) The load event on the document has fired
188 * This behaviour can be overridden by setting the explicit_done property to
189 * true in a call to setup(). If explicit_done is true, the test harness will
190 * not assume it is done until the global done() function is called. Once done()
191 * is called, the two conditions above apply like normal.
193 * == Generating tests ==
195 * NOTE: this functionality may be removed
197 * There are scenarios in which is is desirable to create a large number of
198 * (synchronous) tests that are internally similar but vary in the parameters
199 * used. To make this easier, the generate_tests function allows a single
200 * function to be called with each set of parameters in a list:
202 * generate_tests(test_function, parameter_lists, properties)
206 * generate_tests(assert_equals, [
207 * ["Sum one and one", 1+1, 2],
208 * ["Sum one and zero", 1+0, 1]
213 * test(function() {assert_equals(1+1, 2)}, "Sum one and one")
214 * test(function() {assert_equals(1+0, 1)}, "Sum one and zero")
216 * Note that the first item in each parameter list corresponds to the name of
219 * The properties argument is identical to that for test(). This may be a
220 * single object (used for all generated tests) or an array.
224 * The framework provides callbacks corresponding to 3 events:
226 * start - happens when the first Test is created
227 * result - happens when a test result is recieved
228 * complete - happens when all results are recieved
230 * The page defining the tests may add callbacks for these events by calling
231 * the following methods:
233 * add_start_callback(callback) - callback called with no arguments
234 * add_result_callback(callback) - callback called with a test argument
235 * add_completion_callback(callback) - callback called with an array of tests
236 * and an status object
238 * tests have the following properties:
239 * status: A status code. This can be compared to the PASS, FAIL, TIMEOUT and
240 * NOTRUN properties on the test object
241 * message: A message indicating the reason for failure. In the future this
242 * will always be a string
244 * The status object gives the overall status of the harness. It has the
245 * following properties:
246 * status: Can be compared to the OK, ERROR and TIMEOUT properties
247 * message: An error message set when the status is ERROR
251 * In order to collect the results of multiple pages containing tests, the test
252 * harness will, when loaded in a nested browsing context, attempt to call
253 * certain functions in each ancestor and opener browsing context:
255 * start - start_callback
256 * result - result_callback
257 * complete - completion_callback
259 * These are given the same arguments as the corresponding internal callbacks
262 * == External API through cross-document messaging ==
264 * Where supported, the test harness will also send messages using
265 * cross-document messaging to each ancestor and opener browsing context. Since
266 * it uses the wildcard keyword (*), cross-origin communication is enabled and
267 * script on different origins can collect the results.
269 * This API follows similar conventions as those described above only slightly
270 * modified to accommodate message event API. Each message is sent by the harness
271 * is passed a single vanilla object, available as the `data` property of the
272 * event object. These objects are structures as follows:
274 * start - { type: "start" }
275 * result - { type: "result", test: Test }
276 * complete - { type: "complete", tests: [Test, ...], status: TestsStatus }
278 * == List of assertions ==
280 * assert_true(actual, description)
281 * asserts that /actual/ is strictly true
283 * assert_false(actual, description)
284 * asserts that /actual/ is strictly false
286 * assert_equals(actual, expected, description)
287 * asserts that /actual/ is the same value as /expected/
289 * assert_not_equals(actual, expected, description)
290 * asserts that /actual/ is a different value to /expected/. Yes, this means
291 * that "expected" is a misnomer
293 * assert_in_array(actual, expected, description)
294 * asserts that /expected/ is an Array, and /actual/ is equal to one of the
295 * members -- expected.indexOf(actual) != -1
297 * assert_array_equals(actual, expected, description)
298 * asserts that /actual/ and /expected/ have the same length and the value of
299 * each indexed property in /actual/ is the strictly equal to the corresponding
300 * property value in /expected/
302 * assert_approx_equals(actual, expected, epsilon, description)
303 * asserts that /actual/ is a number within +/- /epsilon/ of /expected/
305 * assert_less_than(actual, expected, description)
306 * asserts that /actual/ is a number less than /expected/
308 * assert_greater_than(actual, expected, description)
309 * asserts that /actual/ is a number greater than /expected/
311 * assert_less_than_equal(actual, expected, description)
312 * asserts that /actual/ is a number less than or equal to /expected/
314 * assert_greater_than_equal(actual, expected, description)
315 * asserts that /actual/ is a number greater than or equal to /expected/
317 * assert_regexp_match(actual, expected, description)
318 * asserts that /actual/ matches the regexp /expected/
320 * assert_class_string(object, class_name, description)
321 * asserts that the class string of /object/ as returned in
322 * Object.prototype.toString is equal to /class_name/.
324 * assert_own_property(object, property_name, description)
325 * assert that object has own property property_name
327 * assert_inherits(object, property_name, description)
328 * assert that object does not have an own property named property_name
329 * but that property_name is present in the prototype chain for object
331 * assert_idl_attribute(object, attribute_name, description)
332 * assert that an object that is an instance of some interface has the
333 * attribute attribute_name following the conditions specified by WebIDL
335 * assert_readonly(object, property_name, description)
336 * assert that property property_name on object is readonly
338 * assert_throws(code, func, description)
339 * code - the expected exception:
340 * o string: the thrown exception must be a DOMException with the given
341 * name, e.g., "TimeoutError" (for compatibility with existing
342 * tests, a constant is also supported, e.g., "TIMEOUT_ERR")
343 * o object: the thrown exception must have a property called "name" that
345 * o null: allow any exception (in general, one of the options above
347 * func - a function that should throw
349 * assert_unreached(description)
350 * asserts if called. Used to ensure that some codepath is *not* taken e.g.
351 * an event does not fire.
353 * assert_any(assert_func, actual, expected_array, extra_arg_1, ... extra_arg_N)
354 * asserts that one assert_func(actual, expected_array_N, extra_arg1, ..., extra_arg_N)
355 * is true for some expected_array_N in expected_array. This only works for assert_func
356 * with signature assert_func(actual, expected, args_1, ..., args_N). Note that tests
357 * with multiple allowed pass conditions are bad practice unless the spec specifically
358 * allows multiple behaviours. Test authors should not use this method simply to hide
361 * assert_exists(object, property_name, description)
363 * asserts that object has an own property property_name
365 * assert_not_exists(object, property_name, description)
367 * assert that object does not have own property property_name
373 // default timeout is 10 seconds, test can override if needed
376 harness_timeout:{"normal":10000,
381 var xhtml_ns = "http://www.w3.org/1999/xhtml";
383 // script_prefix is used by Output.prototype.show_results() to figure out
384 // where to get testharness.css from. It's enclosed in an extra closure to
385 // not pollute the library's namespace with variables like "src".
386 var script_prefix = null;
389 var scripts = document.getElementsByTagName("script");
390 for (var i = 0; i < scripts.length; i++)
394 var src = scripts[i].src;
396 else if (scripts[i].href)
399 var src = scripts[i].href.baseVal;
401 if (src && src.slice(src.length - "testharness.js".length) === "testharness.js")
403 script_prefix = src.slice(0, src.length - "testharness.js".length);
413 var name_counter = 0;
414 function next_default_name()
416 //Don't use document.title to work around an Opera bug in XHTML documents
417 var title = document.getElementsByTagName("title")[0];
418 var prefix = (title && title.firstChild && title.firstChild.data) || "Untitled";
419 var suffix = name_counter > 0 ? " " + name_counter : "";
421 return prefix + suffix;
424 function test(func, name, properties)
426 var test_name = name ? name : next_default_name();
427 properties = properties ? properties : {};
428 var test_obj = new Test(test_name, properties);
430 if (test_obj.phase === test_obj.phases.STARTED) {
435 function async_test(func, name, properties)
437 if (typeof func !== "function") {
442 var test_name = name ? name : next_default_name();
443 properties = properties ? properties : {};
444 var test_obj = new Test(test_name, properties);
446 test_obj.step(func, test_obj, test_obj);
451 function setup(func_or_properties, maybe_properties)
455 if (arguments.length === 2) {
456 func = func_or_properties;
457 properties = maybe_properties;
458 } else if (func_or_properties instanceof Function){
459 func = func_or_properties;
461 properties = func_or_properties;
463 tests.setup(func, properties);
464 output.setup(properties);
471 function generate_tests(func, args, properties) {
472 forEach(args, function(x, i)
477 func.apply(this, x.slice(1));
480 Array.isArray(properties) ? properties[i] : properties);
484 function on_event(object, event, callback)
486 object.addEventListener(event, callback, false);
489 expose(test, 'test');
490 expose(async_test, 'async_test');
491 expose(generate_tests, 'generate_tests');
492 expose(setup, 'setup');
493 expose(done, 'done');
494 expose(on_event, 'on_event');
497 * Return a string truncated to the given length, with ... added at the end
500 function truncate(s, len)
502 if (s.length > len) {
503 return s.substring(0, len - 3) + "...";
509 * Return true if object is probably a Node object.
511 function is_node(object)
513 // I use duck-typing instead of instanceof, because
514 // instanceof doesn't work if the node is from another window (like an
515 // iframe's contentWindow):
516 // http://www.w3.org/Bugs/Public/show_bug.cgi?id=12295
517 if ("nodeType" in object
518 && "nodeName" in object
519 && "nodeValue" in object
520 && "childNodes" in object)
528 // The object is probably Node.prototype or another prototype
529 // object that inherits from it, and not a Node instance.
538 * Convert a value to a nice, human-readable string
540 function format_value(val, seen)
545 if (typeof val === "object" && val !== null)
547 if (seen.indexOf(val) >= 0)
553 if (Array.isArray(val))
555 return "[" + val.map(function(x) {return format_value(x, seen)}).join(", ") + "]";
561 val = val.replace("\\", "\\\\");
562 for (var i = 0; i < 32; i++)
566 case 0: replace += "0"; break;
567 case 1: replace += "x01"; break;
568 case 2: replace += "x02"; break;
569 case 3: replace += "x03"; break;
570 case 4: replace += "x04"; break;
571 case 5: replace += "x05"; break;
572 case 6: replace += "x06"; break;
573 case 7: replace += "x07"; break;
574 case 8: replace += "b"; break;
575 case 9: replace += "t"; break;
576 case 10: replace += "n"; break;
577 case 11: replace += "v"; break;
578 case 12: replace += "f"; break;
579 case 13: replace += "r"; break;
580 case 14: replace += "x0e"; break;
581 case 15: replace += "x0f"; break;
582 case 16: replace += "x10"; break;
583 case 17: replace += "x11"; break;
584 case 18: replace += "x12"; break;
585 case 19: replace += "x13"; break;
586 case 20: replace += "x14"; break;
587 case 21: replace += "x15"; break;
588 case 22: replace += "x16"; break;
589 case 23: replace += "x17"; break;
590 case 24: replace += "x18"; break;
591 case 25: replace += "x19"; break;
592 case 26: replace += "x1a"; break;
593 case 27: replace += "x1b"; break;
594 case 28: replace += "x1c"; break;
595 case 29: replace += "x1d"; break;
596 case 30: replace += "x1e"; break;
597 case 31: replace += "x1f"; break;
599 val = val.replace(RegExp(String.fromCharCode(i), "g"), replace);
601 return '"' + val.replace(/"/g, '\\"') + '"';
606 // In JavaScript, -0 === 0 and String(-0) == "0", so we have to
608 if (val === -0 && 1/val === -Infinity)
619 // Special-case Node objects, since those come up a lot in my tests. I
620 // ignore namespaces.
623 switch (val.nodeType)
625 case Node.ELEMENT_NODE:
626 var ret = "<" + val.tagName.toLowerCase();
627 for (var i = 0; i < val.attributes.length; i++)
629 ret += " " + val.attributes[i].name + '="' + val.attributes[i].value + '"';
631 ret += ">" + val.innerHTML + "</" + val.tagName.toLowerCase() + ">";
632 return "Element node " + truncate(ret, 60);
634 return 'Text node "' + truncate(val.data, 60) + '"';
635 case Node.PROCESSING_INSTRUCTION_NODE:
636 return "ProcessingInstruction node with target " + format_value(truncate(val.target, 60)) + " and data " + format_value(truncate(val.data, 60));
637 case Node.COMMENT_NODE:
638 return "Comment node <!--" + truncate(val.data, 60) + "-->";
639 case Node.DOCUMENT_NODE:
640 return "Document node with " + val.childNodes.length + (val.childNodes.length == 1 ? " child" : " children");
641 case Node.DOCUMENT_TYPE_NODE:
642 return "DocumentType node";
643 case Node.DOCUMENT_FRAGMENT_NODE:
644 return "DocumentFragment node with " + val.childNodes.length + (val.childNodes.length == 1 ? " child" : " children");
646 return "Node object of unknown type";
650 // Fall through to default
652 return typeof val + ' "' + truncate(String(val), 60) + '"';
655 expose(format_value, "format_value");
661 function assert_true(actual, description)
663 assert(actual === true, "assert_true", description,
664 "expected true got ${actual}", {actual:actual});
666 expose(assert_true, "assert_true");
668 function assert_false(actual, description)
670 assert(actual === false, "assert_false", description,
671 "expected false got ${actual}", {actual:actual});
673 expose(assert_false, "assert_false");
675 function same_value(x, y) {
681 else if (x === 0 && y === 0) {
682 //Distinguish +0 and -0
692 function assert_equals(actual, expected, description)
695 * Test if two primitives are equal or two objects
696 * are the same object
698 if (typeof actual != typeof expected)
700 assert(false, "assert_equals", description,
701 "expected (" + typeof expected + ") ${expected} but got (" + typeof actual + ") ${actual}",
702 {expected:expected, actual:actual});
705 assert(same_value(actual, expected), "assert_equals", description,
706 "expected ${expected} but got ${actual}",
707 {expected:expected, actual:actual});
709 expose(assert_equals, "assert_equals");
711 function assert_not_equals(actual, expected, description)
714 * Test if two primitives are unequal or two objects
715 * are different objects
717 assert(!same_value(actual, expected), "assert_not_equals", description,
718 "got disallowed value ${actual}",
721 expose(assert_not_equals, "assert_not_equals");
723 function assert_in_array(actual, expected, description)
725 assert(expected.indexOf(actual) != -1, "assert_in_array", description,
726 "value ${actual} not in array ${expected}",
727 {actual:actual, expected:expected});
729 expose(assert_in_array, "assert_in_array");
731 function assert_object_equals(actual, expected, description)
733 //This needs to be improved a great deal
734 function check_equal(actual, expected, stack)
741 assert(expected.hasOwnProperty(p), "assert_object_equals", description,
742 "unexpected property ${p}", {p:p});
744 if (typeof actual[p] === "object" && actual[p] !== null)
746 if (stack.indexOf(actual[p]) === -1)
748 check_equal(actual[p], expected[p], stack);
753 assert(same_value(actual[p], expected[p]), "assert_object_equals", description,
754 "property ${p} expected ${expected} got ${actual}",
755 {p:p, expected:expected, actual:actual});
760 assert(actual.hasOwnProperty(p),
761 "assert_object_equals", description,
762 "expected property ${p} missing", {p:p});
766 check_equal(actual, expected, []);
768 expose(assert_object_equals, "assert_object_equals");
770 function assert_array_equals(actual, expected, description)
772 assert(actual.length === expected.length,
773 "assert_array_equals", description,
774 "lengths differ, expected ${expected} got ${actual}",
775 {expected:expected.length, actual:actual.length});
777 for (var i=0; i < actual.length; i++)
779 assert(actual.hasOwnProperty(i) === expected.hasOwnProperty(i),
780 "assert_array_equals", description,
781 "property ${i}, property expected to be $expected but was $actual",
782 {i:i, expected:expected.hasOwnProperty(i) ? "present" : "missing",
783 actual:actual.hasOwnProperty(i) ? "present" : "missing"});
784 assert(same_value(expected[i], actual[i]),
785 "assert_array_equals", description,
786 "property ${i}, expected ${expected} but got ${actual}",
787 {i:i, expected:expected[i], actual:actual[i]});
790 expose(assert_array_equals, "assert_array_equals");
792 function assert_approx_equals(actual, expected, epsilon, description)
795 * Test if two primitive numbers are equal withing +/- epsilon
797 assert(typeof actual === "number",
798 "assert_approx_equals", description,
799 "expected a number but got a ${type_actual}",
800 {type_actual:typeof actual});
802 assert(Math.abs(actual - expected) <= epsilon,
803 "assert_approx_equals", description,
804 "expected ${expected} +/- ${epsilon} but got ${actual}",
805 {expected:expected, actual:actual, epsilon:epsilon});
807 expose(assert_approx_equals, "assert_approx_equals");
809 function assert_less_than(actual, expected, description)
812 * Test if a primitive number is less than another
814 assert(typeof actual === "number",
815 "assert_less_than", description,
816 "expected a number but got a ${type_actual}",
817 {type_actual:typeof actual});
819 assert(actual < expected,
820 "assert_less_than", description,
821 "expected a number less than ${expected} but got ${actual}",
822 {expected:expected, actual:actual});
824 expose(assert_less_than, "assert_less_than");
826 function assert_greater_than(actual, expected, description)
829 * Test if a primitive number is greater than another
831 assert(typeof actual === "number",
832 "assert_greater_than", description,
833 "expected a number but got a ${type_actual}",
834 {type_actual:typeof actual});
836 assert(actual > expected,
837 "assert_greater_than", description,
838 "expected a number greater than ${expected} but got ${actual}",
839 {expected:expected, actual:actual});
841 expose(assert_greater_than, "assert_greater_than");
843 function assert_less_than_equal(actual, expected, description)
846 * Test if a primitive number is less than or equal to another
848 assert(typeof actual === "number",
849 "assert_less_than_equal", description,
850 "expected a number but got a ${type_actual}",
851 {type_actual:typeof actual});
853 assert(actual <= expected,
854 "assert_less_than", description,
855 "expected a number less than or equal to ${expected} but got ${actual}",
856 {expected:expected, actual:actual});
858 expose(assert_less_than_equal, "assert_less_than_equal");
860 function assert_greater_than_equal(actual, expected, description)
863 * Test if a primitive number is greater than or equal to another
865 assert(typeof actual === "number",
866 "assert_greater_than_equal", description,
867 "expected a number but got a ${type_actual}",
868 {type_actual:typeof actual});
870 assert(actual >= expected,
871 "assert_greater_than_equal", description,
872 "expected a number greater than or equal to ${expected} but got ${actual}",
873 {expected:expected, actual:actual});
875 expose(assert_greater_than_equal, "assert_greater_than_equal");
877 function assert_regexp_match(actual, expected, description) {
879 * Test if a string (actual) matches a regexp (expected)
881 assert(expected.test(actual),
882 "assert_regexp_match", description,
883 "expected ${expected} but got ${actual}",
884 {expected:expected, actual:actual});
886 expose(assert_regexp_match, "assert_regexp_match");
888 function assert_class_string(object, class_string, description) {
889 assert_equals({}.toString.call(object), "[object " + class_string + "]",
892 expose(assert_class_string, "assert_class_string");
895 function _assert_own_property(name) {
896 return function(object, property_name, description)
898 assert(object.hasOwnProperty(property_name),
900 "expected property ${p} missing", {p:property_name});
903 expose(_assert_own_property("assert_exists"), "assert_exists");
904 expose(_assert_own_property("assert_own_property"), "assert_own_property");
906 function assert_not_exists(object, property_name, description)
908 assert(!object.hasOwnProperty(property_name),
909 "assert_not_exists", description,
910 "unexpected property ${p} found", {p:property_name});
912 expose(assert_not_exists, "assert_not_exists");
914 function _assert_inherits(name) {
915 return function (object, property_name, description)
917 assert(typeof object === "object",
919 "provided value is not an object");
921 assert("hasOwnProperty" in object,
923 "provided value is an object but has no hasOwnProperty method");
925 assert(!object.hasOwnProperty(property_name),
927 "property ${p} found on object expected in prototype chain",
930 assert(property_name in object,
932 "property ${p} not found in prototype chain",
936 expose(_assert_inherits("assert_inherits"), "assert_inherits");
937 expose(_assert_inherits("assert_idl_attribute"), "assert_idl_attribute");
939 function assert_readonly(object, property_name, description)
941 var initial_value = object[property_name];
943 //Note that this can have side effects in the case where
944 //the property has PutForwards
945 object[property_name] = initial_value + "a"; //XXX use some other value here?
946 assert(same_value(object[property_name], initial_value),
947 "assert_readonly", description,
948 "changing property ${p} succeeded",
953 object[property_name] = initial_value;
956 expose(assert_readonly, "assert_readonly");
958 function assert_throws(code, func, description)
963 assert(false, "assert_throws", description,
964 "${func} did not throw", {func:func});
968 if (e instanceof AssertionError) {
975 if (typeof code === "object")
977 assert(typeof e == "object" && "name" in e && e.name == code.name,
978 "assert_throws", description,
979 "${func} threw ${actual} (${actual_name}) expected ${expected} (${expected_name})",
980 {func:func, actual:e, actual_name:e.name,
982 expected_name:code.name});
986 var code_name_map = {
987 INDEX_SIZE_ERR: 'IndexSizeError',
988 HIERARCHY_REQUEST_ERR: 'HierarchyRequestError',
989 WRONG_DOCUMENT_ERR: 'WrongDocumentError',
990 INVALID_CHARACTER_ERR: 'InvalidCharacterError',
991 NO_MODIFICATION_ALLOWED_ERR: 'NoModificationAllowedError',
992 NOT_FOUND_ERR: 'NotFoundError',
993 NOT_SUPPORTED_ERR: 'NotSupportedError',
994 INVALID_STATE_ERR: 'InvalidStateError',
995 SYNTAX_ERR: 'SyntaxError',
996 INVALID_MODIFICATION_ERR: 'InvalidModificationError',
997 NAMESPACE_ERR: 'NamespaceError',
998 INVALID_ACCESS_ERR: 'InvalidAccessError',
999 TYPE_MISMATCH_ERR: 'TypeMismatchError',
1000 SECURITY_ERR: 'SecurityError',
1001 NETWORK_ERR: 'NetworkError',
1002 ABORT_ERR: 'AbortError',
1003 URL_MISMATCH_ERR: 'URLMismatchError',
1004 QUOTA_EXCEEDED_ERR: 'QuotaExceededError',
1005 TIMEOUT_ERR: 'TimeoutError',
1006 INVALID_NODE_TYPE_ERR: 'InvalidNodeTypeError',
1007 DATA_CLONE_ERR: 'DataCloneError'
1010 var name = code in code_name_map ? code_name_map[code] : code;
1012 var name_code_map = {
1014 HierarchyRequestError: 3,
1015 WrongDocumentError: 4,
1016 InvalidCharacterError: 5,
1017 NoModificationAllowedError: 7,
1019 NotSupportedError: 9,
1020 InvalidStateError: 11,
1022 InvalidModificationError: 13,
1024 InvalidAccessError: 15,
1025 TypeMismatchError: 17,
1029 URLMismatchError: 21,
1030 QuotaExceededError: 22,
1032 InvalidNodeTypeError: 24,
1038 TransactionInactiveError: 0,
1043 if (!(name in name_code_map))
1045 throw new AssertionError('Test bug: unrecognized DOMException code "' + code + '" passed to assert_throws()');
1048 var required_props = { code: name_code_map[name] };
1050 if (required_props.code === 0
1051 || ("name" in e && e.name !== e.name.toUpperCase() && e.name !== "DOMException"))
1053 // New style exception: also test the name property.
1054 required_props.name = name;
1057 //We'd like to test that e instanceof the appropriate interface,
1058 //but we can't, because we don't know what window it was created
1059 //in. It might be an instanceof the appropriate interface on some
1060 //unknown other window. TODO: Work around this somehow?
1062 assert(typeof e == "object",
1063 "assert_throws", description,
1064 "${func} threw ${e} with type ${type}, not an object",
1065 {func:func, e:e, type:typeof e});
1067 for (var prop in required_props)
1069 assert(typeof e == "object" && prop in e && e[prop] == required_props[prop],
1070 "assert_throws", description,
1071 "${func} threw ${e} that is not a DOMException " + code + ": property ${prop} is equal to ${actual}, expected ${expected}",
1072 {func:func, e:e, prop:prop, actual:e[prop], expected:required_props[prop]});
1076 expose(assert_throws, "assert_throws");
1078 function assert_unreached(description) {
1079 assert(false, "assert_unreached", description,
1080 "Reached unreachable code");
1082 expose(assert_unreached, "assert_unreached");
1084 function assert_any(assert_func, actual, expected_array)
1086 var args = [].slice.call(arguments, 3)
1089 forEach(expected_array,
1093 assert_func.apply(this, [actual, expected].concat(args))
1096 errors.push(e.message);
1100 throw new AssertionError(errors.join("\n\n"));
1103 expose(assert_any, "assert_any");
1105 function Test(name, properties)
1115 this.phase = this.phases.INITIAL;
1117 this.status = this.NOTRUN;
1118 this.timeout_id = null;
1120 this.properties = properties;
1121 var timeout = properties.timeout ? properties.timeout : settings.test_timeout
1122 if (timeout != null) {
1123 this.timeout_length = timeout * tests.timeout_multiplier;
1125 this.timeout_length = null;
1128 this.message = null;
1130 var this_obj = this;
1143 Test.prototype = merge({}, Test.statuses);
1145 Test.prototype.structured_clone = function()
1147 if(!this._structured_clone)
1149 var msg = this.message;
1150 msg = msg ? String(msg) : msg;
1151 this._structured_clone = merge({
1152 name:String(this.name),
1157 return this._structured_clone;
1160 Test.prototype.step = function(func, this_obj)
1162 if (this.phase > this.phases.STARTED)
1166 this.phase = this.phases.STARTED;
1167 //If we don't get a result before the harness times out that will be a test timout
1168 this.set_status(this.TIMEOUT, "Test timed out");
1170 tests.started = true;
1172 if (this.timeout_id === null)
1177 this.steps.push(func);
1179 if (arguments.length === 1)
1186 return func.apply(this_obj, Array.prototype.slice.call(arguments, 2));
1190 if (this.phase >= this.phases.HAS_RESULT)
1194 var message = (typeof e === "object" && e !== null) ? e.message : e;
1195 if (typeof e.stack != "undefined" && typeof e.message == "string") {
1196 //Try to make it more informative for some exceptions, at least
1197 //in Gecko and WebKit. This results in a stack dump instead of
1198 //just errors like "Cannot read property 'parentNode' of null"
1199 //or "root is null". Makes it a lot longer, of course.
1200 message += "(stack: " + e.stack + ")";
1202 this.set_status(this.FAIL, message);
1203 this.phase = this.phases.HAS_RESULT;
1208 Test.prototype.step_func = function(func, this_obj)
1210 var test_this = this;
1212 if (arguments.length === 1)
1214 this_obj = test_this;
1219 test_this.step.apply(test_this, [func, this_obj].concat(
1220 Array.prototype.slice.call(arguments)));
1224 Test.prototype.step_func_done = function(func, this_obj)
1226 var test_this = this;
1228 if (arguments.length === 1)
1230 this_obj = test_this;
1235 test_this.step.apply(test_this, [func, this_obj].concat(
1236 Array.prototype.slice.call(arguments)));
1241 Test.prototype.set_timeout = function()
1243 if (this.timeout_length !== null)
1245 var this_obj = this;
1246 this.timeout_id = setTimeout(function()
1249 }, this.timeout_length);
1253 Test.prototype.set_status = function(status, message)
1255 this.status = status;
1256 this.message = message;
1259 Test.prototype.timeout = function()
1261 this.timeout_id = null;
1262 this.set_status(this.TIMEOUT, "Test timed out")
1263 this.phase = this.phases.HAS_RESULT;
1267 Test.prototype.done = function()
1269 if (this.phase == this.phases.COMPLETE) {
1271 } else if (this.phase <= this.phases.STARTED)
1273 this.set_status(this.PASS, null);
1276 if (this.status == this.NOTRUN)
1281 this.phase = this.phases.COMPLETE;
1283 clearTimeout(this.timeout_id);
1292 function TestsStatus()
1295 this.message = null;
1298 TestsStatus.statuses = {
1304 TestsStatus.prototype = merge({}, TestsStatus.statuses);
1306 TestsStatus.prototype.structured_clone = function()
1308 if(!this._structured_clone)
1310 var msg = this.message;
1311 msg = msg ? String(msg) : msg;
1312 this._structured_clone = merge({
1315 }, TestsStatus.statuses);
1317 return this._structured_clone;
1323 this.num_pending = 0;
1332 this.phase = this.phases.INITIAL;
1334 this.properties = {};
1336 //All tests can't be done until the load event fires
1337 this.all_loaded = false;
1338 this.wait_for_finish = false;
1339 this.processing_callbacks = false;
1341 this.allow_uncaught_exception = false;
1343 this.timeout_multiplier = 1;
1344 this.timeout_length = this.get_timeout();
1345 this.timeout_id = null;
1347 this.start_callbacks = [];
1348 this.test_done_callbacks = [];
1349 this.all_done_callbacks = [];
1351 this.status = new TestsStatus();
1353 var this_obj = this;
1355 on_event(window, "load",
1358 this_obj.all_loaded = true;
1359 if (this_obj.all_done())
1361 this_obj.complete();
1368 Tests.prototype.setup = function(func, properties)
1370 if (this.phase >= this.phases.HAVE_RESULTS)
1374 if (this.phase < this.phases.SETUP)
1376 this.phase = this.phases.SETUP;
1379 this.properties = properties;
1381 for (var p in properties)
1383 if (properties.hasOwnProperty(p))
1385 var value = properties[p]
1386 if (p == "allow_uncaught_exception") {
1387 this.allow_uncaught_exception = value;
1389 else if (p == "explicit_done" && value)
1391 this.wait_for_finish = true;
1393 else if (p == "explicit_timeout" && value) {
1394 this.timeout_length = null;
1395 if (this.timeout_id)
1397 clearTimeout(this.timeout_id);
1400 else if (p == "timeout_multiplier")
1402 this.timeout_multiplier = value;
1414 this.status.status = this.status.ERROR;
1415 this.status.message = e;
1421 Tests.prototype.get_timeout = function()
1423 var metas = document.getElementsByTagName("meta");
1424 for (var i=0; i<metas.length; i++)
1426 if (metas[i].name == "timeout")
1428 if (metas[i].content == "long")
1430 return settings.harness_timeout.long;
1435 return settings.harness_timeout.normal;
1438 Tests.prototype.set_timeout = function()
1440 var this_obj = this;
1441 clearTimeout(this.timeout_id);
1442 if (this.timeout_length !== null)
1444 this.timeout_id = setTimeout(function() {
1446 }, this.timeout_length);
1450 Tests.prototype.timeout = function() {
1451 this.status.status = this.status.TIMEOUT;
1455 Tests.prototype.end_wait = function()
1457 this.wait_for_finish = false;
1458 if (this.all_done()) {
1463 Tests.prototype.push = function(test)
1465 if (this.phase < this.phases.HAVE_TESTS) {
1469 this.tests.push(test);
1472 Tests.prototype.all_done = function() {
1473 return (this.all_loaded && this.num_pending === 0 &&
1474 !this.wait_for_finish && !this.processing_callbacks);
1477 Tests.prototype.start = function() {
1478 this.phase = this.phases.HAVE_TESTS;
1479 this.notify_start();
1482 Tests.prototype.notify_start = function() {
1483 var this_obj = this;
1484 forEach (this.start_callbacks,
1487 callback(this_obj.properties);
1490 function(w, is_same_origin)
1492 if(is_same_origin && w.start_callback)
1496 w.start_callback(this_obj.properties);
1506 if (supports_post_message(w) && w !== self)
1510 properties: this_obj.properties
1516 Tests.prototype.result = function(test)
1518 if (this.phase > this.phases.HAVE_RESULTS)
1522 this.phase = this.phases.HAVE_RESULTS;
1524 this.notify_result(test);
1527 Tests.prototype.notify_result = function(test) {
1528 var this_obj = this;
1529 this.processing_callbacks = true;
1530 forEach(this.test_done_callbacks,
1533 callback(test, this_obj);
1537 function(w, is_same_origin)
1539 if(is_same_origin && w.result_callback)
1543 w.result_callback(test);
1552 if (supports_post_message(w) && w !== self)
1556 test: test.structured_clone()
1560 this.processing_callbacks = false;
1561 if (this_obj.all_done())
1563 this_obj.complete();
1567 Tests.prototype.complete = function() {
1568 if (this.phase === this.phases.COMPLETE) {
1571 this.phase = this.phases.COMPLETE;
1572 var this_obj = this;
1576 if(x.status === x.NOTRUN)
1578 this_obj.notify_result(x);
1582 this.notify_complete();
1585 Tests.prototype.notify_complete = function()
1587 clearTimeout(this.timeout_id);
1588 var this_obj = this;
1589 var tests = map(this_obj.tests,
1592 return test.structured_clone();
1594 if (this.status.status === null)
1596 this.status.status = this.status.OK;
1599 forEach (this.all_done_callbacks,
1602 callback(this_obj.tests, this_obj.status);
1606 function(w, is_same_origin)
1608 if(is_same_origin && w.completion_callback)
1612 w.completion_callback(this_obj.tests, this_obj.status);
1622 if (supports_post_message(w) && w !== self)
1627 status: this_obj.status.structured_clone()
1633 var tests = new Tests();
1635 window.onerror = function(msg) {
1636 if (!tests.allow_uncaught_exception)
1638 tests.status.status = tests.status.ERROR;
1639 tests.status.message = msg;
1644 function timeout() {
1645 if (tests.timeout_length === null)
1650 expose(timeout, 'timeout');
1652 function add_start_callback(callback) {
1653 tests.start_callbacks.push(callback);
1656 function add_result_callback(callback)
1658 tests.test_done_callbacks.push(callback);
1661 function add_completion_callback(callback)
1663 tests.all_done_callbacks.push(callback);
1666 expose(add_start_callback, 'add_start_callback');
1667 expose(add_result_callback, 'add_result_callback');
1668 expose(add_completion_callback, 'add_completion_callback');
1675 this.output_document = document;
1676 this.output_node = null;
1677 this.done_count = 0;
1678 this.enabled = settings.output;
1679 this.phase = this.INITIAL;
1682 Output.prototype.INITIAL = 0;
1683 Output.prototype.STARTED = 1;
1684 Output.prototype.HAVE_RESULTS = 2;
1685 Output.prototype.COMPLETE = 3;
1687 Output.prototype.setup = function(properties) {
1688 if (this.phase > this.INITIAL) {
1692 //If output is disabled in testharnessreport.js the test shouldn't be
1693 //able to override that
1694 this.enabled = this.enabled && (properties.hasOwnProperty("output") ?
1695 properties.output : settings.output);
1698 Output.prototype.init = function(properties)
1700 if (this.phase >= this.STARTED) {
1703 if (properties.output_document) {
1704 this.output_document = properties.output_document;
1706 this.output_document = document;
1708 this.phase = this.STARTED;
1711 Output.prototype.resolve_log = function()
1713 var output_document;
1714 if (typeof this.output_document === "function")
1716 output_document = this.output_document.apply(undefined);
1719 output_document = this.output_document;
1721 if (!output_document)
1725 var node = output_document.getElementById("log");
1728 this.output_document = output_document;
1729 this.output_node = node;
1733 Output.prototype.show_status = function(test)
1735 if (this.phase < this.STARTED)
1743 if (this.phase < this.HAVE_RESULTS)
1746 this.phase = this.HAVE_RESULTS;
1749 if (this.output_node)
1751 if (this.done_count < 100
1752 || (this.done_count < 1000 && this.done_count % 100 == 0)
1753 || this.done_count % 1000 == 0) {
1754 this.output_node.textContent = "Running, "
1755 + this.done_count + " complete, "
1756 + tests.num_pending + " remain";
1761 Output.prototype.show_results = function (tests, harness_status)
1763 if (this.phase >= this.COMPLETE) {
1770 if (!this.output_node) {
1773 this.phase = this.COMPLETE;
1775 var log = this.output_node;
1780 var output_document = this.output_document;
1782 while (log.lastChild)
1784 log.removeChild(log.lastChild);
1787 if (script_prefix != null) {
1788 var stylesheet = output_document.createElementNS(xhtml_ns, "link");
1789 stylesheet.setAttribute("rel", "stylesheet");
1790 stylesheet.setAttribute("href", script_prefix + "testharness.css");
1791 var heads = output_document.getElementsByTagName("head");
1793 heads[0].appendChild(stylesheet);
1797 var status_text_harness = {};
1798 status_text_harness[harness_status.OK] = "OK";
1799 status_text_harness[harness_status.ERROR] = "Error";
1800 status_text_harness[harness_status.TIMEOUT] = "Timeout";
1802 var status_text = {};
1803 status_text[Test.prototype.PASS] = "Pass";
1804 status_text[Test.prototype.FAIL] = "Fail";
1805 status_text[Test.prototype.TIMEOUT] = "Timeout";
1806 status_text[Test.prototype.NOTRUN] = "Not Run";
1808 var status_number = {};
1809 forEach(tests, function(test) {
1810 var status = status_text[test.status];
1811 if (status_number.hasOwnProperty(status))
1813 status_number[status] += 1;
1815 status_number[status] = 1;
1819 function status_class(status)
1821 return status.replace(/\s/g, '').toLowerCase();
1824 var summary_template = ["section", {"id":"summary"},
1825 ["h2", {}, "Summary"],
1828 if (harness_status.status === harness_status.OK)
1834 var status = status_text_harness[harness_status.status];
1835 var rv = [["p", {"class":status_class(status)}]];
1837 if (harness_status.status === harness_status.ERROR)
1839 rv[0].push("Harness encountered an error:");
1840 rv.push(["pre", {}, harness_status.message]);
1842 else if (harness_status.status === harness_status.TIMEOUT)
1844 rv[0].push("Harness timed out.");
1848 rv[0].push("Harness got an unexpected status.");
1854 ["p", {}, "Found ${num_tests} tests"],
1856 var rv = [["div", {}]];
1858 while (status_text.hasOwnProperty(i)) {
1859 if (status_number.hasOwnProperty(status_text[i])) {
1860 var status = status_text[i];
1861 rv[0].push(["div", {"class":status_class(status)},
1863 ["input", {type:"checkbox", checked:"checked"}],
1864 status_number[status] + " " + status]]);
1871 log.appendChild(render(summary_template, {num_tests:tests.length}, output_document));
1873 forEach(output_document.querySelectorAll("section#summary label"),
1876 on_event(element, "click",
1879 if (output_document.getElementById("results") === null)
1884 var result_class = element.parentNode.getAttribute("class");
1885 var style_element = output_document.querySelector("style#hide-" + result_class);
1886 var input_element = element.querySelector("input");
1887 if (!style_element && !input_element.checked) {
1888 style_element = output_document.createElementNS(xhtml_ns, "style");
1889 style_element.id = "hide-" + result_class;
1890 style_element.textContent = "table#results > tbody > tr."+result_class+"{display:none}";
1891 output_document.body.appendChild(style_element);
1892 } else if (style_element && input_element.checked) {
1893 style_element.parentNode.removeChild(style_element);
1898 // This use of innerHTML plus manual escaping is not recommended in
1899 // general, but is necessary here for performance. Using textContent
1900 // on each individual <td> adds tens of seconds of execution time for
1901 // large test suites (tens of thousands of tests).
1902 function escape_html(s)
1904 return s.replace(/\&/g, "&")
1905 .replace(/</g, "<")
1906 .replace(/"/g, """)
1907 .replace(/'/g, "'");
1910 function has_assertions()
1912 for (var i = 0; i < tests.length; i++) {
1913 if (tests[i].properties.hasOwnProperty("assert")) {
1920 function get_assertion(test)
1922 if (test.properties.hasOwnProperty("assert")) {
1923 if (Array.isArray(test.properties.assert)) {
1924 return test.properties.assert.join(' ');
1926 return test.properties.assert;
1931 log.appendChild(document.createElementNS(xhtml_ns, "section"));
1932 var assertions = has_assertions();
1933 var html = "<h2>Details</h2><table id='results' " + (assertions ? "class='assertions'" : "" ) + ">"
1934 + "<thead><tr><th>Result</th><th>Test Name</th>"
1935 + (assertions ? "<th>Assertion</th>" : "")
1936 + "<th>Message</th></tr></thead>"
1938 for (var i = 0; i < tests.length; i++) {
1939 html += '<tr class="'
1940 + escape_html(status_class(status_text[tests[i].status]))
1942 + escape_html(status_text[tests[i].status])
1944 + escape_html(tests[i].name)
1946 + (assertions ? escape_html(get_assertion(tests[i])) + "</td><td>" : "")
1947 + escape_html(tests[i].message ? tests[i].message : " ")
1950 html += "</tbody></table>";
1952 log.lastChild.innerHTML = html;
1954 log.appendChild(document.createElementNS(xhtml_ns, "p"))
1955 .textContent = "Setting innerHTML for the log threw an exception.";
1956 log.appendChild(document.createElementNS(xhtml_ns, "pre"))
1957 .textContent = html;
1961 var output = new Output();
1962 add_start_callback(function (properties) {output.init(properties);});
1963 add_result_callback(function (test) {output.show_status(tests);});
1964 add_completion_callback(function (tests, harness_status) {output.show_results(tests, harness_status);});
1969 * A template is just a javascript structure. An element is represented as:
1971 * [tag_name, {attr_name:attr_value}, child1, child2]
1973 * the children can either be strings (which act like text nodes), other templates or
1974 * functions (see below)
1976 * A text node is represented as
1980 * String values have a simple substitution syntax; ${foo} represents a variable foo.
1982 * It is possible to embed logic in templates by using a function in a place where a
1983 * node would usually go. The function must either return part of a template or null.
1985 * In cases where a set of nodes are required as output rather than a single node
1986 * with children it is possible to just use a list
1987 * [node1, node2, node3]
1991 * render(template, substitutions) - take a template and an object mapping
1992 * variable names to parameters and return either a DOM node or a list of DOM nodes
1994 * substitute(template, substitutions) - take a template and variable mapping object,
1995 * make the variable substitutions and return the substituted template
1999 function is_single_node(template)
2001 return typeof template[0] === "string";
2004 function substitute(template, substitutions)
2006 if (typeof template === "function") {
2007 var replacement = template(substitutions);
2010 var rv = substitute(replacement, substitutions);
2018 else if (is_single_node(template))
2020 return substitute_single(template, substitutions);
2024 return filter(map(template, function(x) {
2025 return substitute(x, substitutions);
2026 }), function(x) {return x !== null;});
2030 function substitute_single(template, substitutions)
2032 var substitution_re = /\${([^ }]*)}/g;
2034 function do_substitution(input) {
2035 var components = input.split(substitution_re);
2037 for (var i=0; i<components.length; i+=2)
2039 rv.push(components[i]);
2040 if (components[i+1])
2042 rv.push(String(substitutions[components[i+1]]));
2049 rv.push(do_substitution(String(template[0])).join(""));
2051 if (template[0] === "{text}") {
2052 substitute_children(template.slice(1), rv);
2054 substitute_attrs(template[1], rv);
2055 substitute_children(template.slice(2), rv);
2058 function substitute_attrs(attrs, rv)
2061 for (var name in template[1])
2063 if (attrs.hasOwnProperty(name))
2065 var new_name = do_substitution(name).join("");
2066 var new_value = do_substitution(attrs[name]).join("");
2067 rv[1][new_name] = new_value;
2072 function substitute_children(children, rv)
2074 for (var i=0; i<children.length; i++)
2076 if (children[i] instanceof Object) {
2077 var replacement = substitute(children[i], substitutions);
2078 if (replacement !== null)
2080 if (is_single_node(replacement))
2082 rv.push(replacement);
2086 extend(rv, replacement);
2092 extend(rv, do_substitution(String(children[i])));
2101 function make_dom_single(template, doc)
2103 var output_document = doc || document;
2104 if (template[0] === "{text}")
2106 var element = output_document.createTextNode("");
2107 for (var i=1; i<template.length; i++)
2109 element.data += template[i];
2114 var element = output_document.createElementNS(xhtml_ns, template[0]);
2115 for (var name in template[1]) {
2116 if (template[1].hasOwnProperty(name))
2118 element.setAttribute(name, template[1][name]);
2121 for (var i=2; i<template.length; i++)
2123 if (template[i] instanceof Object)
2125 var sub_element = make_dom(template[i]);
2126 element.appendChild(sub_element);
2130 var text_node = output_document.createTextNode(template[i]);
2131 element.appendChild(text_node);
2141 function make_dom(template, substitutions, output_document)
2143 if (is_single_node(template))
2145 return make_dom_single(template, output_document);
2149 return map(template, function(x) {
2150 return make_dom_single(x, output_document);
2155 function render(template, substitutions, output_document)
2157 return make_dom(substitute(template, substitutions), output_document);
2163 function assert(expected_true, function_name, description, error, substitutions)
2165 if (expected_true !== true)
2167 throw new AssertionError(make_message(function_name, description,
2168 error, substitutions));
2172 function AssertionError(message)
2174 this.message = message;
2177 function make_message(function_name, description, error, substitutions)
2179 for (var p in substitutions) {
2180 if (substitutions.hasOwnProperty(p)) {
2181 substitutions[p] = format_value(substitutions[p]);
2184 var node_form = substitute(["{text}", "${function_name}: ${description}" + error],
2185 merge({function_name:function_name,
2186 description:(description?description + " ":"")},
2188 return node_form.slice(1).join("");
2191 function filter(array, callable, thisObj) {
2193 for (var i=0; i<array.length; i++)
2195 if (array.hasOwnProperty(i))
2197 var pass = callable.call(thisObj, array[i], i, array);
2206 function map(array, callable, thisObj)
2209 rv.length = array.length;
2210 for (var i=0; i<array.length; i++)
2212 if (array.hasOwnProperty(i))
2214 rv[i] = callable.call(thisObj, array[i], i, array);
2220 function extend(array, items)
2222 Array.prototype.push.apply(array, items);
2225 function forEach (array, callback, thisObj)
2227 for (var i=0; i<array.length; i++)
2229 if (array.hasOwnProperty(i))
2231 callback.call(thisObj, array[i], i, array);
2250 function expose(object, name)
2252 var components = name.split(".");
2253 var target = window;
2254 for (var i=0; i<components.length - 1; i++)
2256 if (!(components[i] in target))
2258 target[components[i]] = {};
2260 target = target[components[i]];
2262 target[components[components.length - 1]] = object;
2265 function forEach_windows(callback) {
2266 // Iterate of the the windows [self ... top, opener]. The callback is passed
2267 // two objects, the first one is the windows object itself, the second one
2268 // is a boolean indicating whether or not its on the same origin as the
2270 var cache = forEach_windows.result_cache;
2272 cache = [[self, true]];
2276 var origins = location.ancestorOrigins;
2277 while (w != w.parent)
2280 // In WebKit, calls to parent windows' properties that aren't on the same
2281 // origin cause an error message to be displayed in the error console but
2282 // don't throw an exception. This is a deviation from the current HTML5
2283 // spec. See: https://bugs.webkit.org/show_bug.cgi?id=43504
2284 // The problem with WebKit's behavior is that it pollutes the error console
2285 // with error messages that can't be caught.
2287 // This issue can be mitigated by relying on the (for now) proprietary
2288 // `location.ancestorOrigins` property which returns an ordered list of
2289 // the origins of enclosing windows. See:
2290 // http://trac.webkit.org/changeset/113945.
2292 so = (location.origin == origins[i]);
2296 so = is_same_origin(w);
2298 cache.push([w, so]);
2304 // window.opener isn't included in the `location.ancestorOrigins` prop.
2305 // We'll just have to deal with a simple check and an error msg on WebKit
2306 // browsers in this case.
2307 cache.push([w, is_same_origin(w)]);
2309 forEach_windows.result_cache = cache;
2315 callback.apply(null, a);
2319 function is_same_origin(w) {
2328 function supports_post_message(w)
2332 // Given IE implements postMessage across nested iframes but not across
2333 // windows or tabs, you can't infer cross-origin communication from the presence
2334 // of postMessage on the current window object only.
2336 // Touching the postMessage prop on a window can throw if the window is
2337 // not from the same origin AND post message is not supported in that
2338 // browser. So just doing an existence test here won't do, you also need
2339 // to wrap it in a try..cacth block.
2342 type = typeof w.postMessage;
2343 if (type === "function")
2347 // IE8 supports postMessage, but implements it as a host object which
2348 // returns "object" as its `typeof`.
2349 else if (type === "object")
2353 // This is the case where postMessage isn't supported AND accessing a
2354 // window property across origins does NOT throw (e.g. old Safari browser).
2361 // This is the case where postMessage isn't supported AND accessing a
2362 // window property across origins throws (e.g. old Firefox browser).
2368 // vim: set expandtab shiftwidth=4 tabstop=4: