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

improve url handling and support uri schemes and fragments #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"codex"
],
"rules": {
"jsdoc/no-undefined-types": "off"
"jsdoc/no-undefined-types": "off",
"no-control-regex": 0
}
}
1 change: 0 additions & 1 deletion src/utils/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,3 @@ export function isElement(node) {

return node && node.nodeType && node.nodeType === Node.ELEMENT_NODE;
}

6 changes: 4 additions & 2 deletions src/utils/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ export class SelectionUtils {

/**
* Methods return boolean that true if selection exists on the page
*
* @returns {boolean}
*/
static get isSelectionExists() {
const selection = SelectionUtils.get();
Expand Down Expand Up @@ -208,8 +210,8 @@ export class SelectionUtils {
/**
* Set focus to contenteditable or native input element
*
* @param element - element where to set focus
* @param offset - offset of cursor
* @param {HTMLElement} element - element where to set focus
* @param {number} offset - offset of cursor
*
* @returns {DOMRect} of range
*/
Expand Down
32 changes: 30 additions & 2 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,36 @@ export class Utils {
* @returns {boolean}
*/
static isUrl(textString) {
const regex = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/;
let test;
const URISchemes = {
Mailto: 'mailto:',
Tel: 'tel:',
};
// Regex for valid urls.
// protocol:// sub.domain. tld /extra?a=b#hash
const urlRegex = /https?:\/\/(?:[a-zA-Z0-9-]+\.)+(?:[a-zA-Z0-9-]+)[^\s]*/;

return regex.test(textString);
// Check for URI fragments
if (textString.startsWith('#')) {
test = true;
// Check for URI schemes
} else if (textString.startsWith(URISchemes.Mailto)) {
// Regex for current specification for email addresses RFC 5322
const emailRegex = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/;

textString = textString.substring(URISchemes.Mailto.length);
test = emailRegex.test(textString);
} else if (textString.startsWith(URISchemes.Tel)) {
// Regex for any phone number and flexible formatting
const telRegex = /[+]?[0-9](?:[0-9-.()])+[0-9]/;

textString = textString.substring(URISchemes.Tel.length);
test = telRegex.test(textString);
} else {
// String will contain a standard url
test = urlRegex.test(textString);
}

return test;
};
}