mediawiki-extensions-Visual.../modules/ve/test/ve.test.js

466 lines
12 KiB
JavaScript
Raw Normal View History

JSDuck: Generated code documentation! See CODING.md for how to run it. Mistakes fixed: * Warning: Unknown type function -> Function * Warning: Unknown type DOMElement -> HTMLElement * Warning: Unknown type DOM Node -> HTMLElement * Warning: Unknown type Integer -> Mixed * Warning: Unknown type Command -> ve.Command * Warning: Unknown type any -> number * Warning: Unknown type ve.Transaction -> ve.dm.Transaction * Warning: Unknown type ve.dm.AnnotationSet -> ve.AnnotationSet * Warning: Unknown type false -> boolean * Warning: Unknown type ve.dm.AlienNode ve.dm doesn't have a generic AlienNode like ve.ce -> Unknown type ve.dm.AlienInlineNode|ve.dm.AlienBlockNode * Warning: Unknown type ve.ve.Surface -> ve.ce.Surface * ve.example.lookupNode: -> Last @param should be @return * ve.dm.Transaction.prototype.pushReplace: -> @param {Array] should be @param {Array} * Warning: ve.BranchNode.js:27: {@link ve.Node#hasChildren} links to non-existing member -> (removed) * Warning: ve.LeafNode.js:21: {@link ve.Node#hasChildren} links to non-existing member -> (removed) Differences fixed: * Variadic arguments are like @param {Type...} [name] instead of @param {Type} [name...] * Convert all file headers from /** to /*! because JSDuck tries to parse all /** blocks and fails to parse with all sorts of errors for "Global property", "Unnamed property", and "Duplicate property". Find: \/\*\*([^@]+)(@copyright) Replace: /*!$1$2 * Indented blocks are considered code examples. A few methods had documentation with numbered lists that were indented, which have now been updated to not be intended. * The free-form text descriptions are parsed with Markdown, which requires lists to be separated from paragraphs by an empty line. And we should use `backticks` instead of {braces} for inline code in text paragraphs. * Doc blocks for classes and their constructor have to be in the correct order (@constructor, @param, @return must be before @class, @abstract, @extends etc.) * `@extends Class` must not have Class {wrapped} * @throws must start with a {Type} * @example means something else. It is used for an inline demo iframe, not code block. For that simply indent with spaces. * @member means something else. Non-function properties are marked with @property, not @member. * To create a link to a class or member, in most cases the name is enough to create a link. E.g. Foo, Foo.bar, Foo.bar#quux, where a hash stands for "instance member", so Foo.bar#quux, links to Foo.bar.prototype.quux (the is not supported, as "prototype" is considered an implementation detail, it only indexes class name and method name). If the magic linker doesn't work for some case, the verbose syntax is {@link #target label}. * @property can't have sub-properties (nested @param and @return values are supported, only @static @property can't be nested). We only have one case of this, which can be worked around by moving those in a new virtual class. The code is unaltered (only moved down so that it isn't with the scope of the main @class block). ve.dm.TransactionProcessor.processors. New: * @mixins: Classes mixed into the current class. * @event: Events that can be emitted by a class. These are also inherited by subclasses. (+ @param, @return and @preventable). So ve.Node#event-attach is inherited to ve.dm.BreakNode, just like @method is. * @singleton: Plain objects such as ve, ve.dm, ve.ce were missing documentation causing a tree error. Documented those as a JSDuck singleton, which they but just weren't documented yet. NB: Members of @singleton don't need @static (if present, triggers a compiler warning). * @chainable: Shorthand for "@return this". We were using "@return {classname}" which is ambiguous (returns the same instance or another instance?), @chainable is specifically for "@return this". Creates proper labels in the generated HTML pages. Removed: * @mixin: (not to be confused with @mixins). Not supported by JSDuck. Every class is standalone anyway. Where needed marked them @class + @abstract instead. Change-Id: I6a7c9e8ee8f995731bc205d666167874eb2ebe23
2013-01-04 08:54:17 +00:00
/*!
Refactor ve.getHash: Stabilize cross-browser differences; + unit tests * Replaces c8b4a289364966432b58104e975d37cda1fefb84 * Use Object() casting to detect objects instead of .constructor (or instanceof). Both .constructor and instanceof compare by reference the type "Object" which means if the object comes from another window (where there is a different "Object" and "Object.prototype") it will drop out of the system and go freewack. Theory: If a variable casted to an object returns true when strictly compared to the original, the input must be an object. Which is true. It doesn't change the inheritance, it doesn't make it inherit from this window's Object if the object is from another window's object. All it does is cast to an object if not an object already. So e.g. "Object(5) !== 5" because 5 is a primitive value as opposed to an instance of Number. And contrary to "typeof", it doesn't return true for "null". * .constructor also has the problem that it only works this way if the input is a plain object. e.g. a simple construtor function that creates an object also get in the wrong side of the if/else case since it is an instance of Object, but not directly (rather indirectly via another constructor). * Added unit tests for basic getHash usage, as well as regression tests against the above two mentioned problems (these tests fail before this commit). * While at it, also improved other utilities a bit. - Use hasOwnProperty instead of casting to boolean when checking for presence of native support. Thanks to Douglas Crockford for that tip. - Fix documentation for ve.getHash: Parameter is not named "obj". - Add Object-check to ve.getObjectKeys per ES5 Object.keys spec (to match native behavior) - Add Object-check to ve.getObjectValues to match ve.getObjectKeys - Improved performance of ve.getObjectKeys shim. Tried several potential optimizations and compared with jsperf. Using a "static" reference to hasOwn improves performance (by not having to look it up 4 scopes up and 3 property levels deep). Also using [.length] instead of .push() shared off a few ms. - Added unit tests for ve.getObjectValues Change-Id: If24d09405321f201c67f7df75d332bb1171c8a36
2012-08-12 18:27:31 +00:00
* VisualEditor Base method tests.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
Refactor ve.getHash: Stabilize cross-browser differences; + unit tests * Replaces c8b4a289364966432b58104e975d37cda1fefb84 * Use Object() casting to detect objects instead of .constructor (or instanceof). Both .constructor and instanceof compare by reference the type "Object" which means if the object comes from another window (where there is a different "Object" and "Object.prototype") it will drop out of the system and go freewack. Theory: If a variable casted to an object returns true when strictly compared to the original, the input must be an object. Which is true. It doesn't change the inheritance, it doesn't make it inherit from this window's Object if the object is from another window's object. All it does is cast to an object if not an object already. So e.g. "Object(5) !== 5" because 5 is a primitive value as opposed to an instance of Number. And contrary to "typeof", it doesn't return true for "null". * .constructor also has the problem that it only works this way if the input is a plain object. e.g. a simple construtor function that creates an object also get in the wrong side of the if/else case since it is an instance of Object, but not directly (rather indirectly via another constructor). * Added unit tests for basic getHash usage, as well as regression tests against the above two mentioned problems (these tests fail before this commit). * While at it, also improved other utilities a bit. - Use hasOwnProperty instead of casting to boolean when checking for presence of native support. Thanks to Douglas Crockford for that tip. - Fix documentation for ve.getHash: Parameter is not named "obj". - Add Object-check to ve.getObjectKeys per ES5 Object.keys spec (to match native behavior) - Add Object-check to ve.getObjectValues to match ve.getObjectKeys - Improved performance of ve.getObjectKeys shim. Tried several potential optimizations and compared with jsperf. Using a "static" reference to hasOwn improves performance (by not having to look it up 4 scopes up and 3 property levels deep). Also using [.length] instead of .push() shared off a few ms. - Added unit tests for ve.getObjectValues Change-Id: If24d09405321f201c67f7df75d332bb1171c8a36
2012-08-12 18:27:31 +00:00
* @license The MIT License (MIT); see LICENSE.txt
*/
QUnit.module( 've' );
/* Tests */
// ve.cloneObject: Tested upstream (OOJS)
// ve.getObjectValues: Tested upstream (OOJS)
// ve.getObjectKeys: Tested upstream (JavaScript)
// ve.compare: Tested upstream (OOJS)
// ve.copy: Tested upstream (OOJS)
// ve.isPlainObject: Tested upstream (jQuery)
// ve.isEmptyObject: Tested upstream (jQuery)
// ve.isArray: Tested upstream (jQuery)
// ve.bind: Tested upstream (jQuery)
// ve.indexOf: Tested upstream (jQuery)
// ve.extendObject: Tested upstream (jQuery)
QUnit.test( 'getHash: Basic usage', 7, function ( assert ) {
var tmp,
cases = {},
hash = '{"a":1,"b":1,"c":1}',
customHash = '{"first":1,"last":1}';
cases['a-z literal'] = {
object: {
a: 1,
b: 1,
c: 1
},
hash: hash
Refactor ve.getHash: Stabilize cross-browser differences; + unit tests * Replaces c8b4a289364966432b58104e975d37cda1fefb84 * Use Object() casting to detect objects instead of .constructor (or instanceof). Both .constructor and instanceof compare by reference the type "Object" which means if the object comes from another window (where there is a different "Object" and "Object.prototype") it will drop out of the system and go freewack. Theory: If a variable casted to an object returns true when strictly compared to the original, the input must be an object. Which is true. It doesn't change the inheritance, it doesn't make it inherit from this window's Object if the object is from another window's object. All it does is cast to an object if not an object already. So e.g. "Object(5) !== 5" because 5 is a primitive value as opposed to an instance of Number. And contrary to "typeof", it doesn't return true for "null". * .constructor also has the problem that it only works this way if the input is a plain object. e.g. a simple construtor function that creates an object also get in the wrong side of the if/else case since it is an instance of Object, but not directly (rather indirectly via another constructor). * Added unit tests for basic getHash usage, as well as regression tests against the above two mentioned problems (these tests fail before this commit). * While at it, also improved other utilities a bit. - Use hasOwnProperty instead of casting to boolean when checking for presence of native support. Thanks to Douglas Crockford for that tip. - Fix documentation for ve.getHash: Parameter is not named "obj". - Add Object-check to ve.getObjectKeys per ES5 Object.keys spec (to match native behavior) - Add Object-check to ve.getObjectValues to match ve.getObjectKeys - Improved performance of ve.getObjectKeys shim. Tried several potential optimizations and compared with jsperf. Using a "static" reference to hasOwn improves performance (by not having to look it up 4 scopes up and 3 property levels deep). Also using [.length] instead of .push() shared off a few ms. - Added unit tests for ve.getObjectValues Change-Id: If24d09405321f201c67f7df75d332bb1171c8a36
2012-08-12 18:27:31 +00:00
};
cases['z-a literal'] = {
object: {
c: 1,
b: 1,
a: 1
},
hash: hash
Refactor ve.getHash: Stabilize cross-browser differences; + unit tests * Replaces c8b4a289364966432b58104e975d37cda1fefb84 * Use Object() casting to detect objects instead of .constructor (or instanceof). Both .constructor and instanceof compare by reference the type "Object" which means if the object comes from another window (where there is a different "Object" and "Object.prototype") it will drop out of the system and go freewack. Theory: If a variable casted to an object returns true when strictly compared to the original, the input must be an object. Which is true. It doesn't change the inheritance, it doesn't make it inherit from this window's Object if the object is from another window's object. All it does is cast to an object if not an object already. So e.g. "Object(5) !== 5" because 5 is a primitive value as opposed to an instance of Number. And contrary to "typeof", it doesn't return true for "null". * .constructor also has the problem that it only works this way if the input is a plain object. e.g. a simple construtor function that creates an object also get in the wrong side of the if/else case since it is an instance of Object, but not directly (rather indirectly via another constructor). * Added unit tests for basic getHash usage, as well as regression tests against the above two mentioned problems (these tests fail before this commit). * While at it, also improved other utilities a bit. - Use hasOwnProperty instead of casting to boolean when checking for presence of native support. Thanks to Douglas Crockford for that tip. - Fix documentation for ve.getHash: Parameter is not named "obj". - Add Object-check to ve.getObjectKeys per ES5 Object.keys spec (to match native behavior) - Add Object-check to ve.getObjectValues to match ve.getObjectKeys - Improved performance of ve.getObjectKeys shim. Tried several potential optimizations and compared with jsperf. Using a "static" reference to hasOwn improves performance (by not having to look it up 4 scopes up and 3 property levels deep). Also using [.length] instead of .push() shared off a few ms. - Added unit tests for ve.getObjectValues Change-Id: If24d09405321f201c67f7df75d332bb1171c8a36
2012-08-12 18:27:31 +00:00
};
tmp = {};
cases['a-z augmented'] = {
object: tmp,
hash: hash
};
Refactor ve.getHash: Stabilize cross-browser differences; + unit tests * Replaces c8b4a289364966432b58104e975d37cda1fefb84 * Use Object() casting to detect objects instead of .constructor (or instanceof). Both .constructor and instanceof compare by reference the type "Object" which means if the object comes from another window (where there is a different "Object" and "Object.prototype") it will drop out of the system and go freewack. Theory: If a variable casted to an object returns true when strictly compared to the original, the input must be an object. Which is true. It doesn't change the inheritance, it doesn't make it inherit from this window's Object if the object is from another window's object. All it does is cast to an object if not an object already. So e.g. "Object(5) !== 5" because 5 is a primitive value as opposed to an instance of Number. And contrary to "typeof", it doesn't return true for "null". * .constructor also has the problem that it only works this way if the input is a plain object. e.g. a simple construtor function that creates an object also get in the wrong side of the if/else case since it is an instance of Object, but not directly (rather indirectly via another constructor). * Added unit tests for basic getHash usage, as well as regression tests against the above two mentioned problems (these tests fail before this commit). * While at it, also improved other utilities a bit. - Use hasOwnProperty instead of casting to boolean when checking for presence of native support. Thanks to Douglas Crockford for that tip. - Fix documentation for ve.getHash: Parameter is not named "obj". - Add Object-check to ve.getObjectKeys per ES5 Object.keys spec (to match native behavior) - Add Object-check to ve.getObjectValues to match ve.getObjectKeys - Improved performance of ve.getObjectKeys shim. Tried several potential optimizations and compared with jsperf. Using a "static" reference to hasOwn improves performance (by not having to look it up 4 scopes up and 3 property levels deep). Also using [.length] instead of .push() shared off a few ms. - Added unit tests for ve.getObjectValues Change-Id: If24d09405321f201c67f7df75d332bb1171c8a36
2012-08-12 18:27:31 +00:00
tmp.a = 1;
tmp.b = 1;
tmp.c = 1;
tmp = {};
cases['z-a augmented'] = {
object: tmp,
hash: hash
};
Refactor ve.getHash: Stabilize cross-browser differences; + unit tests * Replaces c8b4a289364966432b58104e975d37cda1fefb84 * Use Object() casting to detect objects instead of .constructor (or instanceof). Both .constructor and instanceof compare by reference the type "Object" which means if the object comes from another window (where there is a different "Object" and "Object.prototype") it will drop out of the system and go freewack. Theory: If a variable casted to an object returns true when strictly compared to the original, the input must be an object. Which is true. It doesn't change the inheritance, it doesn't make it inherit from this window's Object if the object is from another window's object. All it does is cast to an object if not an object already. So e.g. "Object(5) !== 5" because 5 is a primitive value as opposed to an instance of Number. And contrary to "typeof", it doesn't return true for "null". * .constructor also has the problem that it only works this way if the input is a plain object. e.g. a simple construtor function that creates an object also get in the wrong side of the if/else case since it is an instance of Object, but not directly (rather indirectly via another constructor). * Added unit tests for basic getHash usage, as well as regression tests against the above two mentioned problems (these tests fail before this commit). * While at it, also improved other utilities a bit. - Use hasOwnProperty instead of casting to boolean when checking for presence of native support. Thanks to Douglas Crockford for that tip. - Fix documentation for ve.getHash: Parameter is not named "obj". - Add Object-check to ve.getObjectKeys per ES5 Object.keys spec (to match native behavior) - Add Object-check to ve.getObjectValues to match ve.getObjectKeys - Improved performance of ve.getObjectKeys shim. Tried several potential optimizations and compared with jsperf. Using a "static" reference to hasOwn improves performance (by not having to look it up 4 scopes up and 3 property levels deep). Also using [.length] instead of .push() shared off a few ms. - Added unit tests for ve.getObjectValues Change-Id: If24d09405321f201c67f7df75d332bb1171c8a36
2012-08-12 18:27:31 +00:00
tmp.c = 1;
tmp.b = 1;
tmp.a = 1;
cases['custom hash'] = {
object: {
getHashObject: function () {
return {
'first': 1,
'last': 1
};
}
},
hash: customHash
};
cases['custom hash reversed'] = {
object: {
getHashObject: function () {
return {
'last': 1,
'first': 1
};
}
},
hash: customHash
};
Refactor ve.getHash: Stabilize cross-browser differences; + unit tests * Replaces c8b4a289364966432b58104e975d37cda1fefb84 * Use Object() casting to detect objects instead of .constructor (or instanceof). Both .constructor and instanceof compare by reference the type "Object" which means if the object comes from another window (where there is a different "Object" and "Object.prototype") it will drop out of the system and go freewack. Theory: If a variable casted to an object returns true when strictly compared to the original, the input must be an object. Which is true. It doesn't change the inheritance, it doesn't make it inherit from this window's Object if the object is from another window's object. All it does is cast to an object if not an object already. So e.g. "Object(5) !== 5" because 5 is a primitive value as opposed to an instance of Number. And contrary to "typeof", it doesn't return true for "null". * .constructor also has the problem that it only works this way if the input is a plain object. e.g. a simple construtor function that creates an object also get in the wrong side of the if/else case since it is an instance of Object, but not directly (rather indirectly via another constructor). * Added unit tests for basic getHash usage, as well as regression tests against the above two mentioned problems (these tests fail before this commit). * While at it, also improved other utilities a bit. - Use hasOwnProperty instead of casting to boolean when checking for presence of native support. Thanks to Douglas Crockford for that tip. - Fix documentation for ve.getHash: Parameter is not named "obj". - Add Object-check to ve.getObjectKeys per ES5 Object.keys spec (to match native behavior) - Add Object-check to ve.getObjectValues to match ve.getObjectKeys - Improved performance of ve.getObjectKeys shim. Tried several potential optimizations and compared with jsperf. Using a "static" reference to hasOwn improves performance (by not having to look it up 4 scopes up and 3 property levels deep). Also using [.length] instead of .push() shared off a few ms. - Added unit tests for ve.getObjectValues Change-Id: If24d09405321f201c67f7df75d332bb1171c8a36
2012-08-12 18:27:31 +00:00
$.each( cases, function ( key, val ) {
Refactor ve.getHash: Stabilize cross-browser differences; + unit tests * Replaces c8b4a289364966432b58104e975d37cda1fefb84 * Use Object() casting to detect objects instead of .constructor (or instanceof). Both .constructor and instanceof compare by reference the type "Object" which means if the object comes from another window (where there is a different "Object" and "Object.prototype") it will drop out of the system and go freewack. Theory: If a variable casted to an object returns true when strictly compared to the original, the input must be an object. Which is true. It doesn't change the inheritance, it doesn't make it inherit from this window's Object if the object is from another window's object. All it does is cast to an object if not an object already. So e.g. "Object(5) !== 5" because 5 is a primitive value as opposed to an instance of Number. And contrary to "typeof", it doesn't return true for "null". * .constructor also has the problem that it only works this way if the input is a plain object. e.g. a simple construtor function that creates an object also get in the wrong side of the if/else case since it is an instance of Object, but not directly (rather indirectly via another constructor). * Added unit tests for basic getHash usage, as well as regression tests against the above two mentioned problems (these tests fail before this commit). * While at it, also improved other utilities a bit. - Use hasOwnProperty instead of casting to boolean when checking for presence of native support. Thanks to Douglas Crockford for that tip. - Fix documentation for ve.getHash: Parameter is not named "obj". - Add Object-check to ve.getObjectKeys per ES5 Object.keys spec (to match native behavior) - Add Object-check to ve.getObjectValues to match ve.getObjectKeys - Improved performance of ve.getObjectKeys shim. Tried several potential optimizations and compared with jsperf. Using a "static" reference to hasOwn improves performance (by not having to look it up 4 scopes up and 3 property levels deep). Also using [.length] instead of .push() shared off a few ms. - Added unit tests for ve.getObjectValues Change-Id: If24d09405321f201c67f7df75d332bb1171c8a36
2012-08-12 18:27:31 +00:00
assert.equal(
ve.getHash( val.object ),
val.hash,
key + ': object has expected hash, regardless of "property order"'
Refactor ve.getHash: Stabilize cross-browser differences; + unit tests * Replaces c8b4a289364966432b58104e975d37cda1fefb84 * Use Object() casting to detect objects instead of .constructor (or instanceof). Both .constructor and instanceof compare by reference the type "Object" which means if the object comes from another window (where there is a different "Object" and "Object.prototype") it will drop out of the system and go freewack. Theory: If a variable casted to an object returns true when strictly compared to the original, the input must be an object. Which is true. It doesn't change the inheritance, it doesn't make it inherit from this window's Object if the object is from another window's object. All it does is cast to an object if not an object already. So e.g. "Object(5) !== 5" because 5 is a primitive value as opposed to an instance of Number. And contrary to "typeof", it doesn't return true for "null". * .constructor also has the problem that it only works this way if the input is a plain object. e.g. a simple construtor function that creates an object also get in the wrong side of the if/else case since it is an instance of Object, but not directly (rather indirectly via another constructor). * Added unit tests for basic getHash usage, as well as regression tests against the above two mentioned problems (these tests fail before this commit). * While at it, also improved other utilities a bit. - Use hasOwnProperty instead of casting to boolean when checking for presence of native support. Thanks to Douglas Crockford for that tip. - Fix documentation for ve.getHash: Parameter is not named "obj". - Add Object-check to ve.getObjectKeys per ES5 Object.keys spec (to match native behavior) - Add Object-check to ve.getObjectValues to match ve.getObjectKeys - Improved performance of ve.getObjectKeys shim. Tried several potential optimizations and compared with jsperf. Using a "static" reference to hasOwn improves performance (by not having to look it up 4 scopes up and 3 property levels deep). Also using [.length] instead of .push() shared off a few ms. - Added unit tests for ve.getObjectValues Change-Id: If24d09405321f201c67f7df75d332bb1171c8a36
2012-08-12 18:27:31 +00:00
);
} );
Refactor ve.getHash: Stabilize cross-browser differences; + unit tests * Replaces c8b4a289364966432b58104e975d37cda1fefb84 * Use Object() casting to detect objects instead of .constructor (or instanceof). Both .constructor and instanceof compare by reference the type "Object" which means if the object comes from another window (where there is a different "Object" and "Object.prototype") it will drop out of the system and go freewack. Theory: If a variable casted to an object returns true when strictly compared to the original, the input must be an object. Which is true. It doesn't change the inheritance, it doesn't make it inherit from this window's Object if the object is from another window's object. All it does is cast to an object if not an object already. So e.g. "Object(5) !== 5" because 5 is a primitive value as opposed to an instance of Number. And contrary to "typeof", it doesn't return true for "null". * .constructor also has the problem that it only works this way if the input is a plain object. e.g. a simple construtor function that creates an object also get in the wrong side of the if/else case since it is an instance of Object, but not directly (rather indirectly via another constructor). * Added unit tests for basic getHash usage, as well as regression tests against the above two mentioned problems (these tests fail before this commit). * While at it, also improved other utilities a bit. - Use hasOwnProperty instead of casting to boolean when checking for presence of native support. Thanks to Douglas Crockford for that tip. - Fix documentation for ve.getHash: Parameter is not named "obj". - Add Object-check to ve.getObjectKeys per ES5 Object.keys spec (to match native behavior) - Add Object-check to ve.getObjectValues to match ve.getObjectKeys - Improved performance of ve.getObjectKeys shim. Tried several potential optimizations and compared with jsperf. Using a "static" reference to hasOwn improves performance (by not having to look it up 4 scopes up and 3 property levels deep). Also using [.length] instead of .push() shared off a few ms. - Added unit tests for ve.getObjectValues Change-Id: If24d09405321f201c67f7df75d332bb1171c8a36
2012-08-12 18:27:31 +00:00
// .. and that something completely different is in face different
// (just incase getHash is broken and always returns the same)
assert.notEqual(
ve.getHash( { a: 2, b: 2 } ),
hash,
'A different object has a different hash'
);
} );
QUnit.test( 'getHash: Complex usage', 3, function ( assert ) {
Refactor ve.getHash: Stabilize cross-browser differences; + unit tests * Replaces c8b4a289364966432b58104e975d37cda1fefb84 * Use Object() casting to detect objects instead of .constructor (or instanceof). Both .constructor and instanceof compare by reference the type "Object" which means if the object comes from another window (where there is a different "Object" and "Object.prototype") it will drop out of the system and go freewack. Theory: If a variable casted to an object returns true when strictly compared to the original, the input must be an object. Which is true. It doesn't change the inheritance, it doesn't make it inherit from this window's Object if the object is from another window's object. All it does is cast to an object if not an object already. So e.g. "Object(5) !== 5" because 5 is a primitive value as opposed to an instance of Number. And contrary to "typeof", it doesn't return true for "null". * .constructor also has the problem that it only works this way if the input is a plain object. e.g. a simple construtor function that creates an object also get in the wrong side of the if/else case since it is an instance of Object, but not directly (rather indirectly via another constructor). * Added unit tests for basic getHash usage, as well as regression tests against the above two mentioned problems (these tests fail before this commit). * While at it, also improved other utilities a bit. - Use hasOwnProperty instead of casting to boolean when checking for presence of native support. Thanks to Douglas Crockford for that tip. - Fix documentation for ve.getHash: Parameter is not named "obj". - Add Object-check to ve.getObjectKeys per ES5 Object.keys spec (to match native behavior) - Add Object-check to ve.getObjectValues to match ve.getObjectKeys - Improved performance of ve.getObjectKeys shim. Tried several potential optimizations and compared with jsperf. Using a "static" reference to hasOwn improves performance (by not having to look it up 4 scopes up and 3 property levels deep). Also using [.length] instead of .push() shared off a few ms. - Added unit tests for ve.getObjectValues Change-Id: If24d09405321f201c67f7df75d332bb1171c8a36
2012-08-12 18:27:31 +00:00
var obj, hash, frame;
obj = {
a: 1,
b: 1,
c: 1,
// Nested array
d: ['x', 'y', 'z'],
e: {
a: 2,
b: 2,
c: 2
}
};
assert.equal(
ve.getHash( obj ),
'{"a":1,"b":1,"c":1,"d":["x","y","z"],"e":{"a":2,"b":2,"c":2}}',
'Object with nested array and nested object'
Refactor ve.getHash: Stabilize cross-browser differences; + unit tests * Replaces c8b4a289364966432b58104e975d37cda1fefb84 * Use Object() casting to detect objects instead of .constructor (or instanceof). Both .constructor and instanceof compare by reference the type "Object" which means if the object comes from another window (where there is a different "Object" and "Object.prototype") it will drop out of the system and go freewack. Theory: If a variable casted to an object returns true when strictly compared to the original, the input must be an object. Which is true. It doesn't change the inheritance, it doesn't make it inherit from this window's Object if the object is from another window's object. All it does is cast to an object if not an object already. So e.g. "Object(5) !== 5" because 5 is a primitive value as opposed to an instance of Number. And contrary to "typeof", it doesn't return true for "null". * .constructor also has the problem that it only works this way if the input is a plain object. e.g. a simple construtor function that creates an object also get in the wrong side of the if/else case since it is an instance of Object, but not directly (rather indirectly via another constructor). * Added unit tests for basic getHash usage, as well as regression tests against the above two mentioned problems (these tests fail before this commit). * While at it, also improved other utilities a bit. - Use hasOwnProperty instead of casting to boolean when checking for presence of native support. Thanks to Douglas Crockford for that tip. - Fix documentation for ve.getHash: Parameter is not named "obj". - Add Object-check to ve.getObjectKeys per ES5 Object.keys spec (to match native behavior) - Add Object-check to ve.getObjectValues to match ve.getObjectKeys - Improved performance of ve.getObjectKeys shim. Tried several potential optimizations and compared with jsperf. Using a "static" reference to hasOwn improves performance (by not having to look it up 4 scopes up and 3 property levels deep). Also using [.length] instead of .push() shared off a few ms. - Added unit tests for ve.getObjectValues Change-Id: If24d09405321f201c67f7df75d332bb1171c8a36
2012-08-12 18:27:31 +00:00
);
// Include a circular reference
/*
* PhantomJS hangs when calling JSON.stringify with an object containing a
* circular reference (https://github.com/ariya/phantomjs/issues/11206).
* We know latest Chrome/Firefox and IE8+ support this. So, for the sake of
* having qunit/phantomjs work, lets disable this for now.
Refactor ve.getHash: Stabilize cross-browser differences; + unit tests * Replaces c8b4a289364966432b58104e975d37cda1fefb84 * Use Object() casting to detect objects instead of .constructor (or instanceof). Both .constructor and instanceof compare by reference the type "Object" which means if the object comes from another window (where there is a different "Object" and "Object.prototype") it will drop out of the system and go freewack. Theory: If a variable casted to an object returns true when strictly compared to the original, the input must be an object. Which is true. It doesn't change the inheritance, it doesn't make it inherit from this window's Object if the object is from another window's object. All it does is cast to an object if not an object already. So e.g. "Object(5) !== 5" because 5 is a primitive value as opposed to an instance of Number. And contrary to "typeof", it doesn't return true for "null". * .constructor also has the problem that it only works this way if the input is a plain object. e.g. a simple construtor function that creates an object also get in the wrong side of the if/else case since it is an instance of Object, but not directly (rather indirectly via another constructor). * Added unit tests for basic getHash usage, as well as regression tests against the above two mentioned problems (these tests fail before this commit). * While at it, also improved other utilities a bit. - Use hasOwnProperty instead of casting to boolean when checking for presence of native support. Thanks to Douglas Crockford for that tip. - Fix documentation for ve.getHash: Parameter is not named "obj". - Add Object-check to ve.getObjectKeys per ES5 Object.keys spec (to match native behavior) - Add Object-check to ve.getObjectValues to match ve.getObjectKeys - Improved performance of ve.getObjectKeys shim. Tried several potential optimizations and compared with jsperf. Using a "static" reference to hasOwn improves performance (by not having to look it up 4 scopes up and 3 property levels deep). Also using [.length] instead of .push() shared off a few ms. - Added unit tests for ve.getObjectValues Change-Id: If24d09405321f201c67f7df75d332bb1171c8a36
2012-08-12 18:27:31 +00:00
obj.f = obj;
assert.throws( function () {
ve.getHash( obj );
}, 'Throw exceptions for objects with cirular refences ' );
*/
Refactor ve.getHash: Stabilize cross-browser differences; + unit tests * Replaces c8b4a289364966432b58104e975d37cda1fefb84 * Use Object() casting to detect objects instead of .constructor (or instanceof). Both .constructor and instanceof compare by reference the type "Object" which means if the object comes from another window (where there is a different "Object" and "Object.prototype") it will drop out of the system and go freewack. Theory: If a variable casted to an object returns true when strictly compared to the original, the input must be an object. Which is true. It doesn't change the inheritance, it doesn't make it inherit from this window's Object if the object is from another window's object. All it does is cast to an object if not an object already. So e.g. "Object(5) !== 5" because 5 is a primitive value as opposed to an instance of Number. And contrary to "typeof", it doesn't return true for "null". * .constructor also has the problem that it only works this way if the input is a plain object. e.g. a simple construtor function that creates an object also get in the wrong side of the if/else case since it is an instance of Object, but not directly (rather indirectly via another constructor). * Added unit tests for basic getHash usage, as well as regression tests against the above two mentioned problems (these tests fail before this commit). * While at it, also improved other utilities a bit. - Use hasOwnProperty instead of casting to boolean when checking for presence of native support. Thanks to Douglas Crockford for that tip. - Fix documentation for ve.getHash: Parameter is not named "obj". - Add Object-check to ve.getObjectKeys per ES5 Object.keys spec (to match native behavior) - Add Object-check to ve.getObjectValues to match ve.getObjectKeys - Improved performance of ve.getObjectKeys shim. Tried several potential optimizations and compared with jsperf. Using a "static" reference to hasOwn improves performance (by not having to look it up 4 scopes up and 3 property levels deep). Also using [.length] instead of .push() shared off a few ms. - Added unit tests for ve.getObjectValues Change-Id: If24d09405321f201c67f7df75d332bb1171c8a36
2012-08-12 18:27:31 +00:00
function Foo() {
this.a = 1;
this.c = 3;
this.b = 2;
}
hash = '{"a":1,"b":2,"c":3}';
assert.equal(
ve.getHash( new Foo() ),
hash,
// This was previously broken when we used .constructor === Object
// ve.getHash.keySortReplacer, because although instances of Foo
// do inherit from Object (( new Foo() ) instanceof Object === true),
// direct comparison would return false.
'Treat objects constructed by a function as well'
);
frame = document.createElement( 'frame' );
frame.src = 'about:blank';
$( '#qunit-fixture' ).append( frame );
obj = new frame.contentWindow.Object();
obj.c = 3;
obj.b = 2;
obj.a = 1;
assert.equal(
ve.getHash( obj ),
hash,
// This was previously broken when we used comparison with "Object" in
// ve.getHash.keySortReplacer, because they are an instance of the other
// window's "Object".
'Treat objects constructed by a another window as well'
);
} );
QUnit.test( 'getDomAttributes', 1, function ( assert ) {
assert.deepEqual(
ve.getDomAttributes( $( '<div foo="bar" baz quux=3></div>' ).get( 0 ) ),
{ 'foo': 'bar', 'baz': '', 'quux': '3' },
'getDomAttributes() returns object with correct attributes'
);
} );
QUnit.test( 'setDomAttributes', 3, function ( assert ) {
var element = document.createElement( 'div' );
ve.setDomAttributes( element, { 'foo': 'bar', 'baz': '', 'quux': 3 } );
assert.deepEqual(
ve.getDomAttributes( element ),
{ 'foo': 'bar', 'baz': '', 'quux': '3' },
'setDomAttributes() sets attributes correctly'
);
ve.setDomAttributes( element, { 'foo': null, 'bar': 1, 'baz': undefined, 'quux': 5, 'whee': 'yay' } );
assert.deepEqual(
ve.getDomAttributes( element ),
{ 'bar': '1', 'quux': '5', 'whee': 'yay' },
'setDomAttributes() overwrites attributes, removes attributes, and sets new attributes'
);
ve.setDomAttributes( element, { 'onclick': 'alert(1);' }, ['foo', 'bar', 'baz', 'quux', 'whee'] );
assert.ok( !element.hasAttribute( 'onclick' ), 'event attributes are blocked when sanitizing' );
} );
QUnit.test( 'getOpeningHtmlTag', 5, function ( assert ) {
assert.deepEqual(
ve.getOpeningHtmlTag( 'code', {} ),
'<code>',
'opening tag without attributes'
);
assert.deepEqual(
ve.getOpeningHtmlTag( 'img', { 'src': 'foo' } ),
'<img src="foo">',
'opening tag with one attribute'
);
assert.deepEqual(
ve.getOpeningHtmlTag( 'a', { 'href': 'foo', 'rel': 'bar' } ),
'<a href="foo" rel="bar">',
'tag with two attributes'
);
assert.deepEqual(
ve.getOpeningHtmlTag( 'option', { 'selected': true, 'blah': false, 'value': 3 } ),
'<option selected="selected" value="3">',
'handling of booleans and numbers'
);
assert.deepEqual(
ve.getOpeningHtmlTag( 'input', { 'placeholder': '<foo>&"bar"&\'baz\'' } ),
'<input placeholder="&lt;foo&gt;&amp;&quot;bar&quot;&amp;&#039;baz&#039;">',
'escaping of attribute values'
);
} );
( function () {
var plainObj, funcObj, arrObj;
plainObj = {
'foo': 3,
'bar': {
'baz': null,
'quux': {
'whee': 'yay'
}
}
};
funcObj = function abc( d ) { return d; };
funcObj.foo = 3;
funcObj.bar = {
'baz': null,
'quux': {
'whee': 'yay'
}
};
arrObj = ['a', 'b', 'c'];
arrObj.foo = 3;
arrObj.bar = {
'baz': null,
'quux': {
'whee': 'yay'
}
};
$.each( {
'Object': plainObj,
'Function': funcObj,
'Array': arrObj
}, function ( type, obj ) {
QUnit.test( 'getProp( ' + type + ' )', 9, function ( assert ) {
assert.deepEqual(
ve.getProp( obj, 'foo' ),
3,
'single key'
);
assert.deepEqual(
ve.getProp( obj, 'bar' ),
{ 'baz': null, 'quux': { 'whee': 'yay' } },
'single key, returns object'
);
assert.deepEqual(
ve.getProp( obj, 'bar', 'baz' ),
null,
'two keys, returns null'
);
assert.deepEqual(
ve.getProp( obj, 'bar', 'quux', 'whee' ),
'yay',
'three keys'
);
assert.deepEqual(
ve.getProp( obj, 'x' ),
undefined,
'missing property returns undefined'
);
assert.deepEqual(
ve.getProp( obj, 'foo', 'bar' ),
undefined,
'missing 2nd-level property returns undefined'
);
assert.deepEqual(
ve.getProp( obj, 'foo', 'bar', 'baz', 'quux', 'whee' ),
undefined,
'multiple missing properties don\'t cause an error'
);
assert.deepEqual(
ve.getProp( obj, 'bar', 'baz', 'quux' ),
undefined,
'accessing property of null returns undefined, doesn\'t cause an error'
);
assert.deepEqual(
ve.getProp( obj, 'bar', 'baz', 'quux', 'whee', 'yay' ),
undefined,
'accessing multiple properties of null'
);
} );
QUnit.test( 'setProp( ' + type + ' )' , 7, function ( assert ) {
ve.setProp( obj, 'foo', 4 );
assert.deepEqual( 4, obj.foo, 'setting an existing key with depth 1' );
ve.setProp( obj, 'test', 'TEST' );
assert.deepEqual( 'TEST', obj.test, 'setting a new key with depth 1' );
ve.setProp( obj, 'bar', 'quux', 'whee', 'YAY' );
assert.deepEqual( 'YAY', obj.bar.quux.whee, 'setting an existing key with depth 3' );
ve.setProp( obj, 'bar', 'a', 'b', 'c' );
assert.deepEqual( 'c', obj.bar.a.b, 'setting two new keys within an existing key' );
ve.setProp( obj, 'a', 'b', 'c', 'd', 'e', 'f' );
assert.deepEqual( 'f', obj.a.b.c.d.e, 'setting new keys with depth 5' );
ve.setProp( obj, 'bar', 'baz', 'whee', 'wheee', 'wheeee' );
assert.deepEqual( null, obj.bar.baz, 'descending into null fails silently' );
ve.setProp( obj, 'foo', 'bar', 'baz', 5 );
assert.deepEqual( undefined, obj.foo.bar, 'descending into a non-object fails silently' );
} );
} );
}() );
QUnit.test( 'batchSplice', 8, function ( assert ) {
var actual = [ 'a', 'b', 'c', 'd', 'e' ], expected = actual.slice( 0 ), bigArr = [],
actualRet, expectedRet, i;
actualRet = ve.batchSplice( actual, 1, 1, [] );
expectedRet = expected.splice( 1, 1 );
assert.deepEqual( expectedRet, actualRet, 'removing 1 element (return value)' );
assert.deepEqual( expected, actual, 'removing 1 element (array)' );
actualRet = ve.batchSplice( actual, 3, 2, [ 'w', 'x', 'y', 'z' ] );
expectedRet = expected.splice( 3, 2, 'w', 'x', 'y', 'z' );
assert.deepEqual( expectedRet, actualRet, 'replacing 2 elements with 4 elements (return value)' );
assert.deepEqual( expected, actual, 'replacing 2 elements with 4 elements (array)' );
actualRet = ve.batchSplice( actual, 0, 0, [ 'f', 'o', 'o' ] );
expectedRet = expected.splice( 0, 0, 'f', 'o', 'o' );
assert.deepEqual( expectedRet, actualRet, 'inserting 3 elements (return value)' );
assert.deepEqual( expected, actual, 'inserting 3 elements (array)' );
for ( i = 0; i < 2100; i++ ) {
bigArr[i] = i;
}
actualRet = ve.batchSplice( actual, 2, 3, bigArr );
expectedRet = expected.splice.apply( expected, [2, 3].concat( bigArr.slice( 0, 1050 ) ) );
expected.splice.apply( expected, [1052, 0].concat( bigArr.slice( 1050 ) ) );
assert.deepEqual( expectedRet, actualRet, 'replacing 3 elements with 2100 elements (return value)' );
assert.deepEqual( expected, actual, 'replacing 3 elements with 2100 elements (array)' );
} );
QUnit.test( 'createDocumentFromHtml', function ( assert ) {
var key, doc, expectedHead, expectedBody,
cases = [
{
'msg': 'simple document with doctype, head and body',
'html': '<!doctype html><html><head><title>Foo</title></head><body><p>Bar</p></body></html>',
'head': '<title>Foo</title>',
'body': '<p>Bar</p>'
},
{
'msg': 'simple document without doctype',
'html': '<html><head><title>Foo</title></head><body><p>Bar</p></body></html>',
'head': '<title>Foo</title>',
'body': '<p>Bar</p>'
},
{
'msg': 'document with missing closing tags and missing <html> tag',
'html': '<!doctype html><head><title>Foo</title><base href="yay"><body><p>Bar<b>Baz',
'head': '<title>Foo</title><base href="yay" />',
'body': '<p>Bar<b>Baz</b></p>'
},
{
'msg': 'empty string results in empty document',
'html': '',
'head': '',
'body': ''
}
];
QUnit.expect( cases.length*2 );
for ( key in cases ) {
doc = ve.createDocumentFromHtml( cases[key].html );
expectedHead = $( '<head>' ).html( cases[key].head ).get( 0 );
expectedBody = $( '<body>' ).html( cases[key].body ).get( 0 );
assert.equalDomElement( $( 'head', doc ).get( 0 ), expectedHead, cases[key].msg + ' (head)' );
assert.equalDomElement( $( 'body', doc ).get( 0 ), expectedBody, cases[key].msg + ' (body)' );
}
} );
// ve.splitClusters: Tested upstream (UnicodeJS)
// TODO: ve.isUnattachedCombiningMark
// TODO: ve.getByteOffset
// TODO: ve.getCharacterOffset
QUnit.test( 'graphemeSafeSubstring', function ( assert ) {
var i, text = '12\ud860\udee245\ud860\udee2789\ud860\udee2bc', cases = [
{
'msg': 'start and end inside multibyte',
'start': 3,
'end': 12,
'expected': [ '\ud860\udee245\ud860\udee2789\ud860\udee2', '45\ud860\udee2789' ]
},
{
'msg': 'start and end next to multibyte',
'start': 4,
'end': 11,
'expected': [ '45\ud860\udee2789', '45\ud860\udee2789' ]
},
{
'msg': 'complete string',
'start': 0,
'end': text.length,
'expected': [ text, text ]
},
{
'msg': 'collapsed selection inside multibyte',
'start': 3,
'end': 3,
'expected': [ '\ud860\udee2', '' ]
}
];
QUnit.expect( cases.length * 2 );
for ( i = 0; i < cases.length; i++ ) {
assert.equal(
ve.graphemeSafeSubstring( text, cases[i].start, cases[i].end, true ),
cases[i].expected[0],
cases[i].msg + ' (outer)'
);
assert.equal(
ve.graphemeSafeSubstring( text, cases[i].start, cases[i].end, false ),
cases[i].expected[1],
cases[i].msg + ' (inner)'
);
}
} );