Class: StringDoc::Node Private

Inherits:
Object
  • Object
show all
Includes:
Pakyow::Support::Inspectable
Defined in:
lib/string_doc/node.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

String-based XML node.

Constant Summary collapse

SELF_CLOSING =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%w[area base basefont br hr input img link meta].freeze
FORM_INPUTS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%w[input select textarea button].freeze
VALUELESS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%w[select].freeze
REGEX_TAGS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/<[^>]*>/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag_open_start = "", attributes = Attributes.new, tag_open_end = "", children = StringDoc.empty, tag_close = "", parent: nil, significance: [], labels: {}) ⇒ Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Node.



48
49
50
51
52
53
54
# File 'lib/string_doc/node.rb', line 48

def initialize(tag_open_start = "", attributes = Attributes.new, tag_open_end = "", children = StringDoc.empty, tag_close = "", parent: nil, significance: [], labels: {})
  @tag_open_start, @attributes, @tag_open_end, @children, @tag_close = tag_open_start, attributes, tag_open_end, children, tag_close
  @parent, @labels, @significance = parent, labels, significance
  @transforms = { high: [], default: [], low: [] }
  @pipeline = nil
  @finalized_labels = {}
end

Instance Attribute Details

#attributesObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



35
36
37
# File 'lib/string_doc/node.rb', line 35

def attributes
  @attributes
end

#childrenObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



38
39
40
# File 'lib/string_doc/node.rb', line 38

def children
  @children
end

#nodeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



38
39
40
# File 'lib/string_doc/node.rb', line 38

def node
  @node
end

#parentObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



38
39
40
# File 'lib/string_doc/node.rb', line 38

def parent
  @parent
end

#significanceObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



38
39
40
# File 'lib/string_doc/node.rb', line 38

def significance
  @significance
end

#tag_closeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



38
39
40
# File 'lib/string_doc/node.rb', line 38

def tag_close
  @tag_close
end

#tag_open_endObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



38
39
40
# File 'lib/string_doc/node.rb', line 38

def tag_open_end
  @tag_open_end
end

#tag_open_startObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



38
39
40
# File 'lib/string_doc/node.rb', line 38

def tag_open_start
  @tag_open_start
end

#transformsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



38
39
40
# File 'lib/string_doc/node.rb', line 38

def transforms
  @transforms
end

Class Method Details

.form_input?(tag) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns true if tag is a form input.

Returns:

  • (Boolean)


24
25
26
# File 'lib/string_doc/node.rb', line 24

def form_input?(tag)
  FORM_INPUTS.include?(tag)
end

.self_closing?(tag) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns true if tag is self-closing.

Returns:

  • (Boolean)


18
19
20
# File 'lib/string_doc/node.rb', line 18

def self_closing?(tag)
  SELF_CLOSING.include?(tag)
end

.without_value?(tag) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns true if tag does not contain a value.

Returns:

  • (Boolean)


30
31
32
# File 'lib/string_doc/node.rb', line 30

def without_value?(tag)
  VALUELESS.include?(tag)
end

Instance Method Details

#==(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



314
315
316
317
318
319
320
321
# File 'lib/string_doc/node.rb', line 314

def ==(other)
  other.is_a?(Node) &&
    @tag_open_start == other.tag_open_start &&
    @attributes == other.attributes &&
    @tag_open_end == other.tag_open_end &&
    @children == other.children &&
    @tag_close == other.tag_close
end

#after(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Inserts node after self.



220
221
222
# File 'lib/string_doc/node.rb', line 220

def after(node)
  @parent.insert_after(node, self)
end

#append(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Appends node as a child.



232
233
234
# File 'lib/string_doc/node.rb', line 232

def append(node)
  children.append(node)
end

#append_html(html) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Appends html as a child.



238
239
240
# File 'lib/string_doc/node.rb', line 238

def append_html(html)
  children.append_html(html)
end

#before(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Inserts node before self.



226
227
228
# File 'lib/string_doc/node.rb', line 226

def before(node)
  @parent.insert_before(node, self)
end

#clearObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Removes all children.



214
215
216
# File 'lib/string_doc/node.rb', line 214

def clear
  children.clear
end

#close(tag, child) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Close self with tag and a child.



121
122
123
124
125
126
127
# File 'lib/string_doc/node.rb', line 121

def close(tag, child)
  tap do
    @children = StringDoc.from_nodes(child)
    @tag_open_end = tag ? ">" : ""
    @tag_close = (tag && !self.class.self_closing?(tag)) ? "</#{tag}>" : ""
  end
