@taiga-ui/cdk
Version:
Base library for creating Angular components and applications using Taiga UI principles regarding of actual visual appearance
46 lines • 2.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getControlStateAttrs = getControlStateAttrs;
exports.removeControlStateAttrs = removeControlStateAttrs;
exports.stringifyControlStateAttrs = stringifyControlStateAttrs;
const CONTROL_STATE_ATTR_NAMES_LOWER = new Set([
'[disabled]'.toLowerCase(),
'[readOnly]'.toLowerCase(),
'disabled'.toLowerCase(),
'readOnly'.toLowerCase(),
]);
const CANONICAL_NAME_BY_LOWER = new Map([
['[disabled]'.toLowerCase(), '[disabled]'],
['[readOnly]'.toLowerCase(), '[readOnly]'],
['disabled'.toLowerCase(), 'disabled'],
['readOnly'.toLowerCase(), 'readOnly'],
]);
function getControlStateAttrs(element) {
return element.attrs.filter((attr) => CONTROL_STATE_ATTR_NAMES_LOWER.has(attr.name.toLowerCase()));
}
function removeControlStateAttrs(recorder, templateOffset, element, template, attrs = getControlStateAttrs(element)) {
var _a, _b, _c;
for (const attr of attrs) {
const { startOffset = 0, endOffset = 0 } = (_c = (_b = (_a = element.sourceCodeLocation) === null || _a === void 0 ? void 0 : _a.attrs) === null || _b === void 0 ? void 0 : _b[attr.name]) !== null && _c !== void 0 ? _c : {};
const lineStart = template.lastIndexOf('\n', startOffset) + 1;
const lineEnd = template.indexOf('\n', endOffset);
const attrText = template.slice(startOffset, endOffset);
const lineText = lineEnd === -1 ? '' : template.slice(lineStart, lineEnd);
if (lineText.trim() === attrText.trim() && lineEnd !== -1) {
recorder.remove(templateOffset + lineStart, lineEnd - lineStart + 1);
continue;
}
recorder.remove(templateOffset + startOffset - 1, endOffset - startOffset + 1);
}
}
function stringifyControlStateAttrs(attrs) {
return attrs
.map(({ name, value }) => {
var _a;
const normalized = (_a = CANONICAL_NAME_BY_LOWER.get(name.toLowerCase())) !== null && _a !== void 0 ? _a : name;
const token = value ? `${normalized}="${value}"` : normalized;
return ` ${token}`;
})
.join('');
}
//# sourceMappingURL=control-state-attrs.js.map