-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-element.min.js
1 lines (1 loc) · 986 Bytes
/
create-element.min.js
1
export default createElement=function(a={},b={}){const c={type:a.type||'div',id:a.id||null,style:a.style||null,attributes:a.attributes||null,src:a.src||null,link:a.link||null,content:a.content||null},{target:d=null,method:e='after',type:f='sibling'}=b;let g=document.createElement(c.type);if(c.id&&(g.id=c.id),'string'==typeof c.style&&(g.className=c.style),'object'==typeof c.style)for(let h in c.style){let i=h.replace(/-([a-z])/ig,(j,k)=>{return k.toUpperCase()});c.style.hasOwnProperty(h)&&(g.style[i]=c.style[h])}if('img'===c.type&&c.src&&(g.src=c.src),'a'===c.type&&c.link&&(g.href=c.link),c.attributes)for(let h in c.attributes)c.attributes.hasOwnProperty(h)&&g.setAttribute(h,c.attributes[h]);if(c.content&&(g.innerHTML=c.content),d){const h='container'===f?d:d.parentNode,i='before'===e&&'sibling'===f?d:'after'===e&&'sibling'===f?d.nextSibling:'before'===e&&'container'===f?d.firstChild:'after'===e&&'container'===f?d.lastChild.nextSibling:null;h.insertBefore(g,i)}return g};