1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
<?php
namespace AmpProject;
use AmpProject\Dom\Document; use DOMDocument; use DOMElement; use DOMNode;
/** * Helper functionality to deal with AMP dev-mode. * * @link https://github.com/ampproject/amphtml/issues/20974 * * @package ampproject/common */ final class DevMode {
/** * Attribute name for AMP dev mode. * * @var string */ const DEV_MODE_ATTRIBUTE = 'data-ampdevmode';
/** * Check whether the provided document is in dev mode. * * @param DOMDocument $document Document for which to check whether dev mode is active. * @return bool Whether the document is in dev mode. */ public static function isActiveForDocument(DOMDocument $document) { if (! $document instanceof Document) { $document = Document::fromNode($document); }
return $document->documentElement->hasAttribute(self::DEV_MODE_ATTRIBUTE); }
/** * Check whether a node is exempt from validation during dev mode. * * @param DOMNode $node Node to check. * @return bool Whether the node should be exempt during dev mode. */ public static function hasExemptionForNode(DOMNode $node) { if (! $node instanceof DOMElement) { return false; }
$document = self::getDocument($node);
if ($node === $document->documentElement) { return $document->hasInitialAmpDevMode(); }
return $node->hasAttribute(self::DEV_MODE_ATTRIBUTE); }
/** * Check whether a certain node should be exempt from validation. * * @param DOMNode $node Node to check. * @return bool Whether the node should be exempt from validation. */ public static function isExemptFromValidation(DOMNode $node) { $document = self::getDocument($node); return self::isActiveForDocument($document) && self::hasExemptionForNode($node); }
/** * Get the document from the specified node. * * @param DOMNode $node * @return Document */ private static function getDocument(DOMNode $node) { $document = $node->ownerDocument; if (! $document instanceof Document) { $document = Document::fromNode($node); }
return $document; } }
|