-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfluentTranslationParser.min.js
1 lines (1 loc) · 4.34 KB
/
fluentTranslationParser.min.js
1
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(e.fluentTranslationParser={})}(this,function(e){"use strict";var n={area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,keygen:!0,link:!0,menuitem:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0},t=/([\w-]+)|=|(['"])([.\s\S]*?)\2/g,i=/(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g,r=Object.create?Object.create(null):{};function s(e,n,t,i,r,s){var a=n.indexOf("<",i),c=n.slice(i,-1===a?void 0:a);!s&&/^\s*$/.test(c)&&(c=" "),(!r&&a>-1&&t+e.length>=0||" "!==c)&&e.push({type:"text",content:c})}function a(e,n){switch(n.type){case"text":return e+n.content;case"tag":return e+="<"+n.name+(n.attrs?function(e){var n=[];for(var t in e)n.push(t+'="'+e[t]+'"');return n.length?" "+n.join(" "):""}(n.attrs):"")+(n.voidElement?"/>":">"),n.voidElement?e:e+n.children.reduce(a,"")+"</"+n.name+">"}}var c={parse:function(e,a){a||(a={}),a.components||(a.components=r);var c,f=[],o=-1,l=[],u={},p=!1;return e.replace(i,function(i,r){if(p){if(i!=="</"+c.name+">")return;p=!1}var d,g="/"!==i.charAt(1),h=0===i.indexOf("\x3c!--"),x=r+i.length,m=e.charAt(x);g&&!h&&(o++,"tag"===(c=function(e){var i,r=0,s=!0,a={type:"tag",name:"",voidElement:!1,attrs:{},children:[]};return e.replace(t,function(t){if("="===t)return s=!0,void r++;s?0===r?((n[t]||"/"===e.charAt(e.length-2))&&(a.voidElement=!0),a.name=t):(a.attrs[i]=t.replace(/^['"]|['"]$/g,""),i=void 0):(i&&(a.attrs[i]=i),i=t),r++,s=!1}),a}(i)).type&&a.components[c.name]&&(c.type="component",p=!0),c.voidElement||p||!m||"<"===m||s(c.children,e,o,x,a.ignoreWhitespace,a.ignoreCollapse),u[c.tagName]=c,0===o&&f.push(c),(d=l[o-1])&&d.children.push(c),l[o]=c),(h||!g||c.voidElement)&&(h||o--,!p&&"<"!==m&&m&&s(d=-1===o?f:l[o].children,e,o,x,a.ignoreWhitespace,a.ignoreCollapse))}),!f.length&&e.length&&s(f,e,0,0,a.ignoreWhitespace,a.ignoreCollapse),f},stringify:function(e){return e.reduce(function(e,n){return e+a("",n)},"")}};const f=["{[^$}[>]+}","{ *\\$[^}[>]+}","{[A-Z (]*\\$[^}[>]+}","{ *\\$[^}[]+->","\\*\\[[^[]+\\]","\\[[^[]+\\]","{","}"].join("|"),o=new RegExp(`(${f})`,"g");e.parse=function(e){const n=c.parse(`<dummyI18nTag>${e}</dummyI18nTag>`,{ignoreCollapse:!0});return function(e){!function e(n){n&&n.forEach(n=>{if("text"===n.type&&(n.content.indexOf("{")>-1||n.content.indexOf("[")>-1)){const e=n.content.split(o),t=e.length>1?e.reduce((e,n,t)=>{if(t%2==0)e.push({type:"text",content:n});else if(1===n.length&&"{"===n)e.push({type:"openingBracket",raw:n,prefix:"{",suffix:"",content:""});else if(1===n.length&&"}"===n)e.push({type:"closingBracket",raw:n,prefix:"",suffix:"}",content:""});else if(0===n.indexOf("{")&&n.indexOf("$")<0){const t=n.substring(1,n.length-1);e.push({type:"reference",raw:n,prefix:"{",suffix:"}",content:t,reference:t.trim()})}else if(0===n.indexOf("{")&&n.indexOf("$")>-1&&n.indexOf("->")<0&&n.indexOf("(")>-1){const t=n.substring(1,n.length-1);e.push({type:"variable",formatted:!0,raw:n,prefix:"{",suffix:"}",content:t,variable:t.substring(t.indexOf("$")+1,t.indexOf(",")).trim()})}else if(0===n.indexOf("{")&&n.indexOf("$")>-1&&n.indexOf("->")<0){const t=n.substring(1,n.length-1);e.push({type:"variable",raw:n,prefix:"{",suffix:"}",content:t,variable:t.trim().replace("$","")})}else if(0===n.indexOf("{")&&n.indexOf("$")>-1&&n.indexOf("->")>-1){const t=n.substring(1,n.length-2);e.push({type:"selector",raw:n,prefix:"{",suffix:"->",content:t,variable:t.trim().replace("$","")})}else if(0===n.indexOf("[")&&n.indexOf("*")<0){const t=n.substring(1,n.length-1);e.push({type:"variant",isDefault:!1,raw:n,prefix:"[",suffix:"]",content:t,variable:t.trim()})}else if(1===n.indexOf("[")&&0===n.indexOf("*")){const t=n.substring(2,n.length-1);e.push({type:"variant",isDefault:!0,raw:n,prefix:"*[",suffix:"]",content:t,variable:t.trim()})}return e},[]):[];n.children=t}n.children&&e(n.children)})}(e)}(n),n[0].children||[]},e.stringify=function(e){const n=[{type:"tag",name:"dummyI18nTag",voidElement:!1,attrs:void 0,children:e}],t=c.stringify(n);return t.substring(14,t.length-15)},e.astStats=function(e){const n={references:0,variables:0,selectors:0,tags:0};return function e(t){t&&t.forEach(t=>{"tag"===t.type&&n.tags++,"variable"===t.type&&n.variables++,"reference"===t.type&&n.references++,"selector"===t.type&&n.selectors++,t.children&&e(t.children)})}(e),n},Object.defineProperty(e,"__esModule",{value:!0})});