diff --git a/src/Dom/LinkManager.php b/src/Dom/LinkManager.php index e478bcaed..fc28472d9 100644 --- a/src/Dom/LinkManager.php +++ b/src/Dom/LinkManager.php @@ -3,6 +3,7 @@ namespace AmpProject\Dom; use AmpProject\Attribute; +use AmpProject\Exception\FailedToCreateLink; use AmpProject\RequestDestination; use AmpProject\Tag; use DOMNode; @@ -258,13 +259,16 @@ public function add($rel, $href, $attributes = []) $this->referenceNode = $this->document->viewport; } - /** @var Element $link */ if ($this->referenceNode) { $link = $this->document->head->insertBefore($link, $this->referenceNode->nextSibling); } else { $link = $this->document->head->appendChild($link); } + if (! $link instanceof Element) { + throw FailedToCreateLink::forLink($link); + } + $this->links[$this->getKey($link)] = $link; $this->referenceNode = $link; diff --git a/src/Exception/FailedToCreateLink.php b/src/Exception/FailedToCreateLink.php new file mode 100644 index 000000000..fc6e03f21 --- /dev/null +++ b/src/Exception/FailedToCreateLink.php @@ -0,0 +1,29 @@ +