Skip to content

Decouple load rels method from load styles so that @rels variable can be initialized #158

@ThiagoAlves15

Description

@ThiagoAlves15

Describe the bug

An error undefined method 'xpath' for nil:NilClass @rels.xpath("//xmlns:Relationship[contains(@Type,'hyperlink')]") is raised when the object fails to load the documents styles because the @rels variable did not load due to method coupling between load_rels and load_styles, making it so that if an error occurs when loading file styles the @rels variable is not loaded into the object.

To Reproduce

1- Create a .docx file with missing styles, so that load_styles method will return a No such file or directory - word/styles.xml message;
2- Try and access the paragraphs property of the loaded doc object with doc.paragraphs;

example

require 'docx'

doc = Docx::Document.new('/path/to/your/docx/file.docx')

doc.send(:load_styles)
# No such file or directory - word/styles.xml

doc.paragraphs.each do |p|
  puts p
end

Sample docx file

Cannot add the sample due to having confidential information in it and I do not have the means to save a docx file that will preserve the broken style used in it.

Expected behavior

Expected to be able to parse the document's text without issue when style is not present.

Environment

  • Ruby version: ruby 3.1.6p260 (2024-05-29 revision a777087be6)
  • docx gem version: ~> 0.8.0
  • OS: x86_64-linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions