예제 #1
0
 public function testLinkShortcodeHandler()
 {
     $testFile = $this->objFromFixture('File', 'asdf');
     $parser = new ShortcodeParser();
     $parser->register('file_link', array('File', 'link_shortcode_handler'));
     $fileShortcode = sprintf('[file_link,id=%d]', $testFile->ID);
     $fileEnclosed = sprintf('[file_link,id=%d]Example Content[/file_link]', $testFile->ID);
     $fileShortcodeExpected = $testFile->Link();
     $fileEnclosedExpected = sprintf('<a href="%s" class="file" data-type="txt" data-size="977 KB">Example Content</a>', $testFile->Link());
     $this->assertEquals($fileShortcodeExpected, $parser->parse($fileShortcode), 'Test that simple linking works.');
     $this->assertEquals($fileEnclosedExpected, $parser->parse($fileEnclosed), 'Test enclosed content is linked.');
     $testFile->delete();
     $fileShortcode = '[file_link,id="-1"]';
     $fileEnclosed = '[file_link,id="-1"]Example Content[/file_link]';
     $this->assertEquals('', $parser->parse('[file_link]'), 'Test that invalid ID attributes are not parsed.');
     $this->assertEquals('', $parser->parse('[file_link,id="text"]'));
     $this->assertEquals('', $parser->parse('[file_link]Example Content[/file_link]'));
     if (class_exists('ErrorPage')) {
         $errorPage = ErrorPage::get()->filter('ErrorCode', 404)->First();
         $this->assertEquals($errorPage->Link(), $parser->parse($fileShortcode), 'Test link to 404 page if no suitable matches.');
         $this->assertEquals(sprintf('<a href="%s">Example Content</a>', $errorPage->Link()), $parser->parse($fileEnclosed));
     } else {
         $this->assertEquals('', $parser->parse($fileShortcode), 'Short code is removed if file record is not present.');
         $this->assertEquals('', $parser->parse($fileEnclosed));
     }
 }
 public function parse($content)
 {
     $context = $this->getContext();
     // Replace custom elements
     $content = $this->substituteCustomElements($content, $context);
     // Delegate to parent to handle shortcodes proper.
     return parent::parse($content);
 }
 public static function handle($arguments, $content, ShortcodeParser $parser, $tag, array $extra = array())
 {
     if (!empty($arguments['id'])) {
         $document = DMSDocument::get()->byID($arguments['id']);
         if ($document && !$document->isHidden()) {
             if ($content) {
                 return sprintf('<a href="%s">%s</a>', $document->Link(), $parser->parse($content));
             } else {
                 if (isset($extra['element'])) {
                     $extra['element']->setAttribute('data-ext', $document->getExtension());
                     $extra['element']->setAttribute('data-size', $document->getFileSizeFormatted());
                 }
                 return $document->Link();
             }
         }
     }
     $error = ErrorPage::get()->filter('ErrorCode', '404')->First();
     if ($error) {
         return $error->Link();
     }
     return '';
 }
 public function testLinkShortcodeHandler()
 {
     $aboutPage = $this->objFromFixture('Page', 'about');
     $errorPage = $this->objFromFixture('ErrorPage', '404');
     $redirectPage = $this->objFromFixture('RedirectorPage', 'external');
     $parser = new ShortcodeParser();
     $parser->register('sitetree_link', array('SiteTree', 'link_shortcode_handler'));
     $aboutShortcode = sprintf('[sitetree_link,id=%d]', $aboutPage->ID);
     $aboutEnclosed = sprintf('[sitetree_link,id=%d]Example Content[/sitetree_link]', $aboutPage->ID);
     $aboutShortcodeExpected = $aboutPage->Link();
     $aboutEnclosedExpected = sprintf('<a href="%s">Example Content</a>', $aboutPage->Link());
     $this->assertEquals($aboutShortcodeExpected, $parser->parse($aboutShortcode), 'Test that simple linking works.');
     $this->assertEquals($aboutEnclosedExpected, $parser->parse($aboutEnclosed), 'Test enclosed content is linked.');
     $aboutPage->delete();
     $this->assertEquals($aboutShortcodeExpected, $parser->parse($aboutShortcode), 'Test that deleted pages still link.');
     $this->assertEquals($aboutEnclosedExpected, $parser->parse($aboutEnclosed));
     $aboutShortcode = '[sitetree_link,id="-1"]';
     $aboutEnclosed = '[sitetree_link,id="-1"]Example Content[/sitetree_link]';
     $aboutShortcodeExpected = $errorPage->Link();
     $aboutEnclosedExpected = sprintf('<a href="%s">Example Content</a>', $errorPage->Link());
     $this->assertEquals($aboutShortcodeExpected, $parser->parse($aboutShortcode), 'Test link to 404 page if no suitable matches.');
     $this->assertEquals($aboutEnclosedExpected, $parser->parse($aboutEnclosed));
     $redirectShortcode = sprintf('[sitetree_link,id=%d]', $redirectPage->ID);
     $redirectEnclosed = sprintf('[sitetree_link,id=%d]Example Content[/sitetree_link]', $redirectPage->ID);
     $redirectExpected = 'http://www.google.com?a&amp;b';
     $this->assertEquals($redirectExpected, $parser->parse($redirectShortcode));
     $this->assertEquals(sprintf('<a href="%s">Example Content</a>', $redirectExpected), $parser->parse($redirectEnclosed));
     $this->assertEquals('', $parser->parse('[sitetree_link]'), 'Test that invalid ID attributes are not parsed.');
     $this->assertEquals('', $parser->parse('[sitetree_link,id="text"]'));
     $this->assertEquals('', $parser->parse('[sitetree_link]Example Content[/sitetree_link]'));
 }
