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