relaton-bib 2.1.1 → 2.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dbd5aea414046c022dca2c76a39b03469e284b3e40e5f466d45cc2095b872426
4
- data.tar.gz: 549058e3a8f7d02fa187cc49764c674894bbf8853ec1aca87de367320f8a3700
3
+ metadata.gz: ddc9644848b2d2d3215b088b0490e2769f541d3415806d6cee1a7d5f5f459ea3
4
+ data.tar.gz: 1ed5a54ad4dc216b17c0c29bbec68248e36e243f918f4edf753b3827cf163e3a
5
5
  SHA512:
6
- metadata.gz: 247065b32df801e9e0d4b01b66ca3e9ce3c17840eb7209f61d0717df322e84ef0996b1de3d706990b774733f3018e5dc8fb0c4f36a320ca8f2b94b5732163748
7
- data.tar.gz: 31e0342c3793206c94478852cec92e31f4274aca78f69ed64b7ad2e79a39b66b6e68e326e22e5b38e9f8ecd2c69e38e4942a2939b7175a3869b7d73dcc273a8a
6
+ metadata.gz: 67bf2aa865f1b688e72c02e8b5c724a3db3a038ce4db56b9a20ed343ee5e39546c71d1fcf0b5390b2031c018290cbf99f8529f94cfb8dd41f63b06267f5800af
7
+ data.tar.gz: 7176d810c8e1290fe038b6bfc390ddfd5e1676fe9cafcb799deeb2a279c2f54369c9aa0a578ae2344052a0fa42a8932cbd716144478e0af82b0ea2d3ba531b6b
@@ -12,11 +12,37 @@ module Relaton
12
12
  map_element "text", to: :text
13
13
  end
14
14
 
15
+ # Returns the explicit text if set, else the Isoics description for
16
+ # `code`. Kept for consumers that read `.text` directly.
15
17
  def text
16
- val = @text
17
- return val if val && !val.empty?
18
+ return @text if @text.is_a?(String) && !@text.empty?
18
19
 
19
- Isoics.fetch(code)&.description if code
20
+ Isoics.fetch(code)&.description if code.is_a?(String) && !code.empty?
21
+ end
22
+
23
+ # When code is assigned, eagerly populate text from Isoics if no
24
+ # explicit text has been set. Going through the public writer
25
+ # registers the value with the lutaml-model `value_set_for` tracker
26
+ # so the attribute is emitted on serialization.
27
+ def code=(val)
28
+ super
29
+ return unless val.is_a?(String) && !val.empty?
30
+ return if @text.is_a?(String) && !@text.empty?
31
+
32
+ description = Isoics.fetch(val)&.description
33
+ self.text = description if description
34
+ end
35
+
36
+ # When the deserializer reaches the end of the XML element and
37
+ # records that <text> was absent, it calls `using_default_for(:text)`
38
+ # to mark the attribute as default-valued (suppressing serialization).
39
+ # Refuse that mark if we've already populated text from Isoics so the
40
+ # value survives round-trip. See #112.
41
+ def using_default_for(attribute_name)
42
+ return if attribute_name == :text &&
43
+ @text.is_a?(String) && !@text.empty?
44
+
45
+ super
20
46
  end
21
47
  end
22
48
  end
@@ -26,7 +26,14 @@ module Relaton
26
26
  end
27
27
 
28
28
  class LocalizedMarkedUpString < LocalizedStringAttrs
29
+ module ContentSanitization
30
+ def content=(value)
31
+ super(Relaton::Bib::Sanitizer.sanitize(value))
32
+ end
33
+ end
34
+
29
35
  attribute :content, :string, raw: true
36
+ prepend ContentSanitization
30
37
 
31
38
  xml do
32
39
  map_all to: :content
@@ -0,0 +1,47 @@
1
+ require "nokogiri"
2
+
3
+ module Relaton
4
+ module Bib
5
+ # Strips inline markup not in the basicdoc PureTextElement set
6
+ # (plus <p>, <eref>, <xref>) from raw marked-up content strings.
7
+ # Disallowed elements are unwrapped: tags removed, inner text kept.
8
+ module Sanitizer
9
+ ALLOWED = %w[
10
+ em strong sub sup tt underline strike smallcap br stem
11
+ p eref xref
12
+ ].freeze
13
+
14
+ RENAME = {
15
+ "italic" => "em",
16
+ }.freeze
17
+
18
+ TAG_RX = %r{<[a-zA-Z/!?]}
19
+
20
+ def self.sanitize(content)
21
+ return content unless sanitizable?(content)
22
+
23
+ fragment = Nokogiri::XML::DocumentFragment.parse(content)
24
+ return content if fragment.errors.any?
25
+
26
+ sanitize_children(fragment)
27
+ fragment.children.map { |c| c.to_xml(encoding: "UTF-8") }.join
28
+ end
29
+
30
+ def self.sanitizable?(content)
31
+ content.is_a?(::String) && !content.empty? && content.match?(TAG_RX)
32
+ end
33
+ private_class_method :sanitizable?
34
+
35
+ def self.sanitize_children(node)
36
+ node.children.to_a.each do |child|
37
+ next unless child.element?
38
+
39
+ child.name = RENAME[child.name] if RENAME.key?(child.name)
40
+ sanitize_children(child)
41
+ child.replace(child.children) unless ALLOWED.include?(child.name)
42
+ end
43
+ end
44
+ private_class_method :sanitize_children
45
+ end
46
+ end
47
+ end
@@ -1,5 +1,5 @@
1
1
  module Relaton
2
2
  module Bib
3
- VERSION = "2.1.1".freeze
3
+ VERSION = "2.1.3".freeze
4
4
  end
5
5
  end
data/lib/relaton/bib.rb CHANGED
@@ -7,6 +7,7 @@ require "rfcxml"
7
7
  require "relaton/core"
8
8
  require_relative "bib/version"
9
9
  require_relative "bib/util"
10
+ require_relative "bib/sanitizer"
10
11
  require_relative "bib/namespace_helper"
11
12
  require_relative "bib/item_data"
12
13
  require_relative "bib/model/item"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relaton-bib
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-05-06 00:00:00.000000000 Z
11
+ date: 2026-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bibtex-ruby
@@ -232,6 +232,7 @@ files:
232
232
  - lib/relaton/bib/model/validity.rb
233
233
  - lib/relaton/bib/model/version.rb
234
234
  - lib/relaton/bib/namespace_helper.rb
235
+ - lib/relaton/bib/sanitizer.rb
235
236
  - lib/relaton/bib/util.rb
236
237
  - lib/relaton/bib/version.rb
237
238
  - relaton-bib.gemspec