UNPKG

htmr

Version:

Simple and lightweight (< 2kB) HTML to React converter that works in server and browser

2 lines (1 loc) 2.9 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):(e=e||self).htmr=t(e.React)}(this,(function(e){"use strict";function t(e,t,n,o){return void 0===t&&(t={}),Object.keys(t).reduce((function(a,i){if(/^on.*/.test(i))return a;var l=i;/^(data|aria)-/.test(i)||0===n.filter((function(e){return e instanceof RegExp?e.test(i):e===i})).length&&(l=r(i));var u,s,c=o(e,l);if("style"===c.name)a[c.name]=(u=t.style,s={},u.split(";").filter((function(e){return""!==e.trim()})).forEach((function(e){var t,n=e.split(":");if(n.length>1){var o=function(e){return/^-ms-/.test(e)&&(e=e.substr(1)),e.startsWith("--")?e:r(e)}(n[0].trim()),a=(t=n.slice(1).join(":").trim(),/^\d+$/.test(t)?Number(t):t.replace(/'/g,'"'));s[o]=a}})),s);else{var f=t[i],m=""===f||String(f).toLowerCase()===l.toLowerCase();a[c.name]=c.isBoolean?m:f}return a}),{})}function r(e){return e.replace(/(-|:)(.)/g,(function(e,t,r){return r.toUpperCase()}))}e=e&&e.hasOwnProperty("default")?e.default:e;var n=["table","tbody","thead","tfoot","tr"];var o={for:"htmlFor",class:"className",allowfullscreen:["allowFullScreen","allowFullscreen"],autocomplete:"autoComplete",autofocus:["autoFocus"],contenteditable:"contentEditable",spellcheck:"spellCheck",srcdoc:"srcDoc",srcset:"srcSet",itemscope:"itemScope",itemprop:"itemProp",itemtype:"itemType"};function a(e,t){var r=o[t],n=document.createElement(e);if(r)return{name:Array.isArray(r)?r[0]:r,isBoolean:i(n,Array.isArray(r)?r[1]||t:r)};for(var a in n)if(a.toLowerCase()===t.toLowerCase())return{name:a,isBoolean:i(n,a)};return{name:t,isBoolean:i(n,t)}}function i(e,t){return e.setAttribute(t,""),!0===e[t]||l.indexOf(t)>-1}var l=["itemScope"];function u(t,r,n,o){void 0===o&&(o=null);var a=n[t],i=n._;return a?e.createElement(a,r,o):i?i(t,r,o):e.createElement(t,r,o)}return function(e,r){if(void 0===r&&(r={}),"string"!=typeof e)throw new TypeError("Expected HTML string");var o=document.createElement("div");o.innerHTML=e.trim();var i=Array.from(o.childNodes).map((function(e,o){return function e(r,o,i){var l=i.transform||{};var s=i.preserveAttributes||[];var c=i.dangerouslySetChildren||["style"];var f=l._;if(8===r.nodeType)return null;if(3===r.nodeType){var m=r.textContent;return f?f(m):m}var d={};var p=r.attributes;for(var v=0;v<p.length;v++)d[p[v].name]=p[v].value;d.key=o.toString();var y=r.tagName.toLowerCase();var h=t(y,d,s,a);var g=Array.from(r.childNodes).map((function(t,r){return n.indexOf(y)>-1&&3===t.nodeType&&(t.textContent=t.textContent.trim(),""===t.textContent)?null:e(t,o+"."+r,i)})).filter(Boolean);if(c.indexOf(y)>-1){var C=r.innerHTML;return C&&("style"!==y&&"script"!==y&&(C=C.replace(/"/g,"&quot;")),h.dangerouslySetInnerHTML={__html:C.trim()}),u(y,h,l)}var b=0===g.length?null:g;return u(y,h,l,b)}(e,String(o),r)})).filter(Boolean);return 1===i.length?i[0]:i}}));