Skip to content

Instantly share code, notes, and snippets.

@bga
Created February 25, 2010 18:18
Show Gist options
  • Save bga/314850 to your computer and use it in GitHub Desktop.
Save bga/314850 to your computer and use it in GitHub Desktop.
/**
@fn construct HTMLDocumentFragment or HTMLElement from 'html' string in 'doc'
@param html string that contains html
@param doc owner HTMLDocument for result. Optional
@return HTMLDocumentFragment or HTMLElement depend from 'html'
*/
$jb.DOMNode._fromHTML=null;
if("applyElement" in $h)
{
$jb.DOMNode._fromHTML=function(html,doc)
{
if(doc==null)
doc=$d;
var div=doc.createElement("div");
div.innerHTML=html+"";
if(div.childNodes.length==1)
return div.removeChild(div.firstChild);
var df=doc.createDocumentFragment();
var div2=doc.createElement("div");
df.appendChild(div);
div2.applyElement(div,"inside");
return df;
};
}
else
{
$jb.DOMNode._fromHTML=function(html,doc)
{
if(doc==null)
doc=$d;
var div=doc.createElement("div");
div.innerHTML=html+"";
if(div.childNodes.length==1)
return div.removeChild(div.firstChild);
var df=doc.createDocumentFragment();
var tmp;
while((tmp=div.firstChild)!=null)
df.appendChild(tmp);
return df;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment