@@ -53,6 +53,8 @@ function processNodes(tree, options, messages) {
53
53
54
54
const html = parseToPostHtml ( fs . readFileSync ( filePath , options . encoding ) ) ;
55
55
56
+ options . expressions . locals = { ...options . locals }
57
+
56
58
const { attributes, locals} = parseLocals ( options , node , html ) ;
57
59
58
60
options . expressions . locals = attributes ;
@@ -77,8 +79,12 @@ function processNodes(tree, options, messages) {
77
79
Object . keys ( attributes ) . forEach ( attr => {
78
80
if ( typeof locals [ attr ] === 'undefined' ) {
79
81
if ( [ 'class' ] . includes ( attr ) ) {
82
+ if ( typeof nodeAttrs [ attr ] === 'undefined' ) {
83
+ nodeAttrs [ attr ] = [ ] ;
84
+ }
80
85
nodeAttrs [ attr ] . push ( attributes [ attr ] ) ;
81
86
} else if ( [ 'style' ] . includes ( attr ) ) {
87
+ // TODO append style ?
82
88
nodeAttrs [ attr ] = attributes [ attr ] ;
83
89
}
84
90
}
@@ -150,17 +156,23 @@ function parseLocals(options, {attrs}, html) {
150
156
attributes = merge ( options . expressions . locals , attributes ) ;
151
157
152
158
// Retrieve default locals from <script defaultLocals> and merge with attributes
153
- const { locals} = scriptDataLocals ( html , { localsAttr : options . scriptLocalAttribute , removeScriptLocals : true , locals : attributes } ) ;
159
+ const { locals} = scriptDataLocals ( html , { localsAttr : options . scriptLocalAttribute , removeScriptLocals : true , locals : { ... attributes } } ) ;
154
160
155
161
// Merge default locals and attributes
156
162
// or overrides locals with attributes
157
163
if ( locals ) {
164
+ if ( mergeAttributeWithDefault . length > 0 ) {
165
+ const attributesToBeMerged = Object . fromEntries ( Object . entries ( attributes ) . filter ( ( [ attribute ] ) => mergeAttributeWithDefault . includes ( attribute ) ) ) ;
166
+ const localsToBeMerged = Object . fromEntries ( Object . entries ( locals ) . filter ( ( [ local ] ) => mergeAttributeWithDefault . includes ( local ) ) ) ;
167
+ if ( Object . keys ( localsToBeMerged ) . length > 0 ) {
168
+ mergeAttributeWithDefault . forEach ( attribute => {
169
+ attributes [ attribute ] = merge ( localsToBeMerged [ attribute ] , attributesToBeMerged [ attribute ] ) ;
170
+ } ) ;
171
+ }
172
+ }
173
+
158
174
Object . keys ( locals ) . forEach ( local => {
159
- if ( mergeAttributeWithDefault . includes ( local ) ) {
160
- attributes [ local ] = merge ( { ...locals [ local ] } , { ...attributes [ local ] } ) ;
161
- } else if ( computedAttributes . includes ( local ) ) {
162
- attributes [ local ] = locals [ local ] ;
163
- } else if ( typeof attributes [ local ] === 'undefined' ) {
175
+ if ( computedAttributes . includes ( local ) || typeof attributes [ local ] === 'undefined' ) {
164
176
attributes [ local ] = locals [ local ] ;
165
177
}
166
178
} ) ;
@@ -458,6 +470,8 @@ module.exports = (options = {}) => {
458
470
}
459
471
}
460
472
473
+ options . locals = { ...options . expressions . locals }
474
+
461
475
return function ( tree ) {
462
476
tree = processNodes ( tree , options , tree . messages ) ;
463
477
0 commit comments