end

#delete_label(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Delete the label with name.



277
278
279
# File 'lib/string_doc/node.rb', line 277

def delete_label(name)
  @labels.delete(name.to_sym)
end

#each(descend: false) {|_self| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Yields:

  • (_self)

Yield Parameters:



323
324
325
326
# File 'lib/string_doc/node.rb', line 323

def each(descend: false)
  return enum_for(:each, descend: descend) unless block_given?
  yield self
end

#each_significant_node(type, descend: false, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



328
329
330
331
332
333
334
# File 'lib/string_doc/node.rb', line 328

def each_significant_node(type, descend: false, &block)
  return enum_for(:each_significant_node, type, descend: descend) unless block_given?

  if @children.is_a?(StringDoc)
    @children.each_significant_node(type, descend: descend, &block)
  end
end

#each_significant_node_with_name(type, name, descend: false, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



344
345
346
347
348
349
350
# File 'lib/string_doc/node.rb', line 344

def each_significant_node_with_name(type, name, descend: false, &block)
  return enum_for(:each_significant_node_with_name, type, name, descend: descend) unless block_given?

  if @children.is_a?(StringDoc)
    @children.each_significant_node_with_name(type, name, descend: descend, &block)
  end
end

#each_significant_node_without_descending_into_type(type, descend: false, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



336
337
338
339
340
341
342
# File 'lib/string_doc/node.rb', line 336

def each_significant_node_without_descending_into_type(type, descend: false, &block)
  return enum_for(:each_significant_node_without_descending_into_type, type, descend: descend) unless block_given?

  if @children.is_a?(StringDoc)
    @children.each_significant_node_without_descending_into_type(type, descend: descend, &block)
  end
end

#empty?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


114
115
116
# File 'lib/string_doc/node.rb', line 114

def empty?
  to_s.strip.empty?
end

#finalize_labels(keep: []) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



93
94
95
96
97
98
99
100
101
102
# File 'lib/string_doc/node.rb', line 93

def finalize_labels(keep: [])
  @finalized_labels = @labels
  @labels = keep.each_with_object({}) { |key, hash|
    hash[key] = @finalized_labels.delete(key).deep_dup
  }

  if children.is_a?(StringDoc)
    children.finalize_labels(keep: keep)
  end
end

#find_first_significant_node(type, descend: false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



352
353
354
355
356
357
358
# File 'lib/string_doc/node.rb', line 352

def find_first_significant_node(type, descend: false)
  if @children.is_a?(StringDoc)
    @children.find_first_significant_node(type, descend: descend)
  else
    nil
  end
end

#find_significant_nodes(type, descend: false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



360
361
362
363
364
365
366
# File 'lib/string_doc/node.rb', line 360

def find_significant_nodes(type, descend: false)
  if @children.is_a?(StringDoc)
    @children.find_significant_nodes(type, descend: descend)
  else
    []
  end
end

#find_significant_nodes_with_name(type, name, descend: false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



368
369
370
371
372
373
374
# File 'lib/string_doc/node.rb', line 368

def find_significant_nodes_with_name(type, name, descend: false)
  if @children.is_a?(StringDoc)
    @children.find_significant_nodes_with_name(type, name, descend: descend)
  else
    []
  end
end

#freezeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



108
109
110
111
# File 'lib/string_doc/node.rb', line 108

def freeze(*)
  pipeline
  super
end

#htmlObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the html contained within self.



190
191
192
# File 'lib/string_doc/node.rb', line 190

def html
  children.to_s
end

#html=(html) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Replaces self’s inner html, without making it available for further manipulation.



196
197
198
# File 'lib/string_doc/node.rb', line 196

def html=(html)
  @children = html.to_s
end

#initialize_copy(_) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/string_doc/node.rb', line 57

def initialize_copy(_)
  super

  @labels = @labels.deep_dup
  @finalized_labels = @finalized_labels.deep_dup
  @attributes = @attributes.dup
  @children = @children.dup
  @significance = @significance.dup

  @transforms = @transforms.each_with_object({}) { |(key, value), hash|
    hash[key] = value.dup
  }

  @pipeline = nil
end

#label(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the value for label with name.



250
251
252
253
254
255
256
257
# File 'lib/string_doc/node.rb', line 250

def label(name)
  name = name.to_sym
  if @labels.key?(name)
    @labels[name.to_sym]
  else
    @finalized_labels[name.to_sym]
  end
end

#labeled?(name) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns true if label exists with name.

