├── License.txt ├── README.md ├── late.js ├── monotype.js └── test ├── editor.html ├── editor.js ├── expect.js ├── mocha.css ├── mocha.html ├── mocha.js ├── skin.js └── test.js /License.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright 2014 Mark Nadal and other contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Monotype 2 | ======== 3 | 4 | Monotype so that way your font doesn't have to monospace! 5 | 6 | Monotype is an excellent library that allows you to perform arbitrary formatting or filtering on your user's contenteditable input while they type. 7 | 8 | #Problem 9 | 10 | Existing DOM Range libraries, which handle caret position and selections, cannot restore themselves properly if the document tree is modified. Therefore one cannot alter the HTML without abruptly interrupting the user and losing their focus. This is because a range is bound to a node, thus if it is removed the range is destroyed. Monotype takes an exciting new approach, where ranges are restored based on the input's content rather than a tree. A whole new world of opportunities is now possible, which previously was implausible. 11 | 12 | If you have not already been aggravated by this problem, I'm surprised you have even read this far - it is the sort of thing that seems like it would be a 'given'. Let's dive into some explanations, or if you are familiar with it - just skip to the demos. 13 | 14 | ###Examples 15 | 16 | 1. Facebook inline friend-tagging. As a developer you may think it is just as easy as highlighting the name and wrapping a stylized span tag around it. Right? Nope. First off, the entry field is a textarea which doesn't allow any formatting, just raw text. Second, even if it did, the insertion of an element behind the caret would change the caret's offset in the parent node, which would force you to restore it manually. 17 | So how does Facebook do it? This will sound ugly, because it is. They make the textarea have an invisible background, then they layer a container behind it that replicates the textarea. This container can then be formatted, such that the necessary stylizing can be achieved. 18 | 19 | 2. Browser based IDEs and code editors. You may have noticed that your favorite online editors require you to use monospace fonts. But have you ever thought why that is the case? It is not to enhance the nerdy superpower aesthetic, but once again because formatting text during user input is tricky. They go a few nasty steps further, because we all want nicely color printed syntax. However, this is not possible with Facebook's technique because the textarea on top has a solid font color, which would block the coloring beneath. 20 | As a result we have to make the entire textarea invisible, not just the background. We still need it on top though, to catch click focus (or rig up a pass-through system) - but poses the user a problem, what are they clicking on? The user can no longer see their caret in the textarea because the textarea is hidden! 21 | To solve this we need to emulate a caret below, but matching caret position is the nasty part given the imprecision of character widths in fonts. If though, a font existed that had the exact same width for every character, then matching the position up would be trivial. Just count how many characters there are on a given line and multiply by a pixel width to get the absolute position of the caret aligned with the text. 22 | 23 | Phew! Or we could just use Monotype. 24 | 25 | #Solution 26 | 27 | Introducing Monotype, unlimited formatting for free. But that is not all, Monotype also allows you to sanitize user input on the fly! Hopefully though, you do not need an exposé on why [unfiltered user input](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) is a bad idea. That digression shall not happen here. Onwards to the demos! 28 | 29 | Monotype is not designed or intended to be a full fledged editor, it instead lays a reliable foundation for you to build an editor on top without worrying about caret trickery. Use it in place of your existing Range library or native code. 30 | 31 | #Demos 32 | [Examples](http://db.marknadal.com/monotype/test/mocha.html) ([45s video](http://www.screenr.com/Al7N)) 33 | 34 | [Syntax code editor](http://db.marknadal.com/monotype/test/editor.html) 35 | 36 | #API 37 | When you want to save the current range selection or caret, which is typically right before any formatting or filtering, do: 38 | 39 | `r = monotype(editor)` 40 | 41 | Where editor is the jQuery element of your contenteditable container, r is whatever variable you hence forth want to call your saved selection, and monotype is the global given to you when you included the script tag. 42 | When you fancy you'd like to restore the user's selection, just: 43 | 44 | `r.restore()` 45 | 46 | Simple enough? Excellently so. Check out the code behind the demos if you need any further illustrations. 47 | 48 | #Support 49 | Monotype is a mean and lean **1.5KB** gzipped. Chrome, FF 3.6+, Safari 5.1+, some Opera/IE6+ and full IE9+. No dependencies other than the fantastic jQuery. However, it is doing minimal support of cross-browser DOM traversal and checking, so it hypothetically could be easy to replace and remove. There is a known problem of backtracking in certain browsers. 50 | 51 | 52 | Crafted with love by Mark Nadal, whom is not responsible for any liabilities from the use of this code. 53 | -------------------------------------------------------------------------------- /late.js: -------------------------------------------------------------------------------- 1 | monotype.late = function(r,opt){ 2 | var m = monotype(r,opt) 3 | , strhml = function(t){ 4 | return (t[0] === '<' && $(t).length) 5 | }, jqtxt = function(n){ 6 | return n.jquery?n[0]:(strhml(n))?$(n)[0]:document.createTextNode(n); 7 | } 8 | m.remove = function(n,R){ 9 | R = m.range(); 10 | R.deleteContents(); 11 | monotype.restore(R); 12 | m = monotype(m,opt); 13 | return m; 14 | } 15 | m.insert = function(n,R){ 16 | n = jqtxt(n); 17 | R = m.range(); 18 | R.deleteContents(); 19 | R.insertNode(n); 20 | R.selectNodeContents(n); 21 | monotype.restore(R); 22 | m = monotype(m,opt); 23 | return m; 24 | } 25 | m.wrap = function(n,R){ 26 | var jq; 27 | n = jqtxt(n); 28 | R = m.range(); 29 | if(monotype.text(R.startContainer) || monotype.text(R.endContainer)){ 30 | var b = R.cloneContents(); 31 | R.deleteContents(); 32 | jq = $(n); 33 | jq.html(b); 34 | jq = jq[0]; 35 | R.insertNode(jq); 36 | }else{ 37 | R.surroundContents(n); 38 | } 39 | R.selectNodeContents(jq||n); 40 | monotype.restore(R); 41 | m = monotype(m,opt); 42 | return m; 43 | } 44 | return m; 45 | } -------------------------------------------------------------------------------- /monotype.js: -------------------------------------------------------------------------------- 1 | ;var monotype = monotype || (function(monotype){ 2 | monotype.range = function(n){ 3 | var R, s, t, n = n || 0, win = monotype.win || window, doc = win.document; 4 | if(!arguments.length) return doc.createRange(); 5 | if(!(win.Range && R instanceof Range)){ 6 | s = win.getSelection? win.getSelection() : {}; 7 | if(s.rangeCount){ 8 | R = s.getRangeAt(n); 9 | } else { 10 | if(doc.createRange){ 11 | R = doc.createRange(); 12 | R.setStart(doc.body, 0); 13 | } else 14 | if (doc.selection){ // 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 26 | 27 | 28 | 29 |
30 | Font: 31 | Theme: 77 | Size: 78 |
79 |
// Syntax code editor in 20 LOC with fancy fonts!
 80 | $(function(){
 81 | 	var prevent = {
 82 | 		'13': '\n'
 83 | 		,'9': '\t'
 84 | 	}, write = $('[contenteditable=true]').keyup(function(e){
 85 | 		var r = monotype(write);
 86 | 		write.html(hljs.highlight('javascript', write.text()).value);
 87 | 		r.restore();
 88 | 	}).keydown(function(e){
 89 | 		if(e.keyCode in prevent){
 90 | 			e.preventDefault();
 91 | 			var r = monotype(write);
 92 | 			write.html(write.text().slice(0, r.s)
 93 | 				+ prevent[e.keyCode] +
 94 | 			write.text().slice(r.e));
 95 | 			r.e = ++r.s;
 96 | 			r.restore();
 97 | 		}
 98 | 	});
 99 | });
