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

fix textDecoration inheritance #1490

Merged
merged 1 commit into from Sep 8, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 29 additions & 2 deletions packages/layout/src/steps/resolveInheritance.js
Expand Up @@ -30,16 +30,43 @@ const getInheritStyles = R.compose(
R.propOr({}, 'style'),
);

// Merge style values
const mergeValues = (styleName, value, inheritedValue) => {
switch (styleName) {
case 'textDecoration': {
// merge not none and not false textDecoration values to one rule
return [inheritedValue, value].filter(v => v && v !== 'none').join(' ');
}
default:
return value;
}
};

// Merge inherited and node styles
const merge = (inheritedStyles, style) => {
const mergedStyles = { ...inheritedStyles };

Object.entries(style).forEach(([styleName, value]) => {
mergedStyles[styleName] = mergeValues(
styleName,
value,
inheritedStyles[styleName],
);
});

return mergedStyles;
};

/**
* Merges styles with node
*
* @param {Object} style object
* @param {Object} node
* @returns {Object} node with styles merged
*/
const mergeStyles = styles =>
const mergeStyles = inheritedStyles =>
R.evolve({
style: R.merge(styles),
style: style => merge(inheritedStyles, style),
});

/**
Expand Down
31 changes: 31 additions & 0 deletions packages/layout/tests/steps/resolveInhritance.test.js
Expand Up @@ -80,6 +80,37 @@ describe('layout resolveInheritance', () => {
expect(subview.style).toHaveProperty('color', 'green');
});

test('Should inherit multiple textDecoration properly', () => {
const root = {
type: 'DOCUMENT',
children: [
{
type: 'PAGE',
style: {},
children: [
{
type: 'TEXT',
style: { textDecoration: 'line-through' },
children: [
{ type: 'TEXT', style: { textDecoration: 'underline' } },
],
},
],
},
],
};

const result = resolveInheritance(root);
const text1 = result.children[0].children[0];
const text2 = text1.children[0];

expect(text1.style).toHaveProperty('textDecoration', 'line-through');
expect(text2.style).toHaveProperty(
'textDecoration',
'line-through underline',
);
});

test('Should inherit color value', shouldInherit('color'));
test('Should inherit fontFamily value', shouldInherit('fontFamily'));
test('Should inherit fontSize value', shouldInherit('fontSize'));
Expand Down