Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ProtectEsiTags Document filter #343

Merged
merged 2 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Dom/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ public function __construct($version = '', $encoding = null)
Filter\DocumentEncoding::class,
Filter\HttpEquivCharset::class,
Filter\LibxmlCompatibility::class,
Filter\ProtectEsiTags::class,
]
);
}
Expand Down
61 changes: 61 additions & 0 deletions src/Dom/Document/Filter/ProtectEsiTags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace AmpProject\Dom\Document\Filter;

use AmpProject\Dom\Document\AfterSaveFilter;
use AmpProject\Dom\Document\BeforeLoadFilter;

/**
* Protect the esi tags from being broken.
*
* @package ampproject/amp-toolbox
*/
final class ProtectEsiTags implements BeforeLoadFilter, AfterSaveFilter
{

/**
* List of self closing ESI tags.
*
* @link https://www.w3.org/TR/esi-lang/
*
* @var string[]
*/
const SELF_CLOSING_TAGS = [
'esi:include',
'esi:comment',
];

/**
* Preprocess the HTML to be loaded into the Dom\Document.
*
* @param string $html String of HTML markup to be preprocessed.
* @return string Preprocessed string of HTML markup.
*/
public function beforeLoad($html)
{
$selfClosingregex = '#<(' . implode('|', self::SELF_CLOSING_TAGS) . ')([^>]*?)(?>\s*(?<!\\\\)/)?>(?!</\1>)#';

$html = preg_replace($selfClosingregex, '<$1$2></$1>', $html);
schlessera marked this conversation as resolved.
Show resolved Hide resolved
$html = preg_replace('/(<esi:include.+?)(src)=/', '$1esi-src=', $html);
$html = preg_replace('/(<\/?)esi:/', '$1esi-', $html);

return $html;
}

/**
* Process the Dom\Document after being saved from Dom\Document.
*
* @param string $html String of HTML markup to be preprocessed.
* @return string Preprocessed string of HTML markup.
*/
public function afterSave($html)
{
$selfClosingregex = '#></(' . implode('|', self::SELF_CLOSING_TAGS) . ')>#i';

$html = preg_replace('/(<\/?)esi-/', '$1esi:', $html);
$html = preg_replace('/(<esi:include.+?)(esi-src)=/', '$1src=', $html);
$html = preg_replace($selfClosingregex, '/>', $html);

return $html;
}
}
23 changes: 23 additions & 0 deletions tests/Dom/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,29 @@ public function dataDomDocument()
'<!DOCTYPE html><html amp data-text="⚡">' . $head . '<body><script>let foo = { bar: "<div>amp</div>" }</script></body></html>',
'<!DOCTYPE html><html amp data-text="⚡">' . $head . '<body><script>let foo = { bar: "<div>amp</div>" }</script></body></html>',
],
'preserve Varnish esi tags' => [
'utf-8',
'<!DOCTYPE html><html><head></head><body>'
. ' <esi:choose>'
. ' <esi:when test="$(logindata{name}) == null">'
. ' <esi:include src="/login/$(logindata{name})"/>'
. ' </esi:when>'
. ' <esi:otherwise>'
. ' <esi:include src="/login/guest.html"/>'
. ' </esi:otherwise>'
. ' </esi:choose>'
. '</body></html>',
'<!DOCTYPE html><html>' . $head . '<body>'
. ' <esi:choose>'
. ' <esi:when test="$(logindata{name}) == null">'
. ' <esi:include src="/login/$(logindata{name})"/>'
. ' </esi:when>'
. ' <esi:otherwise>'
. ' <esi:include src="/login/guest.html"/>'
. ' </esi:otherwise>'
. ' </esi:choose>'
. '</body></html>',
]
];
}

Expand Down