Module: Docx::Elements::Element

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

DEFAULT_TAG =
''

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nodeObject

Returns the value of attribute node.



16
17
18
# File 'lib/docx/elements/element.rb', line 16

def node
  @node
end

Class Method Details

.included(base) ⇒ Object

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



11
12
13
14
# 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

Instance Method Details

#append_to(element) ⇒ Object

Insertion methods Insert node as last child



31
32
33
34
# File 'lib/docx/elements/element.rb', line 31

def append_to(element)
  @node = element.node.add_child(@node)
  self
end

#copyObject

Creation/edit methods



54
55
56
# File 'lib/docx/elements/element.rb', line 54

def copy
  self.class.new(@node.dup)
end

#html_tag(name, options = {}) ⇒ Object

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’ }



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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) ⇒ Object



42
43
44
45
46
# 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) ⇒ Object



48
49
50
51
# File 'lib/docx/elements/element.rb', line 48

def insert_before(element)
  @node = element.node.add_previous_sibling(@node)
  self
end

#parent(type = '*') ⇒ Object

TODO: Should create a docx object from this



20
21
22
# File 'lib/docx/elements/element.rb', line 20

def parent(type = '*')
  @node.at_xpath("./parent::#{type}")
end

#parent_paragraphObject

Get parent paragraph of element



25
26
27
# File 'lib/docx/elements/element.rb', line 25

def parent_paragraph
  Elements::Containers::Paragraph.new(parent('w:p'))
end

#prepend_to(element) ⇒ Object

Insert node as first child (after properties)



37
38
39
40
# File 'lib/docx/elements/element.rb', line 37

def prepend_to(element)
  @node = element.node.properties.add_next_sibling(@node)
  self
end