| Current Path : /var/www/homesaver/www/bitrix/js/ui/bbcode/ast-processor/dist/ |
| Current File : /var/www/homesaver/www/bitrix/js/ui/bbcode/ast-processor/dist/ast-processor.bundle.js.map |
{"version":3,"file":"ast-processor.bundle.js","sources":["../../shared/src/get-by-index.js","../src/ast-processor.js"],"sourcesContent":["import { Type } from 'main.core';\n\nexport function getByIndex<T>(array: Array<T>, index: number): ?T\n{\n\tif (!Type.isArray(array))\n\t{\n\t\tthrow new TypeError('array is not a array');\n\t}\n\n\tif (!Type.isInteger(index))\n\t{\n\t\tthrow new TypeError('index is not a integer');\n\t}\n\n\tconst preparedIndex = index < 0 ? array.length + index : index;\n\n\treturn array[preparedIndex];\n}\n","import { Type } from 'main.core';\nimport { typeof BBCodeNode } from 'ui.bbcode.model';\nimport { getByIndex } from '../../shared';\n\ntype ParsedSelector = {\n\tnodeName: string,\n\tprops: Array<{key: string, value: any}>,\n};\n\nexport class AstProcessor\n{\n\t/**\n\t * Makes flat list from AST\n\t */\n\tstatic flattenAst(ast): Array<any>\n\t{\n\t\tif (ast && ast.getChildren)\n\t\t{\n\t\t\tconst children = ast.getChildren();\n\n\t\t\treturn [\n\t\t\t\t...children,\n\t\t\t\t...children.flatMap((node) => {\n\t\t\t\t\treturn AstProcessor.flattenAst(node);\n\t\t\t\t}),\n\t\t\t];\n\t\t}\n\n\t\treturn [];\n\t}\n\n\t/**\n\t * Parses selector\n\t */\n\tstatic parseSelector(selector: string): Array<ParsedSelector | '>'>\n\t{\n\t\tconst regex = /(\\w+)\\[(.*?)]|\\s*(>)\\s*|\\w+/g;\n\t\tconst matches = [...selector.matchAll(regex)];\n\n\t\treturn matches.map(([fullMatch: string, nodeName: ?string, rawProps: ?string, arrow: ?string]) => {\n\t\t\tif (arrow)\n\t\t\t{\n\t\t\t\treturn '>';\n\t\t\t}\n\n\t\t\tif (rawProps)\n\t\t\t{\n\t\t\t\tconst propsRegexp = /(\\w+)=[\"'](.*?)[\"']/g;\n\t\t\t\tconst propsMatches = [...rawProps.matchAll(propsRegexp)];\n\t\t\t\tconst props = propsMatches.map(([, key: string, value: string]) => {\n\t\t\t\t\treturn [key, value];\n\t\t\t\t});\n\n\t\t\t\treturn {\n\t\t\t\t\tnodeName,\n\t\t\t\t\tprops,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tnodeName: fullMatch,\n\t\t\t\tprops: [],\n\t\t\t};\n\t\t});\n\t}\n\n\t/**\n\t * @private\n\t */\n\tstatic matchesNodeWithSelector(node, selector: ParsedSelector): boolean\n\t{\n\t\tif (node && node.constructor.name === selector.nodeName)\n\t\t{\n\t\t\tif (selector.props.length > 0)\n\t\t\t{\n\t\t\t\treturn selector.props.every(([key, value]) => {\n\t\t\t\t\tconst propValue = (() => {\n\t\t\t\t\t\tconst name = `${key.charAt(0).toUpperCase()}${key.slice(1)}`;\n\t\t\t\t\t\tif (Type.isFunction(node[`get${name}`]))\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\treturn node[`get${name}`]();\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (Type.isFunction(node[`is${name}`]))\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\treturn node[`is${name}`]();\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t})();\n\n\t\t\t\t\tif (['true', 'false'].includes(value))\n\t\t\t\t\t{\n\t\t\t\t\t\treturn propValue === (value === 'true');\n\t\t\t\t\t}\n\n\t\t\t\t\treturn propValue === value;\n\t\t\t\t});\n\t\t\t}\n\n\t\t\treturn true;\n\t\t}\n\n\t\treturn false;\n\t}\n\n\t/**\n\t * Finds parent node by parsed selector\n\t */\n\tstatic findParentNode(node, selector: ParsedSelector | string): ?any\n\t{\n\t\tif (node)\n\t\t{\n\t\t\tconst preparedSelector = (() => {\n\t\t\t\tif (Type.isStringFilled(selector))\n\t\t\t\t{\n\t\t\t\t\treturn AstProcessor.parseSelector(selector)[0];\n\t\t\t\t}\n\n\t\t\t\treturn selector;\n\t\t\t})();\n\t\t\tconst parent = node.getParent();\n\n\t\t\tif (AstProcessor.matchesNodeWithSelector(parent, preparedSelector))\n\t\t\t{\n\t\t\t\treturn parent;\n\t\t\t}\n\n\t\t\treturn AstProcessor.findParentNode(parent, preparedSelector);\n\t\t}\n\n\t\treturn null;\n\t}\n\n\tstatic findParentNodeByName(node: BBCodeNode, name: string): BBCodeNode | null\n\t{\n\t\tif (node)\n\t\t{\n\t\t\tconst parent: ?BBCodeNode = node.getParent();\n\t\t\tif (parent && parent.getName() === name)\n\t\t\t{\n\t\t\t\treturn parent;\n\t\t\t}\n\n\t\t\treturn AstProcessor.findParentNodeByName(parent, name);\n\t\t}\n\n\t\treturn null;\n\t}\n\n\t/**\n\t * Find elements by selector\n\t */\n\tstatic findElements(ast, selector: string): Array<any>\n\t{\n\t\tconst flattenedAst = AstProcessor.flattenAst(ast);\n\t\tconst parsedSelector = AstProcessor.parseSelector(selector);\n\t\tconst lastSelector = getByIndex(parsedSelector, -1);\n\n\t\tlet checkClosestParent = false;\n\n\t\treturn parsedSelector.reduceRight((acc: Array<any>, currentSelector: ParsedSelector) => {\n\t\t\tif (Type.isPlainObject(currentSelector))\n\t\t\t{\n\t\t\t\tif (currentSelector === lastSelector)\n\t\t\t\t{\n\t\t\t\t\treturn acc.filter((node) => {\n\t\t\t\t\t\treturn AstProcessor.matchesNodeWithSelector(node, currentSelector);\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tif (checkClosestParent)\n\t\t\t\t{\n\t\t\t\t\tcheckClosestParent = false;\n\n\t\t\t\t\treturn acc.filter((node) => {\n\t\t\t\t\t\treturn AstProcessor.matchesNodeWithSelector(node.getParent(), currentSelector);\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\treturn acc.filter((node) => {\n\t\t\t\t\treturn AstProcessor.findParentNode(node, currentSelector) !== null;\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tif (currentSelector === '>')\n\t\t\t{\n\t\t\t\tcheckClosestParent = true;\n\t\t\t}\n\n\t\t\treturn acc;\n\t\t}, flattenedAst);\n\t}\n\n\t/**\n\t * Reduces AST\n\t */\n\tstatic reduceAst(ast, reducer: (node: any, children?: Array<any>) => any | null): any\n\t{\n\t\tconst children = ast.getChildren?.().reduce((acc, child) => {\n\t\t\tconst preparedChild = [AstProcessor.reduceAst(child, reducer)].flat();\n\t\t\tif (!Type.isNil(preparedChild))\n\t\t\t{\n\t\t\t\tacc.replaceChild(child, ...preparedChild);\n\t\t\t}\n\n\t\t\treturn acc;\n\t\t}, ast);\n\n\t\treturn reducer(ast, children);\n\t}\n}\n"],"names":["getByIndex","array","index","Type","isArray","TypeError","isInteger","preparedIndex","length","AstProcessor","flattenAst","ast","getChildren","children","flatMap","node","parseSelector","selector","regex","matches","matchAll","map","fullMatch","nodeName","rawProps","arrow","propsRegexp","propsMatches","props","key","value","matchesNodeWithSelector","constructor","name","every","propValue","charAt","toUpperCase","slice","isFunction","includes","findParentNode","preparedSelector","isStringFilled","parent","getParent","findParentNodeByName","getName","findElements","flattenedAst","parsedSelector","lastSelector","checkClosestParent","reduceRight","acc","currentSelector","isPlainObject","filter","reduceAst","reducer","reduce","child","preparedChild","flat","isNil","replaceChild"],"mappings":";;;;;;CAEO,SAASA,UAAU,CAAIC,KAAe,EAAEC,KAAa,EAC5D;GACC,IAAI,CAACC,cAAI,CAACC,OAAO,CAACH,KAAK,CAAC,EACxB;KACC,MAAM,IAAII,SAAS,CAAC,sBAAsB,CAAC;;GAG5C,IAAI,CAACF,cAAI,CAACG,SAAS,CAACJ,KAAK,CAAC,EAC1B;KACC,MAAM,IAAIG,SAAS,CAAC,wBAAwB,CAAC;;GAG9C,MAAME,aAAa,GAAGL,KAAK,GAAG,CAAC,GAAGD,KAAK,CAACO,MAAM,GAAGN,KAAK,GAAGA,KAAK;GAE9D,OAAOD,KAAK,CAACM,aAAa,CAAC;CAC5B;;CCRO,MAAME,YAAY,CACzB;;CAEA;CACA;GACC,OAAOC,UAAU,CAACC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