style_inference_test.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <?php
  2. /**
  3. * ezcDocumentPdfStyleInferenceTests
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. * @package Document
  23. * @version //autogen//
  24. * @subpackage Tests
  25. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  26. */
  27. /**
  28. * Test suite for class.
  29. *
  30. * @package Document
  31. * @subpackage Tests
  32. */
  33. class ezcDocumentPcssStyleInferenceTests extends ezcTestCase
  34. {
  35. protected $document;
  36. protected $xpath;
  37. public static function suite()
  38. {
  39. return new PHPUnit_Framework_TestSuite( __CLASS__ );
  40. }
  41. public function setUp()
  42. {
  43. $this->document = new DOMDocument();
  44. $this->document->registerNodeClass( 'DOMElement', 'ezcDocumentLocateableDomElement' );
  45. $this->document->load( dirname( __FILE__ ) . '/../files/docbook/pdf/location_ids.xml' );
  46. $this->xpath = new DOMXPath( $this->document );
  47. $this->xpath->registerNamespace( 'doc', 'http://docbook.org/ns/docbook' );
  48. }
  49. public function testRootNodeWithoutFormats()
  50. {
  51. $inferencer = new ezcDocumentPcssStyleInferencer( false );
  52. $element = $this->xpath->query( '//doc:article' )->item( 0 );
  53. $this->assertEquals(
  54. array(),
  55. $inferencer->inferenceFormattingRules( $element )
  56. );
  57. }
  58. public function testRootNodeFormatting()
  59. {
  60. $inferencer = new ezcDocumentPcssStyleInferencer( false );
  61. $element = $this->xpath->query( '//doc:article' )->item( 0 );
  62. $inferencer->appendStyleDirectives( array(
  63. new ezcDocumentPcssLayoutDirective(
  64. array( 'article' ),
  65. array(
  66. 'foo' => 'bar',
  67. )
  68. ),
  69. ) );
  70. $this->assertEquals(
  71. array(
  72. 'foo' => new ezcDocumentPcssStyleStringValue( 'bar' ),
  73. ),
  74. $inferencer->inferenceFormattingRules( $element )
  75. );
  76. }
  77. public function testRootNodeFormattingPartialOverwrite()
  78. {
  79. $inferencer = new ezcDocumentPcssStyleInferencer( false );
  80. $element = $this->xpath->query( '//doc:article' )->item( 0 );
  81. $inferencer->appendStyleDirectives( array(
  82. new ezcDocumentPcssLayoutDirective(
  83. array( 'article' ),
  84. array(
  85. 'foo' => 'bar',
  86. 'baz' => 'bar',
  87. )
  88. ),
  89. new ezcDocumentPcssLayoutDirective(
  90. array( 'article' ),
  91. array(
  92. 'foo' => 'blubb',
  93. )
  94. ),
  95. ) );
  96. $this->assertEquals(
  97. array(
  98. 'foo' => new ezcDocumentPcssStyleStringValue( 'blubb' ),
  99. 'baz' => new ezcDocumentPcssStyleStringValue( 'bar' ),
  100. ),
  101. $inferencer->inferenceFormattingRules( $element )
  102. );
  103. }
  104. public function testRootNodeFormattingRuleInheritance()
  105. {
  106. $inferencer = new ezcDocumentPcssStyleInferencer( false );
  107. $element = $this->xpath->query( '//doc:section' )->item( 0 );
  108. $inferencer->appendStyleDirectives( array(
  109. new ezcDocumentPcssLayoutDirective(
  110. array( 'article' ),
  111. array(
  112. 'foo' => 'bar',
  113. 'baz' => 'bar',
  114. )
  115. ),
  116. new ezcDocumentPcssLayoutDirective(
  117. array( 'article', '> section' ),
  118. array(
  119. 'foo' => 'blubb',
  120. )
  121. ),
  122. ) );
  123. $this->assertEquals(
  124. array(
  125. 'foo' => new ezcDocumentPcssStyleStringValue( 'blubb' ),
  126. 'baz' => new ezcDocumentPcssStyleStringValue( 'bar' ),
  127. ),
  128. $inferencer->inferenceFormattingRules( $element )
  129. );
  130. }
  131. public function testIntValueHandler()
  132. {
  133. $inferencer = new ezcDocumentPcssStyleInferencer( false );
  134. $element = $this->xpath->query( '//doc:article' )->item( 0 );
  135. $inferencer->appendStyleDirectives( array(
  136. new ezcDocumentPcssLayoutDirective(
  137. array( 'article' ),
  138. array(
  139. 'text-columns' => '1',
  140. )
  141. ),
  142. ) );
  143. $this->assertEquals(
  144. array(
  145. 'text-columns' => new ezcDocumentPcssStyleIntValue( 1 ),
  146. ),
  147. $inferencer->inferenceFormattingRules( $element )
  148. );
  149. }
  150. public function testMeasureValueHandler()
  151. {
  152. $inferencer = new ezcDocumentPcssStyleInferencer( false );
  153. $element = $this->xpath->query( '//doc:article' )->item( 0 );
  154. $inferencer->appendStyleDirectives( array(
  155. new ezcDocumentPcssLayoutDirective(
  156. array( 'article' ),
  157. array(
  158. 'font-size' => '10',
  159. )
  160. ),
  161. ) );
  162. $this->assertEquals(
  163. array(
  164. 'font-size' => new ezcDocumentPcssStyleMeasureValue( 10 ),
  165. ),
  166. $inferencer->inferenceFormattingRules( $element )
  167. );
  168. }
  169. public function testExceptionPostDecoration()
  170. {
  171. $inferencer = new ezcDocumentPcssStyleInferencer( false );
  172. $element = $this->xpath->query( '//doc:article' )->item( 0 );
  173. try
  174. {
  175. $inferencer->appendStyleDirectives( array(
  176. new ezcDocumentPcssLayoutDirective(
  177. array( 'article' ),
  178. array(
  179. 'font-size' => 'unparseable',
  180. ),
  181. 'my.css', 23, 42
  182. ),
  183. ) );
  184. $this->fail( 'Expected ezcDocumentParserException.' );
  185. }
  186. catch ( ezcDocumentParserException $e )
  187. {
  188. $this->assertEquals(
  189. 'Parse error: Fatal error: \'Could not parse \'unparseable\' as size value.\' in file \'my.css\' in line 23 at position 42.',
  190. $e->getMessage()
  191. );
  192. }
  193. }
  194. protected function assertCorrectMerge( $property, $styles, $expected )
  195. {
  196. $inferencer = new ezcDocumentPcssStyleInferencer( false );
  197. $element = $this->xpath->query( '//doc:article' )->item( 0 );
  198. $inferencer->appendStyleDirectives( array(
  199. new ezcDocumentPcssLayoutDirective( array( 'article' ), $styles ),
  200. ) );
  201. $rules = $inferencer->inferenceFormattingRules( $element );
  202. $this->assertTrue( isset( $rules[$property] ), "Missing property $property in inferenced rules." );
  203. $this->assertEquals( 1, count( $rules ), "Wrong number of inferenced rules" );
  204. $this->assertEquals( $expected, (string) $rules[$property] );
  205. }
  206. public static function getMarginDefinitions()
  207. {
  208. return array(
  209. array(
  210. array(
  211. 'margin' => '10mm',
  212. ),
  213. '10.00mm 10.00mm 10.00mm 10.00mm',
  214. ),
  215. array(
  216. array(
  217. 'margin-top' => '10mm',
  218. ),
  219. '10.00mm 0.00mm 0.00mm 0.00mm',
  220. ),
  221. array(
  222. array(
  223. 'margin' => '10mm',
  224. 'margin-top' => '20',
  225. ),
  226. '20.00mm 10.00mm 10.00mm 10.00mm',
  227. ),
  228. array(
  229. array(
  230. 'margin' => '10mm',
  231. 'margin-top' => '20',
  232. 'margin-right' => '30',
  233. 'margin-left' => '40',
  234. 'margin-bottom' => '50',
  235. ),
  236. '20.00mm 30.00mm 50.00mm 40.00mm',
  237. ),
  238. array(
  239. array(
  240. 'margin-top' => '20',
  241. 'margin-right' => '30',
  242. 'margin-left' => '40',
  243. 'margin-bottom' => '50',
  244. 'margin' => '0in',
  245. ),
  246. '0.00mm 0.00mm 0.00mm 0.00mm',
  247. ),
  248. );
  249. }
  250. /**
  251. * @dataProvider getMarginDefinitions
  252. */
  253. public function testMergeMarginValues( $styles, $expected )
  254. {
  255. $this->assertCorrectMerge( 'margin', $styles, $expected );
  256. }
  257. public static function getPaddingDefinitions()
  258. {
  259. return array(
  260. array(
  261. array(
  262. 'padding' => '10mm',
  263. 'padding-top' => '20',
  264. ),
  265. '20.00mm 10.00mm 10.00mm 10.00mm',
  266. ),
  267. );
  268. }
  269. /**
  270. * @dataProvider getPaddingDefinitions
  271. */
  272. public function testMergePaddingValues( $styles, $expected )
  273. {
  274. $this->assertCorrectMerge( 'padding', $styles, $expected );
  275. }
  276. public static function getBorderColorDefinitions()
  277. {
  278. return array(
  279. array(
  280. array(
  281. 'border-color' => '#f00',
  282. 'border-color-top' => '#00f',
  283. ),
  284. '0.00mm solid #0000ff 0.00mm solid #ff0000 0.00mm solid #ff0000 0.00mm solid #ff0000',
  285. ),
  286. );
  287. }
  288. /**
  289. * @dataProvider getBorderColorDefinitions
  290. */
  291. public function testMergeBorderColorValues( $styles, $expected )
  292. {
  293. $this->assertCorrectMerge( 'border', $styles, $expected );
  294. }
  295. public static function getBorderStyleDefinitions()
  296. {
  297. return array(
  298. array(
  299. array(
  300. 'border-style' => 'solid double',
  301. 'border-style-top' => 'inset',
  302. ),
  303. '0.00mm inset #ffffff 0.00mm double #ffffff 0.00mm solid #ffffff 0.00mm double #ffffff',
  304. ),
  305. );
  306. }
  307. /**
  308. * @dataProvider getBorderStyleDefinitions
  309. */
  310. public function testMergeBorderStyleValues( $styles, $expected )
  311. {
  312. $this->assertCorrectMerge( 'border', $styles, $expected );
  313. }
  314. public static function getBorderWidthDefinitions()
  315. {
  316. return array(
  317. array(
  318. array(
  319. 'border-width' => '1mm 2mm 3mm 4mm',
  320. 'border-width-left' => '5mm',
  321. ),
  322. '1.00mm solid #ffffff 2.00mm solid #ffffff 3.00mm solid #ffffff 5.00mm solid #ffffff',
  323. ),
  324. );
  325. }
  326. /**
  327. * @dataProvider getBorderWidthDefinitions
  328. */
  329. public function testMergeBorderWidthValues( $styles, $expected )
  330. {
  331. $this->assertCorrectMerge( 'border', $styles, $expected );
  332. }
  333. public static function getBorderDefinitions()
  334. {
  335. return array(
  336. array(
  337. array(
  338. 'border' => '1mm 2mm #0f0 3mm inset 4mm ',
  339. 'border-left' => '5mm',
  340. ),
  341. '1.00mm solid #ffffff 2.00mm solid #00ff00 3.00mm inset #ffffff 5.00mm solid #ffffff',
  342. ),
  343. array(
  344. array(
  345. 'border' => '1mm 2mm #0f0 3mm inset 4mm double',
  346. 'border-width-left' => '5mm',
  347. 'border-color-right' => '#0f0f0f',
  348. ),
  349. '1.00mm solid #ffffff 2.00mm solid #0f0f0f 3.00mm inset #ffffff 5.00mm double #ffffff',
  350. ),
  351. array(
  352. array(
  353. 'border' => '1mm 2mm #0f0 3mm inset 4mm double',
  354. 'border-width-left' => '5mm',
  355. 'border-color-right' => '#0f0f0f',
  356. 'border-style-top' => 'outset',
  357. ),
  358. '1.00mm outset #ffffff 2.00mm solid #0f0f0f 3.00mm inset #ffffff 5.00mm double #ffffff',
  359. ),
  360. array(
  361. array(
  362. 'border' => '1mm 2mm #0f0 3mm inset 4mm double',
  363. 'border-color' => '#0f0f0f',
  364. ),
  365. '1.00mm solid #0f0f0f 2.00mm solid #0f0f0f 3.00mm inset #0f0f0f 4.00mm double #0f0f0f',
  366. ),
  367. array(
  368. array(
  369. 'border-color' => '#0f0f0f',
  370. 'border' => '1mm 2mm #0f0 3mm inset 4mm double',
  371. ),
  372. '1.00mm solid #ffffff 2.00mm solid #00ff00 3.00mm inset #ffffff 4.00mm double #ffffff',
  373. ),
  374. );
  375. }
  376. /**
  377. * @dataProvider getBorderDefinitions
  378. */
  379. public function testMergeBorder( $styles, $expected )
  380. {
  381. $this->assertCorrectMerge( 'border', $styles, $expected );
  382. }
  383. public function testDefinitionExtraction1()
  384. {
  385. $inferencer = new ezcDocumentPcssStyleInferencer( false );
  386. $inferencer->appendStyleDirectives( array(
  387. $d1 = new ezcDocumentPcssDeclarationDirective(
  388. '@font-face',
  389. array(
  390. 'font-size' => '10',
  391. )
  392. ),
  393. ) );
  394. $this->assertEquals(
  395. array( $d1 ),
  396. $inferencer->getDefinitions( 'font-face' )
  397. );
  398. }
  399. public function testDefinitionExtraction2()
  400. {
  401. $inferencer = new ezcDocumentPcssStyleInferencer( false );
  402. $inferencer->appendStyleDirectives( array(
  403. $d1 = new ezcDocumentPcssDeclarationDirective(
  404. '@font-FACE',
  405. array(
  406. 'font-size' => '10',
  407. )
  408. ),
  409. new ezcDocumentPcssDeclarationDirective(
  410. '@unknown',
  411. array(
  412. 'font-size' => '10',
  413. )
  414. ),
  415. ) );
  416. $this->assertEquals(
  417. array( $d1 ),
  418. $inferencer->getDefinitions( 'font-face' )
  419. );
  420. }
  421. }
  422. ?>