`

HTML::Selector #select #match

阅读更多
match(element, first?) => array or nil

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



select(root) => array

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
453:     def select(root)
454:       matches = []
455:       stack = [root]
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



分享到:
评论

相关推荐

    XSLT元素使用说明

    `&lt;xsl:apply-templates select="selector"/&gt;` 其中`select`属性指定一个XPath表达式,用于选择将应用于模板的节点集。 #### 例子 假设我们有以下XML文档: ```xml &lt;text&gt;Hello, World! ``` 我们可以使用`&lt;xsl:...

    SelectMaster.rar

    在Android开发中,Selector是一种非常重要的图形元素,用于定义不同状态下的视图样式。Selector通常以XML文件的形式存在,可以在Drawable资源目录下创建。本文将深入探讨如何在CardView和GridView中使用Selector来...

    Android使用selector修改TextView中字体颜色和背景色的方法

    在Android开发中,Selector是用于定义视图状态变化时外观的一种资源,它可以是颜色、图像或者形状等。本文将深入探讨如何使用Selector来修改TextView的字体颜色和背景颜色。 首先,我们来创建两个XML文件,分别用于...

    批量下载图片

    from cssselect import CSSSelector # 目标网址 url = "http://example.com" # 发起GET请求获取网页内容 response = requests.get(url) html_content = response.text # 使用BeautifulSoup解析HTML soup = ...

    rematch-learn:探究rematch原始码如何实现

    4. **插件系统(Plugin System)**:Rematch 有丰富的插件生态系统,例如 `@rematch/loading` 用于处理加载状态,`@rematch/select` 提供类似于 React-Redux 的 `useSelector` 钩子。 接下来,我们逐一分析 Rematch...

    登录及注册界面界面并用selector和shape进行美化

    android:layout_width="match_parent" android:layout_height="200dp" app:indicator_visibility="invisible"/&gt; ``` 在代码中,我们需要初始化`SliderLayout`,设置Adapter加载图片,并监听滑动事件: ```java ...

    Prototype使用指南之selector.js说明

    此外,`Selector`对象还提供了其他实用的方法,例如`select`(等同于`findElements`),以及用于组合多个选择器的`combine`方法。这些功能使得在JavaScript中操作DOM元素变得更加方便和高效。 总的来说,Prototype...

    jQuery完全实例.rar

    This function accepts a string containing a CSS selector which is then used to match a set of elements. The core functionality of jQuery centers around this function. Everything in jQuery is based ...

    Prototype Cheat Sheet

    - **`match(element, selector)`**: Determines if an element matches a CSS selector. - **`next(element[, cssRule][, index=0])`**: Selects the next sibling element based on a CSS selector and an ...

    jquery 自动完成 Autocomplete

    `selector` 是你的输入框元素选择器,`options` 是一个包含各种配置项的对象,如数据源(`source`)、匹配方式(`match`)等。 ### 2. 数据源 数据源是 Autocomplete 的核心,它可以是静态数组、函数、AJAX 请求...

    prototype手册1.6.0.2版本

    - **`immediateDescendants`**、**`insert(element,{position:content})`**、**`inspect(element)`**、**`makeClipping(element)`**、**`makePositioned(element)`**、**`match(element, selector)`**、**`next...

    Atuocomplete自动完成

    要使输入框具备自动完成功能,我们需要使用`$(selector).autocomplete(options)`方法。例如,我们可以为id为`autocomplete-input`的输入框添加Autocomplete功能: ```javascript $(document).ready(function() { $...

    C#(.NET)常用函数汇总

    - `Select, R&gt;(Func, R&gt; selector)`:投影元素。 - `OrderBy(Func, TKey&gt; keySelector)`:按指定键排序。 - `GroupBy, TKey&gt;(Func, TKey&gt; keySelector)`:按键进行分组。 以上只是C#中众多常用函数的一部分,...

    PLSQL.Developer v11.0.0.1762 主程序+ v10中文包+keygen

    The align button has a new Window width option to resize the SQL Window width to match the result set width You can now use an AltRowColor=[RRGGBB | name] directive in a comment section of the SQL ...

    Android流式布局FlowLayout详解

    android:textColor="@drawable/text_color_selector" &lt;!-- 文字颜色选择器 --&gt; /&gt; ``` 总的来说,FlowLayout为开发者提供了一种灵活的方式来排列视图,特别是在需要适应不同屏幕尺寸和动态数据时。虽然其内部实现...

    GridView实现多选,全选,反选功能

    android:listSelector="@drawable/list_selector" android:choiceMode="CHOICE_MODE_MULTIPLE" /&gt; ``` 2. **自定义Adapter** 为了实现多选功能,我们需要自定义一个继承自BaseAdapter的Adapter。在Adapter中,...

    OptionMonad

    public static Option&lt;TOut&gt; Select, TOut&gt;(this Option&lt;TIn&gt; option, Func, TOut&gt; selector) { return option.Match( some: value =&gt; Option.Some(selector(value)), none: () =&gt; Option.None() ); } public ...

    Android实现动态添加标签及其点击事件

    android:background="@drawable/mark_select" android:enabled="false" android:padding="10dp" android:text="TextView" /&gt; ``` 在上面的代码中,我们定义了一个 TextView,用于显示标签的文字,并设置了...

    MultipleChoice:GridView多项选择

    为了实现全选和全取消功能,我们还可以添加两个按钮,分别调用Adapter的`selectAll()`和`clearSelection()`方法。同时,为了保存用户的选中状态,可以在`onSaveInstanceState()`和`onRestoreInstanceState()`中序列...

Global site tag (gtag.js) - Google Analytics