PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/school/605.441.Databases/project/htmlpurifier/tests/HTMLPurifier/TagTransformTest.php

https://github.com/hank/life
PHP | 175 lines | 109 code | 28 blank | 38 comment | 0 complexity | b0fc1179cb60bea6425ba45c9b9ca7a4 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, Apache-2.0
  1. <?php
  2. // needs to be seperated into files
  3. class HTMLPurifier_TagTransformTest extends HTMLPurifier_Harness
  4. {
  5. /**
  6. * Asserts that a transformation happens
  7. *
  8. * This assertion performs several tests on the transform:
  9. *
  10. * -# Transforms a start tag with only $name and no attributes
  11. * -# Transforms a start tag with $name and $attributes
  12. * -# Transform an end tag
  13. * -# Transform an empty tag with only $name and no attributes
  14. * -# Transform an empty tag with $name and $attributes
  15. *
  16. * In its current form, it assumes that start and empty tags would be
  17. * treated the same, and is really ensuring that the tag transform doesn't
  18. * do anything wonky to the tag type.
  19. *
  20. * @param $transformer HTMLPurifier_TagTransform class to test
  21. * @param $name Name of the original tag
  22. * @param $attributes Attributes of the original tag
  23. * @param $expect_name Name of output tag
  24. * @param $expect_attributes Attributes of output tag when $attributes
  25. * is included.
  26. * @param $expect_added_attributes Attributes of output tag when $attributes
  27. * are omitted.
  28. * @param $config_array Configuration array for HTMLPurifier_Config
  29. * @param $context_array Context array for HTMLPurifier_Context
  30. */
  31. protected function assertTransformation($transformer,
  32. $name, $attributes,
  33. $expect_name, $expect_attributes,
  34. $expect_added_attributes = array(),
  35. $config_array = array(), $context_array = array()) {
  36. $config = HTMLPurifier_Config::createDefault();
  37. $config->loadArray($config_array);
  38. $context = new HTMLPurifier_Context();
  39. $context->loadArray($context_array);
  40. // start tag transform
  41. $this->assertIdentical(
  42. new HTMLPurifier_Token_Start($expect_name, $expect_added_attributes),
  43. $transformer->transform(
  44. new HTMLPurifier_Token_Start($name), $config, $context)
  45. );
  46. // start tag transform with attributes
  47. $this->assertIdentical(
  48. new HTMLPurifier_Token_Start($expect_name, $expect_attributes),
  49. $transformer->transform(
  50. new HTMLPurifier_Token_Start($name, $attributes),
  51. $config, $context
  52. )
  53. );
  54. // end tag transform
  55. $this->assertIdentical(
  56. new HTMLPurifier_Token_End($expect_name),
  57. $transformer->transform(
  58. new HTMLPurifier_Token_End($name), $config, $context
  59. )
  60. );
  61. // empty tag transform
  62. $this->assertIdentical(
  63. new HTMLPurifier_Token_Empty($expect_name, $expect_added_attributes),
  64. $transformer->transform(
  65. new HTMLPurifier_Token_Empty($name), $config, $context
  66. )
  67. );
  68. // empty tag transform with attributes
  69. $this->assertIdentical(
  70. new HTMLPurifier_Token_Empty($expect_name, $expect_attributes),
  71. $transformer->transform(
  72. new HTMLPurifier_Token_Empty($name, $attributes),
  73. $config, $context
  74. )
  75. );
  76. }
  77. function testSimple() {
  78. $transformer = new HTMLPurifier_TagTransform_Simple('ul');
  79. $this->assertTransformation(
  80. $transformer,
  81. 'menu', array('class' => 'boom'),
  82. 'ul', array('class' => 'boom')
  83. );
  84. }
  85. function testSimpleWithCSS() {
  86. $transformer = new HTMLPurifier_TagTransform_Simple('div', 'text-align:center;');
  87. $this->assertTransformation(
  88. $transformer,
  89. 'center', array('class' => 'boom', 'style'=>'font-weight:bold;'),
  90. 'div', array('class' => 'boom', 'style'=>'text-align:center;font-weight:bold;'),
  91. array('style'=>'text-align:center;')
  92. );
  93. // test special case, uppercase attribute key
  94. $this->assertTransformation(
  95. $transformer,
  96. 'center', array('STYLE'=>'font-weight:bold;'),
  97. 'div', array('style'=>'text-align:center;font-weight:bold;'),
  98. array('style'=>'text-align:center;')
  99. );
  100. }
  101. protected function assertSizeToStyle($transformer, $size, $style) {
  102. $this->assertTransformation(
  103. $transformer,
  104. 'font', array('size' => $size),
  105. 'span', array('style' => 'font-size:' . $style . ';')
  106. );
  107. }
  108. function testFont() {
  109. $transformer = new HTMLPurifier_TagTransform_Font();
  110. // test a font-face transformation
  111. $this->assertTransformation(
  112. $transformer,
  113. 'font', array('face' => 'Arial'),
  114. 'span', array('style' => 'font-family:Arial;')
  115. );
  116. // test a color transformation
  117. $this->assertTransformation(
  118. $transformer,
  119. 'font', array('color' => 'red'),
  120. 'span', array('style' => 'color:red;')
  121. );
  122. // test the size transforms
  123. $this->assertSizeToStyle($transformer, '0', 'xx-small');
  124. $this->assertSizeToStyle($transformer, '1', 'xx-small');
  125. $this->assertSizeToStyle($transformer, '2', 'small');
  126. $this->assertSizeToStyle($transformer, '3', 'medium');
  127. $this->assertSizeToStyle($transformer, '4', 'large');
  128. $this->assertSizeToStyle($transformer, '5', 'x-large');
  129. $this->assertSizeToStyle($transformer, '6', 'xx-large');
  130. $this->assertSizeToStyle($transformer, '7', '300%');
  131. $this->assertSizeToStyle($transformer, '-1', 'smaller');
  132. $this->assertSizeToStyle($transformer, '-2', '60%');
  133. $this->assertSizeToStyle($transformer, '-3', '60%');
  134. $this->assertSizeToStyle($transformer, '+1', 'larger');
  135. $this->assertSizeToStyle($transformer, '+2', '150%');
  136. $this->assertSizeToStyle($transformer, '+3', '200%');
  137. $this->assertSizeToStyle($transformer, '+4', '300%');
  138. $this->assertSizeToStyle($transformer, '+5', '300%');
  139. // test multiple transforms, the alphabetical ordering is important
  140. $this->assertTransformation(
  141. $transformer,
  142. 'font', array('color' => 'red', 'face' => 'Arial', 'size' => '6'),
  143. 'span', array('style' => 'color:red;font-family:Arial;font-size:xx-large;')
  144. );
  145. }
  146. }
  147. // vim: et sw=4 sts=4