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
f43fa51e
Commit
f43fa51e
authored
Aug 03, 2017
by
Robert Knight
Committed by
GitHub
Aug 03, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #508 from SteelWagstaff/master
HTML5 Audio elements
parents
c8e523f1
ce7e2d6a
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
6 deletions
+47
-6
media-embedder.js
src/sidebar/media-embedder.js
+24
-6
media-embedder-test.js
src/sidebar/test/media-embedder-test.js
+23
-0
No files found.
src/sidebar/media-embedder.js
View file @
f43fa51e
'use strict'
;
/**
* Return an HTML5 audio player with the given src URL.
*/
function
audioElement
(
src
)
{
var
html5audio
=
document
.
createElement
(
'audio'
);
html5audio
.
controls
=
true
;
html5audio
.
src
=
src
;
return
html5audio
;
}
/**
* Return an iframe DOM element with the given src URL.
*/
...
...
@@ -24,8 +35,8 @@ function vimeoEmbed(id) {
}
/**
* A list of functions that return an "embed" DOM element (e.g. an <iframe>
)
* for a given link.
* A list of functions that return an "embed" DOM element (e.g. an <iframe>
or
*
an html5 <audio> element)
for a given link.
*
* Each function either returns `undefined` if it can't generate an embed for
* the link, or a DOM element if it can.
...
...
@@ -88,6 +99,15 @@ var embedGenerators = [
}
return
null
;
},
// Matches URLs that end with .mp3, .ogg, or .wav (assumed to be audio files)
function
html5audioFromMp3Link
(
link
)
{
if
(
link
.
pathname
.
endsWith
(
'.mp3'
)
||
link
.
pathname
.
endsWith
(
'.ogg'
)
||
link
.
pathname
.
endsWith
(
'.wav'
))
{
return
audioElement
(
link
.
href
);
}
return
null
;
},
];
/**
...
...
@@ -115,7 +135,7 @@ function embedForLink(link) {
*
* If the given link element is a link to an embeddable media and if its link
* text is the same as its href then it will be replaced in the DOM with an
* embed (e.g. an <iframe>) of the same media.
* embed (e.g. an <iframe>
or html5 <audio> element
) of the same media.
*
* If the link text is different from the href, then the link will be left
* untouched. We want to convert links like these from the Markdown source into
...
...
@@ -138,14 +158,12 @@ function replaceLinkWithEmbed(link) {
if
(
link
.
href
!==
link
.
textContent
)
{
return
;
}
var
embed
=
embedForLink
(
link
);
if
(
embed
)
{
if
(
embed
){
link
.
parentElement
.
replaceChild
(
embed
,
link
);
}
}
/**
* Replace all embeddable link elements beneath the given element with embeds.
*
...
...
src/sidebar/test/media-embedder-test.js
View file @
f43fa51e
...
...
@@ -90,6 +90,29 @@ describe('media-embedder', function () {
});
});
it
(
'replaces audio links with html5 audio elements'
,
function
()
{
var
urls
=
[
'https://archive.org/download/testmp3testfile/mpthreetest.mp3'
,
'https://archive.org/download/testmp3testfile/mpthreetest.mp3#fragment'
,
'https://archive.org/download/testmp3testfile/mpthreetest.mp3?foo=bar&id=1'
,
'http://www.music.helsinki.fi/tmt/opetus/uusmedia/esim/a2002011001-e02.wav'
,
'http://www.music.helsinki.fi/tmt/opetus/uusmedia/esim/a2002011001-e02.wav#fragment'
,
'http://www.music.helsinki.fi/tmt/opetus/uusmedia/esim/a2002011001-e02.wav?foo=bar&id=4'
,
'https://www.w3schools.com/html/horse.ogg'
,
'https://www.w3schools.com/html/horse.ogg#fragment'
,
'https://www.w3schools.com/html/horse.ogg?foo=bar&id=31'
,
];
urls
.
forEach
(
function
(
url
)
{
var
element
=
domElement
(
'<a href="'
+
url
+
'">'
+
url
+
'</a>'
);
mediaEmbedder
.
replaceLinksWithEmbeds
(
element
);
assert
.
equal
(
element
.
childElementCount
,
1
);
assert
.
equal
(
element
.
children
[
0
].
tagName
,
'AUDIO'
);
assert
.
equal
(
element
.
children
[
0
].
src
,
url
.
toLowerCase
());
});
});
it
(
'does not replace links if the link text is different'
,
function
()
{
var
url
=
'https://youtu.be/QCkm0lL-6lc'
;
var
element
=
domElement
(
'<a href="'
+
url
+
'">different label</a>'
);
...
...
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