소스 검색

Merge pull request #1884 from blink1073/fix-urlext

Use url utils from services
Afshin Darian 8 년 전
부모
커밋
a31d65c748
5개의 변경된 파일51개의 추가작업 그리고 38개의 파일을 삭제
  1. 1 0
      package.json
  2. 22 23
      src/coreutils/url.ts
  3. 1 0
      src/typings.d.ts
  4. 0 15
      test/src/coreutils/url.spec.ts
  5. 27 0
      typings/url-parse/url-parse.d.ts

+ 1 - 0
package.json

@@ -30,6 +30,7 @@
     "path-posix": "^1.0.0",
     "sanitize-html": "^1.12.0",
     "semver": "^5.3.0",
+    "url-parse": "^1.1.8",
     "xterm": "^2.4.0"
   },
   "devDependencies": {

+ 22 - 23
src/coreutils/url.ts

@@ -5,9 +5,8 @@ import {
   JSONObject
 } from '@phosphor/coreutils';
 
-import * as posix
- from 'path-posix';
-
+import * as urlparse
+ from 'url-parse';
 
 /**
  * The namespace for URL-related functions.
@@ -27,21 +26,7 @@ namespace URLExt {
       let a = document.createElement('a');
       return a;
     }
-    throw Error('Cannot parse a URL without a document object');
-  }
-
-  /**
-   * Resolve a target URL relative to a base URL.
-   *
-   * @param from - The Base URL being resolved against.
-   *
-   * @param to - The HREF URL being resolved
-   *
-   * @returns the resolved url.
-   */
-  export
-  function resolve(from: string, to: string): string {
-    return posix.resolve(from, to);
+    return urlparse(url);
   }
 
   /**
@@ -53,7 +38,24 @@ namespace URLExt {
    */
   export
   function join(...parts: string[]): string {
-    return posix.join(...parts);
+    // Adapted from url-join.
+    // Copyright (c) 2016 José F. Romaniello, MIT License.
+    // https://github.com/jfromaniello/url-join/blob/v1.1.0/lib/url-join.js
+    let str = [].slice.call(parts, 0).join('/');
+
+    // make sure protocol is followed by two slashes
+    str = str.replace(/:\//g, '://');
+
+    // remove consecutive slashes
+    str = str.replace(/([^:\s])\/+/g, '$1/');
+
+    // remove trailing slash before parameters or hash
+    str = str.replace(/\/(\?|&|#[^!])/g, '$1');
+
+    // replace ? in parameters with &
+    str = str.replace(/(\?.+)\?/g, '$1&');
+
+    return str;
   }
 
   /**
@@ -69,10 +71,7 @@ namespace URLExt {
    */
   export
   function encodeParts(url: string): string {
-    // Normalize and join, split, encode, then join.
-    url = join(url);
-    let parts = url.split('/').map(encodeURIComponent);
-    return join(...parts);
+    return join(...url.split('/').map(encodeURIComponent));
   }
 
   /**

+ 1 - 0
src/typings.d.ts

@@ -4,4 +4,5 @@
 /// <reference path="../typings/ansi_up/ansi_up.d.ts"/>
 /// <reference path="../typings/codemirror/codemirror.d.ts"/>
 /// <reference path="../typings/path-posix/path-posix.d.ts"/>
+/// <reference path="../typings/url-parse/url-parse.d.ts"/>
 /// <reference path="../typings/xterm/xterm.d.ts"/>

+ 0 - 15
test/src/coreutils/url.spec.ts

@@ -38,21 +38,6 @@ describe('@jupyterlab/coreutils', () => {
 
     });
 
-    describe('.resolve()', () => {
-
-      it('should resolve a target URLExt relative to a base url', () => {
-        let path = URLExt.resolve('/foo/bar/baz', '/bar');
-        expect(path).to.equal('/bar');
-        expect(URLExt.resolve('/foo/bar', '.')).to.equal('/foo/');
-        path = URLExt.resolve(
-          'http://example.com/b//c//d;p?q#blarg',
-          'https://u:p@h.com/p/a/t/h?s#hash2'
-        );
-        expect(path).to.equal('https://u:p@h.com/p/a/t/h?s#hash2');
-      });
-
-    });
-
     describe('.join()', () => {
 
       it('should join a sequence of url components', () => {

+ 27 - 0
typings/url-parse/url-parse.d.ts

@@ -0,0 +1,27 @@
+// Type definitions for url-parse v1.1.8
+// Project: https://github.com/unshiftio/url-parse
+// Definitions by: Steven Silvester <https://github.com/blink1073>
+
+// We use the hack mentioned in https://github.com/Microsoft/TypeScript/issues/5073
+// to enable `import * as urlparse from 'url-parse';`
+
+declare module 'url-parse' {
+  interface IURL {
+    protocol: string;
+    slashes: boolean;
+    auth: string;
+    username: string;
+    password: string;
+    host: string;
+    hostname: string;
+    port: string;
+    pathname: string;
+    query: any;
+    hash: string;
+    href: string;
+    origin: string;
+  }
+  function parse(url: string, parseQuery?: boolean): IURL;
+  namespace parse { }
+  export = parse;
+}