HTML::Selector #select #match

[b]match(element, first?) => array or nil[/b]

Matches an element against the selector.

For a simple selector this method returns an array with the element if the element matches, nil otherwise.

For a complex selector (sibling and descendant) this method returns an array with all matching elements, nil if no match is found.

Use +first_only=true+ if you are only interested in the first element.

For example:

if selector.match(element)
puts "Element is a login form"
end

[ hide source ]

# File vendor/rails/actionpack/lib/action_controller/vendor/html-scanner/html/selector.rb, line 363
363:     def match(element, first_only = false)
364: # Match element if no element name or element name same as element name
365: if matched = (!@tag_name || @tag_name == element.name)
366: # No match if one of the attribute matches failed
367: for attr in @attributes
368: if element.attributes[attr[0]] !~ attr[1]
369: matched = false
370: break
371: end
372: end
373: end
374:
375: # Pseudo class matches (nth-child, empty, etc).
376: if matched
377: for pseudo in @pseudo
378: unless pseudo.call(element)
379: matched = false
380: break
381: end
382: end
383: end
384:
385: # Negation. Same rules as above, but we fail if a match is made.
386: if matched && @negation
387: for negation in @negation
388: if negation[:tag_name] == element.name
389: matched = false
390: else
391: for attr in negation[:attributes]
392: if element.attributes[attr[0]] =~ attr[1]
393: matched = false
394: break
395: end
396: end
397: end
398: if matched
399: for pseudo in negation[:pseudo]
400: if pseudo.call(element)
401: matched = false
402: break
403: end
404: end
405: end
406: break unless matched
407: end
408: end
409:
410: # If element matched but depends on another element (child,
411: # sibling, etc), apply the dependent matches instead.
412: if matched && @depends
413: matches = @depends.call(element, first_only)
414: else
415: matches = matched ? [element] : nil
416: end
417:
418: # If this selector is part of the group, try all the alternative
419: # selectors (unless first_only).
420: if !first_only || !matches
421: @alternates.each do |alternate|
422: break if matches && first_only
423: if subset = alternate.match(element, first_only)
424: if matches
425: matches.concat subset
426: else
427: matches = subset
428: end
429: end
430: end
431: end
432:
433: matches
434: end



[b]select(root) => array[/b]

Selects and returns an array with all matching elements, beginning with one node and traversing through all children depth-first. Returns an empty array if no match is found.

The root node may be any element in the document, or the document itself.

For example:

selector = HTML::Selector.new "input[type=text]"
matches = selector.select(element)
matches.each do |match|
puts "Found text field with name #{match.attributes['name']}"
end

[ hide source ]

# File vendor/rails/actionpack/lib/action_controller/vendor/html-scanner/html/selector.rb, line 453
[code="java"]453: def select(root)
454: matches = []
455: stack =
456: while node = stack.pop
457: if node.tag? && subset = match(node, false)
458: subset.each do |match|
459: matches << match unless matches.any? { |item| item.equal?(match) }
460: end
461: elsif children = node.children
462: stack.concat children.reverse
463: end
464: end
465: matches
466: end[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值