Module: ExpoTurbo::Rails::XmlFragments
- Defined in:
- lib/expo_turbo/rails/xml_fragments.rb
Defined Under Namespace
Classes: DocumentIdError, ParseError
Class Method Summary
collapse
Class Method Details
.parse_document(xml) ⇒ Object
21
22
23
|
# File 'lib/expo_turbo/rails/xml_fragments.rb', line 21
def parse_document(xml)
parse_xml(validate_source!(xml, "document", allow_declaration: true), "document")
end
|
.parse_frame_fragment(xml, root_name: DEFAULT_FRAGMENT_ROOT) ⇒ Object
50
51
52
53
54
55
56
|
# File 'lib/expo_turbo/rails/xml_fragments.rb', line 50
def parse_frame_fragment(xml, root_name: DEFAULT_FRAGMENT_ROOT)
document = parse_fragment(xml, "Frame fragment", root_name:)
validate_fragment_root!(document, "Frame fragment") do |elements|
elements.one? && literal_element?(elements.first, "turbo-frame")
end
validate_fragment_ids!(document)
end
|
.parse_stream_fragment(xml, root_name: DEFAULT_FRAGMENT_ROOT) ⇒ Object
42
43
44
45
46
47
48
|
# File 'lib/expo_turbo/rails/xml_fragments.rb', line 42
def parse_stream_fragment(xml, root_name: DEFAULT_FRAGMENT_ROOT)
document = parse_fragment(xml, "Stream fragment", root_name:)
validate_fragment_root!(document, "Stream fragment") do |elements|
elements.present? && elements.all? { |element| literal_element?(element, "turbo-stream") }
end
validate_fragment_ids!(document)
end
|
.validate_document_ids!(document) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/expo_turbo/rails/xml_fragments.rb', line 25
def validate_document_ids!(document)
ids = {}
document.xpath("//*").each do |element|
id = literal_attribute_value(element, "id")
next if id.nil?
if id.match?(ECMASCRIPT_TRIMMED_EMPTY) || ids.key?(id)
raise DocumentIdError, "Expo Turbo document ids must be unique nonblank literal values"
end
ids[id] = true
end
document
end
|