diff --git a/lua/unity/handler.lua b/lua/unity/handler.lua index 4e18a0c..82861e8 100644 --- a/lua/unity/handler.lua +++ b/lua/unity/handler.lua @@ -1,6 +1,16 @@ local utils = require("unity.utils") local config = require("unity.config") +local function xml_escape_attr(value) + if not value then return "" end + value = value:gsub("&", "&") + value = value:gsub("<", "<") + value = value:gsub(">", ">") + value = value:gsub('"', """) + value = value:gsub("'", "'") + return value +end + local XmlCsprojHandler = {} XmlCsprojHandler.__index = XmlCsprojHandler @@ -90,33 +100,30 @@ function XmlCsprojHandler:checkProjectCapability(attribute) return false end --- Função para adicionar uma nova Compile tag function XmlCsprojHandler:addCompileTag(value) - -- Protege o valor para uso em pattern - local escapedValue = value:gsub("([%.%+%-%*%?%^%$%(%)%[%]%%])", "%%%1") - local existingPattern = "" - - -- Evita duplicação - if self.content:match(existingPattern) then - return false, "[NvimUnity] Script already added in Unity project" - end - - -- Se placeholder existe, insere nele - local placeholderPattern = "" - if self.content:match(placeholderPattern) then - local newLine = ' \n ' - self.content = self.content:gsub(placeholderPattern, newLine, 1) - return true, "[NvimUnity] Script added to Unity project" - end - - -- Se não existe placeholder, adiciona bloco novo com placeholder e tag - local newItemGroup = " \n" - .. "\n" - .. "\n" - .. ' \n' - .. " " + local encoded = xml_escape_attr(value) + + local patternValue = encoded:gsub("([%.%+%-%*%?%^%$%(%)%[%]%%])", "%%%1") + local existingPattern = "" + + if self.content:match(existingPattern) then + return false, "[NvimUnity] Script already added in Unity project" + end + + local placeholderPattern = "" + if self.content:match(placeholderPattern) then + local newLine = ' \n ' + self.content = self.content:gsub(placeholderPattern, newLine, 1) + return true, "[NvimUnity] Script added to Unity project" + end + + local newItemGroup = " \n" + .. "\n" + .. "\n" + .. ' \n' + .. " " -- Extrai a tag local openTag, innerContent, closeTag = self.content:match("()") @@ -273,8 +280,9 @@ function XmlCsprojHandler:resetCompileTags() -- Criar novo bloco local newCompileTags = {} for _, file in ipairs(files) do - local cutFile = utils.cutPath(utils.uriToPath(file), "Assets") - table.insert(newCompileTags, ' ') + local cutFile = utils.cutPath(utils.uriToPath(file), "Assets") + local encoded = xml_escape_attr(cutFile) + table.insert(newCompileTags, ' ') end local newBlock = table.concat(newCompileTags, "\n") -- Procurar placeholder