2017-07-16 21:14:03 +02:00
|
|
|
// This reloads the module in development rather than refreshing the page
|
|
|
|
if (module.hot) {
|
|
|
|
module.hot.accept();
|
|
|
|
}
|
|
|
|
|
2019-01-17 16:55:25 +01:00
|
|
|
exports.status_classes = 'alert-error alert-success alert-info alert-warning';
|
2017-06-22 22:08:43 +02:00
|
|
|
|
|
|
|
exports.autofocus = function (selector) {
|
2020-07-02 01:45:54 +02:00
|
|
|
$(() => {
|
2017-07-08 17:43:42 +02:00
|
|
|
$(selector).focus();
|
2012-08-29 17:45:15 +02:00
|
|
|
});
|
2017-06-22 22:08:43 +02:00
|
|
|
};
|
2013-04-03 22:30:36 +02:00
|
|
|
|
2013-04-08 20:21:20 +02:00
|
|
|
// Return a boolean indicating whether the password is acceptable.
|
|
|
|
// Also updates a Bootstrap progress bar control (a jQuery object)
|
|
|
|
// if provided.
|
2013-04-03 22:30:36 +02:00
|
|
|
//
|
|
|
|
// Assumes that zxcvbn.js has been loaded.
|
|
|
|
//
|
|
|
|
// This is in common.js because we want to use it from the signup page
|
|
|
|
// and also from the in-app password change interface.
|
2017-06-22 22:08:43 +02:00
|
|
|
exports.password_quality = function (password, bar, password_field) {
|
2013-04-04 00:55:36 +02:00
|
|
|
// We load zxcvbn.js asynchronously, so the variable might not be set.
|
2013-08-01 17:47:48 +02:00
|
|
|
if (typeof zxcvbn === 'undefined') {
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2013-04-04 00:55:36 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const min_length = password_field.data('minLength');
|
|
|
|
const min_guesses = password_field.data('minGuesses');
|
2017-01-09 18:04:23 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const result = zxcvbn(password);
|
|
|
|
const acceptable = password.length >= min_length
|
2018-06-06 18:19:09 +02:00
|
|
|
&& result.guesses >= min_guesses;
|
2017-01-09 18:04:23 +01:00
|
|
|
|
|
|
|
if (bar !== undefined) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const t = result.crack_times_seconds.offline_slow_hashing_1e4_per_second;
|
|
|
|
let bar_progress = Math.min(1, Math.log(1 + t) / 22);
|
2017-10-03 19:52:38 +02:00
|
|
|
|
|
|
|
// Even if zxcvbn loves your short password, the bar should be
|
|
|
|
// filled at most 1/3 of the way, because we won't accept it.
|
|
|
|
if (!acceptable) {
|
|
|
|
bar_progress = Math.min(bar_progress, 0.33);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The bar bottoms out at 10% so there's always something
|
2013-04-08 20:21:20 +02:00
|
|
|
// for the user to see.
|
2018-06-06 18:50:09 +02:00
|
|
|
bar.width(90 * bar_progress + 10 + '%')
|
2018-05-06 21:43:17 +02:00
|
|
|
.removeClass('bar-success bar-danger')
|
|
|
|
.addClass(acceptable ? 'bar-success' : 'bar-danger');
|
2013-04-08 20:21:20 +02:00
|
|
|
}
|
2013-04-03 22:30:36 +02:00
|
|
|
|
2013-04-08 20:31:00 +02:00
|
|
|
return acceptable;
|
2017-06-22 22:08:43 +02:00
|
|
|
};
|
|
|
|
|
2017-06-29 16:26:48 +02:00
|
|
|
exports.password_warning = function (password, password_field) {
|
|
|
|
if (typeof zxcvbn === 'undefined') {
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2017-06-29 16:26:48 +02:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const min_length = password_field.data('minLength');
|
2017-06-29 16:26:48 +02:00
|
|
|
|
|
|
|
if (password.length < min_length) {
|
|
|
|
return i18n.t('Password should be at least __length__ characters long', {length: min_length});
|
|
|
|
}
|
|
|
|
return zxcvbn(password).feedback.warning || i18n.t("Password is too weak");
|
|
|
|
};
|
|
|
|
|
2018-06-25 17:14:45 +02:00
|
|
|
exports.phrase_match = function (query, phrase) {
|
|
|
|
// match "tes" to "test" and "stream test" but not "hostess"
|
2019-11-02 00:06:25 +01:00
|
|
|
let i;
|
2018-06-25 17:14:45 +02:00
|
|
|
query = query.toLowerCase();
|
|
|
|
|
|
|
|
phrase = phrase.toLowerCase();
|
2020-01-28 15:26:02 +01:00
|
|
|
if (phrase.startsWith(query)) {
|
2018-06-25 17:14:45 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const parts = phrase.split(' ');
|
2018-06-25 17:14:45 +02:00
|
|
|
for (i = 0; i < parts.length; i += 1) {
|
2020-01-28 15:26:02 +01:00
|
|
|
if (parts[i].startsWith(query)) {
|
2018-06-25 17:14:45 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2019-06-12 16:09:24 +02:00
|
|
|
exports.copy_data_attribute_value = function (elem, key) {
|
|
|
|
// function to copy the value of data-key
|
|
|
|
// attribute of the element to clipboard
|
2019-11-02 00:06:25 +01:00
|
|
|
const temp = $(document.createElement('input'));
|
2019-06-12 16:09:24 +02:00
|
|
|
$("body").append(temp);
|
|
|
|
temp.val(elem.data(key)).select();
|
|
|
|
document.execCommand("copy");
|
|
|
|
temp.remove();
|
|
|
|
elem.fadeOut(250);
|
|
|
|
elem.fadeIn(1000);
|
|
|
|
};
|
|
|
|
|
2019-06-10 09:09:04 +02:00
|
|
|
exports.has_mac_keyboard = function () {
|
2019-06-24 14:11:21 +02:00
|
|
|
return /Mac/i.test(navigator.platform);
|
2019-06-10 09:09:04 +02:00
|
|
|
};
|
|
|
|
|
2019-06-10 09:22:55 +02:00
|
|
|
exports.adjust_mac_shortcuts = function (key_elem_class, require_cmd_style) {
|
2019-06-07 11:03:13 +02:00
|
|
|
if (!exports.has_mac_keyboard()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const keys_map = new Map([
|
2019-06-07 11:03:13 +02:00
|
|
|
['Backspace', 'Delete'],
|
|
|
|
['Enter', 'Return'],
|
|
|
|
['Home', 'Fn + ←'],
|
|
|
|
['End', 'Fn + →'],
|
|
|
|
['PgUp', 'Fn + ↑'],
|
|
|
|
['PgDn', 'Fn + ↓'],
|
2019-06-10 09:22:55 +02:00
|
|
|
['Ctrl', '⌘'],
|
2019-06-07 11:03:13 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$(key_elem_class).each(function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
let key_text = $(this).text();
|
2020-02-14 00:57:20 +01:00
|
|
|
const keys = key_text.match(/[^\s\+]+/g) || [];
|
2019-06-07 11:03:13 +02:00
|
|
|
|
js: Convert a.indexOf(…) !== -1 to a.includes(…).
Babel polyfills this for us for Internet Explorer.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import K from "ast-types/gen/kinds";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
recast.visit(ast, {
visitBinaryExpression(path) {
const { operator, left, right } = path.node;
if (
n.CallExpression.check(left) &&
n.MemberExpression.check(left.callee) &&
!left.callee.computed &&
n.Identifier.check(left.callee.property) &&
left.callee.property.name === "indexOf" &&
left.arguments.length === 1 &&
checkExpression(left.arguments[0]) &&
((["===", "!==", "==", "!=", ">", "<="].includes(operator) &&
n.UnaryExpression.check(right) &&
right.operator == "-" &&
n.Literal.check(right.argument) &&
right.argument.value === 1) ||
([">=", "<"].includes(operator) &&
n.Literal.check(right) &&
right.value === 0))
) {
const test = b.callExpression(
b.memberExpression(left.callee.object, b.identifier("includes")),
[left.arguments[0]]
);
path.replace(
["!==", "!=", ">", ">="].includes(operator)
? test
: b.unaryExpression("!", test)
);
changed = true;
}
this.traverse(path);
},
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-08 04:55:06 +01:00
|
|
|
if (key_text.includes('Ctrl') && require_cmd_style) {
|
2019-06-10 09:22:55 +02:00
|
|
|
$(this).addClass("mac-cmd-key");
|
|
|
|
}
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
|
|
|
|
for (const key of keys) {
|
2019-06-07 11:03:13 +02:00
|
|
|
if (keys_map.get(key)) {
|
|
|
|
key_text = key_text.replace(key, keys_map.get(key));
|
|
|
|
}
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
|
|
|
|
2019-06-07 11:03:13 +02:00
|
|
|
$(this).text(key_text);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.common = exports;
|