예제 #5
0
 /**
  * Replace"[file_link id=n]" shortcode with an anchor tag or link to the file.
  * 
  * @param array $arguments Arguments passed to the parser
  * @param string $content Raw shortcode
  * @param ShortcodeParser $parser Parser
  * @param string $shortcode Name of shortcode used to register this handler
  * @param array $extra Extra arguments
  * @return string Result of the handled shortcode
  */
 public static function handle_shortcode($arguments, $content, $parser, $shortcode, $extra = array())
 {
     if (!isset($arguments['id']) || !is_numeric($arguments['id'])) {
         return;
     }
     $record = DataObject::get_by_id('File', $arguments['id']);
     if (!$record) {
         if (class_exists('ErrorPage')) {
             $record = ErrorPage::get()->filter("ErrorCode", 404)->first();
         }
         if (!$record) {
             return;
             // There were no suitable matches at all.
         }
     }
     // build the HTML tag
     if ($content) {
         // build some useful meta-data (file type and size) as data attributes
         $attrs = ' ';
         if ($record instanceof File) {
             foreach (array('class' => 'file', 'data-type' => $record->getExtension(), 'data-size' => $record->getSize()) as $name => $value) {
                 $attrs .= sprintf('%s="%s" ', $name, $value);
             }
         }
         return sprintf('<a href="%s"%s>%s</a>', $record->Link(), rtrim($attrs), $parser->parse($content));
     } else {
         return $record->Link();
     }
 }
 public function testShortcodeHandlerAddsDefaultAttributes()
 {
     $image = $this->objFromFixture('Image', 'imageWithoutTitle');
     $parser = new ShortcodeParser();
     $parser->register('image', array('Image', 'handle_shortcode'));
     $this->assertEquals(sprintf('<img src="%s" alt="%s">', $image->Link(), $image->Title), $parser->parse(sprintf('[image id="%d"]', $image->ID)));
 }
예제 #7
0
 /**
  * Replace "[file_link id=n]" shortcode with an anchor tag or link to the file.
  *
  * @param array $arguments Arguments passed to the parser
  * @param string $content Raw shortcode
  * @param ShortcodeParser $parser Parser
  * @param string $shortcode Name of shortcode used to register this handler
  * @param array $extra Extra arguments
  * @return string Result of the handled shortcode
  */
 public static function handle_shortcode($arguments, $content, $parser, $shortcode, $extra = array())
 {
     // Find appropriate record, with fallback for error handlers
     $record = static::find_shortcode_record($arguments, $errorCode);
     if ($errorCode) {
         $record = static::find_error_record($errorCode);
     }
     if (!$record) {
         return null;
         // There were no suitable matches at all.
     }
     // build the HTML tag
     if ($content) {
         // build some useful meta-data (file type and size) as data attributes
         $attrs = ' ';
         if ($record instanceof File) {
             foreach (array('class' => 'file', 'data-type' => $record->getExtension(), 'data-size' => $record->getSize()) as $name => $value) {
                 $attrs .= sprintf('%s="%s" ', $name, $value);
             }
         }
         return sprintf('<a href="%s"%s>%s</a>', $record->Link(), rtrim($attrs), $parser->parse($content));
     } else {
         return $record->Link();
     }
 }
예제 #8
0
 /**
  * Replace"[file_link id=n]" shortcode with an anchor tag or link to the file.
  *
  * @param array $arguments Arguments passed to the parser
  * @param string $content Raw shortcode
  * @param ShortcodeParser $parser Parser
  * @param string $shortcode Name of shortcode used to register this handler
  * @param array $extra Extra arguments
  * @return string Result of the handled shortcode
  */
 public static function handle_shortcode($arguments, $content, $parser, $shortcode, $extra = array())
 {
     if (!isset($arguments['id']) || !is_numeric($arguments['id'])) {
         return null;
     }
     /** @var File|SiteTree $record */
     $record = DataObject::get_by_id('File', $arguments['id']);
     // Check record for common errors
     $errorCode = null;
     if (!$record) {
         $errorCode = 404;
     } elseif (!$record->canView()) {
         $errorCode = 403;
     }
     if ($errorCode) {
         $result = static::singleton()->invokeWithExtensions('getErrorRecordFor', $errorCode);
         $result = array_filter($result);
         if ($result) {
             $record = reset($result);
         }
     }
     if (!$record) {
         return null;
         // There were no suitable matches at all.
     }
     // build the HTML tag
     if ($content) {
         // build some useful meta-data (file type and size) as data attributes
         $attrs = ' ';
         if ($record instanceof File) {
             foreach (array('class' => 'file', 'data-type' => $record->getExtension(), 'data-size' => $record->getSize()) as $name => $value) {
                 $attrs .= sprintf('%s="%s" ', $name, $value);
             }
         }
         return sprintf('<a href="%s"%s>%s</a>', $record->Link(), rtrim($attrs), $parser->parse($content));
     } else {
         return $record->Link();
     }
 }
 public function replaceChildrenCalls($html)
 {
     $codes = new ShortcodeParser();
     $codes->register('CHILDREN', array($this, 'includeChildren'));
     return $codes->parse($html);
 }