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

working webio #208

Closed
wants to merge 14 commits into from
2 changes: 1 addition & 1 deletion assets/providers/websocket_connection.js
Expand Up @@ -13,7 +13,7 @@
ws.onclose = function (evt) {
if (evt.code === 1005) {
// TODO what to do?
//tryconnect(url)
// tryconnect(url)
}
}
}
Expand Down
67 changes: 2 additions & 65 deletions assets/webio/dist/bundle.js
Expand Up @@ -66,7 +66,7 @@
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 13);
/******/ return __webpack_require__(__webpack_require__.s = 12);
/******/ })
/************************************************************************/
/******/ ([
Expand Down Expand Up @@ -272,7 +272,7 @@ function sendNotSetUp()
//unescape closing script tags which cause problems in html documents
function unencode_scripts(htmlstr){
if (typeof htmlstr !== "string") return htmlstr;
return htmlstr.replace(new RegExp("</_script>", "g"), "</scr"+"ipt>")
return htmlstr.replace(new RegExp("</_*script>", "g"), function (x) { return x.replace("_script", "script"); })
}

// Add an html string as the contents of an element
Expand Down Expand Up @@ -3042,71 +3042,8 @@ module.exports = isArray || function (val) {

/***/ }),
/* 12 */
/***/ (function(module, exports) {

(function() {

var webComponentsSupported = ('customElements' in window
&& 'import' in document.createElement('link')
&& 'content' in document.createElement('template'));

function evalInContext(js, context) {
return (function() { return eval(js); }).call(context);
}

function setup() {

class UnsafeScript extends HTMLElement {
constructor() {
super()
}
connectedCallback () {
evalInContext(this.textContent, this)
}
}

window.customElements.define("unsafe-script", UnsafeScript);
console.log("defining unsafe-script")
}

if (!webComponentsSupported) {
var script = document.createElement('script');
script.async = true;

var webiojs_elem = document.querySelector("script[src*='webio/dist/bundle.js']")
if (!webiojs_elem) {
console.warn("WebIO cannot find webio/dist/bundle.js, so it cannot load the webcomponents-lite support. Widgets may be broken in this environment.");
} else {
script.src = webiojs_elem.src.replace("bundle.js", "webcomponents-lite.min.js")
}

document.head.appendChild(script);
document.addEventListener("WebComponentsReady", function () {
if (customElements.get("unsafe-script") === undefined) {
setup()
}
})
} else if (customElements.get("unsafe-script") === undefined) {
if (document.readyState === "complete" || document.readyState === "loaded") {
setup()
} else {
if (window.frameElement) {
// DOMContentLoaded is never fired, we'll just do this now.
setup();
} else {
// https://stackoverflow.com/questions/48498581/accessing-textcontent-within-connectedcallback-for-a-custom-htmlelement
document.addEventListener("DOMContentLoaded", function () { setup(); })
}
}
}
})();


/***/ }),
/* 13 */
/***/ (function(module, exports, __webpack_require__) {

__webpack_require__(12)
module.exports = __webpack_require__(1)
__webpack_require__(9)

Expand Down
1 change: 0 additions & 1 deletion assets/webio/index.js
@@ -1,3 +1,2 @@
require('./unsafe_script.js')
module.exports = require('./webio.js')
require('./node.js')
7 changes: 1 addition & 6 deletions assets/webio/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions assets/webio/package.json
@@ -1,6 +1,6 @@
{
"name": "julia-web-io",
"version": "0.1.0",
"version": "0.2.0",
"description": "Supporting Javascript files for WebIO.jl Julia package",
"repository": "https://github.com/shashi/WebIO.jl",
"author": "Shashi Gowda",
Expand All @@ -11,7 +11,6 @@
"dependencies": {
"array-equal": "^1.0",
"is-array": "^1.0.1",
"@webcomponents/webcomponentsjs": "^1.0.2",
"systemjs": "0.21.0"
},
"devDependencies": {
Expand Down
56 changes: 0 additions & 56 deletions assets/webio/unsafe_script.js

This file was deleted.

2 changes: 1 addition & 1 deletion assets/webio/webio.js
Expand Up @@ -171,7 +171,7 @@ function sendNotSetUp()
//unescape closing script tags which cause problems in html documents
function unencode_scripts(htmlstr){
if (typeof htmlstr !== "string") return htmlstr;
return htmlstr.replace(new RegExp("</_script>", "g"), "</scr"+"ipt>")
return htmlstr.replace(new RegExp("</_*script>", "g"), function (x) { return x.replace("_script", "script"); })
}

// Add an html string as the contents of an element
Expand Down
16 changes: 0 additions & 16 deletions assets/webio/webpack.config.js
Expand Up @@ -14,20 +14,4 @@ module.exports = [{
node: {
fs: 'empty'
}
},
{
mode: 'production',
optimization: {
minimize: true
},
entry: {
'webcomponents-lite.min': './node_modules/@webcomponents/webcomponentsjs/webcomponents-lite.js'
},
output: {
filename: '[name].js'
},
plugins: [new webpack.IgnorePlugin(/vertx/)],
node: {
fs: 'empty'
}
}]
3 changes: 0 additions & 3 deletions src/WebIO.jl
Expand Up @@ -57,9 +57,6 @@ function __init__()
@require Mux="a975b10e-0019-58db-a62f-e48ff68538c9" begin
include(joinpath("providers", "mux.jl"))
end
@require Juno="e5e0dc1b-0480-54bc-9374-aad01c23163d" begin
include(joinpath("providers", "atom.jl"))
end
@require Blink="ad839575-38b3-5650-b840-f874b8c74a25" begin
include(joinpath("providers", "blink.jl"))
end
Expand Down
36 changes: 14 additions & 22 deletions src/node.jl
Expand Up @@ -35,13 +35,17 @@ because if they are printed inside a <script> tag, even if they are in quotes in
a javascript string, the html parser will still read them as a closing script
tag, and thus end the script content prematurely, causing untold woe.
"""
encode_scripts(htmlstr::String) =
replace(htmlstr, "</script>" => "</_script>")
function encode_scripts(htmlstr::String)
# sometimes a </script> can be inside a string inside a string inside a string...
# we need to go deeper....
replace(htmlstr, r"</(_*)script>" => x->replace(x, "/"=>"/_"))
end

function kwargs2props(propkwargs)
props = Dict{Symbol,Any}(propkwargs)
Symbol("setInnerHtml") in keys(props) &&
(props[:setInnerHtml] = encode_scripts(props[:setInnerHtml]))
if :setInnerHtml in keys(props)
(props[:setInnerHtml] = props[:setInnerHtml])
end
props # XXX IJulia/JSON bug? kernel seems to crash if this is a String not a Dict (which is obviously silly but still, it shouldn't crash the IJulia kernel)
end

Expand Down Expand Up @@ -92,24 +96,12 @@ function JSON.lower(n::Node)
)
end

## TODO -- optimize
function escapeHTML(i::String)
# Refer to http://stackoverflow.com/a/7382028/3822752 for spec. links
o = replace(i, "&" => "&amp;")
o = replace(o, "\"" => "&quot;")
o = replace(o, "'" => "&#39;")
o = replace(o, "<" => "&lt;")
o = replace(o, ">" => "&gt;")
return o
end

function Base.show(io::IO, m::MIME"text/html", x::Node)
write(io, "<div class='display:none'></div>" *
"""<unsafe-script style='display:none'>
WebIO.mount(this.previousSibling,""")
# NOTE: do NOT add space between </div> and <unsafe-script>
write(io, escapeHTML(sprint(s->jsexpr(s, x))))
write(io, ")</unsafe-script>")
id = WebIO.newid("div")
write(io, "<div class='display:inherit; margin: inherit' id='$id'></div>" *
"""<script>WebIO.mount(document.getElementById('$id'),""")
jsexpr(io, x)
write(io, ")</script>")
end

Base.show(io::IO, m::MIME"text/html", x::Observable) = show(io, m, WebIO.render(x))
Expand All @@ -118,7 +110,7 @@ function Base.show(io::IO, m::MIME"text/html", x::AbstractWidget)
if !Widgets.isijulia()
show(io, m, WebIO.render(x))
else
write(io, "<div class='tex2jax_ignore interactbulma'>\n")
write(io, "<div class='tex2jax_ignore interactbulma'>\n") # Hack to make MathJax look away ( makes Jupyter much faster)
show(io, m, WebIO.render(x))
write(io, "\n</div>")
end
Expand Down
25 changes: 0 additions & 25 deletions src/providers/atom.jl

This file was deleted.