本文整理汇总了PHP中HTMLPurifier_Config::create方法的典型用法代码示例。如果您正苦于以下问题:PHP HTMLPurifier_Config::create方法的具体用法?PHP HTMLPurifier_Config::create怎么用?PHP HTMLPurifier_Config::create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HTMLPurifier_Config
的用法示例。
在下文中一共展示了HTMLPurifier_Config::create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testLineNumbers
function testLineNumbers()
{
// . . . . . . . . . .
// 01234567890123 01234567890123 0123456789012345 0123456789012 012345
$html = "<b>Line 1</b>\n<i>Line 2</i>\nStill Line 2<br\n/>Now Line 4\n\n<br />";
$expect = array(0 => new HTMLPurifier_Token_Start('b'), 1 => new HTMLPurifier_Token_Text('Line 1'), 2 => new HTMLPurifier_Token_End('b'), 3 => new HTMLPurifier_Token_Text("\n"), 4 => new HTMLPurifier_Token_Start('i'), 5 => new HTMLPurifier_Token_Text('Line 2'), 6 => new HTMLPurifier_Token_End('i'), 7 => new HTMLPurifier_Token_Text("\nStill Line 2"), 8 => new HTMLPurifier_Token_Empty('br'), 9 => new HTMLPurifier_Token_Text("Now Line 4\n\n"), 10 => new HTMLPurifier_Token_Empty('br'));
$context = new HTMLPurifier_Context();
$config = HTMLPurifier_Config::createDefault();
$output = $this->DirectLex->tokenizeHTML($html, $config, $context);
$this->assertIdentical($output, $expect);
$context = new HTMLPurifier_Context();
$config = HTMLPurifier_Config::create(array('Core.MaintainLineNumbers' => true));
$expect[0]->position(1, 0);
$expect[1]->position(1, 3);
$expect[2]->position(1, 9);
$expect[3]->position(2, -1);
$expect[4]->position(2, 0);
$expect[5]->position(2, 3);
$expect[6]->position(2, 9);
$expect[7]->position(3, -1);
$expect[8]->position(3, 12);
$expect[9]->position(4, 2);
$expect[10]->position(6, 0);
$output = $this->DirectLex->tokenizeHTML($html, $config, $context);
$this->assertIdentical($output, $expect);
}
示例2: prepareCommon
/**
* Accepts config and context and prepares them into a valid state
* @param &$config Reference to config variable
* @param &$context Reference to context variable
*/
protected function prepareCommon(&$config, &$context)
{
$config = HTMLPurifier_Config::create($config);
if (!$context) {
$context = new HTMLPurifier_Context();
}
}
示例3: test_setup
public function test_setup()
{
$i = 0;
// counter, helps us isolate expectations
// initialize partial mock
$module = new HTMLPurifier_HTMLModule_Tidy_TestForConstruct();
$module->fixesForLevel['light'] = array('light-fix-1', 'light-fix-2');
$module->fixesForLevel['medium'] = array('medium-fix-1', 'medium-fix-2');
$module->fixesForLevel['heavy'] = array('heavy-fix-1', 'heavy-fix-2');
$j = 0;
$fixes = array('light-fix-1' => $lf1 = $j++, 'light-fix-2' => $lf2 = $j++, 'medium-fix-1' => $mf1 = $j++, 'medium-fix-2' => $mf2 = $j++, 'heavy-fix-1' => $hf1 = $j++, 'heavy-fix-2' => $hf2 = $j++);
$module->setReturnValue('makeFixes', $fixes);
$config = HTMLPurifier_Config::create(array('HTML.TidyLevel' => 'none'));
$module->expectAt($i++, 'populate', array(array()));
$module->setup($config);
// basic levels
$config = HTMLPurifier_Config::create(array('HTML.TidyLevel' => 'light'));
$module->expectAt($i++, 'populate', array(array('light-fix-1' => $lf1, 'light-fix-2' => $lf2)));
$module->setup($config);
$config = HTMLPurifier_Config::create(array('HTML.TidyLevel' => 'heavy'));
$module->expectAt($i++, 'populate', array(array('light-fix-1' => $lf1, 'light-fix-2' => $lf2, 'medium-fix-1' => $mf1, 'medium-fix-2' => $mf2, 'heavy-fix-1' => $hf1, 'heavy-fix-2' => $hf2)));
$module->setup($config);
// fine grained tuning
$config = HTMLPurifier_Config::create(array('HTML.TidyLevel' => 'none', 'HTML.TidyAdd' => array('light-fix-1', 'medium-fix-1')));
$module->expectAt($i++, 'populate', array(array('light-fix-1' => $lf1, 'medium-fix-1' => $mf1)));
$module->setup($config);
$config = HTMLPurifier_Config::create(array('HTML.TidyLevel' => 'medium', 'HTML.TidyRemove' => array('light-fix-1', 'medium-fix-1')));
$module->expectAt($i++, 'populate', array(array('light-fix-2' => $lf2, 'medium-fix-2' => $mf2)));
$module->setup($config);
}
示例4: generateEnLanguage
protected function generateEnLanguage()
{
$factory = HTMLPurifier_LanguageFactory::instance();
$config = HTMLPurifier_Config::create(array('Core.Language' => 'en'));
$context = new HTMLPurifier_Context();
return $factory->create($config, $context);
}
示例5: test
function test()
{
generate_mock_once('HTMLPurifier_URIScheme');
$config = HTMLPurifier_Config::create(array('URI.AllowedSchemes' => 'http, telnet', 'URI.OverrideAllowedSchemes' => true));
$context = new HTMLPurifier_Context();
$registry = new HTMLPurifier_URISchemeRegistry();
$this->assertIsA($registry->getScheme('http', $config, $context), 'HTMLPurifier_URIScheme_http');
$scheme_http = new HTMLPurifier_URISchemeMock();
$scheme_telnet = new HTMLPurifier_URISchemeMock();
$scheme_foobar = new HTMLPurifier_URISchemeMock();
// register a new scheme
$registry->register('telnet', $scheme_telnet);
$this->assertIdentical($registry->getScheme('telnet', $config, $context), $scheme_telnet);
// overload a scheme, this is FINAL (forget about defaults)
$registry->register('http', $scheme_http);
$this->assertIdentical($registry->getScheme('http', $config, $context), $scheme_http);
// when we register a scheme, it's automatically allowed
$registry->register('foobar', $scheme_foobar);
$this->assertIdentical($registry->getScheme('foobar', $config, $context), $scheme_foobar);
// now, test when overriding is not allowed
$config = HTMLPurifier_Config::create(array('URI.AllowedSchemes' => 'http, telnet', 'URI.OverrideAllowedSchemes' => false));
$this->assertNull($registry->getScheme('foobar', $config, $context));
// scheme not allowed and never registered
$this->assertNull($registry->getScheme('ftp', $config, $context));
}
示例6: process
/**
* Passes markup through HTMLPurifier making it safe to output to end user
*
* @param string $content
* @param array|null $config
* @return string
*/
public static function process($content, $config = null)
{
$configInstance = \HTMLPurifier_Config::create($config);
$configInstance->autoFinalize = false;
$purifier = \HTMLPurifier::instance($configInstance);
$purifier->config->set('Cache.SerializerPath', \Yii::$app->getRuntimePath());
return $purifier->purify($content);
}
示例7: purifyHTML
/**
* @param string $html
* @param array $config
* @return string
*/
protected function purifyHTML($html, $config)
{
$configInstance = \HTMLPurifier_Config::create($config);
$configInstance->autoFinalize = false;
$purifier = \HTMLPurifier::instance($configInstance);
$purifier->config->set('Cache.SerializerPath', $this->tmpPath);
return $purifier->purify($html);
}
示例8: __construct
public function __construct($root_dir)
{
$config = \HTMLPurifier_Config::create(array('Cache.SerializerPath' => $root_dir));
$def = $config->getHTMLDefinition(true);
$def->addAttribute('a', 'data-code', 'Text');
$this->purifier_service = new \HTMLPurifier($config);
$this->markdown_service = new \Parsedown();
}
示例9: setup
public function setup()
{
$this->config = HTMLPurifier_Config::create(array('Core.CollectErrors' => true));
$this->context = new HTMLPurifier_Context();
generate_mock_once('HTMLPurifier_ErrorCollector');
$this->collector = new HTMLPurifier_ErrorCollectorEMock();
$this->collector->prepare($this->context);
$this->context->register('ErrorCollector', $this->collector);
$this->callCount = 0;
}
示例10: process
/**
* Passes markup through HTMLPurifier making it safe to output to end user
*
* @param string $content The HTML content to purify
* @param array|\Closure|null $config The config to use for HtmlPurifier.
* If not specified or `null` the default config will be used.
* You can use an array or an anonymous function to provide configuration options:
*
* - An array will be passed to the `HTMLPurifier_Config::create()` method.
* - An anonymous function will be called after the config was created.
* The signature should be: `function($config)` where `$config` will be an
* instance of `HTMLPurifier_Config`.
*
* Here is a usage example of such a function:
*
* ~~~
* // Allow the HTML5 data attribute `data-type` on `img` elements.
* $content = HtmlPurifier::process($content, function ($config) {
* $config->getHTMLDefinition(true)
* ->addAttribute('img', 'data-type', 'Text');
* });
* ~~~
*
* @return string the purified HTML content.
*/
public static function process($content, $config = null)
{
$configInstance = \HTMLPurifier_Config::create($config instanceof \Closure ? null : $config);
$configInstance->autoFinalize = false;
$purifier = \HTMLPurifier::instance($configInstance);
$purifier->config->set('Cache.SerializerPath', Application::$app->getRuntimePath());
if ($config instanceof \Closure) {
call_user_func($config, $configInstance);
}
return $purifier->purify($content);
}
示例11: truncateHtml
protected static function truncateHtml($string, $count, $suffix, $wordsPerLine, $encoding)
{
$config = \HTMLPurifier_Config::create(null);
$config->set('Cache.SerializerPath', \Yii::$app->getRuntimePath());
$lexer = \HTMLPurifier_Lexer::create($config);
$tokens = $lexer->tokenizeHTML($string, $config, null);
$openTokens = 0;
$totalCount = 0;
$truncated = [];
foreach ($tokens as $token) {
if ($token instanceof \HTMLPurifier_Token_Start) {
//Tag begins
$openTokens++;
$truncated[] = $token;
} elseif ($token instanceof \HTMLPurifier_Token_Text && $totalCount <= $count) {
//Text
if (false === $encoding) {
$token->data = self::truncateWords($token->data, ($count - $totalCount) * $wordsPerLine, '');
$currentWords = str_word_count($token->data);
} else {
$token->data = self::truncate($token->data, ($count - $totalCount) * $wordsPerLine, '', $encoding) . ' ';
$currentWords = mb_strlen($token->data, $encoding);
}
//$totalCount += $currentWords;
if (!$token->is_whitespace) {
$totalCount += intval(ceil($currentWords / $wordsPerLine));
}
//turn into lines
if (1 === $currentWords) {
$token->data = ' ' . $token->data;
}
$truncated[] = $token;
} elseif ($token instanceof \HTMLPurifier_Token_End) {
//Tag ends
$openTokens--;
$truncated[] = $token;
} elseif ($token instanceof \HTMLPurifier_Token_Empty) {
//Self contained tags, i.e. <img/> etc.
if ($token->name == 'img') {
//filter img tag
} else {
$truncated[] = $token;
}
}
if (0 === $openTokens && $totalCount >= $count) {
break;
}
}
$context = new \HTMLPurifier_Context();
$generator = new \HTMLPurifier_Generator($config, $context);
return $generator->generateFromTokens($truncated) . $suffix;
}
示例12: testAllowedModules
public function testAllowedModules()
{
$manager = new HTMLPurifier_HTMLModuleManager();
$manager->doctypes->register('Fantasy Inventory 1.0', true, array('Weapons', 'Magic'));
// register these modules so it doesn't blow up
$weapons_module = new HTMLPurifier_HTMLModule();
$weapons_module->name = 'Weapons';
$manager->registerModule($weapons_module);
$magic_module = new HTMLPurifier_HTMLModule();
$magic_module->name = 'Magic';
$manager->registerModule($magic_module);
$config = HTMLPurifier_Config::create(array('HTML.CustomDoctype' => 'Fantasy Inventory 1.0', 'HTML.AllowedModules' => 'Weapons'));
$manager->setup($config);
$this->assertTrue(isset($manager->modules['Weapons']));
$this->assertFalse(isset($manager->modules['Magic']));
}
示例13: phorum_htmlpurifier_get_config
/**
* Initializes the appropriate configuration from either a PHP file
* or a module configuration value
* @return Instance of HTMLPurifier_Config
*/
function phorum_htmlpurifier_get_config()
{
global $PHORUM;
$config_exists = phorum_htmlpurifier_config_file_exists();
if ($config_exists || !isset($PHORUM['mod_htmlpurifier']['config'])) {
$config = HTMLPurifier_Config::createDefault();
include dirname(__FILE__) . '/config.default.php';
if ($config_exists) {
include dirname(__FILE__) . '/config.php';
}
unset($PHORUM['mod_htmlpurifier']['config']);
// unnecessary
} else {
$config = HTMLPurifier_Config::create($PHORUM['mod_htmlpurifier']['config']);
}
return $config;
}
示例14: purify
public static function purify($var)
{
$external = Ajde_Core_ExternalLibs::getInstance();
if ($external->has('HTMLPurifier')) {
$purifier = $external->get('HTMLPurifier');
/* @var $purifier HTMLPurifier */
$config = HTMLPurifier_Config::createDefault();
$config->set('AutoFormat.AutoParagraph', true);
$config->set('AutoFormat.DisplayLinkURI', false);
$config->set('AutoFormat.Linkify', false);
$config->set('AutoFormat.RemoveEmpty', true);
$config->set('AutoFormat.RemoveSpansWithoutAttributes', true);
$config->set('CSS.AllowedProperties', '');
$config->set('HTML.Doctype', 'XHTML 1.0 Strict');
$config->set('URI.DisableExternalResources', true);
$purifier->config = HTMLPurifier_Config::create($config);
return $purifier->purify($var);
} else {
return self::clean($var);
}
}
示例15: truncateHtml
/**
* Truncate a string while preserving the HTML.
*
* @param string $string The string to truncate
* @param integer $count
* @param string $suffix String to append to the end of the truncated string.
* @param string|boolean $encoding
* @return string
* @since 2.0.1
*/
protected static function truncateHtml($string, $count, $suffix, $encoding = false)
{
$config = \HTMLPurifier_Config::create(null);
$lexer = \HTMLPurifier_Lexer::create($config);
$tokens = $lexer->tokenizeHTML($string, $config, null);
$openTokens = 0;
$totalCount = 0;
$truncated = [];
foreach ($tokens as $token) {
if ($token instanceof \HTMLPurifier_Token_Start) {
//Tag begins
$openTokens++;
$truncated[] = $token;
} else {
if ($token instanceof \HTMLPurifier_Token_Text && $totalCount <= $count) {
//Text
if (false === $encoding) {
$token->data = self::truncateWords($token->data, $count - $totalCount, '');
$currentCount = str_word_count($token->data);
} else {
$token->data = self::truncate($token->data, $count - $totalCount, '', $encoding) . ' ';
$currentCount = mb_strlen($token->data, $encoding);
}
$totalCount += $currentCount;
if (1 === $currentCount) {
$token->data = ' ' . $token->data;
}
$truncated[] = $token;
} else {
if ($token instanceof \HTMLPurifier_Token_End) {
//Tag ends
$openTokens--;
$truncated[] = $token;
} else {
if ($token instanceof \HTMLPurifier_Token_Empty) {
//Self contained tags, i.e. <img/> etc.
$truncated[] = $token;
}
}
}
}
if (0 === $openTokens && $totalCount >= $count) {
break;
}
}
$context = new \HTMLPurifier_Context();
$generator = new \HTMLPurifier_Generator($config, $context);
return $generator->generateFromTokens($truncated) . $suffix;
}