Elgg  Version 3.0
Public Member Functions | Protected Attributes | List of all members
Elgg\Views\HtmlFormatter Class Reference

Various helper method for formatting and sanitizing output. More...

Public Member Functions

 __construct (LoggerInterface $logger, ViewsService $views, PluginHooksService $hooks,\ElggAutoP $autop)
 Output constructor. More...
 
 formatBlock ($html, array $options=[])
 Prepare HTML output. More...
 
 parseUrls ($text)
 Takes a string and turns any URLs into formatted links. More...
 
 parseEmails ($text)
 Takes a string and turns any email addresses into formatted links. More...
 
 addParagaraphs ($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 ($tag_name, array $attributes=[], $text= '', array $options=[])
 Format an HTML element. More...
 
 stripTags ($string, $allowable_tags=null)
 Strip tags and offer plugins the chance. More...
 
 decode ($string)
 Decode HTML markup into a raw text string. More...
 

Protected Attributes

 $views
 
 $hooks
 
 $autop
 

Detailed Description

Various helper method for formatting and sanitizing output.

Definition at line 13 of file HtmlFormatter.php.

Constructor & Destructor Documentation

Elgg\Views\HtmlFormatter::__construct ( LoggerInterface  $logger,
ViewsService  $views,
PluginHooksService  $hooks,
\ElggAutoP  $autop 
)

Output constructor.

Parameters
LoggerInterface$loggerLogger
ViewsService$viewsViews service
PluginHooksService$hooksHooks
\ElggAutoP$autopParagraph wrapper

Definition at line 40 of file HtmlFormatter.php.

Member Function Documentation

Elgg\Views\HtmlFormatter::addParagaraphs (   $string)

Create paragraphs from text with line spacing.

Parameters
string$stringThe string
Returns
string

Definition at line 143 of file HtmlFormatter.php.

Elgg\Views\HtmlFormatter::decode (   $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. &gt; 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.

Parameters
string$stringEncoded HTML
Returns
string
Author
Pádraic Brady

Definition at line 367 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>)

Parameters
array$attrsAttributes An array of attribute => value pairs Attribute value can be a scalar value, an array of scalar values, or true $attrs = array( '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 );
Returns
string
See also
elgg_format_element()

Definition at line 179 of file HtmlFormatter.php.

Elgg\Views\HtmlFormatter::formatBlock (   $html,
array  $options = [] 
)

Prepare HTML output.

Parameters
string$htmlHTML string
array$optionsFormatting 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

Returns
string

Definition at line 66 of file HtmlFormatter.php.

Elgg\Views\HtmlFormatter::formatElement (   $tag_name,
array  $attributes = [],
  $text = '',
array  $options = [] 
)

Format an HTML element.

Parameters
string | array$tag_nameThe element tagName. e.g. "div". This will not be validated. All function arguments can be given as a single array: The array will be used as $attributes, except for the keys "#tag_name", "#text", and "#options", which will be extracted as the other arguments.
array$attributesThe element attributes. This is passed to elgg_format_attributes().
string$textThe contents of the element. Assumed to be HTML unless encode_text is true.
array$optionsOptions 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 />"

Returns
string
Since
1.9.0

Definition at line 262 of file HtmlFormatter.php.

Elgg\Views\HtmlFormatter::parseEmails (   $text)

Takes a string and turns any email addresses into formatted links.

Parameters
string$textThe input string
Returns
string The output string with formatted links
Since
2.3

Definition at line 130 of file HtmlFormatter.php.

Elgg\Views\HtmlFormatter::parseUrls (   $text)

Takes a string and turns any URLs into formatted links.

Parameters
string$textThe input string
Returns
string The output string with formatted links

Definition at line 114 of file HtmlFormatter.php.

Elgg\Views\HtmlFormatter::stripTags (   $string,
  $allowable_tags = null 
)

Strip tags and offer plugins the chance.

Plugins register for output:strip_tags plugin hook. Original string included in $params['original_string']

Parameters
string$stringFormatted string
string$allowable_tagsOptional parameter to specify tags which should not be stripped
Returns
string String run through strip_tags() and any plugin hooks.

Definition at line 328 of file HtmlFormatter.php.

Member Data Documentation

Elgg\Views\HtmlFormatter::$autop
protected

Definition at line 30 of file HtmlFormatter.php.

Elgg\Views\HtmlFormatter::$hooks
protected

Definition at line 25 of file HtmlFormatter.php.

Elgg\Views\HtmlFormatter::$views
protected

Definition at line 20 of file HtmlFormatter.php.


The documentation for this class was generated from the following file: