Skip to content

undefined method `parent' for nil #198

@Amnesthesia

Description

@Amnesthesia

We use Sablon for docx templating, and we perform MailMerge operations on our server. We quite often run into this stacktrace here:

  undefined method `parent' for nil
    /vendor/ruby/3.3.0/gems/sablon-0.4.1/lib/sablon/content.rb:136:in `add_siblings_to'
    /vendor/ruby/3.3.0/gems/sablon-0.4.1/lib/sablon/content.rb:88:in `append_to'
    /vendor/ruby/3.3.0/gems/sablon-0.4.1/lib/sablon/parser/mail_merge.rb:35:in `replace_field_display'
    /vendor/ruby/3.3.0/gems/sablon-0.4.1/lib/sablon/parser/mail_merge.rb:56:in `replace'
    /vendor/ruby/3.3.0/gems/sablon-0.4.1/lib/sablon/operations.rb:7:in `evaluate'
    /vendor/ruby/3.3.0/gems/sablon-0.4.1/lib/sablon/processor/document.rb:75:in `block in manipulate'
    /vendor/ruby/3.3.0/gems/sablon-0.4.1/lib/sablon/processor/document.rb:74:in `each'
    /vendor/ruby/3.3.0/gems/sablon-0.4.1/lib/sablon/processor/document.rb:74:in `manipulate'
    /vendor/ruby/3.3.0/gems/sablon-0.4.1/lib/sablon/processor/document.rb:61:in `process'
    /vendor/ruby/3.3.0/gems/sablon-0.4.1/lib/sablon/template.rb:75:in `block (2 levels) in process'
    ... and 109 more lines

I can't seem to find a way to consistently reproduce this, but looking at the code, it seems like Sablon is appending to a paragraph, and then removing that paragraph — could that be causing the issue?

# /vendor/ruby/3.3.0/gems/sablon-0.4.1/lib/sablon/content.rb:88:in `append_to'

def append_to(paragraph, display_node, env)
    # if all nodes are inline then add them to the existing paragraph
    # otherwise replace the paragraph with the new content.
    if all_inline?
      pr_tag = display_node.parent.at_xpath('./w:rPr')
      add_siblings_to(display_node.parent, pr_tag)
      display_node.parent.remove
    else
      add_siblings_to(paragraph)   # <--- This is where it crashes
      paragraph.remove
    end
  end

Update: I believe this happens if you place multiple variables on the same line, especially if these variables insert more complex things like tables. Seems like if one of the variables isn't inline, the paragraph is replaced with the variables content, but the next variable then has no parent

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions