src/loader.js
function sanitize(uri, options) {
  options = extend({}, this.options, options);

  return new Promise(function(accept, reject) {
    var result = {href: null},
        isFile, hasProtocol, loadFile, base;

    if (uri == null || typeof uri !== 'string') {
      reject('Sanitize failure, invalid URI: ' + stringValue(uri));
      return;
    }

    hasProtocol = protocol_re.test(uri);

    // if relative url (no protocol/host), prepend baseURL
    if ((base = options.baseURL) && !hasProtocol) {
      // Ensure that there is a slash between the baseURL (e.g. hostname) and url
      if (!startsWith(uri, '/') && base[base.length-1] !== '/') {
        uri = '/' + uri;
      }
      uri = base + uri;
    }

    // should we load from file system?
    loadFile = (isFile = startsWith(uri, fileProtocol))
      || options.mode === 'file'
      || options.mode !== 'http' && !hasProtocol && fs();

    if (isFile) {
      // strip file protocol
      uri = uri.slice(fileProtocol.length);
    } else if (startsWith(uri, '//')) {
      if (options.defaultProtocol === 'file') {
        // if is file, strip protocol and set loadFile flag
        uri = uri.slice(2);
        loadFile = true;
      } else {
        // if relative protocol (starts with '//'), prepend default protocol
        uri = (options.defaultProtocol || 'http') + ':' + uri;
      }
    }

    // set non-enumerable mode flag to indicate local file load
    Object.defineProperty(result, 'localFile', {value: !!loadFile});

    // set uri
    result.href = uri;

    // set default result target, if specified
    if (options.target) {
      result.target = options.target + '';
    }

    // return
    accept(result);
  });
}
function sanitize(uri, options) {
  options = extend({}, this.options, options);

  return new Promise(function(accept, reject) {
    var result = {href: null},
        isFile, hasProtocol, loadFile, base;

    if (uri == null || typeof uri !== 'string') {
      reject('Sanitize failure, invalid URI: ' + stringValue(uri));
      return;
    }

    hasProtocol = protocol_re.test(uri);

    // if relative url (no protocol/host), prepend baseURL
    if ((base = options.baseURL) && !hasProtocol) {
      // Ensure that there is a slash between the baseURL (e.g. hostname) and url
      if (!startsWith(uri, '/') && base[base.length-1] !== '/') {
        uri = '/' + uri;
      }
      uri = base + uri;
    }

    // should we load from file system?
    loadFile = (isFile = startsWith(uri, fileProtocol))
      || options.mode === 'file'
      || options.mode !== 'http' && !hasProtocol && fs();

    if (isFile) {
      // strip file protocol
      uri = uri.slice(fileProtocol.length);
    } else if (startsWith(uri, '//')) {
      if (options.defaultProtocol === 'file') {
        // if is file, strip protocol and set loadFile flag
        uri = uri.slice(2);
        loadFile = true;
      } else {
        // if relative protocol (starts with '//'), prepend default protocol
        uri = (options.defaultProtocol || 'http') + ':' + uri;
      }
    }

    // set non-enumerable mode flag to indicate local file load
    Object.defineProperty(result, 'localFile', {value: !!loadFile});

    // set uri
    result.href = uri;

    // set default result target, if specified
    if (options.target) {
      result.target = options.target + '';
    }

    // return
    accept(result);
  });
}
function sanitize ( uri , options ) { options = extend ( { } , this . options , options ) ; return new Promise ( function ( accept , reject ) { var result = { href : null } , isFile , hasProtocol , loadFile , base ; if ( uri == null || typeof uri !== 'string' ) { reject ( 'Sanitize failure, invalid URI: ' + stringValue ( uri ) ) ; return ; } hasProtocol = protocol_re . test ( uri ) ; // if relative url (no protocol/host), prepend baseURL if ( ( base = options . baseURL ) && ! hasProtocol ) { // Ensure that there is a slash between the baseURL (e.g. hostname) and url if ( ! startsWith ( uri , '/' ) && base [ base . length - 1 ] !== '/' ) { uri = '/' + uri ; } uri = base + uri ; } // should we load from file system? loadFile = ( isFile = startsWith ( uri , fileProtocol ) ) || options . mode === 'file' || options . mode !== 'http' && ! hasProtocol && fs ( ) ; if ( isFile ) { // strip file protocol uri = uri . slice ( fileProtocol . length ) ; } else if ( startsWith ( uri , '//' ) ) { if ( options . defaultProtocol === 'file' ) { // if is file, strip protocol and set loadFile flag uri = uri . slice ( 2 ) ; loadFile = true ; } else { // if relative protocol (starts with '//'), prepend default protocol uri = ( options . defaultProtocol || 'http' ) + ':' + uri ; } } // set non-enumerable mode flag to indicate local file load Object . defineProperty ( result , 'localFile' , { value : ! ! loadFile } ) ; // set uri result . href = uri ; // set default result target, if specified if ( options . target ) { result . target = options . target + '' ; } // return accept ( result ) ; } ) ; }
URI sanitizer function.
@param {string} uri - The uri (url or filename) to sanity check.
@param {object} options - An options hash.
@return {Promise} - A promise that resolves to an object containing
sanitized uri data, or rejects it the input uri is deemed invalid.
The properties of the resolved object are assumed to be
valid attributes for an HTML 'a' tag. The sanitized uri *must* be
provided by the 'href' property of the returned object.
URI sanitizer function .