From e26693e3f64b55b2ec2f7ae61e3112648d9ac5ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 22 Dec 2023 15:25:12 +0100 Subject: [PATCH] Sync and improve regex (#2959) We also use the regex on the JavaScript side, so it provides better validation it works as desired. --- assets/js/phoenix_live_view/rendered.js | 67 +++++++++---------------- lib/phoenix_live_view/test/dom.ex | 4 +- 2 files changed, 26 insertions(+), 45 deletions(-) diff --git a/assets/js/phoenix_live_view/rendered.js b/assets/js/phoenix_live_view/rendered.js index 08d6487468..e464e9a6be 100644 --- a/assets/js/phoenix_live_view/rendered.js +++ b/assets/js/phoenix_live_view/rendered.js @@ -37,60 +37,41 @@ const VOID_TAGS = new Set([ "track", "wbr" ]) -const endingTagNameChars = new Set([">", "/", " ", "\n", "\t", "\r"]) const quoteChars = new Set(["'", '"']) export let modifyRoot = (html, attrs, clearInnerHTML) => { let i = 0 let insideComment = false let beforeTag, afterTag, tag, tagNameEndsAt, id, newHTML - while(i < html.length){ - let char = html.charAt(i) - if(insideComment){ - if(char === "-" && html.slice(i, i + 3) === "-->"){ - insideComment = false - i += 3 - } else { + + let lookahead = html.match(/^(\s*(?:\s*)*)<([^\s\/>]+)/) + if(lookahead === null) { throw new Error(`malformed html ${html}`) } + + i = lookahead[0].length + beforeTag = lookahead[1] + tag = lookahead[2] + tagNameEndsAt = i + + // Scan the opening tag for id, if there is any + for(i; i < html.length; i++){ + if(html.charAt(i) === ">" ){ break } + if(html.charAt(i) === "="){ + let isId = html.slice(i - 3, i) === " id" + i++; + let char = html.charAt(i) + if (quoteChars.has(char)) { + let attrStartsAt = i i++ - } - } else if(char === "<" && html.slice(i, i + 4) === ")?<[^\s\r\n\t/>]+)", + ~r/^(\s*(?:\s*)*)<([^\s\/>]+)/, IO.iodata_to_binary(head), - "\\1 #{@phx_component}=\"#{to_string(cid)}\"", + "\\0 #{@phx_component}=\"#{to_string(cid)}\"", global: false )