From 493c20cd4a66aa5585f260e542798c714de780db Mon Sep 17 00:00:00 2001 From: Chriest Yu Date: Thu, 27 Aug 2015 17:09:19 +0800 Subject: [PATCH] fix path conversion fix path conversion when fromBase or toBase contain backtracks. Same modification as https://github.com/jcppman/require-css/commit/ff30bd1eb2dc238ad48b984af67f79415885b7c2 --- normalize.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/normalize.js b/normalize.js index 41780f3..fc78b60 100644 --- a/normalize.js +++ b/normalize.js @@ -42,6 +42,20 @@ define(function() { // given a relative URI, and two absolute base URIs, convert it from one base to another var protocolRegEx = /[^\:\/]*:\/\/([^\/])*/; var absUrlRegEx = /^(\/|data:)/; + + // given a URI, remove '..' if possible + function normalizeURI(uri) { + var val= uri.split('/').reduce(function(prev, curr, idx) { + if (curr !== '..' || idx === 0) { + prev.push(curr); + } else { + prev.pop(); + } + return prev; + }, []).join('/'); + return val; + } + function convertURIBase(uri, fromBase, toBase) { if (uri.match(absUrlRegEx) || uri.match(protocolRegEx)) return uri; @@ -113,8 +127,8 @@ define(function() { var normalizeCSS = function(source, fromBase, toBase) { - fromBase = removeDoubleSlashes(fromBase); - toBase = removeDoubleSlashes(toBase); + fromBase = normalizeURI(removeDoubleSlashes(fromBase)); + toBase = normalizeURI(removeDoubleSlashes(toBase)); var urlRegEx = /@import\s*("([^"]*)"|'([^']*)')|url\s*\(\s*(\s*"([^"]*)"|'([^']*)'|[^\)]*\s*)\s*\)/ig; var result, url, source;