public function testLinkShortcodeHandler() { $testFile = $this->objFromFixture('SilverStripe\\Assets\\File', 'asdf'); $parser = new ShortcodeParser(); $parser->register('file_link', array('SilverStripe\\Assets\\File', 'handle_shortcode')); $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('SilverStripe\\CMS\\Model\\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 RAW() { if ($this->processShortcodes) { return ShortcodeParser::get_active()->parse($this->value); } else { return $this->value; } }
public function setUp() { parent::setUp(); Config::inst()->update('SilverStripe\\Control\\Director', 'alternate_base_url', '/'); if (!self::$original_host) { self::$original_host = $_SERVER['HTTP_HOST']; } $_SERVER['HTTP_HOST'] = 'www.example.org'; ShortcodeParser::get('default')->register('test_shortcode', function () { return 'test shortcode output'; }); }
/** * Tests that valid short codes that have not been registered are not replaced. */ public function testNotRegisteredShortcode() { ShortcodeParser::$error_behavior = ShortcodeParser::STRIP; $this->assertEquals('', $this->parser->parse('[not_shortcode]')); $this->assertEquals('<img class="">', $this->parser->parse('<img class="[not_shortcode]">')); ShortcodeParser::$error_behavior = ShortcodeParser::WARN; $this->assertEquals('<strong class="warning">[not_shortcode]</strong>', $this->parser->parse('[not_shortcode]')); ShortcodeParser::$error_behavior = ShortcodeParser::LEAVE; $this->assertEquals('[not_shortcode]', $this->parser->parse('[not_shortcode]')); $this->assertEquals('[not_shortcode /]', $this->parser->parse('[not_shortcode /]')); $this->assertEquals('[not_shortcode,foo="bar"]', $this->parser->parse('[not_shortcode,foo="bar"]')); $this->assertEquals('[not_shortcode]a[/not_shortcode]', $this->parser->parse('[not_shortcode]a[/not_shortcode]')); $this->assertEquals('[/not_shortcode]', $this->parser->parse('[/not_shortcode]')); $this->assertEquals('<img class="[not_shortcode]">', $this->parser->parse('<img class="[not_shortcode]">')); }
public function testShortcodeHandlerAddsDefaultAttributes() { $image = $this->objFromFixture('SilverStripe\\Assets\\Image', 'imageWithoutTitle'); $parser = new ShortcodeParser(); $parser->register('image', array('SilverStripe\\Assets\\Image', 'handle_shortcode')); $this->assertEquals(sprintf('<img src="%s" alt="%s">', $image->Link(), $image->Title), $parser->parse(sprintf('[image id="%d"]', $image->ID))); }
public function testShortCodeParsedInTemplateHelpers() { $parser = ShortcodeParser::get('HTMLTextTest'); $parser->register('shortcode', function ($arguments, $content, $parser, $tagName, $extra) { return 'Replaced short code with this. <a href="home">home</a>'; }); ShortcodeParser::set_active('HTMLTextTest'); /** @var DBHTMLText $field */ $field = DBField::create_field('HTMLText', '<p>[shortcode]</p>'); $this->assertEquals('<p>Replaced short code with this. <a href="home">home</a></p>', $field->HTMLATT()); $this->assertEquals('%3Cp%3EReplaced+short+code+with+this.+%3Ca+href%3D%22home%22%3Ehome%3C%2Fa%3E%3C%2Fp%3E', $field->URLATT()); $this->assertEquals('%3Cp%3EReplaced%20short%20code%20with%20this.%20%3Ca%20href%3D%22home%22%3Ehome%3C%2Fa%3E%3C%2Fp%3E', $field->RAWURLATT()); $this->assertEquals('<p>Replaced short code with this. <a href="home">home</a></p>', $field->ATT()); $this->assertEquals('<p>Replaced short code with this. <a href="home">home</a></p>', $field->RAW()); $this->assertEquals('\\x3cp\\x3eReplaced short code with this. \\x3ca href=\\"home\\"\\x3ehome\\x3c/a\\x3e\\x3c/p\\x3e', $field->JS()); $this->assertEquals('<p>Replaced short code with this. <a href="home">home</a></p>', $field->HTML()); $this->assertEquals('<p>Replaced short code with this. <a href="home">home</a></p>', $field->XML()); $this->assertEquals('Repl...', $field->LimitCharacters(4, '...')); $this->assertEquals('Replaced...', $field->LimitCharactersToClosestWord(10, '...')); $this->assertEquals('Replaced...', $field->LimitWordCount(1, '...')); $this->assertEquals('<p>replaced short code with this. <a href="home">home</a></p>', $field->LowerCase()); $this->assertEquals('<P>REPLACED SHORT CODE WITH THIS. <A HREF="HOME">HOME</A></P>', $field->UpperCase()); $this->assertEquals('Replaced short code with this. home', $field->Plain()); Config::nest(); Config::inst()->update('SilverStripe\\Control\\Director', 'alternate_base_url', 'http://example.com/'); $this->assertEquals('<p>Replaced short code with this. <a href="http://example.com/home">home</a></p>', $field->AbsoluteLinks()); Config::unnest(); $this->assertEquals('Replaced short code with this.', $field->LimitSentences(1)); $this->assertEquals('Replaced short code with this.', $field->FirstSentence()); $this->assertEquals('Replaced short...', $field->Summary(2)); $this->assertEquals('Replaced short code with this. home', $field->FirstParagraph()); $this->assertEquals('Replaced <span class="highlight">short</span> <span class="highlight">code</span> with this. home', $field->ContextSummary(500, 'short code')); ShortcodeParser::set_active('default'); }
/** * Helper method to regenerate all shortcode links. * * @param string $value HTML value * @return string value with links resampled */ public static function regenerate_html_links($value) { // Create a shortcode generator which only regenerates links $regenerator = ShortcodeParser::get('regenerator'); return $regenerator->parse($value); }
use SilverStripe\Core\Cache; use SilverStripe\Dev\Deprecation; use SilverStripe\View\Parsers\ShortcodeParser; /** * Framework configuration file * * Here you can make different settings for the Framework module (the core * module). * * For example you can register the authentication methods you wish to use * on your site, e.g. to register the OpenID authentication method type * * <code> * Authenticator::register_authenticator('OpenIDAuthenticator'); * </code> */ ShortcodeParser::get('default')->register('file_link', array('SilverStripe\\Assets\\File', 'handle_shortcode'))->register('embed', array('SilverStripe\\Forms\\HtmlEditor\\EmbedShortcodeProvider', 'handle_shortcode'))->register('image', array('SilverStripe\\Assets\\Image', 'handle_shortcode')); // Shortcode parser which only regenerates shortcodes ShortcodeParser::get('regenerator')->register('image', array('SilverStripe\\Assets\\Image', 'regenerate_shortcode')); // @todo // ->register('dbfile_link', array('DBFile', 'handle_shortcode')) // Zend_Cache temp directory setting $_ENV['TMPDIR'] = TEMP_FOLDER; // for *nix $_ENV['TMP'] = TEMP_FOLDER; // for Windows Cache::set_cache_lifetime('GDBackend_Manipulations', null, 100); // If you don't want to see deprecation errors for the new APIs, change this to 3.2.0-dev. Deprecation::notification_version('3.2.0'); // TODO Remove once new ManifestBuilder with submodule support is in place require_once 'admin/_config.php';