From 23986b65d683b5ec34822de198185b0571634767 Mon Sep 17 00:00:00 2001 From: Christian A. Weber Date: Fri, 18 Aug 2017 17:38:49 +0200 Subject: U(): params can override base args, correctly add fragment to end of url if params are present, add some tests --- it_html.class | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'it_html.class') diff --git a/it_html.class b/it_html.class index 1ca2acf..ea8a159 100644 --- a/it_html.class +++ b/it_html.class @@ -542,6 +542,7 @@ static function U(/* ... */) if (!($u = @parse_url($base))) list($u['path'], $u['query']) = explode("?", $base, 2); + parse_str($u['query'], $u['params']); $u['host'] = preg_match('/[^-_.0-9a-z]/i', $u['host']) && function_exists('idn_to_ascii') && ($idnahost = idn_to_ascii($GLOBALS['it_html']->p['charset'] == "iso-8859-1" ? utf8_encode($u['host']) : $u['host'])) ? $idnahost : $u['host']; # Punycode hostname to include into webpage $u['host'] = preg_replace_callback('/[^-_.0-9a-z\x80-\xff]/i', function($m) { return rawurlencode($m[0]); }, $u['host']); # Encode garbage chars in host @@ -554,20 +555,23 @@ static function U(/* ... */) $schemepart = $hostpart ? ($u['scheme'] ? $u['scheme'] . ":" : "") . "//$hostpart" : ($u['scheme'] == "mailto" ? $u['scheme'] . ":" : ""); - $path = $u['path'] . ($u['query'] ? "?" . strtr($u['query'], array("?" => rawurlencode("?"))) : "") . ($u['fragment'] ? "#" . $u['fragment'] : ""); - # hack: encode % if not followed by two hex digits - $parts = preg_split('/%([^%]{0,2})/', $path, -1, PREG_SPLIT_DELIM_CAPTURE); + $parts = preg_split('/%([^%]{0,2})/', $u['path'], -1, PREG_SPLIT_DELIM_CAPTURE); for ($i = 1; $i < count($parts); $i+=2) $parts[$i] = (preg_match('/[0-9a-f][0-9a-f]/i', $parts[$i]) ? "%" : "%25") . $parts[$i]; $path = join("", $parts); + # if params replace url parameters that are present in base, replace them but keep their order + foreach ($u['params'] as $key => $dummy) + if (isset($params[$key])) + $u['params'][$key] = $params[$key]; + $path = preg_replace_callback('|[^-a-z0-9_.+!*(),:?@&=/~$%#]|i', function($m) { return rawurlencode($m[0]); }, $path); $path = preg_replace('|^([a-z0-9_]+:)?//[^/]*$|', '$0/', $path); # Add slash if absolute url without a path, e.g. http://gna.ch - $queryparams = it_url::params($params); + $queryparams = it_url::params($u['params'] + $params); $separator = strpos($path, "?") === false ? "?" : "&"; - return $schemepart . $path . ($queryparams ? "$separator$queryparams" : ""); + return $schemepart . $path . ($queryparams ? "$separator$queryparams" : "") . ($u['fragment'] ? "#" . $u['fragment'] : "");; } -- cgit v1.2.3