From 62ba78dc35aa655020a60f375da98d37c4485326 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Wed, 24 Dec 2014 12:56:46 +0100 Subject: [PATCH] Issue #2396793 by xjm: Token API will match tokens with empty types or tokens, e.g. [:invalid] --- core/lib/Drupal/Core/Utility/Token.php | 4 ++-- core/modules/system/src/Tests/System/TokenScanTest.php | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/core/lib/Drupal/Core/Utility/Token.php b/core/lib/Drupal/Core/Utility/Token.php index 4cfc63f6a7..0f5d30e651 100644 --- a/core/lib/Drupal/Core/Utility/Token.php +++ b/core/lib/Drupal/Core/Utility/Token.php @@ -185,9 +185,9 @@ public function scan($text) { // $type may not contain : or whitespace characters, but $name may. preg_match_all('/ \[ # [ - pattern start - ([^\s\[\]:]*) # match $type not containing whitespace : [ or ] + ([^\s\[\]:]+) # match $type not containing whitespace : [ or ] : # : - separator - ([^\[\]]*) # match $name not containing [ or ] + ([^\[\]]+) # match $name not containing [ or ] \] # ] - pattern end /x', $text, $matches); diff --git a/core/modules/system/src/Tests/System/TokenScanTest.php b/core/modules/system/src/Tests/System/TokenScanTest.php index 4cb2d69a20..65fc041d50 100644 --- a/core/modules/system/src/Tests/System/TokenScanTest.php +++ b/core/modules/system/src/Tests/System/TokenScanTest.php @@ -24,12 +24,18 @@ function testTokenScan() { // strings. $text = 'First a [valid:simple], but dummy token, and a dummy [valid:token with: spaces].'; $text .= 'Then a [not valid:token].'; + $text .= 'Then an [:empty token type].'; + $text .= 'Then an [empty token:].'; + $text .= 'Then a totally empty token: [:].'; $text .= 'Last an existing token: [node:author:name].'; $token_wannabes = \Drupal::token()->scan($text); $this->assertTrue(isset($token_wannabes['valid']['simple']), 'A simple valid token has been matched.'); $this->assertTrue(isset($token_wannabes['valid']['token with: spaces']), 'A valid token with space characters in the token name has been matched.'); $this->assertFalse(isset($token_wannabes['not valid']), 'An invalid token with spaces in the token type has not been matched.'); + $this->assertFalse(isset($token_wannabes['empty token']), 'An empty token has not been matched.'); + $this->assertFalse(isset($token_wannabes['']['empty token type']), 'An empty token type has not been matched.'); + $this->assertFalse(isset($token_wannabes['']['']), 'An empty token and type has not been matched.'); $this->assertTrue(isset($token_wannabes['node']), 'An existing valid token has been matched.'); } -- GitLab