svg_conform 0.2.10 → 0.2.12
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.
- checksums.yaml +4 -4
- data/.github/workflows/release.yml +3 -0
- data/Gemfile +0 -1
- data/config/profiles/svg_1_2_rfc.yml +18 -0
- data/lib/svg_conform/adapter_configuration.rb +25 -0
- data/lib/svg_conform/document.rb +5 -6
- data/lib/svg_conform/remediations/disallowed_attribute_remediation.rb +42 -0
- data/lib/svg_conform/remediations/namespace_attribute_remediation.rb +30 -24
- data/lib/svg_conform/remediations.rb +2 -0
- data/lib/svg_conform/requirements/namespace_attributes_requirement.rb +3 -12
- data/lib/svg_conform/requirements/svg_1_1_document_requirement.rb +14 -13
- data/lib/svg_conform/semantic_comparator.rb +1 -1
- data/lib/svg_conform/validation/svg_1_1_document_conformance.rb +185 -23
- data/lib/svg_conform/version.rb +1 -1
- data/lib/svg_conform.rb +32 -7
- data/spec/svg_conform/adapter_configuration_spec.rb +36 -0
- data/spec/svg_conform/external_checkers/svgcheck/parser_spec.rb +52 -0
- data/spec/svg_conform/gemspec_spec.rb +1 -1
- data/spec/svg_conform/profiles_plantuml_spec.rb +14 -5
- data/spec/svg_conform/quality_metrics/configuration_spec.rb +0 -4
- data/spec/svg_conform/remediations/disallowed_attribute_remediation_spec.rb +78 -0
- data/spec/svg_conform/yaml_backend_spec.rb +103 -0
- data/svg_conform.gemspec +1 -1
- metadata +17 -12
- data/Gemfile.lock +0 -299
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: '063019373f9e0a4b74e0536559552368b3d14096586f2e22022fcba0a5ec8fe6'
|
|
4
|
+
data.tar.gz: cdeefc4f3948521321dfa3eb044497bd98f5f48227e5f6c75479aa7f78cc1587
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e656e5fd53af6095badb39dbe2f58731a6ba726a151dfc8bcdfe24e3e640ffe3e7726ebbaa14e87671b689e584e42d6666949da9c1c8ac2f0bcbe7f188d5c59f
|
|
7
|
+
data.tar.gz: e56d056fcb420ddcd1d05b412a7fef762360d56c6dd1afa39564c376952651ecc96100dbdd1550a04a07970e6b44b9ae9959141748dd044b83abbc6b32b9fb48
|
|
@@ -20,6 +20,7 @@ on:
|
|
|
20
20
|
permissions:
|
|
21
21
|
contents: write
|
|
22
22
|
id-token: write
|
|
23
|
+
pull-requests: write
|
|
23
24
|
|
|
24
25
|
concurrency:
|
|
25
26
|
group: release
|
|
@@ -28,5 +29,7 @@ concurrency:
|
|
|
28
29
|
jobs:
|
|
29
30
|
release:
|
|
30
31
|
uses: metanorma/ci/.github/workflows/rubygems-release.yml@main
|
|
32
|
+
secrets:
|
|
33
|
+
pat_token: ${{ secrets.CLARICLE_CI_PAT_TOKEN }}
|
|
31
34
|
with:
|
|
32
35
|
next_version: ${{ github.event.inputs.next_version }}
|
data/Gemfile
CHANGED
|
@@ -11,7 +11,6 @@ gem "lutaml-model", "~> 0.8.155" # rubygems line; drop the git-main float
|
|
|
11
11
|
gem "moxml", "~> 0.6.0"
|
|
12
12
|
# lens_core is optional at runtime (#121); development and CI use the released v1 gem.
|
|
13
13
|
gem "lens_core", "~> 0.1"
|
|
14
|
-
gem "nokogiri", "~> 1.16"
|
|
15
14
|
gem "openssl", "~> 3.0"
|
|
16
15
|
gem "rake"
|
|
17
16
|
gem "rspec"
|
|
@@ -277,6 +277,24 @@ remediations:
|
|
|
277
277
|
description: "Promote valid style properties to attributes per RFC 7996 Section 2.1"
|
|
278
278
|
targets: ["style_promotion"]
|
|
279
279
|
|
|
280
|
+
# RFC 7996 Section 2.1: enable-background is removed in SVG 1.2 RFC
|
|
281
|
+
# (never rendered); Inkscape-authored documents carry it on the root
|
|
282
|
+
- id: "enable_background_removal"
|
|
283
|
+
type: "DisallowedAttributeRemediation"
|
|
284
|
+
description: "Remove enable-background (removed in SVG 1.2 RFC, never rendered)"
|
|
285
|
+
targets: ["allowed_elements"]
|
|
286
|
+
attributes:
|
|
287
|
+
- "enable-background"
|
|
288
|
+
|
|
289
|
+
# RFC 7996: the Inkscape namespace is not permitted - strip its
|
|
290
|
+
# attributes and declarations from authoring-tool output
|
|
291
|
+
- id: "inkscape_namespace_removal"
|
|
292
|
+
type: "NamespaceAttributeRemediation"
|
|
293
|
+
description: "Strip Inkscape-namespace attributes and declarations"
|
|
294
|
+
targets: ["namespace_validation", "namespace_attributes"]
|
|
295
|
+
disallowed_namespaces:
|
|
296
|
+
- "http://www.inkscape.org/namespaces/inkscape"
|
|
297
|
+
|
|
280
298
|
# RFC 7996 Section 2.1: ViewBox auto-generation
|
|
281
299
|
- id: "viewbox_generation"
|
|
282
300
|
type: "ViewboxRemediation"
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SvgConform
|
|
4
|
+
# Boots the Lutaml::Model adapter pins for standalone runs.
|
|
5
|
+
#
|
|
6
|
+
# A library must not clobber the host application's adapter choice:
|
|
7
|
+
# the boot configure used to run unconditionally at require time, and
|
|
8
|
+
# any consumer that required svg_conform (directly or through
|
|
9
|
+
# metanorma-standoc) had its pinned XML adapter silently replaced by
|
|
10
|
+
# :leptris — flipping the fleet's parse lanes after boot (the
|
|
11
|
+
# metanorma-document#233 ambient red). Pin an engine only when the
|
|
12
|
+
# host has not configured that format itself.
|
|
13
|
+
module AdapterConfiguration
|
|
14
|
+
module_function
|
|
15
|
+
|
|
16
|
+
def configure
|
|
17
|
+
Lutaml::Model::Config.configure do |config|
|
|
18
|
+
resolver = Lutaml::Model::AdapterResolver
|
|
19
|
+
config.xml_adapter_type = :leptris unless resolver.configured_type(:xml)
|
|
20
|
+
config.json_adapter_type = :standard_json unless resolver.configured_type(:json)
|
|
21
|
+
config.yaml_adapter_type = :yeptris unless resolver.configured_type(:yaml)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
data/lib/svg_conform/document.rb
CHANGED
|
@@ -251,12 +251,11 @@ module SvgConform
|
|
|
251
251
|
|
|
252
252
|
def parse_document
|
|
253
253
|
begin
|
|
254
|
-
# Create a Moxml context and parse the document. The
|
|
255
|
-
#
|
|
256
|
-
#
|
|
257
|
-
#
|
|
258
|
-
|
|
259
|
-
context = Moxml.new(:nokogiri)
|
|
254
|
+
# Create a Moxml context and parse the document. The adapter
|
|
255
|
+
# is leptris (the stack engine); re-verify the attribute-
|
|
256
|
+
# traversal segfault (leptris#1712) on every leptris bump —
|
|
257
|
+
# this gem's remediation passes are exactly that workload.
|
|
258
|
+
context = Moxml.new(:leptris)
|
|
260
259
|
@moxml_document = context.parse(@content)
|
|
261
260
|
rescue StandardError => e
|
|
262
261
|
raise ParseError, "Failed to parse SVG document: #{e.message}"
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "base_remediation"
|
|
4
|
+
|
|
5
|
+
module SvgConform
|
|
6
|
+
module Remediations
|
|
7
|
+
# Removes explicitly disallowed attributes wherever they appear
|
|
8
|
+
# (config: `attributes`, e.g. the removed-in-RFC enable-background
|
|
9
|
+
# property attribute — svg_1_2_rfc's allowed_elements walk flags it
|
|
10
|
+
# :not_allowed on every element carrying it).
|
|
11
|
+
class DisallowedAttributeRemediation < BaseRemediation
|
|
12
|
+
attribute :type, :string, default: -> { "DisallowedAttributeRemediation" }
|
|
13
|
+
attribute :attributes, :string, collection: true, default: -> { [] }
|
|
14
|
+
|
|
15
|
+
yaml do
|
|
16
|
+
map "attributes", to: :attributes
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def apply(document, _context)
|
|
20
|
+
changes = []
|
|
21
|
+
|
|
22
|
+
document.traverse do |node|
|
|
23
|
+
next unless element?(node)
|
|
24
|
+
|
|
25
|
+
attributes.each do |attr_name|
|
|
26
|
+
next unless has_attribute?(node, attr_name)
|
|
27
|
+
|
|
28
|
+
remove_attribute(node, attr_name)
|
|
29
|
+
changes << {
|
|
30
|
+
type: :attribute_removed,
|
|
31
|
+
description: "Removed disallowed attribute '#{attr_name}'",
|
|
32
|
+
node_name: node.name,
|
|
33
|
+
attribute: attr_name,
|
|
34
|
+
}
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
changes
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -53,7 +53,7 @@ module SvgConform
|
|
|
53
53
|
if node.respond_to?(:attribute_nodes)
|
|
54
54
|
# Use attribute_nodes if available (nokogiri-compatible API style)
|
|
55
55
|
node.attribute_nodes.each do |attr|
|
|
56
|
-
namespace_uri = attr.namespace&.
|
|
56
|
+
namespace_uri = attr.namespace&.uri
|
|
57
57
|
next unless namespace_uri
|
|
58
58
|
|
|
59
59
|
if namespace_disallowed?(namespace_uri)
|
|
@@ -117,19 +117,34 @@ module SvgConform
|
|
|
117
117
|
namespaces_to_remove = removed_namespaces.to_a + disallowed_namespaces
|
|
118
118
|
|
|
119
119
|
namespaces_to_remove.uniq.each do |ns_identifier|
|
|
120
|
-
#
|
|
121
|
-
#
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
120
|
+
# URI form (the common case): find the prefix the document
|
|
121
|
+
# actually declared for this namespace, then drop that
|
|
122
|
+
# declaration; also covers the default-namespace spelling.
|
|
123
|
+
decl = /\s+xmlns:([A-Za-z_][\w.-]*)="#{Regexp.escape(ns_identifier)}"/
|
|
124
|
+
prefix_form = /\s+xmlns:#{Regexp.escape(ns_identifier)}="[^"]*"/
|
|
125
|
+
default_form = /\s+xmlns="#{Regexp.escape(ns_identifier)}"/
|
|
126
|
+
|
|
127
|
+
removed_name = nil
|
|
128
|
+
if (m = xml_str.match(decl))
|
|
129
|
+
xml_str = xml_str.gsub(decl, "")
|
|
130
|
+
removed_name = "xmlns:#{m[1]}"
|
|
131
|
+
elsif xml_str.match?(default_form)
|
|
132
|
+
xml_str = xml_str.gsub(default_form, "")
|
|
133
|
+
removed_name = "xmlns"
|
|
134
|
+
elsif xml_str.match?(prefix_form)
|
|
135
|
+
# a caller handed the prefix itself
|
|
136
|
+
xml_str = xml_str.gsub(prefix_form, "")
|
|
137
|
+
removed_name = "xmlns:#{ns_identifier}"
|
|
132
138
|
end
|
|
139
|
+
|
|
140
|
+
next unless removed_name
|
|
141
|
+
|
|
142
|
+
changes << {
|
|
143
|
+
type: :namespace_removed,
|
|
144
|
+
description: "Removed unused namespace declaration '#{removed_name}'",
|
|
145
|
+
node_name: "svg",
|
|
146
|
+
attribute: removed_name,
|
|
147
|
+
}
|
|
133
148
|
end
|
|
134
149
|
|
|
135
150
|
# Reparse the document to update the internal DOM
|
|
@@ -179,13 +194,7 @@ module SvgConform
|
|
|
179
194
|
|
|
180
195
|
def should_remove_moxml_attribute?(attr, node, removed_namespaces)
|
|
181
196
|
if attr.respond_to?(:namespace) && attr.namespace
|
|
182
|
-
namespace_uri =
|
|
183
|
-
attr.namespace.href
|
|
184
|
-
elsif attr.namespace.respond_to?(:uri)
|
|
185
|
-
attr.namespace.uri
|
|
186
|
-
else
|
|
187
|
-
attr.namespace.to_s
|
|
188
|
-
end
|
|
197
|
+
namespace_uri = attr.namespace.uri
|
|
189
198
|
|
|
190
199
|
return false unless namespace_uri && !namespace_uri.empty?
|
|
191
200
|
|
|
@@ -211,10 +220,7 @@ module SvgConform
|
|
|
211
220
|
ns.prefix == prefix
|
|
212
221
|
end
|
|
213
222
|
if ns_def
|
|
214
|
-
return ns_def.uri
|
|
215
|
-
return ns_def.href if ns_def.respond_to?(:href)
|
|
216
|
-
|
|
217
|
-
return ns_def.to_s
|
|
223
|
+
return ns_def.uri
|
|
218
224
|
end
|
|
219
225
|
end
|
|
220
226
|
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative "remediations/base_remediation"
|
|
4
4
|
require_relative "remediations/color_remediation"
|
|
5
|
+
require_relative "remediations/disallowed_attribute_remediation"
|
|
5
6
|
require_relative "remediations/font_embedding_remediation"
|
|
6
7
|
require_relative "remediations/font_remediation"
|
|
7
8
|
require_relative "remediations/image_embedding_remediation"
|
|
@@ -18,6 +19,7 @@ module SvgConform
|
|
|
18
19
|
def self.all
|
|
19
20
|
[
|
|
20
21
|
ColorRemediation,
|
|
22
|
+
DisallowedAttributeRemediation,
|
|
21
23
|
FontEmbeddingRemediation,
|
|
22
24
|
FontRemediation,
|
|
23
25
|
ImageEmbeddingRemediation,
|
|
@@ -111,7 +111,7 @@ module SvgConform
|
|
|
111
111
|
def check_attribute_nodes(node, context)
|
|
112
112
|
node.attribute_nodes.each do |attr|
|
|
113
113
|
# Check if attribute has a namespace
|
|
114
|
-
namespace_uri = attr.namespace&.
|
|
114
|
+
namespace_uri = attr.namespace&.uri
|
|
115
115
|
next unless namespace_uri
|
|
116
116
|
|
|
117
117
|
# Determine if this namespace is invalid based on configuration
|
|
@@ -164,13 +164,7 @@ module SvgConform
|
|
|
164
164
|
def check_moxml_attribute(attr, node, context)
|
|
165
165
|
# For Moxml attributes, check if they have namespace information
|
|
166
166
|
if attr.respond_to?(:namespace) && attr.namespace
|
|
167
|
-
namespace_uri =
|
|
168
|
-
attr.namespace.href
|
|
169
|
-
elsif attr.namespace.respond_to?(:uri)
|
|
170
|
-
attr.namespace.uri
|
|
171
|
-
else
|
|
172
|
-
attr.namespace.to_s
|
|
173
|
-
end
|
|
167
|
+
namespace_uri = attr.namespace.uri
|
|
174
168
|
|
|
175
169
|
# Skip if no namespace URI
|
|
176
170
|
return unless namespace_uri && !namespace_uri.empty?
|
|
@@ -250,10 +244,7 @@ module SvgConform
|
|
|
250
244
|
ns.prefix == prefix
|
|
251
245
|
end
|
|
252
246
|
if ns_def
|
|
253
|
-
return ns_def.uri
|
|
254
|
-
return ns_def.href if ns_def.respond_to?(:href)
|
|
255
|
-
|
|
256
|
-
return ns_def.to_s
|
|
247
|
+
return ns_def.uri
|
|
257
248
|
end
|
|
258
249
|
end
|
|
259
250
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "
|
|
3
|
+
require "moxml"
|
|
4
4
|
require "strscan"
|
|
5
5
|
require_relative "base_requirement"
|
|
6
6
|
require_relative "../validation/svg_1_1_document_conformance"
|
|
@@ -77,11 +77,11 @@ module SvgConform
|
|
|
77
77
|
def check_root(source, context)
|
|
78
78
|
root = root_of(source, context)
|
|
79
79
|
return unless root
|
|
80
|
-
return if root.name == "svg" && root.namespace&.
|
|
80
|
+
return if root.name == "svg" && root.namespace&.uri == SVG_NS
|
|
81
81
|
|
|
82
82
|
fail_with(context, "document element must be 'svg' in the " \
|
|
83
83
|
"#{SVG_NS} namespace, found " \
|
|
84
|
-
"'#{root.name}' in '#{root.namespace&.
|
|
84
|
+
"'#{root.name}' in '#{root.namespace&.uri}'")
|
|
85
85
|
end
|
|
86
86
|
|
|
87
87
|
def check_version(source, context)
|
|
@@ -106,10 +106,10 @@ module SvgConform
|
|
|
106
106
|
|
|
107
107
|
def check_css2(source, context)
|
|
108
108
|
parsed = parse(source, context)
|
|
109
|
-
return unless parsed.is_a?(
|
|
109
|
+
return unless parsed.is_a?(Moxml::Document)
|
|
110
110
|
|
|
111
111
|
parsed.traverse do |node|
|
|
112
|
-
next unless node.element
|
|
112
|
+
next unless node.node_type == :element && node.namespace&.uri == SVG_NS
|
|
113
113
|
|
|
114
114
|
check_style_attribute(node, context)
|
|
115
115
|
check_style_element(node, context)
|
|
@@ -118,13 +118,14 @@ module SvgConform
|
|
|
118
118
|
|
|
119
119
|
def check_xml_stylesheet(source, context)
|
|
120
120
|
parsed = parse(source, context)
|
|
121
|
-
return unless parsed.is_a?(
|
|
121
|
+
return unless parsed.is_a?(Moxml::Document)
|
|
122
122
|
|
|
123
123
|
parsed.children.each do |node|
|
|
124
|
-
next unless node.processing_instruction
|
|
124
|
+
next unless node.node_type == :processing_instruction &&
|
|
125
|
+
node.name == STYLESHEET_TARGET
|
|
125
126
|
|
|
126
127
|
stylesheet_problems(node.content).each do |problem|
|
|
127
|
-
fail_with(context, "xml-stylesheet: #{problem}", line: node.
|
|
128
|
+
fail_with(context, "xml-stylesheet: #{problem}", line: node.line_number)
|
|
128
129
|
end
|
|
129
130
|
end
|
|
130
131
|
end
|
|
@@ -148,7 +149,7 @@ module SvgConform
|
|
|
148
149
|
def report_css(result, where, node, context)
|
|
149
150
|
result.errors.each do |error|
|
|
150
151
|
fail_with(context, "CSS 2 grammar in #{where}: #{error}",
|
|
151
|
-
line: node.
|
|
152
|
+
line: node.line_number)
|
|
152
153
|
end
|
|
153
154
|
end
|
|
154
155
|
|
|
@@ -190,16 +191,16 @@ module SvgConform
|
|
|
190
191
|
def parse(source, context)
|
|
191
192
|
state = context.state_for(self)
|
|
192
193
|
state.parsed ||= begin
|
|
193
|
-
doc =
|
|
194
|
-
doc.root ? doc : { error:
|
|
195
|
-
rescue
|
|
194
|
+
doc = Moxml.parse(source)
|
|
195
|
+
doc.root ? doc : { error: Moxml::ParseError.new("document has no root element") }
|
|
196
|
+
rescue Moxml::ParseError => e
|
|
196
197
|
{ error: e }
|
|
197
198
|
end
|
|
198
199
|
end
|
|
199
200
|
|
|
200
201
|
def root_of(source, context)
|
|
201
202
|
parsed = parse(source, context)
|
|
202
|
-
parsed.is_a?(
|
|
203
|
+
parsed.is_a?(Moxml::Document) ? parsed.root : nil
|
|
203
204
|
end
|
|
204
205
|
|
|
205
206
|
def fail_with(context, message, line: nil)
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "
|
|
3
|
+
require "moxml"
|
|
4
|
+
require "leptris"
|
|
4
5
|
|
|
5
6
|
module SvgConform
|
|
6
7
|
module Validation
|
|
@@ -13,10 +14,9 @@ module SvgConform
|
|
|
13
14
|
# from a separate parse of it, and a document that declares an external
|
|
14
15
|
# entity is rejected before any entity is expanded.
|
|
15
16
|
#
|
|
16
|
-
# Library choice (see the validator spec for the probe results):
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
# validates the flat DTD offline.
|
|
17
|
+
# Library choice (see the validator spec for the probe results): leptris
|
|
18
|
+
# validates the flat DTD offline once the DTD is flattened into an
|
|
19
|
+
# internal subset, because leptris never loads external DTDs.
|
|
20
20
|
class Svg11DocumentConformance
|
|
21
21
|
SVG_NS = "http://www.w3.org/2000/svg"
|
|
22
22
|
XLINK_NS = "http://www.w3.org/1999/xlink"
|
|
@@ -26,6 +26,12 @@ module SvgConform
|
|
|
26
26
|
MANIFEST_PATH = File.join(DATA_DIR, "manifest.yml")
|
|
27
27
|
EXTERNAL_ENTITY = /<!ENTITY\s+(?:%\s+)?[^\s>]+\s+(?:SYSTEM|PUBLIC)\b/m
|
|
28
28
|
|
|
29
|
+
PUBLIC_DOCTYPE_RE = /<!DOCTYPE[^>]*>/
|
|
30
|
+
XML_DECL_VERSION = /\A\s*<\?xml[^?]*\bversion\s*=\s*["']([^"']+)["']/
|
|
31
|
+
ELEMENT_DECL = /<!ELEMENT\s+([A-Za-z_:][A-Za-z0-9._:-]*)/
|
|
32
|
+
ATTLIST_DECL = /<!ATTLIST\s+([A-Za-z_:][A-Za-z0-9._:-]*)(.*?)>/m
|
|
33
|
+
ATTR_DECL = /([A-Za-z_:][A-Za-z0-9._:-]*)\s+(?:CDATA|ID|IDREF|IDREFS|ENTITY|ENTITIES|NMTOKEN|NMTOKENS|NOTATION|\()/m
|
|
34
|
+
|
|
29
35
|
Issue = Struct.new(:kind, :message, :line, keyword_init: true)
|
|
30
36
|
|
|
31
37
|
# Outcome of one validation. +transformed+ is the Appendix G.2 copy
|
|
@@ -47,7 +53,7 @@ module SvgConform
|
|
|
47
53
|
parsed = parse_expanded(source)
|
|
48
54
|
return parsed if parsed.is_a?(Result)
|
|
49
55
|
|
|
50
|
-
copy = transform(parsed)
|
|
56
|
+
copy = transform(parsed, xml_decl_version(source))
|
|
51
57
|
return failure(:no_svg_root, "document element is not an SVG element") unless copy
|
|
52
58
|
|
|
53
59
|
Result.new(issues: dtd_issues(copy), transformed: copy)
|
|
@@ -57,18 +63,100 @@ module SvgConform
|
|
|
57
63
|
# keep xlink attributes under the xlink prefix, and set the two
|
|
58
64
|
# namespace declarations on the document element. Returns the serialized
|
|
59
65
|
# copy (with the DOCTYPE for the local DTD) or nil without an SVG root.
|
|
60
|
-
def transform(doc)
|
|
66
|
+
def transform(doc, decl_version = "1.0")
|
|
61
67
|
root = doc.root
|
|
62
|
-
return unless root&.namespace&.
|
|
68
|
+
return unless root&.namespace&.uri == SVG_NS
|
|
63
69
|
|
|
64
70
|
body = +""
|
|
65
71
|
write_element(root, body, root: true)
|
|
66
|
-
%(<?xml version="#{
|
|
72
|
+
%(<?xml version="#{decl_version}" encoding="UTF-8"?>\n) +
|
|
67
73
|
%(<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "#{dtd_uri}">\n#{body}\n)
|
|
68
74
|
end
|
|
75
|
+
# A sequential DTD preprocessor. libxml2 expands a parameter-entity
|
|
76
|
+
# reference to the definition in effect at the reference point, and the
|
|
77
|
+
# flat DTD leans on that: attribute PEs are redeclared per module and
|
|
78
|
+
# qname PEs are redeclared to literal element names after the modular
|
|
79
|
+
# machinery. A batch expansion cannot reproduce it, so the text is
|
|
80
|
+
# walked in order: PE declarations register and vanish, conditional
|
|
81
|
+
# sections resolve to INCLUDE content or nothing, and every remaining
|
|
82
|
+
# reference expands live. The result is an internal-subset-safe
|
|
83
|
+
# declaration stream (no PE references, no conditional sections).
|
|
84
|
+
|
|
85
|
+
def self.flatten_dtd(dtd_text)
|
|
86
|
+
current = {}
|
|
87
|
+
out = +""
|
|
88
|
+
process_section(dtd_text, current, out)
|
|
89
|
+
out
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
PE_TOKEN = %r{
|
|
93
|
+
<!ENTITY\s+%\s+([A-Za-z0-9._-]+)\s+(["'])(.*?)\2\s*>
|
|
94
|
+
|<!\[\s*(?:(INCLUDE|IGNORE)|%([A-Za-z0-9._-]+);?)\s*\[
|
|
95
|
+
|%([A-Za-z0-9._-]+);
|
|
96
|
+
}xm
|
|
97
|
+
CONDITIONAL_BODY_OPEN = /<!\[\s*(?:(?:INCLUDE|IGNORE)|%[A-Za-z0-9._-]+;?)\s*\[/i
|
|
98
|
+
MAX_PE_DEPTH = 50
|
|
99
|
+
|
|
100
|
+
def self.process_section(text, current, out)
|
|
101
|
+
pos = 0
|
|
102
|
+
while (match = PE_TOKEN.match(text, pos))
|
|
103
|
+
out << expand_refs(text[pos...match.begin(0)], current)
|
|
104
|
+
if match[1]
|
|
105
|
+
# libxml2 keeps the first declaration of a parameter entity and
|
|
106
|
+
# ignores later duplicates.
|
|
107
|
+
current[match[1]] ||= match[3]
|
|
108
|
+
pos = match.end(0)
|
|
109
|
+
elsif match[4] || match[5]
|
|
110
|
+
keyword = expand_refs(match[4] || current[match[5]] || "", current)
|
|
111
|
+
.to_s.strip.upcase
|
|
112
|
+
body_start, body_end, pos = conditional_extent(text, match.end(0))
|
|
113
|
+
process_section(text[body_start...body_end], current, out) if keyword == "INCLUDE"
|
|
114
|
+
else
|
|
115
|
+
out << expand_refs(match[0], current)
|
|
116
|
+
pos = match.end(0)
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
out << expand_refs(text[pos..], current)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# Offsets of one conditional section's body: [start, end, after_close],
|
|
123
|
+
# honouring nested conditional sections.
|
|
124
|
+
def self.conditional_extent(text, open_end)
|
|
125
|
+
depth = 1
|
|
126
|
+
pos = open_end
|
|
127
|
+
while depth.positive?
|
|
128
|
+
close = text.index("]]>", pos)
|
|
129
|
+
raise ArgumentError, "unterminated conditional section" unless close
|
|
130
|
+
|
|
131
|
+
inner = CONDITIONAL_BODY_OPEN.match(text, pos)
|
|
132
|
+
if inner && inner.end(0) <= close + 3
|
|
133
|
+
depth += 1
|
|
134
|
+
pos = inner.end(0)
|
|
135
|
+
else
|
|
136
|
+
depth -= 1
|
|
137
|
+
pos = close + 3
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
[open_end, pos - 3, pos]
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def self.expand_refs(text, current, depth = 0)
|
|
144
|
+
raise ArgumentError, "parameter entity recursion too deep" if depth > MAX_PE_DEPTH
|
|
145
|
+
|
|
146
|
+
text.gsub(/%([A-Za-z0-9._-]+);/) do
|
|
147
|
+
value = current[Regexp.last_match(1)]
|
|
148
|
+
value ? expand_refs(value, current, depth + 1) : Regexp.last_match(0)
|
|
149
|
+
end
|
|
150
|
+
end
|
|
69
151
|
|
|
70
152
|
private
|
|
71
153
|
|
|
154
|
+
# Moxml documents do not expose the XML declaration; read it from the
|
|
155
|
+
# source text.
|
|
156
|
+
def xml_decl_version(source)
|
|
157
|
+
source.match(XML_DECL_VERSION)&.captures&.first || "1.0"
|
|
158
|
+
end
|
|
159
|
+
|
|
72
160
|
# A file URI that libxml2 accepts for POSIX paths and for Windows drive
|
|
73
161
|
# paths ("D:\\a\\x" becomes "file:///D:/a/x").
|
|
74
162
|
def dtd_uri
|
|
@@ -89,8 +177,8 @@ module SvgConform
|
|
|
89
177
|
# Entities are expanded (Appendix G.2) only after the source is known
|
|
90
178
|
# to declare no external entity, so expansion cannot read a file.
|
|
91
179
|
def parse_expanded(source)
|
|
92
|
-
|
|
93
|
-
rescue
|
|
180
|
+
Moxml.parse(source)
|
|
181
|
+
rescue Moxml::ParseError => e
|
|
94
182
|
failure(:malformed_xml, e.message, e.line)
|
|
95
183
|
end
|
|
96
184
|
|
|
@@ -99,7 +187,7 @@ module SvgConform
|
|
|
99
187
|
if root
|
|
100
188
|
out << %( xmlns="#{SVG_NS}" xmlns:xlink="#{XLINK_NS}")
|
|
101
189
|
end
|
|
102
|
-
node.
|
|
190
|
+
node.attributes.each { |attr| write_attribute(attr, out) }
|
|
103
191
|
children = node.children
|
|
104
192
|
if children.empty?
|
|
105
193
|
out << "/>"
|
|
@@ -111,7 +199,7 @@ module SvgConform
|
|
|
111
199
|
end
|
|
112
200
|
|
|
113
201
|
def write_attribute(attr, out)
|
|
114
|
-
href = attr.namespace&.
|
|
202
|
+
href = attr.namespace&.uri
|
|
115
203
|
name = case href
|
|
116
204
|
when nil then attr.name
|
|
117
205
|
when XLINK_NS then "xlink:#{attr.name}"
|
|
@@ -121,12 +209,12 @@ module SvgConform
|
|
|
121
209
|
end
|
|
122
210
|
|
|
123
211
|
def write_node(node, out)
|
|
124
|
-
case node.
|
|
125
|
-
when
|
|
126
|
-
write_element(node, out) if node.namespace&.
|
|
127
|
-
when
|
|
212
|
+
case node.node_type
|
|
213
|
+
when :element
|
|
214
|
+
write_element(node, out) if node.namespace&.uri == SVG_NS
|
|
215
|
+
when :text, :cdata
|
|
128
216
|
out << escape_text(node.content)
|
|
129
|
-
when
|
|
217
|
+
when :comment
|
|
130
218
|
out << "<!--" << node.content << "-->"
|
|
131
219
|
end
|
|
132
220
|
end
|
|
@@ -140,15 +228,89 @@ module SvgConform
|
|
|
140
228
|
.gsub("\n", " ").gsub("\r", " ")
|
|
141
229
|
end
|
|
142
230
|
|
|
231
|
+
# leptris never loads external DTDs, so the flat DTD cannot be applied
|
|
232
|
+
# through the DOCTYPE the transform emits. For validation only, the
|
|
233
|
+
# vendored DTD is flattened into an internal subset: parameter entities
|
|
234
|
+
# are expanded textually and conditional sections resolved, because
|
|
235
|
+
# libxml2 permits neither PE references nor conditional sections in an
|
|
236
|
+
# internal subset and silently drops the whole subset otherwise.
|
|
237
|
+
#
|
|
238
|
+
# leptris also does not flag undeclared elements and attributes, so the
|
|
239
|
+
# same flattened DTD is scanned for its ELEMENT and ATTLIST
|
|
240
|
+
# declarations to reproduce those checks offline. Messages keep the
|
|
241
|
+
# libxml2 wording the validator contract is written against.
|
|
143
242
|
def dtd_issues(copy)
|
|
144
|
-
|
|
145
|
-
|
|
243
|
+
validation_source = copy.sub(PUBLIC_DOCTYPE_RE, inline_doctype)
|
|
244
|
+
doc = Leptris::XML.parse(validation_source)
|
|
245
|
+
issues = Leptris::XML::DTD.from_document(doc).validate(doc)
|
|
246
|
+
.map { |error| normalize_dtd_error(error) }
|
|
247
|
+
issues.concat(declaration_issues(Moxml.parse(validation_source)))
|
|
248
|
+
issues.map { |issue| Issue.new(kind: :dtd, message: issue[:message], line: issue[:line]) }
|
|
249
|
+
rescue Leptris::XML::Error, Moxml::ParseError => e
|
|
250
|
+
[Issue.new(kind: :dtd, message: e.message, line: nil)]
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
def normalize_dtd_error(error)
|
|
254
|
+
message = error[:message].to_s.sub(/\A\d+:\d+: ERROR: /, "")
|
|
255
|
+
message = case message
|
|
256
|
+
when /\AElement '([^']+)' missing #REQUIRED attribute '([^']+)'\z/
|
|
257
|
+
"Element #{Regexp.last_match(1)} does not carry attribute #{Regexp.last_match(2)}"
|
|
258
|
+
when /\AElement '([^']+)' content model requires (.+)\z/m
|
|
259
|
+
"Element #{Regexp.last_match(1)} content does not follow the DTD, " \
|
|
260
|
+
"expecting #{Regexp.last_match(2)}"
|
|
261
|
+
else
|
|
262
|
+
message
|
|
263
|
+
end
|
|
264
|
+
{ message: message, line: error[:line] }
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def declaration_issues(doc)
|
|
268
|
+
issues = []
|
|
269
|
+
doc.traverse do |node|
|
|
270
|
+
next unless node.node_type == :element
|
|
271
|
+
|
|
272
|
+
name = node.name
|
|
273
|
+
unless declared_elements.include?(name)
|
|
274
|
+
issues << { message: "No declaration for element #{name}", line: node.line_number }
|
|
275
|
+
end
|
|
276
|
+
node.attributes.each do |attr|
|
|
277
|
+
key = prefixed_attribute_name(attr)
|
|
278
|
+
next if key.start_with?("xmlns")
|
|
279
|
+
next if declared_attributes[name]&.include?(key)
|
|
280
|
+
|
|
281
|
+
issues << { message: "No declaration for attribute #{key} on element #{name}",
|
|
282
|
+
line: node.line_number }
|
|
283
|
+
end
|
|
146
284
|
end
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
285
|
+
issues
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
def prefixed_attribute_name(attr)
|
|
289
|
+
case attr.namespace&.uri
|
|
290
|
+
when XLINK_NS then "xlink:#{attr.name}"
|
|
291
|
+
when XML_NS then "xml:#{attr.name}"
|
|
292
|
+
else attr.name
|
|
150
293
|
end
|
|
151
294
|
end
|
|
295
|
+
|
|
296
|
+
def declared_elements
|
|
297
|
+
@declared_elements ||= flattened_dtd.scan(ELEMENT_DECL).to_set(&:first)
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
def declared_attributes
|
|
301
|
+
@declared_attributes ||= flattened_dtd.scan(ATTLIST_DECL)
|
|
302
|
+
.each_with_object({}) do |(element, body), map|
|
|
303
|
+
map[element] = body.scan(ATTR_DECL).to_set(&:first)
|
|
304
|
+
end
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
def inline_doctype
|
|
308
|
+
@inline_doctype ||= "<!DOCTYPE svg [#{flattened_dtd}]>"
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
def flattened_dtd
|
|
312
|
+
@flattened_dtd ||= self.class.flatten_dtd(File.read(@dtd_path))
|
|
313
|
+
end
|
|
152
314
|
end
|
|
153
315
|
end
|
|
154
316
|
end
|