Skip to content

x-component does not work with self-closing tag #197

@nodesiremonk

Description

@nodesiremonk

It seems the self-closing tag (e.g. <x-component key="value" />) somehow will make the match[4] undefined in the callback function.

I tested the regex in preg_replace_callback() function and it seems the $match[4] should be empty rather than undefined in the case of using self-closing tag /> Not sure why it will throw an undefined error here.

As a temporary workaround, I have to add the ?? '' after $match[4] parameter to make it work for now.
$match[4] = $this->compileComponents( $match[4] ?? '');

Might need to investigate more for a better solution to fix the problem.

protected function compileComponents($value)
    {
        /**
         * @param array $match
         *                    [0]=full expression with @ and parenthesis
         *                    [1]=Component name
         *                    [2]=parameters
         *                    [3]=...
         *                    [4]=content
         *
         * @return string
         */

        $callback = function($match) {

            if(static::contains($match[0], 'x-')) {
                $match[4] = $this->compileComponents( $match[4]);
            }
            $paramsCompiled = $this->parseParams($match[2]);
            $str =  "('components.".$match[1]."',".$paramsCompiled.")";

            return self::compileComponent($str).$match[4].self::compileEndComponent();
        };
        return preg_replace_callback('/<x-([a-z0-9.-]+)(\s[^>]*)?(>((?:(?!<\/x-\1>).)*)<\/x-\1>|\/>)/ms', $callback, $value);

    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions