From: Jakub Pawlowicz Date: Thu, 6 Nov 2014 13:51:57 +0000 (+0000) Subject: Fixes line tracking in blocks (@media, @font-face, etc). X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=8d7d2178e2398a75cc5c3d95c40c088e932d3a55;p=clean-css.git Fixes line tracking in blocks (@media, @font-face, etc). --- diff --git a/lib/selectors/tokenizer.js b/lib/selectors/tokenizer.js index 48e48d3a..d1c727cc 100644 --- a/lib/selectors/tokenizer.js +++ b/lib/selectors/tokenizer.js @@ -127,14 +127,15 @@ function tokenize(context) { context.cursor = nextEnd + 1; } else { nextEnd = chunk.indexOf('{', nextSpecial + 1); - value = chunk.substring(context.cursor, nextEnd).trim(); + value = chunk.substring(context.cursor, nextEnd); - var isFlat = flatBlock.test(value); + var trimmedValue = value.trim(); + var isFlat = flatBlock.test(trimmedValue); oldMode = context.mode; context.cursor = nextEnd + 1; context.mode = isFlat ? 'body' : 'block'; - newToken = { kind: 'block', value: value, isFlatBlock: isFlat }; + newToken = { kind: 'block', value: trimmedValue, isFlatBlock: isFlat }; if (addSourceMap) newToken.metadata = SourceMaps.saveAndTrack(value, context, true); diff --git a/lib/utils/extractors.js b/lib/utils/extractors.js index 40d1cc94..95759079 100644 --- a/lib/utils/extractors.js +++ b/lib/utils/extractors.js @@ -1,26 +1,6 @@ var Splitter = require('./splitter'); var SourceMaps = require('../utils/source-maps'); -function tokenMetadata(value, context, addExtra) { - var withoutContent; - var total; - var split = value.split('\n'); - var shift = 0; - for (withoutContent = 0, total = split.length; withoutContent < total; withoutContent++) { - var part = split[withoutContent]; - if (/\S/.test(part)) - break; - - shift += part.length + 1; - } - - context.line += withoutContent; - context.column = withoutContent > 0 ? 1 : context.column; - context.column += /^(\s)*/.exec(split[withoutContent])[0].length; - - return SourceMaps.saveAndTrack(value.substring(shift).trimLeft(), context, addExtra); -} - var Extractors = { properties: function (string, context) { var tokenized = []; @@ -58,7 +38,7 @@ var Extractors = { list.push(property); if (addSourceMap) - token.metadata = tokenMetadata(all.join(''), context, !isEscape); + token.metadata = SourceMaps.saveAndTrack(all.join(''), context, !isEscape); } buffer = []; all = []; @@ -95,7 +75,7 @@ var Extractors = { list.push(property); if (addSourceMap) - token.metadata = tokenMetadata(all.join(''), context, false); + token.metadata = SourceMaps.saveAndTrack(all.join(''), context, false); } else if (all.indexOf('\n') > -1) { SourceMaps.track(all.join('\n'), context); } @@ -121,7 +101,7 @@ var Extractors = { tokenized.push(token); if (addSourceMap) - token.metadata = tokenMetadata(selector, context, true); + token.metadata = SourceMaps.saveAndTrack(selector, context, true); } return { diff --git a/lib/utils/source-maps.js b/lib/utils/source-maps.js index 70b4cd8e..a36ccb21 100644 --- a/lib/utils/source-maps.js +++ b/lib/utils/source-maps.js @@ -1,12 +1,34 @@ +function trimLeft(value, context) { + var withoutContent; + var total; + var split = value.split('\n'); + var shift = 0; + for (withoutContent = 0, total = split.length; withoutContent < total; withoutContent++) { + var part = split[withoutContent]; + if (/\S/.test(part)) + break; + + shift += part.length + 1; + } + + context.line += withoutContent; + context.column = withoutContent > 0 ? 1 : context.column; + context.column += /^(\s)*/.exec(split[withoutContent])[0].length; + + return value.substring(shift).trimLeft(); +} + var SourceMaps = { saveAndTrack: function (data, context, hasSuffix) { + var trimmedValue = trimLeft(data, context); + var metadata = { line: context.line, column: context.column, source: context.source }; - this.track(data, context); + this.track(trimmedValue, context); if (hasSuffix) context.column++; diff --git a/test/selectors/tokenizer-source-maps-test.js b/test/selectors/tokenizer-source-maps-test.js index 84868311..e72589d9 100644 --- a/test/selectors/tokenizer-source-maps-test.js +++ b/test/selectors/tokenizer-source-maps-test.js @@ -267,12 +267,12 @@ vows.describe('source-maps/analyzer') body: [ { kind: 'selector', - value: [{ value: '\na', metadata: { line: 3, column: 1, source: undefined } }], - body: [{ value: 'color:red', metadata: { line: 4, column: 1, source: undefined } }] + value: [{ value: '\na', metadata: { line: 4, column: 1, source: undefined } }], + body: [{ value: 'color:red', metadata: { line: 5, column: 1, source: undefined } }] }, { kind: 'selector', - value: [{ value: 'p', metadata: { line: 5, column: 5, source: undefined } }], + value: [{ value: 'p', metadata: { line: 6, column: 5, source: undefined } }], body: [] } ] @@ -300,6 +300,20 @@ vows.describe('source-maps/analyzer') body: [] } ] + ], + '@font-face with breaks': [ + '\n@font-face\n{font-family: "Font"}', + [ + { + kind: 'block', + value: '@font-face', + metadata: { line: 2, column: 1, source: undefined }, + isFlatBlock: true, + body: [ + { value: 'font-family:"Font"', metadata: { line: 3, column: 2, source: undefined } }, + ] + } + ] ] }) )