)|(?:<\/div>)|(?:\{[^\{\}]*\})/g, '')
- // collapse whitespace
- .replace(/([\n\r]+ ?)+/g, '\n')
- .replace(/[\t ]+/g, ' ');
-
- translations = removeTranslatorPatterns(translations);
-
- return {
- namespace: namespace,
- translations: translations,
- };
- });
-
+ // use cache if exists, else make it
+ fallbackCache[namespace] = fallbackCache[namespace] || initFallback(namespace);
return fallbackCache[namespace];
}
function initDict(language) {
return getAdminNamespaces().then(function (namespaces) {
return Promise.all(namespaces.map(function (namespace) {
- return loadLanguage(language, namespace).then(function (translations) {
- return { namespace: namespace, translations: translations };
- }).then(function (params) {
- var namespace = params.namespace;
- var translations = params.translations;
+ return loadLanguage(language, namespace)
+ .then(function (translations) {
+ // join all translations into one string separated by newlines
+ var str = Object.keys(translations).map(function (key) {
+ return translations[key];
+ }).join('\n');
- var str = Object.keys(translations).map(function (key) {
- return translations[key];
- }).join('\n');
-
- return {
- namespace: namespace,
- translations: str
- };
- })
- // TODO: Use translator to get title for admin route?
- .catch(function () {
- return fallback(namespace);
- })
- .catch(function () {
- return { namespace: namespace, translations: '' };
- });
+ return {
+ namespace: namespace,
+ translations: str,
+ };
+ })
+ // TODO: Use translator to get title for admin route?
+ .catch(function () {
+ // no translations for this route, fallback to template
+ return fallback(namespace);
+ })
+ .catch(function () {
+ // no fallback, just return blank
+ return {
+ namespace: namespace,
+ translations: '',
+ };
+ });
}));
});
}
var cache = {};
-function getDict(language, term) {
+function getDict(language) {
+ // use cache if exists, else make it
cache[language] = cache[language] || initDict(language);
return cache[language];
}
module.exports.getDict = getDict;
+module.exports.filterDirectories = filterDirectories;
+module.exports.simplify = simplify;
+module.exports.sanitize = sanitize;
diff --git a/test/search-admin.js b/test/search-admin.js
new file mode 100644
index 0000000000..5351018057
--- /dev/null
+++ b/test/search-admin.js
@@ -0,0 +1,82 @@
+'use strict';
+/*global require*/
+
+var assert = require('assert');
+var search = require('../src/admin/search.js');
+
+describe('admin search', function () {
+ describe('filterDirectories', function () {
+ it('should resolve all paths to relative paths', function (done) {
+ assert.deepEqual(search.filterDirectories([
+ 'hfjksfd/fdsgagag/admin/gdhgfsdg/sggag.tpl',
+ ]), [
+ 'admin/gdhgfsdg/sggag',
+ ]);
+ done();
+ });
+ it('should exclude partials', function (done) {
+ assert.deepEqual(search.filterDirectories([
+ 'hfjksfd/fdsgagag/admin/gdhgfsdg/sggag.tpl',
+ 'dfahdfsgf/admin/partials/hgkfds/fdhsdfh.tpl',
+ ]), [
+ 'admin/gdhgfsdg/sggag',
+ ]);
+ done();
+ });
+ it('should exclude files in the admin directory', function (done) {
+ assert.deepEqual(search.filterDirectories([
+ 'hfjksfd/fdsgagag/admin/gdhgfsdg/sggag.tpl',
+ 'dfdasg/admin/hjkdfsk.tpl',
+ ]), [
+ 'admin/gdhgfsdg/sggag',
+ ]);
+ done();
+ });
+ });
+
+ describe('sanitize', function () {
+ it('should strip out scripts', function (done) {
+ assert.equal(
+ search.sanitize('Pellentesque tristique senectus' +
+ ' habitant morbi'),
+ 'Pellentesque tristique senectus' +
+ ' habitant morbi'
+ );
+ done();
+ });
+ it('should remove all tags', function (done) {
+ assert.equal(
+ search.sanitize('
Pellentesque habitant morbi tristique senectus' +
+ 'Aenean vitae est.Mauris eleifend leo.
'),
+ 'Pellentesque habitant morbi tristique senectus' +
+ 'Aenean vitae est.Mauris eleifend leo.'
+ );
+ done();
+ });
+ });
+
+ describe('simplify', function () {
+ it('should remove all mustaches', function (done) {
+ assert.equal(
+ search.simplify(
+ 'Pellentesque tristique {{senectus}}habitant morbi' +
+ 'liquam tincidunt {{mauris.eu}}risus'
+ ),
+ 'Pellentesque tristique habitant morbi' +
+ 'liquam tincidunt risus'
+ );
+ done();
+ });
+ it('should collapse all whitespace', function (done) {
+ assert.equal(
+ search.simplify(
+ 'Pellentesque tristique habitant morbi' +
+ ' \n\n liquam tincidunt mauris eu risus.'
+ ),
+ 'Pellentesque tristique habitant morbi' +
+ '\nliquam tincidunt mauris eu risus.'
+ );
+ done();
+ });
+ });
+});
\ No newline at end of file
diff --git a/test/translator.js b/test/translator.js
index 9df035cfde..91fbf8f696 100644
--- a/test/translator.js
+++ b/test/translator.js
@@ -233,3 +233,15 @@ describe('Translator modules', function () {
done();
});
});
+
+describe('Translator static methods', function () {
+ describe('.removePatterns', function () {
+ it('should remove translator patterns from text', function (done) {
+ assert.strictEqual(
+ Translator.removePatterns('Lorem ipsum dolor [[sit:amet]], consectetur adipiscing elit. [[sed:vitae, [[semper:dolor]]]] lorem'),
+ 'Lorem ipsum dolor , consectetur adipiscing elit. lorem'
+ );
+ done();
+ });
+ });
+});