88 The above copyright notice and this permission notice shall be
99 included in all copies or substantial portions of this Source Code Form.
1010*/
11+ const assert = require ( 'assert' ) . strict ;
12+
1113const Input = require ( 'postcss/lib/input' ) ;
1214
1315const Parser = require ( './ValuesParser' ) ;
16+ const SubInput = require ( './SubInput' ) ;
1417const { stringify } = require ( './ValuesStringifier' ) ;
1518
19+ const NEWLINE = '\n' . charCodeAt ( 0 ) ;
20+ const FEED = '\f' . charCodeAt ( 0 ) ;
21+ const CR = '\r' . charCodeAt ( 0 ) ;
22+
23+ function positionAfter ( node , chunks ) {
24+ let { line } = node . source . start ;
25+ let { column } = node . source . start ;
26+ for ( const chunk of chunks ) {
27+ for ( let i = 0 ; i < chunk . length ; i ++ ) {
28+ const code = chunk . charCodeAt ( i ) ;
29+ if (
30+ code === NEWLINE ||
31+ code === FEED ||
32+ ( code === CR && chunk . charCodeAt ( i + 1 ) !== NEWLINE )
33+ ) {
34+ column = 1 ;
35+ line += 1 ;
36+ } else {
37+ column += 1 ;
38+ }
39+ }
40+ }
41+
42+ return { line, column } ;
43+ }
44+
1645module . exports = {
1746 parse ( css , options ) {
18- const input = new Input ( css , options ) ;
47+ let input ;
48+ if ( options . context ) {
49+ assert ( options . lineInContext ) ;
50+ assert ( options . columnInContext ) ;
51+ input = new SubInput ( css , options . context , options . lineInContext , options . columnInContext ) ;
52+ } else {
53+ input = new Input ( css , options ) ;
54+ }
55+
1956 const parser = new Parser ( input , options ) ;
2057
2158 parser . parse ( ) ;
@@ -32,6 +69,24 @@ module.exports = {
3269 return parser . root ;
3370 } ,
3471
72+ parseDeclValue ( decl ) {
73+ const { line, column } = positionAfter ( decl , [ decl . prop , decl . raws . between ] ) ;
74+ return module . exports . parse ( decl . value , {
75+ context : decl . source . input ,
76+ lineInContext : line ,
77+ columnInContext : column
78+ } ) ;
79+ } ,
80+
81+ parseAtRuleParams ( rule ) {
82+ const { line, column } = positionAfter ( rule , [ '@' , rule . name , rule . raws . afterName ] ) ;
83+ return module . exports . parse ( rule . value , {
84+ context : rule ,
85+ lineInContext : line ,
86+ columnInContext : column
87+ } ) ;
88+ } ,
89+
3590 stringify,
3691
3792 nodeToString ( node ) {
0 commit comments