-
-
Notifications
You must be signed in to change notification settings - Fork 284
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
Problems with SVG files containing <script>
#546
Comments
Do you have an example notebook using Gadfly? |
Okay tried it myself 😁 Until we have this covered, you can wrap the snippet in # ╔═╡ af27fc22-0964-11eb-0382-8343faea368e
using Gadfly
# ╔═╡ 09a1b2ba-0965-11eb-2f11-73708c4a8bd9
HTMLDocument(plot(y=[1,2,3]))
# ╔═╡ 0751415a-0966-11eb-07d0-69316a9b61c8
struct HTMLDocument
embedded
end
# ╔═╡ 062f3880-0967-11eb-3c2c-1db7aa9d40b8
function Base.show(io::IO, mime::MIME"text/html", doc::HTMLDocument)
println(io, "<html>")
show(io, mime, doc.embedded)
println(io, "</html>")
end |
Oh, sorry. I mentioned Perhaps the only essential issue is AMD support. |
Yeah, I was expecting all AMD packages to be imported with |
1: we will not change this behaviour. Your suggestion is correct: a package that wants scripts to work should output the html mime type. 2: fixed in 694526b 3: I am not sure what you mean with "environment", but our ID tracking is only used for a pluto-specific (additional) feature. It should be unrelated 4 & 5: this is a good point. Let's make that the focus of this issue 6: you must have misread something |
Thank you for your response and the fix.
Please explain in a little more detail. The |
Ah it is I who misread. Still I don't think this matters. |
Yes. So I placed it at the end (i.e. 6.). However, in SVG, an XML format, ### A Pluto.jl notebook ###
# v0.12.6
using Markdown
using InteractiveUtils
# ╔═╡ 10303500-1cc2-11eb-22fc-6711d6ea7162
struct MyStruct end
# ╔═╡ 2f160ee0-1cc2-11eb-0db8-9f9c1b645189
Base.showable(::MIME"text/html", ::MyStruct) = true
# ╔═╡ 54186492-1cc2-11eb-109d-c31ef1da0c3c
function Base.show(io::IO, ::MIME"text/html", ::MyStruct)
write(io, """
<html>
<body>
<svg width="20mm" height="15mm" viewBox="0,0,20,15">
<script><![CDATA[
if (1 > 0) {
console.log(document.getElementById("my-circle"));
}
]]></script>
<circle fill="green" cx="10" cy="7" r="5" id="my-circle" />
</svg>
</body>
</html>
""")
end
# ╔═╡ 051d1660-1cdb-11eb-3e3f-01545ab6cb41
MyStruct()
# ╔═╡ Cell order:
# ╠═10303500-1cc2-11eb-22fc-6711d6ea7162
# ╠═2f160ee0-1cc2-11eb-0db8-9f9c1b645189
# ╠═54186492-1cc2-11eb-109d-c31ef1da0c3c
# ╠═051d1660-1cdb-11eb-3e3f-01545ab6cb41 |
Ah okay, your original wording made it sound like a style comment. We'll change it |
We reverted to |
This fixes the interactivity of Gadfly plots on Pluto, solving the Gadfly example in fonsp/Pluto.jl#546 and this [stackoverflow question](https://stackoverflow.com/questions/66297678/using-gadfly-interactively-in-pluto).
First of all, I don't know much about the Pluto inside or JavaScript. Please correct me if there is any misunderstanding.
For example, the
SVGJS
backend ofGadfly
outputs a<svg>
with embedded<script>
s (cf. http://gadflyjl.org/v1.3/man/backends/#Interactive-SVGs-1). However, there are some problems in using the interactive feature.<img>
Since
Pluto
uses<img>
to display SVG images, interactive features are not available. This problem can be solved by changing the output format toMIME"text/html"
(in the packages or user code).SVGScriptElement
SVG's
<script>
element (SVGScriptElement
) is similar to HTML's<script>
, but they are not identical in the specifications.The problem is that
SVGScriptElement
does not have thesrc
attribute. (Some implementations may support it, though.) Thus,node.src
in the following lines may beundefined
, and(node.src != "")
will betrue
.Pluto.jl/frontend/components/CellOutput.js
Lines 157 to 161 in b113783
This problem should be solved by changing the condition to
if (node.src)
. Of course, we can do a more strict check.An SVG image may contain multiple
<script>
elements. They are usually assumed to share an environment. However, the separation is currently done based onnode.id
.Pluto.jl/frontend/components/CellOutput.js
Line 180 in b113783
I don't know of a versatile solution to this, but I think that we could loosen up the scope a bit more, as shown below.
The
SVGJS
backend ofGadfly
uses Snap.svg.Snap.svg
uses the AMD (Asynchronous Module Definition) for module management.Now that ES Modules are widely supported, the AMD is an old technology, but some JavaScript libraries still use the AMD and RequireJS. This can lead to the following problem.
The libraries which use the AMD usually register a module object in the global environment when the AMD is not available. In general, this can cause problems with conflicts between multiple versions, but as far as the libraries are used in a single Pluto notebook, I think they rarely cause conflicts.
However, on the Pluto front-end, the default environment (
this
) isundefined
. This can cause an error in code which assumes thatthis
is the global environment, i.e.,window
.Pluto.jl/frontend/components/CellOutput.js
Line 183 in b113783
I don't think polluting the global environment is a good idea. However, it would be better to share a
Node
object of some kind (e.g.root_node
) with the samescript_id
instead ofundefined
.innerHTML
I think
textContent
is more appropriate thaninnerHTML
.Pluto.jl/frontend/components/CellOutput.js
Line 188 in b113783
The text was updated successfully, but these errors were encountered: