Class: Dommy::CharacterDataNode

Inherits:
Object
  • Object
show all
Includes:
Node
Defined in:
lib/dommy/element.rb

Overview

CharacterData base — TextNode and CommentNode share the data / nodeValue / textContent API and ‘remove` / `cloneNode` semantics.

Direct Known Subclasses

CommentNode, TextNode

Constant Summary collapse

JS_METHOD_NAMES =

Methods routed through js_call (keep in sync with its when-arms).

%w[remove before after replaceWith].freeze

Constants included from Node

Node::ATTRIBUTE_NODE, Node::CDATA_SECTION_NODE, Node::COMMENT_NODE, Node::DOCUMENT_FRAGMENT_NODE, Node::DOCUMENT_NODE, Node::DOCUMENT_POSITION_CONTAINED_BY, Node::DOCUMENT_POSITION_CONTAINS, Node::DOCUMENT_POSITION_DISCONNECTED, Node::DOCUMENT_POSITION_FOLLOWING, Node::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, Node::DOCUMENT_POSITION_PRECEDING, Node::DOCUMENT_TYPE_NODE, Node::ELEMENT_NODE, Node::PROCESSING_INSTRUCTION_NODE, Node::TEXT_NODE

Instance Method Summary collapse

Constructor Details

#initialize(document, nokogiri_node) ⇒ CharacterDataNode

Returns a new instance of CharacterDataNode.



170
171
172
173
# File 'lib/dommy/element.rb', line 170

def initialize(document, nokogiri_node)
  @document = document
  @__node__ = nokogiri_node
end

Instance Method Details

#[](key) ⇒ Object



230
231
232
# File 'lib/dommy/element.rb', line 230

def [](key)
  __js_get__(key.to_s)
end

#[]=(key, value) ⇒ Object



234
235
236
# File 'lib/dommy/element.rb', line 234

def []=(key, value)
  __js_set__(key.to_s, value)
end

#__dommy_backend_node__Object



168
# File 'lib/dommy/element.rb', line 168

def __dommy_backend_node__ = @__node__

#__js_call__(method, args) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/dommy/element.rb', line 272

def __js_call__(method, args)
  case method
  when "remove"
    remove
  when "before"
    before(*args)
  when "after"
    after(*args)
  when "replaceWith"
    replace_with(*args)
  end
end

#__js_get__(key) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/dommy/element.rb', line 238

def __js_get__(key)
  case key
  when "nodeType"
    node_type
  when "textContent"
    @__node__.content
  when "data"
    @__node__.content
  when "nodeValue"
    @__node__.content
  when "parentNode"
    parent_node
  when "nextSibling"
    next_sibling
  when "previousSibling"
    previous_sibling
  end
end

#__js_method_names__Object



268
269
270
# File 'lib/dommy/element.rb', line 268

def __js_method_names__
  JS_METHOD_NAMES
end

#__js_set__(key, value) ⇒ Object



257
258
259
260
261
262
263
264
# File 'lib/dommy/element.rb', line 257

def __js_set__(key, value)
  case key
  when "textContent", "data", "nodeValue"
    write_data(value)
  end

  nil
end

#after(*args) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/dommy/element.rb', line 301

def after(*args)
  parent = @__node__.parent
  return nil unless parent

  added = args.map { |arg| coerce_node(arg) }.compact
  anchor = @__node__.next_sibling
  if anchor
    added.reverse_each { |node| anchor.add_previous_sibling(node) }
  else
    added.each { |node| parent.add_child(node) }
  end
  notify_child_list_added(parent, added)
  nil
end

#before(*args) ⇒ Object

ChildNode mixin: WHATWG DOM defines ‘before`, `after`, `replaceWith` on all child nodes, including Text and Comment. Implementations operate on the Nokogiri layer and notify the MutationObserver with the underlying nodes (mirroring Element#remove_child / replace_child).



291
292
293
294
295
296
297
298
299
# File 'lib/dommy/element.rb', line 291

def before(*args)
  parent = @__node__.parent
  return nil unless parent

  added = args.map { |arg| coerce_node(arg) }.compact
  added.reverse_each { |node| @__node__.add_previous_sibling(node) }
  notify_child_list_added(parent, added)
  nil
end

#dataObject

Snake_case facade (CRuby idiomatic)



177
178
179
# File 'lib/dommy/element.rb', line 177

def data
  @__node__.content
end

#data=(value) ⇒ Object



181
182
183
# File 'lib/dommy/element.rb', line 181

def data=(value)
  write_data(value)
end

#next_siblingObject



222
223
224
# File 'lib/dommy/element.rb', line 222

def next_sibling
  @__node__.next && @document.wrap_node(@__node__.next)
end

#node_valueObject



185
186
187
# File 'lib/dommy/element.rb', line 185

def node_value
  @__node__.content
end

#node_value=(value) ⇒ Object



189
190
191
# File 'lib/dommy/element.rb', line 189

def node_value=(value)
  write_data(value)
end

#parent_nodeObject



218
219
220
# File 'lib/dommy/element.rb', line 218

def parent_node
  @__node__.parent && @document.wrap_node(@__node__.parent)
end

#previous_siblingObject



226
227
228
# File 'lib/dommy/element.rb', line 226

def previous_sibling
  @__node__.previous && @document.wrap_node(@__node__.previous)
end

#removeObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/dommy/element.rb', line 201

def remove
  parent = @__node__.parent
  removed = @__node__
  @__node__.unlink
  # Mirror Element#remove_child: notify with the Nokogiri::Node
  # (not the Dommy wrapper) so MutationCoordinator's wrap_node
  # cache keys consistently.
  if parent
    @document.notify_child_list_mutation(
      target_node: parent,
      added_nodes: [],
      removed_nodes: [removed]
    )
  end
  nil
end

#replace_with(*args) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/dommy/element.rb', line 316

def replace_with(*args)
  parent = @__node__.parent
  return nil unless parent

  added = args.map { |arg| coerce_node(arg) }.compact
  removed = @__node__
  anchor = @__node__.next_sibling
  @__node__.unlink
  if anchor
    added.reverse_each { |node| anchor.add_previous_sibling(node) }
  else
    added.each { |node| parent.add_child(node) }
  end
  @document.notify_child_list_mutation(
    target_node: parent,
    added_nodes: added,
    removed_nodes: [removed]
  )
  nil
end

#text_contentObject



193
194
195
# File 'lib/dommy/element.rb', line 193

def text_content
  @__node__.content
end

#text_content=(value) ⇒ Object



197
198
199
# File 'lib/dommy/element.rb', line 197

def text_content=(value)
  write_data(value)
end