100 | /* Courtesy the excellent Monotype, Highlight.js, and jQuery libraries! */
101 | 102 | 103 | 104 | -------------------------------------------------------------------------------- /test/editor.js: -------------------------------------------------------------------------------- 1 | // Syntax code editor in 20 LOC with fancy fonts! 2 | $(function(){ 3 | var prevent = { 4 | '13': '\n' 5 | ,'9': '\t' 6 | }, write = $('[contenteditable=true]').keyup(function(e){ 7 | var r = monotype(write); 8 | write.html(hljs.highlight('javascript', write.text()).value); 9 | r.restore(); 10 | }).keydown(function(e){ 11 | if(e.keyCode in prevent){ 12 | e.preventDefault(); 13 | var r = monotype(write); 14 | write.html(write.text().slice(0, r.s) 15 | + prevent[e.keyCode] + 16 | write.text().slice(r.e)); 17 | r.e = ++r.s; 18 | r.restore(); 19 | } 20 | }); 21 | }); 22 | /* Courtesy the excellent Monotype, Highlight.js, and jQuery libraries! */ -------------------------------------------------------------------------------- /test/expect.js: -------------------------------------------------------------------------------- 1 | (function (global, module) { 2 | 3 | var exports = module.exports; 4 | 5 | /** 6 | * Exports. 7 | */ 8 | 9 | module.exports = expect; 10 | expect.Assertion = Assertion; 11 | 12 | /** 13 | * Exports version. 14 | */ 15 | 16 | expect.version = '0.1.2'; 17 | 18 | /** 19 | * Possible assertion flags. 20 | */ 21 | 22 | var flags = { 23 | not: ['to', 'be', 'have', 'include', 'only'] 24 | , to: ['be', 'have', 'include', 'only', 'not'] 25 | , only: ['have'] 26 | , have: ['own'] 27 | , be: ['an'] 28 | }; 29 | 30 | function expect (obj) { 31 | return new Assertion(obj); 32 | } 33 | 34 | /** 35 | * Constructor 36 | * 37 | * @api private 38 | */ 39 | 40 | function Assertion (obj, flag, parent) { 41 | this.obj = obj; 42 | this.flags = {}; 43 | 44 | if (undefined != parent) { 45 | this.flags[flag] = true; 46 | 47 | for (var i in parent.flags) { 48 | if (parent.flags.hasOwnProperty(i)) { 49 | this.flags[i] = true; 50 | } 51 | } 52 | } 53 | 54 | var $flags = flag ? flags[flag] : keys(flags) 55 | , self = this; 56 | 57 | if ($flags) { 58 | for (var i = 0, l = $flags.length; i < l; i++) { 59 | // avoid recursion 60 | if (this.flags[$flags[i]]) continue; 61 | 62 | var name = $flags[i] 63 | , assertion = new Assertion(this.obj, name, this) 64 | 65 | if ('function' == typeof Assertion.prototype[name]) { 66 | // clone the function, make sure we dont touch the prot reference 67 | var old = this[name]; 68 | this[name] = function () { 69 | return old.apply(self, arguments); 70 | }; 71 | 72 | for (var fn in Assertion.prototype) { 73 | if (Assertion.prototype.hasOwnProperty(fn) && fn != name) { 74 | this[name][fn] = bind(assertion[fn], assertion); 75 | } 76 | } 77 | } else { 78 | this[name] = assertion; 79 | } 80 | } 81 | } 82 | } 83 | 84 | /** 85 | * Performs an assertion 86 | * 87 | * @api private 88 | */ 89 | 90 | Assertion.prototype.assert = function (truth, msg, error, expected) { 91 | var msg = this.flags.not ? error : msg 92 | , ok = this.flags.not ? !truth : truth 93 | , err; 94 | 95 | if (!ok) { 96 | err = new Error(msg.call(this)); 97 | if (arguments.length > 3) { 98 | err.actual = this.obj; 99 | err.expected = expected; 100 | err.showDiff = true; 101 | } 102 | throw err; 103 | } 104 | 105 | this.and = new Assertion(this.obj); 106 | }; 107 | 108 | /** 109 | * Check if the value is truthy 110 | * 111 | * @api public 112 | */ 113 | 114 | Assertion.prototype.ok = function () { 115 | this.assert( 116 | !!this.obj 117 | , function(){ return 'expected ' + i(this.obj) + ' to be truthy' } 118 | , function(){ return 'expected ' + i(this.obj) + ' to be falsy' }); 119 | }; 120 | 121 | /** 122 | * Creates an anonymous function which calls fn with arguments. 123 | * 124 | * @api public 125 | */ 126 | 127 | Assertion.prototype.withArgs = function() { 128 | expect(this.obj).to.be.a('function'); 129 | var fn = this.obj; 130 | var args = Array.prototype.slice.call(arguments); 131 | return expect(function() { fn.apply(null, args); }); 132 | }; 133 | 134 | /** 135 | * Assert that the function throws. 136 | * 137 | * @param {Function|RegExp} callback, or regexp to match error string against 138 | * @api public 139 | */ 140 | 141 | Assertion.prototype.throwError = 142 | Assertion.prototype.throwException = function (fn) { 143 | expect(this.obj).to.be.a('function'); 144 | 145 | var thrown = false 146 | , not = this.flags.not; 147 | 148 | try { 149 | this.obj(); 150 | } catch (e) { 151 | if (isRegExp(fn)) { 152 | var subject = 'string' == typeof e ? e : e.message; 153 | if (not) { 154 | expect(subject).to.not.match(fn); 155 | } else { 156 | expect(subject).to.match(fn); 157 | } 158 | } else if ('function' == typeof fn) { 159 | fn(e); 160 | } 161 | thrown = true; 162 | } 163 | 164 | if (isRegExp(fn) && not) { 165 | // in the presence of a matcher, ensure the `not` only applies to 166 | // the matching. 167 | this.flags.not = false; 168 | } 169 | 170 | var name = this.obj.name || 'fn'; 171 | this.assert( 172 | thrown 173 | , function(){ return 'expected ' + name + ' to throw an exception' } 174 | , function(){ return 'expected ' + name + ' not to throw an exception' }); 175 | }; 176 | 177 | /** 178 | * Checks if the array is empty. 179 | * 180 | * @api public 181 | */ 182 | 183 | Assertion.prototype.empty = function () { 184 | var expectation; 185 | 186 | if ('object' == typeof this.obj && null !== this.obj && !isArray(this.obj)) { 187 | if ('number' == typeof this.obj.length) { 188 | expectation = !this.obj.length; 189 | } else { 190 | expectation = !keys(this.obj).length; 191 | } 192 | } else { 193 | if ('string' != typeof this.obj) { 194 | expect(this.obj).to.be.an('object'); 195 | } 196 | 197 | expect(this.obj).to.have.property('length'); 198 | expectation = !this.obj.length; 199 | } 200 | 201 | this.assert( 202 | expectation 203 | , function(){ return 'expected ' + i(this.obj) + ' to be empty' } 204 | , function(){ return 'expected ' + i(this.obj) + ' to not be empty' }); 205 | return this; 206 | }; 207 | 208 | /** 209 | * Checks if the obj exactly equals another. 210 | * 211 | * @api public 212 | */ 213 | 214 | Assertion.prototype.be = 215 | Assertion.prototype.equal = function (obj) { 216 | this.assert( 217 | obj === this.obj 218 | , function(){ return 'expected ' + i(this.obj) + ' to equal ' + i(obj) } 219 | , function(){ return 'expected ' + i(this.obj) + ' to not equal ' + i(obj) }); 220 | return this; 221 | }; 222 | 223 | /** 224 | * Checks if the obj sortof equals another. 225 | * 226 | * @api public 227 | */ 228 | 229 | Assertion.prototype.eql = function (obj) { 230 | this.assert( 231 | expect.eql(this.obj, obj) 232 | , function(){ return 'expected ' + i(this.obj) + ' to sort of equal ' + i(obj) } 233 | , function(){ return 'expected ' + i(this.obj) + ' to sort of not equal ' + i(obj) } 234 | , obj); 235 | return this; 236 | }; 237 | 238 | /** 239 | * Assert within start to finish (inclusive). 240 | * 241 | * @param {Number} start 242 | * @param {Number} finish 243 | * @api public 244 | */ 245 | 246 | Assertion.prototype.within = function (start, finish) { 247 | var range = start + '..' + finish; 248 | this.assert( 249 | this.obj >= start && this.obj <= finish 250 | , function(){ return 'expected ' + i(this.obj) + ' to be within ' + range } 251 | , function(){ return 'expected ' + i(this.obj) + ' to not be within ' + range }); 252 | return this; 253 | }; 254 | 255 | /** 256 | * Assert typeof / instance of 257 | * 258 | * @api public 259 | */ 260 | 261 | Assertion.prototype.a = 262 | Assertion.prototype.an = function (type) { 263 | if ('string' == typeof type) { 264 | // proper english in error msg 265 | var n = /^[aeiou]/.test(type) ? 'n' : ''; 266 | 267 | // typeof with support for 'array' 268 | this.assert( 269 | 'array' == type ? isArray(this.obj) : 270 | 'regexp' == type ? isRegExp(this.obj) : 271 | 'object' == type 272 | ? 'object' == typeof this.obj && null !== this.obj 273 | : type == typeof this.obj 274 | , function(){ return 'expected ' + i(this.obj) + ' to be a' + n + ' ' + type } 275 | , function(){ return 'expected ' + i(this.obj) + ' not to be a' + n + ' ' + type }); 276 | } else { 277 | // instanceof 278 | var name = type.name || 'supplied constructor'; 279 | this.assert( 280 | this.obj instanceof type 281 | , function(){ return 'expected ' + i(this.obj) + ' to be an instance of ' + name } 282 | , function(){ return 'expected ' + i(this.obj) + ' not to be an instance of ' + name }); 283 | } 284 | 285 | return this; 286 | }; 287 | 288 | /** 289 | * Assert numeric value above _n_. 290 | * 291 | * @param {Number} n 292 | * @api public 293 | */ 294 | 295 | Assertion.prototype.greaterThan = 296 | Assertion.prototype.above = function (n) { 297 | this.assert( 298 | this.obj > n 299 | , function(){ return 'expected ' + i(this.obj) + ' to be above ' + n } 300 | , function(){ return 'expected ' + i(this.obj) + ' to be below ' + n }); 301 | return this; 302 | }; 303 | 304 | /** 305 | * Assert numeric value below _n_. 306 | * 307 | * @param {Number} n 308 | * @api public 309 | */ 310 | 311 | Assertion.prototype.lessThan = 312 | Assertion.prototype.below = function (n) { 313 | this.assert( 314 | this.obj < n 315 | , function(){ return 'expected ' + i(this.obj) + ' to be below ' + n } 316 | , function(){ return 'expected ' + i(this.obj) + ' to be above ' + n }); 317 | return this; 318 | }; 319 | 320 | /** 321 | * Assert string value matches _regexp_. 322 | * 323 | * @param {RegExp} regexp 324 | * @api public 325 | */ 326 | 327 | Assertion.prototype.match = function (regexp) { 328 | this.assert( 329 | regexp.exec(this.obj) 330 | , function(){ return 'expected ' + i(this.obj) + ' to match ' + regexp } 331 | , function(){ return 'expected ' + i(this.obj) + ' not to match ' + regexp }); 332 | return this; 333 | }; 334 | 335 | /** 336 | * Assert property "length" exists and has value of _n_. 337 | * 338 | * @param {Number} n 339 | * @api public 340 | */ 341 | 342 | Assertion.prototype.length = function (n) { 343 | expect(this.obj).to.have.property('length'); 344 | var len = this.obj.length; 345 | this.assert( 346 | n == len 347 | , function(){ return 'expected ' + i(this.obj) + ' to have a length of ' + n + ' but got ' + len } 348 | , function(){ return 'expected ' + i(this.obj) + ' to not have a length of ' + len }); 349 | return this; 350 | }; 351 | 352 | /** 353 | * Assert property _name_ exists, with optional _val_. 354 | * 355 | * @param {String} name 356 | * @param {Mixed} val 357 | * @api public 358 | */ 359 | 360 | Assertion.prototype.property = function (name, val) { 361 | if (this.flags.own) { 362 | this.assert( 363 | Object.prototype.hasOwnProperty.call(this.obj, name) 364 | , function(){ return 'expected ' + i(this.obj) + ' to have own property ' + i(name) } 365 | , function(){ return 'expected ' + i(this.obj) + ' to not have own property ' + i(name) }); 366 | return this; 367 | } 368 | 369 | if (this.flags.not && undefined !== val) { 370 | if (undefined === this.obj[name]) { 371 | throw new Error(i(this.obj) + ' has no property ' + i(name)); 372 | } 373 | } else { 374 | var hasProp; 375 | try { 376 | hasProp = name in this.obj 377 | } catch (e) { 378 | hasProp = undefined !== this.obj[name] 379 | } 380 | 381 | this.assert( 382 | hasProp 383 | , function(){ return 'expected ' + i(this.obj) + ' to have a property ' + i(name) } 384 | , function(){ return 'expected ' + i(this.obj) + ' to not have a property ' + i(name) }); 385 | } 386 | 387 | if (undefined !== val) { 388 | this.assert( 389 | val === this.obj[name] 390 | , function(){ return 'expected ' + i(this.obj) + ' to have a property ' + i(name) 391 | + ' of ' + i(val) + ', but got ' + i(this.obj[name]) } 392 | , function(){ return 'expected ' + i(this.obj) + ' to not have a property ' + i(name) 393 | + ' of ' + i(val) }); 394 | } 395 | 396 | this.obj = this.obj[name]; 397 | return this; 398 | }; 399 | 400 | /** 401 | * Assert that the array contains _obj_ or string contains _obj_. 402 | * 403 | * @param {Mixed} obj|string 404 | * @api public 405 | */ 406 | 407 | Assertion.prototype.string = 408 | Assertion.prototype.contain = function (obj) { 409 | if ('string' == typeof this.obj) { 410 | this.assert( 411 | ~this.obj.indexOf(obj) 412 | , function(){ return 'expected ' + i(this.obj) + ' to contain ' + i(obj) } 413 | , function(){ return 'expected ' + i(this.obj) + ' to not contain ' + i(obj) }); 414 | } else { 415 | this.assert( 416 | ~indexOf(this.obj, obj) 417 | , function(){ return 'expected ' + i(this.obj) + ' to contain ' + i(obj) } 418 | , function(){ return 'expected ' + i(this.obj) + ' to not contain ' + i(obj) }); 419 | } 420 | return this; 421 | }; 422 | 423 | /** 424 | * Assert exact keys or inclusion of keys by using 425 | * the `.own` modifier. 426 | * 427 | * @param {Array|String ...} keys 428 | * @api public 429 | */ 430 | 431 | Assertion.prototype.key = 432 | Assertion.prototype.keys = function ($keys) { 433 | var str 434 | , ok = true; 435 | 436 | $keys = isArray($keys) 437 | ? $keys 438 | : Array.prototype.slice.call(arguments); 439 | 440 | if (!$keys.length) throw new Error('keys required'); 441 | 442 | var actual = keys(this.obj) 443 | , len = $keys.length; 444 | 445 | // Inclusion 446 | ok = every($keys, function (key) { 447 | return ~indexOf(actual, key); 448 | }); 449 | 450 | // Strict 451 | if (!this.flags.not && this.flags.only) { 452 | ok = ok && $keys.length == actual.length; 453 | } 454 | 455 | // Key string 456 | if (len > 1) { 457 | $keys = map($keys, function (key) { 458 | return i(key); 459 | }); 460 | var last = $keys.pop(); 461 | str = $keys.join(', ') + ', and ' + last; 462 | } else { 463 | str = i($keys[0]); 464 | } 465 | 466 | // Form 467 | str = (len > 1 ? 'keys ' : 'key ') + str; 468 | 469 | // Have / include 470 | str = (!this.flags.only ? 'include ' : 'only have ') + str; 471 | 472 | // Assertion 473 | this.assert( 474 | ok 475 | , function(){ return 'expected ' + i(this.obj) + ' to ' + str } 476 | , function(){ return 'expected ' + i(this.obj) + ' to not ' + str }); 477 | 478 | return this; 479 | }; 480 | 481 | /** 482 | * Assert a failure. 483 | * 484 | * @param {String ...} custom message 485 | * @api public 486 | */ 487 | Assertion.prototype.fail = function (msg) { 488 | var error = function() { return msg || "explicit failure"; } 489 | this.assert(false, error, error); 490 | return this; 491 | }; 492 | 493 | /** 494 | * Function bind implementation. 495 | */ 496 | 497 | function bind (fn, scope) { 498 | return function () { 499 | return fn.apply(scope, arguments); 500 | } 501 | } 502 | 503 | /** 504 | * Array every compatibility 505 | * 506 | * @see bit.ly/5Fq1N2 507 | * @api public 508 | */ 509 | 510 | function every (arr, fn, thisObj) { 511 | var scope = thisObj || global; 512 | for (var i = 0, j = arr.length; i < j; ++i) { 513 | if (!fn.call(scope, arr[i], i, arr)) { 514 | return false; 515 | } 516 | } 517 | return true; 518 | } 519 | 520 | /** 521 | * Array indexOf compatibility. 522 | * 523 | * @see bit.ly/a5Dxa2 524 | * @api public 525 | */ 526 | 527 | function indexOf (arr, o, i) { 528 | if (Array.prototype.indexOf) { 529 | return Array.prototype.indexOf.call(arr, o, i); 530 | } 531 | 532 | if (arr.length === undefined) { 533 | return -1; 534 | } 535 | 536 | for (var j = arr.length, i = i < 0 ? i + j < 0 ? 0 : i + j : i || 0 537 | ; i < j && arr[i] !== o; i++); 538 | 539 | return j <= i ? -1 : i; 540 | } 541 | 542 | // https://gist.github.com/1044128/ 543 | var getOuterHTML = function(element) { 544 | if ('outerHTML' in element) return element.outerHTML; 545 | var ns = "http://www.w3.org/1999/xhtml"; 546 | var container = document.createElementNS(ns, '_'); 547 | var xmlSerializer = new XMLSerializer(); 548 | var html; 549 | if (document.xmlVersion) { 550 | return xmlSerializer.serializeToString(element); 551 | } else { 552 | container.appendChild(element.cloneNode(false)); 553 | html = container.innerHTML.replace('><', '>' + element.innerHTML + '<'); 554 | container.innerHTML = ''; 555 | return html; 556 | } 557 | }; 558 | 559 | // Returns true if object is a DOM element. 560 | var isDOMElement = function (object) { 561 | if (typeof HTMLElement === 'object') { 562 | return object instanceof HTMLElement; 563 | } else { 564 | return object && 565 | typeof object === 'object' && 566 | object.nodeType === 1 && 567 | typeof object.nodeName === 'string'; 568 | } 569 | }; 570 | 571 | /** 572 | * Inspects an object. 573 | * 574 | * @see taken from node.js `util` module (copyright Joyent, MIT license) 575 | * @api private 576 | */ 577 | 578 | function i (obj, showHidden, depth) { 579 | var seen = []; 580 | 581 | function stylize (str) { 582 | return str; 583 | } 584 | 585 | function format (value, recurseTimes) { 586 | // Provide a hook for user-specified inspect functions. 587 | // Check that value is an object with an inspect function on it 588 | if (value && typeof value.inspect === 'function' && 589 | // Filter out the util module, it's inspect function is special 590 | value !== exports && 591 | // Also filter out any prototype objects using the circular check. 592 | !(value.constructor && value.constructor.prototype === value)) { 593 | return value.inspect(recurseTimes); 594 | } 595 | 596 | // Primitive types cannot have properties 597 | switch (typeof value) { 598 | case 'undefined': 599 | return stylize('undefined', 'undefined'); 600 | 601 | case 'string': 602 | var simple = '\'' + json.stringify(value).replace(/^"|"$/g, '') 603 | .replace(/'/g, "\\'") 604 | .replace(/\\"/g, '"') + '\''; 605 | return stylize(simple, 'string'); 606 | 607 | case 'number': 608 | return stylize('' + value, 'number'); 609 | 610 | case 'boolean': 611 | return stylize('' + value, 'boolean'); 612 | } 613 | // For some reason typeof null is "object", so special case here. 614 | if (value === null) { 615 | return stylize('null', 'null'); 616 | } 617 | 618 | if (isDOMElement(value)) { 619 | return getOuterHTML(value); 620 | } 621 | 622 | // Look up the keys of the object. 623 | var visible_keys = keys(value); 624 | var $keys = showHidden ? Object.getOwnPropertyNames(value) : visible_keys; 625 | 626 | // Functions without properties can be shortcutted. 627 | if (typeof value === 'function' && $keys.length === 0) { 628 | if (isRegExp(value)) { 629 | return stylize('' + value, 'regexp'); 630 | } else { 631 | var name = value.name ? ': ' + value.name : ''; 632 | return stylize('[Function' + name + ']', 'special'); 633 | } 634 | } 635 | 636 | // Dates without properties can be shortcutted 637 | if (isDate(value) && $keys.length === 0) { 638 | return stylize(value.toUTCString(), 'date'); 639 | } 640 | 641 | // Error objects can be shortcutted 642 | if (value instanceof Error) { 643 | return stylize("["+value.toString()+"]", 'Error'); 644 | } 645 | 646 | var base, type, braces; 647 | // Determine the object type 648 | if (isArray(value)) { 649 | type = 'Array'; 650 | braces = ['[', ']']; 651 | } else { 652 | type = 'Object'; 653 | braces = ['{', '}']; 654 | } 655 | 656 | // Make functions say that they are functions 657 | if (typeof value === 'function') { 658 | var n = value.name ? ': ' + value.name : ''; 659 | base = (isRegExp(value)) ? ' ' + value : ' [Function' + n + ']'; 660 | } else { 661 | base = ''; 662 | } 663 | 664 | // Make dates with properties first say the date 665 | if (isDate(value)) { 666 | base = ' ' + value.toUTCString(); 667 | } 668 | 669 | if ($keys.length === 0) { 670 | return braces[0] + base + braces[1]; 671 | } 672 | 673 | if (recurseTimes < 0) { 674 | if (isRegExp(value)) { 675 | return stylize('' + value, 'regexp'); 676 | } else { 677 | return stylize('[Object]', 'special'); 678 | } 679 | } 680 | 681 | seen.push(value); 682 | 683 | var output = map($keys, function (key) { 684 | var name, str; 685 | if (value.__lookupGetter__) { 686 | if (value.__lookupGetter__(key)) { 687 | if (value.__lookupSetter__(key)) { 688 | str = stylize('[Getter/Setter]', 'special'); 689 | } else { 690 | str = stylize('[Getter]', 'special'); 691 | } 692 | } else { 693 | if (value.__lookupSetter__(key)) { 694 | str = stylize('[Setter]', 'special'); 695 | } 696 | } 697 | } 698 | if (indexOf(visible_keys, key) < 0) { 699 | name = '[' + key + ']'; 700 | } 701 | if (!str) { 702 | if (indexOf(seen, value[key]) < 0) { 703 | if (recurseTimes === null) { 704 | str = format(value[key]); 705 | } else { 706 | str = format(value[key], recurseTimes - 1); 707 | } 708 | if (str.indexOf('\n') > -1) { 709 | if (isArray(value)) { 710 | str = map(str.split('\n'), function (line) { 711 | return ' ' + line; 712 | }).join('\n').substr(2); 713 | } else { 714 | str = '\n' + map(str.split('\n'), function (line) { 715 | return ' ' + line; 716 | }).join('\n'); 717 | } 718 | } 719 | } else { 720 | str = stylize('[Circular]', 'special'); 721 | } 722 | } 723 | if (typeof name === 'undefined') { 724 | if (type === 'Array' && key.match(/^\d+$/)) { 725 | return str; 726 | } 727 | name = json.stringify('' + key); 728 | if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) { 729 | name = name.substr(1, name.length - 2); 730 | name = stylize(name, 'name'); 731 | } else { 732 | name = name.replace(/'/g, "\\'") 733 | .replace(/\\"/g, '"') 734 | .replace(/(^"|"$)/g, "'"); 735 | name = stylize(name, 'string'); 736 | } 737 | } 738 | 739 | return name + ': ' + str; 740 | }); 741 | 742 | seen.pop(); 743 | 744 | var numLinesEst = 0; 745 | var length = reduce(output, function (prev, cur) { 746 | numLinesEst++; 747 | if (indexOf(cur, '\n') >= 0) numLinesEst++; 748 | return prev + cur.length + 1; 749 | }, 0); 750 | 751 | if (length > 50) { 752 | output = braces[0] + 753 | (base === '' ? '' : base + '\n ') + 754 | ' ' + 755 | output.join(',\n ') + 756 | ' ' + 757 | braces[1]; 758 | 759 | } else { 760 | output = braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1]; 761 | } 762 | 763 | return output; 764 | } 765 | return format(obj, (typeof depth === 'undefined' ? 2 : depth)); 766 | } 767 | 768 | expect.stringify = i; 769 | 770 | function isArray (ar) { 771 | return Object.prototype.toString.call(ar) === '[object Array]'; 772 | } 773 | 774 | function isRegExp(re) { 775 | var s; 776 | try { 777 | s = '' + re; 778 | } catch (e) { 779 | return false; 780 | } 781 | 782 | return re instanceof RegExp || // easy case 783 | // duck-type for context-switching evalcx case 784 | typeof(re) === 'function' && 785 | re.constructor.name === 'RegExp' && 786 | re.compile && 787 | re.test && 788 | re.exec && 789 | s.match(/^\/.*\/[gim]{0,3}$/); 790 | } 791 | 792 | function isDate(d) { 793 | return d instanceof Date; 794 | } 795 | 796 | function keys (obj) { 797 | if (Object.keys) { 798 | return Object.keys(obj); 799 | } 800 | 801 | var keys = []; 802 | 803 | for (var i in obj) { 804 | if (Object.prototype.hasOwnProperty.call(obj, i)) { 805 | keys.push(i); 806 | } 807 | } 808 | 809 | return keys; 810 | } 811 | 812 | function map (arr, mapper, that) { 813 | if (Array.prototype.map) { 814 | return Array.prototype.map.call(arr, mapper, that); 815 | } 816 | 817 | var other= new Array(arr.length); 818 | 819 | for (var i= 0, n = arr.length; i= 2) { 845 | var rv = arguments[1]; 846 | } else { 847 | do { 848 | if (i in this) { 849 | rv = this[i++]; 850 | break; 851 | } 852 | 853 | // if array contains no values, no initial value to return 854 | if (++i >= len) 855 | throw new TypeError(); 856 | } while (true); 857 | } 858 | 859 | for (; i < len; i++) { 860 | if (i in this) 861 | rv = fun.call(null, rv, this[i], i, this); 862 | } 863 | 864 | return rv; 865 | } 866 | 867 | /** 868 | * Asserts deep equality 869 | * 870 | * @see taken from node.js `assert` module (copyright Joyent, MIT license) 871 | * @api private 872 | */ 873 | 874 | expect.eql = function eql(actual, expected) { 875 | // 7.1. All identical values are equivalent, as determined by ===. 876 | if (actual === expected) { 877 | return true; 878 | } else if ('undefined' != typeof Buffer 879 | && Buffer.isBuffer(actual) && Buffer.isBuffer(expected)) { 880 | if (actual.length != expected.length) return false; 881 | 882 | for (var i = 0; i < actual.length; i++) { 883 | if (actual[i] !== expected[i]) return false; 884 | } 885 | 886 | return true; 887 | 888 | // 7.2. If the expected value is a Date object, the actual value is 889 | // equivalent if it is also a Date object that refers to the same time. 890 | } else if (actual instanceof Date && expected instanceof Date) { 891 | return actual.getTime() === expected.getTime(); 892 | 893 | // 7.3. Other pairs that do not both pass typeof value == "object", 894 | // equivalence is determined by ==. 895 | } else if (typeof actual != 'object' && typeof expected != 'object') { 896 | return actual == expected; 897 | // If both are regular expression use the special `regExpEquiv` method 898 | // to determine equivalence. 899 | } else if (isRegExp(actual) && isRegExp(expected)) { 900 | return regExpEquiv(actual, expected); 901 | // 7.4. For all other Object pairs, including Array objects, equivalence is 902 | // determined by having the same number of owned properties (as verified 903 | // with Object.prototype.hasOwnProperty.call), the same set of keys 904 | // (although not necessarily the same order), equivalent values for every 905 | // corresponding key, and an identical "prototype" property. Note: this 906 | // accounts for both named and indexed properties on Arrays. 907 | } else { 908 | return objEquiv(actual, expected); 909 | } 910 | }; 911 | 912 | function isUndefinedOrNull (value) { 913 | return value === null || value === undefined; 914 | } 915 | 916 | function isArguments (object) { 917 | return Object.prototype.toString.call(object) == '[object Arguments]'; 918 | } 919 | 920 | function regExpEquiv (a, b) { 921 | return a.source === b.source && a.global === b.global && 922 | a.ignoreCase === b.ignoreCase && a.multiline === b.multiline; 923 | } 924 | 925 | function objEquiv (a, b) { 926 | if (isUndefinedOrNull(a) || isUndefinedOrNull(b)) 927 | return false; 928 | // an identical "prototype" property. 929 | if (a.prototype !== b.prototype) return false; 930 | //~~~I've managed to break Object.keys through screwy arguments passing. 931 | // Converting to array solves the problem. 932 | if (isArguments(a)) { 933 | if (!isArguments(b)) { 934 | return false; 935 | } 936 | a = pSlice.call(a); 937 | b = pSlice.call(b); 938 | return expect.eql(a, b); 939 | } 940 | try{ 941 | var ka = keys(a), 942 | kb = keys(b), 943 | key, i; 944 | } catch (e) {//happens when one is a string literal and the other isn't 945 | return false; 946 | } 947 | // having the same number of owned properties (keys incorporates hasOwnProperty) 948 | if (ka.length != kb.length) 949 | return false; 950 | //the same set of keys (although not necessarily the same order), 951 | ka.sort(); 952 | kb.sort(); 953 | //~~~cheap key test 954 | for (i = ka.length - 1; i >= 0; i--) { 955 | if (ka[i] != kb[i]) 956 | return false; 957 | } 958 | //equivalent values for every corresponding key, and 959 | //~~~possibly expensive deep test 960 | for (i = ka.length - 1; i >= 0; i--) { 961 | key = ka[i]; 962 | if (!expect.eql(a[key], b[key])) 963 | return false; 964 | } 965 | return true; 966 | } 967 | 968 | var json = (function () { 969 | "use strict"; 970 | 971 | if ('object' == typeof JSON && JSON.parse && JSON.stringify) { 972 | return { 973 | parse: nativeJSON.parse 974 | , stringify: nativeJSON.stringify 975 | } 976 | } 977 | 978 | var JSON = {}; 979 | 980 | function f(n) { 981 | // Format integers to have at least two digits. 982 | return n < 10 ? '0' + n : n; 983 | } 984 | 985 | function date(d, key) { 986 | return isFinite(d.valueOf()) ? 987 | d.getUTCFullYear() + '-' + 988 | f(d.getUTCMonth() + 1) + '-' + 989 | f(d.getUTCDate()) + 'T' + 990 | f(d.getUTCHours()) + ':' + 991 | f(d.getUTCMinutes()) + ':' + 992 | f(d.getUTCSeconds()) + 'Z' : null; 993 | } 994 | 995 | var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, 996 | escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, 997 | gap, 998 | indent, 999 | meta = { // table of character substitutions 1000 | '\b': '\\b', 1001 | '\t': '\\t', 1002 | '\n': '\\n', 1003 | '\f': '\\f', 1004 | '\r': '\\r', 1005 | '"' : '\\"', 1006 | '\\': '\\\\' 1007 | }, 1008 | rep; 1009 | 1010 | 1011 | function quote(string) { 1012 | 1013 | // If the string contains no control characters, no quote characters, and no 1014 | // backslash characters, then we can safely slap some quotes around it. 1015 | // Otherwise we must also replace the offending characters with safe escape 1016 | // sequences. 1017 | 1018 | escapable.lastIndex = 0; 1019 | return escapable.test(string) ? '"' + string.replace(escapable, function (a) { 1020 | var c = meta[a]; 1021 | return typeof c === 'string' ? c : 1022 | '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); 1023 | }) + '"' : '"' + string + '"'; 1024 | } 1025 | 1026 | 1027 | function str(key, holder) { 1028 | 1029 | // Produce a string from holder[key]. 1030 | 1031 | var i, // The loop counter. 1032 | k, // The member key. 1033 | v, // The member value. 1034 | length, 1035 | mind = gap, 1036 | partial, 1037 | value = holder[key]; 1038 | 1039 | // If the value has a toJSON method, call it to obtain a replacement value. 1040 | 1041 | if (value instanceof Date) { 1042 | value = date(key); 1043 | } 1044 | 1045 | // If we were called with a replacer function, then call the replacer to 1046 | // obtain a replacement value. 1047 | 1048 | if (typeof rep === 'function') { 1049 | value = rep.call(holder, key, value); 1050 | } 1051 | 1052 | // What happens next depends on the value's type. 1053 | 1054 | switch (typeof value) { 1055 | case 'string': 1056 | return quote(value); 1057 | 1058 | case 'number': 1059 | 1060 | // JSON numbers must be finite. Encode non-finite numbers as null. 1061 | 1062 | return isFinite(value) ? String(value) : 'null'; 1063 | 1064 | case 'boolean': 1065 | case 'null': 1066 | 1067 | // If the value is a boolean or null, convert it to a string. Note: 1068 | // typeof null does not produce 'null'. The case is included here in 1069 | // the remote chance that this gets fixed someday. 1070 | 1071 | return String(value); 1072 | 1073 | // If the type is 'object', we might be dealing with an object or an array or 1074 | // null. 1075 | 1076 | case 'object': 1077 | 1078 | // Due to a specification blunder in ECMAScript, typeof null is 'object', 1079 | // so watch out for that case. 1080 | 1081 | if (!value) { 1082 | return 'null'; 1083 | } 1084 | 1085 | // Make an array to hold the partial results of stringifying this object value. 1086 | 1087 | gap += indent; 1088 | partial = []; 1089 | 1090 | // Is the value an array? 1091 | 1092 | if (Object.prototype.toString.apply(value) === '[object Array]') { 1093 | 1094 | // The value is an array. Stringify every element. Use null as a placeholder 1095 | // for non-JSON values. 1096 | 1097 | length = value.length; 1098 | for (i = 0; i < length; i += 1) { 1099 | partial[i] = str(i, value) || 'null'; 1100 | } 1101 | 1102 | // Join all of the elements together, separated with commas, and wrap them in 1103 | // brackets. 1104 | 1105 | v = partial.length === 0 ? '[]' : gap ? 1106 | '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' : 1107 | '[' + partial.join(',') + ']'; 1108 | gap = mind; 1109 | return v; 1110 | } 1111 | 1112 | // If the replacer is an array, use it to select the members to be stringified. 1113 | 1114 | if (rep && typeof rep === 'object') { 1115 | length = rep.length; 1116 | for (i = 0; i < length; i += 1) { 1117 | if (typeof rep[i] === 'string') { 1118 | k = rep[i]; 1119 | v = str(k, value); 1120 | if (v) { 1121 | partial.push(quote(k) + (gap ? ': ' : ':') + v); 1122 | } 1123 | } 1124 | } 1125 | } else { 1126 | 1127 | // Otherwise, iterate through all of the keys in the object. 1128 | 1129 | for (k in value) { 1130 | if (Object.prototype.hasOwnProperty.call(value, k)) { 1131 | v = str(k, value); 1132 | if (v) { 1133 | partial.push(quote(k) + (gap ? ': ' : ':') + v); 1134 | } 1135 | } 1136 | } 1137 | } 1138 | 1139 | // Join all of the member texts together, separated with commas, 1140 | // and wrap them in braces. 1141 | 1142 | v = partial.length === 0 ? '{}' : gap ? 1143 | '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' : 1144 | '{' + partial.join(',') + '}'; 1145 | gap = mind; 1146 | return v; 1147 | } 1148 | } 1149 | 1150 | // If the JSON object does not yet have a stringify method, give it one. 1151 | 1152 | JSON.stringify = function (value, replacer, space) { 1153 | 1154 | // The stringify method takes a value and an optional replacer, and an optional 1155 | // space parameter, and returns a JSON text. The replacer can be a function 1156 | // that can replace values, or an array of strings that will select the keys. 1157 | // A default replacer method can be provided. Use of the space parameter can 1158 | // produce text that is more easily readable. 1159 | 1160 | var i; 1161 | gap = ''; 1162 | indent = ''; 1163 | 1164 | // If the space parameter is a number, make an indent string containing that 1165 | // many spaces. 1166 | 1167 | if (typeof space === 'number') { 1168 | for (i = 0; i < space; i += 1) { 1169 | indent += ' '; 1170 | } 1171 | 1172 | // If the space parameter is a string, it will be used as the indent string. 1173 | 1174 | } else if (typeof space === 'string') { 1175 | indent = space; 1176 | } 1177 | 1178 | // If there is a replacer, it must be a function or an array. 1179 | // Otherwise, throw an error. 1180 | 1181 | rep = replacer; 1182 | if (replacer && typeof replacer !== 'function' && 1183 | (typeof replacer !== 'object' || 1184 | typeof replacer.length !== 'number')) { 1185 | throw new Error('JSON.stringify'); 1186 | } 1187 | 1188 | // Make a fake root object containing our value under the key of ''. 1189 | // Return the result of stringifying the value. 1190 | 1191 | return str('', {'': value}); 1192 | }; 1193 | 1194 | // If the JSON object does not yet have a parse method, give it one. 1195 | 1196 | JSON.parse = function (text, reviver) { 1197 | // The parse method takes a text and an optional reviver function, and returns 1198 | // a JavaScript value if the text is a valid JSON text. 1199 | 1200 | var j; 1201 | 1202 | function walk(holder, key) { 1203 | 1204 | // The walk method is used to recursively walk the resulting structure so 1205 | // that modifications can be made. 1206 | 1207 | var k, v, value = holder[key]; 1208 | if (value && typeof value === 'object') { 1209 | for (k in value) { 1210 | if (Object.prototype.hasOwnProperty.call(value, k)) { 1211 | v = walk(value, k); 1212 | if (v !== undefined) { 1213 | value[k] = v; 1214 | } else { 1215 | delete value[k]; 1216 | } 1217 | } 1218 | } 1219 | } 1220 | return reviver.call(holder, key, value); 1221 | } 1222 | 1223 | 1224 | // Parsing happens in four stages. In the first stage, we replace certain 1225 | // Unicode characters with escape sequences. JavaScript handles many characters 1226 | // incorrectly, either silently deleting them, or treating them as line endings. 1227 | 1228 | text = String(text); 1229 | cx.lastIndex = 0; 1230 | if (cx.test(text)) { 1231 | text = text.replace(cx, function (a) { 1232 | return '\\u' + 1233 | ('0000' + a.charCodeAt(0).toString(16)).slice(-4); 1234 | }); 1235 | } 1236 | 1237 | // In the second stage, we run the text against regular expressions that look 1238 | // for non-JSON patterns. We are especially concerned with '()' and 'new' 1239 | // because they can cause invocation, and '=' because it can cause mutation. 1240 | // But just to be safe, we want to reject all unexpected forms. 1241 | 1242 | // We split the second stage into 4 regexp operations in order to work around 1243 | // crippling inefficiencies in IE's and Safari's regexp engines. First we 1244 | // replace the JSON backslash pairs with '@' (a non-JSON character). Second, we 1245 | // replace all simple value tokens with ']' characters. Third, we delete all 1246 | // open brackets that follow a colon or comma or that begin the text. Finally, 1247 | // we look to see that the remaining characters are only whitespace or ']' or 1248 | // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval. 1249 | 1250 | if (/^[\],:{}\s]*$/ 1251 | .test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@') 1252 | .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']') 1253 | .replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { 1254 | 1255 | // In the third stage we use the eval function to compile the text into a 1256 | // JavaScript structure. The '{' operator is subject to a syntactic ambiguity 1257 | // in JavaScript: it can begin a block or an object literal. We wrap the text 1258 | // in parens to eliminate the ambiguity. 1259 | 1260 | j = eval('(' + text + ')'); 1261 | 1262 | // In the optional fourth stage, we recursively walk the new structure, passing 1263 | // each name/value pair to a reviver function for possible transformation. 1264 | 1265 | return typeof reviver === 'function' ? 1266 | walk({'': j}, '') : j; 1267 | } 1268 | 1269 | // If the text is not JSON parseable, then a SyntaxError is thrown. 1270 | 1271 | throw new SyntaxError('JSON.parse'); 1272 | }; 1273 | 1274 | return JSON; 1275 | })(); 1276 | 1277 | if ('undefined' != typeof window) { 1278 | window.expect = module.exports; 1279 | } 1280 | 1281 | })( 1282 | this 1283 | , 'undefined' != typeof module ? module : {exports: {}} 1284 | ); -------------------------------------------------------------------------------- /test/mocha.css: -------------------------------------------------------------------------------- 1 | 2 | body { 3 | font: 20px/1.5 "Helvetica Neue", Helvetica, Arial, sans-serif; 4 | padding: 60px 50px; 5 | } 6 | 7 | #mocha ul, #mocha li { 8 | margin: 0; 9 | padding: 0; 10 | } 11 | 12 | #mocha ul { 13 | list-style: none; 14 | } 15 | 16 | #mocha h1, #mocha h2 { 17 | margin: 0; 18 | } 19 | 20 | #mocha h1 { 21 | margin-top: 15px; 22 | font-size: 1em; 23 | font-weight: 200; 24 | } 25 | 26 | #mocha h1 a { 27 | text-decoration: none; 28 | color: inherit; 29 | } 30 | 31 | #mocha h1 a:hover { 32 | text-decoration: underline; 33 | } 34 | 35 | #mocha .suite .suite h1 { 36 | margin-top: 0; 37 | font-size: .8em; 38 | } 39 | 40 | #mocha h2 { 41 | font-size: 12px; 42 | font-weight: normal; 43 | cursor: pointer; 44 | } 45 | 46 | #mocha .suite { 47 | margin-left: 15px; 48 | } 49 | 50 | #mocha .test { 51 | margin-left: 15px; 52 | } 53 | 54 | #mocha .test:hover h2::after { 55 | position: relative; 56 | top: 0; 57 | right: -10px; 58 | content: '(view source)'; 59 | font-size: 12px; 60 | font-family: arial; 61 | color: #888; 62 | } 63 | 64 | #mocha .test.pending:hover h2::after { 65 | content: '(pending)'; 66 | font-family: arial; 67 | } 68 | 69 | #mocha .test.pass.medium .duration { 70 | background: #C09853; 71 | } 72 | 73 | #mocha .test.pass.slow .duration { 74 | background: #B94A48; 75 | } 76 | 77 | #mocha .test.pass::before { 78 | content: '?'; 79 | font-size: 12px; 80 | display: block; 81 | float: left; 82 | margin-right: 5px; 83 | color: #00d6b2; 84 | } 85 | 86 | #mocha .test.pass .duration { 87 | font-size: 9px; 88 | margin-left: 5px; 89 | padding: 2px 5px; 90 | color: white; 91 | -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.2); 92 | -moz-box-shadow: inset 0 1px 1px rgba(0,0,0,.2); 93 | box-shadow: inset 0 1px 1px rgba(0,0,0,.2); 94 | -webkit-border-radius: 5px; 95 | -moz-border-radius: 5px; 96 | -ms-border-radius: 5px; 97 | -o-border-radius: 5px; 98 | border-radius: 5px; 99 | } 100 | 101 | #mocha .test.pass.fast .duration { 102 | display: none; 103 | } 104 | 105 | #mocha .test.pending { 106 | color: #0b97c4; 107 | } 108 | 109 | #mocha .test.pending::before { 110 | content: '?'; 111 | color: #0b97c4; 112 | } 113 | 114 | #mocha .test.fail { 115 | color: #c00; 116 | } 117 | 118 | #mocha .test.fail pre { 119 | color: black; 120 | } 121 | 122 | #mocha .test.fail::before { 123 | content: '?'; 124 | font-size: 12px; 125 | display: block; 126 | float: left; 127 | margin-right: 5px; 128 | color: #c00; 129 | } 130 | 131 | #mocha .test pre.error { 132 | color: #c00; 133 | } 134 | 135 | #mocha .test pre { 136 | display: inline-block; 137 | font: 12px/1.5 monaco, monospace; 138 | margin: 5px; 139 | padding: 15px; 140 | border: 1px solid #eee; 141 | border-bottom-color: #ddd; 142 | -webkit-border-radius: 3px; 143 | -webkit-box-shadow: 0 1px 3px #eee; 144 | } 145 | 146 | #error { 147 | color: #c00; 148 | font-size: 1.5 em; 149 | font-weight: 100; 150 | letter-spacing: 1px; 151 | } 152 | 153 | #stats { 154 | position: fixed; 155 | top: 15px; 156 | right: 10px; 157 | font-size: 12px; 158 | margin: 0; 159 | color: #888; 160 | } 161 | 162 | #stats .progress { 163 | float: right; 164 | padding-top: 0; 165 | } 166 | 167 | #stats em { 168 | color: black; 169 | } 170 | 171 | #stats li { 172 | display: inline-block; 173 | margin: 0 5px; 174 | list-style: none; 175 | padding-top: 11px; 176 | } 177 | 178 | code .comment { color: #ddd } 179 | code .init { color: #2F6FAD } 180 | code .string { color: #5890AD } 181 | code .keyword { color: #8A6343 } 182 | code .number { color: #2F6FAD } 183 | -------------------------------------------------------------------------------- /test/mocha.html: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amark/monotype/fa30f1039128da0bca3553f16841c803c681a82c/test/mocha.html -------------------------------------------------------------------------------- /test/mocha.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amark/monotype/fa30f1039128da0bca3553f16841c803c681a82c/test/mocha.js -------------------------------------------------------------------------------- /test/skin.js: -------------------------------------------------------------------------------- 1 | $(function(){ 2 | $('#size').keyup(function(e){ 3 | var val = $(this).val(); 4 | $('html, body, pre, code').css({ 5 | 'font-size': val 6 | }); 7 | }); 8 | $('#theme').change(function(e){ 9 | var val = $(this).val(); 10 | $('#style').remove(); 11 | $('head').append(''); 12 | }); 13 | $('#font').keyup(function(e){ 14 | var val = $(this).val(); 15 | $('html, body, pre, code').css({ 16 | 'font-family': val 17 | }); 18 | WebFontConfig = { 19 | google: {families: [val]} 20 | }; 21 | if(window.WebFont && WebFont.load){ 22 | return WebFont.load(WebFontConfig); 23 | } 24 | (function() { 25 | var wf = document.createElement('script'); 26 | wf.src = ('https:' == document.location.protocol ? 'https' : 'http') + 27 | '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js'; 28 | wf.type = 'text/javascript'; 29 | wf.async = 'true'; 30 | var s = document.getElementsByTagName('script')[0]; 31 | s.parentNode.insertBefore(wf, s); 32 | console.log("fontAPI"); 33 | console.log(val); 34 | })(); 35 | }); 36 | }); -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | $(function(){ 2 | /* 3 | Tests not finished! 4 | How does one automate testing of human input? 5 | */ 6 | describe('Flatten',function(){ 7 | it('Down',function(){ 8 | var m = monotype($('#imgs')) 9 | , c = m.root.contents() 10 | , z = m.root[0] 11 | , t = monotype.deep(z, 11); 12 | expect(t.container).to.be(c.last()[0]); 13 | expect(t.offset).to.be(0); 14 | // test [text, 1] vs [text, 4] 15 | m.root.on('keyup',function(e){ 16 | var r = monotype(m.root); 17 | m.root.html(m.root.html()); 18 | m.root.children().last().remove(); 19 | r.restore(); 20 | }); 21 | }); 22 | var m = monotype($('#nest')); 23 | m.root.on('keyup',function(e){ 24 | var r = monotype(m.root); 25 | m.root.html(m.root.html()); 26 | r.restore(); 27 | }); 28 | it('Forwards',function(){ 29 | var c = m.root.contents() 30 | , d = m.root[0] 31 | , z = c[0]; 32 | expect(z = monotype.next(z,d)).to.be(c[1]); 33 | expect(z = monotype.next(z,d)).to.be(c[2]); 34 | expect(z = monotype.next(z,d)).to.be($(c[3]).contents()[0]); 35 | expect(z = monotype.next(z,d)).to.be(c[4]); 36 | expect(z = monotype.next(z,d)).to.be($(c[5]).contents()[0]); 37 | expect(z = monotype.next(z,d)).to.be($($(c[5]).contents()[1]).contents()[0]); 38 | expect(z = monotype.next(z,d)).to.be(c[6]); 39 | expect(z = monotype.next(z,d)).to.be(c[7]); 40 | expect(z = monotype.next(z,d)).to.be(c[8]); 41 | expect(z = monotype.next(z,d)).to.be($(c[9]).contents()[0]); 42 | expect(z = monotype.next(z,d)).to.be($(c[9]).contents()[1]); 43 | expect(z = monotype.next(z,d)).to.be($(c[9]).contents()[2]); 44 | expect(z = monotype.next(z,d)).to.be($($($(c[9]).contents()[3]).contents()[0]).contents()[0]); 45 | expect(z = monotype.next(z,d)).to.be(c[10]); 46 | expect(z = monotype.next(z,d)).to.be(c[11]); 47 | expect(z = monotype.next(z,d)).to.be(c[12]); 48 | expect(z = monotype.next(z,d)).to.be(c[13]); 49 | console.log(z); 50 | expect(z = monotype.next(z,d)).to.be(null); 51 | alert(1); 52 | expect(z = monotype.next(z,d)).to.be(null); 53 | }); 54 | it('Backwards',function(){ 55 | var c = m.root.contents() 56 | , d = m.root[0] 57 | , z = c.last()[0]; 58 | expect(z = monotype.prev(z,d)).to.be(c[12]); 59 | expect(z = monotype.prev(z,d)).to.be(c[11]); 60 | expect(z = monotype.prev(z,d)).to.be(c[10]); 61 | expect(z = monotype.prev(z,d)).to.be($($($(c[9]).contents()[3]).contents()[0]).contents()[0]); 62 | expect(z = monotype.prev(z,d)).to.be($(c[9]).contents()[2]); 63 | expect(z = monotype.prev(z,d)).to.be($(c[9]).contents()[1]); 64 | expect(z = monotype.prev(z,d)).to.be($(c[9]).contents()[0]); 65 | expect(z = monotype.prev(z,d)).to.be(c[8]); 66 | expect(z = monotype.prev(z,d)).to.be(c[7]); 67 | expect(z = monotype.prev(z,d)).to.be(c[6]); 68 | expect(z = monotype.prev(z,d)).to.be($($(c[5]).contents()[1]).contents()[0]); 69 | expect(z = monotype.prev(z,d)).to.be($(c[5]).contents()[0]); 70 | expect(z = monotype.prev(z,d)).to.be(c[4]); 71 | expect(z = monotype.prev(z,d)).to.be($(c[3]).contents()[0]); 72 | expect(z = monotype.prev(z,d)).to.be(c[2]); 73 | expect(z = monotype.prev(z,d)).to.be(c[1]); 74 | expect(z = monotype.prev(z,d)).to.be(c[0]); 75 | expect(z = monotype.prev(z,d)).to.be(null); 76 | expect(z = monotype.prev(z,d)).to.be(null); 77 | }); 78 | }); 79 | var m = monotype($('#hn')); 80 | m.root.on('keyup',function(e){ 81 | var r = monotype(m.root); 82 | m.root.html(m.root.html()); 83 | r.restore(); 84 | }); 85 | mocha.run(); 86 | }); --------------------------------------------------------------------------------