forked from mtibben/html2text
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[+]: merge changes from yeahbert/html2text "elementConversionOptions"
- Loading branch information
Lars Moelleken
committed
Jun 21, 2016
1 parent
057138c
commit 552bd09
Showing
9 changed files
with
394 additions
and
97 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,101 @@ | ||
<?php | ||
|
||
namespace Html2Text; | ||
|
||
use voku\Html2Text\Html2Text; | ||
|
||
class ElementsTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
public function testPrependAndAppend() | ||
{ | ||
$html = <<<EOT | ||
<h1>Should have "AAA " prepended</h1> | ||
<h4>Should have " BBB" appended</h4> | ||
<h6>Should have "AAA " prepended and " BBB" appended</h6> | ||
<li>Dash instead of asterisk</li> | ||
EOT; | ||
|
||
$expected = <<<EOT | ||
AAA Should have "AAA " prepended | ||
Should have " BBB" appended BBB | ||
AAA Should have "AAA " prepended and " BBB" appended BBB | ||
- Dash instead of asterisk | ||
EOT; | ||
|
||
$html2text = new Html2Text( | ||
$html, | ||
array( | ||
'elements' => array( | ||
'h1' => array('case' => Html2Text::OPTION_NONE, 'prepend' => "\nAAA "), | ||
'h4' => array('case' => Html2Text::OPTION_NONE, 'append' => " BBB\n"), | ||
'h6' => array('case' => Html2Text::OPTION_NONE, 'prepend' => "\nAAA ", 'append' => " BBB\n"), | ||
'li' => array('prepend' => "\n\t- "), | ||
), | ||
) | ||
); | ||
|
||
self::assertEquals($this->normalizeString($expected), $html2text->getText()); | ||
} | ||
|
||
public function testReplace() | ||
{ | ||
$html = <<<EOT | ||
<h1>Should have "AAA" changed to BBB</h1> | ||
<li>• Custom bullet should be removed</li> | ||
EOT; | ||
|
||
$expected = <<<EOT | ||
Should have "BBB" changed to BBB | ||
* Custom bullet should be removed | ||
EOT; | ||
|
||
$html2text = new Html2Text( | ||
$html, | ||
array( | ||
'width' => 0, | ||
'elements' => array( | ||
'h1' => array('case' => Html2Text::OPTION_NONE, 'replace' => array('AAA', 'BBB')), | ||
'li' => array('replace' => array('•', '')), | ||
), | ||
) | ||
); | ||
|
||
self::assertEquals($this->normalizeString($expected), $html2text->getText()); | ||
} | ||
|
||
/** | ||
* @dataProvider getSpacesData | ||
* | ||
* @param $expected | ||
* @param $html | ||
*/ | ||
public function testTrimSpaces($expected, $html) | ||
{ | ||
$html2text = new Html2Text($html); | ||
|
||
self::assertEquals($expected, $html2text->getText()); | ||
} | ||
|
||
/** | ||
* @see testTrimSpaces | ||
* @return array | ||
*/ | ||
public function getSpacesData() | ||
{ | ||
return array( | ||
array($this->normalizeString('BOLD WITH SPACE: Rest of text'), "<b>Bold with space: </b>Rest of text"), | ||
array($this->normalizeString("BOLD WITH SPACE: Rest of text"), " <p> <b>Bold with space: </b>Rest of text </p> "), | ||
array($this->normalizeString("BOLD WITH SPACE: Rest of text"), " <p> <b>Bold with space: </b>Rest of text </p> "), | ||
); | ||
} | ||
|
||
protected function normalizeString($string) | ||
{ | ||
return str_replace(array("\r\n", "\r"), "\n", $string); | ||
} | ||
} |
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,56 @@ | ||
<?php | ||
namespace Html2Text; | ||
|
||
use voku\Html2Text\Html2Text; | ||
|
||
/** | ||
* @copyright ResearchGate GmbH | ||
*/ | ||
class HeadingsTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
public function testHeadings() | ||
{ | ||
$html = <<<EOT | ||
<h1>Will be UTF-8 (äöüèéилčλ) lowercased</h1> | ||
<h2>Will be UTF-8 (äöüèéилčλ) ucfirst</h2> | ||
<h3>Will be UTF-8 (äöüèéилčλ) titled</h3> | ||
<h5>Will be UTF-8 (äöüèéилčλ) uppercased as default</h5> | ||
<h6>Will be UTF-8 (äöüèéилčλ) uppercased</h6> | ||
<p>Will remain lowercased</p> | ||
EOT; | ||
$expected = <<<EOT | ||
will be utf-8 (äöüèéилčλ) lowercased | ||
Will be utf-8 (äöüèéилčλ) ucfirst | ||
Will Be Utf-8 (Äöüèéилčλ) Titled | ||
WILL BE UTF-8 (ÄÖÜÈÉИЛČΛ) UPPERCASED AS DEFAULT | ||
WILL BE UTF-8 (ÄÖÜÈÉИЛČΛ) UPPERCASED | ||
Will remain lowercased | ||
EOT; | ||
|
||
$html2text = new Html2Text( | ||
$html, | ||
array( | ||
'elements' => array( | ||
'h1' => array('case' => Html2Text::OPTION_LOWERCASE), | ||
'h2' => array('case' => Html2Text::OPTION_UCFIRST), | ||
'h3' => array('case' => Html2Text::OPTION_TITLE), | ||
'h6' => array('case' => Html2Text::OPTION_UPPERCASE), | ||
), | ||
) | ||
); | ||
$output = $html2text->getText(); | ||
|
||
self::assertEquals($this->normalizeString($expected), $output); | ||
} | ||
|
||
protected function normalizeString($string) | ||
{ | ||
return str_replace(array("\r\n", "\r"), "\n", $string); | ||
} | ||
} |
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
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
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