Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deal with escaping links that are textual content of links when prote… #908

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions lib/metanorma/standoc/macros.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
def init_indent(line)
/^(?<prefix>[ \t]*)(?<suffix>.*)$/ =~ line
prefix = prefix.gsub("\t", "\u00a0\u00a0\u00a0\u00a0")
.gsub(/ /, "\u00a0")
.tr(" ", "\u00a0")
prefix + suffix
end

Expand Down Expand Up @@ -127,7 +127,7 @@
def inlinelink_escape(text)
text.gsub(InlineLinkRx) do
body, suffix = $4.nil? ? [$3 + $6, "[]"] : [$3, ""]
p = $1 and s = $2 and b = $4
p = $1 and s = $2 and b = linkcontents_escape($4)
if p == "link:" then "#{p}++#{s}#{body}++#{b}#{suffix}"
elsif p == "<"
"#{p}link:++#{s}#{body.sub(/>$/, '')}++#{b}#{suffix}>"
Expand All @@ -136,9 +136,24 @@
end
end

# because links are escaped, https within link text also need
# to be escaped, # otherwise they will be treated as links themselves
def linkcontents_escape(text)
text.nil? and return nil
text
# .gsub(InlineLinkMacroRx) do
# $1.empty? ? "\\#{$2}#{$3}#{$4}" : text
# end
.gsub(InlineLinkRx) do
esc = $1 == "link:" ? "" : "\\"
x = $4 || "#{$5}#{$6}"
"#{$1}#{esc}#{$2}#{$3}#{x}"
end
Comment on lines +143 to +151

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on a
library input
may run slow on strings starting with 'irc://!' and with many repetitions of '!!'.
end

# InlineLinkMacroRx = /\\?(?:link|(mailto)):(|[^:\s\[][^\s\[]*)\[(|#{CC_ALL}*?[^\\])\]/m
InlineLinkMacroRx1 = <<~REGEX.freeze
(\\\\?\\b(?<!-) # optional backslash, no hyphen, word boundary
(\\\\?)(\\b(?<!-) # optional backslash, no hyphen, word boundary
(?:link|mailto):) # link: or mailto:
(?!\\+) # no link:+ passthrough
(|[^:\\s\\[][^\\s\\[]*) # link: ... up to [
Expand All @@ -151,7 +166,7 @@
((text.include? "link:") || (text.include? "ilto:"))) or return text
pass_inline_split(text) do |x|
x.gsub(InlineLinkMacroRx) do
"#{$1}++#{$2}++#{$3}"
"#{$1}#{$2}++#{$3}++#{linkcontents_escape($4)}"
end
end.join
end
Expand Down
30 changes: 30 additions & 0 deletions spec/metanorma/cleanup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,18 @@

http://www.example.com/...abc[x]

http://www.example.com/...abc[http://www.example.com/...abc]

http://www.example.com/...abc[http://www.example.com/abc]

http://www.example.com/...abc[http://www.example.com/...abc]

http://www.example.com/...abc[\\http://www.example.com/...abc]

http://www.example.com/...abc[link:http://www.example.com/...abc[\\]]

http://www.example.com/...abc[\\link:http://www.example.com/...abc[\\]]

++http://www.example.com++

https://isotc.iso.org/livelink/livelink/fetch/-15620806/15620808/15623592/15768654/TMB_resolutions_-_2012_%28Resolution_1-148%29.pdf?nodeid=15768229&vernum=-2
Expand Down Expand Up @@ -842,6 +854,24 @@
<p id="_">
<link target="http://www.example.com/...abc">x</link>
</p>
<p id="_">
<link target="http://www.example.com/...abc">http://www.example.com/…​abc</link>
</p>
<p id="_">
<link target="http://www.example.com/...abc">http://www.example.com/abc</link>
</p>
<p id="_">
<link target="http://www.example.com/...abc">http://www.example.com/…​abc</link>
</p>
<p id="_">
<link target="http://www.example.com/...abc">http://www.example.com/…​abc</link>
</p>
<p id="_">
<link target="http://www.example.com/...abc">link:http://www.example.com/…abc[]</link>
</p>
<p id="_">
<link target="http://www.example.com/...abc">\\link:http://www.example.com/…abc[]</link>
</p>
<p id="_">http://www.example.com</p>
<p id="_">
<link target="https://isotc.iso.org/livelink/livelink/fetch/-15620806/15620808/15623592/15768654/TMB_resolutions_-_2012_%28Resolution_1-148%29.pdf?nodeid=15768229&amp;vernum=-2"/>
Expand Down
Loading