CoolCMS API

EsHighlightSnippet
in package

FinalYes

Shortens an already safe snippet from {@see EsHighlighter} to a length a listing can bear.

It does not produce HTML, it only cuts it; the safety invariant stays in EsHighlighter and this class must not break it.

EsHighlighter::requestBody() asks for number_of_fragments: 0, so ES returns the whole field, which can be over a thousand characters, and a highlighted advert would flood the listing. A plain |truncate:150 on the finished HTML is not enough: it could cut inside <em> or an entity (&amp; to &am), it would count tags into the limit, and it cuts from the start, so a match at the 400th character would be invisible. Therefore the window is chosen around the first match and cut by tokens.

Output invariants (same as for the input, which may contain only text, entities and paired <em>/</em> without attributes):

  • no new tag or attribute ever appears (tags are only copied or dropped),
  • <em> is always paired (an open window is closed, a cut one is reopened),
  • an entity is never split (it is one token).

Table of Contents

Constants

ELLIPSIS  : mixed = '…'
Ellipsis at the start or end of a shortened window.

Methods

around()  : string
Returns a window around the first highlighted match with at most `$limit` visible characters.
tagDelta()  : int
Returns +1 for `<em>`, -1 for `</em>`, 0 for a visible token.
tokenize()  : array<int, string>
Splits into tokens: `<em>`, `</em>`, an HTML entity, one UTF-8 character. Cutting happens only on them, so a tag or entity is never split.
visibleLength()  : int
windowStart()  : int
Returns the token index where the window starts: `$lead` visible characters before the first `<em>`, or the beginning when there is no highlight (highlight on another field).

Constants

ELLIPSIS

Ellipsis at the start or end of a shortened window.

private mixed ELLIPSIS = '…'

Methods

around()

Returns a window around the first highlighted match with at most `$limit` visible characters.

public static around(string $safeHtml[, int $limit = 150 ][, int $lead = 40 ]) : string
Parameters
$safeHtml : string

output of EsHighlighter::toSafeHtml()

$limit : int = 150

maximum number of visible characters (tags are not counted)

$lead : int = 40

how many visible characters to keep before the match (context)

Return values
string

tagDelta()

Returns +1 for `<em>`, -1 for `</em>`, 0 for a visible token.

private static tagDelta(string $token) : int
Parameters
$token : string
Return values
int

tokenize()

Splits into tokens: `<em>`, `</em>`, an HTML entity, one UTF-8 character. Cutting happens only on them, so a tag or entity is never split.

private static tokenize(string $safeHtml) : array<int, string>
Parameters
$safeHtml : string
Return values
array<int, string>

visibleLength()

private static visibleLength(array<int, string> $tokens) : int
Parameters
$tokens : array<int, string>
Return values
int

windowStart()

Returns the token index where the window starts: `$lead` visible characters before the first `<em>`, or the beginning when there is no highlight (highlight on another field).

private static windowStart(array<int, string> $tokens, int $lead) : int
Parameters
$tokens : array<int, string>
$lead : int
Return values
int
On this page

Search results