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