Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
16689899
Unverified
Commit
16689899
authored
Nov 05, 2019
by
Robert Knight
Committed by
GitHub
Nov 05, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1465 from hypothesis/boot-url-template-ie11
Make boot URL templating work in IE 11 / early Edge
parents
7ffba4e1
863dc558
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
10 deletions
+54
-10
url-template-test.js
src/boot/test/url-template-test.js
+18
-3
url-template.js
src/boot/url-template.js
+36
-7
No files found.
src/boot/test/url-template-test.js
View file @
16689899
...
@@ -6,7 +6,7 @@ describe('processUrlTemplate', () => {
...
@@ -6,7 +6,7 @@ describe('processUrlTemplate', () => {
let
fakeDocument
;
let
fakeDocument
;
beforeEach
(()
=>
{
beforeEach
(()
=>
{
fakeDocument
=
{
currentScript
:
null
};
fakeDocument
=
{
currentScript
:
null
,
querySelectorAll
:
sinon
.
stub
()
};
});
});
context
(
'when `document.currentScript` is set'
,
()
=>
{
context
(
'when `document.currentScript` is set'
,
()
=>
{
...
@@ -34,12 +34,27 @@ describe('processUrlTemplate', () => {
...
@@ -34,12 +34,27 @@ describe('processUrlTemplate', () => {
});
});
context
(
'when `document.currentScript` is not set'
,
()
=>
{
context
(
'when `document.currentScript` is not set'
,
()
=>
{
it
(
'does not replace parameters'
,
()
=>
{
beforeEach
(()
=>
{
fakeDocument
.
querySelectorAll
.
withArgs
(
'script'
)
.
returns
([{
src
:
'http://test-host:3001/script.js'
}]);
});
it
(
'falls back to using origin info from the last <script> tag in the document'
,
()
=>
{
const
url
=
processUrlTemplate
(
const
url
=
processUrlTemplate
(
'{current_scheme}://{current_host}:2000/style.css'
,
'{current_scheme}://{current_host}:2000/style.css'
,
fakeDocument
fakeDocument
);
);
assert
.
equal
(
url
,
'{current_scheme}://{current_host}:2000/style.css'
);
assert
.
equal
(
url
,
'http://test-host:2000/style.css'
);
});
it
(
'does not try to determine the origin if there are no URL template params'
,
()
=>
{
const
url
=
processUrlTemplate
(
'https://hypothes.is/embed.js'
,
fakeDocument
);
assert
.
equal
(
url
,
'https://hypothes.is/embed.js'
);
assert
.
notCalled
(
fakeDocument
.
querySelectorAll
);
});
});
});
});
});
});
src/boot/url-template.js
View file @
16689899
'use strict'
;
'use strict'
;
function
currentScriptUrl
(
document_
=
document
)
{
/**
* Extract the protocol and hostname (ie. host without port) from the URL.
*
* We don't use the URL constructor here because IE and early versions of Edge
* do not support it and this code runs early in the life of the app before any
* polyfills can be loaded.
*/
function
extractOrigin
(
url
)
{
const
match
=
url
.
match
(
/
(
https
?)
:
\/\/([^
:
/]
+
)
/
);
if
(
!
match
)
{
return
null
;
}
return
{
protocol
:
match
[
1
],
hostname
:
match
[
2
]
};
}
function
currentScriptOrigin
(
document_
=
document
)
{
try
{
try
{
const
scriptEl
=
document_
.
currentScript
;
let
scriptEl
=
document_
.
currentScript
;
return
new
URL
(
scriptEl
.
src
);
if
(
!
scriptEl
)
{
// Fallback for IE 11.
const
scripts
=
document_
.
querySelectorAll
(
'script'
);
scriptEl
=
scripts
[
scripts
.
length
-
1
];
}
return
extractOrigin
(
scriptEl
.
src
);
}
catch
(
err
)
{
}
catch
(
err
)
{
return
null
;
return
null
;
}
}
...
@@ -19,11 +41,18 @@ function currentScriptUrl(document_ = document) {
...
@@ -19,11 +41,18 @@ function currentScriptUrl(document_ = document) {
* with the IP/hostname of the dev server.
* with the IP/hostname of the dev server.
*/
*/
function
processUrlTemplate
(
url
,
document_
=
document
)
{
function
processUrlTemplate
(
url
,
document_
=
document
)
{
const
scriptUrl
=
currentScriptUrl
(
document_
);
if
(
url
.
indexOf
(
'{'
)
===
-
1
)
{
if
(
scriptUrl
)
{
// Not a template. This should always be the case in production.
url
=
url
.
replace
(
'{current_host}'
,
scriptUrl
.
hostname
);
return
url
;
url
=
url
.
replace
(
'{current_scheme}'
,
scriptUrl
.
protocol
.
slice
(
0
,
-
1
));
}
const
origin
=
currentScriptOrigin
(
document_
);
if
(
origin
)
{
url
=
url
.
replace
(
'{current_host}'
,
origin
.
hostname
);
url
=
url
.
replace
(
'{current_scheme}'
,
origin
.
protocol
);
}
}
return
url
;
return
url
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment