/** * @test */ public function getAndSetInnerTagContentTest() { $html = '<p class="bodytext">Testtext bla bla <a href="#">Test</a> Test</p>'; $htmlExpected = '<p class="bodytext">Testtext bla bla <a href="#">Test</a> Test TESTSTRING</p>'; $testCallback = function ($content) { return $content . ' TESTSTRING'; }; $this->assertEquals($htmlExpected, ParserUtility::getAndSetInnerTagContent($html, $testCallback)); $html = '<p>Testtext <p>Test</p>Test</p>'; $htmlExpected = '<p>Testtext <p>Test</p>Test TESTSTRING</p>'; $this->assertEquals($htmlExpected, ParserUtility::getAndSetInnerTagContent($html, $testCallback)); }
/** * Main function called by hook 'contentPostProc-all' * * @return void */ public function pageParser() { // extract Pids which should be parsed $parsingPids = GeneralUtility::trimExplode(',', $this->settings['parsingPids']); // extract Pids which should NOT be parsed $excludePids = GeneralUtility::trimExplode(',', $this->settings['parsingExcludePidList']); // Get Tags which content should be parsed $tags = GeneralUtility::trimExplode(',', $this->settings['parsingTags']); // Remove "a" from parsingTags if it was added unknowingly if (TRUE === in_array('a', $tags)) { $tags = array_diff($tags, array('a')); } /* * Abort if: * - Parsing tags are empty * - Page type is not 0 * - Terms array is empty * - tsConfig is empty * - no storagePid is set * - parsingPids doesn't contains 0 and * + current page is excluded * + current page is not whitelisted * - current page is the glossary detailpage * - current page is the glossary listpage */ if (TRUE === empty($tags) || 0 !== $GLOBALS['TSFE']->type || TRUE === empty($this->terms) || TRUE === empty($this->tsConfig) || TRUE === empty($this->tsConfig['persistence.']['storagePid']) || FALSE === in_array('0', $parsingPids) && (TRUE === in_array($GLOBALS['TSFE']->id, $excludePids) || FALSE === in_array($GLOBALS['TSFE']->id, $parsingPids)) || $GLOBALS['TSFE']->id === intval($this->settings['detailPage']) || $GLOBALS['TSFE']->id === intval($this->settings['listPage']) || TRUE === (bool) $this->settings['disableParser']) { return; } // Tags which are not allowed as direct parent for a parsingTag $forbiddenParentTags = array_filter(GeneralUtility::trimExplode(',', $this->settings['forbiddenParentTags'])); // Add "a" if unknowingly deleted to prevent errors if (FALSE === in_array('a', $forbiddenParentTags)) { $forbiddenParentTags[] = 'a'; } //Create new DOMDocument $DOM = new \DOMDocument(); // Prevent crashes caused by HTML5 entities with internal errors libxml_use_internal_errors(true); // Load Page HTML in DOM and check if HTML is valid else abort // use XHTML tag for avoiding UTF-8 encoding problems if (FALSE === $DOM->loadHTML('<?xml encoding="UTF-8">' . ParserUtility::protectInlineJSFromDOM($GLOBALS['TSFE']->content))) { return; } // remove unnecessary whitespaces in nodes (no visible whitespace) $DOM->preserveWhiteSpace = false; /** @var \DOMElement $DOMBody */ $DOMBody = $DOM->getElementsByTagName('body')->item(0); // iterate over tags which are defined to be parsed foreach ($tags as $tag) { // extract the tags $DOMTags = $DOMBody->getElementsByTagName($tag); // call the nodereplacer for each node to parse its content /** @var \DOMNode $DOMTag */ foreach ($DOMTags as $DOMTag) { // get parent tags from root tree string $parentTags = explode('/', preg_replace('#\\[([^\\]]*)\\]#i', '', substr($DOMTag->parentNode->getNodePath(), 1))); // check if element is children of a forbidden parent if (FALSE === in_array($parentTags, $forbiddenParentTags)) { ParserUtility::domNodeContentReplacer($DOMTag, ParserUtility::getAndSetInnerTagContent($DOMTag->ownerDocument->saveHTML($DOMTag), array($this, 'textParser'))); } } } // set the parsed html page and remove XHTML tag which is not needed anymore $GLOBALS['TSFE']->content = str_replace('<?xml encoding="UTF-8">', '', ParserUtility::protectInlineJSFromDOMReverse($DOM->saveHTML())); }