- md5
- 289538afdb0742ed8b061ad4340ddb2e
- sha1
- fab681860264ef01543820237e402afcda88507a
- sha256
- 2868a21e81d0cc1d113ca76a153d5ceb1977d2d5531a89f15fb1c2c341a987f6
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
<?php
namespace enshrined\svgSanitize;
class Helper
{
const XLINK_NAMESPACE_URI = 'http://www.w3.org/1999/xlink';
/**
* Resolves the `href`/`xlink:href` value of an element.
*
* The lookup is case insensitive on purpose: `Sanitizer::cleanHrefAttributes()`
* normalizes names such as `HrEf`/`xlink:HrEf` back to `href`/`xlink:href`,
* so anything consuming this helper has to see those attributes as well -
* otherwise a mixed-case name hides the reference from e.g. the `<use>`
* reference graph, which is built before that normalization happens.
*
* @param \DOMElement $element
* @return string|null
*/
public static function getElementHref(\DOMElement $element)
{
if ($element->hasAttribute('href')) {
return $element->getAttribute('href');
}
if ($element->hasAttributeNS(self::XLINK_NAMESPACE_URI, 'href')) {
return $element->getAttributeNS(self::XLINK_NAMESPACE_URI, 'href');
}
foreach ($element->attributes as $attribute) {
if (!$attribute instanceof \DOMAttr
|| strtolower($attribute->localName) !== 'href'
) {
continue;
}
$prefix = strtolower((string)$attribute->prefix);
if ($prefix === '' || $prefix === 'xlink') {
return $attribute->value;
}
}
return null;
}
/**
* @param string $href
* @return string|null
*/
public static function extractIdReferenceFromHref($href)
{
if (!is_string($href)) {
return null;
}
// Read the href the way the URL parser does: it trims spaces and control
// characters from both ends and drops tabs and line breaks anywhere, so
// `#a ` and `# a` both reach the element with id `a`.
$href = str_replace(array("\t", "\n", "\r"), '', trim($href, "\x00..\x20"));
if (strpos($href, '#') !== 0) {
return null;
}
// A fragment is percent-decoded before it is matched against ids, so
// `#%61` addresses the element with id `a`.
return rawurldecode(substr($href, 1));
}
/**
* @param \DOMElement $needle
* @param \DOMElement $haystack
* @return bool
*/
public static function isElementContainedIn(\DOMElement $needle, \DOMElement $haystack)
{
if ($needle === $haystack) {
return true;
}
foreach ($haystack->childNodes as $childNode) {
if (!$childNode instanceof \DOMElement) {
continue;
}
if (self::isElementContainedIn($needle, $childNode)) {
return true;
}
}
return false;
}
}
The same content is in 4 releases, Joomla 5.4.11 to 6.3.0-alpha1. See them all, or every version of this file.