Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nodesiremonk opened this issue Oct 10, 2024 · 0 comments
Open

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

nodesiremonk opened this issue Oct 10, 2024 · 0 comments

Comments

@nodesiremonk
Copy link

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);

    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant