/** * @test */ public function trimFromHTMLString() { $helper = new DOMHelper(); $directory = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'htmlData' . DIRECTORY_SEPARATOR; $dom = new DOMDocument(); $input = file_get_contents($directory . 'trimAfterString_input_1.html'); // Si le marqueur n'existe pas, le texte est renvoyé intact $expected = str_replace("\n", "", $input); $actual = str_replace("\n", "", $helper->trimFromHTMLString($input, "{{XXXXXX}}")); $this->assertEquals(preg_replace('/\\s+/', '', $expected), preg_replace('/\\s+/', '', $actual)); // Suppression simple $htmlHead = '<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"><title>***</title></head><body>'; $htmlFoot = '</body></html>'; $actual = str_replace("\n", "", $helper->trimFromHTMLString($input, "{{LIRE_LA_SUITE}}")); $dom->loadHTML($htmlHead . file_get_contents($directory . 'trimAfterString_output_1.html') . $htmlFoot); $expected = $this->cleanTmpHTML(str_replace("\n", "", $dom->saveHTML()), $htmlHead, $htmlFoot); $this->assertEquals(preg_replace('/\\s+/', '', $expected), preg_replace('/\\s+/', '', $actual)); // Suppression avec insertion d'un bouton "Lire la suite" $actual = str_replace("\n", "", $helper->trimFromHTMLString($input, "{{LIRE_LA_SUITE}}", "<button>Lire la suite</button>")); $dom->loadHTML($htmlHead . file_get_contents($directory . 'trimAfterString_output_2.html') . $htmlFoot); $expected = $this->cleanTmpHTML(str_replace("\n", "", $dom->saveHTML()), $htmlHead, $htmlFoot); $this->assertEquals(preg_replace('/\\s+/', '', $expected), preg_replace('/\\s+/', '', $actual)); // Suppression avec insertion d'un texte et d'un bouton "Lire la suite" $actual = str_replace("\n", "", $helper->trimFromHTMLString($input, "{{LIRE_LA_SUITE}}", "Pour en savoir plus : <button>Lire la suite</button>")); $dom->loadHTML($htmlHead . file_get_contents($directory . 'trimAfterString_output_3.html') . $htmlFoot); $expected = $this->cleanTmpHTML(str_replace("\n", "", $dom->saveHTML()), $htmlHead, $htmlFoot); $this->assertEquals(preg_replace('/\\s+/', '', $expected), preg_replace('/\\s+/', '', $actual)); }
/** * Tronque un texte formatté en HTML à partir du marqueur {{LIRE_LA_SUITE}}. * Toutes les balises ouvertes sont refermées pour que le texte renvoyé soit toujours du HTML correct. * Le marqueur {{LIRE_LA_SUITE}} est remplacé par le bouton $buttonMore * @param string $fullText * @param string $buttonMore * @return string */ public static function getShortHTMLText($fullText, $buttonMore = null) { $tag = '{{LIRE_LA_SUITE}}'; if (strpos($fullText, $tag) === false) { // Si le marqueur n'est pas là, inutile de faire tous ces traitements... return $fullText; } if (is_null($buttonMore)) { $buttonMore = self::getButtonMore(); } $helper = new DOMHelper(); try { if (!($out = $helper->trimFromHTMLString($fullText, $tag, $buttonMore))) { Yii::log(h::_("Erreur sur l'appel à trimFromHTMLString()", __FILE__, __LINE__, __METHOD__), CLogger::LEVEL_ERROR); return $fullText; } } catch (Exception $x) { Yii::log(h::_($x, __FILE__, __LINE__, __METHOD__), CLogger::LEVEL_ERROR); return "<error>" . $x->getMessage() . "</error>"; } return $out; }