mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 10:45:47 +00:00
feature(cloudcmd) improve support of NBSP
This commit is contained in:
parent
e129ab6a79
commit
42c8fd7bfe
8 changed files with 40 additions and 39 deletions
|
|
@ -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);
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
|
|
|
|||
|
|
@ -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
|
||||
*
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
const Entities = {
|
||||
' ': ' ',
|
||||
// ' ': ' ',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'"': '"',
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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 = '"><svg onload=alert(3);>';
|
||||
const nameEncoded = '"><svg onload=alert(3);>';
|
||||
const path = '/';
|
||||
const files = [{
|
||||
name,
|
||||
|
|
|
|||
|
|
@ -5,21 +5,21 @@ const entity = require('../../common/entity');
|
|||
|
||||
test('cloudcmd: entity: encode', (t) => {
|
||||
const result = entity.encode('<hello> ');
|
||||
const expected = '<hello> ';
|
||||
const expected = '<hello> ';
|
||||
|
||||
t.equal(result, expected, 'should encode entity');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('cloudcmd: entity: decode', (t) => {
|
||||
const result = entity.decode('<hello> ');
|
||||
const result = entity.decode('<hello> ');
|
||||
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 = '"hello"';
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue