url-regex
url-regex copied to clipboard
Don't allow ' in path
failing test case: background: url('http://example.com/pic.jpg');
resulted in http://example.com/pic.jpg'
fixed #54
' is a valid character in the path: https://stackoverflow.com/questions/4669692/valid-characters-for-directory-part-of-a-url-for-short-links
well than... So the test cast I've mentioned is ambiguous. One can make the argument that it would be better to not support ' as part of the path. Many use cases of this module might encounter an enclosing ', but much fewer will encounter them as an intended charterer of the url
What do you think? If you'll decide to stick to the specs - also a valid argument, close this issue.
I think this could be addressed in get-urls instead of here: adding an option to cut off anything beginning with ' or ". That is what I did:
// @flow
import urlRegex from 'url-regex';
import {
uniq,
} from 'lodash';
import normalizeUrl from './normalizeUrl';
const extractUrl = (subject: string): string => {
const matchedUrls = subject.match(urlRegex()) || [];
const normalizedUrls = uniq(matchedUrls.map((matchedUrl) => {
return normalizeUrl(matchedUrl.split(/['"]/)[0]);
}));
if (normalizedUrls.length === 0) {
throw new Error('URL not found.');
}
if (normalizedUrls.length > 1) {
throw new Error('Found multiple URLs. Input must contain at most one URL.');
}
return normalizedUrls[0];
};
~Feel free to submit a PR to https://github.com/niftylettuce/url-regex-safe.~ See comment below
I have already done this for you, and added an apostrophe option to url-regex-safe, which is false by default (which matches the PR behavior you've submitted). Please try url-safe-regex v0.0.4+ at https://github.com/niftylettuce/url-safe-regex.