Skip to content

literal filter/operator conflict #4767

Description

@brandonkelly

Craft CMS defines a literal filter, which conflicts with the new literal operator introduced in Twig 3.21 via #4543.

For example, the following template will trigger the error: Unexpected token "operator" of value "literal" ("name" expected).

{{ 'foo' | literal }}

Lexer::getOperatorRegex() is defensive against filters being confused for operators here:

Twig/src/Lexer.php

Lines 545 to 548 in a64dc5d

// an operator that begins with a character must not have a dot or pipe before
if (ctype_alpha($expressionParser[0])) {
$r = '(?<![\.\|])'.$r;
}

However the lookbehind doesn’t account for filters that have spaces around them, which is pretty commonplace.

Suggested fix

Changing that lookbehind code to the following fixes the problem:

$r = '(?<![\.\|]\s|.[\.\|])'.$r;

(It’s not possible to have variable-length lookbehinds, so I believe this is the only way to do it, with that . and all, rather than just (?<![\.\|]\s?).)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions