@@ -2,8 +2,8 @@ import { RawSourceMap, DecodedSourceMap } from '@ampproject/remapping/dist/types
2
2
import { getLocator } from 'locate-character' ;
3
3
import { MappedCode , SourceLocation , sourcemap_add_offset , combine_sourcemaps } from '../utils/mapped_code' ;
4
4
import { decode_map } from './decode_sourcemap' ;
5
- import { replace_in_code , Source } from './replace_in_code' ;
6
- import { MarkupPreprocessor , Preprocessor , PreprocessorGroup , Processed } from './types' ;
5
+ import { replace_in_code , slice_source } from './replace_in_code' ;
6
+ import { MarkupPreprocessor , Source , Preprocessor , PreprocessorGroup , Processed } from './types' ;
7
7
8
8
interface SourceUpdate {
9
9
string ?: string ;
@@ -36,7 +36,7 @@ class PreprocessResult implements Source {
36
36
}
37
37
38
38
update_source ( { string : source , map, dependencies } : SourceUpdate ) {
39
- if ( source ) {
39
+ if ( source != null ) {
40
40
this . source = source ;
41
41
this . get_location = getLocator ( source ) ;
42
42
}
@@ -95,16 +95,18 @@ function processed_tag_to_code(
95
95
processed : Processed ,
96
96
tag_name : 'style' | 'script' ,
97
97
attributes : string ,
98
- { source, file_basename , get_location } : Source
98
+ source : Source
99
99
) : MappedCode {
100
- const build_mapped_code = ( source : string , offset : number ) =>
101
- MappedCode . from_source ( file_basename , source , get_location ( offset ) ) ;
100
+ const { file_basename, get_location } = source ;
101
+
102
+ const build_mapped_code = ( code : string , offset : number ) =>
103
+ MappedCode . from_source ( slice_source ( code , offset , source ) ) ;
102
104
103
105
const tag_open = `<${ tag_name } ${ attributes || '' } >` ;
104
106
const tag_close = `</${ tag_name } >` ;
105
107
106
108
const tag_open_code = build_mapped_code ( tag_open , 0 ) ;
107
- const tag_close_code = build_mapped_code ( tag_close , tag_open . length + source . length ) ;
109
+ const tag_close_code = build_mapped_code ( tag_close , tag_open . length + source . source . length ) ;
108
110
109
111
const content_code = processed_content_to_code ( processed , get_location ( tag_open . length ) , file_basename ) ;
110
112
@@ -117,7 +119,8 @@ function parse_tag_attributes(str: string) {
117
119
. split ( / \s + / )
118
120
. filter ( Boolean )
119
121
. reduce ( ( attrs , attr ) => {
120
- const [ key , value ] = attr . split ( '=' ) ;
122
+ const i = attr . indexOf ( '=' ) ;
123
+ const [ key , value ] = i > 0 ? [ attr . slice ( 0 , i ) , attr . slice ( i + 1 ) ] : [ attr ] ;
121
124
const [ , unquoted ] = ( value && value . match ( / ^ [ ' " ] ( .* ) [ ' " ] $ / ) ) || [ ] ;
122
125
123
126
return { ...attrs , [ key ] : unquoted ?? value ?? true } ;
@@ -132,7 +135,7 @@ async function process_tag(
132
135
preprocessor : Preprocessor ,
133
136
source : Source
134
137
) : Promise < SourceUpdate > {
135
- const { file_basename , filename, get_location } = source ;
138
+ const { filename } = source ;
136
139
const tag_regex =
137
140
tag_name === 'style'
138
141
? / < ! - - [ ^ ] * ?- - > | < s t y l e ( \s [ ^ ] * ?) ? (?: > ( [ ^ ] * ?) < \/ s t y l e > | \/ > ) / gi
@@ -146,7 +149,7 @@ async function process_tag(
146
149
content = '' ,
147
150
tag_offset : number
148
151
) : Promise < MappedCode > {
149
- const no_change = ( ) => MappedCode . from_source ( file_basename , tag_with_content , get_location ( tag_offset ) ) ;
152
+ const no_change = ( ) => MappedCode . from_source ( slice_source ( tag_with_content , tag_offset , source ) ) ;
150
153
151
154
if ( ! attributes && ! content ) return no_change ( ) ;
152
155
@@ -160,12 +163,7 @@ async function process_tag(
160
163
if ( processed . dependencies ) dependencies . push ( ...processed . dependencies ) ;
161
164
if ( ! processed . map && processed . code === content ) return no_change ( ) ;
162
165
163
- return processed_tag_to_code ( processed , tag_name , attributes , {
164
- source : content ,
165
- get_location : offset => source . get_location ( offset + tag_offset ) ,
166
- filename,
167
- file_basename
168
- } ) ;
166
+ return processed_tag_to_code ( processed , tag_name , attributes , slice_source ( content , tag_offset , source ) ) ;
169
167
}
170
168
171
169
const { string, map } = await replace_in_code ( tag_regex , process_single_tag , source ) ;
0 commit comments