From 7fc5234cbee56deb538510131cf2f87467e38e4c Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Tue, 22 May 2012 13:26:25 +1000 Subject: [PATCH] Scribunto parser support Add $indexOffset parameter to PPFrame::newChild(). This makes it possible to use newChild() for interpreting named parameters to invoke in Scribunto -- otherwise I would have had to duplicate its functionality, which would have been tricky given that I wanted to make a real frame with an expand() method. Setting $indexOffset allows newChild() to start counting numbered parameters from somewhere other than the first pipe character, leaving room for the Scribunto function name. Fixed PPCustomFrame_*::getArguments(), was missing for no apparent reason. I didn't end up using it in Scribunto, but there's no harm in adding it anyway. Change-Id: I0c761aab8a7f1ae74e8d151a1346febb5c466e18 --- includes/parser/Preprocessor.php | 6 +++++- includes/parser/Preprocessor_DOM.php | 10 +++++++++- includes/parser/Preprocessor_Hash.php | 11 ++++++++--- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/includes/parser/Preprocessor.php b/includes/parser/Preprocessor.php index 3d2f2f869a..19bcbf238c 100644 --- a/includes/parser/Preprocessor.php +++ b/includes/parser/Preprocessor.php @@ -78,15 +78,19 @@ interface PPFrame { const RECOVER_ORIG = 27; // = 1|2|8|16 no constant expression support in PHP yet + /** This constant exists when $indexOffset is supported in newChild() */ + const SUPPORTS_INDEX_OFFSET = 1; + /** * Create a child frame * * @param $args array * @param $title Title + * @param $indexOffset A number subtracted from the index attributes of the arguments * * @return PPFrame */ - function newChild( $args = false, $title = false ); + function newChild( $args = false, $title = false, $indexOffset = 0 ); /** * Expand a document tree node diff --git a/includes/parser/Preprocessor_DOM.php b/includes/parser/Preprocessor_DOM.php index 5bb951b4f1..f991df2a2f 100644 --- a/includes/parser/Preprocessor_DOM.php +++ b/includes/parser/Preprocessor_DOM.php @@ -926,7 +926,7 @@ class PPFrame_DOM implements PPFrame { * * @return PPTemplateFrame_DOM */ - function newChild( $args = false, $title = false ) { + function newChild( $args = false, $title = false, $indexOffset = 0 ) { $namedArgs = array(); $numberedArgs = array(); if ( $title === false ) { @@ -938,6 +938,9 @@ class PPFrame_DOM implements PPFrame { $args = $args->node; } foreach ( $args as $arg ) { + if ( $arg instanceof PPNode ) { + $arg = $arg->node; + } if ( !$xpath ) { $xpath = new DOMXPath( $arg->ownerDocument ); } @@ -947,6 +950,7 @@ class PPFrame_DOM implements PPFrame { if ( $nameNodes->item( 0 )->hasAttributes() ) { // Numbered parameter $index = $nameNodes->item( 0 )->attributes->getNamedItem( 'index' )->textContent; + $index = $index - $indexOffset; $numberedArgs[$index] = $value->item( 0 ); unset( $namedArgs[$index] ); } else { @@ -1549,6 +1553,10 @@ class PPCustomFrame_DOM extends PPFrame_DOM { } return $this->args[$index]; } + + function getArguments() { + return $this->args; + } } /** diff --git a/includes/parser/Preprocessor_Hash.php b/includes/parser/Preprocessor_Hash.php index 32ba7070ec..f455a1db7e 100644 --- a/includes/parser/Preprocessor_Hash.php +++ b/includes/parser/Preprocessor_Hash.php @@ -888,7 +888,7 @@ class PPFrame_Hash implements PPFrame { * * @return PPTemplateFrame_Hash */ - function newChild( $args = false, $title = false ) { + function newChild( $args = false, $title = false, $indexOffset = 0 ) { $namedArgs = array(); $numberedArgs = array(); if ( $title === false ) { @@ -904,8 +904,9 @@ class PPFrame_Hash implements PPFrame { $bits = $arg->splitArg(); if ( $bits['index'] !== '' ) { // Numbered parameter - $numberedArgs[$bits['index']] = $bits['value']; - unset( $namedArgs[$bits['index']] ); + $index = $bits['index'] - $indexOffset; + $numberedArgs[$index] = $bits['value']; + unset( $namedArgs[$index] ); } else { // Named parameter $name = trim( $this->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) ); @@ -1496,6 +1497,10 @@ class PPCustomFrame_Hash extends PPFrame_Hash { } return $this->args[$index]; } + + function getArguments() { + return $this->args; + } } /** -- 2.20.1