コード例 #1
0
ファイル: CompatibilityTest.php プロジェクト: vanilla/nbbc
 /**
  * Test that new templates work like old templates.
  *
  * @param string $source The BBCode to parse.
  * @param string $expected The expected output.
  * @dataProvider provideTemplateTests
  */
 public function testTemplates($source, $expected)
 {
     $bbcode = new BBCode();
     $bbcode->setDetectURLs(true);
     $html = $bbcode->parse($source);
     $this->assertSame($expected, $html);
 }
コード例 #2
0
ファイル: UtilityMethodTest.php プロジェクト: vanilla/nbbc
 /**
  * Test {@link BBCode::isValidTLD()}.
  *
  * @param string $host The host to test.
  * @param bool $allowIPs Whether or not IP addresses count as valid IDs.
  * @dataProvider provideTLDInvalidTests
  */
 public function testIsValidTLDInvalid($host, $allowIPs = false)
 {
     $bbCode = new BBCode();
     $valid = $bbCode->isValidTLD($host, $allowIPs);
     $this->assertFalse($valid);
 }
コード例 #3
0
ファイル: ConformanceTest.php プロジェクト: vanilla/nbbc
 /**
  * Run a given test array.
  *
  * @param array $test The test to run.
  */
 protected function performTest($test)
 {
     $testDefaults = ['newline_ignore' => false, 'detect_urls' => false, 'urltarget' => false, 'urlforcetarget' => false, 'plainmode' => false, 'tag_marker' => '', 'skip' => false];
     $test = array_replace($testDefaults, $test);
     if ($test['skip']) {
         $this->markTestSkipped('Skipped test: ' . $test['descr']);
         return;
     }
     $bbcode = new BBCode();
     $bbcode->addRule('wstest', ['mode' => BBCode::BBCODE_MODE_ENHANCED, 'allow' => ['_default' => '/^[a-zA-Z0-9._ -]+$/'], 'template' => '<span style="wstest:{$_default}">{$_content}</span>', 'class' => 'inline', 'allow_in' => ['listitem', 'block', 'columns', 'inline', 'link']]);
     $bbcode->setLocalImgDir(__DIR__ . '/../smileys');
     $bbcode->setLocalImgURL('smileys');
     $bbcode->setTagMarker('[');
     $bbcode->setAllowAmpersand(false);
     $bbcode->setIgnoreNewlines((bool) $test['newline_ignore']);
     $bbcode->setDetectURLs((bool) $test['detect_urls']);
     $bbcode->setURLTargetable($test['urltarget'] == true);
     $bbcode->setURLTarget($test['urlforcetarget']);
     $bbcode->setPlainMode($test['plainmode']);
     if ($test['tag_marker'] === '<') {
         $bbcode->setTagMarker('<');
         $bbcode->setAllowAmpersand(true);
     } elseif (!empty($test['tag_marker'])) {
         $bbcode->setTagMarker($test['tag_marker']);
     }
     $result = $bbcode->parse($test['bbcode']);
     if (!empty($test['regex'])) {
         $this->assertRegExp($test['regex'], $result, $test['descr']);
     } else {
         $this->assertSame($test['html'], $result, $test['descr']);
     }
 }
