@markuplint/ml-spec
Version:
Types and schema that specs of the Markup languages for markuplint
24 lines (23 loc) • 903 B
JavaScript
import { getComputedRole } from './get-computed-role.js';
export function matchesContextRole(conditions,
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
ownedEl, specs, version) {
return conditions.some(condition => matchesCondition(condition, ownedEl.parentElement, specs, version));
}
function matchesCondition(condition,
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
parentEl, specs, version) {
const conditions = condition.split(/\s+>\s+/).reverse();
while (conditions.length > 0) {
if (!parentEl) {
return false;
}
const condition = conditions.shift();
const parentRole = getComputedRole(specs, parentEl, version, true).role;
if (condition !== parentRole?.name) {
return false;
}
parentEl = parentEl.parentElement;
}
return true;
}