Class: Prosereflect::Schema::Node

Inherits:
Object
  • Object
show all
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

TextNode

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#attrsObject (readonly)

Returns the value of attribute attrs.



8
9
10
# File 'lib/prosereflect/schema/node.rb', line 8

def attrs
  @attrs
end

#contentObject (readonly)

Returns the value of attribute content.



8
9
10
# File 'lib/prosereflect/schema/node.rb', line 8

def content
  @content
end

#marksObject (readonly)

Returns the value of attribute marks.



8
9
10
# File 'lib/prosereflect/schema/node.rb', line 8

def marks
  @marks
end

#typeObject (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

Returns:

  • (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_atomObject



53
54
55
# File 'lib/prosereflect/schema/node.rb', line 53

def is_atom
  @type.is_atom?
end

#is_blockObject



41
42
43
# File 'lib/prosereflect/schema/node.rb', line 41

def is_block
  @type.is_block?
end

#is_inlineObject



45
46
47
# File 'lib/prosereflect/schema/node.rb', line 45

def is_inline
  @type.is_inline?
end

#is_leafObject



49
50
51
# File 'lib/prosereflect/schema/node.rb', line 49

def is_leaf
  @type.is_leaf?
end

#is_textObject



37
38
39
# File 'lib/prosereflect/schema/node.rb', line 37

def is_text
  text?
end

#node_sizeObject



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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


33
34
35
# File 'lib/prosereflect/schema/node.rb', line 33

def text?
  @type.text?
end

#to_hObject



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_sObject



161
162
163
# File 'lib/prosereflect/schema/node.rb', line 161

def to_s
  "<Node #{@type.name}>"
end

#with_text(new_text) ⇒ Object



65
66
67
# File 'lib/prosereflect/schema/node.rb', line 65

def with_text(new_text)
  TextNode.new(type: @type, attrs: @attrs, text: new_text, marks: @marks)
end