diff options
author | Azul <azul@riseup.net> | 2020-06-18 11:11:23 +0300 |
---|---|---|
committer | Azul <azul@riseup.net> | 2020-08-11 16:43:33 +0300 |
commit | 5349987621145bb2d3884eddd2e63a3d33fea9d4 (patch) | |
tree | 464adf42c8bfa699ab982320a428d08d7c22ae64 /src | |
parent | 56701f9e7b9c93e1d4b1ab10b041f132e21701b1 (diff) |
calculate dir for relative links
Signed-off-by: Azul <azul@riseup.net>
Diffstat (limited to 'src')
-rw-r--r-- | src/helpers/links.js | 33 | ||||
-rw-r--r-- | src/tests/helpers/links.spec.js | 19 |
2 files changed, 47 insertions, 5 deletions
diff --git a/src/helpers/links.js b/src/helpers/links.js index 344a9ee0d..9da27351f 100644 --- a/src/helpers/links.js +++ b/src/helpers/links.js @@ -20,6 +20,31 @@ * */ +const absolutePath = function(base, rel) { + if (!rel) { + return base + } + if (rel[0] === '/') { + return rel + } + base = base.split('/') + rel = rel.split('/') + while (rel[0] === '..' || rel[0] === '.') { + if (rel[0] === '..') { + base.pop() + } + rel.shift() + } + return base.concat(rel).join('/') +} + +const basedir = function(file) { + const end = file.lastIndexOf('/') + return (end > 0) + ? file.slice(0, end) + : file.slice(0, end + 1) // basedir('/toplevel') should return '/' +} + const domHref = function(node) { const ref = node.attrs.href if (!ref) { @@ -30,9 +55,11 @@ const domHref = function(node) { } const match = ref.match(/^([^?]*)\?fileId=(\d*)/) if (match) { - const [, path, id] = match - const dir = OC.Util.History.parseUrlQuery().dir - return `?dir=${dir}&openfile=${id}&relPath=${path}` + const [, relPath, id] = match + const currentDir = OC.Util.History.parseUrlQuery().dir + || basedir(OCA.Viewer.state.file) + const dir = absolutePath(currentDir, basedir(relPath)) + return `?dir=${dir}&openfile=${id}&relPath=${relPath}` } } diff --git a/src/tests/helpers/links.spec.js b/src/tests/helpers/links.spec.js index 9317e320c..ece8dd715 100644 --- a/src/tests/helpers/links.spec.js +++ b/src/tests/helpers/links.spec.js @@ -35,6 +35,21 @@ describe('Preparing href attributes for the DOM', () => { .toBe('?dir=/Wiki&openfile=123&relPath=otherfile') }) + test('relative path with ../', () => { + expect(domHref({attrs: {href: '../other/otherfile?fileId=123'}})) + .toBe('?dir=/other&openfile=123&relPath=../other/otherfile') + }) + + test('absolute path', () => { + expect(domHref({attrs: {href: '/other/otherfile?fileId=123'}})) + .toBe('?dir=/other&openfile=123&relPath=/other/otherfile') + }) + + test('absolute path', () => { + expect(domHref({attrs: {href: '/otherfile?fileId=123'}})) + .toBe('?dir=/&openfile=123&relPath=/otherfile') + }) + }) describe('Extracting short urls from the DOM', () => { @@ -54,8 +69,8 @@ describe('Extracting short urls from the DOM', () => { }) test('relative link with fileid', () => { - expect(parseHref(domStub('?dir=/Wiki&openfile=123&relPath=otherfile'))) - .toBe('otherfile?fileId=123') + expect(parseHref(domStub('?dir=/other&openfile=123&relPath=../other/otherfile'))) + .toBe('../other/otherfile?fileId=123') }) }) |