* Element.update(@element[, newContent]) -> Element * * Replaces _the content_ of `element` with the `newContent` argument and * returns `element`. * * `newContent` may be in any of these forms: * - [[String]]: A string of HTML to be parsed and rendered * - [[Element]]: An
(element, content)
| 634 | * // -> 'I am a fruit and my name is "apple".' |
| 635 | **/ |
| 636 | function update(element, content) { |
| 637 | element = $(element); |
| 638 | |
| 639 | // Purge the element's existing contents of all storage keys and |
| 640 | // event listeners, since said content will be replaced no matter |
| 641 | // what. |
| 642 | var descendants = element.getElementsByTagName('*'), |
| 643 | i = descendants.length; |
| 644 | while (i--) purgeElement(descendants[i]); |
| 645 | |
| 646 | if (content && content.toElement) |
| 647 | content = content.toElement(); |
| 648 | |
| 649 | if (Object.isElement(content)) |
| 650 | return element.update().insert(content); |
| 651 | |
| 652 | |
| 653 | content = Object.toHTML(content); |
| 654 | var tagName = element.tagName.toUpperCase(); |
| 655 | |
| 656 | if (tagName === 'SCRIPT' && SCRIPT_ELEMENT_REJECTS_TEXTNODE_APPENDING) { |
| 657 | // Scripts are not evaluated when updating a SCRIPT element. |
| 658 | element.text = content; |
| 659 | return element; |
| 660 | } |
| 661 | |
| 662 | if (ANY_INNERHTML_BUGGY) { |
| 663 | if (tagName in INSERTION_TRANSLATIONS.tags) { |
| 664 | while (element.firstChild) |
| 665 | element.removeChild(element.firstChild); |
| 666 | |
| 667 | var nodes = getContentFromAnonymousElement(tagName, content.stripScripts()); |
| 668 | for (var i = 0, node; node = nodes[i]; i++) |
| 669 | element.appendChild(node); |
| 670 | |
| 671 | } else if (LINK_ELEMENT_INNERHTML_BUGGY && Object.isString(content) && content.indexOf('<link') > -1) { |
| 672 | // IE barfs when inserting a string that beings with a LINK |
| 673 | // element. The workaround is to add any content to the beginning |
| 674 | // of the string; we'll be inserting a text node (see |
| 675 | // getContentFromAnonymousElement below). |
| 676 | while (element.firstChild) |
| 677 | element.removeChild(element.firstChild); |
| 678 | |
| 679 | var nodes = getContentFromAnonymousElement(tagName, |
| 680 | content.stripScripts(), true); |
| 681 | |
| 682 | for (var i = 0, node; node = nodes[i]; i++) |
| 683 | element.appendChild(node); |
| 684 | } else { |
| 685 | element.innerHTML = content.stripScripts(); |
| 686 | } |
| 687 | } else { |
| 688 | element.innerHTML = content.stripScripts(); |
| 689 | } |
| 690 | |
| 691 | content.evalScripts.bind(content).defer(); |
| 692 | return element; |
| 693 | } |
nothing calls this directly
no test coverage detected