Skip to content

Commit 17b33e2

Browse files
ikhsanalatsaryExilz
authored andcommitted
fix(): Font size contains absolute size.
e.g: medium|xx-small|x-small|small|large|x-large|xx-large|smaller|larger|length|initial|inherit solve issue meliorence#122
1 parent 14d3ab6 commit 17b33e2

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

src/HTML.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export default class HTML extends PureComponent {
3535
height: PropTypes.number
3636
}),
3737
emSize: PropTypes.number.isRequired,
38+
ptSize: PropTypes.number.isRequired,
3839
baseFontStyle: PropTypes.object.isRequired,
3940
textSelectable: PropTypes.bool
4041
}
@@ -44,6 +45,7 @@ export default class HTML extends PureComponent {
4445
debug: false,
4546
decodeEntities: true,
4647
emSize: 14,
48+
ptSize: 1.3,
4749
ignoredTags: IGNORED_TAGS,
4850
ignoredStyles: [],
4951
baseFontStyle: { fontSize: 14 },
@@ -395,7 +397,7 @@ export default class HTML extends PureComponent {
395397
* @memberof HTML
396398
*/
397399
renderRNElements (RNElements, parentWrapper = 'root', parentIndex = 0, props = this.props) {
398-
const { tagsStyles, classesStyles, emSize, ignoredStyles, allowedStyles } = props;
400+
const { tagsStyles, classesStyles, emSize, ptSize, ignoredStyles, allowedStyles } = props;
399401
return RNElements && RNElements.length ? RNElements.map((element, index) => {
400402
const { attribs, data, tagName, parentTag, children, nodeIndex, wrapper } = element;
401403
const Wrapper = wrapper === 'Text' ? Text : View;
@@ -405,7 +407,7 @@ export default class HTML extends PureComponent {
405407
cssStringToRNStyle(
406408
attribs.style,
407409
Wrapper === Text ? STYLESETS.TEXT : STYLESETS.VIEW, // proper prop-types validation
408-
{ parentTag: tagName, emSize, ignoredStyles, allowedStyles }
410+
{ parentTag: tagName, emSize, ptSize, ignoredStyles, allowedStyles }
409411
) :
410412
{};
411413

src/HTMLStyles.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PERC_SUPPORTED_STYLES, STYLESETS, stylePropTypes } from './HTMLUtils';
1+
import { PERC_SUPPORTED_STYLES, STYLESETS, ABSOLUTE_FONT_SIZE, stylePropTypes } from './HTMLUtils';
22
import { generateDefaultBlockStyles, generateDefaultTextStyles } from './HTMLDefaultStyles';
33
import checkPropTypes from './checkPropTypes';
44

@@ -101,7 +101,7 @@ export function _getElementCSSClasses (htmlAttribs) {
101101
* @param {object} { parentTag, emSize, ignoredStyles }
102102
* @returns {object}
103103
*/
104-
function cssToRNStyle (css, styleset, { parentTag, emSize, ignoredStyles, allowedStyles }) {
104+
function cssToRNStyle (css, styleset, { parentTag, emSize, ptSize, ignoredStyles, allowedStyles }) {
105105
const styleProps = stylePropTypes[styleset];
106106
return Object.keys(css)
107107
.filter((key) => allowedStyles ? allowedStyles.indexOf(key) !== -1 : true)
@@ -138,6 +138,13 @@ function cssToRNStyle (css, styleset, { parentTag, emSize, ignoredStyles, allowe
138138
const pxSize = parseFloat(value.replace('em', '')) * emSize;
139139
return [key, pxSize];
140140
}
141+
if (value.search('pt') !== -1) {
142+
const pxSize = parseFloat(value.replace('pt', '')) * ptSize;
143+
return [key, pxSize];
144+
}
145+
if (key === 'fontSize') {
146+
return mapAbsoluteFontSize(key, value);
147+
}
141148
// See if we can convert a 20px to a 20 automagically
142149
const numericValue = parseFloat(value.replace('px', ''));
143150
if (!isNaN(numericValue)) {
@@ -158,6 +165,19 @@ function cssToRNStyle (css, styleset, { parentTag, emSize, ignoredStyles, allowe
158165
}, {});
159166
}
160167

168+
/**
169+
* @param {string} key: the key of style
170+
* @param {string} value: the value of style
171+
* @return {array}
172+
*/
173+
function mapAbsoluteFontSize (key, value) {
174+
let fontSize = value;
175+
if (ABSOLUTE_FONT_SIZE.hasOwnProperty(value)) {
176+
fontSize = ABSOLUTE_FONT_SIZE[value];
177+
}
178+
return [key, fontSize];
179+
}
180+
161181
/**
162182
* @param str: the css style string
163183
* @param styleset=STYLESETS.TEXT: the styleset to convert the styles against

src/HTMLUtils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ export const MIXED_TAGS = ['a'];
2626
// These text tags shouldn't be associated with their siblings in the associateRawTags method
2727
export const TEXT_TAGS_IGNORING_ASSOCIATION = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
2828

29+
export const ABSOLUTE_FONT_SIZE = {
30+
'medium': 14,
31+
'xx-small': 8.5,
32+
'x-small': 10,
33+
'small': 12,
34+
'large': 17,
35+
'x-large': 20,
36+
'xx-large': 24,
37+
'smaller': 13.3,
38+
'larger': 16,
39+
'length': null,
40+
'initial': null,
41+
'inherit': null
42+
};
43+
2944
export const IGNORED_TAGS = ['head', 'scripts', 'audio', 'video', 'track', 'embed', 'object', 'param', 'source', 'canvas', 'noscript',
3045
'caption', 'col', 'colgroup', 'table', 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr', 'button', 'datalist', 'fieldset', 'form',
3146
'input', 'label', 'legend', 'meter', 'optgroup', 'option', 'output', 'progress', 'select', 'textarea', 'details', 'diaglog',

0 commit comments

Comments
 (0)