コード例 #4
0
ファイル: class.bbcode.php プロジェクト: vanilla/vanilla
 /**
  * Create, configure and return an instance of Nbbc\BBcode.
  *
  * @return Nbbc
  */
 public function nbbc()
 {
     if ($this->nbbc === null) {
         $nbbc = new Nbbc();
         $nbbc->setEnableSmileys(false);
         $nbbc->setAllowAmpersand(true);
         $nbbc->addRule('attach', ['allow_in' => ['listitem', 'block', 'columns', 'inline', 'link'], 'class' => "image", 'content' => Nbbc::BBCODE_REQUIRED, 'end_tag' => Nbbc::BBCODE_REQUIRED, 'method' => [$this, 'doAttachment'], 'mode' => Nbbc::BBCODE_MODE_CALLBACK, 'plain_content' => [], 'plain_start' => "[image]"]);
         $nbbc->addRule('attachment', ['allow_in' => ['listitem', 'block', 'columns', 'inline', 'link'], 'class' => "image", 'content' => Nbbc::BBCODE_REQUIRED, 'end_tag' => Nbbc::BBCODE_REQUIRED, 'method' => [$this, 'removeAttachment'], 'mode' => Nbbc::BBCODE_MODE_CALLBACK, 'plain_content' => [], 'plain_start' => "[image]"]);
         $nbbc->addRule('code', ['after_endtag' => "sns", 'after_tag' => "sn", 'allow_in' => ['listitem', 'block', 'columns'], 'before_endtag' => "sn", 'before_tag' => "sns", 'class' => 'code', 'content' => Nbbc::BBCODE_VERBATIM, 'mode' => Nbbc::BBCODE_MODE_ENHANCED, 'plain_end' => "\n", 'plain_start' => "\n<b>Code:</b>\n", 'template' => "\n<pre>{\$_content/v}\n</pre>\n"]);
         $nbbc->addRule('hr', ['after_endtag' => "sns", 'after_tag' => "sns", 'allow_in' => ['listitem', 'block', 'columns'], 'before_endtag' => "sns", 'before_tag' => "sns", 'plain_end' => "\n", 'plain_start' => "\n", 'simple_end' => "", 'simple_start' => ""]);
         $nbbc->addRule('img', ['allow_in' => ['listitem', 'block', 'columns', 'inline', 'link'], 'class' => "image", 'content' => Nbbc::BBCODE_REQUIRED, 'end_tag' => Nbbc::BBCODE_REQUIRED, 'method' => [$this, 'doImage'], 'mode' => Nbbc::BBCODE_MODE_CALLBACK, 'plain_content' => [], 'plain_start' => "[image]"]);
         $nbbc->addRule('quote', ['after_endtag' => "sns", 'after_tag' => "sns", 'allow_in' => ['listitem', 'block', 'columns'], 'before_endtag' => "sns", 'before_tag' => "sns", 'method' => [$this, 'doQuote'], 'mode' => Nbbc::BBCODE_MODE_CALLBACK, 'plain_end' => "\n", 'plain_start' => "\n<b>Quote:</b>\n"]);
         // The original NBBC rule was copied here and the regex was updated to meet our new criteria.
         $nbbc->addRule('size', ['allow' => ['_default' => '/^[0-9.]+(em|px)?$/D'], 'allow_in' => ['listitem', 'block', 'columns', 'inline', 'link'], 'class' => "inline", 'method' => [$this, 'doSize'], 'mode' => Nbbc::BBCODE_MODE_CALLBACK]);
         $nbbc->addRule('snapback', ['after_endtag' => "sns", 'after_tag' => "sn", 'allow_in' => ['listitem', 'block', 'columns'], 'before_endtag' => "sn", 'before_tag' => "sns", 'class' => 'code', 'content' => Nbbc::BBCODE_VERBATIM, 'mode' => Nbbc::BBCODE_MODE_ENHANCED, 'plain_end' => "\n", 'plain_start' => "\n<b>Snapback:</b>\n", 'template' => ' <a href="' . url('/discussion/comment/{$_content/v}#Comment_{$_content/v}', true) . '" class="SnapBack">»</a> ']);
         $nbbc->addRule('spoiler', ['after_endtag' => "sns", 'after_tag' => "sns", 'allow_in' => ['listitem', 'block', 'columns'], 'before_endtag' => "sns", 'before_tag' => "sns", 'plain_end' => "\n", 'plain_start' => "\n", 'simple_end' => "</div>\n", 'simple_start' => "\n<div class=\"Spoiler\">"]);
         $nbbc->addRule('url', ['allow_in' => ['listitem', 'block', 'columns', 'inline'], 'class' => "link", 'content' => Nbbc::BBCODE_REQUIRED, 'method' => [$this, 'doURL'], 'mode' => Nbbc::BBCODE_MODE_CALLBACK, 'plain_content' => ['_content', '_default'], 'plain_end' => "</a>", 'plain_link' => ['_default', '_content'], 'plain_start' => "<a rel=\"nofollow\" href=\"{\$link}\">"]);
         $nbbc->addRule('video', ['after_endtag' => "sns", 'after_tag' => "sns", 'allow_in' => ['listitem', 'block', 'columns'], 'before_endtag' => "sns", 'before_tag' => "sns", 'method' => [$this, 'doVideo'], 'mode' => Nbbc::BBCODE_MODE_CALLBACK, 'plain_end' => "\n", 'plain_start' => "\n<b>Video:</b>\n"]);
         $nbbc->addRule('youtube', ['allow_in' => ['listitem', 'block', 'columns', 'inline'], 'class' => "link", 'content' => Nbbc::BBCODE_REQUIRED, 'method' => [$this, 'doYouTube'], 'mode' => Nbbc::BBCODE_MODE_CALLBACK, 'plain_content' => ['_content', '_default'], 'plain_end' => "\n", 'plain_link' => ['_default', '_content'], 'plain_start' => "\n<b>Video:</b>\n"]);
         // Prevent unsupported tags from displaying
         $nbbc->addRule('table', []);
         $nbbc->addRule('tr', []);
         $nbbc->addRule('td', []);
         $this->EventArguments['BBCode'] = $nbbc;
         $this->fireEvent('AfterBBCodeSetup');
         $this->nbbc = $nbbc;
     }
     return $this->nbbc;
 }