Steven Fukase 4 лет назад
Родитель
Сommit
5fa7ed2ead
2 измененных файлов с 12 добавлено и 32 удалено
  1. 12 6
      src/client/js/components/PageCreateModal.jsx
  2. 0 26
      src/lib/util/path-utils.js

+ 12 - 6
src/client/js/components/PageCreateModal.jsx

@@ -8,7 +8,7 @@ import { withTranslation } from 'react-i18next';
 import { format } from 'date-fns';
 
 import {
-  userPageRoot, isCreatablePage, generateEditorPath, joinUrl,
+  userPageRoot, isCreatablePage, generateEditorPath,
 } from '@commons/util/path-utils';
 import { pathUtils } from 'growi-commons';
 
@@ -77,12 +77,18 @@ const PageCreateModal = (props) => {
    * @param {string} paths
    */
   function redirectToEditor(...paths) {
-    const joinedUrl = joinUrl(...paths);
-    if (!isCreatablePage(joinedUrl)) {
-      toastError(new Error('Invalid characters found on new page path'));
-      return;
+    const joinedPath = [...paths].map(str => str.replace(/^\/+|\/+$|\s/g, '')).join('/');
+    try {
+      const url = new URL(joinedPath, 'https://dummy');
+      if (!isCreatablePage(url)) {
+        toastError(new Error('Invalid characters'));
+        return;
+      }
+      window.location.href = generateEditorPath(url.pathname);
+    }
+    catch (err) {
+      toastError(new Error('Invalid path format'));
     }
-    window.location.href = generateEditorPath(...paths);
   }
 
   /**

+ 0 - 26
src/lib/util/path-utils.js

@@ -1,6 +1,3 @@
-// const nodePath = require('path');
-// const nodeUrl = require('url');
-// const urljoin = require('url-join');
 const escapeStringRegexp = require('escape-string-regexp');
 
 /**
@@ -72,28 +69,6 @@ const isCreatablePage = (path) => {
   return isCreatable;
 };
 
-/**
- * join url
- * @param {string} paths
- * @returns {string}
- */
-function joinUrl(...paths) {
-  const pathArray = [...paths].map(str => encodeURI(str.replace(/^\/+|\/+$|\s/g, '')));
-  const queries = new Set(pathArray.filter(str => /(\?|&)([^?&=]+)=([^?&=]+)/.test(str)));
-  const hashes = new Set(pathArray.filter(str => /^#/.test(str)));
-  if (queries.size > 1 || hashes.size > 1) {
-    throw new Error('Do not enter more than 1 query string or hash');
-  }
-
-  const path = pathArray
-    .filter(item => !queries.has(item) && !hashes.has(item))
-    .join('/')
-    .concat([...queries])
-    .concat([...hashes]);
-
-  return `/${path}`;
-}
-
 /**
  * return path to editor
  * @param {string} path
@@ -151,7 +126,6 @@ module.exports = {
   isUserPage,
   isCreatablePage,
   generateEditorPath,
-  joinUrl,
   userPageRoot,
   convertToNewAffiliationPath,
   encodeSpaces,