Extract <a> element for <img>

@param \DOMElement $html
@param Bag         $bag
@param null|string $domain
334296
protected static function extractA(\DOMElement $img, $domain = null)
    {
        $parent = $img->parentNode;
        while ($parent && isset($parent->tagName)) {
            if ($parent->tagName === 'a') {
                if ($parent->hasAttribute('href')) {
                    return self::_resolveFullPath($parent->getAttribute('href'), $domain);
                }
            }

            $parent = $parent->parentNode;
        }
        return null;
    }
1313140
public function jQueryValidationEngine($html)
	{
		$prefix = self::idPrefix;
		$html .= <<<HERE
<script type="text/javascript">
jQuery(document).ready(function(){
	jQuery("#{$prefix}-{$this->id}").validationEngine();

HERE;
		foreach($this->children as $child)
		{
			if(isset($child->parents))
			{
				foreach($child->parents as $parent => $condition)
				{
					$parent = '#'.self::idPrefix.$parent;
					$id = self::idPrefix.$child->id;
					$html .= "$('#{$id}_container').ConditionManager(".json_encode($parent).", ".json_encode($condition).");\n";
				}
			}
		}
		$html .= '});</script>';
		return $html;
	}
434754
public static function extract(\DOMElement $element)
    {
        $doc = new \DOMDocument('1.0', 'utf-8');
        $imported = $doc->importNode($element, true);
        $doc->appendChild($imported);

        $prefix = $doc->lookupPrefix('http://www.w3.org/1999/xhtml');
        if ('' !== $prefix) {
            $prefix .= ':';
        }
        $patterns = [
            '/<\?xml[^<]*>[^<]*<' . $prefix . 'div[^<]*/',
            '/<\/' . $prefix . 'div>\s*$/'
        ];
        $text = preg_replace($patterns, '', $doc->saveXML());
        if ('' !== $prefix) {
            $text = preg_replace('/(<[\/]?)' . $prefix . '([a-zA-Z]+)/', '$1$2', $text);
        }

        return $text;
    }
1223288
public function check(DOMElement $template, Tag $tag)
	{
		$xpath = new DOMXPath($template->ownerDocument);
		$query
			= '//*[translate(local-name(), "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz") = "' . $this->elName . '"]'
			. '|'
			. '//xsl:element[translate(@name,"ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz") = "' . $this->elName . '"]';

		$node = $xpath->query($query)->item(0);
		if ($node)
		{
			throw new UnsafeTemplateException("Element '" . $this->elName . "' is disallowed", $node);
		}
	}
768006
protected function checkElementNode(DOMElement $element, Tag $tag)
	{
		$xpath = new DOMXPath($element->ownerDocument);

		// If current node is not an <xsl:attribute/> element, we exclude descendants
		// with an <xsl:attribute/> ancestor so that content such as:
		//   <script><xsl:attribute name="id"><xsl:value-of/></xsl:attribute></script>
		// would not trigger a false-positive due to the presence of an <xsl:value-of/>
		// element in a <script>
		$predicate = ($element->localName === 'attribute') ? '' : '[not(ancestor::xsl:attribute)]';

		// Test the select expression of <xsl:value-of/> nodes
		$query = './/xsl:value-of' . $predicate;
		foreach ($xpath->query($query, $element) as $valueOf)
		{
			$this->checkSelectNode($valueOf->getAttributeNode('select'), $tag);
		}

		// Reject all <xsl:apply-templates/> nodes
		$query = './/xsl:apply-templates' . $predicate;
		foreach ($xpath->query($query, $element) as $applyTemplates)
		{
			throw new UnsafeTemplateException('Cannot allow unfiltered data in this context', $applyTemplates);
		}
	}
763860
public function images($regex = null, $attr = 'src')
    {
        /**
         * @var \DOMElement $node
         * @var \DOMElement $node2
         */
        $query = $this->_document->getQuery();
        $document = $this->_document;

        $data = [];
        foreach ($this->_nodes as $node) {
            if ($node->nodeName === 'img') {
                $src = $document->absolutizeUrl($node->getAttribute($attr));

                if ($regex && !preg_match($regex, $src)) {
                    continue;
                }

                $data[$node->getNodePath()] = $src;
            } else {
                foreach ($query->xpath("descendant::img[@$attr]", $node) as $node2) {
                    $src = $document->absolutizeUrl($node2->getAttribute($attr));

                    if ($regex && !preg_match($regex, $src)) {
                        continue;
                    }

                    $data[$node2->getNodePath()] = $src;
                }
            }
        }

        return array_values($data);
    }
14505
public static function contentToCatalogue($content, $locale, $domain)
    {
        $loader = new XliffLoader();
        $catalogue = new MessageCatalogue($locale);
        $loader->extractFromContent($content, $catalogue, $domain);

        return $catalogue;
    }
500502
public function update(string $domain, string $tag, string $description)
    {
        Assert::stringNotEmpty($domain);
        Assert::stringNotEmpty($tag);

        $params = [
            'description' => $description,
        ];

        $response = $this->httpPut(sprintf('/v3/%s/tags/%s', $domain, $tag), $params);

        return $this->hydrateResponse($response, UpdateResponse::class);
    }
1112845
private function extractAttributes(\DOMElement $node, array $data, $nodeName)
    {
        foreach ($data['@attributes'] as $key => $value) {

            if (! $this->isValidTagName($key)) {

                throw new \Exception(__CLASS__.' Illegal character in attribute name. attribute: '.$key.' in node: '.$nodeName);

            }

            $node->setAttribute($key, $this->boolToString($value));
        }
    }
981219
protected function checkAttributeNode(DOMAttr $attribute, Tag $tag)
	{
		// Parse the attribute value for XPath expressions and assess their safety
		foreach (AVTHelper::parse($attribute->value) as $token)
		{
			if ($token[0] === 'expression')
			{
				$this->checkExpression($attribute, $token[1], $tag);
			}
		}
	}
52210
public function validationDomain($domain = null)
    {
        if ($domain === null) {
            return $this->_validationDomain;
        }
        $this->_validationDomain = $domain;
    }
650095
public function jQueryValidationEngine($html)
	{
		$validation = array();
		if($this->required===true)
		{
			$validation[] = 'required';
		}
		if(!empty($validation))
			$html = str_replace("\" class=\"", "\" class=\"validate[".implode(',', $validation)."] ", $html);
		return $html;
	}
133961
protected function checkSelectNode(DOMAttr $select, Tag $tag)
	{
		$this->checkExpression($select, $select->value, $tag);
	}
613031
static public function getSecondDomain($domain=null)
	{
	    $domain = !empty($domain) ? $domain : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : null);
	    $n = preg_match('/([^.]*\.)?([^.]*\.)?\w+\.\w+$/', $domain, $matches);
	    return $domain && isset($matches[2]) ? $matches[2] : (isset($matches[1]) ? $matches[1] : '');
	}
1120474
public function clean(\DOMElement $e, $tag)
    {
        $currentItem = null;
        $targetList = $e->getElementsByTagName($tag);
        $isEmbed = ('audio' === $tag || 'video' === $tag || 'iframe' === $tag || 'object' === $tag || 'embed' === $tag);

        for ($y = $targetList->length - 1; $y >= 0; --$y) {
            // Allow youtube and vimeo videos through as people usually want to see those.
            $currentItem = $targetList->item($y);

            if ($isEmbed) {
                $attributeValues = $currentItem->getAttribute('src') . ' ' . $currentItem->getAttribute('href');

                // First, check the elements attributes to see if any of them contain known media hosts
                if (preg_match($this->regexps['media'], $attributeValues)) {
                    continue;
                }

                // Then check the elements inside this element for the same.
                if (preg_match($this->regexps['media'], $targetList->item($y)->getInnerHTML())) {
                    continue;
                }
            }

            $currentItem->parentNode->removeChild($currentItem);
        }
    }
1340000
protected function prepareCacheId($locale, $domain) {
		if (is_array($locale)) {
			$list = [];
			
			// many locales to many domains
			if (is_array($domain)) {
				foreach ($locale as $part1) {
					foreach ($domain as $part2) {
						$list[] = $part1 . '_' . $part2;
					}
				}
				
				return $list;
			}
			
			// many locales to one domain
			foreach ($locale as $part) {
				$list[] = $part1 . '_' . $domain;
			}
			
			return $list;
		}
		
		// one locale to many domains
		if (is_array($domain)) {
			$list = [];
			
			foreach ($domain as $part) {
				$list[] = $locale . '_' . $part;
			}
			
			return $list;
		}
		
		// one locale to one domain
		return $locale . '_' . $domain;
	}
1296448
public function gettext($attrib, $domain = null)
    {
        // load localization files if not done yet
        if (empty($this->texts)) {
            $this->load_language();
        }

        // extract attributes
        if (is_string($attrib)) {
            $attrib = array('name' => $attrib);
        }

        $name = (string) $attrib['name'];

        // attrib contain text values: use them from now
        if (($setval = $attrib[strtolower($_SESSION['language'])]) || ($setval = $attrib['en_us'])) {
            $this->texts[$name] = $setval;
        }

        // check for text with domain
        if ($domain && ($text = $this->texts[$domain.'.'.$name])) {
        }
        // text does not exist
        else if (!($text = $this->texts[$name])) {
            return "[$name]";
        }

        // replace vars in text
        if (is_array($attrib['vars'])) {
            foreach ($attrib['vars'] as $var_key => $var_value) {
                $text = str_replace($var_key[0] != '$' ? '$'.$var_key : $var_key, $var_value, $text);
            }
        }

        // replace \n with real line break
        $text = strtr($text, array('\n' => "\n"));

        // case folding
        if (($attrib['uppercase'] && strtolower($attrib['uppercase']) == 'first') || $attrib['ucfirst']) {
            $case_mode = MB_CASE_TITLE;
        }
        else if ($attrib['uppercase']) {
            $case_mode = MB_CASE_UPPER;
        }
        else if ($attrib['lowercase']) {
            $case_mode = MB_CASE_LOWER;
        }

        if (isset($case_mode)) {
            $text = mb_convert_case($text, $case_mode);
        }

        return $text;
    }