From 79fdbafce5bbc3655294f139c92a4ce5071de315 Mon Sep 17 00:00:00 2001
From: LarsDW223 <lars_paulsen@web.de>
Date: Sat, 20 May 2017 13:59:49 +0200
Subject: [PATCH] Adjusted pattern for matching media links to properly handle
 a single '}' in the link text. Fixes #1587.

---
 _test/tests/inc/parser/parser_media.test.php | 61 ++++++++++++++++++++
 inc/parser/parser.php                        |  2 +-
 2 files changed, 62 insertions(+), 1 deletion(-)

diff --git a/_test/tests/inc/parser/parser_media.test.php b/_test/tests/inc/parser/parser_media.test.php
index 0ed52b9f8..2b22dabff 100644
--- a/_test/tests/inc/parser/parser_media.test.php
+++ b/_test/tests/inc/parser/parser_media.test.php
@@ -4,6 +4,7 @@ require_once 'parser.inc.php';
 /**
  * Tests for the implementation of audio and video files
  *
+ * @group parser_media
  * @author  Michael Große <grosse@cosmocode.de>
 */
 class TestOfDoku_Parser_Media extends TestOfDoku_Parser {
@@ -131,4 +132,64 @@ class TestOfDoku_Parser_Media extends TestOfDoku_Parser {
         $substr_start = strlen($url) - strlen($rest);
         $this->assertEquals($rest, substr($url, $substr_start));
     }
+
+    function testSimpleLinkText() {
+        $file = 'wiki:dokuwiki-128.png';
+        $parser_response = p_get_instructions('{{' . $file . '|This is a simple text.}}');
+
+        $calls = array (
+            array('document_start',array()),
+            array('p_open',array()),
+            array('internalmedia',array($file,'This is a simple text.',null,null,null,'cache','details')),
+            array('cdata',array(null)),
+            array('p_close',array()),
+            array('document_end',array()),
+        );
+        $this->assertEquals(array_map('stripbyteindex',$parser_response),$calls);
+    }
+
+    function testLinkTextWithWavedBrackets_1() {
+        $file = 'wiki:dokuwiki-128.png';
+        $parser_response = p_get_instructions('{{' . $file . '|We got a { here.}}');
+
+        $calls = array (
+            array('document_start',array()),
+            array('p_open',array()),
+            array('internalmedia',array($file,'We got a { here.',null,null,null,'cache','details')),
+            array('cdata',array(null)),
+            array('p_close',array()),
+            array('document_end',array()),
+        );
+        $this->assertEquals(array_map('stripbyteindex',$parser_response),$calls);
+    }
+
+    function testLinkTextWithWavedBrackets_2() {
+        $file = 'wiki:dokuwiki-128.png';
+        $parser_response = p_get_instructions('{{' . $file . '|We got a } here.}}');
+
+        $calls = array (
+            array('document_start',array()),
+            array('p_open',array()),
+            array('internalmedia',array($file,'We got a } here.',null,null,null,'cache','details')),
+            array('cdata',array(null)),
+            array('p_close',array()),
+            array('document_end',array()),
+        );
+        $this->assertEquals(array_map('stripbyteindex',$parser_response),$calls);
+    }
+
+    function testLinkTextWithWavedBrackets_3() {
+        $file = 'wiki:dokuwiki-128.png';
+        $parser_response = p_get_instructions('{{' . $file . '|We got a { and a } here.}}');
+
+        $calls = array (
+            array('document_start',array()),
+            array('p_open',array()),
+            array('internalmedia',array($file,'We got a { and a } here.',null,null,null,'cache','details')),
+            array('cdata',array(null)),
+            array('p_close',array()),
+            array('document_end',array()),
+        );
+        $this->assertEquals(array_map('stripbyteindex',$parser_response),$calls);
+    }
 }
diff --git a/inc/parser/parser.php b/inc/parser/parser.php
index 6f25dc9e1..5ffb5cc00 100644
--- a/inc/parser/parser.php
+++ b/inc/parser/parser.php
@@ -916,7 +916,7 @@ class Doku_Parser_Mode_media extends Doku_Parser_Mode {
 
     function connectTo($mode) {
         // Word boundaries?
-        $this->Lexer->addSpecialPattern("\{\{[^\}]+\}\}",$mode,'media');
+        $this->Lexer->addSpecialPattern("\{\{(?:[^\}]|(?:\}[^\}]))+\}\}",$mode,'media');
     }
 
     function getSort() {
-- 
GitLab