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,
});
});
}
}

View file

@ -1,7 +1,7 @@
'use strict';
const Entities = {
' ': ' ',
// ' ': ' ',
'&lt;': '<',
'&gt;': '>',
'&quot;': '"',

View file

@ -119,7 +119,7 @@
"onezip": "^5.0.0",
"open": "^8.0.5",
"package-json": "^7.0.0",
"ponse": "^6.0.0",
"ponse": "^7.0.0",
"pullout": "^4.0.0",
"putout": "^25.0.1",
"redzip": "^2.0.0",

View file

@ -229,7 +229,7 @@ test('cloudcmd: route: not found', async (t) => {
test('cloudcmd: route: sendIndex: encode', async (t) => {
const name = '"><svg onload=alert(3);>';
const nameEncoded = '&quot;&gt;&lt;svg&nbsp;onload=alert(3);&gt;';
const nameEncoded = '&quot;&gt;&lt;svg onload=alert(3);&gt;';
const path = '/';
const files = [{
name,

View file

@ -5,21 +5,21 @@ const entity = require('../../common/entity');
test('cloudcmd: entity: encode', (t) => {
const result = entity.encode('<hello> ');
const expected = '&lt;hello&gt;&nbsp;';
const expected = '&lt;hello&gt; ';
t.equal(result, expected, 'should encode entity');
t.end();
});
test('cloudcmd: entity: decode', (t) => {
const result = entity.decode('&lt;hello&gt;&nbsp;');
const result = entity.decode('&lt;hello&gt; ');
const expected = '<hello> ';
t.equal(result, expected, 'should decode entity');
t.end();
});
test('cloudcmd: entity: encode', (t) => {
test('cloudcmd: entity: encode quote', (t) => {
const result = entity.encode('"hello"');
const expected = '&quot;hello&quot;';