url-regex icon indicating copy to clipboard operation
url-regex copied to clipboard

Don't allow ' in path

Open alexpusch opened this issue 7 years ago • 5 comments

failing test case: background: url('http://example.com/pic.jpg'); resulted in http://example.com/pic.jpg'

fixed #54

alexpusch avatar Mar 29 '18 12:03 alexpusch

' is a valid character in the path: https://stackoverflow.com/questions/4669692/valid-characters-for-directory-part-of-a-url-for-short-links

sindresorhus avatar Apr 21 '19 07:04 sindresorhus

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.

alexpusch avatar Apr 26 '19 18:04 alexpusch

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];
};

gajus avatar Aug 26 '19 13:08 gajus

~Feel free to submit a PR to https://github.com/niftylettuce/url-regex-safe.~ See comment below

niftylettuce avatar Aug 15 '20 08:08 niftylettuce

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.

niftylettuce avatar Aug 15 '20 10:08 niftylettuce