Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync and improve regex #2959

Merged
merged 3 commits into from
Dec 22, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Sync and improve regex
We also use the regex on the JavaScript side,
so it provides better validation it works as
desired.
josevalim committed Dec 21, 2023
commit cf673d26092bd35b6db8c3da9ac61ca5c992d6ee
71 changes: 29 additions & 42 deletions assets/js/phoenix_live_view/rendered.js
Original file line number Diff line number Diff line change
@@ -44,53 +44,40 @@ 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*)*<[^!]/)
if(lookahead === null) { throw new Error(`malformed html ${html}`) }

i = lookahead[0].length - 1
beforeTag = html.slice(0, i - 1)

for(i; i < html.length; i++){
if(endingTagNameChars.has(html.charAt(i))){ break }
}

tagNameEndsAt = i
tag = html.slice(beforeTag.length + 1, tagNameEndsAt)

// 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) === "<!--"){
insideComment = true
i += 4
} else if(char === "<"){
beforeTag = html.slice(0, i)
let iAtOpen = i
i++
for(i; i < html.length; i++){
if(endingTagNameChars.has(html.charAt(i))){ break }
}
tagNameEndsAt = i
tag = html.slice(iAtOpen + 1, tagNameEndsAt)
// 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++
for(i; i < html.length; i++){
if(html.charAt(i) === char){ break }
}
if (isId) {
id = html.slice(attrStartsAt + 1, i)
break
}
}
for(i; i < html.length; i++){
if(html.charAt(i) === char){ break }
}
if (isId) {
id = html.slice(attrStartsAt + 1, i)
break
}
}
break
} else {
i++
}
}
if(!tag){ throw new Error(`malformed html ${html}`) }

let closeAt = html.length - 1
insideComment = false
4 changes: 2 additions & 2 deletions lib/phoenix_live_view/test/dom.ex
Original file line number Diff line number Diff line change
@@ -282,9 +282,9 @@ defmodule Phoenix.LiveViewTest.DOM do
defp add_cid_attr(cid, [head | tail]) do
head_with_cid =
Regex.replace(
~r"^((?:<!--.*-->)?<[^\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
)