-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Flesh out Transformer\ServerSideRendering
- Loading branch information
1 parent
95bf0a7
commit 8835c9a
Showing
14 changed files
with
1,656 additions
and
1,170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace Amp\Optimizer\Error; | ||
|
||
use Amp\Optimizer\Error; | ||
use DOMElement; | ||
|
||
final class CannotRemoveBoilerplate implements Error | ||
{ | ||
|
||
use ErrorProperties; | ||
|
||
/** | ||
* Code to use for the error. | ||
* | ||
* @var string | ||
*/ | ||
const CODE = 'CANNOT_REMOVE_BOILERPLATE'; | ||
|
||
const ATTRIBUTES_STRING = 'Cannot remove boilerplate as either heights, media or sizes attribute is set: '; | ||
const RENDER_DELAYING_SCRIPT_STRING = 'Cannot remove boilerplate because the document contains a render-delaying extension: '; | ||
const AMP_AUDIO_STRING = 'Cannot remove boilerplate because the document contains an extension that needs to know the dimensions of the browser: '; | ||
const UNSUPPORTED_LAYOUT_STRING = 'Cannot remove boilerplate because of an unsupported layout: '; | ||
|
||
/** | ||
* Instantiate a CannotRemoveBoilerplate object. | ||
* | ||
* @param string $message Message for the error. | ||
*/ | ||
public function __construct($message) | ||
{ | ||
$this->code = self::CODE; | ||
$this->message = $message; | ||
} | ||
|
||
/** | ||
* Instantiate a CannotRemoveBoilerplate object for attributes that require the boilerplate to be around. | ||
* | ||
* @param DOMElement $element Element that contains the attributes that need the boilerplate. | ||
* @return self | ||
*/ | ||
public static function from_attributes_requiring_boilerplate(DOMElement $element) | ||
{ | ||
return new self(self::ATTRIBUTES_STRING . new ElementDump($element)); | ||
} | ||
|
||
/** | ||
* Instantiate a CannotRemoveBoilerplate object for an amp-experiment element. | ||
* | ||
* @param DOMElement $element amp-experiment element. | ||
* @return self | ||
*/ | ||
public static function from_amp_experiment(DOMElement $element) | ||
{ | ||
return new self(self::RENDER_DELAYING_SCRIPT_STRING . $element->tagName); | ||
} | ||
|
||
/** | ||
* Instantiate a CannotRemoveBoilerplate object for an amp-audio element. | ||
* | ||
* @param DOMElement $element amp-audio element. | ||
* @return self | ||
*/ | ||
public static function from_amp_audio(DOMElement $element) | ||
{ | ||
return new self(self::AMP_AUDIO_STRING . new ElementDump($element)); | ||
} | ||
|
||
/** | ||
* Instantiate a CannotRemoveBoilerplate object for an element with an unsupported layout. | ||
* | ||
* @param DOMElement $element Element with an unsupported layout. | ||
* @return self | ||
*/ | ||
public static function from_unsupported_layout(DOMElement $element) | ||
{ | ||
return new self(self::UNSUPPORTED_LAYOUT_STRING . new ElementDump($element)); | ||
} | ||
|
||
/** | ||
* Instantiate a CannotRemoveBoilerplate object for an element with an unsupported layout. | ||
* | ||
* @param DOMElement $element Element with an unsupported layout. | ||
* @return self | ||
*/ | ||
public static function from_render_delaying_script(DOMElement $element) | ||
{ | ||
$elementName = $element->hasAttribute('custom-element') ? $element->getAttribute('custom-element') : '<unknown>'; | ||
|
||
return new self(self::UNSUPPORTED_LAYOUT_STRING . $elementName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace Amp\Optimizer\Error; | ||
|
||
use DOMAttr; | ||
use DOMElement; | ||
|
||
/** | ||
* Dump an element with its attributes. | ||
* | ||
* This is meant for quick identification of an element and does not dump the child element or other inner content | ||
* from that element. | ||
*/ | ||
final class ElementDump | ||
{ | ||
|
||
/** | ||
* Element to dump. | ||
* | ||
* @var DOMElement | ||
*/ | ||
private $element; | ||
/** | ||
* | ||
* | ||
* Defaults to 120. | ||
* | ||
* @var int | ||
*/ | ||
private $truncate; | ||
|
||
/** | ||
* Instantiate an ElementDump object. | ||
* | ||
* The object is meant to be cast to a string to do its magic. | ||
* | ||
* @param DOMElement $element Element to dump. | ||
* @param int $truncate Optional. Maximum length of the dumped string to truncate to. Defaults to 120. | ||
*/ | ||
public function __construct(DOMElement $element, $truncate = 120) | ||
{ | ||
$this->element = $element; | ||
$this->truncate = $truncate; | ||
} | ||
|
||
/** | ||
* Dump the provided element into a string. | ||
* | ||
* @return string Dump of the element. | ||
*/ | ||
public function __toString() | ||
{ | ||
static $dump = null; | ||
|
||
if ($dump === null) { | ||
$dump = sprintf( | ||
'%s%s', | ||
$this->element->tagName, | ||
array_reduce( | ||
iterator_to_array($this->element->attributes, true), | ||
static function ($text, DOMAttr $attribute) { | ||
return $text . " {$attribute->nodeName}=\"{$attribute->value}\""; | ||
}, | ||
'' | ||
) | ||
); | ||
|
||
if (mb_strlen($dump) > $this->truncate) { | ||
$dump = mb_substr($dump, 0, $this->truncate - 1) . '…'; | ||
} | ||
} | ||
|
||
return (string)$dump; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.