Class: WebkitRemote::Client::DomNode

Inherits:
Object
  • Object
show all
Defined in:
lib/webkit_remote/client/dom.rb

Overview

Cached information about a DOM node.

Constant Summary collapse

NODE_TYPES =

Maps numeric DOM types to their symbolic representation.

{
  1 => :element, 2 => :attribute, 3 => :text, 4 => :cdata_section,
  5 => :entity_reference, 6 => :entity, 7 => :processing_instruction,
  8 => :comment, 9 => :document, 10 => :document_type,
  11 => :document_fragment, 12 => :notation
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(remote_id, client) ⇒ DomNode

Returns a new instance of DomNode.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/webkit_remote/client/dom.rb', line 208

def initialize(remote_id, client)
  @remote_id = remote_id
  @client = client

  @attributes = nil
  @attr_name = nil
  @attr_value = nil
  @children = nil
  @content_document = nil
  @document_url = nil
  @internal_subset = nil
  @js_object = nil
  @local_name = nil
  @name = nil
  @node_type = nil
  @outer_html = nil
  @public_id = nil
  @system_id = nil
  @value = nil
  @xml_version = nil

  initialize_modules
end

Instance Attribute Details

#attr_nameString (readonly)

Returns name, for attribute nodes.

Returns:

  • (String)

    name, for attribute nodes



78
79
80
# File 'lib/webkit_remote/client/dom.rb', line 78

def attr_name
  @attr_name
end

#attr_valueString (readonly)

Returns value, for attribute nodes.

Returns:

  • (String)

    value, for attribute nodes



80
81
82
# File 'lib/webkit_remote/client/dom.rb', line 80

def attr_value
  @attr_value
end

#childrenArray<WebkitRemote::Client::DomNode> (readonly)

Returns children nodes.

Returns:



66
67
68
# File 'lib/webkit_remote/client/dom.rb', line 66

def children
  @children
end

#document_urlWebkitRemote::Client::DomNode, String (readonly)

Returns:



92
93
94
# File 'lib/webkit_remote/client/dom.rb', line 92

def document_url
  @document_url
end

#internal_subsetString (readonly)

Returns internal subset, for doctype nodes.

Returns:

  • (String)

    internal subset, for doctype nodes



83
84
85
# File 'lib/webkit_remote/client/dom.rb', line 83

def internal_subset
  @internal_subset
end

#local_nameString (readonly)

Returns the node’s local name.

Returns:

  • (String)

    the node’s local name



69
70
71
# File 'lib/webkit_remote/client/dom.rb', line 69

def local_name
  @local_name
end

#nameString (readonly)

Returns the node’s name.

Returns:

  • (String)

    the node’s name



71
72
73
# File 'lib/webkit_remote/client/dom.rb', line 71

def name
  @name
end

#node_typeSymbol (readonly)

Returns the DOM node type (such as :element, :text, :attribute).

Returns:

  • (Symbol)

    the DOM node type (such as :element, :text, :attribute)



75
76
77
# File 'lib/webkit_remote/client/dom.rb', line 75

def node_type
  @node_type
end

#public_idString (readonly)

Returns public ID, for doctype nodes.

Returns:

  • (String)

    public ID, for doctype nodes



85
86
87
# File 'lib/webkit_remote/client/dom.rb', line 85

def public_id
  @public_id
end

#system_idString (readonly)

Returns system ID, for doctype nodes.

Returns:

  • (String)

    system ID, for doctype nodes



87
88
89
# File 'lib/webkit_remote/client/dom.rb', line 87

def system_id
  @system_id
end

#valueString (readonly)

Returns the node’s value.

Returns:

  • (String)

    the node’s value



73
74
75
# File 'lib/webkit_remote/client/dom.rb', line 73

def value
  @value
end

#xml_versionString (readonly)

Returns the XML version, for document nodes.

Returns:

  • (String)

    the XML version, for document nodes



94
95
96
# File 'lib/webkit_remote/client/dom.rb', line 94

def xml_version
  @xml_version
end

Class Method Details

.initializer(name) ⇒ Object

Registers a module initializer.



237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/webkit_remote/client/dom.rb', line 237

def self.initializer(name)
  before_name = :"initialize_modules_before_#{name}"
  alias_method before_name, :initialize_modules
  private before_name
  remove_method :initialize_modules
  eval <<END_METHOD
    def initialize_modules
      #{name}
      #{before_name.to_s}
    end
END_METHOD
  private :initialize_modules
end

Instance Method Details

#attributesHash<String, Object>

Returns the node’s attributes.

Returns:

  • (Hash<String, Object>)

    the node’s attributes



97
98
99
# File 'lib/webkit_remote/client/dom.rb', line 97

def attributes
  @attributes ||= attributes!
end

#attributes!Hash<String, Object>

Retrieves this node’s attributes, bypassing its cache.

Returns:

  • (Hash<String, Object>)

    the node’s attributes



104
105
106
107
# File 'lib/webkit_remote/client/dom.rb', line 104

def attributes!
  result = @client.rpc.call 'DOM.getAttributes', nodeId: @remote_id
  @attributes = Hash[result['attributes'].each_slice(2).to_a]
end

#highlight!(options) ⇒ Object

Highlights this DOM node.

Parameters:

  • options (Hash<Symbol, Hash>)

    colors to be used for highlighting

Options Hash (options):

  • margin (Hash<Symbol, Number>)

    color used for highlighting the element’s border

  • border (Hash<Symbol, Number>)

    color used for highlighting the element’s border

  • padding (Hash<Symbol, Number>)

    color used for highlighting the element’s padding

  • content (Hash<Symbol, Number>)

    color used for highlighting the element’s content

  • tooltip (Boolean)

    if true, a tooltip containing node information is also shown



196
197
198
199
200
201
202
203
204
205
# File 'lib/webkit_remote/client/dom.rb', line 196

def highlight!(options)
  config = {}
  config[:marginColor] = options[:margin] if options[:margin]
  config[:borderColor] = options[:border] if options[:border]
  config[:paddingColor] = options[:padding] if options[:padding]
  config[:contentColor] = options[:content] if options[:content]
  config[:showInfo] = true if options[:tooltip]
  @client.rpc.call 'DOM.highlightNode', nodeId: @remote_id,
                   highlightConfig: config
end

#js_objectWebkitRemote::Client::JsObject

Returns this node’s JavaScript object.

Returns:



110
111
112
# File 'lib/webkit_remote/client/dom.rb', line 110

def js_object
  @js_object ||= js_object!
end

#js_object!(group = nil) ⇒ WebkitRemote::Client::JsObject

Retrieves this node’s JavaScript object, bypassing the node’s cache.

Parameters:

  • group (String) (defaults to: nil)

    the name of an object group (think memory pools); the objects in a group can be released together by one call to WebkitRemote::Client::JsObjectGroup#release

Returns:



120
121
122
123
124
125
# File 'lib/webkit_remote/client/dom.rb', line 120

def js_object!(group = nil)
  group ||= @client.object_group_auto_name
  result = @client.rpc.call 'DOM.resolveNode', nodeId: @remote_id,
                            groupName: group
  WebkitRemote::Client::JsObject.for result['object'], @client, group
end

#outer_htmlString

Returns HTML markup for the node and all its contents.

Returns:

  • (String)

    HTML markup for the node and all its contents



128
129
130
# File 'lib/webkit_remote/client/dom.rb', line 128

def outer_html
  @outer_html ||= outer_html!
end

#outer_html!String

Returns HTML markup for the node and all its contents.

Returns:

  • (String)

    HTML markup for the node and all its contents



133
134
135
136
# File 'lib/webkit_remote/client/dom.rb', line 133

def outer_html!
  result = @client.rpc.call 'DOM.getOuterHTML', nodeId: @remote_id
  @outer_html = result['outerHTML']
end

#query_selector(css_selector) ⇒ WebkitRemote::Client::DomNode

Retrieves the first descendant of this node that matches a CSS selector.

Parameters:

  • css_selector (String)

    the CSS selector that must be matched by the returned node

Returns:

  • (WebkitRemote::Client::DomNode)

    the first DOM node in this node’s subtree that matches the given selector; if no such node exists, nil is returned



145
146
147
148
149
150
151
# File 'lib/webkit_remote/client/dom.rb', line 145

def query_selector(css_selector)
  result = @client.rpc.call 'DOM.querySelector', nodeId: @remote_id,
                            selector: css_selector
  node_id = result['nodeId']
  return nil if node_id == 0
  @client.dom_node result['nodeId']
end

#query_selector_all(css_selector) ⇒ Array<WebkitRemote::Client::DomNode>

Retrieves all this node’s descendants that match a CSS selector.

Parameters:

  • css_selector (String)

    the CSS selector used to filter this node’s subtree

Returns:



159
160
161
162
163
# File 'lib/webkit_remote/client/dom.rb', line 159

def query_selector_all(css_selector)
  result = @client.rpc.call 'DOM.querySelectorAll', nodeId: @remote_id,
                            selector: css_selector
  result['nodeIds'].map { |remote_id| @client.dom_node remote_id }
end

#removeWebkitRemote::Client::DomNode

Removes this node from the document.

Returns:



178
179
180
181
# File 'lib/webkit_remote/client/dom.rb', line 178

def remove
  @client.rpc.call 'DOM.removeNode', nodeId: @remote_id
  self
end

#remove_attribute(attr_name) ⇒ WebkitRemote::Client::DomNode

Deletes one of the node (element)‘s attributes.

Parameters:

  • attr_name (String)

    name of the attribute that will be deleted

Returns:



169
170
171
172
173
# File 'lib/webkit_remote/client/dom.rb', line 169

def remove_attribute(attr_name)
  @attributes.delete attr_name if @attributes
  @client.rpc.call 'DOM.removeAttribute', nodeId: @remote_id, name: attr_name
  self
end

#update_all(raw_node) ⇒ WebkitRemote::Client::DomNode

Updates node state to reflect new data from the Webkit debugging server.

Parameters:

  • raw_node (Hash<String, Object>)

    a Node data structure in the DOM domain, as returned by a raw JSON RPC call to a Webkit remote debugging server

Returns:



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/webkit_remote/client/dom.rb', line 259

def update_all(raw_node)
  if raw_node['attributes']
    @attributes = Hash[raw_node['attributes'].each_slice(2).to_a]
  end
  if raw_node['children']
    @children = raw_node['children'].map do |child_node|
      @client.dom_update_node child_node
    end
  end
  if raw_node['contentDocument']
    @content_document = @client.dom_update_node raw_node['contentDocument']
  end
  @document_url = raw_node['documentURL'] if raw_node['documentURL']
  @internal_subset = raw_node['internalSubset'] if raw_node['internalSubset']
  @node_local_name = raw_node['localName'] if raw_node['localName']
  @attr_name = raw_node['name'] if raw_node['name']
  @name = raw_node['nodeName'] if raw_node['nodeName']
  if raw_node['nodeType']
    @node_type = NODE_TYPES[raw_node['nodeType'].to_i] || raw_node['nodeType']
  end
  @value = raw_node['nodeValue'] if raw_node['nodeValue']
  @public_id = raw_node['publicId'] if raw_node['publicId']
  @system_id = raw_node['systemId'] if raw_node['systemId']
  @attr_value = raw_node['value'] if raw_node['value']
  @xml_version = raw_node['xmlVersion'] if raw_node['xmlVersion']

  self
end