Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MWPW-144254 [merch-card] per type heading map #2063

Merged
merged 2 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions libs/blocks/merch-card/merch-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ const TAG_PATTERN = /^[a-zA-Z0-9_-]+:[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-].*$/;

const CARD_TYPES = ['segment', 'special-offers', 'plans', 'catalog', 'product', 'inline-heading', 'image', 'mini-compare-chart'];

const TEXT_STYLES = {
H5: 'detail-m',
H4: 'body-xxs',
H3: 'heading-xs',
H2: 'heading-m',
};

const HEADING_MAP = {
'special-offers': {
H5: 'H4',
H3: 'H3',
},
};

const MINI_COMPARE_CHART = 'mini-compare-chart';

const MULTI_OFFER_CARDS = ['plans', 'product', MINI_COMPARE_CHART];
Expand All @@ -22,13 +36,6 @@ const intersectionObserver = new IntersectionObserver((entries) => {
});
});

const textStyles = {
H5: 'detail-m',
H4: 'body-xxs',
H3: 'heading-xs',
H2: 'heading-m',
};

const getPodType = (styles) => styles?.find((style) => CARD_TYPES.includes(style));

const isHeadingTag = (tagName) => /^H[2-5]$/.test(tagName);
Expand Down Expand Up @@ -70,17 +77,21 @@ const parseContent = (el, merchCard) => {
innerElements.forEach((element) => {
let { tagName } = element;
if (isHeadingTag(tagName)) {
let slotName = textStyles[tagName];
let slotName = TEXT_STYLES[tagName];
if (slotName) {
if (['H2', 'H3', 'H4', 'H5'].includes(tagName)) {
if (tagName === 'H2') {
headingMCount += 1;
}
if (headingMCount === 2 && merchCard.variant === MINI_COMPARE_CHART) {
slotName = 'heading-m-price';
if (HEADING_MAP[merchCard.variant]?.[tagName]) {
tagName = HEADING_MAP[merchCard.variant][tagName];
} else {
if (tagName === 'H2') {
headingMCount += 1;
}
if (headingMCount === 2 && merchCard.variant === MINI_COMPARE_CHART) {
slotName = 'heading-m-price';
}
tagName = `H${headingSize}`;
headingSize += 1;
}
tagName = `H${headingSize}`;
headingSize += 1;
}
element.setAttribute('slot', slotName);
const newElement = createTag(tagName);
Expand Down
8 changes: 4 additions & 4 deletions test/blocks/merch-card/merch-card.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ describe('Merch Card', () => {
it('Supports Special Offers card', async () => {
document.body.innerHTML = await readFile({ path: './mocks/special-offers.html' });
const merchCard = await init(document.querySelector('.special-offers'));
const heading = merchCard.querySelector('h3[slot="detail-m"]');
const headingOne = merchCard.querySelector('h4[slot="heading-xs"]');
const category = merchCard.querySelector('h4[slot="detail-m"]');
const title = merchCard.querySelector('h3[slot="heading-xs"]');
const body = merchCard.querySelector('div[slot="body-xs"]');
const footer = merchCard.querySelector('div[slot="footer"]');
const buttons = footer.querySelectorAll('.con-button');

expect(merchCard).to.exist;
expect(heading).to.exist;
expect(headingOne).to.exist;
expect(category).to.exist;
expect(title).to.exist;
expect(body).to.exist;
expect(merchCard.getAttribute('variant')).to.be.equal('special-offers');
expect(merchCard.getAttribute('badge-background-color')).to.be.equal('#EDCC2D');
Expand Down
Loading