Class: Sawzall::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/sawzall.rb

Instance Method Summary collapse

Instance Method Details

#attr(attribute) ⇒ String, Nil

Returns the given attribute’s value or nil

Examples:

doc = Sawzall.parse_fragment("<h1 id='title'>Heading</h1>")
h1 = doc.select("h1").first
h1.attr("id") #=> "title"
h1.attr("class") #=> nil

Parameters:

  • attribute (String)

Returns:

  • (String, Nil)


# File 'lib/sawzall.rb', line 39

#child_elementsArray<Sawzall::Element>

Returns the element’s child elements

Examples:

doc = Sawzall.parse_fragment(<<~HTML)
  <div id="parent">
    <div id="child1">
      <div id="grandchild1"></div>
    </div>
    <div id="child2"></div>
  </div>
HTML
parent = doc.select("#parent").first
parent
  .child_elements
  .map { it.attr("id") } #=> ["child1", "child2"]

Returns:



# File 'lib/sawzall.rb', line 71

#htmlString

Returns the element’s outer HTML

Examples:

doc = Sawzall.parse_fragment(<<~HTML)
  <section>
    <h1>Heading</h1>
  </section>
HTML
section = doc.select("section").first
section.html #=> "<section>\n<h1>Heading</h1>\n</section>"

Returns:

  • (String)


# File 'lib/sawzall.rb', line 11

#inner_htmlString

Returns the element’s inner HTML

Examples:

doc = Sawzall.parse_fragment(<<~HTML)
  <section>
    <h1>Heading</h1>
  </section>
HTML
section = doc.select("section").first
section.inner_html #=> "\n<h1>Heading</h1>\n"

Returns:

  • (String)


# File 'lib/sawzall.rb', line 25

#nameString

Returns the element’s name in lowercase

Examples:

doc = Sawzall.parse_fragment("<p>Paragraph</p>")
doc.select("p").first.name #=> "p"

Returns:

  • (String)


# File 'lib/sawzall.rb', line 2

#select(css_selector) ⇒ Array<Sawzall::Element>

Returns the child elements that match the given CSS selector

developer.mozilla.org/en-US/docs/Web/CSS/CSS_selectors

Examples:

doc = Sawzall.parse_fragment(<<~HTML)
  <div class="container">
    <div>inner div 1</div>
    <div>inner div 2</div>
  </div>
HTML
container = doc.select(".container").first
matches = container.select("div")
matches.map(&:text) #=> ["inner div 1", "inner div 2"]

Parameters:

  • css_selector (String)

Returns:

Raises:

  • (ArgumentError)

    if the CSS selector is invalid



# File 'lib/sawzall.rb', line 51

#textString

Returns the element’s text content using a very simplified version of the innerText algorithm.

developer.mozilla.org/en-US/docs/Web/API/HTMLElement/innerText

Examples:

doc = Sawzall.parse_fragment(<<~HTML)
  <ul>
    <li>First item</li>
    <li>Second item</li>
  </ul>
HTML
ul = doc.select("ul").first
ul.text #=> "First item\nSecond item"

Returns:

  • (String)


# File 'lib/sawzall.rb', line 90