@taiga-ui/cdk
Version:
Base library for creating Angular components and applications using Taiga UI principles regarding of actual visual appearance
94 lines • 6.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.migrateInputPassword = migrateInputPassword;
const insert_todo_1 = require("../../../../utils/insert-todo");
const elements_1 = require("../../../../utils/templates/elements");
const template_resource_1 = require("../../../../utils/templates/template-resource");
const remove_attr_1 = require("../../../utils/templates/remove-attr");
const replace_tag_1 = require("../../../utils/templates/replace-tag");
const migrate_hint_on_legacy_controls_1 = require("./migrate-hint-on-legacy-controls");
function migrateInputPassword({ resource, recorder, fileSystem, }) {
const template = (0, template_resource_1.getTemplateFromTemplateResource)(resource, fileSystem);
const templateOffset = (0, template_resource_1.getTemplateOffset)(resource);
const elements = (0, elements_1.findElementsByTagName)(template, 'tui-input-password');
elements.forEach((element) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
const sourceCodeLocation = element.sourceCodeLocation;
const tooltipIconStr = (0, migrate_hint_on_legacy_controls_1.extractHintToTooltipIcon)({
element,
template,
recorder,
templateOffset,
resource,
});
(0, replace_tag_1.replaceTag)(recorder, sourceCodeLocation, 'tui-input-password', 'tui-textfield', template, templateOffset);
const attrs = [...element.attrs].filter((attr) => /formcontrol|ngmodel/.exec(attr.name.toLocaleLowerCase()));
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 : {};
recorder.remove(templateOffset + startOffset, endOffset - startOffset);
}
const { value: labelOutsideValue, isBinding: labelOutsideIsBinding } = (0, remove_attr_1.removeAttr)(element, 'tuiTextfieldLabelOutside', recorder, templateOffset);
const isLabelOutsideTrue = labelOutsideValue === 'true' ||
(!labelOutsideIsBinding && labelOutsideValue === '');
const isDynamic = labelOutsideValue !== null &&
labelOutsideValue !== 'true' &&
labelOutsideValue !== 'false' &&
labelOutsideValue !== '';
// --- Handle label text ---
const labelIndex = element.childNodes.findIndex((node) => node.nodeName === '#text' && (node === null || node === void 0 ? void 0 : node.value.trim()));
let placeholderAttr = '';
if (labelIndex !== -1) {
const labelNode = element.childNodes[labelIndex];
const labelText = labelNode.value.trim();
const labelTextStart = ((_e = (_d = labelNode === null || labelNode === void 0 ? void 0 : labelNode.sourceCodeLocation) === null || _d === void 0 ? void 0 : _d.startOffset) !== null && _e !== void 0 ? _e : 0) + templateOffset;
const labelTextEnd = ((_g = (_f = labelNode === null || labelNode === void 0 ? void 0 : labelNode.sourceCodeLocation) === null || _f === void 0 ? void 0 : _f.endOffset) !== null && _g !== void 0 ? _g : 0) + templateOffset;
if (isLabelOutsideTrue) {
recorder.remove(labelTextStart, labelTextEnd - labelTextStart);
placeholderAttr = ` placeholder="${labelText}"`;
}
else if (!isDynamic) {
recorder.insertRight(labelTextStart, '\n<label tuiLabel>');
recorder.insertRight(labelTextEnd, '</label>\n');
}
}
// --- Build TODO comment ---
const todoNotes = [];
if (isDynamic) {
todoNotes.push(`[tuiTextfieldLabelOutside]="${labelOutsideValue}" is dynamic and cannot be migrated automatically. Use <label tuiLabel> inside <tui-textfield> for floating label or outside for static label.`);
}
if (todoNotes.length > 0) {
const startOffset = ((_h = sourceCodeLocation === null || sourceCodeLocation === void 0 ? void 0 : sourceCodeLocation.startOffset) !== null && _h !== void 0 ? _h : 0) + templateOffset;
const todoComment = `<!-- ${insert_todo_1.TODO_MARK} tui-input-password migration:\n${todoNotes.map((n) => ` - ${n}`).join('\n')}\n-->\n`;
recorder.insertLeft(startOffset, todoComment);
}
// --- Insert <input> and <tui-icon tuiPassword /> ---
const insertOffset = ((_k = (_j = sourceCodeLocation === null || sourceCodeLocation === void 0 ? void 0 : sourceCodeLocation.endTag) === null || _j === void 0 ? void 0 : _j.startOffset) !== null && _k !== void 0 ? _k : 0) + templateOffset;
const inputs = element.childNodes.filter((node) => node.nodeName === 'input');
const migrationAttrs = attrs.reduce((result, attr) => {
const name = attr.name
.replace(/ngmodel/i, 'ngModel')
.replace(/formcontrol/i, 'formControl')
.replace(/formcontrolname/i, 'formControlName');
return `${result} ${name}="${attr.value}"`;
}, '');
const tooltipLine = tooltipIconStr ? `${tooltipIconStr}\n` : '';
if (!inputs.length) {
recorder.insertRight(insertOffset, `\n<input tuiInput type="password"${migrationAttrs}${placeholderAttr} />\n${tooltipLine}<tui-icon tuiPassword />\n`);
}
for (const input of inputs) {
const hasTypePassword = input.attrs.some((attr) => attr.name === 'type' && attr.value === 'password');
const typeAttr = hasTypePassword ? '' : ' type="password"';
input.attrs.forEach((attr) => {
var _a, _b, _c, _d, _e;
if (/^tuiTextfield$|^tuiTextfieldLegacy$/i.exec(attr.name)) {
const { startOffset = 0, endOffset = 0 } = (_c = (_b = (_a = input.sourceCodeLocation) === null || _a === void 0 ? void 0 : _a.attrs) === null || _b === void 0 ? void 0 : _b[attr.name]) !== null && _c !== void 0 ? _c : {};
recorder.remove(templateOffset + startOffset, endOffset - startOffset);
recorder.insertRight(templateOffset + startOffset, `tuiInput${typeAttr}${migrationAttrs}${placeholderAttr}`);
const inputEndOffset = ((_e = (_d = input.sourceCodeLocation) === null || _d === void 0 ? void 0 : _d.endOffset) !== null && _e !== void 0 ? _e : 0) + templateOffset;
recorder.insertRight(inputEndOffset, `\n${tooltipLine}<tui-icon tuiPassword />\n`);
}
});
}
});
}
//# sourceMappingURL=migrate-input-password.js.map