diff options
author | Jo-Philipp Wich <jo@mein.io> | 2019-04-02 17:21:58 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2019-07-07 15:36:24 +0200 |
commit | 74ccea6fa2205dd9d329f3df125ea8ba23b0ed50 (patch) | |
tree | e11ff02e9ebe19660263077dc91f4a0440229151 | |
parent | 675824e377cee636beaaa003ce7dcf346ae148ab (diff) |
luci-base: luci.js: handle postprocessed sources
- Fix discovering base url if cache buster is appended to luci.js href
- Fix extracting require tokens in minified sources
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r-- | modules/luci-base/htdocs/luci-static/resources/luci.js | 41 |
1 files changed, 34 insertions, 7 deletions
diff --git a/modules/luci-base/htdocs/luci-static/resources/luci.js b/modules/luci-base/htdocs/luci-static/resources/luci.js index 61c3e8274..bf7f65a7d 100644 --- a/modules/luci-base/htdocs/luci-static/resources/luci.js +++ b/modules/luci-base/htdocs/luci-static/resources/luci.js @@ -432,8 +432,9 @@ __name__: 'LuCI', __init__: function(env) { - document.querySelectorAll('script[src$="/luci.js"]').forEach(function(s) { - env.base_url = s.getAttribute('src').replace(/\/luci\.js$/, ''); + document.querySelectorAll('script[src*="/luci.js"]').forEach(function(s) { + if (env.base_url == null || env.base_url == '') + env.base_url = s.getAttribute('src').replace(/\/luci\.js(?:\?v=[^?]+)?$/, ''); }); if (env.base_url == null) @@ -542,15 +543,41 @@ 'HTTP error %d while loading class file "%s"', res.status, url); var source = res.text(), - reqmatch = /(?:^|\n)[ \t]*(?:["']require[ \t]+(\S+)(?:[ \t]+as[ \t]+([a-zA-Z_]\S*))?["']);/g, + requirematch = /^require[ \t]+(\S+)(?:[ \t]+as[ \t]+([a-zA-Z_]\S*))?$/, + strictmatch = /^use[ \t]+strict$/, depends = [], args = ''; /* find require statements in source */ - for (var m = reqmatch.exec(source); m; m = reqmatch.exec(source)) { - var dep = m[1], as = m[2] || dep.replace(/[^a-zA-Z0-9_]/g, '_'); - depends.push(L.require(dep, from)); - args += ', ' + as; + for (var i = 0, off = -1, quote = -1, esc = false; i < source.length; i++) { + var chr = source.charCodeAt(i); + + if (esc) { + esc = false; + } + else if (chr == 92) { + esc = true; + } + else if (chr == quote) { + var s = source.substring(off, i), + m = requirematch.exec(s); + + if (m) { + var dep = m[1], as = m[2] || dep.replace(/[^a-zA-Z0-9_]/g, '_'); + depends.push(L.require(dep, from)); + args += ', ' + as; + } + else if (!strictmatch.exec(s)) { + break; + } + + off = -1; + quote = -1; + } + else if (quote == -1 && (chr == 34 || chr == 39)) { + off = i + 1; + quote = chr; + } } /* load dependencies and instantiate class */ |