Skip to content

Element#prepend_to always raises NoMethodError #189

Closed

Description

@satoryu

Docx::Elements::Element#prepend_to calls element.node.properties. That method doesn't exist on a Nokogiri node, so every call raises.

# lib/docx/elements/element.rb
def prepend_to(element)
  @node = element.node.properties.add_next_sibling(@node)
  self
end

Repro

w = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'
body = Nokogiri::XML(%(<w:body xmlns:w="#{w}"><w:p><w:pPr/></w:p><w:r><w:t>x</w:t></w:r></w:body>)).root
para = Docx::Elements::Containers::Paragraph.new(body.at_xpath('w:p'))
run  = Docx::Elements::Containers::TextRun.new(body.at_xpath('w:r'))
run.prepend_to(para)
# => NoMethodError: undefined method 'properties' for an instance of Nokogiri::XML::Element

Expected: the node is inserted as the first child of element, after its properties node (w:pPr etc.) if there is one. That is probably element.properties (the Container method) rather than element.node.properties, with a fallback to prepend_child when there are no properties.

Found while splitting the specs into per-class unit specs (#188).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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