ILIAS  release_4-4 Revision
HTMLPurifier_Injector_AutoParagraph Class Reference

Injector that auto paragraphs text in the root node based on double-spacing. More...

+ Inheritance diagram for HTMLPurifier_Injector_AutoParagraph:
+ Collaboration diagram for HTMLPurifier_Injector_AutoParagraph:

Public Member Functions

 handleText (&$token)
 
 handleElement (&$token)
 
- Public Member Functions inherited from HTMLPurifier_Injector
 rewind ($index)
 Rewind to a spot to re-perform processing. More...
 
 getRewind ()
 Retrieves rewind, and then unsets it. More...
 
 prepare ($config, $context)
 Prepares the injector by giving it the config and context objects: this allows references to important variables to be made within the injector. More...
 
 checkNeeded ($config)
 This function checks if the HTML environment will work with the Injector: if p tags are not allowed, the Auto-Paragraphing injector should not be enabled. More...
 
 allowsElement ($name)
 Tests if the context node allows a certain element. More...
 
 handleText (&$token)
 Handler that is called when a text token is processed. More...
 
 handleElement (&$token)
 Handler that is called when a start or empty token is processed. More...
 
 handleEnd (&$token)
 Handler that is called when an end token is processed. More...
 
 notifyEnd ($token)
 Notifier that is called when an end token is processed. More...
 

Data Fields

 $name = 'AutoParagraph'
 
 $needed = array('p')
 
- Data Fields inherited from HTMLPurifier_Injector
 $name
 Advisory name of injector, this is for friendly error messages. More...
 
 $needed = array()
 Array of elements and attributes this injector creates and therefore need to be allowed by the definition. More...
 

Private Member Functions

 _pStart ()
 
 _splitText ($data, &$result)
 Splits up a text in paragraph tokens and appends them to the result stream that will replace the original. More...
 
 _isInline ($token)
 Returns true if passed token is inline (and, ergo, allowed in paragraph tags) More...
 
 _pLookAhead ()
 Looks ahead in the token list and determines whether or not we need to insert a. More...
 
 _checkNeedsP ($current)
 Determines if a particular token requires an earlier inline token to get a paragraph. More...
 

Additional Inherited Members

- Protected Member Functions inherited from HTMLPurifier_Injector
 forward (&$i, &$current)
 Iterator function, which starts with the next token and continues until you reach the end of the input tokens. More...
 
 forwardUntilEndToken (&$i, &$current, &$nesting)
 Similar to _forward, but accepts a third parameter $nesting (which should be initialized at 0) and stops when we hit the end tag for the node $this->inputIndex starts in. More...
 
 backward (&$i, &$current)
 Iterator function, starts with the previous token and continues until you reach the beginning of input tokens. More...
 
 current (&$i, &$current)
 Initializes the iterator at the current position. More...
 
- Protected Attributes inherited from HTMLPurifier_Injector
 $htmlDefinition
 Instance of HTMLPurifier_HTMLDefinition. More...
 
 $currentNesting
 Reference to CurrentNesting variable in Context. More...
 
 $inputTokens
 Reference to InputTokens variable in Context. More...
 
 $inputIndex
 Reference to InputIndex variable in Context. More...
 
 $rewind = false
 Index of inputTokens to rewind to. More...
 

Detailed Description

Injector that auto paragraphs text in the root node based on double-spacing.

Todo:

Ensure all states are unit tested, including variations as well.

Make a graph of the flow control for this Injector.

Definition at line 9 of file AutoParagraph.php.

Member Function Documentation

◆ _checkNeedsP()

HTMLPurifier_Injector_AutoParagraph::_checkNeedsP (   $current)
private

Determines if a particular token requires an earlier inline token to get a paragraph.

This should be used with _forwardUntilEndToken

Definition at line 322 of file AutoParagraph.php.

References _isInline().

Referenced by _pLookAhead().

322  {
323  if ($current instanceof HTMLPurifier_Token_Start){
324  if (!$this->_isInline($current)) {
325  // <div>PAR1<div>
326  // ----
327  // Terminate early, since we hit a block element
328  return false;
329  }
330  } elseif ($current instanceof HTMLPurifier_Token_Text) {
331  if (strpos($current->data, "\n\n") !== false) {
332  // <div>PAR1<b>PAR1\n\nPAR2
333  // ----
334  return true;
335  } else {
336  // <div>PAR1<b>PAR1...
337  // ----
338  }
339  }
340  return null;
341  }
Concrete start token class.
Definition: Start.php:6
_isInline($token)
Returns true if passed token is inline (and, ergo, allowed in paragraph tags)
Concrete text token class.
Definition: Text.php:12
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _isInline()

