Improved AMP support
authorAlexander Ebert <ebert@woltlab.com>
Mon, 19 Mar 2018 12:16:20 +0000 (13:16 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 19 Mar 2018 12:16:20 +0000 (13:16 +0100)
com.woltlab.wcf/templates/spoilerAmpMetaCode.tpl [new file with mode: 0644]
wcfsetup/install/files/lib/system/bbcode/HtmlBBCodeParser.class.php
wcfsetup/install/files/lib/system/html/output/AmpHtmlOutputProcessor.class.php
wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodeWoltlabSpoiler.class.php

diff --git a/com.woltlab.wcf/templates/spoilerAmpMetaCode.tpl b/com.woltlab.wcf/templates/spoilerAmpMetaCode.tpl
new file mode 100644 (file)
index 0000000..45a7f17
--- /dev/null
@@ -0,0 +1,3 @@
+{* Simplified spoiler for Google AMP pages that outputs its content directly. *}
+
+<!-- META_CODE_INNER_CONTENT -->
index 225ea162d10155af30aa29544c7efe575699d574..187381bc1a8ec8de402d95f73a7c7bf47015a8d0 100644 (file)
@@ -33,6 +33,12 @@ class HtmlBBCodeParser extends BBCodeParser {
         */
        public static $codeTagNames = ['kbd', 'pre'];
        
+       /**
+        * Google AMP support
+        * @var boolean
+        */
+       protected $isGoogleAmp = false;
+       
        /**
         * list of open tags with name and uuid
         * @var array
@@ -77,6 +83,26 @@ class HtmlBBCodeParser extends BBCodeParser {
                return $this->parsedText;
        }
        
+       /**
+        * Enables or disables Google AMP support.
+        * 
+        * @param       boolean         $isGoogleAmp
+        * @since       3.1
+        */
+       public function setIsGoogleAmp($isGoogleAmp) {
+               $this->isGoogleAmp = $isGoogleAmp;
+       }
+       
+       /**
+        * Returns true if Google AMP support is enabled.
+        * 
+        * @return      boolean
+        * @since       3.1
+        */
+       public function getIsGoogleAmp() {
+               return $this->isGoogleAmp;
+       }
+       
        /**
         * Reverts tags to their source representation if they either
         * have no matching counter part (such as opening tags without
index e7e409b6f447257eb68036abd1471653024f8157..82c26335a43a6e3a51f5f9f8ef0c83222151070e 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 namespace wcf\system\html\output;
+use wcf\system\bbcode\HtmlBBCodeParser;
 use wcf\util\DOMUtil;
 
 /**
@@ -101,8 +102,15 @@ class AmpHtmlOutputProcessor extends HtmlOutputProcessor {
         * @inheritDoc
         */
        public function getHtml() {
+               // temporarily enable AMP output mode for bbcodes
+               HtmlBBCodeParser::getInstance()->setIsGoogleAmp(true);
+               
                $html = $this->getHtmlOutputNodeProcessor()->getHtml();
                
+               // disable AMP output again in order to prevent interference with other
+               // content types that may be processed in the same request
+               HtmlBBCodeParser::getInstance()->setIsGoogleAmp(false);
+               
                $html = preg_replace_callback('/<img([^>]+)>/i', function($match) {
                        $attributes = str_replace('data-width="', 'width="', $match[1]);
                        $attributes = str_replace('data-height="', 'height="', $attributes);
index 10b5ede891604ed9b5b280a73804e269d1b4298a..8719be69b93680822dc3f1fdbc1b8a360dff9fdf 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 namespace wcf\system\html\output\node;
+use wcf\system\bbcode\HtmlBBCodeParser;
 use wcf\system\html\node\AbstractHtmlNodeProcessor;
 use wcf\system\WCF;
 use wcf\util\StringUtil;
@@ -48,6 +49,6 @@ class HtmlOutputNodeWoltlabSpoiler extends AbstractHtmlOutputNode {
                WCF::getTPL()->assign([
                        'buttonLabel' => $data['label']
                ]);
-               return WCF::getTPL()->fetch('spoilerMetaCode');
+               return WCF::getTPL()->fetch((HtmlBBCodeParser::getInstance()->getIsGoogleAmp() ? 'spoilerAmpMetaCode' : 'spoilerMetaCode'));
        }
 }