1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
|
require 'test_helper'
class RbpdfCssTest < Test::Unit::TestCase
class MYPDF < RBPDF
def extractCSSproperties(cssdata)
super
end
def isValidCSSSelectorForTag(dom, key, selector)
super
end
def getTagStyleFromCSS(dom, key, css)
super
end
def getHtmlDomArray(html)
super
end
end
test "CSS Basic" do
pdf = MYPDF.new
# empty
css = pdf.extractCSSproperties('')
assert_equal css, {}
# empty blocks
css = pdf.extractCSSproperties('h1 {}')
assert_equal css, {}
# comment
css = pdf.extractCSSproperties('/* comment */')
assert_equal css, {}
css = pdf.extractCSSproperties('h1 { color: navy; font-family: times; }')
assert_equal css, {"0001 h1"=>"color:navy;font-family:times;"}
css = pdf.extractCSSproperties('h1 { color: navy; font-family: times; } p.first { color: #003300; font-family: helvetica; font-size: 12pt; }')
assert_equal css, {"0001 h1"=>"color:navy;font-family:times;", "0021 p.first"=>"color:#003300;font-family:helvetica;font-size:12pt;"}
css = pdf.extractCSSproperties('h1,h2,h3{background-color:#e0e0e0}')
assert_equal css, {"0001 h1"=>"background-color:#e0e0e0", "0001 h2"=>"background-color:#e0e0e0", "0001 h3"=>"background-color:#e0e0e0"}
css = pdf.extractCSSproperties('p.second { color: rgb(00,63,127); font-family: times; font-size: 12pt; text-align: justify; }')
assert_equal css, {"0011 p.second"=>"color:rgb(00,63,127);font-family:times;font-size:12pt;text-align:justify;"}
css = pdf.extractCSSproperties('p#second { color: rgb(00,63,127); font-family: times; font-size: 12pt; text-align: justify; }')
assert_equal css, {"0101 p#second"=>"color:rgb(00,63,127);font-family:times;font-size:12pt;text-align:justify;"}
css = pdf.extractCSSproperties('p.first { color: rgb(00,63,127); } p.second { font-family: times; }')
assert_equal css, {"0021 p.first"=>"color:rgb(00,63,127);", "0011 p.second"=>"font-family:times;"}
css = pdf.extractCSSproperties('p#first { color: rgb(00,63,127); } p#second { color: rgb(00,63,127); }')
assert_equal css, {"0111 p#first"=>"color:rgb(00,63,127);", "0101 p#second"=>"color:rgb(00,63,127);"}
# media
css = pdf.extractCSSproperties('@media print { body { font: 10pt serif } }')
assert_equal css, {"0001 body"=>"font:10pt serif"}
css = pdf.extractCSSproperties('@media screen { body { font: 12pt sans-serif } }')
assert_equal css, {}
css = pdf.extractCSSproperties('@media all { body { line-height: 1.2 } }')
assert_equal css, {"0001 body"=>"line-height:1.2"}
css = pdf.extractCSSproperties('@media print {
#top-menu, #header, #main-menu, #sidebar, #footer, .contextual, .other-formats { display:none; }
#main { background: #fff; }
#content { width: 99%; margin: 0; padding: 0; border: 0; background: #fff; overflow: visible !important;}
#wiki_add_attachment { display:none; }
.hide-when-print { display: none; }
.autoscroll {overflow-x: visible;}
table.list {margin-top:0.5em;}
table.list th, table.list td {border: 1px solid #aaa;}
} @media all { body { line-height: 1.2 } }')
assert_equal css, {"0100 #top-menu"=>"display:none;",
"0100 #header"=>"display:none;",
"0100 #main-menu"=>"display:none;",
"0100 #sidebar"=>"display:none;",
"0100 #footer"=>"display:none;",
"0010 .contextual"=>"display:none;",
"0010 .other-formats"=>"display:none;",
"0100 #main"=>"background:#fff;",
"0100 #content"=>"width:99%;margin:0;padding:0;border:0;background:#fff;overflow:visible !important;",
"0100 #wiki_add_attachment"=>"display:none;",
"0010 .hide-when-print"=>"display:none;",
"0010 .autoscroll"=>"overflow-x:visible;",
"0011 table.list"=>"margin-top:0.5em;",
"0012 table.list th"=>"border:1px solid #aaa;",
"0012 table.list td"=>"border:1px solid #aaa;",
"0001 body"=>"line-height:1.2"}
end
test "CSS Selector Valid test" do
pdf = MYPDF.new
# Simple CSS
dom = pdf.getHtmlDomArray('<p>abc</p>')
assert_equal dom.length, 4
valid = pdf.isValidCSSSelectorForTag(dom, 1, ' p') # dom, key, css selector
assert_equal valid, true
dom = pdf.getHtmlDomArray('<h1>abc</h1>')
assert_equal dom.length, 4
valid = pdf.isValidCSSSelectorForTag(dom, 1, ' h1') # dom, key, css selector
assert_equal valid, true
dom = pdf.getHtmlDomArray('<p class="first">abc</p>')
assert_equal dom.length, 4
valid = pdf.isValidCSSSelectorForTag(dom, 1, ' p.first') # dom, key, css selector
assert_equal valid, true
dom = pdf.getHtmlDomArray('<p class="first">abc<span>def</span></p>')
assert_equal dom.length, 7
valid = pdf.isValidCSSSelectorForTag(dom, 3, ' p.first span') # dom, key, css selector
assert_equal valid, true
dom = pdf.getHtmlDomArray('<p id="second">abc</p>')
assert_equal dom.length, 4
valid = pdf.isValidCSSSelectorForTag(dom, 1, ' p#second') # dom, key, css selector
assert_equal valid, true
dom = pdf.getHtmlDomArray('<p id="second">abc<span>def</span></p>')
assert_equal dom.length, 7
valid = pdf.isValidCSSSelectorForTag(dom, 3, ' p#second > span') # dom, key, css selector
assert_equal valid, true
end
test "CSS Tag Sytle test 1" do
pdf = MYPDF.new
# Simple CSS
dom = pdf.getHtmlDomArray('<h1>abc</h1>')
assert_equal dom.length, 4
tag = pdf.getTagStyleFromCSS(dom, 1, {'0001 h1'=>'color:navy;font-family:times;'}) # dom, key, css selector
assert_equal tag, ';color:navy;font-family:times;'
tag = pdf.getTagStyleFromCSS(dom, 1, {'0001h1'=>'color:navy;font-family:times;'}) # dom, key, css selector
assert_equal tag, ''
tag = pdf.getTagStyleFromCSS(dom, 1, {'0001 h2'=>'color:navy;font-family:times;'}) # dom, key, css selector
assert_equal tag, ''
end
test "CSS Tag Sytle test 2" do
pdf = MYPDF.new
dom = pdf.getHtmlDomArray('<p class="first">abc</p>')
assert_equal dom.length, 4
tag = pdf.getTagStyleFromCSS(dom, 1, {'0021 p.first'=>'color:rgb(00,63,127);'})
assert_equal tag, ';color:rgb(00,63,127);'
dom = pdf.getHtmlDomArray('<p id="second">abc</p>')
assert_equal dom.length, 4
tag = pdf.getTagStyleFromCSS(dom, 1, {'0101 p#second'=>'color:rgb(00,63,127);font-family:times;font-size:12pt;text-align:justify;'})
assert_equal tag, ';color:rgb(00,63,127);font-family:times;font-size:12pt;text-align:justify;'
end
test "CSS Dom test" do
pdf = MYPDF.new
html = '<style> table, td { border: 2px #ff0000 solid; } </style>
<h2>HTML TABLE:</h2>
<table> <tr> <th>abc</th> </tr>
<tr> <td>def</td> </tr> </table>'
dom = pdf.getHtmlDomArray(html)
## remove style tag block (by getHtmlDomArray()) ##
## added marker tag (by getHtmlDomArray()) ##
# '<h2>HTML TABLE:</h2>
# <table><tr><th>abc<marker style="font-size:0"/></th></tr>
# <tr><td>def<marker style="font-size:0"/></td></tr></table>'
assert_equal dom.length, 18
assert_equal dom[0]['parent'], 0 # Root
assert_equal dom[0]['tag'], false
assert_equal dom[0]['attribute'], {}
# <h2>
assert_equal dom[1]['elkey'], 0
assert_equal dom[1]['parent'], 0 # parent -> parent tag key
assert_equal dom[1]['tag'], true
assert_equal dom[1]['opening'], true
assert_equal dom[1]['value'], 'h2'
# <table>
assert_equal dom[4]['elkey'], 3
assert_equal dom[4]['value'], 'table'
assert_equal dom[4]['attribute'], {'border'=>'2px #ff0000 solid', 'style'=>';border:2px #ff0000 solid;'}
assert_equal dom[4]['style']['border'], '2px #ff0000 solid'
assert_equal dom[4]['attribute']['border'], '2px #ff0000 solid'
end
test "CSS Dom table thead test" do
pdf = MYPDF.new
html = '<style> table, td { border: 2px #ff0000 solid; } </style>
<h2>HTML TABLE THEAD:</h2>
<table><thead>
<tr> <th>abc</th> </tr>
</thead>
<tbody>
<tr> <td>def</td> </tr>
<tr> <td>ghi</td> </tr>
</tbody></table>'
dom = pdf.getHtmlDomArray(html)
## remove style tag block (by getHtmlDomArray()) ##
## remove thead/tbody tag block (by getHtmlDomArray()) ##
## added marker tag (by getHtmlDomArray()) ##
# '<h2>HTML TABLE:</h2>
# <table><tr><th>abc<marker style="font-size:0"/></th></tr>
# <tr><td>def<marker style="font-size:0"/></td></tr></table>'
assert_equal dom.length, 24
assert_equal dom[0]['parent'], 0 # Root
assert_equal dom[0]['tag'], false
assert_equal dom[0]['attribute'], {}
# <h2>
assert_equal dom[1]['elkey'], 0
assert_equal dom[1]['parent'], 0 # parent -> parent tag key
assert_equal dom[1]['tag'], true
assert_equal dom[1]['opening'], true
assert_equal dom[1]['value'], 'h2'
# <table>
assert_equal dom[4]['elkey'], 3
assert_equal dom[4]['value'], 'table'
assert_equal dom[4]['attribute'], {'border'=>'2px #ff0000 solid', 'style'=>';border:2px #ff0000 solid;'}
assert_equal dom[4]['style']['border'], '2px #ff0000 solid'
assert_equal dom[4]['attribute']['border'], '2px #ff0000 solid'
assert_equal dom[4]['thead'], '<style>table {;border:2px #ff0000 solid;}</style><table tablehead="1"><tr><th>abc<marker style="font-size:0"/></th></tr></table>'
end
test "CSS Dom line-height test normal" do
pdf = MYPDF.new
html = '<style> h2 { line-height: normal; } </style>
<h2>HTML TEST</h2>'
dom = pdf.getHtmlDomArray(html)
## remove style tag block (by getHtmlDomArray()) ##
# '<h2>HTML TEST</h2>'
assert_equal dom.length, 4
# <h2>
assert_equal dom[1]['elkey'], 0
assert_equal dom[1]['parent'], 0 # parent -> parent tag key
assert_equal dom[1]['tag'], true
assert_equal dom[1]['opening'], true
assert_equal dom[1]['value'], 'h2'
assert_equal dom[1]['line-height'], 1.25
end
test "CSS Dom line-height test numeric" do
pdf = MYPDF.new
html = '<style> h2 { line-height: 1.4; } </style>
<h2>HTML TEST</h2>'
dom = pdf.getHtmlDomArray(html)
## remove style tag block (by getHtmlDomArray()) ##
# '<h2>HTML TEST</h2>'
assert_equal dom.length, 4
# <h2>
assert_equal dom[1]['elkey'], 0
assert_equal dom[1]['parent'], 0 # parent -> parent tag key
assert_equal dom[1]['tag'], true
assert_equal dom[1]['opening'], true
assert_equal dom[1]['value'], 'h2'
assert_equal dom[1]['line-height'], 1.4
end
test "CSS Dom line-height test percentage" do
pdf = MYPDF.new
html = '<style> h2 { line-height: 10%; } </style>
<h2>HTML TEST</h2>'
dom = pdf.getHtmlDomArray(html)
## remove style tag block (by getHtmlDomArray()) ##
# '<h2>HTML TEST</h2>'
assert_equal dom.length, 4
# <h2>
assert_equal dom[1]['parent'], 0 # parent -> parent tag key
assert_equal dom[1]['elkey'], 0
assert_equal dom[1]['tag'], true
assert_equal dom[1]['opening'], true
assert_equal dom[1]['value'], 'h2'
assert_equal dom[1]['line-height'], 0.1
end
test "CSS Dom class test" do
pdf = MYPDF.new
html = '<style>p.first { color: #003300; font-family: helvetica; font-size: 12pt; }
p.first span { color: #006600; font-style: italic; }</style>
<p class="first">Example <span>Fusce</span></p>'
dom = pdf.getHtmlDomArray(html)
## remove style tag block (by getHtmlDomArray()) ##
# '<p class="first">Example <span>Fusce</span></p>'
assert_equal dom.length, 7
# <p class="first">
assert_equal dom[1]['elkey'], 0
assert_equal dom[1]['parent'], 0 # parent -> parent tag key
assert_equal dom[1]['tag'], true
assert_equal dom[1]['opening'], true
assert_equal dom[1]['value'], 'p'
assert_equal dom[1]['attribute']['class'], 'first'
assert_equal dom[1]['style']['color'], '#003300'
assert_equal dom[1]['style']['font-family'], 'helvetica'
assert_equal dom[1]['style']['font-size'], '12pt'
# Example
assert_equal dom[2]['elkey'], 1
assert_equal dom[2]['parent'], 1
assert_equal dom[2]['tag'], false
assert_equal dom[2]['value'], 'Example '
# <span>
assert_equal dom[3]['elkey'], 2
assert_equal dom[3]['parent'], 1
assert_equal dom[3]['tag'], true
assert_equal dom[3]['opening'], true
assert_equal dom[3]['value'], 'span'
assert_equal dom[3]['style']['color'], '#006600'
assert_equal dom[3]['style']['font-style'], 'italic'
end
test "CSS Dom height width test" do
pdf = MYPDF.new
html = '<style> p.first { height: 60%; }
p.second { width: 70%; }</style>
<p class="first">ABC</p><p class="second">DEF</p>'
dom = pdf.getHtmlDomArray(html)
assert_equal dom.length, 7
# <p class="first">
assert_equal dom[1]['elkey'], 0
assert_equal dom[1]['parent'], 0 # parent -> parent tag key
assert_equal dom[1]['tag'], true
assert_equal dom[1]['opening'], true
assert_equal dom[1]['value'], 'p'
assert_not_nil dom[1]['style']
assert_equal dom[1]['style']['height'], '60%'
assert_equal dom[1]['height'], '60%'
# ABC
assert_equal dom[2]['elkey'], 1
assert_equal dom[2]['parent'], 1
assert_equal dom[2]['tag'], false
assert_equal dom[2]['value'], 'ABC'
# <p class="second">
assert_equal dom[4]['elkey'], 3
assert_equal dom[4]['parent'], 0 # parent -> parent tag key
assert_equal dom[4]['tag'], true
assert_equal dom[4]['opening'], true
assert_equal dom[4]['value'], 'p'
assert_not_nil dom[4]['style']
assert_equal dom[4]['style']['width'], '70%'
assert_equal dom[4]['width'], '70%'
end
test "CSS Dom font-weight test" do
pdf = MYPDF.new
html = '<style> p.first { font-weight: bold; }</style>
<p class="first">ABC</p><p class="second">DEF</p>'
dom = pdf.getHtmlDomArray(html)
assert_equal dom.length, 7
# <p class="first">
assert_equal dom[1]['elkey'], 0
assert_equal dom[1]['parent'], 0 # parent -> parent tag key
assert_equal dom[1]['tag'], true
assert_equal dom[1]['opening'], true
assert_equal dom[1]['value'], 'p'
assert_not_nil dom[1]['style']
assert_equal dom[1]['style']['font-weight'], 'bold'
assert_equal dom[1]['fontstyle'], 'B'
# ABC
assert_equal dom[2]['elkey'], 1
assert_equal dom[2]['parent'], 1
assert_equal dom[2]['tag'], false
assert_equal dom[2]['value'], 'ABC'
end
test "CSS Dom id test" do
pdf = MYPDF.new
html = '<style> p#second > span { background-color: #FFFFAA; }</style>
<p id="second">Example <span>Fusce</span></p>'
dom = pdf.getHtmlDomArray(html)
## remove style tag block (by getHtmlDomArray()) ##
# '<p id="second">Example <span>Fusce</span></p>'
assert_equal dom.length, 7
# <p id="second">
assert_equal dom[1]['elkey'], 0
assert_equal dom[1]['parent'], 0 # parent -> parent tag key
assert_equal dom[1]['tag'], true
assert_equal dom[1]['opening'], true
assert_equal dom[1]['value'], 'p'
assert_equal dom[1]['attribute']['id'], 'second'
# Example
assert_equal dom[2]['elkey'], 1
assert_equal dom[2]['parent'], 1
assert_equal dom[2]['tag'], false
assert_equal dom[2]['value'], 'Example '
# <span>
assert_equal dom[3]['elkey'], 2
assert_equal dom[3]['parent'], 1
assert_equal dom[3]['tag'], true
assert_equal dom[3]['opening'], true
assert_equal dom[3]['value'], 'span'
assert_equal dom[3]['style']['background-color'], '#FFFFAA'
end
test "CSS Dom text-decoration test" do
pdf = MYPDF.new
html = '<style> p.first { text-decoration: none;}
p.second {text-decoration: underline;}
p.third {text-decoration: overline;}
p.fourth {text-decoration: line-through;}
p.fifth {text-decoration: underline overline line-through;}</style>
<p class="first">ABC</p><p class="second">DEF</p><p class="third">GHI</p><p class="fourth">JKL</p><p class="fifth">MNO</p>'
dom = pdf.getHtmlDomArray(html)
assert_equal dom.length, 16
# <p class="first">
assert_equal dom[1]['elkey'], 0
assert_equal dom[1]['parent'], 0 # parent -> parent tag key
assert_equal dom[1]['tag'], true
assert_equal dom[1]['opening'], true
assert_equal dom[1]['value'], 'p'
assert_not_nil dom[1]['style']
assert_equal dom[1]['style']['text-decoration'], 'none'
assert_equal dom[1]['fontstyle'], ''
# ABC
assert_equal dom[2]['elkey'], 1
assert_equal dom[2]['parent'], 1
assert_equal dom[2]['tag'], false
assert_equal dom[2]['value'], 'ABC'
# <p class="second">
assert_equal dom[4]['elkey'], 3
assert_equal dom[4]['parent'], 0 # parent -> parent tag key
assert_equal dom[4]['tag'], true
assert_equal dom[4]['opening'], true
assert_equal dom[4]['value'], 'p'
assert_not_nil dom[1]['style']
assert_equal dom[4]['style']['text-decoration'], 'underline'
assert_equal dom[4]['fontstyle'], 'U'
# <p class="third">
assert_equal dom[7]['elkey'], 6
assert_equal dom[7]['parent'], 0 # parent -> parent tag key
assert_equal dom[7]['tag'], true
assert_equal dom[7]['opening'], true
assert_equal dom[7]['value'], 'p'
assert_not_nil dom[7]['style']
assert_equal dom[7]['style']['text-decoration'], 'overline'
assert_equal dom[7]['fontstyle'], 'O'
# <p class="fourth">
assert_equal dom[10]['elkey'], 9
assert_equal dom[10]['parent'], 0 # parent -> parent tag key
assert_equal dom[10]['tag'], true
assert_equal dom[10]['opening'], true
assert_equal dom[10]['value'], 'p'
assert_not_nil dom[10]['style']
assert_equal dom[10]['style']['text-decoration'], 'line-through'
assert_equal dom[10]['fontstyle'], 'D'
# <p class="fifth">
assert_equal dom[13]['elkey'], 12
assert_equal dom[13]['parent'], 0 # parent -> parent tag key
assert_equal dom[13]['tag'], true
assert_equal dom[13]['opening'], true
assert_equal dom[13]['value'], 'p'
assert_not_nil dom[13]['style']
assert_equal dom[13]['style']['text-decoration'], 'underline overline line-through'
assert_equal dom[13]['fontstyle'], 'UOD'
end
test "CSS Dom text-indent test" do
pdf = MYPDF.new
html = '<style> p.first { text-indent: 10px; }
p.second { text-indent: 5em; }
p.third { text-indent: 5ex; }
p.fourth { text-indent: 50%; }</style>
<p class="first">ABC</p><p class="second">DEF</p><p class="third">GHI</p><p class="fourth">JKL</p>'
dom = pdf.getHtmlDomArray(html)
assert_equal dom.length, 13
# <p class="first">
assert_equal dom[1]['elkey'], 0
assert_equal dom[1]['parent'], 0 # parent -> parent tag key
assert_equal dom[1]['tag'], true
assert_equal dom[1]['opening'], true
assert_equal dom[1]['value'], 'p'
assert_not_nil dom[1]['style']
assert_equal dom[1]['style']['text-indent'], '10px'
assert_in_delta dom[1]['text-indent'], 3.53, 0.01
# ABC
assert_equal dom[2]['elkey'], 1
assert_equal dom[2]['parent'], 1
assert_equal dom[2]['tag'], false
assert_equal dom[2]['value'], 'ABC'
# <p class="second">
assert_equal dom[4]['elkey'], 3
assert_equal dom[4]['parent'], 0 # parent -> parent tag key
assert_equal dom[4]['tag'], true
assert_equal dom[4]['opening'], true
assert_equal dom[4]['value'], 'p'
assert_not_nil dom[4]['style']
assert_equal dom[4]['style']['text-indent'], '5em'
assert_equal dom[4]['text-indent'], 5.0
# <p class="third">
assert_equal dom[7]['elkey'], 6
assert_equal dom[7]['parent'], 0 # parent -> parent tag key
assert_equal dom[7]['tag'], true
assert_equal dom[7]['opening'], true
assert_equal dom[7]['value'], 'p'
assert_not_nil dom[7]['style']
assert_equal dom[7]['style']['text-indent'], '5ex'
assert_equal dom[7]['text-indent'], 2.5
# <p class="fourth">
assert_equal dom[10]['elkey'], 9
assert_equal dom[10]['parent'], 0 # parent -> parent tag key
assert_equal dom[10]['tag'], true
assert_equal dom[10]['opening'], true
assert_equal dom[10]['value'], 'p'
assert_not_nil dom[10]['style']
assert_equal dom[10]['style']['text-indent'], '50%'
assert_equal dom[10]['text-indent'], 0.5
end
test "CSS Dom list-style-type test" do
pdf = MYPDF.new
html = '<style> p.first { list-style-type: none; }
p.second { list-style-type: disc; }
p.third { list-style-type: circle; }
p.fourth { list-style-type: square; }</style>
<p class="first">ABC</p><p class="second">DEF</p><p class="third">GHI</p><p class="fourth">JKL</p>'
dom = pdf.getHtmlDomArray(html)
assert_equal dom.length, 13
# <p class="first">
assert_equal dom[1]['elkey'], 0
assert_equal dom[1]['parent'], 0 # parent -> parent tag key
assert_equal dom[1]['tag'], true
assert_equal dom[1]['opening'], true
assert_equal dom[1]['value'], 'p'
assert_not_nil dom[1]['style']
assert_equal dom[1]['style']['list-style-type'], 'none'
assert_equal dom[1]['listtype'], 'none'
# ABC
assert_equal dom[2]['elkey'], 1
assert_equal dom[2]['parent'], 1
assert_equal dom[2]['tag'], false
assert_equal dom[2]['value'], 'ABC'
# <p class="second">
assert_equal dom[4]['elkey'], 3
assert_equal dom[4]['parent'], 0 # parent -> parent tag key
assert_equal dom[4]['tag'], true
assert_equal dom[4]['opening'], true
assert_equal dom[4]['value'], 'p'
assert_not_nil dom[4]['style']
assert_equal dom[4]['style']['list-style-type'], 'disc'
assert_equal dom[4]['listtype'], 'disc'
# <p class="third">
assert_equal dom[7]['elkey'], 6
assert_equal dom[7]['parent'], 0 # parent -> parent tag key
assert_equal dom[7]['tag'], true
assert_equal dom[7]['opening'], true
assert_equal dom[7]['value'], 'p'
assert_not_nil dom[7]['style']
assert_equal dom[7]['style']['list-style-type'], 'circle'
assert_equal dom[7]['listtype'], 'circle'
end
test "CSS Dom page-break test" do
pdf = MYPDF.new
html = '<style> p.first { page-break-before: left; page-break-after: always; }
p.second { page-break-inside:avoid; }</style>
<p class="first">ABC</p><p class="second">DEF</p>'
dom = pdf.getHtmlDomArray(html)
assert_equal dom.length, 7
# <p class="first">
assert_equal dom[1]['elkey'], 0
assert_equal dom[1]['parent'], 0 # parent -> parent tag key
assert_equal dom[1]['tag'], true
assert_equal dom[1]['opening'], true
assert_equal dom[1]['value'], 'p'
assert_not_nil dom[1]['style']
assert_equal dom[1]['style']['page-break-before'], 'left'
assert_equal dom[1]['style']['page-break-after'], 'always'
assert_not_nil dom[1]['attribute']
assert_equal dom[1]['attribute']['pagebreak'], 'left'
assert_equal dom[1]['attribute']['pagebreakafter'], 'true'
# ABC
assert_equal dom[2]['elkey'], 1
assert_equal dom[2]['parent'], 1
assert_equal dom[2]['tag'], false
assert_equal dom[2]['value'], 'ABC'
# <p class="second">
assert_equal dom[4]['elkey'], 3
assert_equal dom[4]['parent'], 0 # parent -> parent tag key
assert_equal dom[4]['tag'], true
assert_equal dom[4]['opening'], true
assert_equal dom[4]['value'], 'p'
assert_not_nil dom[1]['style']
assert_equal dom[4]['style']['page-break-inside'], 'avoid'
assert_not_nil dom[4]['attribute']
assert_equal dom[4]['attribute']['nobr'], 'true'
end
end
|