extractUrlParts method

UrlParts extractUrlParts (
  1. String url,
  2. [String baseUrl]
)

Implementation

UrlParts extractUrlParts(String url, [String baseUrl]) {
  // urlParts[1] = protocol://hostname/ OR /
  // urlParts[2] = / if path relative to host base
  // urlParts[3] = directories
  // urlParts[4] = filename
  // urlParts[5] = parameters

  final urlPartsRegex = RegExp(
      r'^((?:[a-z-]+:)?\/{2}(?:[^\/\?#]*\/)|([\/\\]))?((?:[^\/\\\?#]*[\/\\])*)([^\/\\\?#]*)([#\?].*)?$',
      caseSensitive: false);
  final urlParts = <String>[];

  Match match = urlPartsRegex.firstMatch(url);
  if (match != null) {
    for (var i = 0; i < match.groupCount; i++) {
      urlParts.add(match[i]);
    }
  }

  final returner = UrlParts();

  if (urlParts.isEmpty) {
    throw LessExceptionError(
        LessError(message: "Could not parse sheet href - '$url'"));
  }

  var directories = <String>[];
  var rawPath = '';

  // Stylesheets in IE don't always return the full path
  if (baseUrl != null && (urlParts[1] == null || urlParts[2] != null)) {
    match = urlPartsRegex.firstMatch(baseUrl);
    final baseUrlParts = <String>[];
    if (match != null) {
      for (var i = 0; i < match.groupCount; i++) {
        baseUrlParts.add(match[i]);
      }
    }

    if (baseUrlParts.isEmpty) {
      throw LessExceptionError(
          LessError(message: "Could not parse page url - '$baseUrl'"));
    }

    urlParts[1] ??= baseUrlParts[1] ?? '';
    urlParts[2] ??= '${baseUrlParts[3]}${urlParts[3]}';
  }

  if (urlParts[3] != null) {
    rawPath = urlParts[3].replaceAll('\\', '/');
    // collapse '..' and skip '.'
    directories = path_lib.split(path_lib.normalize(rawPath))..add('');
  }

  for (var i = 0; i < urlParts.length; i++) {
    if (urlParts[i] == null) urlParts[i] = '';
  }

  for (var i = urlParts.length; i < 6; i++) {
    urlParts.add('');
  }

  return returner
    ..hostPart = urlParts[1]
    ..directories = directories
    ..rawPath = '${urlParts[1]}$rawPath'
    ..path = '${urlParts[1]}${directories.join('/')}'
    ..filename = urlParts[4]
    ..fileUrl = '${returner.path}${urlParts[4]}'
    ..url = '${returner.fileUrl}${urlParts[5]}';

//3.0.0 20171009
// helper function, not part of API
//abstractFileManager.prototype.extractUrlParts = function extractUrlParts(url, baseUrl) {
//    // urlParts[1] = protocol://hostname/ OR /
//    // urlParts[2] = / if path relative to host base
//    // urlParts[3] = directories
//    // urlParts[4] = filename
//    // urlParts[5] = parameters
//
//    var urlPartsRegex = /^((?:[a-z-]+:)?\/{2}(?:[^\/\?#]*\/)|([\/\\]))?((?:[^\/\\\?#]*[\/\\])*)([^\/\\\?#]*)([#\?].*)?$/i,
//        urlParts = url.match(urlPartsRegex),
//        returner = {}, rawDirectories = [], directories = [], i, baseUrlParts;
//
//    if (!urlParts) {
//        throw new Error("Could not parse sheet href - '" + url + "'");
//    }
//
//    // Stylesheets in IE don't always return the full path
//    if (baseUrl && (!urlParts[1] || urlParts[2])) {
//        baseUrlParts = baseUrl.match(urlPartsRegex);
//        if (!baseUrlParts) {
//            throw new Error("Could not parse page url - '" + baseUrl + "'");
//        }
//        urlParts[1] = urlParts[1] || baseUrlParts[1] || "";
//        if (!urlParts[2]) {
//            urlParts[3] = baseUrlParts[3] + urlParts[3];
//        }
//    }
//
//    if (urlParts[3]) {
//        rawDirectories = urlParts[3].replace(/\\/g, "/").split("/");
//
//        // collapse '..' and skip '.'
//        for (i = 0; i < rawDirectories.length; i++) {
//
//            if (rawDirectories[i] === "..") {
//                directories.pop();
//            }
//            else if (rawDirectories[i] !== ".") {
//                directories.push(rawDirectories[i]);
//            }
//
//        }
//    }
//
//    returner.hostPart = urlParts[1];
//    returner.directories = directories;
//    returner.rawPath = (urlParts[1] || "") + rawDirectories.join("/");
//    returner.path = (urlParts[1] || "") + directories.join("/");
//    returner.filename = urlParts[4];
//    returner.fileUrl = returner.path + (urlParts[4] || "");
//    returner.url = returner.fileUrl + (urlParts[5] || "");
//    return returner;
//};
}