Skip to content

Commit 275cc9a

Browse files
committed
simplify code
1 parent 2e7e71b commit 275cc9a

File tree

1 file changed

+48
-70
lines changed

1 file changed

+48
-70
lines changed

lib/plugins/validators/sync/06_autoplay.js

Lines changed: 48 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -23,80 +23,58 @@ export default {
2323
// create autoplay variant of the player if requested via link.autoplay attribute
2424
if (link.href && link.rel.indexOf(CONFIG.R.player) > -1 && link.autoplay && typeof link.autoplay === "string" && link.autoplay.indexOf('=') > -1) { // it should always be a query-string
2525

26-
var play = link.autoplay;
27-
// don't need this field any longer
26+
const play_querystring = link.autoplay; // e.g. 'autoplay=1'
2827
delete link.autoplay;
29-
30-
/**
31-
* Three cases:
32-
* 1) Link does not autoplay by default. We need to add an autoplay with autoplay=1.
33-
* 2) Link autoplays:
34-
* - with autoplay=1 in src, maybe with no autoplay in rels
35-
* - or without autoplay=1 in src, but autoplay in rels
36-
* We need to add a non-autoplaying variant with autoplay=0
37-
*/
38-
if ((link.href.indexOf(play) === -1 && link.rel.indexOf(CONFIG.R.autoplay) === -1)
39-
|| link.href.indexOf(play) > -1
40-
|| link.rel.indexOf(CONFIG.R.autoplay) > -1) {
41-
42-
var stop = play.replace(/\=(\w+)/, function (p1, p2) {
43-
var antonyms = {
44-
'1': '0',
45-
'0': '1',
46-
'true': 'false',
47-
'false': 'true',
48-
'on': 'off',
49-
'off': 'on'
50-
};
51-
return '=' + antonyms[p2];
52-
});
53-
54-
const rel = link.rel;
55-
const RELS = [...rel];
56-
RELS.splice(link.rel.indexOf(CONFIG.R.autoplay), 1);
57-
delete link.rel;
58-
59-
const RELS_WITH_AUTOPLAY = [...RELS, CONFIG.R.autoplay];
60-
var new_link = Object.assign({}, link);
61-
62-
if (link.href.indexOf(play) > -1 ) {
63-
64-
// Original link is autoplay
65-
link.rel = RELS_WITH_AUTOPLAY;
66-
67-
// Add a link without autoplay
68-
new_link.href = new_link.href.replace(play, stop);
69-
new_link.rel = RELS;
70-
71-
} else if (/* link.href.indexOf(play) === -1 && */ rel.indexOf(CONFIG.R.autoplay) > -1 ) {
72-
73-
link.rel = RELS_WITH_AUTOPLAY;
74-
75-
// link.href.indexOf(play) === -1, no need to double-check
76-
// Add a link without autoplay
77-
new_link.href += (new_link.href.indexOf('?') > -1 ? '&' : '?') + stop;
78-
new_link.rel = RELS;
79-
80-
} else if (rel.indexOf(CONFIG.R.autoplay) === -1) {
81-
82-
// Original link isnt autoplay, leave it as is
83-
link.rel = RELS;
84-
85-
// Add a link with autoplay
86-
if (link.href.indexOf(stop) > -1) {
87-
new_link.href = new_link.href.replace(stop, play);
88-
} else {
89-
new_link.href += (new_link.href.indexOf('?') > -1 ? '&' : '?') + play;
90-
}
91-
92-
new_link.rel = RELS_WITH_AUTOPLAY;
93-
}
9428

95-
return {
96-
addLink: new_link
29+
const stop_querystring = play_querystring.replace(/\=(\w+)/, function (p1, p2) {
30+
const antonyms = {
31+
'1': '0',
32+
'0': '1',
33+
'true': 'false',
34+
'false': 'true',
35+
'on': 'off',
36+
'off': 'on'
9737
};
38+
return '=' + antonyms[p2];
39+
});
40+
41+
// Fix original link first, if needed
42+
if (link.href.indexOf(play_querystring) > -1 && link.rel.indexOf(CONFIG.R.autoplay) === -1) {
43+
link.rel.push(CONFIG.R.autoplay);
9844
}
99-
45+
46+
if (link.href.indexOf(stop_querystring) > -1 && link.rel.indexOf(CONFIG.R.autoplay) > -1) {
47+
link.rel.splice(link.rel.indexOf(CONFIG.R.autoplay), 1);
48+
}
49+
50+
var other = Object.assign({}, link);
51+
other.rel = [...link.rel];
52+
53+
if (link.rel.indexOf(CONFIG.R.autoplay) === -1) {
54+
55+
// Add a link with autoplay
56+
if (link.href.indexOf(stop_querystring) > -1) {
57+
other.href = other.href.replace(stop_querystring, play_querystring);
58+
} else {
59+
other.href += (other.href.indexOf('?') > -1 ? '&' : '?') + play_querystring;
60+
}
61+
other.rel.push(CONFIG.R.autoplay);
62+
63+
} else {
64+
65+
// Add a link without autoplay
66+
if (link.href.indexOf(play_querystring) > -1) {
67+
other.href = other.href.replace(play_querystring, stop_querystring);
68+
} else {
69+
other.href += (other.href.indexOf('?') > -1 ? '&' : '?') + stop_querystring;
70+
}
71+
other.rel.splice(other.rel.indexOf(CONFIG.R.autoplay), 1);
72+
}
73+
74+
return {
75+
addLink: other
76+
};
77+
10078
} else if (link.autoplay) {
10179
delete link.autoplay; // if not a string - just ignore it
10280
}

0 commit comments

Comments
 (0)