2016-02-12 13:31:26 -05:00
|
|
|
/* global Int8Array:true */
|
2016-06-14 14:31:51 -04:00
|
|
|
import {
|
2020-06-17 06:37:06 -04:00
|
|
|
escapeExpression,
|
2016-06-14 14:31:51 -04:00
|
|
|
emailValid,
|
2016-06-15 13:49:57 -04:00
|
|
|
extractDomainFromUrl,
|
2016-06-14 14:31:51 -04:00
|
|
|
avatarUrl,
|
|
|
|
getRawSize,
|
|
|
|
avatarImg,
|
|
|
|
defaultHomepage,
|
2017-11-09 14:45:19 -05:00
|
|
|
setDefaultHomepage,
|
2016-06-14 14:31:51 -04:00
|
|
|
caretRowCol,
|
2018-01-12 21:43:49 -05:00
|
|
|
setCaretPosition,
|
2020-05-04 21:05:25 -04:00
|
|
|
toAsciiPrintable,
|
|
|
|
slugify,
|
2019-12-09 08:07:15 -05:00
|
|
|
fillMissingDates,
|
|
|
|
inCodeBlock
|
2016-06-14 14:31:51 -04:00
|
|
|
} from "discourse/lib/utilities";
|
2020-06-17 06:37:06 -04:00
|
|
|
import Handlebars from "handlebars";
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.module("lib:utilities");
|
2013-06-19 15:06:23 -04:00
|
|
|
|
2020-06-17 06:37:06 -04:00
|
|
|
QUnit.test("escapeExpression", assert => {
|
|
|
|
assert.equal(
|
|
|
|
escapeExpression(">"),
|
|
|
|
">",
|
|
|
|
"escapes unsafe characters"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
escapeExpression(new Handlebars.SafeString(">")),
|
|
|
|
">",
|
|
|
|
"does not double-escape safe strings"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
escapeExpression(undefined),
|
|
|
|
"",
|
|
|
|
"returns a falsy string when given a falsy value"
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("emailValid", assert => {
|
|
|
|
assert.ok(
|
|
|
|
emailValid("Bob@example.com"),
|
|
|
|
"allows upper case in the first part of emails"
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
emailValid("bob@EXAMPLE.com"),
|
|
|
|
"allows upper case in the email domain"
|
|
|
|
);
|
2013-06-19 15:06:23 -04:00
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("extractDomainFromUrl", assert => {
|
|
|
|
assert.equal(
|
|
|
|
extractDomainFromUrl("http://meta.discourse.org:443/random"),
|
|
|
|
"meta.discourse.org",
|
|
|
|
"extract domain name from url"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
extractDomainFromUrl("meta.discourse.org:443/random"),
|
|
|
|
"meta.discourse.org",
|
|
|
|
"extract domain regardless of scheme presence"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
extractDomainFromUrl("http://192.168.0.1:443/random"),
|
|
|
|
"192.168.0.1",
|
|
|
|
"works for IP address"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
extractDomainFromUrl("http://localhost:443/random"),
|
|
|
|
"localhost",
|
|
|
|
"works for localhost"
|
|
|
|
);
|
2016-06-15 13:49:57 -04:00
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("avatarUrl", assert => {
|
2016-06-14 14:31:51 -04:00
|
|
|
var rawSize = getRawSize;
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.blank(avatarUrl("", "tiny"), "no template returns blank");
|
|
|
|
assert.equal(
|
|
|
|
avatarUrl("/fake/template/{size}.png", "tiny"),
|
|
|
|
"/fake/template/" + rawSize(20) + ".png",
|
|
|
|
"simple avatar url"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
avatarUrl("/fake/template/{size}.png", "large"),
|
|
|
|
"/fake/template/" + rawSize(45) + ".png",
|
|
|
|
"different size"
|
|
|
|
);
|
2013-07-18 12:03:09 -04:00
|
|
|
});
|
|
|
|
|
2015-03-20 08:53:56 -04:00
|
|
|
var setDevicePixelRatio = function(value) {
|
2015-08-11 12:27:07 -04:00
|
|
|
if (Object.defineProperty && !window.hasOwnProperty("devicePixelRatio")) {
|
|
|
|
Object.defineProperty(window, "devicePixelRatio", { value: 2 });
|
2015-03-20 08:53:56 -04:00
|
|
|
} else {
|
|
|
|
window.devicePixelRatio = value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("avatarImg", assert => {
|
2014-05-27 00:40:46 -04:00
|
|
|
var oldRatio = window.devicePixelRatio;
|
2015-03-20 08:53:56 -04:00
|
|
|
setDevicePixelRatio(2);
|
2014-05-27 00:40:46 -04:00
|
|
|
|
2013-08-13 16:08:29 -04:00
|
|
|
var avatarTemplate = "/path/to/avatar/{size}.png";
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(
|
|
|
|
avatarImg({ avatarTemplate: avatarTemplate, size: "tiny" }),
|
2015-12-15 03:56:45 -05:00
|
|
|
"<img alt='' width='20' height='20' src='/path/to/avatar/40.png' class='avatar'>",
|
2013-07-18 12:03:09 -04:00
|
|
|
"it returns the avatar html"
|
|
|
|
);
|
2018-06-15 11:03:24 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(
|
|
|
|
avatarImg({
|
|
|
|
avatarTemplate: avatarTemplate,
|
|
|
|
size: "tiny",
|
|
|
|
title: "evilest trout"
|
|
|
|
}),
|
2020-03-02 14:28:54 -05:00
|
|
|
"<img alt='' width='20' height='20' src='/path/to/avatar/40.png' class='avatar' title='evilest trout' aria-label='evilest trout'>",
|
2013-07-18 12:03:09 -04:00
|
|
|
"it adds a title if supplied"
|
|
|
|
);
|
2018-06-15 11:03:24 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(
|
|
|
|
avatarImg({
|
|
|
|
avatarTemplate: avatarTemplate,
|
|
|
|
size: "tiny",
|
|
|
|
extraClasses: "evil fish"
|
|
|
|
}),
|
2015-12-15 03:56:45 -05:00
|
|
|
"<img alt='' width='20' height='20' src='/path/to/avatar/40.png' class='avatar evil fish'>",
|
2013-07-18 12:03:09 -04:00
|
|
|
"it adds extra classes if supplied"
|
|
|
|
);
|
2018-06-15 11:03:24 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.blank(
|
|
|
|
avatarImg({ avatarTemplate: "", size: "tiny" }),
|
2013-08-13 16:08:29 -04:00
|
|
|
"it doesn't render avatars for invalid avatar template"
|
|
|
|
);
|
2014-05-27 00:40:46 -04:00
|
|
|
|
2015-03-20 08:53:56 -04:00
|
|
|
setDevicePixelRatio(oldRatio);
|
2013-07-21 20:39:17 -04:00
|
|
|
});
|
2013-08-23 12:48:29 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("defaultHomepage", assert => {
|
2013-12-30 12:46:18 -05:00
|
|
|
Discourse.SiteSettings.top_menu = "latest|top|hot";
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(
|
|
|
|
defaultHomepage(),
|
|
|
|
"latest",
|
|
|
|
"default homepage is the first item in the top_menu site setting"
|
|
|
|
);
|
2017-11-09 14:45:19 -05:00
|
|
|
var meta = document.createElement("meta");
|
|
|
|
meta.name = "discourse_current_homepage";
|
|
|
|
meta.content = "hot";
|
|
|
|
document.body.appendChild(meta);
|
|
|
|
assert.equal(
|
|
|
|
defaultHomepage(),
|
|
|
|
"hot",
|
|
|
|
"default homepage is pulled from <meta name=discourse_current_homepage>"
|
|
|
|
);
|
|
|
|
document.body.removeChild(meta);
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test("setDefaultHomepage", assert => {
|
|
|
|
var meta = document.createElement("meta");
|
|
|
|
meta.name = "discourse_current_homepage";
|
|
|
|
meta.content = "hot";
|
|
|
|
document.body.appendChild(meta);
|
|
|
|
setDefaultHomepage("top");
|
|
|
|
assert.equal(
|
|
|
|
meta.content,
|
|
|
|
"top",
|
|
|
|
"default homepage set by setDefaultHomepage"
|
|
|
|
);
|
|
|
|
document.body.removeChild(meta);
|
2013-12-30 12:46:18 -05:00
|
|
|
});
|
2015-12-28 01:28:16 -05:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("caretRowCol", assert => {
|
2015-12-28 01:28:16 -05:00
|
|
|
var textarea = document.createElement("textarea");
|
|
|
|
const content = document.createTextNode("01234\n56789\n012345");
|
|
|
|
textarea.appendChild(content);
|
|
|
|
document.body.appendChild(textarea);
|
|
|
|
|
|
|
|
const assertResult = (setCaretPos, expectedRowNum, expectedColNum) => {
|
2016-06-14 14:31:51 -04:00
|
|
|
setCaretPosition(textarea, setCaretPos);
|
2015-12-28 01:28:16 -05:00
|
|
|
|
2016-06-14 14:31:51 -04:00
|
|
|
const result = caretRowCol(textarea);
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(
|
|
|
|
result.rowNum,
|
|
|
|
expectedRowNum,
|
|
|
|
"returns the right row of the caret"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
result.colNum,
|
|
|
|
expectedColNum,
|
|
|
|
"returns the right col of the caret"
|
|
|
|
);
|
2015-12-28 01:28:16 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
assertResult(0, 1, 0);
|
|
|
|
assertResult(5, 1, 5);
|
|
|
|
assertResult(6, 2, 0);
|
|
|
|
assertResult(11, 2, 5);
|
|
|
|
assertResult(14, 3, 2);
|
|
|
|
|
|
|
|
document.body.removeChild(textarea);
|
2017-07-25 15:11:28 -04:00
|
|
|
});
|
2018-01-12 21:43:49 -05:00
|
|
|
|
2020-05-04 21:05:25 -04:00
|
|
|
QUnit.test("toAsciiPrintable", assert => {
|
|
|
|
const accentedString = "Créme_Brûlée!";
|
|
|
|
const unicodeString = "談話";
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
toAsciiPrintable(accentedString, "discourse"),
|
|
|
|
"Creme_Brulee!",
|
|
|
|
"it replaces accented characters with the appropriate ASCII equivalent"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
toAsciiPrintable(unicodeString, "discourse"),
|
|
|
|
"discourse",
|
|
|
|
"it uses the fallback string when unable to convert"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
typeof toAsciiPrintable(unicodeString),
|
|
|
|
"undefined",
|
|
|
|
"it returns undefined when unable to convert and no fallback is provided"
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test("slugify", assert => {
|
|
|
|
const asciiString = "--- 0__( Some-cool Discourse Site! )__0 --- ";
|
|
|
|
const accentedString = "Créme_Brûlée!";
|
|
|
|
const unicodeString = "談話";
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
slugify(asciiString),
|
|
|
|
"0-some-cool-discourse-site-0",
|
|
|
|
"it properly slugifies an ASCII string"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
slugify(accentedString),
|
|
|
|
"crme-brle",
|
|
|
|
"it removes accented characters"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(slugify(unicodeString), "", "it removes unicode characters");
|
|
|
|
});
|
|
|
|
|
2018-01-12 21:43:49 -05:00
|
|
|
QUnit.test("fillMissingDates", assert => {
|
|
|
|
const startDate = "2017-11-12"; // YYYY-MM-DD
|
|
|
|
const endDate = "2017-12-12"; // YYYY-MM-DD
|
|
|
|
const data =
|
|
|
|
'[{"x":"2017-11-12","y":3},{"x":"2017-11-27","y":2},{"x":"2017-12-06","y":9},{"x":"2017-12-11","y":2}]';
|
2018-06-15 11:03:24 -04:00
|
|
|
|
2018-01-12 21:43:49 -05:00
|
|
|
assert.equal(
|
|
|
|
fillMissingDates(JSON.parse(data), startDate, endDate).length,
|
|
|
|
31,
|
|
|
|
"it returns a JSON array with 31 dates"
|
|
|
|
);
|
|
|
|
});
|
2019-12-09 08:07:15 -05:00
|
|
|
|
|
|
|
QUnit.test("inCodeBlock", assert => {
|
|
|
|
const text =
|
|
|
|
"000\n\n```\n111\n```\n\n000\n\n`111 111`\n\n000\n\n[code]\n111\n[/code]\n\n 111\n\t111\n\n000`000";
|
|
|
|
for (let i = 0; i < text.length; ++i) {
|
|
|
|
if (text[i] === "0") {
|
|
|
|
assert.notOk(inCodeBlock(text, i), `position ${i} is not in code block`);
|
|
|
|
} else if (text[i] === "1") {
|
|
|
|
assert.ok(inCodeBlock(text, i), `position ${i} is in code block`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.skip("inCodeBlock - runs fast", assert => {
|
|
|
|
const phrase = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
|
|
|
|
const text = `${phrase}\n\n\`\`\`\n${phrase}\n\`\`\`\n\n${phrase}\n\n\`${phrase}\n${phrase}\n\n${phrase}\n\n[code]\n${phrase}\n[/code]\n\n${phrase}\n\n ${phrase}\n\n\`${phrase}\`\n\n${phrase}`;
|
|
|
|
|
|
|
|
let time = Number.MAX_VALUE;
|
|
|
|
for (let i = 0; i < 10; ++i) {
|
|
|
|
const start = performance.now();
|
|
|
|
inCodeBlock(text, text.length);
|
|
|
|
const end = performance.now();
|
|
|
|
time = Math.min(time, end - start);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This runs in 'keyUp' event handler so it should run as fast as
|
|
|
|
// possible. It should take less than 1ms for the test text.
|
|
|
|
assert.ok(time < 10);
|
|
|
|
});
|