Преглед изворни кода

Use more "unique" markers format for styling

Olivier Poitrey пре 12 година
родитељ
комит
e521588acb

+ 3 - 3
src/Monolog/Handler/BrowserConsoleHandler.php

@@ -31,12 +31,12 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
      *
      * Example of formatted string:
      *
-     *     You can do [blue text]{color: blue} or [green background]{background-color: green; color: white}
+     *     You can do [[blue text]]{color: blue} or [[green background]]{background-color: green; color: white}
      *
      */
     protected function getDefaultFormatter()
     {
-        return new LineFormatter('[%channel%]{macro: autolabel} [%level_name%]{font-weight: bold} %message%');
+        return new LineFormatter('[[%channel%]]{macro: autolabel} [[%level_name%]]{font-weight: bold} %message%');
     }
 
     /**
@@ -111,7 +111,7 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
         $args = array(self::quote('font-weight: normal'));
         $format = '%c' . $formatted;
         $self = 'Monolog\Handler\BrowserConsoleHandler';
-        $format = preg_replace_callback('/\[(.*?)\]\{(.*?)\}/', function($m) use(&$args, $self) {
+        $format = preg_replace_callback('/\[\[(.*?)\]\]\{([^}]*)\}/s', function($m) use(&$args, $self) {
             $args[] = $self::quote($self::handleCustomStyles($m[2], $m[1]));
             $args[] = $self::quote('font-weight: normal');
             return '%c' . $m[1] . '%c';

+ 21 - 4
tests/Monolog/Handler/BrowserConsoleHandlerTest.php

@@ -29,7 +29,7 @@ class BrowserConsoleHandlerTest extends TestCase
         $handler = new BrowserConsoleHandler();
         $handler->setFormatter($this->getIdentityFormatter());
 
-        $handler->handle($this->getRecord(Logger::DEBUG, 'foo[bar]{color: red}'));
+        $handler->handle($this->getRecord(Logger::DEBUG, 'foo[[bar]]{color: red}'));
 
         $expected = <<<EOF
 (function(c){if (c && c.groupCollapsed) {
@@ -40,14 +40,31 @@ EOF;
         $this->assertEquals($expected, BrowserConsoleHandler::generateScript());
     }
 
+    public function testEscaping()
+    {
+        $handler = new BrowserConsoleHandler();
+        $handler->setFormatter($this->getIdentityFormatter());
+
+        $handler->handle($this->getRecord(Logger::DEBUG, "[foo] [[\"bar\n[baz]\"]]{color: red}"));
+
+        $expected = <<<EOF
+(function(c){if (c && c.groupCollapsed) {
+c.log("%c[foo] %c\"bar\\n[baz]\"%c", "font-weight: normal", "color: red", "font-weight: normal");
+}})(console);
+EOF;
+
+        $this->assertEquals($expected, BrowserConsoleHandler::generateScript());
+    }
+
+
     public function testAutolabel()
     {
         $handler = new BrowserConsoleHandler();
         $handler->setFormatter($this->getIdentityFormatter());
 
-        $handler->handle($this->getRecord(Logger::DEBUG, '[foo]{macro: autolabel}'));
-        $handler->handle($this->getRecord(Logger::DEBUG, '[bar]{macro: autolabel}'));
-        $handler->handle($this->getRecord(Logger::DEBUG, '[foo]{macro: autolabel}'));
+        $handler->handle($this->getRecord(Logger::DEBUG, '[[foo]]{macro: autolabel}'));
+        $handler->handle($this->getRecord(Logger::DEBUG, '[[bar]]{macro: autolabel}'));
+        $handler->handle($this->getRecord(Logger::DEBUG, '[[foo]]{macro: autolabel}'));
 
         $expected = <<<EOF
 (function(c){if (c && c.groupCollapsed) {