| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\Core; |
| 5 |
|
| 6 |
use Wikimedia\Assert\Assert; |
| 7 |
use Wikimedia\Parsoid\Tokens\SourceRange; |
| 8 |
use Wikimedia\Parsoid\Utils\PHPUtils; |
| 9 |
|
| 10 |
/** |
| 11 |
* Represents a DOM source range. That is, for a given DOM tree, gives |
| 12 |
* the source offset range in the original wikitext for this DOM tree, |
| 13 |
* as well as the opening and closing tag widths if appropriate. |
| 14 |
*/ |
| 15 |
class DomSourceRange extends SourceRange { |
| 16 |
/** |
| 17 |
* Opening tag width. |
| 18 |
* @var ?int |
| 19 |
*/ |
| 20 |
public $openWidth; |
| 21 |
|
| 22 |
/** |
| 23 |
* Closing tag width. |
| 24 |
* @var ?int |
| 25 |
*/ |
| 26 |
public $closeWidth; |
| 27 |
|
| 28 |
/** |
| 29 |
* Width of trimmed whitespace between opening tag & first child. |
| 30 |
* Defaults to zero since for most nodes, there is no ws trimming. |
| 31 |
* -1 indicates that this information is invalid and should not be used. |
| 32 |
* @var int |
| 33 |
*/ |
| 34 |
public $leadingWS = 0; |
| 35 |
|
| 36 |
/** |
| 37 |
* Width of trimmed whitespace between last child & closing tag. |
| 38 |
* Defaults to zero since for most nodes, there is no ws trimming. |
| 39 |
* -1 indicates that this information is invalid and should not be used. |
| 40 |
* @var int |
| 41 |
*/ |
| 42 |
public $trailingWS = 0; |
| 43 |
|
| 44 |
/** |
| 45 |
* Create a new DOM source offset range (DSR). |
| 46 |
* @param ?int $start The starting index (UTF-8 byte count, inclusive) |
| 47 |
* @param ?int $end The ending index (UTF-8 byte count, exclusive) |
| 48 |
* @param ?int $openWidth The width of the open container tag |
| 49 |
* @param ?int $closeWidth The width of the close container tag |
| 50 |
* @param int $leadingWS The width of WS chars between opening tag & first child |
| 51 |
* @param int $trailingWS The width of WS chars between last child & closing tag |
| 52 |
*/ |
| 53 |
public function __construct( |
| 54 |
?int $start, ?int $end, ?int $openWidth, ?int $closeWidth, |
| 55 |
int $leadingWS = 0, |
| 56 |
int $trailingWS = 0 |
| 57 |
) { |
| 58 |
parent::__construct( $start, $end ); |
| 59 |
$this->openWidth = $openWidth; |
| 60 |
$this->closeWidth = $closeWidth; |
| 61 |
$this->leadingWS = $leadingWS; |
| 62 |
$this->trailingWS = $trailingWS; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Return the substring of the given string corresponding to the |
| 67 |
* inner portion of this range (that is, not including the opening |
| 68 |
* and closing tag widths). |
| 69 |
* @param string $str The source text string |
| 70 |
* @return string |
| 71 |
*/ |
| 72 |
public function innerSubstr( string $str ): string { |
| 73 |
return PHPUtils::safeSubstr( $str, $this->innerStart(), $this->innerLength() ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Return the "inner start", that is, the start offset plus the open width. |
| 78 |
* @return int |
| 79 |
*/ |
| 80 |
public function innerStart(): int { |
| 81 |
return $this->start + ( $this->openWidth ?? 0 ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Return the "inner end", that is, the end offset minus the close width. |
| 86 |
* @return int |
| 87 |
*/ |
| 88 |
public function innerEnd(): int { |
| 89 |
return $this->end - ( $this->closeWidth ?? 0 ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Return the length of this source range, excluding the open and close |
| 94 |
* tag widths. |
| 95 |
* @return int |
| 96 |
*/ |
| 97 |
public function innerLength(): int { |
| 98 |
return $this->innerEnd() - $this->innerStart(); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Return the substring of the given string corresponding to the |
| 103 |
* open portion of this range. |
| 104 |
* @param string $str The source text string |
| 105 |
* @return string |
| 106 |
*/ |
| 107 |
public function openSubstr( string $str ): string { |
| 108 |
return PHPUtils::safeSubstr( $str, $this->start, $this->openWidth ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Return the substring of the given string corresponding to the |
| 113 |
* close portion of this range. |
| 114 |
* @param string $str The source text string |
| 115 |
* @return string |
| 116 |
*/ |
| 117 |
public function closeSubstr( string $str ): string { |
| 118 |
return PHPUtils::safeSubstr( $str, $this->innerEnd(), $this->closeWidth ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Return the source range corresponding to the open portion of this range. |
| 123 |
* @return SourceRange |
| 124 |
*/ |
| 125 |
public function openRange(): SourceRange { |
| 126 |
return new SourceRange( $this->start, $this->innerStart() ); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Return the source range corresponding to the close portion of this range. |
| 131 |
* @return SourceRange |
| 132 |
*/ |
| 133 |
public function closeRange(): SourceRange { |
| 134 |
return new SourceRange( $this->innerEnd(), $this->end ); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Return the source range corresponding to the inner portion of this range. |
| 139 |
* @return SourceRange |
| 140 |
*/ |
| 141 |
public function innerRange(): SourceRange { |
| 142 |
return new SourceRange( $this->innerStart(), $this->innerEnd() ); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Strip the tag open and close from the beginning and end of the |
| 147 |
* provided string. This is similar to `DomSourceRange::innerSubstr()` |
| 148 |
* but we assume that the string before `$this->start` and after |
| 149 |
* `$this->end` has already been removed. (That is, that the input |
| 150 |
* is `$this->substr( $originalWikitextSource )`.) |
| 151 |
* |
| 152 |
* @param string $src The source text string from `$this->start` |
| 153 |
* (inclusive) to `$this->end` (exclusive). |
| 154 |
* @return string |
| 155 |
*/ |
| 156 |
public function stripTags( string $src ): string { |
| 157 |
Assert::invariant( |
| 158 |
strlen( $src ) === $this->length(), |
| 159 |
"Input string not the expected length" |
| 160 |
); |
| 161 |
return PHPUtils::safeSubstr( |
| 162 |
$src, |
| 163 |
$this->openWidth, |
| 164 |
-$this->closeWidth |
| 165 |
); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Return a new DOM source range shifted by $amount. |
| 170 |
* @param int $amount The amount to shift by |
| 171 |
* @return DomSourceRange |
| 172 |
*/ |
| 173 |
public function offset( int $amount ): DomSourceRange { |
| 174 |
return new DomSourceRange( |
| 175 |
$this->start + $amount, |
| 176 |
$this->end + $amount, |
| 177 |
$this->openWidth, |
| 178 |
$this->closeWidth, |
| 179 |
$this->leadingWS, |
| 180 |
$this->trailingWS |
| 181 |
); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* @return bool True if the tag widths are valid. |
| 186 |
*/ |
| 187 |
public function hasValidTagWidths(): bool { |
| 188 |
return $this->openWidth !== null && $this->closeWidth !== null && |
| 189 |
$this->openWidth >= 0 && $this->closeWidth >= 0; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Determine if this DSR records that whitespace was trimmed from |
| 194 |
* this node. Note that this doesn't mean that the amount trimmed |
| 195 |
* is known; use ::hasValidLeadingWS() or ::hasValidTrimmedWS() |
| 196 |
* to determine that. |
| 197 |
* @return bool True if either leadingWS or trailingWS is non-zero. |
| 198 |
*/ |
| 199 |
public function hasTrimmedWS(): bool { |
| 200 |
return $this->leadingWS !== 0 || $this->trailingWS !== 0; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* @note In most cases you should check to see if this node |
| 205 |
* ::hasTrimmedWS() *and* whether the amount is valid. |
| 206 |
* @return bool if the amount of leading whitespace is known. |
| 207 |
*/ |
| 208 |
public function hasValidLeadingWS(): bool { |
| 209 |
return $this->leadingWS !== -1; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* @note In most cases you should check to see if this node |
| 214 |
* ::hasTrimmedWS() *and* whether the amount is valid. |
| 215 |
* @return bool if the amount of trailing whitespace is known. |
| 216 |
*/ |
| 217 |
public function hasValidTrailingWS(): bool { |
| 218 |
return $this->trailingWS !== -1; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Convert a TSR to a DSR with zero-width container open/close tags. |
| 223 |
* @param SourceRange $tsr |
| 224 |
* @return DomSourceRange |
| 225 |
*/ |
| 226 |
public static function fromTsr( SourceRange $tsr ): DomSourceRange { |
| 227 |
return new DomSourceRange( $tsr->start, $tsr->end, null, null ); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Create a new DomSourceRange from an array of integers/null (such as |
| 232 |
* created during JSON serialization). |
| 233 |
* @param array<int|null> $dsr |
| 234 |
* @return DomSourceRange |
| 235 |
*/ |
| 236 |
public static function newFromJsonArray( array $dsr ): DomSourceRange { |
| 237 |
$n = count( $dsr ); |
| 238 |
Assert::invariant( $n === 2 || $n === 4 || $n === 6, 'Not enough elements in DSR array' ); |
| 239 |
return new DomSourceRange( |
| 240 |
$dsr[0], $dsr[1], $dsr[2] ?? null, $dsr[3] ?? null, $dsr[4] ?? 0, $dsr[5] ?? 0 |
| 241 |
); |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* @inheritDoc |
| 246 |
*/ |
| 247 |
public function toJsonArray(): array { |
| 248 |
$a = [ $this->start, $this->end, $this->openWidth, $this->closeWidth ]; |
| 249 |
if ( $this->leadingWS !== 0 || $this->trailingWS !== 0 ) { |
| 250 |
$a[] = $this->leadingWS; |
| 251 |
$a[] = $this->trailingWS; |
| 252 |
} |
| 253 |
return $a; |
| 254 |
} |
| 255 |
} |