diff --git a/CHANGELOG b/CHANGELOG index 531481a..5ccc748 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,12 @@ *SVN* +* Change Element.insert syntax to allow multiple positions. [Thomas Fuchs] + Examples: + Element.insert('foo', {top:'bar', bottom:'baz'}); + $('foo').insert({after: new Element('p').update('bar')}); + Element.insert('foo', new Element('p').update('bar')); // defaults to bottom + Element.insert('foo', 'bar'); // defaults to bottom + * String.prototype.truncate now explicitly converts its return value into a string if no truncation takes place. This prevents possible issues with methods expecting input data that is typeof == 'string'. [Thomas Fuchs, Tobie Langel, Sam Stephenson] * Event.findElement behaves as expected when the element passed matches the given selector. Closes #8395. [Mislav Marohnić, Tobie Langel] diff --git a/src/ajax.js b/src/ajax.js index 94840e6..a868239 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -261,7 +261,7 @@ Object.extend(Object.extend(Ajax.Updater.prototype, Ajax.Request.prototype), { if (receiver = $(receiver)) { if (options.insertion) { if (typeof options.insertion == 'string') - receiver.insert(response, options.insertion); + receiver.insert( {}[options.insertion] = response ); else options.insertion(receiver, response); } else receiver.update(response); diff --git a/src/deprecated.js b/src/deprecated.js index 5da4043..b320049 100644 --- a/src/deprecated.js +++ b/src/deprecated.js @@ -6,19 +6,19 @@ Element.Methods.childOf = Element.Methods.descendantOf; var Insertion = { Before: function(element, content) { - return Element.insert(element, content, 'before'); + return Element.insert(element, {before:content}); }, Top: function(element, content) { - return Element.insert(element, content, 'top'); + return Element.insert(element, {top:content}); }, Bottom: function(element, content) { - return Element.insert(element, content, 'bottom'); + return Element.insert(element, {bottom:content}); }, After: function(element, content) { - return Element.insert(element, content, 'after'); + return Element.insert(element, {after:content}); } } diff --git a/src/dom.js b/src/dom.js index b17999b..b2d7f2b 100644 --- a/src/dom.js +++ b/src/dom.js @@ -105,23 +105,33 @@ Element.Methods = { return element; }, - insert: function(element, content, position) { + insert: function(element, insertions) { element = $(element); - position = (position || 'bottom').toLowerCase(); - var t = Element._insertionTranslations[position], range; + + if(typeof insertions == 'string' || insertions.ownerDocument === document) + insertions = {bottom:insertions}; - if (content && content.ownerDocument === document) { - t.insert(element, content); - return element; + var content, t, range; + + for (position in insertions) { + content = insertions[position]; + position = position.toLowerCase(); + t = Element._insertionTranslations[position]; + + if (content && content.ownerDocument === document) { + t.insert(element, content); + continue; + } + + content = content.toString(); + + range = element.ownerDocument.createRange(); + t.initializeRange(element, range); + t.insert(element, range.createContextualFragment(content.stripScripts())); + + content.evalScripts.bind(content).defer(); } - content = content.toString(); - - range = element.ownerDocument.createRange(); - t.initializeRange(element, range); - t.insert(element, range.createContextualFragment(content.stripScripts())); - - content.evalScripts.bind(content).defer(); return element; }, @@ -622,28 +632,38 @@ Element._attributeTranslations = { if (!document.createRange || Prototype.Browser.Opera) { - Element.Methods.insert = function(element, content, position) { + Element.Methods.insert = function(element, insertions) { element = $(element); - position = (position || 'bottom').toLowerCase(); - var t = Element._insertionTranslations, pos = t[position], tagName; - if (content && content.ownerDocument === document) { - pos.insert(element, content); - return element; + if(typeof insertions == 'string' || insertions.ownerDocument === document) + insertions = {bottom:insertions}; + + var t = Element._insertionTranslations, content, position, pos, tagName; + + for (position in insertions) { + content = insertions[position]; + position = position.toLowerCase(); + pos = t[position]; + + if (content && content.ownerDocument === document) { + pos.insert(element, content); + continue; + } + + content = content.toString(); + tagName = ((position == 'before' || position == 'after') + ? element.parentNode : element).tagName.toUpperCase(); + + if (t.tags[tagName]) { + var fragments = Element._getContentFromAnonymousElement(tagName, content.stripScripts()); + if (position == 'top' || position == 'after') fragments.reverse(); + fragments.each(pos.insert.curry(element)); + } + else element.insertAdjacentHTML(pos.adjacency, content.stripScripts()); + + content.evalScripts.bind(content).defer(); } - - content = content.toString(); - tagName = ((position == 'before' || position == 'after') - ? element.parentNode : element).tagName.toUpperCase(); - if (t.tags[tagName]) { - var fragments = Element._getContentFromAnonymousElement(tagName, content.stripScripts()); - if (position == 'top' || position == 'after') fragments.reverse(); - fragments.each(pos.insert.curry(element)); - } - else element.insertAdjacentHTML(pos.adjacency, content.stripScripts()); - - content.evalScripts.bind(content).defer(); return element; } } diff --git a/test/unit/dom.html b/test/unit/dom.html index 55e9425..8813484 100644 --- a/test/unit/dom.html +++ b/test/unit/dom.html @@ -292,6 +292,7 @@
some content.
some content.
some content.
some content.