芝麻web文件管理V1.00
编辑当前文件:/home/qrafawbu/www/vendor/league/commonmark/src/Extension/Attributes/Util/AttributesHelper.php
* (c) 2015 Martin Hasoň
* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ declare(strict_types=1); namespace League\CommonMark\Extension\Attributes\Util; use League\CommonMark\Block\Element\AbstractBlock; use League\CommonMark\Cursor; use League\CommonMark\Inline\Element\AbstractInline; use League\CommonMark\Util\RegexHelper; /** * @internal */ final class AttributesHelper { /** * @param Cursor $cursor * * @return array
*/ public static function parseAttributes(Cursor $cursor): array { $state = $cursor->saveState(); $cursor->advanceToNextNonSpaceOrNewline(); if ($cursor->getCharacter() !== '{') { $cursor->restoreState($state); return []; } $cursor->advanceBy(1); if ($cursor->getCharacter() === ':') { $cursor->advanceBy(1); } $attributes = []; $regex = '/^\s*([.#][_a-z0-9-]+|' . RegexHelper::PARTIAL_ATTRIBUTENAME . RegexHelper::PARTIAL_ATTRIBUTEVALUESPEC . ')(?match($regex))) { if ($attribute[0] === '#') { $attributes['id'] = \substr($attribute, 1); continue; } if ($attribute[0] === '.') { $attributes['class'][] = \substr($attribute, 1); continue; } [$name, $value] = \explode('=', $attribute, 2); $first = $value[0]; $last = \substr($value, -1); if ((($first === '"' && $last === '"') || ($first === "'" && $last === "'")) && \strlen($value) > 1) { $value = \substr($value, 1, -1); } if (\strtolower(\trim($name)) === 'class') { foreach (\array_filter(\explode(' ', \trim($value))) as $class) { $attributes['class'][] = $class; } } else { $attributes[trim($name)] = trim($value); } } if ($cursor->match('/}/') === null) { $cursor->restoreState($state); return []; } if ($attributes === []) { $cursor->restoreState($state); return []; } if (isset($attributes['class'])) { $attributes['class'] = \implode(' ', (array) $attributes['class']); } return $attributes; } /** * @param AbstractBlock|AbstractInline|array
$attributes1 * @param AbstractBlock|AbstractInline|array
$attributes2 * * @return array
*/ public static function mergeAttributes($attributes1, $attributes2): array { $attributes = []; foreach ([$attributes1, $attributes2] as $arg) { if ($arg instanceof AbstractBlock || $arg instanceof AbstractInline) { $arg = $arg->data['attributes'] ?? []; } /** @var array
$arg */ $arg = (array) $arg; if (isset($arg['class'])) { foreach (\array_filter(\explode(' ', \trim($arg['class']))) as $class) { $attributes['class'][] = $class; } unset($arg['class']); } $attributes = \array_merge($attributes, $arg); } if (isset($attributes['class'])) { $attributes['class'] = \implode(' ', $attributes['class']); } return $attributes; } }