HTMLPurifier_Injector_AutoParagraph::_isInline (   $token)
private

Returns true if passed token is inline (and, ergo, allowed in paragraph tags)

Definition at line 295 of file AutoParagraph.php.

Referenced by _checkNeedsP(), and handleElement().

295  {
296  return isset($this->htmlDefinition->info['p']->child->elements[$token->name]);
297  }
+ Here is the caller graph for this function:

◆ _pLookAhead()

HTMLPurifier_Injector_AutoParagraph::_pLookAhead ( )
private

Looks ahead in the token list and determines whether or not we need to insert a.

tag.

Definition at line 303 of file AutoParagraph.php.

References $ok, $result, _checkNeedsP(), HTMLPurifier_Injector\current(), and HTMLPurifier_Injector\forwardUntilEndToken().

Referenced by handleElement(), and handleText().

303  {
304  $this->current($i, $current);
305  if ($current instanceof HTMLPurifier_Token_Start) $nesting = 1;
306  else $nesting = 0;
307  $ok = false;
308  while ($this->forwardUntilEndToken($i, $current, $nesting)) {
309  $result = $this->_checkNeedsP($current);
310  if ($result !== null) {
311  $ok = $result;
312  break;
313  }
314  }
315  return $ok;
316  }
$result
Concrete start token class.
Definition: Start.php:6
_checkNeedsP($current)
Determines if a particular token requires an earlier inline token to get a paragraph.
forwardUntilEndToken(&$i, &$current, &$nesting)
Similar to _forward, but accepts a third parameter $nesting (which should be initialized at 0) and st...
Definition: Injector.php:170
current(&$i, &$current)
Initializes the iterator at the current position.
Definition: Injector.php:207
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _pStart()

HTMLPurifier_Injector_AutoParagraph::_pStart ( )
private

Definition at line 15 of file AutoParagraph.php.

Referenced by _splitText(), handleElement(), and handleText().

15  {
16  $par = new HTMLPurifier_Token_Start('p');
17  $par->armor['MakeWellFormed_TagClosedError'] = true;
18  return $par;
19  }
Concrete start token class.
Definition: Start.php:6
+ Here is the caller graph for this function:

◆ _splitText()

HTMLPurifier_Injector_AutoParagraph::_splitText (   $data,
$result 
)
private

Splits up a text in paragraph tokens and appends them to the result stream that will replace the original.

Parameters
$dataString text data that will be processed into paragraphs
$resultReference to array of tokens that the tags will be appended onto
$configInstance of HTMLPurifier_Config
$contextInstance of HTMLPurifier_Context

Definition at line 211 of file AutoParagraph.php.

References $data, $result, and _pStart().

Referenced by handleText().

211  {
212  $raw_paragraphs = explode("\n\n", $data);
213  $paragraphs = array(); // without empty paragraphs
214  $needs_start = false;
215  $needs_end = false;
216 
217  $c = count($raw_paragraphs);
218  if ($c == 1) {
219  // There were no double-newlines, abort quickly. In theory this
220  // should never happen.
222  return;
223  }
224  for ($i = 0; $i < $c; $i++) {
225  $par = $raw_paragraphs[$i];
226  if (trim($par) !== '') {
227  $paragraphs[] = $par;
228  } else {
229  if ($i == 0) {
230  // Double newline at the front
231  if (empty($result)) {
232  // The empty result indicates that the AutoParagraph
233  // injector did not add any start paragraph tokens.
234  // This means that we have been in a paragraph for
235  // a while, and the newline means we should start a new one.
236  $result[] = new HTMLPurifier_Token_End('p');
237  $result[] = new HTMLPurifier_Token_Text("\n\n");
238  // However, the start token should only be added if
239  // there is more processing to be done (i.e. there are
240  // real paragraphs in here). If there are none, the
241  // next start paragraph tag will be handled by the
242  // next call to the injector
243  $needs_start = true;
244  } else {
245  // We just started a new paragraph!
246  // Reinstate a double-newline for presentation's sake, since
247  // it was in the source code.
248  array_unshift($result, new HTMLPurifier_Token_Text("\n\n"));
249  }
250  } elseif ($i + 1 == $c) {
251  // Double newline at the end
252  // There should be a trailing </p> when we're finally done.
253  $needs_end = true;
254  }
255  }
256  }
257 
258  // Check if this was just a giant blob of whitespace. Move this earlier,
259  // perhaps?
260  if (empty($paragraphs)) {
261  return;
262  }
263 
264  // Add the start tag indicated by \n\n at the beginning of $data
265  if ($needs_start) {
266  $result[] = $this->_pStart();
267  }
268 
269  // Append the paragraphs onto the result
270  foreach ($paragraphs as $par) {
271  $result[] = new HTMLPurifier_Token_Text($par);
272  $result[] = new HTMLPurifier_Token_End('p');
273  $result[] = new HTMLPurifier_Token_Text("\n\n");
274  $result[] = $this->_pStart();
275  }
276 
277  // Remove trailing start token; Injector will handle this later if
278  // it was indeed needed. This prevents from needing to do a lookahead,
279  // at the cost of a lookbehind later.
280  array_pop($result);
281 
282  // If there is no need for an end tag, remove all of it and let
283  // MakeWellFormed close it later.
284  if (!$needs_end) {
285  array_pop($result); // removes \n\n
286  array_pop($result); // removes </p>
287  }
288 
289  }
Concrete end token class.
Definition: End.php:10
$result
while($lm_rec=$ilDB->fetchAssoc($lm_set)) $data
Concrete text token class.
Definition: Text.php:12
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handleElement()

