From bee0db865bda1704301ba5bacbd04ae8bb29d478 Mon Sep 17 00:00:00 2001 From: NoCLin Date: Tue, 31 Jul 2018 09:14:03 +0800 Subject: [PATCH] fix: resolve './' and '../' (#590) --- src/core/router/history/base.js | 13 ++++++++----- src/core/router/util.js | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/core/router/history/base.js b/src/core/router/history/base.js index d89e3d567..3d3b5b508 100755 --- a/src/core/router/history/base.js +++ b/src/core/router/history/base.js @@ -3,7 +3,8 @@ import { isAbsolutePath, stringifyQuery, cleanPath, - replaceSlug + replaceSlug, + resolvePath } from '../util' import {noop, merge} from '../../util/core' @@ -76,11 +77,13 @@ export class History { (idIndex > 0 ? currentRoute.substr(0, idIndex) : currentRoute) + path } - if (currentRoute !== undefined) { - currentRoute = currentRoute.substr(0, currentRoute.lastIndexOf('/') + 1) - console.log(currentRoute, path) + if (path.startsWith('/')) { + return cleanPath(path) } - return cleanPath(path.startsWith('/') ? path : currentRoute + path) + if (currentRoute !== undefined) { + const currentDir = currentRoute.substr(0, currentRoute.lastIndexOf('/') + 1) + return cleanPath(resolvePath(currentDir + path)) + } } } diff --git a/src/core/router/util.js b/src/core/router/util.js index 217461f47..117a090f4 100644 --- a/src/core/router/util.js +++ b/src/core/router/util.js @@ -60,3 +60,19 @@ export function getPath(...args) { export const replaceSlug = cached(path => { return path.replace('#', '?id=') }) + +export const resolvePath = cached(path => { + const segments = path.replace(/^\//, '').split('/') + let resolved = [] + + for (let i = 0, len = segments.length; i < len; i++) { + const segment = segments[i] + + if (segment === '..') { + resolved.pop() + } else if (segment !== '.') { + resolved.push(segment) + } + } + return '/' + resolved.join('/') +})