UNPKG

txml

Version:

fastest XML DOM Parser for node/browser/worker

2 lines (1 loc) 4.68 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).txml={})}(this,(function(t){"use strict";function e(t,e){var i=(e=e||{}).pos||0,a=!!e.keepComments,o=!!e.keepWhitespace,s="<".charCodeAt(0),f=">".charCodeAt(0),c="-".charCodeAt(0),u="/".charCodeAt(0),l="!".charCodeAt(0),h="'".charCodeAt(0),d='"'.charCodeAt(0),g="[".charCodeAt(0),p="]".charCodeAt(0);function m(e){for(var r=[];t[i];)if(t.charCodeAt(i)==s){if(t.charCodeAt(i+1)===u){var n=i+2;if(i=t.indexOf(">",i),-1==t.substring(n,i).indexOf(e)){var h=t.substring(0,i).split("\n");throw new Error("Unexpected close tag\nLine: "+(h.length-1)+"\nColumn: "+(h[h.length-1].length+1)+"\nChar: "+t[i])}return i+1&&(i+=1),r}if(t.charCodeAt(i+1)===l){if(t.charCodeAt(i+2)==c){const e=i;for(;-1!==i&&(t.charCodeAt(i)!==f||t.charCodeAt(i-1)!=c||t.charCodeAt(i-2)!=c||-1==i);)i=t.indexOf(">",i+1);-1===i&&(i=t.length),a&&r.push(t.substring(e,i+1))}else{if(t.charCodeAt(i+2)===g&&t.charCodeAt(i+8)===g&&"cdata"===t.substr(i+3,5).toLowerCase()){var d=t.indexOf("]]>",i);-1==d?(r.push(t.substr(i+9)),i=t.length):(r.push(t.substring(i+9,d)),i=d+3);continue}{const e=i+1;i+=2;for(var m=!1;(t.charCodeAt(i)!==f||!0===m)&&t[i];)t.charCodeAt(i)===g?m=!0:!0===m&&t.charCodeAt(i)===p&&(m=!1),i++;r.push(t.substring(e,i))}}i++;continue}var v=A();r.push(v),"?"===v.tagName[0]&&(r.push(...v.children),v.children=[])}else{var C=b();if(o)C.length>0&&r.push(C);else{var y=C.trim();y.length>0&&r.push(y)}i++}return r}function b(){var e=i;return-2===(i=t.indexOf("<",i)-1)&&(i=t.length),t.slice(e,i+1)}function v(){for(var e=i;-1==="\r\n\t>/= ".indexOf(t[i])&&t[i];)i++;return t.slice(e,i)}var C=e.noChildNodes||["img","br","input","meta","link","hr"];function A(){i++;const e=v(),r={};let n=[];for(;t.charCodeAt(i)!==f&&t[i];){var a=t.charCodeAt(i);if(a>64&&a<91||a>96&&a<123){for(var o=v(),s=t.charCodeAt(i);s&&s!==h&&s!==d&&!(s>64&&s<91||s>96&&s<123)&&s!==f;)i++,s=t.charCodeAt(i);if(s===h||s===d){var c=y();if(-1===i)return{tagName:e,attributes:r,children:n}}else c=null,i--;r[o]=c}i++}if(t.charCodeAt(i-1)!==u)if("script"==e){var l=i+1;i=t.indexOf("<\/script>",i),n=[t.slice(l,i)],i+=9}else if("style"==e){l=i+1;i=t.indexOf("</style>",i),n=[t.slice(l,i)],i+=8}else-1===C.indexOf(e)?(i++,n=m(e)):i++;else i++;return{tagName:e,attributes:r,children:n}}function y(){var e=t[i],r=i+1;return i=t.indexOf(e,r),t.slice(r,i)}var x,N=null;if(void 0!==e.attrValue){e.attrName=e.attrName||"id";for(N=[];-1!==(x=void 0,x=new RegExp("\\s"+e.attrName+"\\s*=['\"]"+e.attrValue+"['\"]").exec(t),i=x?x.index:-1);)-1!==(i=t.lastIndexOf("<",i))&&N.push(A()),t=t.substr(i),i=0}else N=e.parseNode?A():m("");return e.filter&&(N=n(N,e.filter)),e.simplify?r(Array.isArray(N)?N:[N]):(e.setPos&&(N.pos=i),N)}function r(t){var e={};if(!t.length)return"";if(1===t.length&&"string"==typeof t[0])return t[0];for(var n in t.forEach((function(t){if("object"==typeof t){e[t.tagName]||(e[t.tagName]=[]);var n=r(t.children);e[t.tagName].push(n),Object.keys(t.attributes).length&&"string"!=typeof n&&(n._attributes=t.attributes)}})),e)1==e[n].length&&(e[n]=e[n][0]);return e}function n(t,e,r=0,i=""){var a=[];return t.forEach((function(t,o){if("object"==typeof t&&e(t,o,r,i)&&a.push(t),t.children){var s=n(t.children,e,r+1,(i?i+".":"")+o+"."+t.tagName);a=a.concat(s)}})),a}t.filter=n,t.getElementById=function(t,r,n){var i=e(t,{attrValue:r});return n?tXml.simplify(i):i[0]},t.getElementsByClassName=function(t,r,n){const i=e(t,{attrName:"class",attrValue:"[a-zA-Z0-9- ]*"+r+"[a-zA-Z0-9- ]*"});return n?tXml.simplify(i):i},t.parse=e,t.simplify=r,t.simplifyLostLess=function t(e,r={}){var n={};return e.length?1===e.length&&"string"==typeof e[0]?Object.keys(r).length?{_attributes:r,value:e[0]}:e[0]:(e.forEach((function(e){if("object"==typeof e){n[e.tagName]||(n[e.tagName]=[]);var r=t(e.children||[],e.attributes);n[e.tagName].push(r),Object.keys(e.attributes).length&&(r._attributes=e.attributes)}})),n):n},t.stringify=function(t){var e="";function r(t){if(t)for(var r=0;r<t.length;r++)"string"==typeof t[r]?e+=t[r].trim():n(t[r])}function n(t){for(var n in e+="<"+t.tagName,t.attributes)null===t.attributes[n]?e+=" "+n:-1===t.attributes[n].indexOf('"')?e+=" "+n+'="'+t.attributes[n].trim()+'"':e+=" "+n+"='"+t.attributes[n].trim()+"'";"?"!==t.tagName[0]?(e+=">",r(t.children),e+="</"+t.tagName+">"):e+="?>"}return r(t),e},t.toContentString=function t(e){if(Array.isArray(e)){var r="";return e.forEach((function(e){r=(r+=" "+t(e)).trim()})),r}return"object"==typeof e?t(e.children):" "+e},Object.defineProperty(t,"__esModule",{value:!0})}));