Class: Sawzall::Element

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

1) Querying collapse

2) Debugging 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 116

#attrsArray<Array(String, String)>

Returns the element’s attributes as an array of key-value pairs

Examples:

doc = Sawzall.parse_fragment("<h1 id='title' class='big'>Heading</h1>")
h1 = doc.select("h1").first
h1.attrs #=> [["class", "big"], ["id", "title"]]

Returns:

  • (Array<Array(String, String)>)


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

#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 158

#classesArray<String>

Returns the element’s classes

Examples:

doc = Sawzall.parse_fragment("<h1 class='one two'>Heading</h1>")
h1 = doc.select("h1").first
h1.classes #=> ["one", "two"]

Returns:

  • (Array<String>)


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

#has_class?(css_class, case_sensitive: true) ⇒ Boolean

Checks whether the element has the given class

Examples:

doc = Sawzall.parse_fragment("<h1 class='title'>Heading</h1>")
h1 = doc.select("h1").first
h1.has_class?("title") #=> true
h1.has_class?("TITLE", case_sensitive: false) #=> true
h1.has_class?("heading") #=> false

Parameters:

  • css_class (String)
  • case_sensitive (Boolean) (defaults to: true)

    Whether matching should be case sensitive. When false, only ASCII characters are matched case-insensitively.

Returns:

  • (Boolean)


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

#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 88

#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 102

#inspectObject

Overrides Ruby’s default Object#inspect so the output is a bit more useful



225
226
227
# File 'lib/sawzall.rb', line 225

def inspect
  "<#{self.class.name} name=#{name.inspect} child_elements=#{child_elements.inspect}>"
end

#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 79

#pretty_print(pp) ⇒ Object

Provides a custom pretty-printing implementation for Ruby’s PP



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/sawzall.rb', line 230

def pretty_print(pp)
  pp.group(2, "#(#{self.class.name} {", "})") do
    pp.breakable

    fields = [:name]
    fields << :child_elements unless child_elements.empty?

    pp.seplist(fields) do |field|
      case field
      when :name
        pp.text("name = ")
        pp.pp(name)
      when :child_elements
        pp.group(2, "child_elements = [", "]") do
          pp.breakable
          pp.seplist(child_elements) do |child|
            pp.pp(child)
          end
        end
      end
    end

    pp.breakable
  end
end

#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 138

#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 177