Skip to content

Commit

Permalink
[#1641] add parent option to createTag (#1698)
Browse files Browse the repository at this point in the history
- add options parameter to createTag signature, with only one parent option for now, as discussed in https://github.com/orgs/adobecom/discussions/1641,
- add unit test for create Tag
  • Loading branch information
npeltier authored Jan 5, 2024
1 parent 89c5bd7 commit 38723f9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion libs/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export function isInTextNode(node) {
return node.parentElement.firstChild.nodeType === Node.TEXT_NODE;
}

export function createTag(tag, attributes, html) {
export function createTag(tag, attributes, html, options = {}) {
const el = document.createElement(tag);
if (html) {
if (html instanceof HTMLElement
Expand All @@ -257,6 +257,7 @@ export function createTag(tag, attributes, html) {
el.setAttribute(key, val);
});
}
options.parent?.append(el);
return el;
}

Expand Down
15 changes: 15 additions & 0 deletions test/utils/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { expect } from '@esm-bundle/chai';
import sinon from 'sinon';
import { waitFor, waitForElement } from '../helpers/waitfor.js';
import { mockFetch } from '../helpers/generalHelpers.js';
import { createTag } from '../../libs/utils/utils.js';

const utils = {};

Expand Down Expand Up @@ -572,6 +573,20 @@ describe('Utils', () => {
});
});

describe('createTag', async () => {
/**
* create tag creates a tag from first parameter tag name,
* second parameter is requested attributes map in created tag,
* third parameter is the innerHTML of the tag, can be either node or text,
* fourth parameter is an object of creation options:
* - @parent parent element to append the tag to.
*/
createTag('var', { class: 'foo' }, 'bar', { parent: document.body });
const varTag = document.querySelector('body > var.foo');
expect(varTag).to.exist;
expect(varTag.textContent).to.equal('bar');
});

describe('personalization', async () => {
const MANIFEST_JSON = {
info: { total: 2, offset: 0, limit: 2, data: [{ key: 'manifest-type', value: 'Personalization' }, { key: 'manifest-override-name', value: '' }, { key: 'name', value: '1' }] }, placeholders: { total: 0, offset: 0, limit: 0, data: [] }, experiences: { total: 1, offset: 0, limit: 1, data: [{ action: 'insertContentAfter', selector: '.marquee', 'page filter (optional)': '/products/special-offers', chrome: 'https://main--milo--adobecom.hlx.page/drafts/mariia/fragments/personalizationtext' }] }, ':version': 3, ':names': ['info', 'placeholders', 'experiences'], ':type': 'multi-sheet',
Expand Down

0 comments on commit 38723f9

Please sign in to comment.