lint: Run eslint --fix on src/

This commit is contained in:
Richard Hansen 2020-11-23 13:24:19 -05:00 committed by John McLear
parent b8d07a42eb
commit 8e5fd19db2
109 changed files with 9061 additions and 10572 deletions

View file

@ -20,129 +20,119 @@
* limitations under the License.
*/
var Security = require('./security');
const Security = require('./security');
/**
* Generates a random String with the given length. Is needed to generate the Author, Group, readonly, session Ids
*/
function randomString(len) {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var randomstring = '';
len = len || 20
for (var i = 0; i < len; i++)
{
var rnum = Math.floor(Math.random() * chars.length);
const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
let randomstring = '';
len = len || 20;
for (let i = 0; i < len; i++) {
const rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum, rnum + 1);
}
return randomstring;
}
var padutils = {
escapeHtml: function(x) {
escapeHtml(x) {
return Security.escapeHTML(String(x));
},
uniqueId: function() {
var pad = require('./pad').pad; // Sidestep circular dependency
uniqueId() {
const pad = require('./pad').pad; // Sidestep circular dependency
function encodeNum(n, width) {
// returns string that is exactly 'width' chars, padding with zeros
// and taking rightmost digits
return (Array(width + 1).join('0') + Number(n).toString(35)).slice(-width);
}
return [pad.getClientIp(), encodeNum(+new Date, 7), encodeNum(Math.floor(Math.random() * 1e9), 4)].join('.');
return [pad.getClientIp(), encodeNum(+new Date(), 7), encodeNum(Math.floor(Math.random() * 1e9), 4)].join('.');
},
uaDisplay: function(ua) {
var m;
uaDisplay(ua) {
let m;
function clean(a) {
var maxlen = 16;
const maxlen = 16;
a = a.replace(/[^a-zA-Z0-9\.]/g, '');
if (a.length > maxlen)
{
if (a.length > maxlen) {
a = a.substr(0, maxlen);
}
return a;
}
function checkver(name) {
var m = ua.match(RegExp(name + '\\/([\\d\\.]+)'));
if (m && m.length > 1)
{
const m = ua.match(RegExp(`${name}\\/([\\d\\.]+)`));
if (m && m.length > 1) {
return clean(name + m[1]);
}
return null;
}
// firefox
if (checkver('Firefox'))
{
if (checkver('Firefox')) {
return checkver('Firefox');
}
// misc browsers, including IE
m = ua.match(/compatible; ([^;]+);/);
if (m && m.length > 1)
{
if (m && m.length > 1) {
return clean(m[1]);
}
// iphone
if (ua.match(/\(iPhone;/))
{
if (ua.match(/\(iPhone;/)) {
return 'iPhone';
}
// chrome
if (checkver('Chrome'))
{
if (checkver('Chrome')) {
return checkver('Chrome');
}
// safari
m = ua.match(/Safari\/[\d\.]+/);
if (m)
{
var v = '?';
if (m) {
let v = '?';
m = ua.match(/Version\/([\d\.]+)/);
if (m && m.length > 1)
{
if (m && m.length > 1) {
v = m[1];
}
return clean('Safari' + v);
return clean(`Safari${v}`);
}
// everything else
var x = ua.split(' ')[0];
const x = ua.split(' ')[0];
return clean(x);
},
// e.g. "Thu Jun 18 2009 13:09"
simpleDateTime: function(date) {
var d = new Date(+date); // accept either number or date
var dayOfWeek = (['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'])[d.getDay()];
var month = (['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])[d.getMonth()];
var dayOfMonth = d.getDate();
var year = d.getFullYear();
var hourmin = d.getHours() + ":" + ("0" + d.getMinutes()).slice(-2);
return dayOfWeek + ' ' + month + ' ' + dayOfMonth + ' ' + year + ' ' + hourmin;
simpleDateTime(date) {
const d = new Date(+date); // accept either number or date
const dayOfWeek = (['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'])[d.getDay()];
const month = (['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])[d.getMonth()];
const dayOfMonth = d.getDate();
const year = d.getFullYear();
const hourmin = `${d.getHours()}:${(`0${d.getMinutes()}`).slice(-2)}`;
return `${dayOfWeek} ${month} ${dayOfMonth} ${year} ${hourmin}`;
},
findURLs: function(text) {
findURLs(text) {
// copied from ACE
var _REGEX_WORDCHAR = /[\u0030-\u0039\u0041-\u005A\u0061-\u007A\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u00FF\u0100-\u1FFF\u3040-\u9FFF\uF900-\uFDFF\uFE70-\uFEFE\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFDC]/;
var _REGEX_URLCHAR = new RegExp('(' + /[-:@a-zA-Z0-9_.,~%+\/?=&#;()$]/.source + '|' + _REGEX_WORDCHAR.source + ')');
var _REGEX_URL = new RegExp(/(?:(?:https?|s?ftp|ftps|file|nfs):\/\/|(about|geo|mailto|tel):)/.source + _REGEX_URLCHAR.source + '*(?![:.,;])' + _REGEX_URLCHAR.source, 'g');
const _REGEX_WORDCHAR = /[\u0030-\u0039\u0041-\u005A\u0061-\u007A\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u00FF\u0100-\u1FFF\u3040-\u9FFF\uF900-\uFDFF\uFE70-\uFEFE\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFDC]/;
const _REGEX_URLCHAR = new RegExp(`(${/[-:@a-zA-Z0-9_.,~%+\/?=&#;()$]/.source}|${_REGEX_WORDCHAR.source})`);
const _REGEX_URL = new RegExp(`${/(?:(?:https?|s?ftp|ftps|file|nfs):\/\/|(about|geo|mailto|tel):)/.source + _REGEX_URLCHAR.source}*(?![:.,;])${_REGEX_URLCHAR.source}`, 'g');
// returns null if no URLs, or [[startIndex1, url1], [startIndex2, url2], ...]
function _findURLs(text) {
_REGEX_URL.lastIndex = 0;
var urls = null;
var execResult;
while ((execResult = _REGEX_URL.exec(text)))
{
let urls = null;
let execResult;
while ((execResult = _REGEX_URL.exec(text))) {
urls = (urls || []);
var startIndex = execResult.index;
var url = execResult[0];
const startIndex = execResult.index;
const url = execResult[0];
urls.push([startIndex, url]);
}
@ -151,24 +141,21 @@ var padutils = {
return _findURLs(text);
},
escapeHtmlWithClickableLinks: function(text, target) {
var idx = 0;
var pieces = [];
var urls = padutils.findURLs(text);
escapeHtmlWithClickableLinks(text, target) {
let idx = 0;
const pieces = [];
const urls = padutils.findURLs(text);
function advanceTo(i) {
if (i > idx)
{
if (i > idx) {
pieces.push(Security.escapeHTML(text.substring(idx, i)));
idx = i;
}
}
if (urls)
{
for (var j = 0; j < urls.length; j++)
{
var startIndex = urls[j][0];
var href = urls[j][1];
if (urls) {
for (let j = 0; j < urls.length; j++) {
const startIndex = urls[j][0];
const href = urls[j][1];
advanceTo(startIndex);
// Using rel="noreferrer" stops leaking the URL/location of the pad when clicking links in the document.
// Not all browsers understand this attribute, but it's part of the HTML5 standard.
@ -177,7 +164,7 @@ var padutils = {
// https://html.spec.whatwg.org/multipage/links.html#link-type-noopener
// https://mathiasbynens.github.io/rel-noopener/
// https://github.com/ether/etherpad-lite/pull/3636
pieces.push('<a ', (target ? 'target="' + Security.escapeHTMLAttribute(target) + '" ' : ''), 'href="', Security.escapeHTMLAttribute(href), '" rel="noreferrer noopener">');
pieces.push('<a ', (target ? `target="${Security.escapeHTMLAttribute(target)}" ` : ''), 'href="', Security.escapeHTMLAttribute(href), '" rel="noreferrer noopener">');
advanceTo(startIndex + href.length);
pieces.push('</a>');
}
@ -185,78 +172,66 @@ var padutils = {
advanceTo(text.length);
return pieces.join('');
},
bindEnterAndEscape: function(node, onEnter, onEscape) {
bindEnterAndEscape(node, onEnter, onEscape) {
// Use keypress instead of keyup in bindEnterAndEscape
// Keyup event is fired on enter in IME (Input Method Editor), But
// keypress is not. So, I changed to use keypress instead of keyup.
// It is work on Windows (IE8, Chrome 6.0.472), CentOs (Firefox 3.0) and Mac OSX (Firefox 3.6.10, Chrome 6.0.472, Safari 5.0).
if (onEnter)
{
node.keypress(function(evt) {
if (evt.which == 13)
{
if (onEnter) {
node.keypress((evt) => {
if (evt.which == 13) {
onEnter(evt);
}
});
}
if (onEscape)
{
node.keydown(function(evt) {
if (evt.which == 27)
{
if (onEscape) {
node.keydown((evt) => {
if (evt.which == 27) {
onEscape(evt);
}
});
}
},
timediff: function(d) {
var pad = require('./pad').pad; // Sidestep circular dependency
timediff(d) {
const pad = require('./pad').pad; // Sidestep circular dependency
function format(n, word) {
n = Math.round(n);
return ('' + n + ' ' + word + (n != 1 ? 's' : '') + ' ago');
return (`${n} ${word}${n != 1 ? 's' : ''} ago`);
}
d = Math.max(0, (+(new Date) - (+d) - pad.clientTimeOffset) / 1000);
if (d < 60)
{
d = Math.max(0, (+(new Date()) - (+d) - pad.clientTimeOffset) / 1000);
if (d < 60) {
return format(d, 'second');
}
d /= 60;
if (d < 60)
{
if (d < 60) {
return format(d, 'minute');
}
d /= 60;
if (d < 24)
{
if (d < 24) {
return format(d, 'hour');
}
d /= 24;
return format(d, 'day');
},
makeAnimationScheduler: function(funcToAnimateOneStep, stepTime, stepsAtOnce) {
if (stepsAtOnce === undefined)
{
makeAnimationScheduler(funcToAnimateOneStep, stepTime, stepsAtOnce) {
if (stepsAtOnce === undefined) {
stepsAtOnce = 1;
}
var animationTimer = null;
let animationTimer = null;
function scheduleAnimation() {
if (!animationTimer)
{
animationTimer = window.setTimeout(function() {
if (!animationTimer) {
animationTimer = window.setTimeout(() => {
animationTimer = null;
var n = stepsAtOnce;
var moreToDo = true;
while (moreToDo && n > 0)
{
let n = stepsAtOnce;
let moreToDo = true;
while (moreToDo && n > 0) {
moreToDo = funcToAnimateOneStep();
n--;
}
if (moreToDo)
{
if (moreToDo) {
// more to do
scheduleAnimation();
}
@ -264,15 +239,15 @@ var padutils = {
}
}
return {
scheduleAnimation: scheduleAnimation
scheduleAnimation,
};
},
makeShowHideAnimator: function(funcToArriveAtState, initiallyShown, fps, totalMs) {
var animationState = (initiallyShown ? 0 : -2); // -2 hidden, -1 to 0 fade in, 0 to 1 fade out
var animationFrameDelay = 1000 / fps;
var animationStep = animationFrameDelay / totalMs;
makeShowHideAnimator(funcToArriveAtState, initiallyShown, fps, totalMs) {
let animationState = (initiallyShown ? 0 : -2); // -2 hidden, -1 to 0 fade in, 0 to 1 fade out
const animationFrameDelay = 1000 / fps;
const animationStep = animationFrameDelay / totalMs;
var scheduleAnimation = padutils.makeAnimationScheduler(animateOneStep, animationFrameDelay).scheduleAnimation;
const scheduleAnimation = padutils.makeAnimationScheduler(animateOneStep, animationFrameDelay).scheduleAnimation;
function doShow() {
animationState = -1;
@ -281,12 +256,9 @@ var padutils = {
}
function doQuickShow() { // start showing without losing any fade-in progress
if (animationState < -1)
{
if (animationState < -1) {
animationState = -1;
}
else if (animationState > 0)
{
} else if (animationState > 0) {
animationState = Math.max(-1, Math.min(0, -animationState));
}
funcToArriveAtState(animationState);
@ -294,47 +266,35 @@ var padutils = {
}
function doHide() {
if (animationState >= -1 && animationState <= 0)
{
if (animationState >= -1 && animationState <= 0) {
animationState = 1e-6;
scheduleAnimation();
}
}
function animateOneStep() {
if (animationState < -1 || animationState == 0)
{
if (animationState < -1 || animationState == 0) {
return false;
}
else if (animationState < 0)
{
} else if (animationState < 0) {
// animate show
animationState += animationStep;
if (animationState >= 0)
{
if (animationState >= 0) {
animationState = 0;
funcToArriveAtState(animationState);
return false;
}
else
{
} else {
funcToArriveAtState(animationState);
return true;
}
}
else if (animationState > 0)
{
} else if (animationState > 0) {
// animate hide
animationState += animationStep;
if (animationState >= 1)
{
if (animationState >= 1) {
animationState = 1;
funcToArriveAtState(animationState);
animationState = -2;
return false;
}
else
{
} else {
funcToArriveAtState(animationState);
return true;
}
@ -344,95 +304,83 @@ var padutils = {
return {
show: doShow,
hide: doHide,
quickShow: doQuickShow
quickShow: doQuickShow,
};
},
_nextActionId: 1,
uncanceledActions: {},
getCancellableAction: function(actionType, actionFunc) {
var o = padutils.uncanceledActions[actionType];
if (!o)
{
getCancellableAction(actionType, actionFunc) {
let o = padutils.uncanceledActions[actionType];
if (!o) {
o = {};
padutils.uncanceledActions[actionType] = o;
}
var actionId = (padutils._nextActionId++);
const actionId = (padutils._nextActionId++);
o[actionId] = true;
return function() {
var p = padutils.uncanceledActions[actionType];
if (p && p[actionId])
{
return function () {
const p = padutils.uncanceledActions[actionType];
if (p && p[actionId]) {
actionFunc();
}
};
},
cancelActions: function(actionType) {
var o = padutils.uncanceledActions[actionType];
if (o)
{
cancelActions(actionType) {
const o = padutils.uncanceledActions[actionType];
if (o) {
// clear it
delete padutils.uncanceledActions[actionType];
}
},
makeFieldLabeledWhenEmpty: function(field, labelText) {
makeFieldLabeledWhenEmpty(field, labelText) {
field = $(field);
function clear() {
field.addClass('editempty');
field.val(labelText);
}
field.focus(function() {
if (field.hasClass('editempty'))
{
field.focus(() => {
if (field.hasClass('editempty')) {
field.val('');
}
field.removeClass('editempty');
});
field.blur(function() {
if (!field.val())
{
field.blur(() => {
if (!field.val()) {
clear();
}
});
return {
clear: clear
clear,
};
},
getCheckbox: function(node) {
getCheckbox(node) {
return $(node).is(':checked');
},
setCheckbox: function(node, value) {
if (value)
{
setCheckbox(node, value) {
if (value) {
$(node).attr('checked', 'checked');
}
else
{
} else {
$(node).removeAttr('checked');
}
},
bindCheckboxChange: function(node, func) {
bindCheckboxChange(node, func) {
$(node).change(func);
},
encodeUserId: function(userId) {
return userId.replace(/[^a-y0-9]/g, function(c) {
if (c == ".") return "-";
return 'z' + c.charCodeAt(0) + 'z';
encodeUserId(userId) {
return userId.replace(/[^a-y0-9]/g, (c) => {
if (c == '.') return '-';
return `z${c.charCodeAt(0)}z`;
});
},
decodeUserId: function(encodedUserId) {
return encodedUserId.replace(/[a-y0-9]+|-|z.+?z/g, function(cc) {
if (cc == '-') return '.';
else if (cc.charAt(0) == 'z')
{
decodeUserId(encodedUserId) {
return encodedUserId.replace(/[a-y0-9]+|-|z.+?z/g, (cc) => {
if (cc == '-') { return '.'; } else if (cc.charAt(0) == 'z') {
return String.fromCharCode(Number(cc.slice(1, -1)));
}
else
{
} else {
return cc;
}
});
}
},
};
let globalExceptionHandler = null;
@ -453,12 +401,12 @@ padutils.setupGlobalExceptionHandler = () => {
} else {
throw new Error(`unknown event: ${e.toString()}`);
}
var errorId = randomString(20);
const errorId = randomString(20);
var msgAlreadyVisible = false;
$('.gritter-item .error-msg').each(function() {
let msgAlreadyVisible = false;
$('.gritter-item .error-msg').each(function () {
if ($(this).text() === msg) {
msgAlreadyVisible = true;
msgAlreadyVisible = true;
}
});
@ -479,9 +427,9 @@ padutils.setupGlobalExceptionHandler = () => {
];
$.gritter.add({
title: "An error occurred",
title: 'An error occurred',
text: errorMsg,
class_name: "error",
class_name: 'error',
position: 'bottom',
sticky: true,
});
@ -505,7 +453,7 @@ padutils.setupGlobalExceptionHandler = () => {
window.addEventListener('error', globalExceptionHandler);
window.addEventListener('unhandledrejection', globalExceptionHandler);
}
}
};
padutils.binarySearch = require('./ace2_common').binarySearch;