-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
195 additions
and
12 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1 +1 @@ | ||
1.02 | ||
1.03 |
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,135 @@ | ||
<?php | ||
/** | ||
* PHP Speller | ||
* | ||
* @copyright 2015, Михаил Красильников <m.krasilnikov@yandex.ru> | ||
* @author Михаил Красильников <m.krasilnikov@yandex.ru> | ||
* @license http://opensource.org/licenses/MIT MIT | ||
*/ | ||
namespace Mekras\Speller\Source\Filter; | ||
|
||
/** | ||
* Filter replaces HTML tags with spaces | ||
* | ||
* @since 1.03 | ||
*/ | ||
class HtmlFilter implements Filter | ||
{ | ||
/** | ||
* Attrs with text contents | ||
* | ||
* @var string[] | ||
*/ | ||
private $textAttrs = ['title']; | ||
|
||
/** | ||
* Filter string | ||
* | ||
* @param string $string string to be filtered | ||
* | ||
* @return string filtered string | ||
* | ||
* @since 1.03 | ||
*/ | ||
public function filter($string) | ||
{ | ||
$result = ''; | ||
|
||
// Current/last tag name | ||
$tagName = null; | ||
// Current/last attribute name | ||
$attrName = null; | ||
// Current context | ||
$context = null; | ||
// Expected context | ||
$expecting = null; | ||
|
||
$length = mb_strlen($string); | ||
for ($i = 0; $i < $length; $i++) { | ||
$ch = mb_substr($string, $i, 1); | ||
switch ($ch) { | ||
|
||
case '<': | ||
$context = 'tag_name'; | ||
$tagName = null; | ||
$ch = ' '; | ||
break; | ||
|
||
case '>': | ||
$context = null; | ||
$expecting = null; | ||
$ch = ' '; | ||
break; | ||
|
||
case ' ': | ||
case "\n": | ||
case "\t": | ||
switch ($context) { | ||
|
||
case 'tag_name': | ||
$context = 'tag_attrs'; | ||
break; | ||
|
||
case 'attr_name': | ||
$context = 'tag_attrs'; | ||
break; | ||
} | ||
break; | ||
|
||
case '=': | ||
if ('attr_name' === $context || 'tag_attrs' === $context) { | ||
$expecting = 'attr_value'; | ||
$ch = ' '; | ||
} | ||
break; | ||
|
||
case '"': | ||
case "'": | ||
switch (true) { | ||
|
||
case 'attr_value' === $expecting: | ||
$context = 'attr_value'; | ||
if (in_array(strtolower($attrName), $this->textAttrs, true)) { | ||
$context = 'attr_text'; | ||
} | ||
$expecting = null; | ||
$ch = ' '; | ||
break; | ||
|
||
case 'attr_value' === $context: | ||
case 'attr_text' === $context: | ||
$context = 'tag_attrs'; | ||
$ch = ' '; | ||
break; | ||
} | ||
break; | ||
|
||
default: | ||
switch ($context) { | ||
|
||
case 'tag_name': | ||
$tagName .= $ch; | ||
$ch = ' '; | ||
break; | ||
|
||
/** @noinspection PhpMissingBreakStatementInspection */ | ||
case 'tag_attrs': | ||
$context = 'attr_name'; | ||
$attrName = null; | ||
// no break needed | ||
case 'attr_name': | ||
$attrName .= $ch; | ||
$ch = ' '; | ||
break; | ||
|
||
case 'attr_value': | ||
$ch = ' '; | ||
break; | ||
} | ||
} | ||
$result .= $ch; | ||
} | ||
|
||
return $result; | ||
} | ||
} |
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,31 @@ | ||
<?php | ||
/** | ||
* PHP Speller | ||
* | ||
* @copyright 2015, Михаил Красильников <m.krasilnikov@yandex.ru> | ||
* @author Михаил Красильников <m.krasilnikov@yandex.ru> | ||
* @license http://opensource.org/licenses/MIT MIT | ||
*/ | ||
namespace Mekras\Speller\Tests\Source\Filter; | ||
|
||
use Mekras\Speller\Source\Filter\HtmlFilter; | ||
use PHPUnit_Framework_TestCase as TestCase; | ||
|
||
/** | ||
* Tests for Mekras\Speller\Source\Filter\HtmlFilter | ||
* | ||
* @covers Mekras\Speller\Source\Filter\HtmlFilter | ||
*/ | ||
class HtmlFilterTest extends TestCase | ||
{ | ||
/** | ||
* Test basic functional | ||
*/ | ||
public function testBasics() | ||
{ | ||
$filter = new HtmlFilter(); | ||
$html = "<br>foo <a\nhref = '#' title='bar'>\nbaz</a>"; | ||
$text = " foo \n bar \nbaz "; | ||
static::assertEquals($text, $filter->filter($html)); | ||
} | ||
} |