HTMLPurifier_Injector_AutoParagraph::handleElement ( $token)

Definition at line 97 of file AutoParagraph.php.

References _isInline(), _pLookAhead(), _pStart(), HTMLPurifier_Injector\allowsElement(), and HTMLPurifier_Injector\backward().

97  {
98  // We don't have to check if we're already in a <p> tag for block
99  // tokens, because the tag would have been autoclosed by MakeWellFormed.
100  if ($this->allowsElement('p')) {
101  if (!empty($this->currentNesting)) {
102  if ($this->_isInline($token)) {
103  // State 1: <div>...<b>
104  // ---
105 
106  // Check if this token is adjacent to the parent token
107  // (seek backwards until token isn't whitespace)
108  $i = null;
109  $this->backward($i, $prev);
110 
111  if (!$prev instanceof HTMLPurifier_Token_Start) {
112  // Token wasn't adjacent
113 
114  if (
115  $prev instanceof HTMLPurifier_Token_Text &&
116  substr($prev->data, -2) === "\n\n"
117  ) {
118  // State 1.1.4: <div><p>PAR1</p>\n\n<b>
119  // ---
120 
121  // Quite frankly, this should be handled by splitText
122  $token = array($this->_pStart(), $token);
123  } else {
124  // State 1.1.1: <div><p>PAR1</p><b>
125  // ---
126 
127  // State 1.1.2: <div><br /><b>
128  // ---
129 
130  // State 1.1.3: <div>PAR<b>
131  // ---
132  }
133 
134  } else {
135  // State 1.2.1: <div><b>
136  // ---
137 
138  // Lookahead to see if <p> is needed.
139  if ($this->_pLookAhead()) {
140  // State 1.3.1: <div><b>PAR1\n\nPAR2
141  // ---
142  $token = array($this->_pStart(), $token);
143  } else {
144  // State 1.3.2: <div><b>PAR1</b></div>
145  // ---
146 
147  // State 1.3.3: <div><b>PAR1</b><div></div>\n\n</div>
148  // ---
149  }
150  }
151  } else {
152  // State 2.3: ...<div>
153  // -----
154  }
155  } else {
156  if ($this->_isInline($token)) {
157  // State 3.1: <b>
158  // ---
159  // This is where the {p} tag is inserted, not reflected in
160  // inputTokens yet, however.
161  $token = array($this->_pStart(), $token);
162  } else {
163  // State 3.2: <div>
164  // -----
165  }
166 
167  $i = null;
168  if ($this->backward($i, $prev)) {
169  if (
170  !$prev instanceof HTMLPurifier_Token_Text
171  ) {
172  // State 3.1.1: ...</p>{p}<b>
173  // ---
174 
175  // State 3.2.1: ...</p><div>
176  // -----
177 
178  if (!is_array($token)) $token = array($token);
179  array_unshift($token, new HTMLPurifier_Token_Text("\n\n"));
180  } else {
181  // State 3.1.2: ...</p>\n\n{p}<b>
182  // ---
183 
184  // State 3.2.2: ...</p>\n\n<div>
185  // -----
186 
187  // Note: PAR<ELEM> cannot occur because PAR would have been
188  // wrapped in <p> tags.
189  }
190  }
191  }
192  } else {
193  // State 2.2: <ul><li>
194  // ----
195 
196  // State 2.4: <p><b>
197  // ---
198  }
199  }
allowsElement($name)
Tests if the context node allows a certain element.
Definition: Injector.php:129
Concrete start token class.
Definition: Start.php:6
_isInline($token)
Returns true if passed token is inline (and, ergo, allowed in paragraph tags)
backward(&$i, &$current)
Iterator function, starts with the previous token and continues until you reach the beginning of inpu...
Definition: Injector.php:190
Concrete text token class.
Definition: Text.php:12
_pLookAhead()
Looks ahead in the token list and determines whether or not we need to insert a.
+ Here is the call graph for this function:

