relaton-render 1.2.1 → 1.2.2

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: 4bcf0448a633329d256fee9e297e7c50182a5e214014a7cb78a9d3ab7ec3147f
4
- data.tar.gz: 766c8ddbb23a88334eaf2d3be35894ffe62ce300126c7cdded5af90e8341a7a6
3
+ metadata.gz: ece7911d68f25665c109d3a9420e30aab669c880655c5bb15b4146c8acee4f43
4
+ data.tar.gz: 1ba627049f678c0b817da7a93af4420b74e34579965a65eeaeb0f80126abed02
5
5
  SHA512:
6
- metadata.gz: 9ff2d1381a4075e7c0c5b46a5fb174b1190621b25760600928f2b49da2379eea21006f17468d74c89e9c1f30cd8abb49f591bd5361e5ab57e029a6de04d09c57
7
- data.tar.gz: 18b0f1710c29a3e8ce67f055d3dcdefeaf62191ff7711beaa296bf95f669f49699186868172a617a429765c9544de52eb30669b98c2b63f41a46c76b36b91b65
6
+ metadata.gz: 92ad5ab36726f3b857ff9d792cce3ae2ba3ba0b8adba21308b0596da88822f16e4131b92c7047b31105036c534c552815d12e10b3e0773cbf04634769713c8f5
7
+ data.tar.gz: 790abb7747575c3b990fdb6a4ed579c3fc2269d35b1e05f0d3ab6c0869686784b4b91dfd8c0f9d7a006516c681e1821604f6351e91918fd210df78c9f106e760
@@ -82,7 +82,12 @@ module Relaton
82
82
  end
83
83
  end
84
84
 
85
- ALLOWED_INLINE_TAGS = %w[em strong sub sup a smallcap].freeze
85
+ ALLOWED_INLINE_TAGS = %w[
86
+ em eref strong stem sub sup tt underline keyword ruby
87
+ strike smallcap xref br link hr pagebreak bookmark
88
+ image index index-xref concept add del span erefstack
89
+ date fn
90
+ ].freeze
86
91
 
87
92
  private
88
93
 
@@ -100,12 +105,39 @@ module Relaton
100
105
  # output against embedded structural markup (most commonly <title>) in
101
106
  # bibliographic text fields, where relaton-bib may surface either form
102
107
  # depending on lutaml-model serialisation behaviour.
108
+ #
109
+ # Authors embed inline markup either literally (<em>foo</em>) or
110
+ # entity-encoded (&lt;em&gt;foo&lt;/em&gt;); both are intended as
111
+ # markup, and downstream template rendering decodes entities anyway
112
+ # (see HTMLEntities.decode in template.rb), so we normalise the
113
+ # encoded form to literal up front and let a single Nokogiri-fragment
114
+ # walk handle both. Elements whose name is in the allow-list are
115
+ # preserved verbatim with their entire subtree, so wrappers that
116
+ # legitimately carry block content (notably <fn><p>…</p></fn>)
117
+ # round-trip intact rather than being shredded one tag at a time.
118
+ # Disallowed elements are unwrapped (tag dropped, children kept),
119
+ # recursing first so allowed wrappers nested inside disallowed
120
+ # wrappers still survive. Nokogiri re-escapes any genuine `<` left in
121
+ # text positions on serialisation, so plain prose like "5 < 10" is
122
+ # preserved correctly.
103
123
  def sanitise_inline_markup(str)
104
124
  blank?(str) and return str
105
- allowed = ALLOWED_INLINE_TAGS.join("|")
106
- literal = %r{</?(?!(?:#{allowed})\b)[A-Za-z][\w:-]*(?:\s[^>]*)?/?>}
107
- encoded = %r{&lt;/?(?!(?:#{allowed})\b)[A-Za-z][\w:-]*(?:\s[^&]*?)?/?&gt;}
108
- str.gsub(literal, "").gsub(encoded, "")
125
+ normalised = str.gsub("&lt;", "<").gsub("&gt;", ">")
126
+ frag = Nokogiri::XML.fragment(normalised)
127
+ unwrap_disallowed(frag)
128
+ save_opts = Nokogiri::XML::Node::SaveOptions::AS_XML |
129
+ Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
130
+ frag.to_xml(encoding: "UTF-8", save_with: save_opts)
131
+ end
132
+
133
+ def unwrap_disallowed(node)
134
+ node.children.to_a.each do |child|
135
+ next unless child.element?
136
+ next if ALLOWED_INLINE_TAGS.include?(child.name)
137
+
138
+ unwrap_disallowed(child)
139
+ child.replace(child.children)
140
+ end
109
141
  end
110
142
 
111
143
  def wrap_in_esc(obj)
@@ -1,5 +1,5 @@
1
1
  module Relaton
2
2
  module Render
3
- VERSION = "1.2.1".freeze
3
+ VERSION = "1.2.2".freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relaton-render
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.