Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined method `parent' for nil #198

Open
Amnesthesia opened this issue Jan 15, 2025 · 0 comments
Open

undefined method `parent' for nil #198

Amnesthesia opened this issue Jan 15, 2025 · 0 comments

Comments

@Amnesthesia
Copy link

Amnesthesia commented Jan 15, 2025

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

Amnesthesia added a commit to Amnesthesia/sablon that referenced this issue Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant