var isImport = require('../utils/is-import');
var isRemoteResource = require('../utils/is-remote-resource');
-var UNKNOWN_SOURCE = 'unknown-source';
+var UNKNOWN_URI = 'uri:unknown';
function readSources(input, context, callback) {
return doReadSources(input, context, function (tokens) {
function fromString(input, context, parentInlinerContext, callback) {
var inputAsHash = {};
- inputAsHash[UNKNOWN_SOURCE] = {
+ inputAsHash[UNKNOWN_URI] = {
styles: input,
sourceMap: (typeof context.options.sourceMap === 'string') ? context.options.sourceMap : null
};
function fromArray(input, context, parentInlinerContext, callback) {
var currentPath = path.resolve('');
var inputAsHash = input.reduce(function (accumulator, uri) {
- var isRemoteUri = isRemoteResource(uri);
- var absolutePath = uri[0] == '/' || isRemoteUri ?
+ var absoluteUri = isAbsoluteResource(uri) || isRemoteResource(uri) ?
uri :
path.resolve(uri);
var relativeToCurrentPath;
- if (isRemoteUri) {
+ if (isRemoteResource(uri)) {
accumulator[uri] = {
styles: restoreImport(uri, '') + ';'
};
- } else if (!fs.existsSync(absolutePath) || !fs.statSync(absolutePath).isFile()) {
- context.errors.push('Ignoring "' + absolutePath + '" as resource is missing.');
+ } else if (!fs.existsSync(absoluteUri) || !fs.statSync(absoluteUri).isFile()) {
+ context.errors.push('Ignoring "' + absoluteUri + '" as resource is missing.');
} else {
- relativeToCurrentPath = path.relative(currentPath, absolutePath);
+ relativeToCurrentPath = path.relative(currentPath, absoluteUri);
accumulator[relativeToCurrentPath] = {
- styles: fs.readFileSync(absolutePath, 'utf-8')
+ styles: fs.readFileSync(absoluteUri, 'utf-8')
};
}
function fromHash(input, context, parentInlinerContext, callback) {
var tokens = [];
var newTokens = [];
- var sourcePath;
+ var uri;
var source;
- var rebaseFrom;
- var rebaseTo;
var parsedMap;
var rebasedMap;
var rebaseConfig;
- for (sourcePath in input) {
- source = input[sourcePath];
-
- if (sourcePath !== UNKNOWN_SOURCE && isRemoteResource(sourcePath)) {
- rebaseFrom = sourcePath;
- rebaseTo = sourcePath;
- } else if (sourcePath !== UNKNOWN_SOURCE && isAbsoluteResource(sourcePath)) {
- rebaseFrom = path.dirname(sourcePath);
- rebaseTo = context.options.rebaseTo;
- } else {
- rebaseFrom = sourcePath !== UNKNOWN_SOURCE ?
- path.dirname(path.resolve(sourcePath)) :
- path.resolve('');
- rebaseTo = context.options.rebaseTo;
- }
-
- rebaseConfig = {
- fromBase: rebaseFrom,
- toBase: rebaseTo
- };
+ for (uri in input) {
+ source = input[uri];
if (source.sourceMap) {
parsedMap = JSON.parse(source.sourceMap);
- rebasedMap = isRemoteResource(sourcePath) ?
- rebaseRemoteMap(parsedMap, sourcePath) :
- rebaseLocalMap(parsedMap, sourcePath, context.options.rebaseTo);
- context.inputSourceMapTracker.track(sourcePath, rebasedMap);
+ rebasedMap = isRemoteResource(uri) ?
+ rebaseRemoteMap(parsedMap, uri) :
+ rebaseLocalMap(parsedMap, uri, context.options.rebaseTo);
+ context.inputSourceMapTracker.track(uri, rebasedMap);
}
- context.source = sourcePath !== UNKNOWN_SOURCE ? sourcePath : undefined;
+ context.source = uri !== UNKNOWN_URI ? uri : undefined;
context.sourcesContent[context.source] = source.styles;
+ rebaseConfig = {};
+
+ if (uri == UNKNOWN_URI) {
+ rebaseConfig.fromBase = path.resolve('');
+ rebaseConfig.toBase = context.options.rebaseTo;
+ } else if (isRemoteResource(uri)) {
+ rebaseConfig.fromBase = uri;
+ rebaseConfig.toBase = uri;
+ } else if (isAbsoluteResource(uri)) {
+ rebaseConfig.fromBase = path.dirname(uri);
+ rebaseConfig.toBase = context.options.rebaseTo;
+ } else {
+ rebaseConfig.fromBase = path.dirname(path.resolve(uri));
+ rebaseConfig.toBase = context.options.rebaseTo;
+ }
+
newTokens = tokenize(source.styles, context);
newTokens = rebase(newTokens, context.options.rebase, context.validator, rebaseConfig);
function inlineLocalStylesheet(uri, mediaQuery, metadata, inlinerContext) {
var currentPath = path.resolve('');
- var relativeTo = uri[0] == '/' ?
- currentPath :
- path.resolve(inlinerContext.rebaseTo || '');
- var absolutePath = uri[0] == '/' ?
- path.resolve(relativeTo, uri.substring(1)) :
- path.resolve(relativeTo, uri);
- var relativeToCurrentPath = path.relative(currentPath, absolutePath);
+ var absoluteUri = isAbsoluteResource(uri) ?
+ path.resolve(currentPath, uri.substring(1)) :
+ path.resolve(inlinerContext.rebaseTo, uri);
+ var relativeToCurrentPath = path.relative(currentPath, absoluteUri);
var importedStyles;
var importedTokens;
var isAllowed = isAllowedResource(uri, false, inlinerContext.processImportFrom);
var sourceHash = {};
- if (inlinerContext.inlinedStylesheets.indexOf(absolutePath) > -1) {
+ if (inlinerContext.inlinedStylesheets.indexOf(absoluteUri) > -1) {
inlinerContext.warnings.push('Ignoring local @import of "' + uri + '" as it has already been imported.');
- } else if (!fs.existsSync(absolutePath) || !fs.statSync(absolutePath).isFile()) {
+ } else if (!fs.existsSync(absoluteUri) || !fs.statSync(absoluteUri).isFile()) {
inlinerContext.errors.push('Ignoring local @import of "' + uri + '" as resource is missing.');
} else if (!isAllowed && inlinerContext.afterContent) {
inlinerContext.warnings.push('Ignoring local @import of "' + uri + '" as resource is not allowed and after other content.');
inlinerContext.warnings.push('Skipping local @import of "' + uri + '" as resource is not allowed.');
inlinerContext.outputTokens = inlinerContext.outputTokens.concat(inlinerContext.sourceTokens.slice(0, 1));
} else {
- importedStyles = fs.readFileSync(absolutePath, 'utf-8');
- inlinerContext.inlinedStylesheets.push(absolutePath);
+ importedStyles = fs.readFileSync(absoluteUri, 'utf-8');
+ inlinerContext.inlinedStylesheets.push(absoluteUri);
sourceHash[relativeToCurrentPath] = {
styles: importedStyles