Implement many more nodes
All checks were successful
CI Pipeline / build (pull_request) Successful in 12s
All checks were successful
CI Pipeline / build (pull_request) Successful in 12s
Adds these new styling and formatting nodes * strike * highlight * linebreaks * pagebreaks * Hyperlinks
This commit is contained in:
@@ -133,4 +133,49 @@ class FormattingTest < Minitest::Test
|
||||
assert_includes xml, "bold+italic "
|
||||
assert_includes xml, "all three"
|
||||
end
|
||||
|
||||
def test_strikethrough_text
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p do
|
||||
doc.s { doc.text "strikethrough text" }
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "<w:strike/>"
|
||||
assert_includes xml, "strikethrough text"
|
||||
end
|
||||
|
||||
def test_strikethrough_with_other_formatting
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p do
|
||||
doc.b do
|
||||
doc.s { doc.text "bold and strikethrough" }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "<w:b/>"
|
||||
assert_includes xml, "<w:strike/>"
|
||||
assert_includes xml, "bold and strikethrough"
|
||||
end
|
||||
|
||||
def test_all_four_formatting_options
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p do
|
||||
doc.b do
|
||||
doc.i do
|
||||
doc.u do
|
||||
doc.s { doc.text "all four" }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "<w:b/>"
|
||||
assert_includes xml, "<w:i/>"
|
||||
assert_includes xml, '<w:u w:val="single"/>'
|
||||
assert_includes xml, "<w:strike/>"
|
||||
assert_includes xml, "all four"
|
||||
end
|
||||
end
|
||||
|
||||
127
test/hyperlink_test.rb
Normal file
127
test/hyperlink_test.rb
Normal file
@@ -0,0 +1,127 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "test_helper"
|
||||
|
||||
class HyperlinkTest < Minitest::Test
|
||||
include NotareTestHelpers
|
||||
|
||||
def test_simple_hyperlink
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p do
|
||||
doc.link "https://example.com", "Example"
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, '<w:hyperlink r:id="rId2"'
|
||||
assert_includes xml, "Example"
|
||||
assert_includes xml, '<w:u w:val="single"/>'
|
||||
assert_includes xml, '<w:color w:val="0000FF"/>'
|
||||
end
|
||||
|
||||
def test_hyperlink_url_as_text
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p do
|
||||
doc.link "https://example.com"
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "https://example.com"
|
||||
end
|
||||
|
||||
def test_hyperlink_with_block
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p do
|
||||
doc.link "https://example.com" do
|
||||
doc.b { doc.text "Bold Link" }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, '<w:hyperlink r:id="rId2"'
|
||||
assert_includes xml, "<w:b/>"
|
||||
assert_includes xml, "Bold Link"
|
||||
end
|
||||
|
||||
def test_hyperlink_relationship
|
||||
xml_files = create_doc_and_read_all_xml do |doc|
|
||||
doc.p do
|
||||
doc.link "https://example.com", "Example"
|
||||
end
|
||||
end
|
||||
|
||||
rels = xml_files["word/_rels/document.xml.rels"]
|
||||
assert_includes rels, "https://example.com"
|
||||
assert_includes rels, 'TargetMode="External"'
|
||||
assert_includes rels, "relationships/hyperlink"
|
||||
end
|
||||
|
||||
def test_multiple_hyperlinks
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p do
|
||||
doc.link "https://one.com", "One"
|
||||
doc.text " and "
|
||||
doc.link "https://two.com", "Two"
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, '<w:hyperlink r:id="rId2"'
|
||||
assert_includes xml, '<w:hyperlink r:id="rId3"'
|
||||
assert_includes xml, "One"
|
||||
assert_includes xml, "Two"
|
||||
end
|
||||
|
||||
def test_hyperlink_in_list_item
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.ul do
|
||||
doc.li do
|
||||
doc.link "https://example.com", "Link in list"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, '<w:hyperlink r:id="rId3"'
|
||||
assert_includes xml, "Link in list"
|
||||
end
|
||||
|
||||
def test_hyperlink_in_table_cell
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.table do
|
||||
doc.tr do
|
||||
doc.td do
|
||||
doc.link "https://example.com", "Link in table"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, '<w:hyperlink r:id="rId2"'
|
||||
assert_includes xml, "Link in table"
|
||||
end
|
||||
|
||||
def test_hyperlink_with_surrounding_text
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p do
|
||||
doc.text "Visit "
|
||||
doc.link "https://example.com", "our site"
|
||||
doc.text " for more info."
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "Visit "
|
||||
assert_includes xml, "our site"
|
||||
assert_includes xml, " for more info."
|
||||
end
|
||||
|
||||
def test_multiple_hyperlinks_relationships
|
||||
xml_files = create_doc_and_read_all_xml do |doc|
|
||||
doc.p do
|
||||
doc.link "https://one.com", "One"
|
||||
doc.link "https://two.com", "Two"
|
||||
end
|
||||
end
|
||||
|
||||
rels = xml_files["word/_rels/document.xml.rels"]
|
||||
assert_includes rels, "https://one.com"
|
||||
assert_includes rels, "https://two.com"
|
||||
end
|
||||
end
|
||||
85
test/line_break_test.rb
Normal file
85
test/line_break_test.rb
Normal file
@@ -0,0 +1,85 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "test_helper"
|
||||
|
||||
class LineBreakTest < Minitest::Test
|
||||
include NotareTestHelpers
|
||||
|
||||
def test_line_break_in_paragraph
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p do
|
||||
doc.text "First line"
|
||||
doc.br
|
||||
doc.text "Second line"
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "First line"
|
||||
assert_includes xml, "Second line"
|
||||
assert_includes xml, "<w:br/>"
|
||||
end
|
||||
|
||||
def test_multiple_line_breaks
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p do
|
||||
doc.text "Line 1"
|
||||
doc.br
|
||||
doc.text "Line 2"
|
||||
doc.br
|
||||
doc.text "Line 3"
|
||||
end
|
||||
end
|
||||
|
||||
assert_equal 2, xml.scan("<w:br/>").count
|
||||
end
|
||||
|
||||
def test_line_break_with_formatting
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p do
|
||||
doc.b { doc.text "Bold first line" }
|
||||
doc.br
|
||||
doc.i { doc.text "Italic second line" }
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "<w:b/>"
|
||||
assert_includes xml, "<w:i/>"
|
||||
assert_includes xml, "<w:br/>"
|
||||
assert_includes xml, "Bold first line"
|
||||
assert_includes xml, "Italic second line"
|
||||
end
|
||||
|
||||
def test_line_break_in_list_item
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.ul do
|
||||
doc.li do
|
||||
doc.text "First line"
|
||||
doc.br
|
||||
doc.text "Second line"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "<w:br/>"
|
||||
assert_includes xml, "First line"
|
||||
assert_includes xml, "Second line"
|
||||
end
|
||||
|
||||
def test_line_break_in_table_cell
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.table do
|
||||
doc.tr do
|
||||
doc.td do
|
||||
doc.text "Cell line 1"
|
||||
doc.br
|
||||
doc.text "Cell line 2"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "<w:br/>"
|
||||
assert_includes xml, "Cell line 1"
|
||||
assert_includes xml, "Cell line 2"
|
||||
end
|
||||
end
|
||||
54
test/page_break_test.rb
Normal file
54
test/page_break_test.rb
Normal file
@@ -0,0 +1,54 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "test_helper"
|
||||
|
||||
class PageBreakTest < Minitest::Test
|
||||
include NotareTestHelpers
|
||||
|
||||
def test_page_break
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p "First page content"
|
||||
doc.page_break
|
||||
doc.p "Second page content"
|
||||
end
|
||||
|
||||
assert_includes xml, "First page content"
|
||||
assert_includes xml, "Second page content"
|
||||
assert_includes xml, '<w:br w:type="page"/>'
|
||||
end
|
||||
|
||||
def test_multiple_page_breaks
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p "Page 1"
|
||||
doc.page_break
|
||||
doc.p "Page 2"
|
||||
doc.page_break
|
||||
doc.p "Page 3"
|
||||
end
|
||||
|
||||
assert_equal 2, xml.scan('<w:br w:type="page"/>').count
|
||||
end
|
||||
|
||||
def test_page_break_between_different_elements
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.h1 "Chapter 1"
|
||||
doc.p "Some content"
|
||||
doc.page_break
|
||||
doc.h1 "Chapter 2"
|
||||
doc.ul do
|
||||
doc.li "Item 1"
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "Chapter 1"
|
||||
assert_includes xml, "Chapter 2"
|
||||
assert_includes xml, '<w:br w:type="page"/>'
|
||||
end
|
||||
|
||||
def test_page_break_renders_in_own_paragraph
|
||||
xml = create_doc_and_read_xml(&:page_break)
|
||||
|
||||
# Page break should be wrapped in its own paragraph
|
||||
assert_match(%r{<w:p>\s*<w:r>\s*<w:br w:type="page"/>\s*</w:r>\s*</w:p>}m, xml)
|
||||
end
|
||||
end
|
||||
@@ -168,4 +168,56 @@ class StyleTest < Minitest::Test
|
||||
assert_includes styles_xml, 'w:ascii="Arial"'
|
||||
assert_includes styles_xml, '<w:jc w:val="center"'
|
||||
end
|
||||
|
||||
def test_style_with_highlight
|
||||
xml_files = create_doc_and_read_all_xml do |doc|
|
||||
doc.define_style :highlighted, highlight: "yellow"
|
||||
doc.p "Highlighted", style: :highlighted
|
||||
end
|
||||
|
||||
styles_xml = xml_files["word/styles.xml"]
|
||||
assert_includes styles_xml, '<w:highlight w:val="yellow"'
|
||||
end
|
||||
|
||||
def test_style_with_strikethrough
|
||||
xml_files = create_doc_and_read_all_xml do |doc|
|
||||
doc.define_style :deleted, strike: true
|
||||
doc.p "Deleted", style: :deleted
|
||||
end
|
||||
|
||||
styles_xml = xml_files["word/styles.xml"]
|
||||
assert_includes styles_xml, "<w:strike/>"
|
||||
end
|
||||
|
||||
def test_invalid_highlight_raises_error
|
||||
assert_raises(ArgumentError) do
|
||||
Notare::Style.new(:bad, highlight: "invalid_color")
|
||||
end
|
||||
end
|
||||
|
||||
def test_valid_highlight_colors
|
||||
# Test a few valid highlight colors
|
||||
%w[yellow red blue green cyan magenta].each do |color|
|
||||
style = Notare::Style.new(:test, highlight: color)
|
||||
assert_equal color, style.highlight
|
||||
end
|
||||
end
|
||||
|
||||
def test_highlight_in_text_run
|
||||
xml_files = create_doc_and_read_all_xml do |doc|
|
||||
doc.define_style :yellow_highlight, highlight: "yellow"
|
||||
doc.p do
|
||||
doc.text "Normal "
|
||||
doc.text "highlighted", style: :yellow_highlight
|
||||
end
|
||||
end
|
||||
|
||||
# Highlight is in the style definition, not inline when using style reference
|
||||
styles_xml = xml_files["word/styles.xml"]
|
||||
assert_includes styles_xml, '<w:highlight w:val="yellow"'
|
||||
|
||||
document_xml = xml_files["word/document.xml"]
|
||||
assert_includes document_xml, '<w:rStyle w:val="YellowHighlight"'
|
||||
assert_includes document_xml, "highlighted"
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user