/* Written by Steve Tucker, 2006, http://www.stevetucker.co.uk Full documentation can be found at http://www.stevetucker.co.uk/page-innerxhtml.php Released under the Creative Commons Attribution-Share Alike 3.0 License, http://creativecommons.org/licenses/by-sa/3.0/ Change Log ---------- 15/10/2006 v0.3 innerXHTML official release. 21/03/2007 v0.4 1. Third argument $appendage added (Steve Tucker & Stef Dawson, www.stefdawson.com) 2. $source argument accepts string ID (Stef Dawson) 3. IE6 'on' functions work (Stef Dawson & Steve Tucker) */ innerXHTML = function($source,$string,$appendage) { // (v0.4) Written 2006 by Steve Tucker, http://www.stevetucker.co.uk if (typeof($source) == 'string') $source = document.getElementById($source); if (!($source.nodeType == 1)) return false; var $children = $source.childNodes; var $xhtml = ''; if (!$string) { for (var $i=0; $i<$children.length; $i++) { if ($children[$i].nodeType == 3) { var $text_content = $children[$i].nodeValue; $text_content = $text_content.replace(//g,'>'); $xhtml += $text_content; } else if ($children[$i].nodeType == 8) { $xhtml += ''; } else { $xhtml += '<'+$children[$i].nodeName.toLowerCase(); var $attributes = $children[$i].attributes; for (var $j=0; $j<$attributes.length; $j++) { var $attName = $attributes[$j].nodeName.toLowerCase(); var $attValue = $attributes[$j].nodeValue; if ($attName == 'style' && $children[$i].style.cssText) { $xhtml += ' style="'+$children[$i].style.cssText.toLowerCase()+'"'; } else if ($attValue && $attName != 'contenteditable') { $xhtml += ' '+$attName+'="'+$attValue+'"'; } } $xhtml += '>'+innerXHTML($children[$i]); $xhtml += ''; } } } else { if (!$appendage) { while ($children.length>0) { $source.removeChild($children[0]); } $appendage = false; } $xhtml = $string; while ($string) { var $returned = translateXHTML($string); var $elements = $returned[0]; $string = $returned[1]; if ($elements) { if (typeof($appendage) == 'string') $appendage = document.getElementById($appendage); if (!($appendage.nodeType == 1)) $source.appendChild($elements); else $source.insertBefore($elements,$appendage); } } } return $xhtml; } function translateXHTML($string) { var $match = /^<\/[a-z0-9]{1,}>/i.test($string); if ($match) { var $return = Array; $return[0] = false; $return[1] = $string.replace(/^<\/[a-z0-9]{1,}>/i,''); return $return; } $match = /^<[a-z]{1,}/i.test($string); if ($match) { $string = $string.replace(/^/.test($string); if ($match) { $string = $string.replace(/^>/,''); var $child = true; while ($child) { var $returned = translateXHTML($string,false); $child = $returned[0]; if ($child) $new_element.appendChild($child); $string = $returned[1]; } } $string = $string.replace(/^\/>/,''); } } $match = /^[^<>]{1,}/i.test($string); if ($match && !$new_element) { var $text_content = $string.match(/^[^<>]{1,}/i)[0]; $text_content = $text_content.replace(/</g,'<'); $text_content = $text_content.replace(/>/g,'>'); var $new_element = document.createTextNode($text_content); $string = $string.replace(/^[^<>]{1,}/i,''); } $match = /^/i.test($string); if ($match && !$new_element) { if (document.createComment) { $string = $string.replace(/^{1,}/i); $text_content = $text_content[0].replace(/-->{1,1}$/,''); var $new_element = document.createComment($text_content); $string = $string.replace(/^[^<>]{1,}-->/i,''); } else $string = $string.replace(/^/i,''); } var $return = Array; $return[0] = $new_element; $return[1] = $string; return $return; }