Skip to content

Commit

Permalink
Fix for #326, avoid double escaping when pre-rendering a html code bl…
Browse files Browse the repository at this point in the history
…ock (#328)

* closes #326
  • Loading branch information
tlienart authored Dec 28, 2019
1 parent 96d8a1a commit 0daa5d5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "JuDoc"
uuid = "4ca9428c-4c75-11e9-2efb-bf5cb6c1e8f8"
authors = ["Thibaut Lienart <[email protected]>"]
version = "0.4"
version = "0.4.1"

This comment has been minimized.

Copy link
@tlienart

tlienart Dec 28, 2019

Author Owner

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
31 changes: 30 additions & 1 deletion src/converter/js_prerender.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,16 @@ function js_prerender_highlight(hs::String)::String
# core code
cs = subs(hs, nextind(hs, matchrange(co).stop), prevind(hs, matchrange(cc).start))
cs = escape_string(cs)

# lang
lang = co.captures[2]
if isnothing(lang)
write(jsbuffer, """console.log("<pre><code class=hljs>$cs</code></pre>");\n""")
else
if lang == "html" && is_html_escaped(cs)
# corner case, see https://github.com/tlienart/JuDoc.jl/issues/326
# highlight will re-escape the string further.
cs = html_unescape(cs) |> escape_string
end
# add to content of jsbuffer
write(jsbuffer, """console.log("<pre><code class=\\"$lang hljs\\">" + hljs.highlight("$lang", "$cs").value + "</code></pre>");""")
end
Expand Down Expand Up @@ -113,3 +118,27 @@ function js2html(hs::String, jsbuffer::IOBuffer, matches::Vector{RegexMatch},

return String(take!(htmls))
end


# Copied from https://github.com/JuliaLang/julia/blob/acb7bd93fb2d5adbbabeaed9f39ab3c85495b02f/stdlib/Markdown/src/render/html.jl#L25-L31
const _htmlescape_chars = LittleDict(
'<' => "&lt;",
'>' => "&gt;",
'"' => "&quot;",
'&' => "&amp;"
)
for ch in "'`!\$%()=+{}[]"
_htmlescape_chars[ch] = "&#$(Int(ch));"
end

const _htmlesc_to = values(_htmlescape_chars) |> collect

is_html_escaped(cs::String) = !isnothing(findfirst(ss -> occursin(ss, cs), _htmlesc_to))

function html_unescape(cs::String)
# this is a bit inefficient but whatever, `cs` shouldn't be very long.
for (ssfrom, ssto) in _htmlescape_chars
cs = replace(cs, ssto => ssfrom)
end
return cs
end
8 changes: 8 additions & 0 deletions test/global/html_esc.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# See https://github.com/tlienart/JuDoc.jl/issues/326

@testset "Issue 326" begin
h1 = "<div class=\"hello\">Blah</div>"
h1e = Markdown.htmlesc(h1)
@test J.is_html_escaped(h1e)
@test J.html_unescape(h1e) == h1
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ println("GLOBAL")
include("global/cases1.jl")
include("global/cases2.jl")
include("global/ordering.jl")
include("global/html_esc.jl")

begin
# create temp dir to do complete integration testing (has to be here in order
Expand Down

1 comment on commit 0daa5d5

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/7237

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.1 -m "<description of version>" 0daa5d5f85f057bb3efa9fe404bd4beab91601bd
git push origin v0.4.1

Please sign in to comment.