Przeglądaj źródła

Merge pull request #338 from watagashi/fix-broken-path

Fix broken path
Yuki Takei 8 lat temu
rodzic
commit
634134c9ee

+ 13 - 12
lib/routes/page.js

@@ -11,6 +11,7 @@ module.exports = function(crowi, app) {
     , UserGroupRelation = crowi.model('UserGroupRelation')
     , ApiResponse = require('../util/apiResponse')
     , interceptorManager = crowi.getInterceptorManager()
+    , pagePathUtil = require('../util/pagePathUtil')
 
     , actions = {};
 
@@ -193,7 +194,7 @@ module.exports = function(crowi, app) {
         seener_threshold: SEENER_THRESHOLD,
       };
       renderVars.pager = generatePager(pagerOptions);
-      renderVars.pages = pageList;
+      renderVars.pages = pagePathUtil.encodePagesPath(pageList);
       res.render('customlayout-selector/page_list', renderVars);
     }).catch(function(err) {
       debug('Error on rendering pageListShow', err);
@@ -248,7 +249,7 @@ module.exports = function(crowi, app) {
       if (page.redirectTo) {
         debug(`Redirect to '${page.redirectTo}'`);
         isRedirect = true;
-        return res.redirect(encodeURI(page.redirectTo + '?redirectFrom=' + page.path));
+        return res.redirect(encodeURI(page.redirectTo + '?redirectFrom=' + pagePathUtil.encodePagePath(page.path)));
       }
 
       renderVars.page = page;
@@ -320,7 +321,7 @@ module.exports = function(crowi, app) {
             seener_threshold: SEENER_THRESHOLD,
           };
           renderVars.pager = generatePager(pagerOptions);
-          renderVars.pages = pageList;
+          renderVars.pages = pagePathUtil.encodePagesPath(pageList);
 
           return Promise.resolve();
         })
@@ -379,7 +380,7 @@ module.exports = function(crowi, app) {
       pagerOptions.length = pageList.length;
 
       renderVars.pager = generatePager(pagerOptions);
-      renderVars.pages = pageList;
+      renderVars.pages = pagePathUtil.encodePagesPath(pageList);
       res.render('customlayout-selector/page_list', renderVars);
     }).catch(function(err) {
       debug('Error on rendering deletedPageListShow', err);
@@ -408,7 +409,7 @@ module.exports = function(crowi, app) {
 
       res.render('customlayout-selector/page_list', {
         path: '/',
-        pages: pages,
+        pages: pagePathUtil.encodePagesPath(pages),
         pager: generatePager({offset: 0, limit: 50})
       });
     }).catch(function(err) {
@@ -426,7 +427,7 @@ module.exports = function(crowi, app) {
     }
 
     if (pageData.redirectTo) {
-      return res.redirect(encodeURI(pageData.redirectTo + '?redirectFrom=' + pageData.path));
+      return res.redirect(encodeURI(pageData.redirectTo + '?redirectFrom=' + pagePathUtil.encodePagePath(pageData.path)));
     }
 
     var renderVars = {
@@ -519,7 +520,7 @@ module.exports = function(crowi, app) {
         return ;
       }
       if (req.query.revision) {
-        return res.redirect(encodeURI(path));
+        return res.redirect(pagePathUtil.encodePagePath(path));
       }
 
       if (isMarkdown) {
@@ -529,13 +530,13 @@ module.exports = function(crowi, app) {
       Page.hasPortalPage(path + '/', req.user)
       .then(function(page) {
         if (page) {
-          return res.redirect(encodeURI(path) + '/');
+          return res.redirect(pagePathUtil.encodePagePath(path) + '/');
         } else {
 
           var fixed = Page.fixToCreatableName(path)
           if (fixed !== path) {
             debug('fixed page name', fixed)
-            res.redirect(encodeURI(fixed));
+            res.redirect(pagePathUtil.encodePagePath(fixed));
             return ;
           }
 
@@ -568,7 +569,7 @@ module.exports = function(crowi, app) {
 
     debug('notify: ', notify);
 
-    var redirectPath = encodeURI(path);
+    var redirectPath = pagePathUtil.encodePagePath(path);
     var pageData = {};
     var updateOrCreate;
     var previousRevision = false;
@@ -657,7 +658,7 @@ module.exports = function(crowi, app) {
       return Promise.resolve(pageData);
     }).then(function(page) {
 
-      return res.redirect(encodeURI(page.path));
+      return res.redirect(pagePathUtil.encodePagePath(page.path));
     }).catch(function(err) {
       return res.redirect('/');
     });
@@ -709,7 +710,7 @@ module.exports = function(crowi, app) {
       pagerOptions.length = pages.length;
 
       var result = {};
-      result.pages = pages;
+      result.pages = pagePathUtil.encodePagesPath(pages);
       return res.json(ApiResponse.success(result));
     }).catch(function(err) {
       return res.json(ApiResponse.error(err));

+ 24 - 0
lib/util/pagePathUtil.js

@@ -0,0 +1,24 @@
+'use strict';
+
+function encodePagesPath(pages) {
+  pages.forEach(function(page) {
+    if (!page.path) {
+      return;
+    }
+    page.path = encodePagePath(page.path);
+  });
+  return pages;
+}
+
+function encodePagePath(path) {
+  var paths = path.split('/');
+  paths.forEach(function(item, index) {
+    paths[index] = encodeURIComponent(item);
+  });
+  return paths.join('/');
+}
+
+module.exports = {
+  encodePagePath: encodePagePath,
+  encodePagesPath: encodePagesPath
+};

+ 1 - 1
lib/views/widget/page_list.html

@@ -12,7 +12,7 @@
   <a href="{{ page.path }}"
     class="page-list-link"
     data-path="{{ page.path }}"
-    data-short-path="{{ page.path|path2name }}">{{ page.path }}
+    data-short-path="{{ page.path|path2name }}">{{ decodeURIComponent(page.path) }}
   </a>
   <span class="page-list-meta">
     {% if page.isPortal() %}

+ 1 - 1
resource/js/components/Page/RevisionPath.js

@@ -36,7 +36,7 @@ export default class RevisionPath extends React.Component {
     let parentPath = '/';
     splitted.forEach((pageName) => {
       pages.push({
-        pagePath: parentPath + pageName,
+        pagePath: parentPath + encodeURIComponent(pageName),
         pageName: pageName,
       });
       parentPath += pageName + '/';

+ 4 - 3
resource/js/legacy/crowi.js

@@ -15,6 +15,7 @@ require('bootstrap-sass');
 require('jquery.cookie');
 
 require('./thirdparty-js/agile-admin');
+const pagePathUtil = require('../../../lib/util/pagePathUtil');
 
 var Crowi = {};
 
@@ -233,7 +234,7 @@ $(function() {
     if (name.match(/.+\/$/)) {
       name = name.substr(0, name.length - 1);
     }
-    top.location.href = name + '#edit-form';
+    top.location.href = pagePathUtil.encodePagePath(name) + '#edit-form';
     return false;
   });
 
@@ -385,8 +386,8 @@ $(function() {
   $('.page-list-link').each(function() {
     var $link = $(this);
     var text = $link.text();
-    var path = $link.data('path');
-    var shortPath = String($link.data('short-path')); // convert to string
+    var path = decodeURIComponent($link.data('path'));
+    var shortPath = decodeURIComponent($link.data('short-path')); // convert to string
 
     if (path == null || shortPath == null) {
       // continue