Elgg
Version master
|
Various helper method for formatting and sanitizing output. More...
Public Member Functions | |
__construct (protected ViewsService $views, protected EventsService $events, protected AutoParagraph $autop) | |
Output constructor. More... | |
formatBlock (string $html, array $options=[]) | |
Prepare HTML output. More... | |
parseUrls (string $text) | |
Takes a string and turns any URLs into formatted links. More... | |
parseEmails (string $text) | |
Takes a string and turns any email addresses into formatted links. More... | |
parseMentions (string $text) | |
Takes a string and turns any @ mentions into a formatted link. More... | |
addParagaraphs (string $string) | |
Create paragraphs from text with line spacing. More... | |
formatAttributes (array $attrs=[]) | |
Converts an associative array into a string of well-formed HTML/XML attributes Returns a concatenated string of HTML attributes to be inserted into a tag (e.g., <tag $attrs>) More... | |
formatElement (string $tag_name, array $attributes=[], string $text= '', array $options=[]) | |
Format an HTML element. More... | |
stripTags (string $string, string $allowable_tags=null) | |
Strip tags and offer plugins the chance. More... | |
decode (string $string) | |
Decode HTML markup into a raw text string. More... | |
inlineCss (string $html, string $css, bool $body_only=false) | |
Adds inline style to html content. More... | |
normalizeUrls (string $text) | |
Replaces relative urls in href or src attributes in text. More... | |
Public Attributes | |
const | MENTION_REGEX = '/<a[^>]*?>.*?<\/a>|<.*?>|(^|\s|\!|\.|\?|>|\G)+(@([^\s<&]+))/iu' |
Mentions regex. More... | |
Various helper method for formatting and sanitizing output.
Definition at line 16 of file HtmlFormatter.php.
Elgg\Views\HtmlFormatter::__construct | ( | protected ViewsService | $views, |
protected EventsService | $events, | ||
protected AutoParagraph | $autop | ||
) |
Output constructor.
ViewsService | $views | Views service |
EventsService | $events | Events service |
AutoParagraph | $autop | Paragraph wrapper |
Definition at line 47 of file HtmlFormatter.php.
Elgg\Views\HtmlFormatter::addParagaraphs | ( | string | $string | ) |
Create paragraphs from text with line spacing.
string | $string | The string |
Definition at line 193 of file HtmlFormatter.php.
Elgg\Views\HtmlFormatter::decode | ( | string | $string | ) |
Decode HTML markup into a raw text string.
This applies html_entity_decode() to a string while re-entitising HTML special char entities to prevent them from being decoded back to their unsafe original forms.
This relies on html_entity_decode() not translating entities when doing so leaves behind another entity, e.g. > if decoded would create > which is another entity itself. This seems to escape the usual behaviour where any two paired entities creating a HTML tag are usually decoded, i.e. a lone > is not decoded, but <foo> would be decoded to <foo> since it creates a full tag.
Note: html_entity_decode() is poorly explained in the manual - which is really bad given its potential for misuse on user input already escaped elsewhere. Stackoverflow is littered with advice to use this function in the precise way that would lead to user input being capable of injecting arbitrary HTML.
string | $string | Encoded HTML |
Definition at line 389 of file HtmlFormatter.php.
Elgg\Views\HtmlFormatter::formatAttributes | ( | array | $attrs = [] | ) |
Converts an associative array into a string of well-formed HTML/XML attributes Returns a concatenated string of HTML attributes to be inserted into a tag (e.g., <tag $attrs>)
An example of the attributes: Attribute value can be a scalar value, an array of scalar values, or true $attrs = [ 'class' => ['elgg-input', 'elgg-input-text'], // will be imploded with spaces 'style' => ['margin-left:10px;', 'color: #666;'], // will be imploded with spaces 'alt' => 'Alt text', // will be left as is 'disabled' => true, // will be converted to disabled="disabled" 'data-options' => json_encode(['foo' => 'bar']), // will be output as an escaped JSON string 'batch' => <>, // will be ignored 'items' => [<>], // will be ignored ];
array | $attrs | An array of attribute => value pairs |
Definition at line 230 of file HtmlFormatter.php.
Elgg\Views\HtmlFormatter::formatBlock | ( | string | $html, |
array | $options = [] |
||
) |
Prepare HTML output.
string | $html | HTML string |
array | $options | Formatting options |
bool $parse_urls Replace URLs with anchor tags bool $parse_emails Replace email addresses with anchor tags bool $sanitize Sanitize HTML tags bool $autop Add paragraphs instead of new lines
Definition at line 67 of file HtmlFormatter.php.
Elgg\Views\HtmlFormatter::formatElement | ( | string | $tag_name, |
array | $attributes = [] , |
||
string | $text = '' , |
||
array | $options = [] |
||
) |
Format an HTML element.
string | $tag_name | The element tagName. e.g. "div". This will not be validated. |
array | $attributes | The element attributes. |
string | $text | The contents of the element. Assumed to be HTML unless encode_text is true. |
array | $options | Options array with keys: - encode_text => (bool, default false) If true, $text will be HTML-escaped. Already-escaped entities will not be double-escaped. - double_encode => (bool, default false) If true, the $text HTML escaping will be allowed to double encode HTML entities: '×' will become '&times;' - is_void => (bool) If given, this determines whether the function will return just the open tag. Otherwise this will be determined by the tag name according to this list: http://www.w3.org/html/wg/drafts/html/master/single-page.html#void-elements - is_xml => (bool, default false) If true, void elements will be formatted like "<tag />" |
InvalidArgumentException |
Definition at line 311 of file HtmlFormatter.php.
Elgg\Views\HtmlFormatter::inlineCss | ( | string | $html, |
string | $css, | ||
bool | $body_only = false |
||
) |
Adds inline style to html content.
string | $html | html content |
string | $css | style text |
bool | $body_only | toggle to return the body contents instead of a full html |
Definition at line 414 of file HtmlFormatter.php.
Elgg\Views\HtmlFormatter::normalizeUrls | ( | string | $text | ) |
Replaces relative urls in href or src attributes in text.
string | $text | source content |
Definition at line 434 of file HtmlFormatter.php.
Elgg\Views\HtmlFormatter::parseEmails | ( | string | $text | ) |
Takes a string and turns any email addresses into formatted links.
string | $text | The input string |
Definition at line 131 of file HtmlFormatter.php.
Elgg\Views\HtmlFormatter::parseMentions | ( | string | $text | ) |
Takes a string and turns any @ mentions into a formatted link.
string | $text | The input string |
Definition at line 145 of file HtmlFormatter.php.
Elgg\Views\HtmlFormatter::parseUrls | ( | string | $text | ) |
Takes a string and turns any URLs into formatted links.
string | $text | The input string |
Definition at line 116 of file HtmlFormatter.php.
Elgg\Views\HtmlFormatter::stripTags | ( | string | $string, |
string | $allowable_tags = null |
||
) |
Strip tags and offer plugins the chance.
Plugins register for output:strip_tags event. Original string included in $params['original_string']
string | $string | Formatted string |
string | $allowable_tags | Optional parameter to specify tags which should not be stripped |
Definition at line 352 of file HtmlFormatter.php.
const Elgg\Views\HtmlFormatter::MENTION_REGEX = '/<a[^>]*?>.*?<\/a>|<.*?>|(^|\s|\!|\.|\?|>|\G)+(@([^\s<&]+))/iu' |
Mentions regex.
Match anchor tag with all attributes and wrapped html we want to exclude matches that have already been wrapped in an anchor '<a[^>]*?>.*?<\/a>'
Match tag name and attributes we want to exclude matches that found within tag attributes '<.*?>'
Match at least one space or punctuation char before a match '(^||!|.|\?|>|)+'
Match @ followed by username
Definition at line 38 of file HtmlFormatter.php.