◆ handleText()

HTMLPurifier_Injector_AutoParagraph::handleText ( $token)

Definition at line 21 of file AutoParagraph.php.

References _pLookAhead(), _pStart(), _splitText(), HTMLPurifier_Injector\allowsElement(), and HTMLPurifier_Injector\forwardUntilEndToken().

21  {
22  $text = $token->data;
23  // Does the current parent allow <p> tags?
24  if ($this->allowsElement('p')) {
25  if (empty($this->currentNesting) || strpos($text, "\n\n") !== false) {
26  // Note that we have differing behavior when dealing with text
27  // in the anonymous root node, or a node inside the document.
28  // If the text as a double-newline, the treatment is the same;
29  // if it doesn't, see the next if-block if you're in the document.
30 
31  $i = $nesting = null;
32  if (!$this->forwardUntilEndToken($i, $current, $nesting) && $token->is_whitespace) {
33  // State 1.1: ... ^ (whitespace, then document end)
34  // ----
35  // This is a degenerate case
36  } else {
37  if (!$token->is_whitespace || $this->_isInline($current)) {
38  // State 1.2: PAR1
39  // ----
40 
41  // State 1.3: PAR1\n\nPAR2
42  // ------------
43 
44  // State 1.4: <div>PAR1\n\nPAR2 (see State 2)
45  // ------------
46  $token = array($this->_pStart());
47  $this->_splitText($text, $token);
48  } else {
49  // State 1.5: \n<hr />
50  // --
51  }
52  }
53  } else {
54  // State 2: <div>PAR1... (similar to 1.4)
55  // ----
56 
57  // We're in an element that allows paragraph tags, but we're not
58  // sure if we're going to need them.
59  if ($this->_pLookAhead()) {
60  // State 2.1: <div>PAR1<b>PAR1\n\nPAR2
61  // ----
62  // Note: This will always be the first child, since any
63  // previous inline element would have triggered this very
64  // same routine, and found the double newline. One possible
65  // exception would be a comment.
66  $token = array($this->_pStart(), $token);
67  } else {
68  // State 2.2.1: <div>PAR1<div>
69  // ----
70 
71  // State 2.2.2: <div>PAR1<b>PAR1</b></div>
72  // ----
73  }
74  }
75  // Is the current parent a <p> tag?
76  } elseif (
77  !empty($this->currentNesting) &&
78  $this->currentNesting[count($this->currentNesting)-1]->name == 'p'
79  ) {
80  // State 3.1: ...<p>PAR1
81  // ----
82 
83  // State 3.2: ...<p>PAR1\n\nPAR2
84  // ------------
85  $token = array();
86  $this->_splitText($text, $token);
87  // Abort!
88  } else {
89  // State 4.1: ...<b>PAR1
90  // ----
91 
92  // State 4.2: ...<b>PAR1\n\nPAR2
93  // ------------
94  }
95  }
allowsElement($name)
Tests if the context node allows a certain element.
Definition: Injector.php:129
_splitText($data, &$result)
Splits up a text in paragraph tokens and appends them to the result stream that will replace the orig...
forwardUntilEndToken(&$i, &$current, &$nesting)
Similar to _forward, but accepts a third parameter $nesting (which should be initialized at 0) and st...
Definition: Injector.php:170
_pLookAhead()
Looks ahead in the token list and determines whether or not we need to insert a.
+ Here is the call graph for this function:

Field Documentation

◆ $name

HTMLPurifier_Injector_AutoParagraph::$name = 'AutoParagraph'

Definition at line 12 of file AutoParagraph.php.

◆ $needed

HTMLPurifier_Injector_AutoParagraph::$needed = array('p')

Definition at line 13 of file AutoParagraph.php.


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