Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
deno
提交
9e1f5ccb
D
deno
项目概览
张重言
/
deno
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
deno
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
9e1f5ccb
编写于
1月 21, 2019
作者:
D
Daijiro Wachi
提交者:
Ryan Dahl
1月 20, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use the same parse processing at contractor of URL with setters (#1549)
上级
7800d9f0
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
57 addition
and
17 deletion
+57
-17
js/url.ts
js/url.ts
+44
-17
js/url_test.ts
js/url_test.ts
+13
-0
未找到文件。
js/url.ts
浏览文件 @
9e1f5ccb
...
...
@@ -7,7 +7,7 @@ interface URLParts {
password
:
string
;
hostname
:
string
;
port
:
string
;
path
:
string
;
path
name
:
string
;
query
:
string
;
hash
:
string
;
}
...
...
@@ -15,7 +15,7 @@ interface URLParts {
const
patterns
=
{
protocol
:
"
(?:([^:/?#]+):)
"
,
authority
:
"
(?://([^/?#]*))
"
,
path
:
"
([^?#]*)
"
,
path
name
:
"
([^?#]*)
"
,
query
:
"
(
\\
?[^#]*)
"
,
hash
:
"
(#.*)
"
,
...
...
@@ -25,7 +25,7 @@ const patterns = {
};
const
urlRegExp
=
new
RegExp
(
`^
${
patterns
.
protocol
}
?
${
patterns
.
authority
}
?
${
patterns
.
path
}${
`^
${
patterns
.
protocol
}
?
${
patterns
.
authority
}
?
${
patterns
.
path
name
}${
patterns
.
query
}
?
${
patterns
.
hash
}
?`
);
...
...
@@ -40,6 +40,17 @@ const searchParamsMethods: Array<keyof urlSearchParams.URLSearchParams> = [
"
set
"
];
const
initializedURLParts
=
{
protocol
:
""
,
username
:
""
,
password
:
""
,
hostname
:
""
,
port
:
""
,
pathname
:
""
,
query
:
""
,
hash
:
""
};
function
parse
(
url
:
string
):
URLParts
|
undefined
{
const
urlMatch
=
urlRegExp
.
exec
(
url
);
if
(
urlMatch
)
{
...
...
@@ -54,7 +65,7 @@ function parse(url: string): URLParts | undefined {
password
:
authorityMatch
[
2
]
||
""
,
hostname
:
authorityMatch
[
3
]
||
""
,
port
:
authorityMatch
[
4
]
||
""
,
path
:
urlMatch
[
3
]
||
""
,
path
name
:
urlMatch
[
3
]
||
""
,
query
:
urlMatch
[
4
]
||
""
,
hash
:
urlMatch
[
5
]
||
""
};
...
...
@@ -155,7 +166,7 @@ export class URL {
}
get
pathname
():
string
{
return
this
.
_parts
.
path
?
this
.
_parts
.
path
:
"
/
"
;
return
this
.
_parts
.
path
name
?
this
.
_parts
.
pathname
:
"
/
"
;
}
set
pathname
(
value
:
string
)
{
...
...
@@ -163,8 +174,8 @@ export class URL {
if
(
!
value
||
value
.
charAt
(
0
)
!==
"
/
"
)
{
value
=
`/
${
value
}
`
;
}
// paths can contain % unescaped
this
.
_parts
.
path
=
escape
(
value
).
replace
(
/%25/g
,
"
%
"
);
// path
name
s can contain % unescaped
this
.
_parts
.
path
name
=
escape
(
value
).
replace
(
/%25/g
,
"
%
"
);
}
get
port
():
string
{
...
...
@@ -218,6 +229,15 @@ export class URL {
return
this
.
_searchParams
;
}
get
query
():
string
{
return
this
.
_parts
.
query
;
}
set
query
(
value
:
string
)
{
value
=
String
(
value
);
this
.
_parts
.
query
=
value
;
}
constructor
(
url
:
string
,
base
?:
string
|
URL
)
{
let
baseParts
:
URLParts
|
undefined
;
if
(
base
)
{
...
...
@@ -234,17 +254,24 @@ export class URL {
if
(
urlParts
.
protocol
)
{
this
.
_parts
=
urlParts
;
this
.
protocol
=
urlParts
.
protocol
;
this
.
username
=
urlParts
.
username
;
this
.
password
=
urlParts
.
password
;
this
.
hostname
=
urlParts
.
hostname
;
this
.
port
=
urlParts
.
port
;
this
.
pathname
=
urlParts
.
pathname
;
this
.
query
=
urlParts
.
query
;
this
.
hash
=
urlParts
.
hash
;
}
else
if
(
baseParts
)
{
this
.
_parts
=
{
protocol
:
baseParts
.
protocol
,
username
:
baseParts
.
username
,
password
:
baseParts
.
password
,
hostname
:
baseParts
.
hostname
,
port
:
baseParts
.
port
,
path
:
urlParts
.
path
||
baseParts
.
path
,
query
:
urlParts
.
query
||
baseParts
.
query
,
hash
:
urlParts
.
hash
};
this
.
_parts
=
initializedURLParts
;
this
.
protocol
=
baseParts
.
protocol
;
this
.
username
=
baseParts
.
username
;
this
.
password
=
baseParts
.
password
;
this
.
hostname
=
baseParts
.
hostname
;
this
.
port
=
baseParts
.
port
;
this
.
pathname
=
urlParts
.
pathname
||
baseParts
.
pathname
;
this
.
query
=
urlParts
.
query
||
baseParts
.
query
;
this
.
hash
=
urlParts
.
hash
;
}
else
{
throw
new
TypeError
(
"
URL requires a base URL.
"
);
}
...
...
js/url_test.ts
浏览文件 @
9e1f5ccb
...
...
@@ -31,6 +31,19 @@ test(function urlParsing() {
);
});
test
(
function
constractorParsing
()
{
const
url
=
new
URL
(
"
http://どめいん.com/ぱす?きー=ばりゅー#はっしゅ
"
);
const
{
host
,
pathname
,
search
,
hash
}
=
url
;
url
.
host
=
"
どめいん.com
"
;
url
.
pathname
=
"
/ぱす
"
;
url
.
search
=
"
?きー=ばりゅー
"
;
url
.
hash
=
"
#はっしゅ
"
;
assertEqual
(
host
,
url
.
host
);
assertEqual
(
pathname
,
url
.
pathname
);
assertEqual
(
search
,
url
.
search
);
assertEqual
(
hash
,
url
.
hash
);
});
test
(
function
urlModifications
()
{
const
url
=
new
URL
(
"
https://foo:bar@baz.qat:8000/qux/quux?foo=bar&baz=12#qat
"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录