-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use HtmlString to distinguish between HTML and plain text strings
Also fix bunch of over and under escaping and document the string semantics in API.
- Loading branch information
Showing
15 changed files
with
140 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace helpers; | ||
|
||
/** | ||
* A string wrapper representing a HTML fragment. | ||
*/ | ||
class HtmlString { | ||
/** @var string */ | ||
private $content; | ||
|
||
private function __construct(string $content) { | ||
$this->content = $content; | ||
} | ||
|
||
/** | ||
* Creates a new HtmlString from a string containing a HTML fragment. | ||
*/ | ||
public static function fromRaw(string $content): self { | ||
return new self($content); | ||
} | ||
|
||
/** | ||
* Creates a new HtmlString from a plain text string. | ||
*/ | ||
public static function fromPlainText(string $content): self { | ||
return new self(htmlspecialchars($content)); | ||
} | ||
|
||
/** | ||
* Returns a HTML fragment represented by the object. | ||
*/ | ||
public function getRaw(): string { | ||
return $this->content; | ||
} | ||
|
||
/** | ||
* Returns a plain text without any HTML tags. | ||
*/ | ||
public function getPlainText(): string { | ||
return htmlspecialchars_decode(strip_tags($this->content)); | ||
} | ||
} |
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
Oops, something went wrong.