Returns:

  • (Boolean)


261
262
263
# File 'lib/string_doc/node.rb', line 261

def labeled?(name)
  @labels.key?(name.to_sym) || @finalized_labels.key?(name.to_sym)
end

#labelsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



104
105
106
# File 'lib/string_doc/node.rb', line 104

def labels
  @labels.merge(@finalized_labels)
end

#next_transformObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



129
130
131
# File 'lib/string_doc/node.rb', line 129

def next_transform
  pipeline.shift
end

#prepend(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prepends node as a child.



244
245
246
# File 'lib/string_doc/node.rb', line 244

def prepend(node)
  children.prepend(node)
end

#remove(label = true, descend = true) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Removes the node.



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/string_doc/node.rb', line 166

def remove(label = true, descend = true)
  if label
    set_label(:removed, true)
  end

  @parent.remove_node(self)

  if descend && children.is_a?(StringDoc)
    children.each do |child|
      child.remove(label, descend)
    end
  end
end

#removed?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


271
272
273
# File 'lib/string_doc/node.rb', line 271

def removed?
  labeled?(:removed)
end

#render(output = String.new, context: nil) ⇒ Object Also known as: to_html, to_xml

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/string_doc/node.rb', line 281

def render(output = String.new, context: nil)
  if transforms_itself?
    __transform(output, context: context)
  else
    output << tag_open_start

    attributes.each_string do |attribute_string|
      output << attribute_string
    end

    output << tag_open_end

    case children
    when StringDoc
      children.render(output, context: context)
    else
      output << children.to_s
    end

    output << tag_close
  end

  output
end

#replace(replacement) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Replaces the current node.



160
161
162
# File 'lib/string_doc/node.rb', line 160

def replace(replacement)
  @parent.replace_node(self, replacement)
end

#replace_children(children) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Replaces self’s children.



202
203
204
# File 'lib/string_doc/node.rb', line 202

def replace_children(children)
  @children.replace(children)
end

#set_label(name, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets the label with name and value.



267
268
269
# File 'lib/string_doc/node.rb', line 267

def set_label(name, value)
  @labels[name.to_sym] = value
end

#significance?(*types) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


154
155
156
# File 'lib/string_doc/node.rb', line 154

def significance?(*types)
  (@significance & types).any?
end

#significant?(type = nil) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


146
147
148
149
150
151
152
# File 'lib/string_doc/node.rb', line 146

def significant?(type = nil)
  if type
    @significance.include?(type.to_sym)
  else
    @significance.any?
  end
end

#soft_copyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/string_doc/node.rb', line 74

def soft_copy
  instance = self.class.allocate

  instance.instance_variable_set(:@tag_open_start, @tag_open_start)
  instance.instance_variable_set(:@tag_open_end, @tag_open_end)
  instance.instance_variable_set(:@tag_close, @tag_close)
  instance.instance_variable_set(:@parent, @parent)
  instance.instance_variable_set(:@significance, @significance)
  instance.instance_variable_set(:@transforms, @transforms)
  instance.instance_variable_set(:@finalized_labels, @finalized_labels)

  instance.instance_variable_set(:@attributes, @attributes.dup)
  instance.instance_variable_set(:@children, @children.is_a?(StringDoc) ? @children.soft_copy : @children.dup)
  instance.instance_variable_set(:@labels, @labels.deep_dup)
  instance.instance_variable_set(:@pipeline, @pipeline.dup)

  instance
end

#tagnameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the node’s tagname.



208
209
210
# File 'lib/string_doc/node.rb', line 208

def tagname
  @tag_open_start.gsub(/[^a-zA-Z0-9]/, "")
end

#textObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the text of this node and all children, joined together.



184
185
186
# File 'lib/string_doc/node.rb', line 184

def text
  html.gsub(REGEX_TAGS, "")
end

#to_sObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the node as an xml string, without transforming.



310
311
312
# File 'lib/string_doc/node.rb', line 310

def to_s
  string_nodes.flatten.map(&:to_s).join
end

#transform(priority: :default, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



133
134
135
136
# File 'lib/string_doc/node.rb', line 133

def transform(priority: :default, &block)
  @transforms[priority] << block
  @pipeline = nil
end

#transforms?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


138
139
140
# File 'lib/string_doc/node.rb', line 138

def transforms?
  transforms_itself? || @children.transforms?
end

#transforms_itself?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


142
143
144
# File 'lib/string_doc/node.rb', line 142

def transforms_itself?
  pipeline.any?
end