• Модуль: landing
  • Путь к файлу: ~/bitrix/modules/landing/lib/node/img.php
  • Класс: BitrixLandingNodeImg
  • Вызов: Img::getNode
static function getNode(BitrixLandingBlock $block, $selector)
{
	$data = [];
	$doc = $block->getDom();
	$resultList = $doc->querySelectorAll($selector);
	if (!$resultList)
	{
		$resultList = NodeStyle::getNodesBySelector($block, $selector);
	}

	foreach ($resultList as $pos => $res)
	{
		$data[$pos] = [
			'src' => '',
			'src2x' => '',
			'id' => null,
			'id2x' => null,
			'alt' => '',
			'isLazy' => 'N',
		];

		if ($res->getTagName() !== 'IMG')
		{
			$styles = StyleInliner::getStyle($res);
			if (isset($styles['background-image']))
			{
				$src = $src2x = null;
				// try gets retina srcset
				if (
					preg_match_all(
						'/url('*([^']+)'*)s*([d]*x*)/is',
						$styles['background-image'],
						$matches
					)
				)
				{
					for ($i = 0, $c = count($matches[1]); $i < $c; $i++)
					{
						if ($matches[2][$i] === '2x')
						{
							$src2x = $matches[1][$i];
						}
						else
						{
							$src = $matches[1][$i];
						}
					}
				}
				if ($src || $src2x)
				{
					if ($src)
					{
						$data[$pos]['src'] = Manager::getUrlFromFile($src);
					}
					if ($src2x)
					{
						$data[$pos]['src2x'] = Manager::getUrlFromFile($src2x);
					}
				}

				// for lazyload
				if (
					($isLazy = $res->getAttribute('data-lazy-bg'))
					&& $isLazy === 'Y'
				)
				{
					$data[$pos]['isLazy'] = 'Y';
					if ($lazyOrigSrc = $res->getAttribute('data-src'))
					{
						$data[$pos]['lazyOrigSrc'] = $lazyOrigSrc;
					}
					if ($lazyOrigSrc2x = $res->getAttribute('data-src2x'))
					{
						$data[$pos]['lazyOrigSrc2x'] = $lazyOrigSrc2x;
					}
					if ($lazyOrigStyle = $res->getAttribute('data-style'))
					{
						$data[$pos]['lazyOrigStyle'] = $lazyOrigStyle;
					}
				}
			}
		}
		else
		{
			$src = $res->getAttribute('src');
			$srcSet = $res->getAttribute('srcset');

			$data[$pos]['src'] = Manager::getUrlFromFile($src);
			$data[$pos]['alt'] = $res->getAttribute('alt');

			if (preg_match('/[,s]*(.*?)s+2x/is', $srcSet, $matches))
			{
				$data[$pos]['src2x'] = Manager::getUrlFromFile($matches[1]);
			}

			// for lazyload
			$isLazy = $res->getAttribute('data-lazy-img');
			if ($isLazy === 'Y')
			{
				$data[$pos]['isLazy'] = 'Y';
				$lazyOrigSrc = $res->getAttribute('data-src');
				if ($lazyOrigSrc)
				{
					$data[$pos]['lazyOrigSrc'] = $lazyOrigSrc;
				}
				$lazyOrigSrcset = $res->getAttribute('data-srcset');
				if ($lazyOrigSrcset)
				{
					if (
						preg_match('/([^ ]+) 2x/i', $lazyOrigSrcset, $matches)
						&& $matches[1]
					)
					{
						$data[$pos]['lazyOrigSrc2x'] = $matches[1];
					}
					// comment just for changes
					$data[$pos]['lazyOrigSrcset'] = $lazyOrigSrcset;
				}
			}
		}

		if ($val = $res->getAttribute('data-pseudo-url'))
		{
			$data[$pos]['url'] = $val;
		}

		if ($val = $res->getAttribute('data-fileid'))
		{
			$data[$pos]['id'] = $val;
		}

		if (
			(isset($data[$pos]['src2x']) || isset($data[$pos]['lazyOrigSrc2x']))
			&& ($val = $res->getAttribute('data-fileid2x'))
		)
		{
			$data[$pos]['id2x'] = $val;
		}
	}

	return $data;
}