196 $forInclusion =
$flags & Parser::PTD_FOR_INCLUSION;
198 $xmlishElements = $this->parser->getStripList();
199 $xmlishAllowMissingEndTag = [
'includeonly',
'noinclude',
'onlyinclude' ];
200 $enableOnlyinclude =
false;
201 if ( $forInclusion ) {
202 $ignoredTags = [
'includeonly',
'/includeonly' ];
203 $ignoredElements = [
'noinclude' ];
204 $xmlishElements[] =
'noinclude';
205 if ( strpos( $text,
'<onlyinclude>' ) !==
false
206 && strpos( $text,
'</onlyinclude>' ) !==
false
208 $enableOnlyinclude =
true;
211 $ignoredTags = [
'noinclude',
'/noinclude',
'onlyinclude',
'/onlyinclude' ];
212 $ignoredElements = [
'includeonly' ];
213 $xmlishElements[] =
'includeonly';
215 $xmlishRegex = implode(
'|', array_merge( $xmlishElements, $ignoredTags ) );
218 $elementsRegex =
"~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
222 $searchBase =
"[{<\n"; # }
224 $revText = strrev( $text );
225 $lengthText = strlen( $text );
230 $accum =& $stack->getAccum();
242 $noMoreClosingTag = [];
244 $findOnlyinclude = $enableOnlyinclude;
246 $fakeLineStart =
true;
251 if ( $findOnlyinclude ) {
253 $startPos = strpos( $text,
'<onlyinclude>', $i );
254 if ( $startPos ===
false ) {
256 $accum .=
'<ignore>' . htmlspecialchars( substr( $text, $i ) ) .
'</ignore>';
259 $tagEndPos = $startPos + strlen(
'<onlyinclude>' );
260 $accum .=
'<ignore>' . htmlspecialchars( substr( $text, $i, $tagEndPos - $i ) ) .
'</ignore>';
262 $findOnlyinclude =
false;
265 if ( $fakeLineStart ) {
266 $found =
'line-start';
269 # Find next opening brace, closing brace or pipe
270 $search = $searchBase;
271 if ( $stack->top ===
false ) {
272 $currentClosing =
'';
274 $currentClosing = $stack->top->close;
275 $search .= $currentClosing;
285 # Output literal section, advance input counter
286 $literalLength = strcspn( $text, $search, $i );
287 if ( $literalLength > 0 ) {
288 $accum .= htmlspecialchars( substr( $text, $i, $literalLength ) );
289 $i += $literalLength;
291 if ( $i >= $lengthText ) {
292 if ( $currentClosing ==
"\n" ) {
301 $curChar = $text[$i];
302 if ( $curChar ==
'|' ) {
304 } elseif ( $curChar ==
'=' ) {
306 } elseif ( $curChar ==
'<' ) {
308 } elseif ( $curChar ==
"\n" ) {
312 $found =
'line-start';
314 } elseif ( $curChar == $currentClosing ) {
316 } elseif ( isset( $this->rules[$curChar] ) ) {
318 $rule = $this->rules[$curChar];
320 # Some versions of PHP have a strcspn which stops on null characters
321 # Ignore and continue
328 if ( $found ==
'angle' ) {
331 if ( $enableOnlyinclude
332 && substr( $text, $i, strlen(
'</onlyinclude>' ) ) ==
'</onlyinclude>'
334 $findOnlyinclude =
true;
339 if ( !preg_match( $elementsRegex, $text,
$matches, 0, $i + 1 ) ) {
354 $endPos = strpos( $text,
'-->', $i + 4 );
355 if ( $endPos ===
false ) {
357 $inner = substr( $text, $i );
358 $accum .=
'<comment>' . htmlspecialchars( $inner ) .
'</comment>';
362 $wsStart = $i ? ( $i - strspn( $revText,
" \t", $lengthText - $i ) ) : 0;
366 $wsEnd = $endPos + 2 + strspn( $text,
" \t", $endPos + 3 );
370 $comments = [ [ $wsStart, $wsEnd ] ];
371 while ( substr( $text, $wsEnd + 1, 4 ) ==
'<!--' ) {
372 $c = strpos( $text,
'-->', $wsEnd + 4 );
373 if ( $c ===
false ) {
376 $c = $c + 2 + strspn( $text,
" \t", $c + 3 );
377 $comments[] = [ $wsEnd + 1, $c ];
385 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) ==
"\n"
386 && substr( $text, $wsEnd + 1, 1 ) ==
"\n"
390 $wsLength = $i - $wsStart;
392 && strspn( $accum,
" \t", -$wsLength ) === $wsLength
394 $accum = substr( $accum, 0, -$wsLength );
398 foreach ( $comments as $j => $com ) {
400 $endPos = $com[1] + 1;
401 if ( $j == ( count( $comments ) - 1 ) ) {
404 $inner = substr( $text, $startPos, $endPos - $startPos );
405 $accum .=
'<comment>' . htmlspecialchars( $inner ) .
'</comment>';
409 $fakeLineStart =
true;
417 $part = $stack->top->getCurrentPart();
418 if ( !( isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 ) ) {
419 $part->visualEnd = $wsStart;
422 $part->commentEnd = $endPos;
425 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
426 $accum .=
'<comment>' . htmlspecialchars( $inner ) .
'</comment>';
431 $lowerName = strtolower( $name );
432 $attrStart = $i + strlen( $name ) + 1;
435 $tagEndPos = $noMoreGT ?
false : strpos( $text,
'>', $attrStart );
436 if ( $tagEndPos ===
false ) {
446 if ( in_array( $lowerName, $ignoredTags ) ) {
448 . htmlspecialchars( substr( $text, $i, $tagEndPos - $i + 1 ) )
455 if ( $text[$tagEndPos - 1] ==
'/' ) {
456 $attrEnd = $tagEndPos - 1;
461 $attrEnd = $tagEndPos;
464 !isset( $noMoreClosingTag[$name] ) &&
465 preg_match(
"/<\/" . preg_quote( $name,
'/' ) .
"\s*>/i",
466 $text,
$matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
468 $inner = substr( $text, $tagEndPos + 1,
$matches[0][1] - $tagEndPos - 1 );
470 $close =
'<close>' . htmlspecialchars(
$matches[0][0] ) .
'</close>';
473 if ( in_array( $name, $xmlishAllowMissingEndTag ) ) {
475 $inner = substr( $text, $tagEndPos + 1 );
481 $accum .= htmlspecialchars( substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
483 $noMoreClosingTag[
$name] =
true;
489 if ( in_array( $lowerName, $ignoredElements ) ) {
490 $accum .=
'<ignore>' . htmlspecialchars( substr( $text, $tagStartPos, $i - $tagStartPos ) )
496 if ( $attrEnd <= $attrStart ) {
499 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
501 $accum .=
'<name>' . htmlspecialchars( $name ) .
'</name>' .
504 '<attr>' . htmlspecialchars( $attr ) .
'</attr>';
505 if ( $inner !==
null ) {
506 $accum .=
'<inner>' . htmlspecialchars( $inner ) .
'</inner>';
508 $accum .= $close .
'</ext>';
509 } elseif ( $found ==
'line-start' ) {
512 if ( $fakeLineStart ) {
513 $fakeLineStart =
false;
519 $count = strspn( $text,
'=', $i, 6 );
520 if (
$count == 1 && $findEquals ) {
533 $stack->push( $piece );
534 $accum =& $stack->getAccum();
535 $flags = $stack->getFlags();
539 } elseif ( $found ==
'line-end' ) {
540 $piece = $stack->top;
542 assert( $piece->open ===
"\n" );
543 $part = $piece->getCurrentPart();
547 $wsLength = strspn( $revText,
" \t", $lengthText - $i );
548 $searchStart = $i - $wsLength;
549 if ( isset( $part->commentEnd ) && $searchStart - 1 == $part->commentEnd ) {
552 $searchStart = $part->visualEnd;
553 $searchStart -= strspn( $revText,
" \t", $lengthText - $searchStart );
556 $equalsLength = strspn( $revText,
'=', $lengthText - $searchStart );
557 if ( $equalsLength > 0 ) {
558 if ( $searchStart - $equalsLength == $piece->startPos ) {
573 $element =
"<h level=\"$count\" i=\"$headingIndex\">$accum</h>";
585 $accum =& $stack->getAccum();
586 $flags = $stack->getFlags();
596 } elseif ( $found ==
'open' ) {
597 # count opening brace characters
598 $count = strspn( $text, $curChar, $i );
600 # we need to add to stack only if opening brace count is enough for one of the rules
601 if (
$count >= $rule[
'min'] ) {
602 # Add it to the stack
605 'close' => $rule[
'end'],
607 'lineStart' => ( $i > 0 && $text[$i - 1] ==
"\n" ),
610 $stack->push( $piece );
611 $accum =& $stack->getAccum();
612 $flags = $stack->getFlags();
615 # Add literal brace(s)
616 $accum .= htmlspecialchars( str_repeat( $curChar,
$count ) );
619 } elseif ( $found ==
'close' ) {
620 $piece = $stack->top;
621 # lets check if there are enough characters for closing brace
622 $maxCount = $piece->count;
623 $count = strspn( $text, $curChar, $i, $maxCount );
625 # check for maximum matching characters (if there are 5 closing
626 # characters, we will probably need only 3 - depending on the rules)
627 $rule = $this->rules[$piece->open];
628 if (
$count > $rule[
'max'] ) {
629 # The specified maximum exists in the callback array, unless the caller
631 $matchingCount = $rule[
'max'];
633 # Count is less than the maximum
634 # Skip any gaps in the callback array to find the true largest match
635 # Need to use array_key_exists not isset because the callback can be null
637 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule[
'names'] ) ) {
642 if ( $matchingCount <= 0 ) {
643 # No matching element found in callback array
644 # Output a literal closing brace and continue
645 $accum .= htmlspecialchars( str_repeat( $curChar,
$count ) );
649 $name = $rule[
'names'][$matchingCount];
650 if ( $name ===
null ) {
652 $element = $piece->breakSyntax( $matchingCount ) . str_repeat( $rule[
'end'], $matchingCount );
655 # Note: $parts is already XML, does not need to be encoded further
656 $parts = $piece->parts;
660 # The invocation is at the start of the line if lineStart is set in
661 # the stack, and all opening brackets are used up.
662 if ( $maxCount == $matchingCount && !empty( $piece->lineStart ) ) {
663 $attr =
' lineStart="1"';
668 $element =
"<$name$attr>";
669 $element .=
"<title>$title</title>";
671 foreach ( $parts as $part ) {
672 if ( isset( $part->eqpos ) ) {
673 $argName = substr( $part->out, 0, $part->eqpos );
674 $argValue = substr( $part->out, $part->eqpos + 1 );
675 $element .=
"<part><name>$argName</name>=<value>$argValue</value></part>";
677 $element .=
"<part><name index=\"$argIndex\" /><value>{$part->out}</value></part>";
681 $element .=
"</$name>";
684 # Advance input pointer
685 $i += $matchingCount;
689 $accum =& $stack->getAccum();
691 # Re-add the old stack element if it still has unmatched opening characters remaining
692 if ( $matchingCount < $piece->count ) {
693 $piece->parts = [
new PPDPart ];
694 $piece->count -= $matchingCount;
695 # do we still qualify for any callback with remaining count?
696 $min = $this->rules[$piece->open][
'min'];
697 if ( $piece->count >= $min ) {
698 $stack->push( $piece );
699 $accum =& $stack->getAccum();
701 $accum .= str_repeat( $piece->open, $piece->count );
704 $flags = $stack->getFlags();
707 # Add XML element to the enclosing accumulator
709 } elseif ( $found ==
'pipe' ) {
712 $accum =& $stack->getAccum();
714 } elseif ( $found ==
'equals' ) {
716 $stack->getCurrentPart()->eqpos = strlen( $accum );
722 # Output any remaining unclosed brackets
723 foreach ( $stack->stack as $piece ) {
724 $stack->rootAccum .= $piece->breakSyntax();
726 $stack->rootAccum .=
'</root>';
727 $xml = $stack->rootAccum;