You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.5KB

  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. }