Skip to content

Commit

Permalink
Merge pull request #54 from razroo/ZETA-6939-optional-classname-add-p…
Browse files Browse the repository at this point in the history
…roperty-html

Zeta 6939 optional classname for add property html
  • Loading branch information
CharlieGreenman authored Oct 20, 2023
2 parents 7e3ef5f + edb5654 commit e1ae714
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,34 @@ describe('addPropertyToHtmlTag', () => {
</devgen-eureka-seven-global-header>
<devgen-eureka-seven-global-header> </devgen-eureka-seven-global-header>
</div>
`;
expect(result).toEqual(expected);
});

it('should add a property to an html tag and use the class name to determine which element', () => {
const fileToBeAddedTo = `<div></div>
<div class="Toolbar__Icons"></div>
`;
const codeBlock = '{"*ngIf": "authenticated"}';

const insertIntoHtmlTag: EditHtmlFile = {
nodeType: 'addPropertyToHtmlTag',
codeBlock: codeBlock,
tagNameToInsertInto: 'div',
className: 'Toolbar__Icons'
};

const editInput = {
fileType: 'html',
fileToBeAddedTo: fileToBeAddedTo,
edits: [
insertIntoHtmlTag
]
};

const result = morphCode(editInput);
const expected = `<div></div>
<div class="Toolbar__Icons" *ngIf="authenticated"></div>
`;
expect(result).toEqual(expected);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ export function addPropertyToHtmlTag(editFile: EditHtmlFile, fileToBeModified: a
let firstMatchFound = false;
visit(fileToBeModified, { type: 'element', tagName: editFile.tagNameToInsertInto }, (node: any, index) => {
// Check if the tag is the one you want to modify
if (!firstMatchFound && node.tagName === editFile.tagNameToInsertInto) {
node.properties = {
...(node.properties || {}),
...codeBlock
};
firstMatchFound = true;
if(!editFile.className || editFile.className === '' || node.properties.className?.includes(editFile.className)) {
if (!firstMatchFound && node.tagName === editFile.tagNameToInsertInto) {
node.properties = {
...(node.properties || {}),
...codeBlock
};
firstMatchFound = true;
}
}
});

Expand Down

0 comments on commit e1ae714

Please sign in to comment.