Module: Docx::Elements::Element
- Included in:
- Bookmark, Containers::Paragraph, Containers::Table, Containers::TableCell, Containers::TableColumn, Containers::TableRow, Containers::TextRun, Text
- Defined in:
- lib/docx/elements/element.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- DEFAULT_TAG =
''
Instance Attribute Summary collapse
-
#node ⇒ Object
Returns the value of attribute node.
Class Method Summary collapse
-
.included(base) ⇒ Object
Ensure that a ‘tag’ corresponding to the XML element that defines the element is defined.
Instance Method Summary collapse
-
#append_to(element) ⇒ Object
Insertion methods Insert node as last child.
- #at_xpath(*args) ⇒ Object
-
#copy ⇒ Object
Creation/edit methods.
-
#html_tag(name, options = {}) ⇒ Object
A method to wrap content in an HTML tag.
- #insert_after(element) ⇒ Object
- #insert_before(element) ⇒ Object
-
#parent(type = '*') ⇒ Object
TODO: Should create a docx object from this.
-
#parent_paragraph ⇒ Object
Get parent paragraph of element.
-
#prepend_to(element) ⇒ Object
Insert node as first child (after properties).
- #xpath(*args) ⇒ Object
Instance Attribute Details
#node ⇒ Object
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
38 39 40 41 |
# File 'lib/docx/elements/element.rb', line 38 def append_to(element) @node = element.node.add_child(@node) self end |
#at_xpath(*args) ⇒ Object
23 24 25 |
# File 'lib/docx/elements/element.rb', line 23 def at_xpath(*args) @node.at_xpath(*args) end |
#copy ⇒ Object
Creation/edit methods
61 62 63 |
# File 'lib/docx/elements/element.rb', line 61 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’ }
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/docx/elements/element.rb', line 72 def html_tag(name, = {}) content = [:content] styles = [:styles] attributes = [:attributes] 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 unless attributes.nil? || attributes.empty? attributes.each do |attr_name, attr_value| html << " #{attr_name}=\"#{attr_value}\"" end end html << ">" html << content if content html << "</#{name.to_s}>" end |
#insert_after(element) ⇒ Object
49 50 51 52 53 |
# File 'lib/docx/elements/element.rb', line 49 def insert_after(element) # Returns newly re-parented node @node = element.node.add_next_sibling(@node) self end |
#insert_before(element) ⇒ Object
55 56 57 58 |
# File 'lib/docx/elements/element.rb', line 55 def insert_before(element) @node = element.node.add_previous_sibling(@node) self end |
#parent(type = '*') ⇒ Object
TODO: Should create a docx object from this
19 20 21 |
# File 'lib/docx/elements/element.rb', line 19 def parent(type = '*') @node.at_xpath("./parent::#{type}") end |
#parent_paragraph ⇒ Object
Get parent paragraph of element
32 33 34 |
# File 'lib/docx/elements/element.rb', line 32 def parent_paragraph Elements::Containers::Paragraph.new(parent('w:p')) end |
#prepend_to(element) ⇒ Object
Insert node as first child (after properties)
44 45 46 47 |
# File 'lib/docx/elements/element.rb', line 44 def prepend_to(element) @node = element.node.properties.add_next_sibling(@node) self end |
#xpath(*args) ⇒ Object
27 28 29 |
# File 'lib/docx/elements/element.rb', line 27 def xpath(*args) @node.xpath(*args) end |