Просмотр исходного кода

Merge pull request #2183 from weseek/support/refactor-addConditionToListByStartWith

Support/refactor add condition to list by start with
Yuki Takei 5 лет назад
Родитель
Сommit
dad120d0ac
2 измененных файлов с 9 добавлено и 29 удалено
  1. 9 18
      src/server/models/page.js
  2. 0 11
      src/test/models/page.test.js

+ 9 - 18
src/server/models/page.js

@@ -155,30 +155,25 @@ class PageQueryBuilder {
     // eslint-disable-next-line no-param-reassign
     // eslint-disable-next-line no-param-reassign
     path = addSlashOfEnd(path);
     path = addSlashOfEnd(path);
 
 
-    // add option to escape the regex strings
-    const combinedOption = Object.assign({ isRegExpEscapedFromPath: true }, option);
-
-    this.addConditionToListByStartWith(path, combinedOption);
-
+    this.addConditionToListByStartWith(path, option);
     return this;
     return this;
   }
   }
 
 
   /**
   /**
    * generate the query to find pages that start with `path`
    * generate the query to find pages that start with `path`
    *
    *
-   * (GROWI) If 'isRegExpEscapedFromPath' is true, `path` should have `/` at the end
-   *   -> returns '{path}/*' and '{path}' self.
-   * (Crowi) If 'isRegExpEscapedFromPath' is false and `path` has `/` at the end
-   *   -> returns '{path}*'
-   * (Crowi) If 'isRegExpEscapedFromPath' is false and `path` doesn't have `/` at the end
-   *   -> returns '{path}*'
+   * In normal case, returns '{path}/*' and '{path}' self.
+   * If top page, return without doing anything.
    *
    *
    * *option*
    * *option*
-   *   - isRegExpEscapedFromPath -- if true, the regex strings included in `path` is escaped (default: false)
+   *   Left for backward compatibility
    */
    */
   addConditionToListByStartWith(path, option) {
   addConditionToListByStartWith(path, option) {
+    // No request is set for the top page
+    if (isTopPage(path)) {
+      return this;
+    }
     const pathCondition = [];
     const pathCondition = [];
-    const isRegExpEscapedFromPath = option.isRegExpEscapedFromPath || false;
 
 
     /*
     /*
      * 1. add condition for finding the page completely match with `path` w/o last slash
      * 1. add condition for finding the page completely match with `path` w/o last slash
@@ -188,13 +183,10 @@ class PageQueryBuilder {
       pathSlashOmitted = path.substr(0, path.length - 1);
       pathSlashOmitted = path.substr(0, path.length - 1);
       pathCondition.push({ path: pathSlashOmitted });
       pathCondition.push({ path: pathSlashOmitted });
     }
     }
-
     /*
     /*
      * 2. add decendants
      * 2. add decendants
      */
      */
-    const pattern = (isRegExpEscapedFromPath)
-      ? escapeStringRegexp(path) // escape
-      : pathSlashOmitted;
+    const pattern = escapeStringRegexp(path); // escape
 
 
     let queryReg;
     let queryReg;
     try {
     try {
@@ -205,7 +197,6 @@ class PageQueryBuilder {
       // force to escape
       // force to escape
       queryReg = new RegExp(`^${escapeStringRegexp(pattern)}`);
       queryReg = new RegExp(`^${escapeStringRegexp(pattern)}`);
     }
     }
-
     pathCondition.push({ path: queryReg });
     pathCondition.push({ path: queryReg });
 
 
     this.query = this.query
     this.query = this.query

+ 0 - 11
src/test/models/page.test.js

@@ -334,16 +334,5 @@ describe('Page', () => {
       expect(pagePaths).toContainEqual('/page2');
       expect(pagePaths).toContainEqual('/page2');
     });
     });
 
 
-    test('should process with regexp', async() => {
-      const result = await Page.findListByStartWith('/page\\d{1}/', testUser0, {});
-
-      // assert totalCount
-      expect(result.totalCount).toEqual(3);
-      // assert paths
-      const pagePaths = result.pages.map((page) => { return page.path });
-      expect(pagePaths).toContainEqual('/page1');
-      expect(pagePaths).toContainEqual('/page1/child1');
-      expect(pagePaths).toContainEqual('/page2');
-    });
   });
   });
 });
 });