module Docx::Elements::Element

Constants

DEFAULT_TAG

Attributes

node[RW]

Public Class Methods

included(base) click to toggle source

Ensure that a 'tag' corresponding to the XML element that defines the element is defined

# File lib/docx/elements/element.rb, line 11
def self.included(base)
  base.extend(ClassMethods)
  base.const_set(:TAG, Element::DEFAULT_TAG) unless base.const_defined?(:TAG)
end

Public Instance Methods

append_to(element) click to toggle source

Insertion methods Insert node as last child

# File lib/docx/elements/element.rb, line 31
def append_to(element)
  @node = element.node.add_child(@node)
  self
end
copy() click to toggle source

Creation/edit methods

# File lib/docx/elements/element.rb, line 54
def copy
  self.class.new(@node.dup)
end
html_tag(name, options = {}) click to toggle source

A method to wrap content in an HTML tag. Currently used in paragraph and text_run for the to_html methods

content

The base text content for the tag.

styles

Hash of the inline CSS styles to be applied. e.g. { 'font-size' => '12pt', 'text-decoration' => 'underline' }

# File lib/docx/elements/element.rb, line 65
def html_tag(name, options = {})
  content = options[:content]
  styles = options[:styles]

  html = "<#{name.to_s}"
  unless styles.nil? || styles.empty?
    styles_array = []
    styles.each do |property, value|
      styles_array << "#{property.to_s}:#{value};"
    end
    html << " style=\"#{styles_array.join('')}\""
  end
  html << ">"
  html << content if content
  html << "</#{name.to_s}>"
end
insert_after(element) click to toggle source
# File lib/docx/elements/element.rb, line 42
def insert_after(element)
  # Returns newly re-parented node
  @node = element.node.add_next_sibling(@node)
  self
end
insert_before(element) click to toggle source
# File lib/docx/elements/element.rb, line 48
def insert_before(element)
  @node = element.node.add_previous_sibling(@node)
  self
end
parent(type = '*') click to toggle source

TODO: Should create a docx object from this

# File lib/docx/elements/element.rb, line 20
def parent(type = '*')
  @node.at_xpath("./parent::#{type}")
end
parent_paragraph() click to toggle source

Get parent paragraph of element

# File lib/docx/elements/element.rb, line 25
def parent_paragraph
  Elements::Containers::Paragraph.new(parent('w:p'))
end
prepend_to(element) click to toggle source

Insert node as first child (after properties)

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