html-validate
Version:
Offline HTML5 validator and linter
62 lines (57 loc) • 1.85 kB
JavaScript
;
var utils_naturalJoin = require('./utils/natural-join.js');
function defineMetadata(metatable) {
return metatable;
}
function allowedIfAttributeIsPresent(...attr) {
return (node) => {
if (attr.some((it) => node.hasAttribute(it))) {
return null;
}
const expected = utils_naturalJoin.naturalJoin(attr.map((it) => `"${it}"`));
return `requires ${expected} attribute to be present`;
};
}
function allowedIfAttributeIsAbsent(...attr) {
return (node) => {
const present = attr.filter((it) => node.hasAttribute(it));
if (present.length === 0) {
return null;
}
const expected = utils_naturalJoin.naturalJoin(present.map((it) => `"${it}"`));
return `cannot be used at the same time as ${expected}`;
};
}
function allowedIfAttributeHasValue(key, expectedValue, { defaultValue } = {}) {
return (node) => {
const attr = node.getAttribute(key);
if (attr && typeof attr !== "string") {
return null;
}
const actualValue = attr ?? defaultValue;
if (actualValue && expectedValue.includes(actualValue.toLocaleLowerCase())) {
return null;
}
const expected = utils_naturalJoin.naturalJoin(expectedValue.map((it) => `"${it}"`));
return `"${key}" attribute must be ${expected}`;
};
}
function allowedIfParentIsPresent(...tags) {
return (node) => {
const match = tags.some((it) => node.closest(it));
if (match) {
return null;
}
const expected = utils_naturalJoin.naturalJoin(tags.map((it) => `<${it}>`));
return `requires ${expected} as parent`;
};
}
const metadataHelper = {
allowedIfAttributeIsPresent,
allowedIfAttributeIsAbsent,
allowedIfAttributeHasValue,
allowedIfParentIsPresent
};
exports.defineMetadata = defineMetadata;
exports.metadataHelper = metadataHelper;
//# sourceMappingURL=meta-helper.js.map