@@ -108,46 +108,54 @@ module.exports = ({ rules, filter } = {}) => {
108
108
109
109
return {
110
110
markup ( { content } ) {
111
- const ast = svelte . parse ( content ) ;
112
-
113
- if ( ast . html ) {
114
- let offset = 0 ;
115
-
116
- svelte . walk ( ast . html , {
117
- enter ( node ) {
118
- if ( node . type === 'Element' ) {
119
- const rulesOfTag = rules . filter ( rule => rule . tag === node . name ) ;
120
-
121
- if ( rulesOfTag . length ) {
122
- node . attributes . forEach ( attr => {
123
- const rule = rulesOfTag . find ( rule => rule . attribute === attr . name ) ;
124
-
125
- if (
126
- rule &&
127
- attr . value instanceof Array &&
128
- attr . value . length === 1 &&
129
- attr . value [ 0 ] . type === 'Text'
130
- ) {
131
- const val = attr . value [ 0 ] ;
132
-
133
- if ( rule . type === 'src' ) {
134
- if ( isLocalPath ( val . data ) ) {
135
- ( { content, offset } = replace ( content , offset , val . start , val . end , transform ( val . data ) ) ) ;
136
- }
137
- } else {
138
- ( { content, offset } = replaceSrcset ( content , offset , val . start , val . end , val . data ) ) ;
139
- }
111
+ const regex = new RegExp ( `<(${ [ ...new Set ( rules . map ( rule => rule . tag ) ) ] . join ( '|' ) } )\\s+[^>]+>` , 'gi' ) ;
112
+ const copy = content ;
113
+ let match ;
114
+ let offset = 0 ;
115
+
116
+ while ( ( match = regex . exec ( copy ) ) !== null ) {
117
+ const tag = match [ 0 ] ;
118
+ const base = match . index ;
119
+
120
+ try {
121
+ const ast = svelte . parse ( tag ) ;
122
+ const node = ast . html . children [ 0 ] ;
123
+ const rulesOfTag = rules . filter ( rule => rule . tag === node . name ) ;
124
+
125
+ if ( rulesOfTag . length ) {
126
+ node . attributes . forEach ( attr => {
127
+ const rule = rulesOfTag . find ( rule => rule . attribute === attr . name ) ;
128
+
129
+ if (
130
+ rule &&
131
+ attr . value instanceof Array &&
132
+ attr . value . length === 1 &&
133
+ attr . value [ 0 ] . type === 'Text'
134
+ ) {
135
+ const val = attr . value [ 0 ] ;
136
+
137
+ if ( rule . type === 'src' ) {
138
+ if ( isLocalPath ( val . data ) ) {
139
+ ( { content, offset } = replace (
140
+ content ,
141
+ offset ,
142
+ val . start + base ,
143
+ val . end + base ,
144
+ transform ( val . data ) )
145
+ ) ;
140
146
}
141
- } ) ;
147
+ } else {
148
+ ( { content, offset } = replaceSrcset ( content , offset , val . start + base , val . end + base , val . data ) ) ;
149
+ }
142
150
}
143
- }
151
+ } ) ;
144
152
}
145
- } ) ;
153
+ } catch ( e ) {
154
+ // nop
155
+ }
146
156
}
147
157
148
- return {
149
- code : content
150
- } ;
158
+ return { code : content } ;
151
159
}
152
160
} ;
153
161
} ;
0 commit comments