Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function handleAttr(
node: HTMLElement,
name: 'src' | 'href',
resolver: IRenderMime.IResolver
): Promise {
let source = node.getAttribute(name);
const isLocal = resolver.isLocal
? resolver.isLocal(source)
: URLExt.isLocal(source);
if (!source || !isLocal) {
return Promise.resolve(undefined);
}
node.setAttribute(name, '');
return resolver
.resolveUrl(source)
.then(path => {
return resolver.getDownloadUrl(path);
})
.then(url => {
// Check protocol again in case it changed:
if (URLExt.parse(url).protocol !== 'data:') {
// Bust caching for local src attrs.
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Bypassing_the_cache
url += (/\?/.test(url) ? '&' : '?') + new Date().getTime();
}
function handleAttr(
node: HTMLElement,
name: 'src' | 'href',
resolver: IRenderMime.IResolver
): Promise {
let source = node.getAttribute(name) || '';
const isLocal = resolver.isLocal
? resolver.isLocal(source)
: URLExt.isLocal(source);
if (!source || !isLocal) {
return Promise.resolve(undefined);
}
node.setAttribute(name, '');
return resolver
.resolveUrl(source)
.then(urlPath => {
return resolver.getDownloadUrl(urlPath);
})
.then(url => {
// Check protocol again in case it changed:
if (URLExt.parse(url).protocol !== 'data:') {
// Bust caching for local src attrs.
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Bypassing_the_cache
url += (/\?/.test(url) ? '&' : '?') + new Date().getTime();
}
export function testDaskDashboard(
url: string,
settings: ServerConnection.ISettings
): Promise {
url = normalizeDashboardUrl(url);
// If this is a url that we are proxying under the notebook server,
// it is easier to check for a valid dashboard.
if (URLExt.isLocal(url)) {
return ServerConnection.makeRequest(
URLExt.join(settings.baseUrl, url, 'individual-plots.json'),
{},
settings
).then(response => {
if (response.status === 200) {
return true;
} else {
return false;
}
});
}
return new Promise(resolve => {
// Hack Alert! We would like to test whether a given URL is actually
// a dask dashboard, since we will be iframe-ing it sight-unseen.
function handleAnchor(
anchor: HTMLAnchorElement,
resolver: IRenderMime.IResolver,
linkHandler: IRenderMime.ILinkHandler | null
): Promise {
// Get the link path without the location prepended.
// (e.g. "./foo.md#Header 1" vs "http://localhost:8888/foo.md#Header 1")
let href = anchor.getAttribute('href') || '';
const isLocal = resolver.isLocal
? resolver.isLocal(href)
: URLExt.isLocal(href);
// Bail if it is not a file-like url.
if (!href || !isLocal) {
return Promise.resolve(undefined);
}
// Remove the hash until we can handle it.
let hash = anchor.hash;
if (hash) {
// Handle internal link in the file.
if (hash === href) {
anchor.target = '_self';
return Promise.resolve(undefined);
}
// For external links, remove the hash until we have hash handling.
href = href.replace(hash, '');
}
// Get the appropriate file path.
export function handleDefaults(
node: HTMLElement,
resolver?: IRenderMime.IResolver
): void {
// Handle anchor elements.
let anchors = node.getElementsByTagName('a');
for (let i = 0; i < anchors.length; i++) {
let path = anchors[i].href;
const isLocal =
resolver && resolver.isLocal
? resolver.isLocal(path)
: URLExt.isLocal(path);
if (isLocal) {
anchors[i].target = '_self';
} else {
anchors[i].target = '_blank';
}
}
// Handle image elements.
let imgs = node.getElementsByTagName('img');
for (let i = 0; i < imgs.length; i++) {
if (!imgs[i].alt) {
imgs[i].alt = 'Image';
}
}
}
it('should test whether the url is a local url', () => {
expect(URLExt.isLocal('https://foo/bar.txt')).to.equal(false);
expect(URLExt.isLocal('http://foo/bar.txt')).to.equal(false);
expect(URLExt.isLocal('//foo/bar.txt')).to.equal(false);
expect(URLExt.isLocal('file://foo/bar.txt')).to.equal(false);
expect(URLExt.isLocal('data:text/plain,123ABC')).to.equal(false);
expect(URLExt.isLocal('/foo/bar.txt')).to.equal(false);
expect(URLExt.isLocal('httpserver/index.html')).to.equal(true);
expect(URLExt.isLocal('../foo/bar.txt')).to.equal(true);
expect(URLExt.isLocal('./foo/bar.txt')).to.equal(true);
expect(URLExt.isLocal('foo/bar.txt')).to.equal(true);
expect(URLExt.isLocal('bar.txt')).to.equal(true);
});
});
it('should test whether the url is a local url', () => {
expect(URLExt.isLocal('https://foo/bar.txt')).to.equal(false);
expect(URLExt.isLocal('http://foo/bar.txt')).to.equal(false);
expect(URLExt.isLocal('//foo/bar.txt')).to.equal(false);
expect(URLExt.isLocal('file://foo/bar.txt')).to.equal(false);
expect(URLExt.isLocal('data:text/plain,123ABC')).to.equal(false);
expect(URLExt.isLocal('/foo/bar.txt')).to.equal(false);
expect(URLExt.isLocal('httpserver/index.html')).to.equal(true);
expect(URLExt.isLocal('../foo/bar.txt')).to.equal(true);
expect(URLExt.isLocal('./foo/bar.txt')).to.equal(true);
expect(URLExt.isLocal('foo/bar.txt')).to.equal(true);
expect(URLExt.isLocal('bar.txt')).to.equal(true);
});
});
loadCSS(path: string): Promise {
const base = this._base;
const href = URLExt.isLocal(path) ? URLExt.join(base, path) : path;
const links = this._links;
return new Promise((resolve, reject) => {
const link = document.createElement('link');
link.setAttribute('rel', 'stylesheet');
link.setAttribute('type', 'text/css');
link.setAttribute('href', href);
link.addEventListener('load', () => {
resolve(undefined);
});
link.addEventListener('error', () => {
reject(`Stylesheet failed to load: ${href}`);
});
document.body.appendChild(link);
isLocal(url: string): boolean {
const path = decodeURI(url);
return URLExt.isLocal(url) || !!this._contents.driveName(path);
}