Add %HTML.TargetNoreferrer, which adds rel="noreferrer" when target attribute is set
[htmlpurifier.git] / tests / HTMLPurifier / ComplexHarness.php
blob55c10ca9df1d2a77584d87dae17fdbc0fc5fbde5
1 <?php
3 /**
4 * General-purpose test-harness that makes testing functions that require
5 * configuration and context objects easier when those two parameters are
6 * meaningless. See HTMLPurifier_ChildDefTest for a good example of usage.
7 */
8 class HTMLPurifier_ComplexHarness extends HTMLPurifier_Harness
11 /**
12 * Instance of the object that will execute the method.
13 * @type object
15 protected $obj;
17 /**
18 * Name of the function to be executed.
19 * @type string
21 protected $func;
23 /**
24 * Whether or not the method deals in tokens.
25 * If set to true, assertResult()
26 * will transparently convert HTML to and back from tokens.
27 * @type bool
29 protected $to_tokens = false;
31 /**
32 * Whether or not the method deals in a node list.
33 * If set to true, assertResult() will transparently convert HTML
34 * to and back from node.
35 * @type bool
37 protected $to_node_list = false;
39 /**
40 * Whether or not to convert tokens back into HTML before performing
41 * equality check, has no effect on bools.
42 * @type bool
44 protected $to_html = false;
46 /**
47 * Instance of an HTMLPurifier_Lexer implementation.
48 * @type HTMLPurifier_Lexer
50 protected $lexer;
52 public function __construct()
54 $this->lexer = new HTMLPurifier_Lexer_DirectLex();
55 parent::__construct();
58 /**
59 * Asserts a specific result from a one parameter + config/context function
60 * @param string $input Input parameter
61 * @param bool|string $expect Expectation
63 protected function assertResult($input, $expect = true)
65 // $func may cause $input to change, so "clone" another copy
66 // to sacrifice
67 if ($this->to_node_list && is_string($input)) {
68 $input = HTMLPurifier_Arborize::arborize($this->tokenize($temp = $input), $this->config, $this->context)->children;
69 $input_c = HTMLPurifier_Arborize::arborize($this->tokenize($temp), $this->config, $this->context)->children;
70 } elseif ($this->to_tokens && is_string($input)) {
71 $input = $this->tokenize($temp = $input);
72 $input_c = $this->tokenize($temp);
73 } else {
74 $input_c = $input;
77 // call the function
78 $func = $this->func;
79 $result = $this->obj->$func($input_c, $this->config, $this->context);
81 // test a bool result
82 if (is_bool($result)) {
83 $this->assertIdentical($expect, $result);
84 return;
85 } elseif (is_bool($expect)) {
86 $expect = $input;
89 if ($this->to_html) {
90 if ($this->to_node_list) {
91 $result = $this->generateTokens($result);
92 if (is_array($expect) && !empty($expect) && $expect[0] instanceof HTMLPurifier_Node) {
93 $expect = $this->generateTokens($expect);
96 $result = $this->generate($result);
97 if (is_array($expect)) {
98 $expect = $this->generate($expect);
101 $this->assertIdentical($expect, $result);
103 if ($expect !== $result) {
104 echo '<pre>' . var_dump($result) . '</pre>';
110 * Tokenize HTML into tokens, uses member variables for common variables
112 protected function tokenize($html)
114 return $this->lexer->tokenizeHTML($html, $this->config, $this->context);
118 * Generate textual HTML from tokens
120 protected function generate($tokens)
122 $generator = new HTMLPurifier_Generator($this->config, $this->context);
123 return $generator->generateFromTokens($tokens);
127 * Generate tokens from node list
129 protected function generateTokens($children)
131 $dummy = new HTMLPurifier_Node_Element("dummy");
132 $dummy->children = $children;
133 return HTMLPurifier_Arborize::flatten($dummy, $this->context, $this->config);
138 // vim: et sw=4 sts=4