Skip to content

Commit

Permalink
Merge pull request #1336 from JFox23/issue_1327
Browse files Browse the repository at this point in the history
fix: #1327 escape URL for safe use in regular expression
  • Loading branch information
JiHong88 authored Dec 6, 2023
2 parents 8443bbb + 39cf59a commit 4210ad8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,23 @@ const util = {
}.bind(this));
},

/**
* @description Escape a string for safe use in regular expressions.
* @param {String} string String to escape
* @returns {String}
*/
escapeStringRegexp: function (string) {
if (typeof string !== 'string') {
throw new TypeError('Expected a string');
}

// Escape characters with special meaning either inside or outside character sets.
// Use a simple backslash escape when it’s always valid, and a `\xnn` escape when the simpler form would be disallowed by Unicode patterns’ stricter grammar.
return string
.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
.replace(/-/g, '\\x2d');
},

_isExcludeSelectionElement: function (element) {
return !/FIGCAPTION/i.test(element.nodeName) && (this.isComponent(element) || /FIGURE/i.test(element.nodeName));
},
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/modules/_anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ export default {
const protocol = this.options.linkProtocol;
const noPrefix = this.options.linkNoPrefix;
const reservedProtocol = /^(mailto\:|tel\:|sms\:|https*\:\/\/|#)/.test(value) || value.indexOf(protocol) === 0;
const sameProtocol = !protocol ? false : this._w.RegExp('^' + value.substr(0, protocol.length)).test(protocol);
const sameProtocol = !protocol ? false : this._w.RegExp('^' + this.util.escapeStringRegexp(value.substr(0, protocol.length))).test(protocol);
value = context.linkValue = preview.textContent = !value ? '' : noPrefix ? value : (protocol && !reservedProtocol && !sameProtocol) ? protocol + value : reservedProtocol ? value : /^www\./.test(value) ? 'http://' + value : this.context.anchor.host + (/^\//.test(value) ? '' : '/') + value;

if (this.plugins.anchor.selfPathBookmark.call(this, value)) {
Expand Down

0 comments on commit 4210ad8

Please sign in to comment.