From aa15289a9370c458951a1f3815212552a77c86a3 Mon Sep 17 00:00:00 2001 From: Vincent De Oliveira Date: Thu, 12 Feb 2015 19:11:05 +0100 Subject: [PATCH] Properly clone nodes in `ignoredAtRules` (fix #21 when `parent` is undefined) --- index.js | 8 ++++++-- test/index.js | 11 +++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index ef8bba37..f2b1f83b 100755 --- a/index.js +++ b/index.js @@ -109,7 +109,7 @@ function addIgnoredAtRulesOnTop(styles, ignoredAtRules) { styles.nodes.unshift(ignoredAtRule) } - if (first) { + if (first && first.before !== undefined) { first.before = "\n\n" + first.before } } @@ -126,13 +126,17 @@ function readAtImport(atRule, options, cb, importedFiles, ignoredAtRules, media) // @todo extract what can be interesting from this one var parsedAtImport = parseImport(atRule.params, atRule.source) + + // adjust media according to current scope media = parsedAtImport.media ? (media ? media + " and " : "") + parsedAtImport.media : (media ? media : null) // just update protocol base uri (protocol://url) or protocol-relative (//url) if media needed if (parsedAtImport.uri.match(/^(?:[a-z]+:)?\/\//i)) { parsedAtImport.media = media - ignoredAtRules.push([atRule, parsedAtImport]) + var atRuleCloned = atRule.clone() + atRuleCloned.parent = atRule.parent.clone() + ignoredAtRules.push([atRuleCloned, parsedAtImport]) atRule.removeSelf() return } diff --git a/test/index.js b/test/index.js index 2d89d5bb..b74c3085 100755 --- a/test/index.js +++ b/test/index.js @@ -63,9 +63,20 @@ test("@import", function(t) { "should not fail with only one absolute import" ) + base = "@import url('http://');\n@import 'test/fixtures/imports/foo.css';"; + t.equal( + postcss() + .use(atImport()) + .process(base) + .css.trim(), + "@import url('http://');\nfoo {}", + "should not fail with absolute and local import" + ) + t.end() }) + test("@import error output", function(t) { var file = importsDir + "/import-missing.css" t.throws(