Skip to content

Commit da41fdb

Browse files
Fix merge attributes
1 parent 6d4b3dc commit da41fdb

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/index.js

+20-6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ function processNodes(tree, options, messages) {
5353

5454
const html = parseToPostHtml(fs.readFileSync(filePath, options.encoding));
5555

56+
options.expressions.locals = {...options.locals}
57+
5658
const {attributes, locals} = parseLocals(options, node, html);
5759

5860
options.expressions.locals = attributes;
@@ -77,8 +79,12 @@ function processNodes(tree, options, messages) {
7779
Object.keys(attributes).forEach(attr => {
7880
if (typeof locals[attr] === 'undefined') {
7981
if (['class'].includes(attr)) {
82+
if (typeof nodeAttrs[attr] === 'undefined') {
83+
nodeAttrs[attr] = [];
84+
}
8085
nodeAttrs[attr].push(attributes[attr]);
8186
} else if (['style'].includes(attr)) {
87+
// TODO append style ?
8288
nodeAttrs[attr] = attributes[attr];
8389
}
8490
}
@@ -150,17 +156,23 @@ function parseLocals(options, {attrs}, html) {
150156
attributes = merge(options.expressions.locals, attributes);
151157

152158
// 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}});
154160

155161
// Merge default locals and attributes
156162
// or overrides locals with attributes
157163
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+
158174
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') {
164176
attributes[local] = locals[local];
165177
}
166178
});
@@ -458,6 +470,8 @@ module.exports = (options = {}) => {
458470
}
459471
}
460472

473+
options.locals = {...options.expressions.locals}
474+
461475
return function (tree) {
462476
tree = processNodes(tree, options, tree.messages);
463477

0 commit comments

Comments
 (0)