|
5 | 5 | describe Docx::Document do |
6 | 6 | before(:all) do |
7 | 7 | @fixtures_path = "spec/fixtures" |
8 | | - @formatting_line_count = 11 # number of lines the formatting.docx file has |
| 8 | + @formatting_line_count = 12 # number of lines the formatting.docx file has |
9 | 9 | end |
10 | 10 |
|
11 | 11 | describe 'reading' do |
|
311 | 311 | before do |
312 | 312 | @doc = Docx::Document.open(@fixtures_path + '/formatting.docx') |
313 | 313 | @formatted_line = @doc.paragraphs[5] |
| 314 | + @p_regex = /(^\<p).+((?<=\>)\w+)(\<\/p>$)/ |
| 315 | + @span_regex = /(\<span).+((?<=\>)\w+)(<\/span>)/ |
| 316 | + @em_regex = /(\<em).+((?<=\>)\w+)(\<\/em\>)/ |
| 317 | + @strong_regex = /(\<strong).+((?<=\>)\w+)(\<\/strong\>)/ |
314 | 318 | end |
315 | 319 |
|
316 | 320 | it 'should wrap pragraphs in a p tag' do |
317 | | - scan = @doc.paragraphs[0].to_html.scan(/(^\<p).+((?<=\>)\w+)(\<\/p>$)/).flatten |
| 321 | + scan = @doc.paragraphs[0].to_html.scan(@p_regex).flatten |
318 | 322 | scan.first.should eq '<p' |
319 | 323 | scan.last.should eq '</p>' |
320 | 324 | scan[1].should eq 'Normal' |
321 | 325 | end |
322 | 326 |
|
323 | 327 | it 'should emphasize italicized text' do |
324 | | - scan = @doc.paragraphs[1].to_html.scan(/(\<em\>)(\w+)(\<\/em\>)/).flatten |
325 | | - scan.first.should eq '<em>' |
| 328 | + scan = @doc.paragraphs[1].to_html.scan(@em_regex).flatten |
| 329 | + scan.first.should eq '<em' |
326 | 330 | scan.last.should eq '</em>' |
327 | 331 | scan[1].should eq 'Italic' |
328 | 332 | end |
329 | 333 |
|
330 | 334 | it 'should strong bolded text' do |
331 | | - scan = @doc.paragraphs[2].to_html.scan(/(\<strong\>)(\w+)(\<\/strong\>)/).flatten |
332 | | - scan.first.should eq '<strong>' |
| 335 | + scan = @doc.paragraphs[2].to_html.scan(@strong_regex).flatten |
| 336 | + scan.first.should eq '<strong' |
333 | 337 | scan.last.should eq '</strong>' |
334 | 338 | scan[1].should eq 'Bold' |
335 | 339 | end |
|
362 | 366 | scan[1].split(';').include?('font-size:16pt').should be_true |
363 | 367 | end |
364 | 368 |
|
365 | | - it 'should output an entire document as html' do |
| 369 | + it 'should properly highlight different text in different places in a sentence' do |
| 370 | + paragraph = @doc.paragraphs[11] |
| 371 | + scan = paragraph.to_html.scan(@em_regex).flatten |
| 372 | + scan.first.should eq '<em' |
| 373 | + scan.last.should eq '</em>' |
| 374 | + scan[1].should eq 'sentence' |
| 375 | + scan = paragraph.to_html.scan(@strong_regex).flatten |
| 376 | + scan.first.should eq '<strong' |
| 377 | + scan.last.should eq '</strong>' |
| 378 | + scan[1].should eq 'formatting' |
| 379 | + scan = paragraph.to_html.scan(@span_regex).flatten |
| 380 | + scan.first.should eq '<span' |
| 381 | + scan.last.should eq '</span>' |
| 382 | + scan[1].should eq 'different' |
| 383 | + scan = paragraph.to_html.scan(/\<span\s+([^\>]+)/).flatten |
| 384 | + scan.first.should eq 'style="text-decoration:underline;"' |
| 385 | + end |
| 386 | + |
| 387 | + it 'should output an entire document as html fragment' do |
366 | 388 | @doc.to_html.scan(/(\<p)/).flatten.size.should eq @formatting_line_count |
367 | 389 | end |
368 | 390 |
|
|
0 commit comments