Module: Sevgi::Graphics::Mixtures::Validate

Defined in:
lib/sevgi/graphics/mixtures/validate.rb

Overview

DSL helpers for SVG standard validation.

Instance Method Summary collapse

Instance Method Details

#CDataString?

Returns text content used as SVG character data.

Returns:

  • (String, nil)

    joined text content or nil



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sevgi/graphics/mixtures/validate.rb', line 19

module Validate
  # Returns text content used as SVG character data.
  # @return [String, nil] joined text content or nil
  def CData
    return if !contents || contents.empty?

    Content.text(contents)
  end

  # Reports whether a namespace is available on this element or an ancestor.
  # @param name [Symbol, String] namespace suffix without xmlns:
  # @return [Boolean]
  def NS?(name)
    self.class.attributes.key?(key = :"xmlns:#{name}") || TraverseUp { Stay(true) if it.has?(key) } || false
  end

  require "sevgi/standard"

  # Validates this subtree against the SVG standard metadata.
  # @return [Sevgi::Graphics::Element] self
  # @raise [Sevgi::ValidationError] when SVG validation fails
  def Validate
    Traverse do |element|
      Standard.conform(
        element.name,
        attributes: element.attributes.list,
        cdata: element.CData(),
        elements: element.children.map(&:name)
      )
    end
  end

rescue ::LoadError => e
  raise unless e.path == "sevgi/standard"

  # @overload Validate
  #   No-op validation fallback when sevgi/standard is unavailable.
  #   @return [nil]
  def Validate(...)
  end
end

#NS?(name) ⇒ Boolean

Reports whether a namespace is available on this element or an ancestor.

Parameters:

  • name (Symbol, String)

    namespace suffix without xmlns:

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sevgi/graphics/mixtures/validate.rb', line 19

module Validate
  # Returns text content used as SVG character data.
  # @return [String, nil] joined text content or nil
  def CData
    return if !contents || contents.empty?

    Content.text(contents)
  end

  # Reports whether a namespace is available on this element or an ancestor.
  # @param name [Symbol, String] namespace suffix without xmlns:
  # @return [Boolean]
  def NS?(name)
    self.class.attributes.key?(key = :"xmlns:#{name}") || TraverseUp { Stay(true) if it.has?(key) } || false
  end

  require "sevgi/standard"

  # Validates this subtree against the SVG standard metadata.
  # @return [Sevgi::Graphics::Element] self
  # @raise [Sevgi::ValidationError] when SVG validation fails
  def Validate
    Traverse do |element|
      Standard.conform(
        element.name,
        attributes: element.attributes.list,
        cdata: element.CData(),
        elements: element.children.map(&:name)
      )
    end
  end

rescue ::LoadError => e
  raise unless e.path == "sevgi/standard"

  # @overload Validate
  #   No-op validation fallback when sevgi/standard is unavailable.
  #   @return [nil]
  def Validate(...)
  end
end

#ValidateSevgi::Graphics::Element?

Validates this subtree against SVG standard metadata when that component is available.

Returns:

Raises:

  • (Sevgi::ValidationError)

    when SVG validation fails



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sevgi/graphics/mixtures/validate.rb', line 19

module Validate
  # Returns text content used as SVG character data.
  # @return [String, nil] joined text content or nil
  def CData
    return if !contents || contents.empty?

    Content.text(contents)
  end

  # Reports whether a namespace is available on this element or an ancestor.
  # @param name [Symbol, String] namespace suffix without xmlns:
  # @return [Boolean]
  def NS?(name)
    self.class.attributes.key?(key = :"xmlns:#{name}") || TraverseUp { Stay(true) if it.has?(key) } || false
  end

  require "sevgi/standard"

  # Validates this subtree against the SVG standard metadata.
  # @return [Sevgi::Graphics::Element] self
  # @raise [Sevgi::ValidationError] when SVG validation fails
  def Validate
    Traverse do |element|
      Standard.conform(
        element.name,
        attributes: element.attributes.list,
        cdata: element.CData(),
        elements: element.children.map(&:name)
      )
    end
  end

rescue ::LoadError => e
  raise unless e.path == "sevgi/standard"

  # @overload Validate
  #   No-op validation fallback when sevgi/standard is unavailable.
  #   @return [nil]
  def Validate(...)
  end
end