feature(cloudcmd) improve support of NBSP

This commit is contained in:
coderaiser 2022-04-23 01:23:27 +03:00
parent e129ab6a79
commit 42c8fd7bfe
8 changed files with 40 additions and 39 deletions

View file

@ -1,8 +1,3 @@
/**
* Parse a `data-name` attribute string back into the original filename
* @param attribute The string we wish to decode
*/
'use strict';
/* global DOM */
@ -24,8 +19,8 @@ const {
let Title;
const CURRENT_FILE = 'current-file';
const NBSP_REG = RegExp(String.fromCharCode(160), 'g');
const SPACE = ' ';
const encodeNBSP = (a) => a?.replace('\xa0', ' ');
const decodeNBSP = (a) => a?.replace(' ', '\xa0');
module.exports._CURRENT_FILE = CURRENT_FILE;
@ -81,16 +76,22 @@ const createNameAttribute = (name) => {
*/
const parseNameAttribute = (attribute) => {
attribute = attribute.replace('js-file-', '');
return decodeURI(atob(attribute));
return decodeNBSP(decodeURI(atob(attribute)));
};
module.exports._parseNameAttribute = parseNameAttribute;
const parseHrefAttribute = (prefix, attribute) => {
attribute = attribute.replace(RegExp('^' + prefix + FS), '');
return decode(decodeNBSP(attribute));
};
module.exports._parseHrefAttribute = parseHrefAttribute;
/**
* get current direcotory path
*/
module.exports.getCurrentDirPath = (panel = DOM.getPanel()) => {
const path = DOM.getByDataName('js-path', panel);
return path.textContent
.replace(NBSP_REG, SPACE);
return path.textContent;
};
/**
@ -103,12 +104,7 @@ module.exports.getCurrentPath = (currentFile) => {
const [element] = DOM.getByTag('a', current);
const {prefix} = CloudCmd;
const path = element
.getAttribute('href')
.replace(RegExp('^' + prefix + FS), '')
.replace(NBSP_REG, SPACE);
return decode(path);
return parseHrefAttribute(prefix, element.getAttribute('href'));
};
/**
@ -161,8 +157,9 @@ module.exports.getCurrentFile = () => {
/**
* get current file by name
*/
module.exports.getCurrentByName = (name, panel = DOM.CurrentInfo.panel) => {
const dataName = 'js-file-' + btoa(encodeURI(name));
const dataName = 'js-file-' + btoa(encodeURI(encodeNBSP(name)));
return DOM.getByDataName(dataName, panel);
};

View file

@ -302,6 +302,23 @@ function getCloudCmd({emit} = {}) {
};
}
test('current-file: parseNameAttribute', (t) => {
const result = currentFile._parseNameAttribute('js-file-aGVsbG8mbmJzcDt3b3JsZA==');
const expected = 'hello\xa0world';
t.equal(result, expected);
t.end();
});
test('current-file: parseHrefAttribute', (t) => {
const prefix = '/api/v1';
const result = currentFile._parseHrefAttribute(prefix, '/api/v1/fs/hello world');
const expected = '/hello\xa0world';
t.equal(result, expected);
t.end();
});
function getDOM({
link = {},
getCurrentDirPath = stub(),

View file

@ -148,16 +148,6 @@ module.exports.getNotCurrentDirPath = () => {
return path;
};
/**
* get current file by name
*/
module.exports.getCurrentByName = (name, panel = CurrentInfo.panel) => {
const dataName = 'js-file-' + btoa(encodeURI(name));
const element = DOM.getByDataName(dataName, panel);
return element;
};
/**
* unified way to get selected files
*

View file

@ -405,18 +405,15 @@ async function _processFiles(options, data) {
panelPassive,
} = Info;
const setCurrent = () => {
const currentName = name || data.names[0];
DOM.setCurrentByName(currentName);
};
if (!Info.isOnePanel)
CloudCmd.refresh({
panel: panelPassive,
noCurrent: true,
});
CloudCmd.refresh({panel}, setCurrent);
CloudCmd.refresh({
panel,
});
});
}
}