class OpenStax::Content::Fragment::Html

Attributes

to_html[R]

Public Class Methods

new(node:, title: nil, labels: nil) click to toggle source
Calls superclass method OpenStax::Content::Fragment::new
# File lib/openstax/content/fragment/html.rb, line 8
def initialize(node:, title: nil, labels: nil)
  super

  @node = Nokogiri::HTML.fragment node.to_html
  @to_html = @node.to_html
end

Public Instance Methods

append(new_node) click to toggle source
# File lib/openstax/content/fragment/html.rb, line 45
def append(new_node)
  (node.at_css('body') || node.root) << new_node

  @to_html = node.to_html
end
as_json(*args) click to toggle source
Calls superclass method
# File lib/openstax/content/fragment/html.rb, line 15
def as_json(*args)
  # Don't attempt to serialize @node (it would fail)
  super.except('node')
end
blank?() click to toggle source
# File lib/openstax/content/fragment/html.rb, line 20
def blank?
  return @blank unless @blank.nil?

  @blank = if to_html.nil? || to_html.strip.empty?
    true
  else
    node_without_title = node.dup
    node_without_title.css('[data-type="document-title"]').remove
    text = node_without_title.text
    text.nil? || text.strip.empty?
  end
end
has_css?(css, custom_css) click to toggle source
# File lib/openstax/content/fragment/html.rb, line 41
def has_css?(css, custom_css)
  !node.at_css(css, custom_css).nil?
end
html?() click to toggle source
# File lib/openstax/content/fragment/html.rb, line 33
def html?
  !blank?
end
node() click to toggle source
# File lib/openstax/content/fragment/html.rb, line 37
def node
  @node ||= Nokogiri::HTML.fragment to_html
end