Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
calculateAWidth(width) {
if (!width) return null
const { parsedWidth, unit } = widthParser(width)
// impossible to handle percents because it depends on padding and text width
if (unit !== 'px') return null
const { borders } = this.getBoxWidths()
const innerPaddings =
this.getShorthandAttrValue('inner-padding', 'left') +
this.getShorthandAttrValue('inner-padding', 'right')
return `${parsedWidth - innerPaddings - borders}px`
}
getChildContext() {
const { containerWidth: parentWidth } = this.context
const { nonRawSiblings, children } = this.props
const paddingSize =
this.getShorthandAttrValue('padding', 'left') +
this.getShorthandAttrValue('padding', 'right')
let containerWidth =
this.getAttribute('width') ||
`${parseFloat(parentWidth) / nonRawSiblings}px`
const { unit, parsedWidth } = widthParser(containerWidth, {
parseFloatToInt: false,
})
if (unit === '%') {
containerWidth = `${parseFloat(parentWidth) * parsedWidth / 100 -
paddingSize}px`
} else {
containerWidth = `${parsedWidth - paddingSize}px`
}
return {
...this.context,
containerWidth,
nonRawSiblings: children.length,
}
}
const getElementWidth = width => {
if (!width) {
return `${parseInt(containerWidth, 10) /
parseInt(nonRawSiblings, 10)}px`
}
const { unit, parsedWidth } = widthParser(width, {
parseFloatToInt: false,
})
if (unit === '%') {
return `${100 * parsedWidth / groupWidth}px`
}
return `${parsedWidth}${unit}`
}
getOutlookWidth() {
const { containerWidth } = this.context
const paddingSize =
this.getShorthandAttrValue('padding', 'left') +
this.getShorthandAttrValue('padding', 'right')
const width = this.getAttribute('width')
const { parsedWidth, unit } = widthParser(width)
switch (unit) {
case '%':
return `${parseInt(containerWidth, 10) *
parseInt(parsedWidth, 10) /
100 -
paddingSize}px`
case 'px':
return width
default:
return `${parseInt(containerWidth, 10) - paddingSize}px`
}
}
getStyles() {
const width = this.getContentWidth()
const fullWidth = this.getAttribute('full-width') === 'full-width'
const { parsedWidth, unit } = widthParser(width)
return {
img: {
border: this.getAttribute('border'),
'border-left': this.getAttribute('left'),
'border-right': this.getAttribute('right'),
'border-top': this.getAttribute('top'),
'border-bottom': this.getAttribute('bottom'),
'border-radius': this.getAttribute('border-radius'),
display: 'block',
outline: 'none',
'text-decoration': 'none',
height: this.getAttribute('height'),
'max-height': this.getAttribute('max-height'),
'min-width': fullWidth ? '100%' : null,
width: '100%',
getParsedWidth(toString) {
const { nonRawSiblings } = this.props
const width = this.getAttribute('width') || `${100 / nonRawSiblings}%`
const { unit, parsedWidth } = widthParser(width, {
parseFloatToInt: false,
})
if (toString) {
return `${parsedWidth}${unit}`
}
return {
unit,
parsedWidth,
}
}
getWidthAsPixel() {
const { containerWidth } = this.context
const { unit, parsedWidth } = widthParser(this.getParsedWidth(true), {
parseFloatToInt: false,
})
if (unit === '%') {
return `${parseFloat(containerWidth) * parsedWidth / 100}px`
}
return `${parsedWidth}px`
}