Wiki source for ActionParsQuoting


Show raw source

=====Quoting of Action parameters=====

//Currently (version 1.1.6.0 and earlier), Wikka recognizes only action parameters if they are enclosed in single quotes. That is a needless limitation, and unintuitive for those who are used to be able to single-quote attributes in HTML.//

====Double or Single Quotes====
This page shows a small change to the ##Action()## method in ##wikka.php## than enables it to recognize parameters enclosed in either double or single quotes (as long as the same type quotation character is used at start and end of the value).

===Making the change===

Find the ##Action()## method in ##wikka.php## (line 759 in version 1.1.6.0). A few lines into the method, find this section of code:
%%(php;772) if ($action) {
// match all attributes (key and value)
preg_match_all("/([A-Za-z0-9]*)=\"(.*)\"/U", $vars_temp, $matches);

// prepare an array for extract() to work with (in $this->IncludeBuffered())
if (is_array($matches)) {
for ($a = 0; $a < count($matches[0]); $a++) {
$vars[$matches[1][$a]] = $matches[2][$a];
}
}
$vars["wikka_vars"] = trim($vars_temp); // <<< add the buffered parameter-string to the array
} else {
%%

Replace it with:
%%(php;772) if ($action) {
// match all attributes (key and value)
preg_match_all('/([A-Za-z0-9]*)=("|\')(.*)\\2/U', $vars_temp, $matches);

// prepare an array for extract() to work with (in $this->IncludeBuffered())
if (is_array($matches)) {
for ($a = 0; $a < count($matches[0]); $a++) {
$vars[$matches[1][$a]] = $matches[3][$a];
}
}
$vars['wikka_vars'] = trim($vars_temp); // <<< add the buffered parameter-string to the array
} else {
%%

===How it works===

In the original regular expression on line 774 %%(php)"/([A-Za-z0-9]*)=\"(.*)\"/U"%% the bit %%(php)=\"(.*)\"%% matches an attribute value as follows:
~- starts with **##=##**
~- immediately followed by a double quote **##\"##**
~- followed by an arbitrary number of characters **##(.*)##**;--- the brackets allow us to pull out the value (the second bracketed expression in the whole regular expression, so we get it with a **##$matches[2]##** while the parameter //name// is in ##$matches[1]## (line 779) - the first bracketed expression)
~- followed by the closing double quote **##\"##**

In the new code we have this regular expression %%(php)'/([A-Za-z0-9]*)=("|\')(.*)\\2/U'%% where the bit %%(php)=("|\')(.*)\\2%% matches an attribute value as follows:
~- starts with **##=##**
~- immediately followed by either a double quote or a single one **##("|\')##**--- (note that the single quote must be escaped now because we have enclosed the whole regular expression in single quotes in stead of double ones); this subexpression is bracketed so we can refer back to it
~- followed by an arbitrary number of characters **##(.*)##**;--- as above, the brackets allow us to pull out the parameter value, but it is now the //third// bracketed expression, so we get at it with **##$matches[3]##** now (line 779)
~- followed by **the same quote character it started with** **##\\2##**:--- this is called a backreference - ##\2## matches whatever the second bracketed expression before matched; this ensures we match either two double quotes or two single quotes.

===Advantages===

~1) Freedom to use either single or double quotes around a parameter value; it's largely a matter of habit, but in some cases single quotes may actually be more readable than double quotes.
~1) You can now **embed** quotes in a parameter value, simply by enclosing the whole parameter in the "opposite" type of quote characters. This is useful for passing actual //text// to an action, but would also enable something like the (extended) TableAction to handle embedded (double-double quoted) HTML in table cells.

----
CategoryDevelopmentCore
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki