|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
require_once( AMP__ROOT__ . '/includes/sanitizers/class-amp-base-sanitizer.php' ); |
|
4
|
|
|
|
|
5
|
|
|
class AMP_Audio_Sanitizer extends AMP_Base_Sanitizer { |
|
6
|
|
|
public static $tag = 'audio'; |
|
7
|
|
|
|
|
8
|
|
|
private static $script_slug = 'amp-audio'; |
|
9
|
|
|
private static $script_src = 'https://cdn.ampproject.org/v0/amp-audio-0.1.js'; |
|
10
|
|
|
|
|
11
|
|
|
public function get_scripts() { |
|
12
|
|
|
if ( ! $this->did_convert_elements ) { |
|
13
|
|
|
return array(); |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
return array( self::$script_slug => self::$script_src ); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function sanitize() { |
|
20
|
|
|
$nodes = $this->dom->getElementsByTagName( self::$tag ); |
|
21
|
|
|
$num_nodes = $nodes->length; |
|
22
|
|
|
if ( 0 === $num_nodes ) { |
|
23
|
|
|
return; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
for ( $i = $num_nodes - 1; $i >= 0; $i-- ) { |
|
27
|
|
|
$node = $nodes->item( $i ); |
|
28
|
|
|
$old_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node ); |
|
29
|
|
|
|
|
30
|
|
|
$new_attributes = $this->filter_attributes( $old_attributes ); |
|
31
|
|
|
|
|
32
|
|
|
$new_node = AMP_DOM_Utils::create_node( $this->dom, 'amp-audio', $new_attributes ); |
|
33
|
|
|
|
|
34
|
|
|
// TODO: `source` does not have closing tag, and DOMDocument doesn't handle it well. |
|
|
|
|
|
|
35
|
|
View Code Duplication |
foreach ( $node->childNodes as $child_node ) { |
|
36
|
|
|
$new_child_node = $child_node->cloneNode( true ); |
|
37
|
|
|
$old_child_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $new_child_node ); |
|
38
|
|
|
$new_child_attributes = $this->filter_attributes( $old_child_attributes ); |
|
39
|
|
|
|
|
40
|
|
|
// Only append source tags with a valid src attribute |
|
41
|
|
|
if ( ! empty( $new_child_attributes['src'] ) && 'source' === $new_child_node->tagName ) { |
|
42
|
|
|
$new_node->appendChild( $new_child_node ); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// If the node has at least one valid source, replace the old node with it. |
|
47
|
|
|
// Otherwise, just remove the node. |
|
48
|
|
|
// |
|
49
|
|
|
// TODO: Add a fallback handler. |
|
|
|
|
|
|
50
|
|
|
// See: https://github.com/ampproject/amphtml/issues/2261 |
|
51
|
|
View Code Duplication |
if ( 0 === $new_node->childNodes->length && empty( $new_attributes['src'] ) ) { |
|
52
|
|
|
$node->parentNode->removeChild( $node ); |
|
53
|
|
|
} else { |
|
54
|
|
|
$node->parentNode->replaceChild( $new_node, $node ); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$this->did_convert_elements = true; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function filter_attributes( $attributes ) { |
|
62
|
|
|
$out = array(); |
|
63
|
|
|
|
|
64
|
|
|
foreach ( $attributes as $name => $value ) { |
|
65
|
|
|
switch ( $name ) { |
|
66
|
|
|
case 'src': |
|
67
|
|
|
$out[ $name ] = $this->maybe_enforce_https_src( $value ); |
|
68
|
|
|
break; |
|
69
|
|
|
|
|
70
|
|
|
case 'width': |
|
71
|
|
|
case 'height': |
|
72
|
|
|
$out[ $name ] = $this->sanitize_dimension( $value, $name ); |
|
73
|
|
|
break; |
|
74
|
|
|
|
|
75
|
|
|
case 'class': |
|
76
|
|
|
$out[ $name ] = $value; |
|
77
|
|
|
break; |
|
78
|
|
|
case 'loop': |
|
79
|
|
|
case 'muted': |
|
80
|
|
|
case 'autoplay': |
|
81
|
|
|
if ( 'false' !== $value ) { |
|
82
|
|
|
$out[ $name ] = ''; |
|
83
|
|
|
} |
|
84
|
|
|
break; |
|
85
|
|
|
|
|
86
|
|
|
default; |
|
|
|
|
|
|
87
|
|
|
break; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $out; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|