{"version":3,"sources":["parser.ts"],"sourcesContent":["import type { DOMContext } from \"../dom/DOMContext.ts\";\nimport { JSX_INSERT_STRING, type DOMUtils, appendableContent } from \"../datex-bindings/dom-utils.ts\";\n\nimport { Logger } from \"datex-core-legacy/datex_all.ts\";\nimport { getCallerFile, getCallerInfo } from \"datex-core-legacy/utils/caller_metadata.ts\";\nimport { Datex } from \"datex-core-legacy/mod.ts\";\nimport { client_type } from \"datex-core-legacy/utils/constants.ts\";\n\n\nconst logger = new Logger(\"JSX Parser\");\n\nexport const SET_DEFAULT_ATTRIBUTES: unique symbol = Symbol(\"SET_DEFAULT_ATTRIBUTES\");\nexport const SET_DEFAULT_CHILDREN: unique symbol = Symbol(\"SET_DEFAULT_CHILDREN\");\nexport const IS_TEMPLATE: unique symbol = Symbol(\"IS_TEMPLATE\");\n\n\nexport function escapeString(string:string) {\n\treturn {[JSX_INSERT_STRING]:true, val:string};\n}\n\nexport function getParseJSX(context: DOMContext, domUtils: DOMUtils) {\n\n\tfunction setChildren(element: Element, children: appendableContent|appendableContent[]|appendableContent[][], shadow_root: boolean) {\n\t\t// is nested arrays in outer array\n\t\tif (children instanceof Array && !Datex.ReactiveValue.isRef(children)) {\n\t\t\tfor (const child of children) {\n\t\t\t\tsetChildren(element, child, shadow_root);\n\t\t\t}\n\t\t}\n\n\t\telse if (shadow_root) {\n\t\t\tconst template = parseJSX(\"template\", {children, shadowrootmode:shadow_root});\n\t\t\tif (domUtils) domUtils.append(element, template);\n\t\t\telse element.append(template)\n\t\t}\n\t\telse {\n\t\t\tif (domUtils) domUtils.append(element, children as appendableContent|appendableContent[]);\n\t\t\telse element.append(...(children instanceof Array ? children : [children]) as appendableContent[])\n\t\t}\n\t}\n\n\n\tfunction initElement({element, children, props, shadow_root, set_default_children, set_default_attributes, allow_invalid_attributes, callerModule}: {\n\t\telement: Element,\n\t\tchildren: JSX.singleChild[],\n\t\tprops: { [x: string]: unknown; },\n\t\tshadow_root: boolean,\n\t\tset_default_children: boolean, \n\t\tset_default_attributes:boolean, \n\t\tallow_invalid_attributes: boolean,\n\t\tcallerModule?: string\n\t}) {\n\t\tif (set_default_children) setChildren(element, children, shadow_root);\n\n\t\tif (set_default_attributes) {\n\t\t\tlet module = ((<Record<string,unknown>>props)['module'] ?? (<Record<string,unknown>>props)['uix-module']) as string|null;\n\t\t\t// ignore module of is explicitly module===null, otherwise fallback to getCallerFile\n\t\t\t// TODO: optimize don't call getCallerFile for each nested jsx element, pass on from parent?\n\t\t\tif (module === undefined) {\n\t\t\t\t// module already determined\n\t\t\t\tif (callerModule) module = callerModule;\n\t\t\t\t// get caller module\n\t\t\t\telse {\n\t\t\t\t\tconst stack = getCallerInfo();\n\t\t\t\t\tif (stack && stack[1]) {\n\t\t\t\t\t\tmodule = (\n\t\t\t\t\t\t\tstack[1].name == \"jsxs\" ? // called via jsxs, skip one more in stack\n\t\t\t\t\t\t\t\tstack[2] : \n\t\t\t\t\t\t\t\tstack[1]\n\t\t\t\t\t\t\t)?.file;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (!module) {\n\t\t\t\t\tlogger.error(\"Could not determine location of JSX definition\")\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\tfor (const [attr,val] of Object.entries(props)) {\n\t\t\t\tif (attr == \"style\" && (element as HTMLElement).style) domUtils.setCSS(element as HTMLElement, <any> val);\n\t\t\t\telse {\n\t\t\t\t\tconst valid_attr = domUtils.setElementAttribute(element, attr, <any>val, module);\n\t\t\t\t\tif (!allow_invalid_attributes && !valid_attr)\n\t\t\t\t\t\tlogger.warn(`Attribute \"${attr}\" is not allowed for <${element.tagName.toLowerCase()}> element. If you believe this is a mistake, please open an issue at https://github.com/unyt-org/uix`);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\n\t\t// !important, cannot return directly because of stack problems, store in ptr variable first\n\t\tif (domUtils) {\n\t\t\tconst ptr = domUtils.addProxy(element);\n\t\t\treturn ptr;\n\t\t}\n\n\t\telse return element;\n\t}\n\n\n\n\tfunction parseJSX(type: string | typeof Element | typeof DocumentFragment | ((...args:unknown[])=>Element|DocumentFragment), params: Record<string,unknown>, isJSXS = false): Element {\n\n\t\tDatex.ReactiveValue.freezeCapturing = true;\n\t\tconst isTemplate = (type as any)[IS_TEMPLATE] ?? false;\n\t\tconst isComponent = typeof type == \"function\" && (context.HTMLElement.isPrototypeOf(type) || type === context.DocumentFragment || context.DocumentFragment.isPrototypeOf(type))\n\n\t\tlet element:Element;\n\n\t\t// normalize children if template/blankTemplate or component - not for plain functions\n\t\tif (isTemplate || isComponent) {\n\t\t\t// always convert children prop to array\n\t\t\tif ('children' in params && !(params.children instanceof Array)) params.children = [params.children];\n\t\t\t// always set children prop to empty array if not defined\n\t\t\tif (!('children' in params)) params.children = [];\n\t\t}\n\t\tconst { children = [], ...props } = params as Record<string,unknown> & {children:JSX.singleChild[]}\n\t\n\t\t// _debug property to debug jsx\n\t\tif (props._debug) {\n\t\t\tdelete props._debug;\n\t\t\tconsole.log(type,children,props,params)\n\t\t}\n\n\t\tlet set_default_children = true;\n\t\tlet set_default_attributes = true;\n\t\tlet allow_invalid_attributes = true;\n\t\n\t\tlet shadow_root = false;\n\t\tif (props['shadow-root']) {\n\t\t\tshadow_root = props['shadow-root']==true?'open':props['shadow-root'];\n\t\t\tdelete props['shadow-root'];\n\t\t}\n\n\t\n\t\t// replace ShadowRoot with shadow-root\n\t\tif (type === context.ShadowRoot) type = \"shadow-root\";\n\t\t\n\t\tif (typeof type === 'function') {\n\n\t\t\t// class component\n\t\t\tif (isComponent) {\n\t\t\t\tset_default_children = (type as any)[SET_DEFAULT_CHILDREN] ?? true;\n\t\t\t\tset_default_attributes = (type as any)[SET_DEFAULT_ATTRIBUTES] ?? true;\n\t\t\t\tif (set_default_children) delete params.children;\n\t\n\t\t\t\telement = new type(set_default_children ? props : {...props, children}) // uix component\n\t\t\t}\n\t\t\t// function component\n\t\t\telse {\n\t\t\t\tset_default_children = (type as any)[SET_DEFAULT_CHILDREN];\n\t\t\t\tset_default_attributes = (type as any)[SET_DEFAULT_ATTRIBUTES];\n\t\t\t\tif (set_default_children) delete params.children;\n\t\n\t\t\t\telement = type(params)\n\n\t\t\t\t// async component, use uix-placeholder\n\t\t\t\tif (element instanceof Promise) {\n\t\t\t\t\tconst placeholder = document.createElement(\"uix-placeholder\");\n\t\t\t\t\tdomUtils.addProxy(placeholder);\n\t\t\t\t\tconst callerModule = getCallerFile();\n\t\t\t\t\t(element as Promise<Element>).then(val => {\n\t\t\t\t\t\tplaceholder.replaceWith(initElement({\n\t\t\t\t\t\t\telement: val,\n\t\t\t\t\t\t\tchildren,\n\t\t\t\t\t\t\tprops,\n\t\t\t\t\t\t\tshadow_root, \n\t\t\t\t\t\t\tset_default_children, \n\t\t\t\t\t\t\tset_default_attributes, \n\t\t\t\t\t\t\tallow_invalid_attributes,\n\t\t\t\t\t\t\tcallerModule\n\t\t\t\t\t\t}))\n\t\t\t\t\t});\n\t\t\t\t\tDatex.ReactiveValue.freezeCapturing = false;\n\t\t\t\t\treturn placeholder;\n\t\t\t\t}\n\n\t\t\t}\n\t\t}\n\t\n\t\n\t\telse {\n\t\t\tallow_invalid_attributes = false;\n\n\t\t\t// frontend-slots only allowed on the backend\n\t\t\tif (type == \"frontend-slot\" && client_type !== \"deno\") {\n\t\t\t\tthrow new Error(\"<frontend-slot> elements are only allowed on the backend\");\n\t\t\t}\n\t\t\t\n\t\t\t// convert shadow-root to template\n\t\t\tif (type == \"shadow-root\") {\n\t\t\t\ttype = \"template\"\n\t\t\t\tprops.shadowrootmode = props.mode ?? \"open\";\n\t\t\t\tdelete props.mode\n\t\t\t}\n\t\t\t\n\t\t\telement = domUtils ? domUtils.createElement(type) : context.document.createElement(type)\n\t\t}\n\n\t\t// if (!(element instanceof context.Element || element instanceof context.DocumentFragment)) {\n\t\t// \tthrow new Error(\"Invalid JSX element, must be of type Element\")\n\t\t// }\n\n\t\tconst res = initElement({\n\t\t\telement,\n\t\t\tchildren,\n\t\t\tprops,\n\t\t\tshadow_root, \n\t\t\tset_default_children, \n\t\t\tset_default_attributes, \n\t\t\tallow_invalid_attributes\n\t\t});\n\t\tDatex.ReactiveValue.freezeCapturing = false;\n\t\treturn res;\n\t\t\n\t}\n\n\treturn parseJSX;\n}"],"names":["JSX_INSERT_STRING","Logger","getCallerFile","getCallerInfo","Datex","client_type","logger","SET_DEFAULT_ATTRIBUTES","Symbol","SET_DEFAULT_CHILDREN","IS_TEMPLATE","escapeString","string","val","getParseJSX","context","domUtils","setChildren","element","children","shadow_root","Array","ReactiveValue","isRef","child","template","parseJSX","shadowrootmode","append","initElement","props","set_default_children","set_default_attributes","allow_invalid_attributes","callerModule","module","undefined","stack","name","file","error","attr","Object","entries","style","setCSS","valid_attr","setElementAttribute","warn","tagName","toLowerCase","addProxy","type","params","isJSXS","freezeCapturing","isTemplate","isComponent","HTMLElement","isPrototypeOf","DocumentFragment","_debug","console","log","ShadowRoot","Promise","placeholder","document","createElement","then","replaceWith","Error","mode","res"],"mappings":"AACA,SAASA,iBAAiB,QAA0C,iCAAiC;AAErG,SAASC,MAAM,QAAQ,iCAAiC;AACxD,SAASC,aAAa,EAAEC,aAAa,QAAQ,6CAA6C;AAC1F,SAASC,KAAK,QAAQ,2BAA2B;AACjD,SAASC,WAAW,QAAQ,uCAAuC;AAGnE,IAAMC,SAAS,IAAIL,OAAO;AAE1B,OAAO,MAAMM,yBAAwCC,OAAO,0BAA0B;AACtF,OAAO,MAAMC,uBAAsCD,OAAO,wBAAwB;AAClF,OAAO,MAAME,cAA6BF,OAAO,eAAe;AAGhE,OAAO,SAASG,aAAaC,MAAa;IACzC,OAAO;QAAC,CAACZ,kBAAkB,EAAC,CAAA;QAAMa,KAAID;IAAM;AAC7C;AAEA,OAAO,SAASE,YAAYC,OAAmB,EAAEC,QAAkB;IAElE,SAASC,YAAYC,OAAgB,EAAEC,QAAqE,EAAEC,WAAoB;QAEjI,IAAID,oBAAoBE,SAAS,CAACjB,MAAMkB,aAAa,CAACC,KAA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