Ticket #7907: insertions-includes-ticket-7903.diff
| File insertions-includes-ticket-7903.diff, 23.3 kB (added by Tobie, 2 years ago) |
|---|
-
test/unit/dom.html
old new 261 261 <p id="test-full">content</p> 262 262 <div id="ancestor"><div id="child"><div><div id="great-grand-child"></div></div></div></div> 263 263 <div id="not-in-the-family"></div> 264 265 <div id="insertions-container"><div id="insertions-main"><p>some content.</p></div></div> 266 <div id="insertions-node-container"><div id="insertions-node-main"><p>some content.</p></div></div> 267 <div id="element-insertions-container"><div id="element-insertions-main"><p>some content.</p></div></div> 268 <div id="wrap-container"><p id="wrap"></p></div> 264 269 <!-- Tests follow --> 265 270 <script type="text/javascript" language="javascript" charset="utf-8"> 266 271 // <![CDATA[ 267 272 268 273 var testVar = 'to be updated'; 269 274 var getInnerHTML = function(id) { 275 return $(id).innerHTML.toString().toLowerCase().gsub(/[\r\n\t]/, ''); 276 }; 270 277 Element.addMethods("LI", { 271 278 pancakes: function(element) { return "pancakes"; } 272 279 }); … … 303 310 assertElementsMatch(document.getElementsByClassName('B', 'class_names'), 'ul#class_names_ul.A.B', 'div.B.C.D'); 304 311 assertElementsMatch(document.getElementsByClassName('B', 'class_names_ul')); 305 312 }}, 313 314 testInsertionWithHTML: function() {with(this) { 315 new Insertion.Before('insertions-main', '<p><em>before</em> text</p><p>more testing</p>'); 316 assert(getInnerHTML('insertions-container').startsWith('<p><em>before</em> text</p><p>more testing</p>')); 317 new Insertion.After('insertions-main', '<p><em>after</em> text</p><p>more testing</p>'); 318 assert(getInnerHTML('insertions-container').endsWith('<p><em>after</em> text</p><p>more testing</p>')); 319 new Insertion.Top('insertions-main', '<p><em>top</em> text.</p><p>more testing</p>'); 320 assert(getInnerHTML('insertions-main').startsWith('<p><em>top</em> text.</p><p>more testing</p>')); 321 new Insertion.Bottom('insertions-main', '<p><em>bottom</em> text.</p><p>more testing</p>'); 322 assert(getInnerHTML('insertions-main').endsWith('<p><em>bottom</em> text.</p><p>more testing</p>')); 323 }}, 324 325 testInsertionWithDOMNode: function() {with(this) { 326 var createParagraph = function(text) { 327 var p = document.createElement('p'); 328 p.appendChild(document.createTextNode(text)); 329 return p; 330 } 331 new Insertion.Before('insertions-node-main', createParagraph('node before')); 332 assert(getInnerHTML('insertions-node-container').startsWith('<p>node before</p>')); 333 new Insertion.After('insertions-node-main', createParagraph('node after')); 334 assert(getInnerHTML('insertions-node-container').endsWith('<p>node after</p>')); 335 new Insertion.Top('insertions-node-main', createParagraph('node top')); 336 assert(getInnerHTML('insertions-node-main').startsWith('<p>node top</p>')); 337 new Insertion.Bottom('insertions-node-main', createParagraph('node bottom')); 338 assert(getInnerHTML('insertions-node-main').endsWith('<p>node bottom</p>')); 339 }}, 306 340 307 testInsertWithTR: function() {with(this) { 341 testInsertionWithNonString: function() {with(this) { 342 new Insertion.Bottom('insertions-main', 3); 343 assert(getInnerHTML('insertions-main').endsWith('3')); 344 }}, 345 346 testInsertionWithTR: function() {with(this) { 308 347 new Insertion.After('second_row', '<tr id="third_row"><td>Third Row</td></tr>'); 309 348 assert($('second_row').descendantOf('table')); 310 349 }}, 311 350 312 351 testElementVisible: function(){with(this) { 313 352 assertNotEqual('none', $('test-visible').style.display); 314 353 assertEqual('none', $('test-hidden').style.display); … … 419 458 }); 420 459 }); 421 460 }}, 422 461 462 testElementAddBefore: function() {with(this) { 463 $('element-insertions-main').addBefore('before'); 464 assert(getInnerHTML('element-insertions-container').startsWith('before')); 465 }}, 466 467 testElementAddAfter: function() {with(this) { 468 $('element-insertions-main').addAfter('after'); 469 assert(getInnerHTML('element-insertions-container').endsWith('after')); 470 }}, 471 472 testElementPrepend: function() {with(this) { 473 $('element-insertions-main').prepend('top'); 474 assert(getInnerHTML('element-insertions-main').startsWith('top')); 475 }}, 476 477 testElementAppend: function() {with(this) { 478 $('element-insertions-main').append('bottom'); 479 assert(getInnerHTML('element-insertions-main').endsWith('bottom')); 480 }}, 481 482 testElementWrap: function() {with(this) { 483 var element = $('wrap'); 484 element.wrap('div'); 485 assert(getInnerHTML('wrap-container').startsWith('<div><p')); 486 element.wrap(document.createElement('div')); 487 assert(getInnerHTML('wrap-container').startsWith('<div><div><p')); 488 assert(Object.isFunction(wrap.setStyle)); 489 }}, 490 423 491 testElementSelectorMethod: function() {with(this) { 424 492 var testSelector = $('container').getElementsBySelector('p.test'); 425 493 assertEqual(testSelector.length, 4); -
test/unit/base.html
old new 120 120 assertEqual('I\'m a div with id test', Object.toJSON(element)); 121 121 }}, 122 122 123 testObjectIs: function() { with(this) { 124 var types = [true, false, 0, 1, '', 'text', null, undefined, [], {}, $H({}), new Hash({}), document.createElement('div'), Prototype.K]; 125 $H({isObject: [false, false, false, false, false, false, true, false, true, true, true, true, true, false], 126 isFunction: [false, false, false, false, false, false, false, false, false, false, false, false, false, true], 127 isArray: [ false, false, false, false, false, false, false, false, true, false, false, false, false, false], 128 isHash: [ false, false, false, false, false, false, false, false, false, false, true, true, false, false], 129 isString: [ false, false, false, false, true, true, false, false, false, false, false, false, false, false], 130 isNumber: [ false, false, true, true, false, false, false, false, false, false, false, false, false, false], 131 isBoolean: [true, true, false, false, false, false, false, false, false, false, false, false, false, false], 132 isUndefined: [false, false, false, false, false, false, false, true, false, false, false, false, false, false], 133 isNull: [ false, false, false, false, false, false, true, false, false, false, false, false, false, false], 134 isDefined: [true, true, true, true, true, true, true, false, true, true, true, true, true, true], 135 isNullOrUndefined: [false, false, false, false, false, false, true, true, false, false, false, false, false, false], 136 isElement: [false, false, false, false, false, false, false, false, false, false, false, false, true, false], 137 isEnumerable: [false, false, false, false, false, false, false, false, true, false, true, true, false, false] 138 }).each(function(item){ 139 item.value.zip(types).each(function(args) { 140 assertIdentical(args[0], Object[item.key](args[1])); 141 }); 142 }); 143 }}, 144 123 145 // sanity check 124 146 testDoesntExtendObjectPrototype: function() {with(this) { 125 147 // for-in is supported with objects -
src/string.js
old new 1 1 Object.extend(String, { 2 2 interpret: function(value) { 3 return value == null? '' : String(value);3 return Object.isNullOrUndefined(value) ? '' : String(value); 4 4 }, 5 5 specialChar: { 6 6 '\b': '\\b', … … 31 31 32 32 sub: function(pattern, replacement, count) { 33 33 replacement = this.gsub.prepareReplacement(replacement); 34 count = count === undefined? 1 : count;34 count = Object.isUndefined(count) ? 1 : count; 35 35 36 36 return this.gsub(pattern, function(match) { 37 37 if (--count < 0) return match[0]; … … 46 46 47 47 truncate: function(length, truncation) { 48 48 length = length || 30; 49 truncation = truncation === undefined? '...' : truncation;49 truncation = Object.isUndefined(truncation) ? '...' : truncation; 50 50 return this.length > length ? 51 51 this.slice(0, length - truncation.length) + truncation : this; 52 52 }, … … 98 98 var name = decodeURIComponent(pair[0]); 99 99 var value = pair[1] ? decodeURIComponent(pair[1]) : undefined; 100 100 101 if ( hash[name] !== undefined) {102 if ( hash[name].constructor != Array)101 if (Object.isDefined(hash[name])) { 102 if (!Object.isArray(hash[name])) 103 103 hash[name] = [hash[name]]; 104 104 if (value) hash[name].push(value); 105 105 } … … 193 193 }); 194 194 195 195 String.prototype.gsub.prepareReplacement = function(replacement) { 196 if ( typeof replacement == 'function') return replacement;196 if (Object.isFunction(replacement)) return replacement; 197 197 var template = new Template(replacement); 198 198 return function(match) { return template.evaluate(match) }; 199 199 } -
src/base.js
old new 18 18 Object.extend(Object, { 19 19 inspect: function(object) { 20 20 try { 21 if ( object === undefined) return 'undefined';22 if ( object === null) return 'null';21 if (Object.isUndefined(object)) return 'undefined'; 22 if (Object.isNull(object)) return 'null'; 23 23 return object.inspect ? object.inspect() : object.toString(); 24 24 } catch (e) { 25 25 if (e instanceof RangeError) return '...'; … … 35 35 case 'unknown': return; 36 36 case 'boolean': return object.toString(); 37 37 } 38 if ( object === null) return 'null';38 if (Object.isNull(object)) return 'null'; 39 39 if (object.toJSON) return object.toJSON(); 40 if ( object.ownerDocument === document) return;40 if (Object.isElement(object)) return; 41 41 var results = []; 42 42 for (var property in object) { 43 43 var value = Object.toJSON(object[property]); 44 if ( value !== undefined)44 if (Object.isDefined(value)) 45 45 results.push(property.toJSON() + ':' + value); 46 46 } 47 47 return '{' + results.join(',') + '}'; … … 63 63 64 64 clone: function(object) { 65 65 return Object.extend({}, object); 66 }, 67 68 isArray: function(object) { 69 return !!(object && object.constructor === Array); 70 }, 71 72 isHash: function(object) { 73 return object instanceof Hash; 74 }, 75 76 isUndefined: function(object) { 77 return object === undefined; 78 }, 79 80 isNull: function(object) { 81 return object === null; 82 }, 83 84 isDefined: function(object) { 85 return object !== undefined; 86 }, 87 88 isNullOrUndefined: function(object) { 89 return object == undefined; 90 }, 91 92 isElement: function(object) { 93 return !!(object && object.ownerDocument === document); 94 }, 95 96 isEnumerable: function(object) { 97 return !!(object && object.each === Enumerable.each); 66 98 } 67 99 }); 68 100 101 (function(type) { 102 for(var i = 0, length = type.length; i < length; i++) 103 Object['is' + type[i]] = function(t) { 104 return function(object) { 105 return typeof object === t; 106 }; 107 }(type[i].toLowerCase()); 108 }(['Number', 'Boolean', 'String', 'Function', 'Object'])); 109 69 110 Function.prototype.bind = function() { 70 111 var __method = this, args = $A(arguments), object = args.shift(); 71 112 return function() { -
src/array.js
old new 13 13 if (Prototype.Browser.WebKit) { 14 14 $A = Array.from = function(iterable) { 15 15 if (!iterable) return []; 16 if (!( typeof iterable == 'function'&& iterable == '[object NodeList]') &&16 if (!(Object.isFunction(iterable) && iterable == '[object NodeList]') && 17 17 iterable.toArray) { 18 18 return iterable.toArray(); 19 19 } else { … … 57 57 58 58 flatten: function() { 59 59 return this.inject([], function(array, value) { 60 return array.concat( value && value.constructor == Array?60 return array.concat(Object.isArray(value) ? 61 61 value.flatten() : [value]); 62 62 }); 63 63 }, … … 107 107 var results = []; 108 108 this.each(function(object) { 109 109 var value = Object.toJSON(object); 110 if ( value !== undefined) results.push(value);110 if (Object.isDefined(value)) results.push(value); 111 111 }); 112 112 return '[' + results.join(',') + ']'; 113 113 } … … 125 125 var array = []; 126 126 for (var i = 0, length = this.length; i < length; i++) array.push(this[i]); 127 127 for (var i = 0, length = arguments.length; i < length; i++) { 128 if ( arguments[i].constructor == Array) {128 if (Object.isArray(arguments[i])) { 129 129 for (var j = 0, arrayLength = arguments[i].length; j < arrayLength; j++) 130 130 array.push(arguments[i][j]); 131 131 } else { -
src/dom.js
old new 4 4 elements.push($(arguments[i])); 5 5 return elements; 6 6 } 7 if ( typeof element == 'string')7 if (Object.isString(element)) 8 8 element = document.getElementById(element); 9 9 return Element.extend(element); 10 10 } … … 59 59 60 60 for (var property in methods) { 61 61 var value = methods[property]; 62 if ( typeof value == 'function'&& !(property in element))62 if (Object.isFunction(value) && !(property in element)) 63 63 element[property] = cache.findOrStore(value); 64 64 } 65 65 … … 103 103 }, 104 104 105 105 update: function(element, html) { 106 html = typeof html == 'undefined'? '' : html.toString();106 html = Object.isUndefined(html) ? '' : html.toString(); 107 107 $(element).innerHTML = html.stripScripts(); 108 108 setTimeout(function() {html.evalScripts()}, 10); 109 109 return element; … … 111 111 112 112 replace: function(element, html) { 113 113 element = $(element); 114 html = typeof html == 'undefined'? '' : html.toString();114 html = Object.isUndefined(html) ? '' : html.toString(); 115 115 if (element.outerHTML) { 116 116 element.outerHTML = html.stripScripts(); 117 117 } else { … … 124 124 return element; 125 125 }, 126 126 127 append: function(element, content) { 128 element = $(element); 129 new Insertion.Bottom(element, content); 130 return element; 131 }, 132 133 prepend: function(element, content) { 134 element = $(element); 135 new Insertion.Top(element, content); 136 return element; 137 }, 138 139 addBefore: function(element, content) { 140 element = $(element); 141 new Insertion.Before(element, content); 142 return element; 143 }, 144 145 addAfter: function(element, content) { 146 element = $(element); 147 new Insertion.After(element, content); 148 return element; 149 }, 150 151 wrap: function(element, wrapper) { 152 element = $(element); 153 if(Object.isString(wrapper)) 154 wrapper = document.createElement(wrapper); 155 element.parentNode.replaceChild(wrapper, element); 156 wrapper.appendChild(element); 157 return element; 158 }, 159 127 160 inspect: function(element) { 128 161 element = $(element); 129 162 var result = '<' + element.tagName.toLowerCase(); … … 173 206 }, 174 207 175 208 match: function(element, selector) { 176 if ( typeof selector == 'string')209 if (Object.isString(selector)) 177 210 selector = new Selector(selector); 178 211 return selector.match($(element)); 179 212 }, … … 329 362 if (property == 'opacity') element.setOpacity(styles[property]) 330 363 else 331 364 elementStyle[(property == 'float' || property == 'cssFloat') ? 332 ( elementStyle.styleFloat === undefined? 'cssFloat' : 'styleFloat') :365 (Object.isUndefined(elementStyle.styleFloat) ? 'cssFloat' : 'styleFloat') : 333 366 (camelized ? property : property.camelize())] = styles[property]; 334 367 335 368 return element; … … 463 496 // IE is missing .innerHTML support for TABLE-related elements 464 497 Element.Methods.update = function(element, html) { 465 498 element = $(element); 466 html = typeof html == 'undefined'? '' : html.toString();499 html = Object.isUndefined(html) ? '' : html.toString(); 467 500 var tagName = element.tagName.toUpperCase(); 468 501 if (['THEAD','TBODY','TR','TD'].include(tagName)) { 469 502 var div = document.createElement('div'); … … 575 608 576 609 if (!tagName) Object.extend(Element.Methods, methods || {}); 577 610 else { 578 if ( tagName.constructor == Array) tagName.each(extend);611 if (Object.isArray(tagName)) tagName.each(extend); 579 612 else extend(tagName); 580 613 } 581 614 … … 629 662 if (F.SpecificElementExtensions) { 630 663 for (var tag in Element.Methods.ByTag) { 631 664 var klass = findDOMClass(tag); 632 if ( typeof klass == "undefined") continue;665 if (Object.isUndefined(klass)) continue; 633 666 copy(T[tag], klass.prototype); 634 667 } 635 668 } … … 646 679 Abstract.Insertion.prototype = { 647 680 initialize: function(element, content) { 648 681 this.element = $(element); 682 if(Object.isElement(content)) return this.insertNode(content); 683 content = content.toString(); 649 684 this.content = content.stripScripts(); 650 685 651 686 if (this.adjacency && this.element.insertAdjacentHTML) { … … 672 707 var div = document.createElement('div'); 673 708 div.innerHTML = '<table><tbody>' + this.content + '</tbody></table>'; 674 709 return $A(div.childNodes[0].childNodes[0].childNodes); 710 }, 711 712 insertContent: function(fragments) { 713 fragments.each(this.insertNode.bind(this)); 675 714 } 676 715 } 677 716 … … 683 722 this.range.setStartBefore(this.element); 684 723 }, 685 724 686 insertContent: function(fragments) { 687 fragments.each((function(fragment) { 688 this.element.parentNode.insertBefore(fragment, this.element); 689 }).bind(this)); 725 insertNode: function(node) { 726 this.element.parentNode.insertBefore(node, this.element); 690 727 } 691 728 }); 692 729 … … 697 734 this.range.collapse(true); 698 735 }, 699 736 737 insertNode: function(node) { 738 this.element.insertBefore(node, this.element.firstChild); 739 }, 740 700 741 insertContent: function(fragments) { 701 fragments.reverse(false).each((function(fragment) { 702 this.element.insertBefore(fragment, this.element.firstChild); 703 }).bind(this)); 742 fragments.reverse().each(this.insertNode.bind(this)); 704 743 } 705 744 }); 706 745 … … 711 750 this.range.collapse(this.element); 712 751 }, 713 752 714 insertContent: function(fragments) { 715 fragments.each((function(fragment) { 716 this.element.appendChild(fragment); 717 }).bind(this)); 753 insertNode: function(node) { 754 this.element.appendChild(node); 718 755 } 719 756 }); 720 757 … … 724 761 this.range.setStartAfter(this.element); 725 762 }, 726 763 727 insertContent: function(fragments) { 728 fragments.each((function(fragment) { 729 this.element.parentNode.insertBefore(fragment, 730 this.element.nextSibling); 731 }).bind(this)); 764 insertNode: function(node) { 765 this.element.parentNode.insertBefore(node, this.element.nextSibling); 732 766 } 733 767 }); 734 768 -
src/enumerable.js
old new 89 89 }, 90 90 91 91 inGroupsOf: function(number, fillWith) { 92 fillWith = fillWith === undefined? null : fillWith;92 fillWith = Object.isUndefined(fillWith) ? null : fillWith; 93 93 return this.eachSlice(number, function(slice) { 94 94 while(slice.length < number) slice.push(fillWith); 95 95 return slice; -
src/selector.js
old new 109 109 pseudo: function(m) { 110 110 var h = Selector.xpath.pseudos[m[1]]; 111 111 if (!h) return ''; 112 if ( typeof h === 'function') return h(m);112 if (Object.isFunction(h)) return h(m); 113 113 return new Template(Selector.xpath.pseudos[m[1]]).evaluate(m); 114 114 }, 115 115 operators: { … … 375 375 var handler = Selector.operators[operator], results = []; 376 376 for (var i = 0, node; node = nodes[i]; i++) { 377 377 var nodeValue = Element.readAttribute(node, attr); 378 if ( nodeValue === null) continue;378 if (Object.isNull(nodeValue)) continue; 379 379 if (handler(nodeValue, value)) results.push(node); 380 380 } 381 381 return results; … … 529 529 }, 530 530 531 531 findElement: function(elements, expression, index) { 532 if ( typeof expression == 'number') {532 if (Object.isNumber(expression)) { 533 533 index = expression; expression = false; 534 534 } 535 535 return Selector.matchElements(elements, expression || '*')[index || 0]; -
src/form.js
old new 10 10 var key = element.name, value = $(element).getValue(); 11 11 if (value != null) { 12 12 if (key in result) { 13 if ( result[key].constructor != Array) result[key] = [result[key]];13 if (!Object.isArray(result[key])) result[key] = [result[key]]; 14 14 result[key].push(value); 15 15 } 16 16 else result[key] = value; … … 91 91 options.parameters = form.serialize(true); 92 92 93 93 if (params) { 94 if ( typeof params == 'string') params = params.toQueryParams();94 if (Object.isString(params)) params = params.toQueryParams(); 95 95 Object.extend(options.parameters, params); 96 96 } 97 97 -
src/hash.js
old new 1 1 var Hash = function(object) { 2 if ( object instanceof Hash) this.merge(object);2 if (Object.isHash(object)) this.merge(object); 3 3 else Object.extend(this, object || {}); 4 4 }; 5 5 … … 12 12 if (!pair.key) return; 13 13 var value = pair.value; 14 14 15 if (value && typeof value == 'object') {16 if ( value.constructor == Array) value.each(function(value) {15 if (value && Object.isObject(value)) { 16 if (Object.isArray(value)) value.each(function(value) { 17 17 parts.add(pair.key, value); 18 18 }); 19 19 return; … … 28 28 var results = []; 29 29 this.prototype._each.call(object, function(pair) { 30 30 var value = Object.toJSON(pair.value); 31 if ( value !== undefined) results.push(pair.key.toJSON() + ':' + value);31 if (Object.isDefined(value)) results.push(pair.key.toJSON() + ':' + value); 32 32 }); 33 33 return '{' + results.join(',') + '}'; 34 34 } 35 35 }); 36 36 37 37 Hash.toQueryString.addPair = function(key, value, prefix) { 38 if ( value == null) return;38 if (Object.isNullOrUndefined(value)) return; 39 39 key = encodeURIComponent(key); 40 this.push(key + '=' + ( value == null? '' : encodeURIComponent(value)));40 this.push(key + '=' + (Object.isNullOrUndefined(value) ? '' : encodeURIComponent(value))); 41 41 } 42 42 43 43 Object.extend(Hash.prototype, Enumerable); … … 73 73 var result; 74 74 for(var i = 0, length = arguments.length; i < length; i++) { 75 75 var value = this[arguments[i]]; 76 if ( value !== undefined){77 if ( result === undefined) result = value;76 if (Object.isDefined(value)){ 77 if (Object.isUndefined(result)) result = value; 78 78 else { 79 if ( result.constructor != Array) result = [result];79 if (!Object.isArray(result)) result = [result]; 80 80 result.push(value) 81 81 } 82 82 } … … 101 101 }); 102 102 103 103 function $H(object) { 104 if ( object instanceof Hash) return object;104 if (Object.isHash(object)) return object; 105 105 return new Hash(object); 106 106 }; 107 107