]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/3rdparty/htmlpurifier/HTMLPurifier/Arborize.php
remove autoload section in composer.json
[github/wallabag/wallabag.git] / inc / 3rdparty / htmlpurifier / HTMLPurifier / Arborize.php
CommitLineData
d4949327
NL
1<?php\r
2\r
3/**\r
4 * Converts a stream of HTMLPurifier_Token into an HTMLPurifier_Node,\r
5 * and back again.\r
6 *\r
7 * @note This transformation is not an equivalence. We mutate the input\r
8 * token stream to make it so; see all [MUT] markers in code.\r
9 */\r
10class HTMLPurifier_Arborize\r
11{\r
12 public static function arborize($tokens, $config, $context) {\r
13 $definition = $config->getHTMLDefinition();\r
14 $parent = new HTMLPurifier_Token_Start($definition->info_parent);\r
15 $stack = array($parent->toNode());\r
16 foreach ($tokens as $token) {\r
17 $token->skip = null; // [MUT]\r
18 $token->carryover = null; // [MUT]\r
19 if ($token instanceof HTMLPurifier_Token_End) {\r
20 $token->start = null; // [MUT]\r
21 $r = array_pop($stack);\r
22 assert($r->name === $token->name);\r
23 assert(empty($token->attr));\r
24 $r->endCol = $token->col;\r
25 $r->endLine = $token->line;\r
26 $r->endArmor = $token->armor;\r
27 continue;\r
28 }\r
29 $node = $token->toNode();\r
30 $stack[count($stack)-1]->children[] = $node;\r
31 if ($token instanceof HTMLPurifier_Token_Start) {\r
32 $stack[] = $node;\r
33 }\r
34 }\r
35 assert(count($stack) == 1);\r
36 return $stack[0];\r
37 }\r
38\r
39 public static function flatten($node, $config, $context) {\r
40 $level = 0;\r
41 $nodes = array($level => new HTMLPurifier_Queue(array($node)));\r
42 $closingTokens = array();\r
43 $tokens = array();\r
44 do {\r
45 while (!$nodes[$level]->isEmpty()) {\r
46 $node = $nodes[$level]->shift(); // FIFO\r
47 list($start, $end) = $node->toTokenPair();\r
48 if ($level > 0) {\r
49 $tokens[] = $start;\r
50 }\r
51 if ($end !== NULL) {\r
52 $closingTokens[$level][] = $end;\r
53 }\r
54 if ($node instanceof HTMLPurifier_Node_Element) {\r
55 $level++;\r
56 $nodes[$level] = new HTMLPurifier_Queue();\r
57 foreach ($node->children as $childNode) {\r
58 $nodes[$level]->push($childNode);\r
59 }\r
60 }\r
61 }\r
62 $level--;\r
63 if ($level && isset($closingTokens[$level])) {\r
64 while ($token = array_pop($closingTokens[$level])) {\r
65 $tokens[] = $token;\r
66 }\r
67 }\r
68 } while ($level > 0);\r
69 return $tokens;\r
70 }\r
71}\r