HtmlTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. <?php
  2. class HtmlTest extends MediaWikiTestCase {
  3. private $restoreWarnings;
  4. protected function setUp() {
  5. parent::setUp();
  6. $this->setMwGlobals( [
  7. 'wgUseMediaWikiUIEverywhere' => false,
  8. ] );
  9. $langObj = Language::factory( 'en' );
  10. // Hardcode namespaces during test runs,
  11. // so that html output based on existing namespaces
  12. // can be properly evaluated.
  13. $langObj->setNamespaces( [
  14. -2 => 'Media',
  15. -1 => 'Special',
  16. 0 => '',
  17. 1 => 'Talk',
  18. 2 => 'User',
  19. 3 => 'User_talk',
  20. 4 => 'MyWiki',
  21. 5 => 'MyWiki_Talk',
  22. 6 => 'File',
  23. 7 => 'File_talk',
  24. 8 => 'MediaWiki',
  25. 9 => 'MediaWiki_talk',
  26. 10 => 'Template',
  27. 11 => 'Template_talk',
  28. 14 => 'Category',
  29. 15 => 'Category_talk',
  30. 100 => 'Custom',
  31. 101 => 'Custom_talk',
  32. ] );
  33. $this->setUserLang( $langObj );
  34. $this->setContentLang( $langObj );
  35. $this->restoreWarnings = false;
  36. }
  37. protected function tearDown() {
  38. if ( $this->restoreWarnings ) {
  39. $this->restoreWarnings = false;
  40. Wikimedia\restoreWarnings();
  41. }
  42. parent::tearDown();
  43. }
  44. /**
  45. * @covers Html::element
  46. * @covers Html::rawElement
  47. * @covers Html::openElement
  48. * @covers Html::closeElement
  49. */
  50. public function testElementBasics() {
  51. $this->assertEquals(
  52. '<img/>',
  53. Html::element( 'img', null, '' ),
  54. 'Self-closing tag for short-tag elements'
  55. );
  56. $this->assertEquals(
  57. '<element></element>',
  58. Html::element( 'element', null, null ),
  59. 'Close tag for empty element (null, null)'
  60. );
  61. $this->assertEquals(
  62. '<element></element>',
  63. Html::element( 'element', [], '' ),
  64. 'Close tag for empty element (array, string)'
  65. );
  66. }
  67. public function dataXmlMimeType() {
  68. return [
  69. // ( $mimetype, $isXmlMimeType )
  70. # HTML is not an XML MimeType
  71. [ 'text/html', false ],
  72. # XML is an XML MimeType
  73. [ 'text/xml', true ],
  74. [ 'application/xml', true ],
  75. # XHTML is an XML MimeType
  76. [ 'application/xhtml+xml', true ],
  77. # Make sure other +xml MimeTypes are supported
  78. # SVG is another random MimeType even though we don't use it
  79. [ 'image/svg+xml', true ],
  80. # Complete random other MimeTypes are not XML
  81. [ 'text/plain', false ],
  82. ];
  83. }
  84. /**
  85. * @dataProvider dataXmlMimeType
  86. * @covers Html::isXmlMimeType
  87. */
  88. public function testXmlMimeType( $mimetype, $isXmlMimeType ) {
  89. $this->assertEquals( $isXmlMimeType, Html::isXmlMimeType( $mimetype ) );
  90. }
  91. /**
  92. * @covers Html::expandAttributes
  93. */
  94. public function testExpandAttributesSkipsNullAndFalse() {
  95. # ## EMPTY ########
  96. $this->assertEmpty(
  97. Html::expandAttributes( [ 'foo' => null ] ),
  98. 'skip keys with null value'
  99. );
  100. $this->assertEmpty(
  101. Html::expandAttributes( [ 'foo' => false ] ),
  102. 'skip keys with false value'
  103. );
  104. $this->assertEquals(
  105. ' foo=""',
  106. Html::expandAttributes( [ 'foo' => '' ] ),
  107. 'keep keys with an empty string'
  108. );
  109. }
  110. /**
  111. * @covers Html::expandAttributes
  112. */
  113. public function testExpandAttributesForBooleans() {
  114. $this->assertEquals(
  115. '',
  116. Html::expandAttributes( [ 'selected' => false ] ),
  117. 'Boolean attributes do not generates output when value is false'
  118. );
  119. $this->assertEquals(
  120. '',
  121. Html::expandAttributes( [ 'selected' => null ] ),
  122. 'Boolean attributes do not generates output when value is null'
  123. );
  124. $this->assertEquals(
  125. ' selected=""',
  126. Html::expandAttributes( [ 'selected' => true ] ),
  127. 'Boolean attributes have no value when value is true'
  128. );
  129. $this->assertEquals(
  130. ' selected=""',
  131. Html::expandAttributes( [ 'selected' ] ),
  132. 'Boolean attributes have no value when value is true (passed as numerical array)'
  133. );
  134. }
  135. /**
  136. * @covers Html::expandAttributes
  137. */
  138. public function testExpandAttributesForNumbers() {
  139. $this->assertEquals(
  140. ' value="1"',
  141. Html::expandAttributes( [ 'value' => 1 ] ),
  142. 'Integer value is cast to a string'
  143. );
  144. $this->assertEquals(
  145. ' value="1.1"',
  146. Html::expandAttributes( [ 'value' => 1.1 ] ),
  147. 'Float value is cast to a string'
  148. );
  149. }
  150. /**
  151. * @covers Html::expandAttributes
  152. */
  153. public function testExpandAttributesForObjects() {
  154. $this->assertEquals(
  155. ' value="stringValue"',
  156. Html::expandAttributes( [ 'value' => new HtmlTestValue() ] ),
  157. 'Object value is converted to a string'
  158. );
  159. }
  160. /**
  161. * Test for Html::expandAttributes()
  162. * Please note it output a string prefixed with a space!
  163. * @covers Html::expandAttributes
  164. */
  165. public function testExpandAttributesVariousExpansions() {
  166. # ## NOT EMPTY ####
  167. $this->assertEquals(
  168. ' empty_string=""',
  169. Html::expandAttributes( [ 'empty_string' => '' ] ),
  170. 'Empty string is always quoted'
  171. );
  172. $this->assertEquals(
  173. ' key="value"',
  174. Html::expandAttributes( [ 'key' => 'value' ] ),
  175. 'Simple string value needs no quotes'
  176. );
  177. $this->assertEquals(
  178. ' one="1"',
  179. Html::expandAttributes( [ 'one' => 1 ] ),
  180. 'Number 1 value needs no quotes'
  181. );
  182. $this->assertEquals(
  183. ' zero="0"',
  184. Html::expandAttributes( [ 'zero' => 0 ] ),
  185. 'Number 0 value needs no quotes'
  186. );
  187. }
  188. /**
  189. * Html::expandAttributes has special features for HTML
  190. * attributes that use space separated lists and also
  191. * allows arrays to be used as values.
  192. * @covers Html::expandAttributes
  193. */
  194. public function testExpandAttributesListValueAttributes() {
  195. # ## STRING VALUES
  196. $this->assertEquals(
  197. ' class="redundant spaces here"',
  198. Html::expandAttributes( [ 'class' => ' redundant spaces here ' ] ),
  199. 'Normalization should strip redundant spaces'
  200. );
  201. $this->assertEquals(
  202. ' class="foo bar"',
  203. Html::expandAttributes( [ 'class' => 'foo bar foo bar bar' ] ),
  204. 'Normalization should remove duplicates in string-lists'
  205. );
  206. # ## "EMPTY" ARRAY VALUES
  207. $this->assertEquals(
  208. ' class=""',
  209. Html::expandAttributes( [ 'class' => [] ] ),
  210. 'Value with an empty array'
  211. );
  212. $this->assertEquals(
  213. ' class=""',
  214. Html::expandAttributes( [ 'class' => [ null, '', ' ', ' ' ] ] ),
  215. 'Array with null, empty string and spaces'
  216. );
  217. # ## NON-EMPTY ARRAY VALUES
  218. $this->assertEquals(
  219. ' class="foo bar"',
  220. Html::expandAttributes( [ 'class' => [
  221. 'foo',
  222. 'bar',
  223. 'foo',
  224. 'bar',
  225. 'bar',
  226. ] ] ),
  227. 'Normalization should remove duplicates in the array'
  228. );
  229. $this->assertEquals(
  230. ' class="foo bar"',
  231. Html::expandAttributes( [ 'class' => [
  232. 'foo bar',
  233. 'bar foo',
  234. 'foo',
  235. 'bar bar',
  236. ] ] ),
  237. 'Normalization should remove duplicates in string-lists in the array'
  238. );
  239. }
  240. /**
  241. * Test feature added by r96188, let pass attributes values as
  242. * a PHP array. Restricted to class,rel, accesskey.
  243. * @covers Html::expandAttributes
  244. */
  245. public function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
  246. $this->assertEquals(
  247. ' class="booltrue one"',
  248. Html::expandAttributes( [ 'class' => [
  249. 'booltrue' => true,
  250. 'one' => 1,
  251. # Method use isset() internally, make sure we do discard
  252. # attributes values which have been assigned well known values
  253. 'emptystring' => '',
  254. 'boolfalse' => false,
  255. 'zero' => 0,
  256. 'null' => null,
  257. ] ] )
  258. );
  259. }
  260. /**
  261. * How do we handle duplicate keys in HTML attributes expansion?
  262. * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
  263. * The latter will take precedence.
  264. *
  265. * Feature added by r96188
  266. * @covers Html::expandAttributes
  267. */
  268. public function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
  269. $this->assertEquals(
  270. ' class=""',
  271. Html::expandAttributes( [ 'class' => [
  272. 'GREEN',
  273. 'GREEN' => false,
  274. 'GREEN',
  275. ] ] )
  276. );
  277. }
  278. /**
  279. * @covers Html::expandAttributes
  280. * @expectedException MWException
  281. */
  282. public function testExpandAttributes_ArrayOnNonListValueAttribute_ThrowsException() {
  283. // Real-life test case found in the Popups extension (see Gerrit cf0fd64),
  284. // when used with an outdated BetaFeatures extension (see Gerrit deda1e7)
  285. Html::expandAttributes( [
  286. 'src' => [
  287. 'ltr' => 'ltr.svg',
  288. 'rtl' => 'rtl.svg'
  289. ]
  290. ] );
  291. }
  292. /**
  293. * @covers Html::namespaceSelector
  294. * @covers Html::namespaceSelectorOptions
  295. */
  296. public function testNamespaceSelector() {
  297. $this->assertEquals(
  298. '<select id="namespace" name="namespace">' . "\n" .
  299. '<option value="0">(Main)</option>' . "\n" .
  300. '<option value="1">Talk</option>' . "\n" .
  301. '<option value="2">User</option>' . "\n" .
  302. '<option value="3">User talk</option>' . "\n" .
  303. '<option value="4">MyWiki</option>' . "\n" .
  304. '<option value="5">MyWiki Talk</option>' . "\n" .
  305. '<option value="6">File</option>' . "\n" .
  306. '<option value="7">File talk</option>' . "\n" .
  307. '<option value="8">MediaWiki</option>' . "\n" .
  308. '<option value="9">MediaWiki talk</option>' . "\n" .
  309. '<option value="10">Template</option>' . "\n" .
  310. '<option value="11">Template talk</option>' . "\n" .
  311. '<option value="14">Category</option>' . "\n" .
  312. '<option value="15">Category talk</option>' . "\n" .
  313. '<option value="100">Custom</option>' . "\n" .
  314. '<option value="101">Custom talk</option>' . "\n" .
  315. '</select>',
  316. Html::namespaceSelector(),
  317. 'Basic namespace selector without custom options'
  318. );
  319. $this->assertEquals(
  320. '<label for="mw-test-namespace">Select a namespace:</label>' . "\u{00A0}" .
  321. '<select id="mw-test-namespace" name="wpNamespace">' . "\n" .
  322. '<option value="all">all</option>' . "\n" .
  323. '<option value="0">(Main)</option>' . "\n" .
  324. '<option value="1">Talk</option>' . "\n" .
  325. '<option value="2" selected="">User</option>' . "\n" .
  326. '<option value="3">User talk</option>' . "\n" .
  327. '<option value="4">MyWiki</option>' . "\n" .
  328. '<option value="5">MyWiki Talk</option>' . "\n" .
  329. '<option value="6">File</option>' . "\n" .
  330. '<option value="7">File talk</option>' . "\n" .
  331. '<option value="8">MediaWiki</option>' . "\n" .
  332. '<option value="9">MediaWiki talk</option>' . "\n" .
  333. '<option value="10">Template</option>' . "\n" .
  334. '<option value="11">Template talk</option>' . "\n" .
  335. '<option value="14">Category</option>' . "\n" .
  336. '<option value="15">Category talk</option>' . "\n" .
  337. '<option value="100">Custom</option>' . "\n" .
  338. '<option value="101">Custom talk</option>' . "\n" .
  339. '</select>',
  340. Html::namespaceSelector(
  341. [ 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ],
  342. [ 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' ]
  343. ),
  344. 'Basic namespace selector with custom values'
  345. );
  346. $this->assertEquals(
  347. '<label for="namespace">Select a namespace:</label>' . "\u{00A0}" .
  348. '<select id="namespace" name="namespace">' . "\n" .
  349. '<option value="0">(Main)</option>' . "\n" .
  350. '<option value="1">Talk</option>' . "\n" .
  351. '<option value="2">User</option>' . "\n" .
  352. '<option value="3">User talk</option>' . "\n" .
  353. '<option value="4">MyWiki</option>' . "\n" .
  354. '<option value="5">MyWiki Talk</option>' . "\n" .
  355. '<option value="6">File</option>' . "\n" .
  356. '<option value="7">File talk</option>' . "\n" .
  357. '<option value="8">MediaWiki</option>' . "\n" .
  358. '<option value="9">MediaWiki talk</option>' . "\n" .
  359. '<option value="10">Template</option>' . "\n" .
  360. '<option value="11">Template talk</option>' . "\n" .
  361. '<option value="14">Category</option>' . "\n" .
  362. '<option value="15">Category talk</option>' . "\n" .
  363. '<option value="100">Custom</option>' . "\n" .
  364. '<option value="101">Custom talk</option>' . "\n" .
  365. '</select>',
  366. Html::namespaceSelector(
  367. [ 'label' => 'Select a namespace:' ]
  368. ),
  369. 'Basic namespace selector with a custom label but no id attribtue for the <select>'
  370. );
  371. }
  372. /**
  373. * @covers Html::namespaceSelector
  374. */
  375. public function testCanFilterOutNamespaces() {
  376. $this->assertEquals(
  377. '<select id="namespace" name="namespace">' . "\n" .
  378. '<option value="2">User</option>' . "\n" .
  379. '<option value="4">MyWiki</option>' . "\n" .
  380. '<option value="5">MyWiki Talk</option>' . "\n" .
  381. '<option value="6">File</option>' . "\n" .
  382. '<option value="7">File talk</option>' . "\n" .
  383. '<option value="8">MediaWiki</option>' . "\n" .
  384. '<option value="9">MediaWiki talk</option>' . "\n" .
  385. '<option value="10">Template</option>' . "\n" .
  386. '<option value="11">Template talk</option>' . "\n" .
  387. '<option value="14">Category</option>' . "\n" .
  388. '<option value="15">Category talk</option>' . "\n" .
  389. '</select>',
  390. Html::namespaceSelector(
  391. [ 'exclude' => [ 0, 1, 3, 100, 101 ] ]
  392. ),
  393. 'Namespace selector namespace filtering.'
  394. );
  395. }
  396. /**
  397. * @covers Html::namespaceSelector
  398. */
  399. public function testCanDisableANamespaces() {
  400. $this->assertEquals(
  401. '<select id="namespace" name="namespace">' . "\n" .
  402. '<option disabled="" value="0">(Main)</option>' . "\n" .
  403. '<option disabled="" value="1">Talk</option>' . "\n" .
  404. '<option disabled="" value="2">User</option>' . "\n" .
  405. '<option disabled="" value="3">User talk</option>' . "\n" .
  406. '<option disabled="" value="4">MyWiki</option>' . "\n" .
  407. '<option value="5">MyWiki Talk</option>' . "\n" .
  408. '<option value="6">File</option>' . "\n" .
  409. '<option value="7">File talk</option>' . "\n" .
  410. '<option value="8">MediaWiki</option>' . "\n" .
  411. '<option value="9">MediaWiki talk</option>' . "\n" .
  412. '<option value="10">Template</option>' . "\n" .
  413. '<option value="11">Template talk</option>' . "\n" .
  414. '<option value="14">Category</option>' . "\n" .
  415. '<option value="15">Category talk</option>' . "\n" .
  416. '<option value="100">Custom</option>' . "\n" .
  417. '<option value="101">Custom talk</option>' . "\n" .
  418. '</select>',
  419. Html::namespaceSelector( [
  420. 'disable' => [ 0, 1, 2, 3, 4 ]
  421. ] ),
  422. 'Namespace selector namespace disabling'
  423. );
  424. }
  425. /**
  426. * @dataProvider provideHtml5InputTypes
  427. * @covers Html::element
  428. */
  429. public function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
  430. $this->assertEquals(
  431. '<input type="' . $HTML5InputType . '"/>',
  432. Html::element( 'input', [ 'type' => $HTML5InputType ] ),
  433. 'In HTML5, Html::element() should accept type="' . $HTML5InputType . '"'
  434. );
  435. }
  436. /**
  437. * @covers Html::warningBox
  438. * @covers Html::messageBox
  439. */
  440. public function testWarningBox() {
  441. $this->assertEquals(
  442. Html::warningBox( 'warn' ),
  443. '<div class="warningbox">warn</div>'
  444. );
  445. }
  446. /**
  447. * @covers Html::errorBox
  448. * @covers Html::messageBox
  449. */
  450. public function testErrorBox() {
  451. $this->assertEquals(
  452. Html::errorBox( 'err' ),
  453. '<div class="errorbox">err</div>'
  454. );
  455. $this->assertEquals(
  456. Html::errorBox( 'err', 'heading' ),
  457. '<div class="errorbox"><h2>heading</h2>err</div>'
  458. );
  459. $this->assertEquals(
  460. Html::errorBox( 'err', '0' ),
  461. '<div class="errorbox"><h2>0</h2>err</div>'
  462. );
  463. }
  464. /**
  465. * @covers Html::successBox
  466. * @covers Html::messageBox
  467. */
  468. public function testSuccessBox() {
  469. $this->assertEquals(
  470. Html::successBox( 'great' ),
  471. '<div class="successbox">great</div>'
  472. );
  473. $this->assertEquals(
  474. Html::successBox( '<script>beware no escaping!</script>' ),
  475. '<div class="successbox"><script>beware no escaping!</script></div>'
  476. );
  477. }
  478. /**
  479. * List of input element types values introduced by HTML5
  480. * Full list at https://www.w3.org/TR/html-markup/input.html
  481. */
  482. public static function provideHtml5InputTypes() {
  483. $types = [
  484. 'datetime',
  485. 'datetime-local',
  486. 'date',
  487. 'month',
  488. 'time',
  489. 'week',
  490. 'number',
  491. 'range',
  492. 'email',
  493. 'url',
  494. 'search',
  495. 'tel',
  496. 'color',
  497. ];
  498. $cases = [];
  499. foreach ( $types as $type ) {
  500. $cases[] = [ $type ];
  501. }
  502. return $cases;
  503. }
  504. /**
  505. * Test out Html::element drops or enforces default value
  506. * @covers Html::dropDefaults
  507. * @dataProvider provideElementsWithAttributesHavingDefaultValues
  508. */
  509. public function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
  510. $this->assertEquals( $expected, Html::element( $element, $attribs ), $message );
  511. }
  512. public static function provideElementsWithAttributesHavingDefaultValues() {
  513. # Use cases in a concise format:
  514. # <expected>, <element name>, <array of attributes> [, <message>]
  515. # Will be mapped to Html::element()
  516. $cases = [];
  517. # ## Generic cases, match $attribDefault static array
  518. $cases[] = [ '<area/>',
  519. 'area', [ 'shape' => 'rect' ]
  520. ];
  521. $cases[] = [ '<button type="submit"></button>',
  522. 'button', [ 'formaction' => 'GET' ]
  523. ];
  524. $cases[] = [ '<button type="submit"></button>',
  525. 'button', [ 'formenctype' => 'application/x-www-form-urlencoded' ]
  526. ];
  527. $cases[] = [ '<canvas></canvas>',
  528. 'canvas', [ 'height' => '150' ]
  529. ];
  530. $cases[] = [ '<canvas></canvas>',
  531. 'canvas', [ 'width' => '300' ]
  532. ];
  533. # Also check with numeric values
  534. $cases[] = [ '<canvas></canvas>',
  535. 'canvas', [ 'height' => 150 ]
  536. ];
  537. $cases[] = [ '<canvas></canvas>',
  538. 'canvas', [ 'width' => 300 ]
  539. ];
  540. $cases[] = [ '<form></form>',
  541. 'form', [ 'action' => 'GET' ]
  542. ];
  543. $cases[] = [ '<form></form>',
  544. 'form', [ 'autocomplete' => 'on' ]
  545. ];
  546. $cases[] = [ '<form></form>',
  547. 'form', [ 'enctype' => 'application/x-www-form-urlencoded' ]
  548. ];
  549. $cases[] = [ '<input/>',
  550. 'input', [ 'formaction' => 'GET' ]
  551. ];
  552. $cases[] = [ '<input/>',
  553. 'input', [ 'type' => 'text' ]
  554. ];
  555. $cases[] = [ '<keygen/>',
  556. 'keygen', [ 'keytype' => 'rsa' ]
  557. ];
  558. $cases[] = [ '<link/>',
  559. 'link', [ 'media' => 'all' ]
  560. ];
  561. $cases[] = [ '<menu></menu>',
  562. 'menu', [ 'type' => 'list' ]
  563. ];
  564. $cases[] = [ '<script></script>',
  565. 'script', [ 'type' => 'text/javascript' ]
  566. ];
  567. $cases[] = [ '<style></style>',
  568. 'style', [ 'media' => 'all' ]
  569. ];
  570. $cases[] = [ '<style></style>',
  571. 'style', [ 'type' => 'text/css' ]
  572. ];
  573. $cases[] = [ '<textarea></textarea>',
  574. 'textarea', [ 'wrap' => 'soft' ]
  575. ];
  576. # ## SPECIFIC CASES
  577. # <link type="text/css">
  578. $cases[] = [ '<link/>',
  579. 'link', [ 'type' => 'text/css' ]
  580. ];
  581. # <input> specific handling
  582. $cases[] = [ '<input type="checkbox"/>',
  583. 'input', [ 'type' => 'checkbox', 'value' => 'on' ],
  584. 'Default value "on" is stripped of checkboxes',
  585. ];
  586. $cases[] = [ '<input type="radio"/>',
  587. 'input', [ 'type' => 'radio', 'value' => 'on' ],
  588. 'Default value "on" is stripped of radio buttons',
  589. ];
  590. $cases[] = [ '<input type="submit" value="Submit"/>',
  591. 'input', [ 'type' => 'submit', 'value' => 'Submit' ],
  592. 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
  593. ];
  594. $cases[] = [ '<input type="color"/>',
  595. 'input', [ 'type' => 'color', 'value' => '' ],
  596. ];
  597. $cases[] = [ '<input type="range"/>',
  598. 'input', [ 'type' => 'range', 'value' => '' ],
  599. ];
  600. # <button> specific handling
  601. # see remarks on https://msdn.microsoft.com/library/ms535211(v=vs.85).aspx
  602. $cases[] = [ '<button type="submit"></button>',
  603. 'button', [ 'type' => 'submit' ],
  604. 'According to standard the default type is "submit". '
  605. . 'Depending on compatibility mode IE might use "button", instead.',
  606. ];
  607. # <select> specific handling
  608. $cases[] = [ '<select multiple=""></select>',
  609. 'select', [ 'size' => '4', 'multiple' => true ],
  610. ];
  611. # .. with numeric value
  612. $cases[] = [ '<select multiple=""></select>',
  613. 'select', [ 'size' => 4, 'multiple' => true ],
  614. ];
  615. $cases[] = [ '<select></select>',
  616. 'select', [ 'size' => '1', 'multiple' => false ],
  617. ];
  618. # .. with numeric value
  619. $cases[] = [ '<select></select>',
  620. 'select', [ 'size' => 1, 'multiple' => false ],
  621. ];
  622. # Passing an array as value
  623. $cases[] = [ '<a class="css-class-one css-class-two"></a>',
  624. 'a', [ 'class' => [ 'css-class-one', 'css-class-two' ] ],
  625. "dropDefaults accepts values given as an array"
  626. ];
  627. # FIXME: doDropDefault should remove defaults given in an array
  628. # Expected should be '<a></a>'
  629. $cases[] = [ '<a class=""></a>',
  630. 'a', [ 'class' => [ '', '' ] ],
  631. "dropDefaults accepts values given as an array"
  632. ];
  633. # Craft the Html elements
  634. $ret = [];
  635. foreach ( $cases as $case ) {
  636. $ret[] = [
  637. $case[0],
  638. $case[1], $case[2],
  639. $case[3] ?? ''
  640. ];
  641. }
  642. return $ret;
  643. }
  644. /**
  645. * @covers Html::input
  646. */
  647. public function testWrapperInput() {
  648. $this->assertEquals(
  649. '<input type="radio" value="testval" name="testname"/>',
  650. Html::input( 'testname', 'testval', 'radio' ),
  651. 'Input wrapper with type and value.'
  652. );
  653. $this->assertEquals(
  654. '<input name="testname"/>',
  655. Html::input( 'testname' ),
  656. 'Input wrapper with all default values.'
  657. );
  658. }
  659. /**
  660. * @covers Html::check
  661. */
  662. public function testWrapperCheck() {
  663. $this->assertEquals(
  664. '<input type="checkbox" value="1" name="testname"/>',
  665. Html::check( 'testname' ),
  666. 'Checkbox wrapper unchecked.'
  667. );
  668. $this->assertEquals(
  669. '<input checked="" type="checkbox" value="1" name="testname"/>',
  670. Html::check( 'testname', true ),
  671. 'Checkbox wrapper checked.'
  672. );
  673. $this->assertEquals(
  674. '<input type="checkbox" value="testval" name="testname"/>',
  675. Html::check( 'testname', false, [ 'value' => 'testval' ] ),
  676. 'Checkbox wrapper with a value override.'
  677. );
  678. }
  679. /**
  680. * @covers Html::radio
  681. */
  682. public function testWrapperRadio() {
  683. $this->assertEquals(
  684. '<input type="radio" value="1" name="testname"/>',
  685. Html::radio( 'testname' ),
  686. 'Radio wrapper unchecked.'
  687. );
  688. $this->assertEquals(
  689. '<input checked="" type="radio" value="1" name="testname"/>',
  690. Html::radio( 'testname', true ),
  691. 'Radio wrapper checked.'
  692. );
  693. $this->assertEquals(
  694. '<input type="radio" value="testval" name="testname"/>',
  695. Html::radio( 'testname', false, [ 'value' => 'testval' ] ),
  696. 'Radio wrapper with a value override.'
  697. );
  698. }
  699. /**
  700. * @covers Html::label
  701. */
  702. public function testWrapperLabel() {
  703. $this->assertEquals(
  704. '<label for="testid">testlabel</label>',
  705. Html::label( 'testlabel', 'testid' ),
  706. 'Label wrapper'
  707. );
  708. }
  709. public static function provideSrcSetImages() {
  710. return [
  711. [ [], '', 'when there are no images, return empty string' ],
  712. [
  713. [ '1x' => '1x.png', '1.5x' => '1_5x.png', '2x' => '2x.png' ],
  714. '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
  715. 'pixel depth keys may include a trailing "x"'
  716. ],
  717. [
  718. [ '1' => '1x.png', '1.5' => '1_5x.png', '2' => '2x.png' ],
  719. '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
  720. 'pixel depth keys may omit a trailing "x"'
  721. ],
  722. [
  723. [ '1' => 'small.png', '1.5' => 'large.png', '2' => 'large.png' ],
  724. 'small.png 1x, large.png 1.5x',
  725. 'omit larger duplicates'
  726. ],
  727. [
  728. [ '1' => 'small.png', '2' => 'large.png', '1.5' => 'large.png' ],
  729. 'small.png 1x, large.png 1.5x',
  730. 'omit larger duplicates in irregular order'
  731. ],
  732. ];
  733. }
  734. /**
  735. * @dataProvider provideSrcSetImages
  736. * @covers Html::srcSet
  737. */
  738. public function testSrcSet( $images, $expected, $message ) {
  739. $this->assertEquals( Html::srcSet( $images ), $expected, $message );
  740. }
  741. public static function provideInlineScript() {
  742. return [
  743. 'Empty' => [
  744. '',
  745. '<script></script>'
  746. ],
  747. 'Simple' => [
  748. 'EXAMPLE.label("foo");',
  749. '<script>EXAMPLE.label("foo");</script>'
  750. ],
  751. 'Ampersand' => [
  752. 'EXAMPLE.is(a && b);',
  753. '<script>EXAMPLE.is(a && b);</script>'
  754. ],
  755. 'HTML' => [
  756. 'EXAMPLE.label("<a>");',
  757. '<script>EXAMPLE.label("<a>");</script>'
  758. ],
  759. 'Script closing string (lower)' => [
  760. 'EXAMPLE.label("</script>");',
  761. '<script>/* ERROR: Invalid script */</script>',
  762. true,
  763. ],
  764. 'Script closing with non-standard attributes (mixed)' => [
  765. 'EXAMPLE.label("</SCriPT and STyLE>");',
  766. '<script>/* ERROR: Invalid script */</script>',
  767. true,
  768. ],
  769. 'HTML-comment-open and script-open' => [
  770. // In HTML, <script> contents aren't just plain CDATA until </script>,
  771. // there are levels of escaping modes, and the below sequence puts an
  772. // HTML parser in a state where </script> would *not* close the script.
  773. // https://html.spec.whatwg.org/multipage/parsing.html#script-data-double-escape-end-state
  774. 'var a = "<!--<script>";',
  775. '<script>/* ERROR: Invalid script */</script>',
  776. true,
  777. ],
  778. ];
  779. }
  780. /**
  781. * @dataProvider provideInlineScript
  782. * @covers Html::inlineScript
  783. */
  784. public function testInlineScript( $code, $expected, $error = false ) {
  785. if ( $error ) {
  786. Wikimedia\suppressWarnings();
  787. $this->restoreWarnings = true;
  788. }
  789. $this->assertSame( Html::inlineScript( $code ), $expected );
  790. }
  791. }
  792. class HtmlTestValue {
  793. function __toString() {
  794. return 'stringValue';
  795. }
  796. }