UNPKG

php-htx-cli

Version:

A cli to convert .htx (different components) files to .php

125 lines (124 loc) 4.97 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isTag = void 0; const regex_1 = __importDefault(require("./regex")); function isWrongTagClose(type, code, index) { if (type == "/>") return code[index - 1] != "-" && code[index - 1] != "/" && code[index] == ">"; return code[index - 2] != "-" && code[index - 1] == "/" && code[index] == ">"; } function isRightTagClose(type, code, index) { if (type == "/>") return code[index - 1] != "-" && code[index] == "/" && code[index + 1] == ">"; return code[index - 1] != "-" && code[index - 1] != "/" && code[index] == ">"; } function isSpace(code, index) { return [" ", "\t", "\r", "\n"].includes(code[index]); } function isTag(type, code, index = 0) { let startIndex = index; let match; if ((match = code.slice(index).match(regex_1.default.componentTag.open)) == null) return false; let tagname = match[0].slice(1); index += match[0].length; let expectSpace = true; let attr = {}; let existSuccess = false; let partIndex; for (partIndex = index; partIndex < code.length; partIndex++) { if (isWrongTagClose(type, code, partIndex)) return false; if (isRightTagClose(type, code, partIndex)) { existSuccess = true; partIndex += (type == "/>") ? 2 : 1; break; } if (expectSpace && !isSpace(code, partIndex)) return false; if (expectSpace) { expectSpace = false; continue; } if ((match = code.slice(partIndex).match(regex_1.default.attributeStart)) != null) { let attrName = match[0].slice(0, -1); expectSpace = true; partIndex += match[0].length - 1; let nestLevel = 0; let isInString = false; let skipNext = false; let charIndex = 1; let output = ""; while (partIndex + charIndex < code.length) { if (code[partIndex + charIndex] == " " && nestLevel == 0 && isInString == false) { break; } if (code[partIndex + charIndex] == "(" && isInString == false) { nestLevel++; } if (code[partIndex + charIndex] == ")" && isInString == false) { nestLevel--; } if (code[partIndex + charIndex] == "{" && isInString == false) { nestLevel++; } if (code[partIndex + charIndex] == "}" && isInString == false) { nestLevel--; } if (code[partIndex + charIndex] == "[" && isInString == false) { nestLevel++; } if (code[partIndex + charIndex] == "]" && isInString == false) { nestLevel--; } if (code[partIndex + charIndex] == "\"" && isInString == false && !skipNext) { isInString = "\""; } else if (code[partIndex + charIndex] == "\"" && isInString == "\"" && !skipNext) { isInString = false; } if (code[partIndex + charIndex] == "'" && isInString == false && !skipNext) { isInString = "'"; } else if (code[partIndex + charIndex] == "'" && isInString == "'" && !skipNext) { isInString = false; } if (code[partIndex + charIndex] == "\\" && isInString != false) { skipNext = true; } else { skipNext = false; } output += code[partIndex + charIndex]; charIndex++; if (nestLevel == 0 && isInString == false && code.slice(partIndex + charIndex).startsWith((type == "/>") ? "/>" : ">") && code[partIndex + charIndex - 1] != "-") { if (type == ">" && code[partIndex + charIndex - 1] == "/") continue; partIndex += charIndex - 1; attr[attrName] = output; return { size: (partIndex - startIndex) + ((type == "/>") ? 3 : 2), attr, tagname }; } } partIndex += charIndex - 1; attr[attrName] = output; continue; } if (![" ", "\t", "\r", "\n"].includes(code[partIndex])) return false; } if (!existSuccess) return false; return { size: partIndex - startIndex, attr, tagname }; } exports.isTag = isTag;