-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
imp: Display feeds description as HTML
- Loading branch information
1 parent
5c4c1f4
commit 28f2308
Showing
8 changed files
with
131 additions
and
8 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace flusio\utils; | ||
|
||
/** | ||
* @author Marien Fressinaud <dev@marienfressinaud.fr> | ||
* @license http://www.gnu.org/licenses/agpl-3.0.en.html AGPL | ||
*/ | ||
class HtmlSanitizer | ||
{ | ||
public static function sanitizeCollectionDescription(string $description, string $base_url): string | ||
{ | ||
$html_sanitizer = new \SpiderBits\HtmlSanitizer([ | ||
'abbr' => [], | ||
'a' => ['href', 'title'], | ||
'blockquote' => [], | ||
'br' => [], | ||
'caption' => [], | ||
'code' => [], | ||
'dd' => [], | ||
'del' => [], | ||
'details' => ['open'], | ||
'div' => [], | ||
'dl' => [], | ||
'dt' => [], | ||
'em' => [], | ||
'figcaption' => [], | ||
'figure' => [], | ||
'i' => [], | ||
'li' => [], | ||
'ol' => [], | ||
'pre' => [], | ||
'p' => [], | ||
'q' => [], | ||
'rp' => [], | ||
'rt' => [], | ||
'ruby' => [], | ||
'small' => [], | ||
'span' => [], | ||
'strong' => [], | ||
'sub' => [], | ||
'summary' => [], | ||
'sup' => [], | ||
'u' => [], | ||
'ul' => [], | ||
], [ | ||
'h1', | ||
'h2', | ||
'h3', | ||
'h4', | ||
'h5', | ||
'h6', | ||
]); | ||
|
||
$healthy_html = $html_sanitizer->sanitize($description); | ||
|
||
$libxml_options = LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD; | ||
$dom = \SpiderBits\Dom::fromText("<div>{$healthy_html}</div>", $libxml_options); | ||
$anchors = $dom->select('//a'); | ||
|
||
if ($anchors) { | ||
foreach ($anchors->list() as $node) { | ||
if (!($node instanceof \DOMElement)) { | ||
continue; | ||
} | ||
|
||
if ($node->hasAttribute('href')) { | ||
// Absolutize the URL of the href attribute. | ||
$href_node = $node->getAttributeNode('href'); | ||
$url = \SpiderBits\Url::absolutize($href_node->value, $base_url); | ||
$href_node->value = \Minz\Output\ViewHelpers::protect($url); | ||
|
||
// Make sure to open the URL in a new tab. | ||
$target_node = new \DOMAttr('target', '_blank'); | ||
$node->appendChild($target_node); | ||
|
||
$rel_node = new \DOMAttr('rel', 'noopener noreferrer'); | ||
$node->appendChild($rel_node); | ||
} | ||
} | ||
} | ||
|
||
return $dom->html(); | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
namespace flusio\utils; | ||
|
||
class HtmlSanitizerTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
public function testSanitizeCollectionDescription(): void | ||
{ | ||
$description = <<<HTML | ||
<h1>Welcome!</h1> | ||
<div>Hello <a href="/world">World</a></div> | ||
<script>alert('oops');</script> | ||
HTML; | ||
$expected_description = <<<HTML | ||
<div>Welcome! | ||
<div>Hello <a href="https://example.com/world" target="_blank" rel="noopener noreferrer">World</a></div> | ||
</div> | ||
HTML; | ||
$base_url = 'https://example.com'; | ||
|
||
$result = HtmlSanitizer::sanitizeCollectionDescription($description, $base_url); | ||
|
||
$this->assertSame($expected_description, trim($result)); | ||
} | ||
} |