##// END OF EJS Templates
Fix XSS reported on Security list...
Fix XSS reported on Security list No CVE-ID yet August 18, 2015 ----- Reported to Quantopian by Juan Broullón <thebrowfc@gmail.com>... If you create a new folder in the iPython file browser and set Javascript code as its name the code injected will be executed. So, if I create a folder called "><img src=x onerror=alert(document.cookie)> and then I access to it, the cookies will be prompted. The XSS code is also executed if you access a link pointing directly at the folder. jik ------

File last commit:

r20538:ae7f6d6a
r21633:3ab41641
Show More
utils.js
888 lines | 30.2 KiB | application/javascript | JavascriptLexer
MinRK
update completion_ and objection_info_request...
r16580 // Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349
Jonathan Frederic
Make page.html require.js friendly.
r17188 define([
'base/js/namespace',
Jonathan Frederic
MWE,...
r17200 'jquery',
Matthias BUSSONNIER
Update to codemirror 4...
r18280 'codemirror/lib/codemirror',
Min RK
add utils.time...
r19501 'moment',
Nicholas Bollweg (Nick)
using codemirror mode/meta for detection
r19286 // silently upgrades CodeMirror
'codemirror/mode/meta',
Min RK
add utils.time...
r19501 ], function(IPython, $, CodeMirror, moment){
Matthias BUSSONNIER
"use strict" in most (if not all) our javascript...
r12103 "use strict";
MinRK
add IPython.load_extensions in js
r15228
Thomas Kluyver
Expose load_extensions in utils
r20020 var load_extensions = function () {
MinRK
add IPython.load_extensions in js
r15228 // load one or more IPython notebook extensions with requirejs
var extensions = [];
var extension_names = arguments;
for (var i = 0; i < extension_names.length; i++) {
extensions.push("nbextensions/" + arguments[i]);
}
require(extensions,
function () {
for (var i = 0; i < arguments.length; i++) {
var ext = arguments[i];
var ext_name = extension_names[i];
// success callback
console.log("Loaded extension: " + ext_name);
if (ext && ext.load_ipython_extension !== undefined) {
ext.load_ipython_extension();
}
}
},
function (err) {
// failure callback
console.log("Failed to load extension(s):", err.requireModules, err);
}
);
};
Thomas Kluyver
Expose load_extensions in utils
r20020
IPython.load_extensions = load_extensions;
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352
Thomas Kluyver
Refactor out load_extensions_from_config function
r19632 /**
* Wait for a config section to load, and then load the extensions specified
* in a 'load_extensions' key inside it.
*/
function load_extensions_from_config(section) {
section.loaded.then(function() {
if (section.data.load_extensions) {
var nbextension_paths = Object.getOwnPropertyNames(
section.data.load_extensions);
Thomas Kluyver
Expose load_extensions in utils
r20020 load_extensions.apply(this, nbextension_paths);
Thomas Kluyver
Refactor out load_extensions_from_config function
r19632 }
});
}
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 //============================================================================
// Cross-browser RegEx Split
//============================================================================
// This code has been MODIFIED from the code licensed below to not replace the
// default browser split. The license is reproduced here.
// see http://blog.stevenlevithan.com/archives/cross-browser-split for more info:
/*!
* Cross-Browser Split 1.1.1
* Copyright 2007-2012 Steven Levithan <stevenlevithan.com>
* Available under the MIT License
* ECMAScript compliant, uniform cross-browser split method
*/
/**
* Splits a string into an array of strings using a regex or string
* separator. Matches of the separator are not included in the result array.
* However, if `separator` is a regex that contains capturing groups,
* backreferences are spliced into the result each time `separator` is
* matched. Fixes browser bugs compared to the native
* `String.prototype.split` and can be used reliably cross-browser.
* @param {String} str String to split.
Min RK
various fixes in utils.js...
r19475 * @param {RegExp} separator Regex to use for separating
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 * the string.
* @param {Number} [limit] Maximum number of items to include in the result
* array.
* @returns {Array} Array of substrings.
* @example
*
* // Basic use
* regex_split('a b c d', ' ');
* // -> ['a', 'b', 'c', 'd']
*
* // With limit
* regex_split('a b c d', ' ', 2);
* // -> ['a', 'b']
*
* // Backreferences in result array
* regex_split('..word1 word2..', /([a-z]+)(\d+)/i);
* // -> ['..', 'word', '1', ' ', 'word', '2', '..']
*/
var regex_split = function (str, separator, limit) {
var output = [],
flags = (separator.ignoreCase ? "i" : "") +
(separator.multiline ? "m" : "") +
(separator.extended ? "x" : "") + // Proposed for ES6
(separator.sticky ? "y" : ""), // Firefox 3+
lastLastIndex = 0,
separator2, match, lastIndex, lastLength;
Min RK
various fixes in utils.js...
r19475 // Make `global` and avoid `lastIndex` issues by working with a copy
separator = new RegExp(separator.source, flags + "g");
Aron Ahmadia
partial refactor, can't quite get environments working
r8565
MinRK
expand ANSI color support...
r11307 var compliantExecNpcg = typeof(/()??/.exec("")[1]) === "undefined";
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 if (!compliantExecNpcg) {
// Doesn't need flags gy, but they don't hurt
separator2 = new RegExp("^" + separator.source + "$(?!\\s)", flags);
}
/* Values for `limit`, per the spec:
* If undefined: 4294967295 // Math.pow(2, 32) - 1
* If 0, Infinity, or NaN: 0
* If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296;
* If negative number: 4294967296 - Math.floor(Math.abs(limit))
* If other: Type-convert, then use the above rules
*/
limit = typeof(limit) === "undefined" ?
-1 >>> 0 : // Math.pow(2, 32) - 1
limit >>> 0; // ToUint32(limit)
Min RK
various fixes in utils.js...
r19475 for (match = separator.exec(str); match; match = separator.exec(str)) {
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 // `separator.lastIndex` is not reliable cross-browser
lastIndex = match.index + match[0].length;
if (lastIndex > lastLastIndex) {
output.push(str.slice(lastLastIndex, match.index));
// Fix browsers whose `exec` methods don't consistently return `undefined` for
// nonparticipating capturing groups
if (!compliantExecNpcg && match.length > 1) {
match[0].replace(separator2, function () {
for (var i = 1; i < arguments.length - 2; i++) {
if (typeof(arguments[i]) === "undefined") {
match[i] = undefined;
}
}
});
}
if (match.length > 1 && match.index < str.length) {
Array.prototype.push.apply(output, match.slice(1));
}
lastLength = match[0].length;
lastLastIndex = lastIndex;
if (output.length >= limit) {
break;
}
}
if (separator.lastIndex === match.index) {
separator.lastIndex++; // Avoid an infinite loop
}
}
if (lastLastIndex === str.length) {
if (lastLength || !separator.test("")) {
output.push("");
}
} else {
output.push(str.slice(lastLastIndex));
}
return output.length > limit ? output.slice(0, limit) : output;
};
//============================================================================
// End contributed Cross-browser RegEx Split
//============================================================================
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 var uuid = function () {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* http://www.ietf.org/rfc/rfc4122.txt
*/
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 var s = [];
var hexDigits = "0123456789ABCDEF";
for (var i = 0; i < 32; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
s[12] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
var uuid = s.join("");
return uuid;
};
//Fix raw text to parse correctly in crazy XML
function xmlencode(string) {
return string.replace(/\&/g,'&'+'amp;')
.replace(/</g,'&'+'lt;')
.replace(/>/g,'&'+'gt;')
.replace(/\'/g,'&'+'apos;')
.replace(/\"/g,'&'+'quot;')
Stefan van der Walt
Clean up javascript based on js2-mode feedback.
r5479 .replace(/`/g,'&'+'#96;');
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349 }
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352
Brian E. Granger
Pager is working again.
r4361
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 //Map from terminal commands to CSS classes
Mikhail Korobov
Some bugs in js (mostly scoping bugs) are fixed
r8839 var ansi_colormap = {
MinRK
use much faster regexp for ansi coloring...
r10250 "01":"ansibold",
MinRK
add indexed-color in 256-color support
r11313
MinRK
use much faster regexp for ansi coloring...
r10250 "30":"ansiblack",
"31":"ansired",
"32":"ansigreen",
"33":"ansiyellow",
"34":"ansiblue",
"35":"ansipurple",
"36":"ansicyan",
MinRK
add ansi background colors
r11312 "37":"ansigray",
MinRK
add indexed-color in 256-color support
r11313
MinRK
add ansi background colors
r11312 "40":"ansibgblack",
"41":"ansibgred",
"42":"ansibggreen",
Andrea Bedini
Fix ansi color code for background yellow
r12095 "43":"ansibgyellow",
MinRK
add ansi background colors
r11312 "44":"ansibgblue",
"45":"ansibgpurple",
"46":"ansibgcyan",
"47":"ansibggray"
Stefan van der Walt
Clean up javascript based on js2-mode feedback.
r5479 };
MinRK
expand ANSI color support...
r11307
function _process_numbers(attrs, numbers) {
// process ansi escapes
var n = numbers.shift();
if (ansi_colormap[n]) {
if ( ! attrs["class"] ) {
attrs["class"] = ansi_colormap[n];
} else {
attrs["class"] += " " + ansi_colormap[n];
}
} else if (n == "38" || n == "48") {
MinRK
be pedantic about VT100 vs ANSI in comments
r11314 // VT100 256 color or 24 bit RGB
MinRK
expand ANSI color support...
r11307 if (numbers.length < 2) {
MinRK
be pedantic about VT100 vs ANSI in comments
r11314 console.log("Not enough fields for VT100 color", numbers);
MinRK
expand ANSI color support...
r11307 return;
}
var index_or_rgb = numbers.shift();
var r,g,b;
if (index_or_rgb == "5") {
MinRK
be pedantic about VT100 vs ANSI in comments
r11314 // 256 color
Min RK
various fixes in utils.js...
r19475 var idx = parseInt(numbers.shift(), 10);
MinRK
expand ANSI color support...
r11307 if (idx < 16) {
MinRK
add indexed-color in 256-color support
r11313 // indexed ANSI
// ignore bright / non-bright distinction
idx = idx % 8;
var ansiclass = ansi_colormap[n[0] + (idx % 8).toString()];
if ( ! attrs["class"] ) {
attrs["class"] = ansiclass;
} else {
attrs["class"] += " " + ansiclass;
}
return;
MinRK
expand ANSI color support...
r11307 } else if (idx < 232) {
MinRK
add indexed-color in 256-color support
r11313 // 216 color 6x6x6 RGB
MinRK
expand ANSI color support...
r11307 idx = idx - 16;
b = idx % 6;
g = Math.floor(idx / 6) % 6;
r = Math.floor(idx / 36) % 6;
// convert to rgb
r = (r * 51);
g = (g * 51);
b = (b * 51);
} else {
// grayscale
idx = idx - 231;
// it's 1-24 and should *not* include black or white,
// so a 26 point scale
MinRK
add indexed-color in 256-color support
r11313 r = g = b = Math.floor(idx * 256 / 26);
MinRK
expand ANSI color support...
r11307 }
} else if (index_or_rgb == "2") {
MinRK
add indexed-color in 256-color support
r11313 // Simple 24 bit RGB
MinRK
expand ANSI color support...
r11307 if (numbers.length > 3) {
console.log("Not enough fields for RGB", numbers);
return;
}
r = numbers.shift();
g = numbers.shift();
b = numbers.shift();
} else {
console.log("unrecognized control", numbers);
return;
}
if (r !== undefined) {
// apply the rgb color
var line;
if (n == "38") {
line = "color: ";
} else {
line = "background-color: ";
}
Min RK
update frontend with path/name changes...
r18752 line = line + "rgb(" + r + "," + g + "," + b + ");";
if ( !attrs.style ) {
attrs.style = line;
MinRK
expand ANSI color support...
r11307 } else {
Min RK
update frontend with path/name changes...
r18752 attrs.style += " " + line;
MinRK
expand ANSI color support...
r11307 }
}
}
}
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352
MinRK
use much faster regexp for ansi coloring...
r10250 function ansispan(str) {
// ansispan function adapted from github.com/mmalecki/ansispan (MIT License)
MinRK
expand ANSI color support...
r11307 // regular ansi escapes (using the table above)
Min RK
update frontend with path/name changes...
r18752 var is_open = false;
MinRK
expand ANSI color support...
r11307 return str.replace(/\033\[(0?[01]|22|39)?([;\d]+)?m/g, function(match, prefix, pattern) {
if (!pattern) {
// [(01|22|39|)m close spans
Mateusz Paprocki
Fix ansispan() to ignore stray [0m
r18191 if (is_open) {
is_open = false;
return "</span>";
} else {
return "";
}
} else {
is_open = true;
// consume sequence of color escapes
var numbers = pattern.match(/\d+/g);
var attrs = {};
while (numbers.length > 0) {
_process_numbers(attrs, numbers);
}
var span = "<span ";
Min RK
various fixes in utils.js...
r19475 Object.keys(attrs).map(function (attr) {
Mateusz Paprocki
Fix ansispan() to ignore stray [0m
r18191 span = span + " " + attr + '="' + attrs[attr] + '"';
Min RK
various fixes in utils.js...
r19475 });
Mateusz Paprocki
Fix ansispan() to ignore stray [0m
r18191 return span + ">";
MinRK
expand ANSI color support...
r11307 }
MinRK
use much faster regexp for ansi coloring...
r10250 });
Min RK
update frontend with path/name changes...
r18752 }
Mateusz Paprocki
Fix ansispan() to ignore stray [0m
r18191
Michael Droettboom
Handle carriage return characters ("\r") in HTML notebook output....
r7339 // Transform ANSI color escape codes into HTML <span> tags with css
Brian Granger
Colors now working in tracebacks and the pager....
r4383 // classes listed in the above ansi_colormap object. The actual color used
// are set in the css file.
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 function fixConsole(txt) {
Stefan van der Walt
Clean up javascript based on js2-mode feedback.
r5479 txt = xmlencode(txt);
Jack Feser
Strip non color related ANSI escape sequences from notebook output....
r10140
// Strip all ANSI codes that are not color related. Matches
// all ANSI codes that do not end with "m".
var ignored_re = /(?=(\033\[[\d;=]*[a-ln-zA-Z]{1}))\1(?!m)/g;
txt = txt.replace(ignored_re, "");
MinRK
use much faster regexp for ansi coloring...
r10250
// color ansi codes
txt = ansispan(txt);
Stefan van der Walt
Clean up javascript based on js2-mode feedback.
r5479 return txt;
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 }
Michael Droettboom
Fix carriage-return handling regular expression so it doesn't treat "\r\n" as "\r".
r7338 // Remove chunks that should be overridden by the effect of
// carriage return characters
function fixCarriageReturn(txt) {
Mikhail Korobov
Some bugs in js (mostly scoping bugs) are fixed
r8839 var tmp = txt;
Michael Droettboom
Fix carriage-return handling regular expression so it doesn't treat "\r\n" as "\r".
r7338 do {
txt = tmp;
Thomas Kluyver
Fix display of plain text containing multiple carriage returns before line feed....
r8702 tmp = txt.replace(/\r+\n/gm, '\n'); // \r followed by \n --> newline
tmp = tmp.replace(/^.*\r+/gm, ''); // Other \r --> clear line
Michael Droettboom
Fix carriage-return handling regular expression so it doesn't treat "\r\n" as "\r".
r7338 } while (tmp.length < txt.length);
return txt;
}
Brian E. Granger
Pager is working again.
r4361
MinRK
fix regular expression for detecting links in stdout...
r10045 // Locate any URLs and convert them to a anchor tag
Erik M. Bray
Locate URLs in text output and convert them to hyperlinks.
r8528 function autoLinkUrls(txt) {
MinRK
fix regular expression for detecting links in stdout...
r10045 return txt.replace(/(^|\s)(https?|ftp)(:[^'">\s]+)/gi,
"$1<a target=\"_blank\" href=\"$2$3\">$2$3</a>");
Erik M. Bray
Locate URLs in text output and convert them to hyperlinks.
r8528 }
Mikhail Korobov
Some bugs in js (mostly scoping bugs) are fixed
r8839 var points_to_pixels = function (points) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* A reasonably good way of converting between points and pixels.
*/
Brian Granger
Resolving conflict in utils.js.
r7353 var test = $('<div style="display: none; width: 10000pt; padding:0; border:0;"></div>');
Min RK
various fixes in utils.js...
r19475 $('body').append(test);
Brian Granger
Resolving conflict in utils.js.
r7353 var pixel_per_point = test.width()/10000;
test.remove();
return Math.floor(points*pixel_per_point);
Mikhail Korobov
Some bugs in js (mostly scoping bugs) are fixed
r8839 };
MinRK
add utils.always_new...
r13206
var always_new = function (constructor) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* wrapper around contructor to avoid requiring `var a = new constructor()`
* useful for passing constructors as callbacks,
* not for programmer laziness.
* from http://programmers.stackexchange.com/questions/118798
*/
MinRK
add utils.always_new...
r13206 return function () {
var obj = Object.create(constructor.prototype);
constructor.apply(obj, arguments);
return obj;
};
};
Brian Granger
Resolving conflict in utils.js.
r7353
MinRK
add utils.url_path_join...
r13063 var url_path_join = function () {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* join a sequence of url components with '/'
*/
MinRK
add utils.url_path_join...
r13063 var url = '';
for (var i = 0; i < arguments.length; i++) {
MinRK
don't let empty strings introduce extra slashes in url_path_join
r13080 if (arguments[i] === '') {
continue;
}
MinRK
add utils.url_path_join...
r13063 if (url.length > 0 && url[url.length-1] != '/') {
url = url + '/' + arguments[i];
} else {
url = url + arguments[i];
}
}
MinRK
add utils.parse_url...
r15241 url = url.replace(/\/\/+/, '/');
MinRK
add utils.url_path_join...
r13063 return url;
};
MinRK
add utils.url_join_encode...
r13686
Min RK
update frontend with path/name changes...
r18752 var url_path_split = function (path) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* Like os.path.split for URLs.
* Always returns two strings, the directory path and the base filename
*/
Min RK
update frontend with path/name changes...
r18752
var idx = path.lastIndexOf('/');
if (idx === -1) {
return ['', path];
} else {
return [ path.slice(0, idx), path.slice(idx + 1) ];
}
};
MinRK
add utils.parse_url...
r15241 var parse_url = function (url) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* an `a` element with an href allows attr-access to the parsed segments of a URL
* a = parse_url("http://localhost:8888/path/name#hash")
* a.protocol = "http:"
* a.host = "localhost:8888"
* a.hostname = "localhost"
* a.port = 8888
* a.pathname = "/path/name"
* a.hash = "#hash"
*/
MinRK
add utils.parse_url...
r15241 var a = document.createElement("a");
a.href = url;
return a;
};
MinRK
add utils.url_join_encode...
r13686
var encode_uri_components = function (uri) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* encode just the components of a multi-segment uri,
* leaving '/' separators
*/
MinRK
add utils.url_join_encode...
r13686 return uri.split('/').map(encodeURIComponent).join('/');
MinRK
add utils.parse_url...
r15241 };
MinRK
add utils.url_join_encode...
r13686
var url_join_encode = function () {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* join a sequence of url components with '/',
* encoding each component with encodeURIComponent
*/
MinRK
add utils.url_join_encode...
r13686 return encode_uri_components(url_path_join.apply(null, arguments));
};
MinRK
add utils.url_path_join...
r13063
MinRK
add utils.splitext to js...
r13120 var splitext = function (filename) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* mimic Python os.path.splitext
* Returns ['base', '.ext']
*/
MinRK
add utils.splitext to js...
r13120 var idx = filename.lastIndexOf('.');
if (idx > 0) {
return [filename.slice(0, idx), filename.slice(idx)];
} else {
return [filename, ''];
}
MinRK
add utils.get_data...
r15233 };
MinRK
add utils.escape_html
r15331 var escape_html = function (text) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* escape text to HTML
*/
MinRK
add utils.escape_html
r15331 return $("<div/>").text(text).html();
MinRK
update completion_ and objection_info_request...
r16580 };
MinRK
add utils.escape_html
r15331
MinRK
s/get_data/get_body_data/
r15240 var get_body_data = function(key) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* get a url-encoded item from body.data and decode it
* we should never have any encoded URLs anywhere else in code
* until we are building an actual request
*/
Cedric GESTES
get_body_data: fix for undefined...
r20409 var val = $('body').data(key);
if (!val)
return val;
return decodeURIComponent(val);
MinRK
add utils.get_data...
r15233 };
MinRK
update completion_ and objection_info_request...
r16580
MinRK
complete_reply has cursor_start and cursor_end, not matched_text
r16588 var to_absolute_cursor_pos = function (cm, cursor) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* get the absolute cursor position from CodeMirror's col, ch
*/
MinRK
update completion_ and objection_info_request...
r16580 if (!cursor) {
cursor = cm.getCursor();
}
var cursor_pos = cursor.ch;
for (var i = 0; i < cursor.line; i++) {
cursor_pos += cm.getLine(i).length + 1;
}
return cursor_pos;
};
MinRK
complete_reply has cursor_start and cursor_end, not matched_text
r16588 var from_absolute_cursor_pos = function (cm, cursor_pos) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* turn absolute cursor postion into CodeMirror col, ch cursor
*/
Matthias Bussonnier
Fix #4777 and #7887...
r20538 var i, line, next_line;
MinRK
complete_reply has cursor_start and cursor_end, not matched_text
r16588 var offset = 0;
Matthias Bussonnier
Fix #4777 and #7887...
r20538 for (i = 0, next_line=cm.getLine(i); next_line !== undefined; i++, next_line=cm.getLine(i)) {
line = next_line;
if (offset + next_line.length < cursor_pos) {
offset += next_line.length + 1;
MinRK
complete_reply has cursor_start and cursor_end, not matched_text
r16588 } else {
return {
line : i,
ch : cursor_pos - offset,
};
}
}
// reached end, return endpoint
return {
line : i - 1,
Matthias Bussonnier
Fix #4777 and #7887...
r20538 ch : line.length - 1,
MinRK
complete_reply has cursor_start and cursor_end, not matched_text
r16588 };
};
Brian E. Granger
Removing call to $.browser which went away in jQuery 1.9....
r9227 // http://stackoverflow.com/questions/2400935/browser-detection-in-javascript
Matthias BUSSONNIER
"use strict" in most (if not all) our javascript...
r12103 var browser = (function() {
MinRK
allow utils.js to be loaded in node...
r14205 if (typeof navigator === 'undefined') {
// navigator undefined in node
return 'None';
}
Brian E. Granger
Removing call to $.browser which went away in jQuery 1.9....
r9227 var N= navigator.appName, ua= navigator.userAgent, tem;
var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
MinRK
complete_reply has cursor_start and cursor_end, not matched_text
r16588 if (M && (tem= ua.match(/version\/([\.\d]+)/i)) !== null) M[2]= tem[1];
Brian E. Granger
Removing call to $.browser which went away in jQuery 1.9....
r9227 M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?'];
return M;
})();
Brian E. Granger
Added platform dep. logic.
r14816 // http://stackoverflow.com/questions/11219582/how-to-detect-my-browser-version-and-operating-system-using-javascript
var platform = (function () {
Brian E. Granger
Protect against navigator undefined in platform.
r14826 if (typeof navigator === 'undefined') {
// navigator undefined in node
return 'None';
}
var OSName="None";
Brian E. Granger
Added platform dep. logic.
r14816 if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";
MinRK
update completion_ and objection_info_request...
r16580 return OSName;
Brian E. Granger
Added platform dep. logic.
r14816 })();
Min RK
add utils.get_url_param...
r19256
var get_url_param = function (name) {
// get a URL parameter. I cannot believe we actually need this.
// Based on http://stackoverflow.com/a/25359264/938949
Min RK
various fixes in utils.js...
r19475 var match = new RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
Min RK
add utils.get_url_param...
r19256 if (match){
return decodeURIComponent(match[1] || '');
}
};
Brian E. Granger
Using a more specific approach for managing CM focus....
r14033 var is_or_has = function (a, b) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* Is b a child of a or a itself?
*/
Brian E. Granger
Using a more specific approach for managing CM focus....
r14033 return a.has(b).length !==0 || a.is(b);
MinRK
update completion_ and objection_info_request...
r16580 };
Brian E. Granger
Using a more specific approach for managing CM focus....
r14033
var is_focused = function (e) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* Is element e, or one of its children focused?
*/
Brian E. Granger
Using a more specific approach for managing CM focus....
r14033 e = $(e);
var target = $(document.activeElement);
if (target.length > 0) {
if (is_or_has(e, target)) {
return true;
} else {
return false;
}
} else {
return false;
}
MinRK
update completion_ and objection_info_request...
r16580 };
MinRK
log all failed ajax API requests
r16445
MinRK
move mergeopt to utils...
r17764 var mergeopt = function(_class, options, overwrite){
options = options || {};
overwrite = overwrite || {};
return $.extend(true, {}, _class.options_default, options, overwrite);
};
MinRK
add utils.ajax_error_msg for extracting the JSON error message.
r17642 var ajax_error_msg = function (jqXHR) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* Return a JSON error message if there is one,
* otherwise the basic HTTP status text.
*/
Jessica B. Hamrick
More informative error messages
r18214 if (jqXHR.responseJSON && jqXHR.responseJSON.traceback) {
return jqXHR.responseJSON.traceback;
} else if (jqXHR.responseJSON && jqXHR.responseJSON.message) {
MinRK
add utils.ajax_error_msg for extracting the JSON error message.
r17642 return jqXHR.responseJSON.message;
} else {
return jqXHR.statusText;
}
Jessica B. Hamrick
More informative error messages
r18214 };
MinRK
log all failed ajax API requests
r16445 var log_ajax_error = function (jqXHR, status, error) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* log ajax failures with informative messages
*/
MinRK
log all failed ajax API requests
r16445 var msg = "API request failed (" + jqXHR.status + "): ";
console.log(jqXHR);
MinRK
add utils.ajax_error_msg for extracting the JSON error message.
r17642 msg += ajax_error_msg(jqXHR);
MinRK
log all failed ajax API requests
r16445 console.log(msg);
};
Nicholas Bollweg (Nick)
using codemirror mode/meta for detection
r19286
MinRK
use require to load CodeMirror modes...
r18286 var requireCodeMirrorMode = function (mode, callback, errback) {
Nicholas Bollweg (Nick)
using codemirror mode/meta for detection
r19286 /**
* find a predefined mode or detect from CM metadata then
* require and callback with the resolveable mode string: mime or
* custom name
Jonathan Frederic
Ran function comment conversion tool
r19176 */
Nicholas Bollweg (Nick)
using codemirror mode/meta for detection
r19286
Nicholas Bollweg (Nick)
handling explicitly provided modes from kernelspec.language_info
r19289 var modename = (typeof mode == "string") ? mode :
mode.mode || mode.name;
Nicholas Bollweg (Nick)
checking in css
r19291 // simplest, cheapest check by mode name: mode may also have config
Nicholas Bollweg (Nick)
handling explicitly provided modes from kernelspec.language_info
r19289 if (CodeMirror.modes.hasOwnProperty(modename)) {
Nicholas Bollweg (Nick)
checking in css
r19291 // return the full mode object, if it has a name
callback(mode.name ? mode : modename);
MinRK
use require to load CodeMirror modes...
r18286 return;
}
Nicholas Bollweg (Nick)
using codemirror mode/meta for detection
r19286
Nicholas Bollweg (Nick)
checking in css
r19291 // *somehow* get back a CM.modeInfo-like object that has .mode and
// .mime
Nicholas Bollweg (Nick)
handling explicitly provided modes from kernelspec.language_info
r19289 var info = (mode && mode.mode && mode.mime && mode) ||
CodeMirror.findModeByName(modename) ||
CodeMirror.findModeByExtension(modename.split(".").slice(-1)) ||
Nicholas Bollweg (Nick)
more fidgeting before starting over
r19290 CodeMirror.findModeByMIME(modename) ||
{mode: modename, mime: modename};
Nicholas Bollweg (Nick)
using codemirror mode/meta for detection
r19286
MinRK
use require to load CodeMirror modes...
r18286 require([
Matthias Bussonnier
add comment about codemirror.modeURL
r18288 // might want to use CodeMirror.modeURL here
Nicholas Bollweg (Nick)
using codemirror mode/meta for detection
r19286 ['codemirror/mode', info.mode, info.mode].join('/'),
Nicholas Bollweg (Nick)
checking in css
r19291 ], function() {
// return the original mode, as from a kernelspec on first load
// or the mimetype, as for most highlighting
callback(mode.name ? mode : info.mime);
}, errback
MinRK
use require to load CodeMirror modes...
r18286 );
};
MinRK
support buffers in comm messages...
r18329
Kester Tong
Modifies Contents API to return Error objects...
r18661 /** Error type for wrapped XHR errors. */
var XHR_ERROR = 'XhrError';
/**
* Wraps an AJAX error as an Error object.
*/
var wrap_ajax_error = function (jqXHR, status, error) {
var wrapped_error = new Error(ajax_error_msg(jqXHR));
wrapped_error.name = XHR_ERROR;
// provide xhr response
wrapped_error.xhr = jqXHR;
wrapped_error.xhr_status = status;
wrapped_error.xhr_error = error;
return wrapped_error;
Min RK
update frontend with path/name changes...
r18752 };
Kester Tong
Modifies Contents API to return Error objects...
r18661
Thomas Kluyver
Infrastructure for AJAX requests returning ES6 promises
r18826 var promising_ajax = function(url, settings) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* Like $.ajax, but returning an ES6 promise. success and error settings
* will be ignored.
*/
Min RK
handle undefined settings in promising_ajax
r19259 settings = settings || {};
Thomas Kluyver
Infrastructure for AJAX requests returning ES6 promises
r18826 return new Promise(function(resolve, reject) {
settings.success = function(data, status, jqXHR) {
resolve(data);
Thomas Kluyver
Semicolons
r18834 };
Thomas Kluyver
Infrastructure for AJAX requests returning ES6 promises
r18826 settings.error = function(jqXHR, status, error) {
log_ajax_error(jqXHR, status, error);
reject(wrap_ajax_error(jqXHR, status, error));
Thomas Kluyver
Semicolons
r18834 };
Thomas Kluyver
Infrastructure for AJAX requests returning ES6 promises
r18826 $.ajax(url, settings);
});
};
Jonathan Frederic
Initial stab at adding promises to the widget framework.
r18882
Jonathan Frederic
Use rsvp.js for Promises
r18897 var WrappedError = function(message, error){
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* Wrappable Error class
*
* The Error class doesn't actually act on `this`. Instead it always
* returns a new instance of Error. Here we capture that instance so we
* can apply it's properties to `this`.
*/
Jonathan Frederic
Use rsvp.js for Promises
r18897 var tmp = Error.apply(this, [message]);
// Copy the properties of the error over to this.
var properties = Object.getOwnPropertyNames(tmp);
for (var i = 0; i < properties.length; i++) {
this[properties[i]] = tmp[properties[i]];
}
// Keep a stack of the original error messages.
if (error instanceof WrappedError) {
this.error_stack = error.error_stack;
} else {
this.error_stack = [error];
}
this.error_stack.push(tmp);
return this;
};
WrappedError.prototype = Object.create(Error.prototype, {});
Jonathan Frederic
Add a WrappedError class
r18895 var load_class = function(class_name, module_name, registry) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* Tries to load a class
*
* Tries to load a class from a module using require.js, if a module
* is specified, otherwise tries to load a class from the global
* registry, if the global registry is provided.
*/
Jonathan Frederic
use es6
r18907 return new Promise(function(resolve, reject) {
Jonathan Frederic
Initial stab at adding promises to the widget framework.
r18882
// Try loading the view module using require.js
if (module_name) {
require([module_name], function(module) {
if (module[class_name] === undefined) {
Jonathan Frederic
Add a WrappedError class
r18895 reject(new Error('Class '+class_name+' not found in module '+module_name));
Jonathan Frederic
Initial stab at adding promises to the widget framework.
r18882 } else {
resolve(module[class_name]);
}
}, reject);
} else {
if (registry && registry[class_name]) {
resolve(registry[class_name]);
} else {
Jonathan Frederic
Don't throw Errors with Objects as the message/
r18902 reject(new Error('Class '+class_name+' not found in registry '));
Jonathan Frederic
Initial stab at adding promises to the widget framework.
r18882 }
}
});
};
Jason Grout
Simplify code by using Promises in a better way; try_load -> load
r18887
Jonathan Frederic
Address @takluyver 's review comments
r18935 var resolve_promises_dict = function(d) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* Resolve a promiseful dictionary.
* Returns a single Promise.
*/
Jason Grout
More simplifications due to promises
r18888 var keys = Object.keys(d);
var values = [];
keys.forEach(function(key) {
Jason Grout
Dictionary key/value typo
r18891 values.push(d[key]);
Jason Grout
More simplifications due to promises
r18888 });
Jonathan Frederic
use es6
r18907 return Promise.all(values).then(function(v) {
Jason Grout
More simplifications due to promises
r18888 d = {};
for(var i=0; i<keys.length; i++) {
d[keys[i]] = v[i];
}
return d;
});
};
Jonathan Frederic
Add a WrappedError class
r18895 var reject = function(message, log) {
Jonathan Frederic
Ran function comment conversion tool
r19176 /**
* Creates a wrappable Promise rejection function.
*
* Creates a function that returns a Promise.reject with a new WrappedError
* that has the provided message and wraps the original error that
* caused the promise to reject.
*/
Jonathan Frederic
Add a WrappedError class
r18895 return function(error) {
var wrapped_error = new WrappedError(message, error);
if (log) console.error(wrapped_error);
Jonathan Frederic
use es6
r18907 return Promise.reject(wrapped_error);
Jonathan Frederic
Add a WrappedError class
r18895 };
};
Nicholas Bollweg (Nick)
moving typeset to utils, usage in cell and outputarea
r19235 var typeset = function(element, text) {
/**
* Apply MathJax rendering to an element, and optionally set its text
*
* If MathJax is not available, make no changes.
*
* Returns the output any number of typeset elements, or undefined if
* MathJax was not available.
*
* Parameters
* ----------
* element: Node, NodeList, or jQuery selection
* text: option string
*/
var $el = element.jquery ? element : $(element);
if(arguments.length > 1){
$el.text(text);
}
dongweiming
fix widget description can not display when use --no-mathjax
r19791 if(!window.MathJax){
return;
}
Nicholas Bollweg (Nick)
moving typeset to utils, usage in cell and outputarea
r19235 return $el.map(function(){
// MathJax takes a DOM node: $.map makes `this` the context
return MathJax.Hub.Queue(["Typeset", MathJax.Hub, this]);
});
};
Min RK
add utils.time...
r19501
var time = {};
time.milliseconds = {};
time.milliseconds.s = 1000;
time.milliseconds.m = 60 * time.milliseconds.s;
time.milliseconds.h = 60 * time.milliseconds.m;
time.milliseconds.d = 24 * time.milliseconds.h;
time.thresholds = {
// moment.js thresholds in milliseconds
s: moment.relativeTimeThreshold('s') * time.milliseconds.s,
m: moment.relativeTimeThreshold('m') * time.milliseconds.m,
h: moment.relativeTimeThreshold('h') * time.milliseconds.h,
d: moment.relativeTimeThreshold('d') * time.milliseconds.d,
};
time.timeout_from_dt = function (dt) {
/** compute a timeout based on dt
input and output both in milliseconds
use moment's relative time thresholds:
- 10 seconds if in 'seconds ago' territory
- 1 minute if in 'minutes ago'
- 1 hour otherwise
*/
if (dt < time.thresholds.s) {
return 10 * time.milliseconds.s;
} else if (dt < time.thresholds.m) {
return time.milliseconds.m;
} else {
return time.milliseconds.h;
}
};
Jonathan Frederic
@carreau review changes
r17204 var utils = {
Thomas Kluyver
Expose load_extensions in utils
r20020 load_extensions: load_extensions,
Thomas Kluyver
Refactor out load_extensions_from_config function
r19632 load_extensions_from_config: load_extensions_from_config,
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 regex_split : regex_split,
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 uuid : uuid,
Brian E. Granger
Pager is working again.
r4361 fixConsole : fixConsole,
Min RK
fix missing comma
r7357 fixCarriageReturn : fixCarriageReturn,
Erik M. Bray
Locate URLs in text output and convert them to hyperlinks.
r8528 autoLinkUrls : autoLinkUrls,
Brian E. Granger
Removing call to $.browser which went away in jQuery 1.9....
r9227 points_to_pixels : points_to_pixels,
MinRK
s/get_data/get_body_data/
r15240 get_body_data : get_body_data,
MinRK
add utils.parse_url...
r15241 parse_url : parse_url,
Min RK
update frontend with path/name changes...
r18752 url_path_split : url_path_split,
MinRK
add utils.url_path_join...
r13063 url_path_join : url_path_join,
MinRK
add utils.url_join_encode...
r13686 url_join_encode : url_join_encode,
encode_uri_components : encode_uri_components,
MinRK
add utils.splitext to js...
r13120 splitext : splitext,
MinRK
add utils.escape_html
r15331 escape_html : escape_html,
MinRK
add utils.always_new...
r13206 always_new : always_new,
MinRK
complete_reply has cursor_start and cursor_end, not matched_text
r16588 to_absolute_cursor_pos : to_absolute_cursor_pos,
from_absolute_cursor_pos : from_absolute_cursor_pos,
Brian E. Granger
Using a more specific approach for managing CM focus....
r14033 browser : browser,
Brian E. Granger
Added platform dep. logic.
r14816 platform: platform,
Min RK
add utils.get_url_param...
r19256 get_url_param: get_url_param,
Brian E. Granger
Using a more specific approach for managing CM focus....
r14033 is_or_has : is_or_has,
MinRK
log all failed ajax API requests
r16445 is_focused : is_focused,
MinRK
move mergeopt to utils...
r17764 mergeopt: mergeopt,
MinRK
add utils.ajax_error_msg for extracting the JSON error message.
r17642 ajax_error_msg : ajax_error_msg,
MinRK
log all failed ajax API requests
r16445 log_ajax_error : log_ajax_error,
MinRK
use require to load CodeMirror modes...
r18286 requireCodeMirrorMode : requireCodeMirrorMode,
Kester Tong
Modifies Contents API to return Error objects...
r18661 XHR_ERROR : XHR_ERROR,
Thomas Kluyver
Infrastructure for AJAX requests returning ES6 promises
r18826 wrap_ajax_error : wrap_ajax_error,
promising_ajax : promising_ajax,
Jonathan Frederic
Use rsvp.js for Promises
r18897 WrappedError: WrappedError,
Jonathan Frederic
Add a WrappedError class
r18895 load_class: load_class,
Jonathan Frederic
Address @takluyver 's review comments
r18935 resolve_promises_dict: resolve_promises_dict,
Jonathan Frederic
Add a WrappedError class
r18895 reject: reject,
Nicholas Bollweg (Nick)
moving typeset to utils, usage in cell and outputarea
r19235 typeset: typeset,
Min RK
add utils.time...
r19501 time: time,
Stefan van der Walt
Clean up javascript based on js2-mode feedback.
r5479 };
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352
Jonathan Frederic
Progress...
r17196 // Backwards compatability.
Jonathan Frederic
More review changes
r17214 IPython.utils = utils;
Jonathan Frederic
Progress...
r17196
Jonathan Frederic
@carreau review changes
r17204 return utils;
Jonathan Frederic
Make page.html require.js friendly.
r17188 });