Class: Dommy::ProcessingInstruction

Inherits:
Object
  • Object
show all
Includes:
Bridge::Methods, EventTarget, Node
Defined in:
lib/dommy/document.rb

Overview

ProcessingInstruction (‘<?target data?>`) — a CharacterData-like node with a `target`; created via `document.createProcessingInstruction`.

Constant Summary

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::HTML_NAMESPACE, Node::PROCESSING_INSTRUCTION_NODE, Node::TEXT_NODE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Bridge::Methods

included

Methods included from EventTarget

#__internal_deliver_event__, #add_event_listener, capture_flag, #deliver_at, #dispatch_event, js_truthy?, #remove_event_listener

Methods included from Node

#compare_document_position, #get_root_node, #is_default_namespace, #is_equal_node, #is_same_node, #lookup_namespace_uri, #lookup_prefix

Constructor Details

#initialize(target, data) ⇒ ProcessingInstruction

Returns a new instance of ProcessingInstruction.



130
131
132
133
# File 'lib/dommy/document.rb', line 130

def initialize(target, data)
  @target = target.to_s
  @data = data.to_s
end

Instance Attribute Details

#targetObject (readonly)

Returns the value of attribute target.



128
129
130
# File 'lib/dommy/document.rb', line 128

def target
  @target
end

Instance Method Details

#__internal_event_parent__Object



199
200
201
# File 'lib/dommy/document.rb', line 199

def __internal_event_parent__
  nil
end

#__js_call__(method, args) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/dommy/document.rb', line 208

def __js_call__(method, args)
  case method
  when "hasChildNodes"
    false
  when "isEqualNode"
    is_equal_node(args[0])
  when "isSameNode"
    is_same_node(args[0])
  when "getRootNode"
    get_root_node(args[0])
  when "compareDocumentPosition"
    compare_document_position(args[0])
  when "appendChild", "insertBefore"
    raise DOMException::HierarchyRequestError, "a ProcessingInstruction may not have children"
  when "removeChild", "replaceChild"
    raise DOMException::NotFoundError, "the node to be removed is not a child of this node"
  when "before", "after", "replaceWith", "remove"
    # ChildNode mixin: a created PI has no parent, so these are no-ops per
    # spec (they act only when the node is inserted in a tree).
    nil
  when "normalize"
    nil
  when "substringData"
    substring_data(args[0], args[1])
  when "appendData"
    append_data(args[0])
  when "insertData"
    insert_data(args[0], args[1])
  when "deleteData"
    delete_data(args[0], args[1])
  when "replaceData"
    replace_data(args[0], args[1], args[2])
  when "addEventListener"
    add_event_listener(args[0], args[1], args[2])
  when "removeEventListener"
    remove_event_listener(args[0], args[1], args[2])
  when "dispatchEvent"
    dispatch_event(args[0])
  end
end

#__js_get__(key) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/dommy/document.rb', line 139

def __js_get__(key)
  case key
  when "target"
    @target
  when "data", "nodeValue", "textContent"
    @data
  when "nodeName"
    @target
  when "nodeType"
    7
  when "length"
    @data.length
  end
end

#__js_set__(key, value) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/dommy/document.rb', line 154

def __js_set__(key, value)
  case key
  when "data", "nodeValue", "textContent"
    @data = value.to_s
  else
    return Bridge::UNHANDLED
  end
  nil
end

#append_data(value) ⇒ Object



172
173
174
175
# File 'lib/dommy/document.rb', line 172

def append_data(value)
  @data += value.to_s
  nil
end

#dataObject



137
# File 'lib/dommy/document.rb', line 137

def data = @data

#delete_data(offset, count) ⇒ Object



185
186
187
188
189
190
191
# File 'lib/dommy/document.rb', line 185

def delete_data(offset, count)
  o = offset.to_i
  raise DOMException::IndexSizeError, "offset out of bounds" if o.negative? || o > @data.length

  @data = @data[0, o].to_s + (@data[(o + count.to_i)..] || "")
  nil
end

#insert_data(offset, value) ⇒ Object



177
178
179
180
181
182
183
# File 'lib/dommy/document.rb', line 177

def insert_data(offset, value)
  o = offset.to_i
  raise DOMException::IndexSizeError, "offset out of bounds" if o.negative? || o > @data.length

  @data = @data[0, o].to_s + value.to_s + (@data[o..] || "")
  nil
end

#replace_data(offset, count, value) ⇒ Object



193
194
195
196
197
# File 'lib/dommy/document.rb', line 193

def replace_data(offset, count, value)
  delete_data(offset, count)
  insert_data(offset, value)
  nil
end

#substring_data(offset, count) ⇒ Object

A PI is CharacterData: its data methods are string operations on @data.



165
166
167
168
169
170
# File 'lib/dommy/document.rb', line 165

def substring_data(offset, count)
  o = offset.to_i
  raise DOMException::IndexSizeError, "offset out of bounds" if o.negative? || o > @data.length

  @data[o, count.to_i] || ""
end