Arborize.php 2.5 KB

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