Skip to content

Commit

Permalink
Merge pull request #216 from elbwalker/213-support-generic-entity-pro…
Browse files Browse the repository at this point in the history
…perties

213 support generic entity properties
  • Loading branch information
alexanderkirtzel committed Jun 2, 2023
2 parents 901bbb9 + fced79d commit 477457d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
9 changes: 9 additions & 0 deletions src/__tests__/html/walker.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,12 @@
<p data-elb-page="ignore:this"></p>
</div>
</div>

<div data-elb-*="p:v">
<div id="generic" data-elbaction="click" data-elb="generic">
<p data-elb-generic="k:v"></p>
<p data-elb-*="g:v"></p>
<p data-elb-generic="o:v"></p>
<p data-elb-*="o:x"></p>
</div>
</div>
9 changes: 9 additions & 0 deletions src/__tests__/walker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,15 @@ describe('Walker', () => {
],
);
});

test('Generic properties', () => {
expect(getEvents(getElem('generic'), Walker.Trigger.Click)).toMatchObject([
{
entity: 'generic',
data: { p: 'v', k: 'v', g: 'v', o: 'v' },
},
]);
});
});

function getElem(selector: string) {
Expand Down
22 changes: 17 additions & 5 deletions src/lib/walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export function getEvents(
// Use page as default entity if no one was set
if (!entities.length) {
const type = 'page';
// Only use explicit page properties and ignore generic properties
const entitySelector = `[${getElbAttributeName(prefix, type)}]`;

// Get matching properties from the element and its parents
Expand Down Expand Up @@ -202,22 +203,31 @@ function getEntity(

if (!type) return null; // It's not a (valid) entity element

const entitySelector = `[${getElbAttributeName(prefix, type)}]`;
const entitySelector = `[${getElbAttributeName(
prefix,
type,
)}],[${getElbAttributeName(prefix, '\\*')}]`;

// Get matching properties from the element and its parents
let [data, context] = getThisAndParentProperties(
let [parentProps, context] = getThisAndParentProperties(
origin || element,
entitySelector,
prefix,
type,
);

// Get properties
let data: Walker.Properties = {};
let genericProps: Walker.Properties = {};
element.querySelectorAll<HTMLElement>(entitySelector).forEach((child) => {
// Eventually override closer peroperties
genericProps = assign(genericProps, getElbValues(prefix, child, '*'));
data = assign(data, getElbValues(prefix, child, type));
});

// Merge properties with the hirarchy data > generic > parent
data = assign(parentProps, assign(genericProps, data));

// Get nested entities
const nested: Walker.Entities = [];
element
Expand Down Expand Up @@ -248,9 +258,11 @@ function getThisAndParentProperties(
// Get all bubbling-up properties with decreasing priority
let contextI = 0; // Context counter
while (parent) {
if (parent.matches(entitySelector))
// Get higher properties first
data = assign(getElbValues(prefix, parent, type), data);
// Get higher properties first
if (parent.matches(entitySelector)) {
data = assign(getElbValues(prefix, parent, '*'), data); // Generic
data = assign(getElbValues(prefix, parent, type), data); // Explicit
}

if (parent.matches(contextSelector)) {
Object.entries(
Expand Down

0 comments on commit 477457d

Please sign in to comment.