Class: Prosereflect::Schema::Node
- Inherits:
-
Object
- Object
- Prosereflect::Schema::Node
- Defined in:
- lib/prosereflect/schema/node.rb
Overview
Lightweight Node class for schema validation This wraps the existing prosereflect Node and provides schema-aware methods
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#marks ⇒ Object
readonly
Returns the value of attribute marks.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #cut(from = 0, to = nil) ⇒ Object
- #descendants(f) ⇒ Object
- #eq?(other) ⇒ Boolean
-
#initialize(type:, attrs: {}, content: nil, marks: []) ⇒ Node
constructor
A new instance of Node.
- #is_atom ⇒ Object
- #is_block ⇒ Object
- #is_inline ⇒ Object
- #is_leaf ⇒ Object
- #is_text ⇒ Object
- #node_size ⇒ Object
- #nodes_between(from, to, f, node_start = 0) ⇒ Object
- #same_marks?(other) ⇒ Boolean
- #same_markup?(other) ⇒ Boolean
- #text? ⇒ Boolean
- #to_h ⇒ Object
- #to_s ⇒ Object
- #with_text(new_text) ⇒ Object
Constructor Details
#initialize(type:, attrs: {}, content: nil, marks: []) ⇒ Node
Returns a new instance of Node.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/prosereflect/schema/node.rb', line 10 def initialize(type:, attrs: {}, content: nil, marks: []) @type = type @attrs = attrs || {} @content = if content.is_a?(Fragment) content else (content ? Fragment.new(content) : Fragment.empty) end @marks = if marks.is_a?(Array) marks else (marks ? [marks] : []) end end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
8 9 10 |
# File 'lib/prosereflect/schema/node.rb', line 8 def attrs @attrs end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
8 9 10 |
# File 'lib/prosereflect/schema/node.rb', line 8 def content @content end |
#marks ⇒ Object (readonly)
Returns the value of attribute marks.
8 9 10 |
# File 'lib/prosereflect/schema/node.rb', line 8 def marks @marks end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
8 9 10 |
# File 'lib/prosereflect/schema/node.rb', line 8 def type @type end |
Class Method Details
.from_json(schema, json) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/prosereflect/schema/node.rb', line 166 def from_json(schema, json) type_name = json["type"] type = schema.node_type(type_name) attrs = json["attrs"] || {} content_data = json["content"] marks = if json["marks"].is_a?(Array) json["marks"].map { |m| schema.mark_from_json(m) } else [] end # Handle text nodes specially if type.text? text = json["text"] || "" TextNode.new(type: type, attrs: attrs, text: text, marks: marks) else content = if content_data.is_a?(Array) content_data.map { |c| from_json(schema, c) } else [] end Node.new(type: type, attrs: attrs, content: Fragment.new(content), marks: marks) end end |
Instance Method Details
#cut(from = 0, to = nil) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/prosereflect/schema/node.rb', line 69 def cut(from = 0, to = nil) to ||= node_size return self if from.zero? && to == node_size if text? # For text nodes, cut by character offset TextNode.new( type: @type, attrs: @attrs, text: @attrs[:text][from...to], marks: @marks, ) else # For non-text nodes, cut content new_content = @content.cut(from - 1, to - 1) Node.new(type: @type, attrs: @attrs, content: new_content, marks: @marks) end end |
#descendants(f) ⇒ Object
122 123 124 |
# File 'lib/prosereflect/schema/node.rb', line 122 def descendants(f) nodes_between(0, node_size, f) end |
#eq?(other) ⇒ Boolean
126 127 128 129 130 131 |
# File 'lib/prosereflect/schema/node.rb', line 126 def eq?(other) return false unless other.is_a?(Node) return false unless @type == other.type @attrs == other.attrs && @content.eq?(other.content) && same_marks?(other) end |
#is_atom ⇒ Object
53 54 55 |
# File 'lib/prosereflect/schema/node.rb', line 53 def is_atom @type.is_atom? end |
#is_block ⇒ Object
41 42 43 |
# File 'lib/prosereflect/schema/node.rb', line 41 def is_block @type.is_block? end |
#is_inline ⇒ Object
45 46 47 |
# File 'lib/prosereflect/schema/node.rb', line 45 def is_inline @type.is_inline? end |
#is_leaf ⇒ Object
49 50 51 |
# File 'lib/prosereflect/schema/node.rb', line 49 def is_leaf @type.is_leaf? end |
#is_text ⇒ Object
37 38 39 |
# File 'lib/prosereflect/schema/node.rb', line 37 def is_text text? end |
#node_size ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/prosereflect/schema/node.rb', line 25 def node_size size = 1 # Every node has at least size 1 @content.content.each do |child| size += child.node_size end size end |
#nodes_between(from, to, f, node_start = 0) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/prosereflect/schema/node.rb', line 89 def nodes_between(from, to, f, node_start = 0) return unless to > from if text? f.call(self, node_start) return end pos = 0 i = 0 while pos < to && i < @content.content.length child = @content.content[i] end_pos = pos + child.node_size if end_pos > from child_start = node_start + pos + 1 if f.call(child, child_start, i) != false && child.content.size.positive? child.nodes_between( [0, from - pos].max, [child.content.size, to - pos].min, f, child_start, ) end end pos = end_pos i += 1 end end |
#same_marks?(other) ⇒ Boolean
133 134 135 136 137 138 139 140 141 |
# File 'lib/prosereflect/schema/node.rb', line 133 def same_marks?(other) return true if @marks.nil? && other.marks.nil? return false if @marks.nil? || other.marks.nil? return false unless @marks.length == other.marks.length @marks.each_with_index.all? do |m, i| m.type == other.marks[i].type && m.attrs == other.marks[i].attrs end end |
#same_markup?(other) ⇒ Boolean
57 58 59 60 61 62 63 |
# File 'lib/prosereflect/schema/node.rb', line 57 def same_markup?(other) return false unless @type == other.type && @marks.length == other.marks.length @marks.zip(other.marks).all? do |m1, m2| m1.type == m2.type && m1.attrs == m2.attrs end end |
#text? ⇒ Boolean
33 34 35 |
# File 'lib/prosereflect/schema/node.rb', line 33 def text? @type.text? end |
#to_h ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/prosereflect/schema/node.rb', line 143 def to_h result = { "type" => @type.name } if @attrs && !@attrs.empty? result["attrs"] = @attrs end if @marks && !@marks.empty? result["marks"] = @marks.map(&:to_h) end if @content && !@content.empty? result["content"] = @content.content.map(&:to_h) end result end |
#to_s ⇒ Object
161 162 163 |
# File 'lib/prosereflect/schema/node.rb', line 161 def to_s "<Node #{@type.name}>" end |