| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\RemexHtml\TreeBuilder; |
| 4 |
|
| 5 |
use Wikimedia\RemexHtml\Tokenizer\Attributes; |
| 6 |
|
| 7 |
/** |
| 8 |
* The "in table text" insertion mode |
| 9 |
*/ |
| 10 |
class InTableText extends InsertionMode { |
| 11 |
/** @inheritDoc */ |
| 12 |
public function characters( $text, $start, $length, $sourceStart, $sourceLength ) { |
| 13 |
$handleNonNull = function ( $text, $start, $length, $sourceStart, $sourceLength ) { |
| 14 |
$this->builder->pendingTableCharacters[] = [ |
| 15 |
$text, $start, $length, $sourceStart, $sourceLength ]; |
| 16 |
}; |
| 17 |
if ( !$this->builder->ignoreNulls ) { |
| 18 |
$this->stripNulls( $handleNonNull, $text, $start, $length, |
| 19 |
$sourceStart, $sourceLength ); |
| 20 |
} else { |
| 21 |
$handleNonNull( $text, $start, $length, $sourceStart, $sourceLength ); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
/** @inheritDoc */ |
| 26 |
public function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ) { |
| 27 |
$this->processPendingCharacters(); |
| 28 |
$this->dispatcher->restoreMode() |
| 29 |
->doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ); |
| 30 |
} |
| 31 |
|
| 32 |
/** @inheritDoc */ |
| 33 |
public function comment( $text, $sourceStart, $sourceLength ) { |
| 34 |
$this->processPendingCharacters(); |
| 35 |
$this->dispatcher->restoreMode() |
| 36 |
->comment( $text, $sourceStart, $sourceLength ); |
| 37 |
} |
| 38 |
|
| 39 |
/** @inheritDoc */ |
| 40 |
public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) { |
| 41 |
$this->processPendingCharacters(); |
| 42 |
$this->dispatcher->restoreMode() |
| 43 |
->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength ); |
| 44 |
} |
| 45 |
|
| 46 |
/** @inheritDoc */ |
| 47 |
public function endTag( $name, $sourceStart, $sourceLength ) { |
| 48 |
$this->processPendingCharacters(); |
| 49 |
$this->dispatcher->restoreMode() |
| 50 |
->endTag( $name, $sourceStart, $sourceLength ); |
| 51 |
} |
| 52 |
|
| 53 |
/** @inheritDoc */ |
| 54 |
public function endDocument( $pos ) { |
| 55 |
$this->processPendingCharacters(); |
| 56 |
$this->dispatcher->restoreMode() |
| 57 |
->endDocument( $pos ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Flush the pending table text and restore the mode |
| 62 |
*/ |
| 63 |
public function flush() { |
| 64 |
$this->processPendingCharacters(); |
| 65 |
$this->dispatcher->restoreMode(); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Common code for the "anything else" case. Process the pending table |
| 70 |
* character tokens. |
| 71 |
*/ |
| 72 |
protected function processPendingCharacters() { |
| 73 |
$builder = $this->builder; |
| 74 |
$allSpace = true; |
| 75 |
foreach ( $builder->pendingTableCharacters as $token ) { |
| 76 |
[ $text, $start, $length, $sourceStart, $sourceLength ] = $token; |
| 77 |
if ( strspn( $text, "\t\n\f\r ", $start, $length ) !== $length ) { |
| 78 |
$allSpace = false; |
| 79 |
} |
| 80 |
} |
| 81 |
if ( $allSpace ) { |
| 82 |
foreach ( $builder->pendingTableCharacters as $token ) { |
| 83 |
[ $text, $start, $length, $sourceStart, $sourceLength ] = $token; |
| 84 |
$builder->insertCharacters( $text, $start, $length, $sourceStart, $sourceLength ); |
| 85 |
} |
| 86 |
} else { |
| 87 |
$builder->fosterParenting = true; |
| 88 |
foreach ( $builder->pendingTableCharacters as $token ) { |
| 89 |
[ $text, $start, $length, $sourceStart, $sourceLength ] = $token; |
| 90 |
$builder->error( 'invalid characters in table text, fostering', $sourceStart ); |
| 91 |
$this->dispatcher->inBody->characters( $text, $start, $length, |
| 92 |
$sourceStart, $sourceLength ); |
| 93 |
} |
| 94 |
$builder->fosterParenting = false; |
| 95 |
} |
| 96 |
} |
| 97 |
} |