@@ -27,7 +27,17 @@ export default {
2727 // don't need this field any longer
2828 delete link . autoplay ;
2929
30- if ( link . href . indexOf ( play ) > - 1 || link . rel . indexOf ( CONFIG . R . autoplay ) == - 1 ) {
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 ) {
3141
3242 var stop = play . replace ( / \= ( \w + ) / , function ( p1 , p2 ) {
3343 var antonyms = {
@@ -39,37 +49,47 @@ export default {
3949 'off' : 'on'
4050 } ;
4151 return '=' + antonyms [ p2 ] ;
42- } ) ;
52+ } ) ;
4353
44- var rels = [ ...link . rel ] ;
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 ] ;
4560 var new_link = Object . assign ( { } , link ) ;
46- new_link . rel = rels ; // deep copy
4761
62+ if ( link . href . indexOf ( play ) > - 1 ) {
4863
49- if ( link . href . indexOf ( play ) > - 1 ) {
50- // original link is autoplay
51- if ( link . rel . indexOf ( CONFIG . R . autoplay ) == - 1 ) {
52- link . rel . push ( CONFIG . R . autoplay ) ;
53- }
54-
64+ // Original link is autoplay
65+ link . rel = RELS_WITH_AUTOPLAY ;
66+
67+ // Add a link without autoplay
5568 new_link . href = new_link . href . replace ( play , stop ) ;
56- if ( new_link . rel . indexOf ( CONFIG . R . autoplay ) > - 1 ) {
57- new_link . rel . splice ( new_link . rel . indexOf ( CONFIG . R . autoplay ) , 1 ) ;
58- }
69+ new_link . rel = RELS ;
5970
60- } else if ( link . rel . indexOf ( CONFIG . R . autoplay ) == - 1 ) {
71+ } else if ( /* link.href.indexOf(play) === -1 && */ rel . indexOf ( CONFIG . R . autoplay ) > - 1 ) {
6172
62- // original link isnt autoplay, leave it alone
63- // autoplay=false may autoplay. Many publishers react to any `autoplay` value
73+ link . rel = RELS_WITH_AUTOPLAY ;
6474
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
6586 if ( link . href . indexOf ( stop ) > - 1 ) {
6687 new_link . href = new_link . href . replace ( stop , play ) ;
6788 } else {
68- new_link . href += ( new_link . href . indexOf ( '?' ) > - 1 ? '&' : '?' ) + play ;
89+ new_link . href += ( new_link . href . indexOf ( '?' ) > - 1 ? '&' : '?' ) + play ;
6990 }
7091
71- new_link . rel . push ( CONFIG . R . autoplay ) ;
72-
92+ new_link . rel = RELS_WITH_AUTOPLAY ;
7393 }
7494
7595 return {
0 commit comments