vectory 0.13.2 → 0.13.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vectory
4
+ # Inspect raw bytes before parsing can discard a DTD or expand an entity.
5
+ module SvgInputPolicy
6
+ DOCTYPE = /\A<!DOCTYPE(?:\s|\[|>)/
7
+
8
+ def self.validate!(content)
9
+ return unless content.is_a?(String)
10
+
11
+ # XML permits UTF-16/32 declarations; ASCII token bytes are NUL padded.
12
+ bytes = content.b.delete("\0")
13
+ cursor = 0
14
+ while (opening = bytes.index("<", cursor))
15
+ cursor = token_end(bytes, opening)
16
+ end
17
+ end
18
+
19
+ # Literal text inside these tokens is not markup.
20
+ OPAQUE = { "<!--" => "-->", "<![CDATA[" => "]]>", "<?" => "?>" }.freeze
21
+
22
+ def self.token_end(bytes, opening)
23
+ return doctype_end(bytes, opening + 9) if bytes[opening, 10].match?(DOCTYPE)
24
+
25
+ start, delimiter = OPAQUE.find { |token, _end| bytes[opening, token.bytesize] == token }
26
+ start ? closing(bytes, delimiter, opening + start.bytesize) : opening + 1
27
+ end
28
+
29
+ # A DOCTYPE without an internal subset declares no entities, and the XML
30
+ # parser never loads its external identifier, so only `[` is refused.
31
+ def self.doctype_end(bytes, cursor)
32
+ while cursor < bytes.bytesize
33
+ case bytes[cursor]
34
+ when ">" then return cursor + 1
35
+ when "[" then raise ForbiddenExternalReferenceError, "XML DOCTYPE internal subsets are forbidden"
36
+ when '"', "'" then cursor = closing(bytes, bytes[cursor], cursor + 1)
37
+ else cursor += 1
38
+ end
39
+ end
40
+ cursor
41
+ end
42
+
43
+ def self.closing(bytes, delimiter, start)
44
+ index = bytes.index(delimiter, start)
45
+ # An unterminated token consumes the rest; the XML parser diagnoses it.
46
+ index ? index + delimiter.bytesize : bytes.bytesize
47
+ end
48
+ private_class_method :token_end, :doctype_end, :closing
49
+ end
50
+ end
@@ -90,6 +90,7 @@ module Vectory
90
90
  # identity. Applied before the index suffix to provide cross-document
91
91
  # uniqueness. Example: "_ISO_17301-1_2016"
92
92
  def initialize(doc, local_directory = "", id_suffix: nil)
93
+ @caller_doc = doc unless doc.is_a?(String)
93
94
  xml = doc.respond_to?(:to_xml) ? doc.to_xml : doc
94
95
  @doc = Leptris::XML.parse(xml)
95
96
  @local_directory = local_directory
@@ -106,6 +107,8 @@ module Vectory
106
107
  process_svgmap(svgmap, index)
107
108
  end
108
109
 
110
+ write_back_to_caller_doc
111
+
109
112
  @doc
110
113
  end
111
114
 
@@ -118,6 +121,62 @@ module Vectory
118
121
 
119
122
  private
120
123
 
124
+ # Writes the processed document back into a caller-supplied document
125
+ # object, restoring the pre-0.13 contract: metanorma-standoc's
126
+ # svgmap_cleanup passes its live document and relies on SvgMapping to
127
+ # update it in place (its return value is discarded), so without this
128
+ # write-back svgmap hotspots keep their unremapped hrefs (issue #84).
129
+ #
130
+ # The write-back runs through the caller's own XML library, which is
131
+ # already loaded in the caller's process; no dependency is added.
132
+ # Callers on the 0.13 return-value contract are unaffected: #call
133
+ # still returns the processed Leptris document.
134
+ #
135
+ # @return [void]
136
+ def write_back_to_caller_doc
137
+ return unless @caller_doc
138
+
139
+ reparsed = reparse_for_caller_doc
140
+ return unless reparsed
141
+
142
+ write_back_root(reparsed)
143
+ end
144
+
145
+ # Moves the reparsed document's children under the caller document's
146
+ # root, the same graft metanorma-standoc applies when it adopts the
147
+ # return-value contract.
148
+ #
149
+ # @param reparsed [Object] The processed XML reparsed by the caller's
150
+ # XML library
151
+ # @return [void]
152
+ def write_back_root(reparsed)
153
+ root = caller_doc_root
154
+ return unless root
155
+
156
+ root.children.each(&:remove)
157
+ reparsed.root.children.each { |n| root << n }
158
+ end
159
+
160
+ # Returns the caller document's root, if its type exposes one.
161
+ #
162
+ # @return [Object, nil]
163
+ def caller_doc_root
164
+ @caller_doc.respond_to?(:root) ? @caller_doc.root : nil
165
+ end
166
+
167
+ # Reparses the processed XML with the caller's own XML library.
168
+ #
169
+ # @return [Object, nil] The reparsed document, or nil if the caller's
170
+ # document object is of an unrecognized type
171
+ def reparse_for_caller_doc
172
+ if defined?(Moxml::Node) && @caller_doc.is_a?(Moxml::Node)
173
+ Moxml.new.parse(@doc.to_xml)
174
+ elsif defined?(Nokogiri::XML::Node) &&
175
+ @caller_doc.is_a?(Nokogiri::XML::Node)
176
+ Nokogiri::XML(@doc.to_xml)
177
+ end
178
+ end
179
+
121
180
  # Processes a single svgmap element.
122
181
  #
123
182
  # @param svgmap [Leptris::XML::Element] The svgmap element to process
@@ -0,0 +1,228 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "leptris"
4
+
5
+ module Vectory
6
+ # Declaration changes on one serialized SVG. Remaps use namespace URIs,
7
+ # rather than lexical source prefixes, so shadows and repeats are unambiguous.
8
+ # Existing aliases remain unless explicitly requested in +unbind+.
9
+ class SvgNamespaceBinding
10
+ def self.rewrite(content, bind: {}, unbind: [], remap: {})
11
+ new(content, bind, unbind, remap).rewrite
12
+ end
13
+
14
+ def initialize(content, bind, unbind, remap)
15
+ unless bind.is_a?(Hash) && unbind.is_a?(Array) && remap.is_a?(Hash)
16
+ raise ArgumentError, "invalid namespace request: expected maps and an array"
17
+ end
18
+
19
+ normalize_options(bind, unbind, remap)
20
+ @document = parse(content)
21
+ @elements = @document.xpath("//*")
22
+ validate_names
23
+ end
24
+
25
+ def rewrite
26
+ targets = requested_targets
27
+ targets.each { |prefix, uri| preflight_target(prefix, uri) }
28
+ preflight_unbind
29
+ targets.each { |prefix, uri| bind_target(prefix, uri) }
30
+ @elements.each { |element| remap_element(element) }
31
+ unbind_namespaces
32
+ @document.to_xml
33
+ end
34
+
35
+ private
36
+
37
+ def normalize_options(bind, unbind, remap)
38
+ @bind = normalize_bindings(bind)
39
+ @unbind = unbind.map { |prefix| normalize_prefix(prefix) }.uniq
40
+ @remap = remap.to_h { |uri, prefix| [validate_uri(uri), normalize_prefix(prefix)] }
41
+ end
42
+
43
+ def normalize_bindings(bind)
44
+ bindings = {}
45
+ bind.each do |prefix, uri|
46
+ prefix = normalize_prefix(prefix)
47
+ uri = validate_uri(uri)
48
+ add_target(bindings, prefix, uri)
49
+ end
50
+ bindings
51
+ end
52
+
53
+ def requested_targets
54
+ targets = @bind.dup
55
+ @remap.each { |uri, prefix| add_target(targets, prefix, uri) }
56
+ targets
57
+ end
58
+
59
+ def add_target(targets, prefix, uri)
60
+ if targets.key?(prefix) && targets[prefix] != uri
61
+ raise ArgumentError, "namespace conflict: incompatible requested bindings"
62
+ end
63
+
64
+ targets[prefix] = uri
65
+ end
66
+
67
+ def bind_target(prefix, uri)
68
+ @document.root.add_namespace_definition(prefix, uri) unless
69
+ @document.root.namespaces[declaration_name(prefix)] == uri
70
+ end
71
+
72
+ def unbind_element(element)
73
+ element.namespace_definitions.each do |namespace|
74
+ element.remove_namespace_definition(namespace.prefix) if @unbind.include?(namespace.prefix)
75
+ end
76
+ end
77
+
78
+ def unbind_namespaces
79
+ # Descendants still need their inherited source bindings during remapping.
80
+ @elements.each { |element| unbind_element(element) }
81
+ end
82
+
83
+ def normalize_prefix(prefix)
84
+ return nil if prefix.nil? || prefix == ""
85
+ if !prefix.is_a?(String) || !/\A[A-Za-z_][A-Za-z0-9_.-]*\z/.match?(prefix) ||
86
+ prefix.downcase.start_with?("xml")
87
+ raise ArgumentError, "invalid namespace request: invalid or reserved prefix"
88
+ end
89
+
90
+ prefix
91
+ end
92
+
93
+ def validate_uri(uri)
94
+ unless uri.is_a?(String) && !uri.empty? && !/\s/.match?(uri) &&
95
+ !["http://www.w3.org/XML/1998/namespace", "http://www.w3.org/2000/xmlns/"].include?(uri)
96
+ raise ArgumentError, "invalid namespace request: expected nonempty namespace URI"
97
+ end
98
+
99
+ uri
100
+ end
101
+
102
+ def parse(content)
103
+ raise ArgumentError, "invalid SVG: expected serialized string" unless content.is_a?(String)
104
+
105
+ document = Leptris::XML.parse(content, strict: true)
106
+ unless document.root&.name == "svg"
107
+ raise ArgumentError, "invalid SVG: expected SVG root"
108
+ end
109
+
110
+ document
111
+ rescue Leptris::XML::Error => e
112
+ raise ArgumentError, "invalid SVG: #{e.message}"
113
+ end
114
+
115
+ def declaration_name(prefix)
116
+ prefix ? "xmlns:#{prefix}" : "xmlns"
117
+ end
118
+
119
+ def validate_names
120
+ @elements.each do |element|
121
+ if element.name.include?(":") && !element.namespace&.href
122
+ raise ArgumentError, "invalid SVG: unbound element prefix"
123
+ end
124
+
125
+ validate_attributes(element)
126
+ end
127
+ end
128
+
129
+ def validate_attributes(element)
130
+ element.attribute_nodes.each do |attr|
131
+ if attr.name.include?(":") && !attr.namespace_uri
132
+ raise ArgumentError, "invalid SVG: unbound attribute prefix"
133
+ end
134
+ end
135
+ end
136
+
137
+ def preflight_target(prefix, uri)
138
+ preflight_declarations(prefix, uri)
139
+ preflight_default(uri) unless prefix
140
+ end
141
+
142
+ def preflight_declarations(prefix, uri)
143
+ @elements.each do |element|
144
+ element.namespace_definitions.each do |namespace|
145
+ if namespace.prefix == prefix && namespace.href != uri
146
+ raise ArgumentError, "namespace conflict: target prefix already bound"
147
+ end
148
+ end
149
+ end
150
+ end
151
+
152
+ def preflight_default(uri)
153
+ if @elements.any? { |element| !element.namespace&.href }
154
+ raise ArgumentError, "default namespace: would qualify unqualified elements"
155
+ end
156
+ return unless remapped_to_default?(uri)
157
+ return unless attributes_use_uri?(uri)
158
+
159
+ raise ArgumentError, "default namespace: attributes require an explicit prefix"
160
+ end
161
+
162
+ def remapped_to_default?(uri)
163
+ @remap[uri].nil? && @remap.key?(uri)
164
+ end
165
+
166
+ def attributes_use_uri?(uri)
167
+ @elements.any? { |element| element.attribute_nodes.any? { |attr| attr.namespace_uri == uri } }
168
+ end
169
+
170
+ def preflight_unbind
171
+ @unbind.each do |prefix|
172
+ if @bind.key?(prefix) || @remap.value?(prefix)
173
+ raise ArgumentError, "namespace conflict: target also requested for unbind"
174
+ end
175
+
176
+ preflight_prefix_usage(prefix)
177
+ end
178
+ end
179
+
180
+ def preflight_prefix_usage(prefix)
181
+ @elements.each do |element|
182
+ used = element_uses_prefix?(element, prefix) || attributes_use_prefix?(element, prefix)
183
+ raise ArgumentError, "namespace in use: cannot remove requested prefix" if used
184
+ end
185
+ end
186
+
187
+ def element_uses_prefix?(element, prefix)
188
+ namespace = element.namespace
189
+ namespace && namespace.prefix == prefix && !@remap.key?(namespace.href)
190
+ end
191
+
192
+ def attributes_use_prefix?(element, prefix)
193
+ element.attribute_nodes.any? do |attr|
194
+ attr.namespace_uri && attr.name.split(":")[0...-1].join(":") == prefix &&
195
+ !@remap.key?(attr.namespace_uri)
196
+ end
197
+ end
198
+
199
+ def remap_element(element)
200
+ # Snapshot before changing names: native namespace resolution is lexical.
201
+ namespace = element.namespace
202
+ attributes = attribute_snapshot(element)
203
+ if namespace && @remap.key?(namespace.href)
204
+ element.name = qualified_name(@remap[namespace.href], element.name)
205
+ end
206
+ remap_attributes(element, attributes)
207
+ end
208
+
209
+ def attribute_snapshot(element)
210
+ element.attribute_nodes.map do |attr|
211
+ [attr, attr.namespace_uri, attr.name.split(":").last, attr.value]
212
+ end
213
+ end
214
+
215
+ def remap_attributes(element, attributes)
216
+ attributes.each do |attr, uri, local, value|
217
+ next unless @remap.key?(uri)
218
+
219
+ attr.remove
220
+ element[qualified_name(@remap[uri], local)] = value
221
+ end
222
+ end
223
+
224
+ def qualified_name(prefix, local)
225
+ prefix ? "#{prefix}:#{local}" : local
226
+ end
227
+ end
228
+ end
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "leptris"
4
+
5
+ module Vectory
6
+ # Replaces serialized root content while retaining the host document envelope.
7
+ class SvgSubtreeGraft
8
+ ENCODING_SIGNATURES = {
9
+ "\x00\x00\xFE\xFF".b => "UTF-32", "\xFF\xFE\x00\x00".b => "UTF-32",
10
+ "\xFE\xFF".b => "UTF-16", "\xFF\xFE".b => "UTF-16",
11
+ "\x00\x00\x00<".b => "UTF-32BE", "<\x00\x00\x00".b => "UTF-32LE",
12
+ "\x00<\x00?".b => "UTF-16BE", "<\x00?\x00".b => "UTF-16LE"
13
+ }.freeze
14
+ private_constant :ENCODING_SIGNATURES
15
+
16
+ class << self
17
+ # @return [String] the complete host document with processed root children
18
+ def replace_root_content(host_content, processed_content)
19
+ host_content = utf8_content(host_content, "host_content")
20
+ processed_content = utf8_content(processed_content, "processed_content")
21
+ host = parse_document(host_content, "host_content")
22
+ processed = parse_document(processed_content, "processed_content")
23
+ # Native node copying/adoption can drop interleaved namespaced attributes.
24
+ # Reparse serialized children instead of crossing that copy boundary.
25
+ children = processed.root.children.map { |node| serialize_child(node, host.root.namespaces) }
26
+ host.root.children = children
27
+ serialize_host(host, host_content)
28
+ end
29
+
30
+ # @api private
31
+ # Leptris consumes UTF-8 bytes without decoding XML encoding declarations.
32
+ # Normalize a working copy; captured pipeline options retain the original.
33
+ def utf8_content(content, label)
34
+ encoding = source_encoding(content)
35
+ utf8 = content.dup.force_encoding(encoding).encode(Encoding::UTF_8)
36
+ return utf8 if Encoding.find(encoding) == Encoding::UTF_8
37
+
38
+ utf8.sub(/\A(<\?xml[ \t\r\n][^?]*\bencoding\s*=\s*)(["'])[^"']+\2/) do
39
+ "#{Regexp.last_match(1)}#{Regexp.last_match(2)}UTF-8#{Regexp.last_match(2)}"
40
+ end
41
+ rescue EncodingError, ArgumentError => e
42
+ raise ParsingError, "invalid #{label} XML: #{e.message}"
43
+ end
44
+
45
+ private
46
+
47
+ def source_encoding(content)
48
+ bytes = content.b
49
+ signature = ENCODING_SIGNATURES.find { |prefix, _| bytes.start_with?(prefix) }
50
+ return signature.last if signature
51
+
52
+ bytes[/\A(?:\xEF\xBB\xBF)?<\?xml[ \t\r\n][^?]*\bencoding\s*=\s*(["'])([^"']+)\1/n, 2] || "UTF-8"
53
+ end
54
+
55
+ def serialize_host(host, content)
56
+ # The document serializer moves DOCTYPE before leading comments/PIs.
57
+ # Keep the original envelope and serialize only the retained host root.
58
+ prefix = content.byteslice(0, host.root.byte_offset)
59
+ suffix = content[/(?:[ \t\r\n]|<!--(?:(?!-->).)*-->|<\?(?:(?!\?>).)*\?>)*\z/m]
60
+ "#{prefix}#{host.root.to_xml}#{suffix}"
61
+ end
62
+
63
+ def parse_document(content, label)
64
+ document = Leptris::XML.parse(content, strict: true)
65
+ raise ParsingError, "#{label} has no document root" unless document.root
66
+
67
+ document
68
+ rescue Leptris::XML::ParseError => e
69
+ # Strict parsing also rejects valid rootless XML. Appending a root
70
+ # validates that envelope without allowing repaired malformed input.
71
+ begin
72
+ Leptris::XML.parse("#{content}<vectory-root-check/>", strict: true)
73
+ rescue Leptris::XML::ParseError
74
+ raise ParsingError, "invalid #{label} XML: #{e.message}"
75
+ end
76
+ raise ParsingError, "#{label} has no document root"
77
+ end
78
+
79
+ def serialize_child(node, host_namespaces)
80
+ seal_namespaces(node, host_namespaces) if node.element?
81
+ node.to_xml
82
+ end
83
+
84
+ def seal_namespaces(node, host_namespaces)
85
+ # Retain inherited bindings only where the host cannot supply them.
86
+ namespaces = { "xmlns" => "" }.merge(node.namespaces)
87
+ declared = node.namespace_definitions.map(&:prefix)
88
+ namespaces.each do |name, uri|
89
+ prefix = name == "xmlns" ? nil : name.delete_prefix("xmlns:")
90
+ next if declared.include?(prefix) || host_namespaces[name] == uri
91
+
92
+ node.add_namespace_definition(prefix, uri)
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "leptris"
4
+
5
+ module Vectory
6
+ # An immutable, named operation at a serialized XML boundary.
7
+ class SvgTransform
8
+ attr_reader :name, :options
9
+
10
+ class << self
11
+ def graft_into(host_content:)
12
+ host = host_content.dup.freeze
13
+ root = Leptris::XML.parse(SvgSubtreeGraft.utf8_content(host, "host_content"), strict: true).root
14
+ raise ParsingError, "host_content has no document root" unless root
15
+
16
+ new(:graft_into, { host_content: host, svg_host: root.name == "svg" })
17
+ rescue Leptris::XML::SyntaxError, Leptris::XML::ParseError => e
18
+ raise ParsingError, "invalid host_content XML: #{e.message}"
19
+ end
20
+
21
+ def disambiguate(index:, duplicate_ids:)
22
+ new(:disambiguate, { index: index, duplicate_ids: duplicate_ids })
23
+ end
24
+
25
+ def suffix_classes(index:)
26
+ new(:suffix_classes, { index: index })
27
+ end
28
+
29
+ def bind_namespaces(bind: {}, unbind: [], remap: {})
30
+ new(:bind_namespaces, { bind: bind, unbind: unbind, remap: remap })
31
+ end
32
+
33
+ private
34
+
35
+ def snapshot(value)
36
+ case value
37
+ when Hash
38
+ value.to_h { |key, item| [snapshot(key), snapshot(item)] }.freeze
39
+ when Array
40
+ value.map { |item| snapshot(item) }.freeze
41
+ else
42
+ value.frozen? ? value : value.dup.freeze
43
+ end
44
+ end
45
+ end
46
+
47
+ def initialize(name, options)
48
+ @name = name
49
+ @options = self.class.send(:snapshot, options)
50
+ freeze
51
+ end
52
+ private_class_method :new
53
+
54
+ def >>(other)
55
+ SvgTransformPipeline.new([self]) >> other
56
+ end
57
+
58
+ def call(content)
59
+ SvgTransformPipeline.new([self]).call(content)
60
+ end
61
+
62
+ # Pipeline execution owns ordering and scope checks.
63
+ def apply(content)
64
+ case name
65
+ when :graft_into
66
+ SvgSubtreeGraft.replace_root_content(options.fetch(:host_content), content)
67
+ when :disambiguate
68
+ SvgIdDisambiguator.rewrite(content, suffix, options.fetch(:duplicate_ids))
69
+ when :suffix_classes
70
+ SvgClassSuffixer.rewrite(content, suffix)
71
+ when :bind_namespaces
72
+ SvgNamespaceBinding.rewrite(content, **options)
73
+ end
74
+ end
75
+
76
+ private
77
+
78
+ def suffix
79
+ "_inject_#{options.fetch(:index)}"
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "leptris"
4
+
5
+ module Vectory
6
+ # Only the supported whole-document and selected-SVG profiles are executable.
7
+ class SvgTransformPipeline
8
+ PROFILES = {
9
+ graft: %i[graft_into],
10
+ svg_cleanup: %i[disambiguate suffix_classes bind_namespaces],
11
+ graft_then_svg_cleanup: %i[graft_into disambiguate suffix_classes bind_namespaces],
12
+ }.transform_values(&:freeze).freeze
13
+
14
+ attr_reader :steps
15
+
16
+ def initialize(steps)
17
+ unless steps.all?(SvgTransform)
18
+ raise ArgumentError, "invalid step sequence: expected SVG transforms"
19
+ end
20
+
21
+ @steps = steps.dup.freeze
22
+ validate_sequence
23
+ freeze
24
+ end
25
+
26
+ def >>(other)
27
+ additions = other.is_a?(SvgTransformPipeline) ? other.steps : [other]
28
+ self.class.new(steps + additions)
29
+ end
30
+
31
+ def profile
32
+ PROFILES.key(steps.map(&:name))
33
+ end
34
+
35
+ def call(content)
36
+ raise ArgumentError, "incomplete profile: #{steps.map(&:name).join(' -> ')}" unless profile
37
+
38
+ validate_scope(content)
39
+ output = steps.reduce(content) { |xml, step| step.apply(xml) }
40
+ profile == :graft_then_svg_cleanup ? finalize_graft(output) : output
41
+ end
42
+
43
+ private
44
+
45
+ def finalize_graft(output)
46
+ # Cleanup serializers may reorder the envelope. Keep the cleaned root,
47
+ # including its new attributes and bindings, inside the captured envelope.
48
+ host_content = SvgSubtreeGraft.utf8_content(steps.first.options.fetch(:host_content), "host_content")
49
+ host = Leptris::XML.parse(host_content, strict: true)
50
+ prefix = host_content.byteslice(0, host.root.byte_offset)
51
+ tail = host_content[/(?:[ \t\r\n]|<!--(?:(?!-->).)*-->|<\?(?:(?!\?>).)*\?>)*\z/m]
52
+ "#{prefix}#{final_graft_root(output).to_xml}#{tail}"
53
+ end
54
+
55
+ def final_graft_root(output)
56
+ root = Leptris::XML.parse(output, strict: true).root
57
+ # Bindings sealed onto grafted children may now be supplied by the root.
58
+ root.xpath(".//*").each { |element| remove_redundant_namespaces(element) }
59
+ root
60
+ end
61
+
62
+ def remove_redundant_namespaces(element)
63
+ inherited = element.parent.namespaces
64
+ element.namespace_definitions.each do |namespace|
65
+ name = namespace.prefix ? "xmlns:#{namespace.prefix}" : "xmlns"
66
+ element.remove_namespace_definition(namespace.prefix) if inherited[name] == namespace.href
67
+ end
68
+ end
69
+
70
+ def validate_sequence
71
+ names = steps.map(&:name)
72
+ unless !names.empty? && PROFILES.values.any? { |profile| profile.take(names.length) == names }
73
+ raise ArgumentError, "invalid step sequence: #{names.join(' -> ')}"
74
+ end
75
+ end
76
+
77
+ def validate_scope(content)
78
+ case profile
79
+ when :svg_cleanup
80
+ validate_svg_root(content)
81
+ when :graft_then_svg_cleanup
82
+ return if steps.first.options.fetch(:svg_host)
83
+
84
+ raise ArgumentError, "execution-scope mismatch: graft_then_svg_cleanup requires an SVG host root"
85
+ end
86
+ rescue Leptris::XML::SyntaxError, Leptris::XML::ParseError => e
87
+ raise ParsingError, "invalid content XML: #{e.message}"
88
+ end
89
+
90
+ def validate_svg_root(content)
91
+ root = Leptris::XML.parse(content, strict: true).root
92
+ return if root&.name == "svg"
93
+
94
+ raise ArgumentError, "execution-scope mismatch: svg_cleanup requires an SVG root"
95
+ end
96
+ end
97
+ end
@@ -1,14 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "tempfile"
4
-
5
3
  module Vectory
6
4
  class Vector < Image
7
- def self.from_path(path)
8
- content = File.read(path, mode: "rb")
9
- new(content, path)
10
- end
11
-
12
5
  def self.from_datauri(uri)
13
6
  Datauri.new(uri).to_vector
14
7
  end
@@ -23,28 +16,10 @@ module Vectory
23
16
  "#mimetype should be implemented in a subclass."
24
17
  end
25
18
 
26
- attr_reader :initial_path
27
-
28
- def initialize(content = nil, initial_path = nil)
29
- super(content)
30
-
31
- @initial_path = initial_path
32
- end
33
-
34
19
  def mime
35
20
  self.class.mimetype
36
21
  end
37
22
 
38
- def size
39
- content.bytesize
40
- end
41
-
42
- def file_size
43
- raise(NotWrittenToDiskError) unless @path
44
-
45
- File.size(@path)
46
- end
47
-
48
23
  def height
49
24
  raise Vectory::NotImplementedError,
50
25
  "#height should be implemented in a subclass when no intrinsic " \
@@ -60,25 +35,5 @@ module Vectory
60
35
  def to_uri
61
36
  Datauri.from_vector(self)
62
37
  end
63
-
64
- def write(path = nil)
65
- target_path = path || @path || tmp_path
66
- File.binwrite(target_path, content)
67
- @path = File.expand_path(target_path)
68
-
69
- self
70
- end
71
-
72
- def path
73
- @path || raise(NotWrittenToDiskError)
74
- end
75
-
76
- private
77
-
78
- def tmp_path
79
- dir = Dir.mktmpdir
80
- filename = "image.#{self.class.default_extension}"
81
- File.join(dir, filename)
82
- end
83
38
  end
84
39
  end