提交 175ab4aa 编写于 作者: J Johannes Rieken

don't warn when fixing missing scheme, just do it... bring back strict mode for parse

上级 666ad41b
......@@ -41,10 +41,14 @@ function _validateUri(ret: URI): void {
}
// graceful behaviour when scheme is missing: fallback to using 'file'-scheme
function _schemeFix(scheme: string): string {
function _schemeFix(scheme: string, strict?: boolean): string {
if (!scheme) {
console.trace('BAD uri lacks scheme, falling back to file-scheme.');
scheme = 'file';
if (strict) {
throw new Error('[UriError]: A scheme must be provided');
} else {
// console.trace('BAD uri lacks scheme, falling back to file-scheme.');
scheme = 'file';
}
}
return scheme;
}
......@@ -263,15 +267,17 @@ export class URI implements UriComponents {
* Creates a new URI from a string, e.g. `http://www.msft.com/some/path`,
* `file:///usr/home`, or `scheme:with/path`.
*
* *Note:* When the input lacks a scheme then `file` is used.
*
* @param value A string which represents an URI (see `URI#toString`).
*/
static parse(value: string): URI {
static parse(value: string, strict?: boolean): URI {
const match = _uriRegExp.exec(value);
if (!match) {
throw new Error(`[UriError]: Invalid input: ${value}`);
}
const scheme = _schemeFix(match[MatchIndex.scheme]) || '';
const scheme = _schemeFix(match[MatchIndex.scheme], strict) || '';
const authority = match[MatchIndex.authority] || '';
const path = _referenceResolution(scheme, match[MatchIndex.path] || '');
const query = match[MatchIndex.query] || '';
......
......@@ -235,6 +235,11 @@ suite('URI', () => {
assert.throws(() => URI.parse('file:////shares/files/p.cs'));
});
test('URI#parse, missing scheme', () => {
assert.throws(() => URI.parse('/foo/bar', true));
assertToString('/foo/bar', 'file:///foo/bar');
});
test('URI#file, win-speciale', () => {
if (isWindows) {
let value = URI.file('c:\\test\\drive');
......
......@@ -129,9 +129,11 @@ declare namespace monaco {
* Creates a new Uri from a string, e.g. `http://www.msft.com/some/path`,
* `file:///usr/home`, or `scheme:with/path`.
*
* *Note:* When the input lacks a scheme then `file` is used.
*
* @param value A string which represents an Uri (see `Uri#toString`).
*/
static parse(value: string): Uri;
static parse(value: string, strict?: boolean): Uri;
/**
* Creates a new Uri from a file system path, e.g. `c:\my\files`,
* `/usr/home`, or `\\server\share\some\path`.
......
......@@ -1236,8 +1236,8 @@ declare module 'vscode' {
* `file:///usr/home`, or `scheme:with/path`.
*
* *Note* that for a while uris without a `scheme` were accepted. That is not correct
* as all uris should have a scheme. To avoid breakage of existing code the optional
* `strict`-argument has been added. We *strongly* advise to use it, e.g. `Uri.parse('my:uri', true)`
* as all uris should have a scheme. When missing the `file`-scheme is being used unless
* the `strict`-argument is `true` in which case an error is thrown.
*
* @see [Uri.toString](#Uri.toString)
* @param value The string value of an Uri.
......
......@@ -260,7 +260,7 @@ export namespace MarkdownString {
const collectUri = (href: string): string => {
try {
let uri = URI.parse(href);
let uri = URI.parse(href, true);
uri = uri.with({ query: _uriMassage(uri.query, resUris) });
resUris[href] = uri;
} catch (e) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册