MCPcopy
hub / github.com/SparkPost/heml / fixWidthsFor

Function fixWidthsFor

packages/heml-inline/src/fixWidthsFor.js:10–35  ·  view source on GitHub ↗
($, selector)

Source from the content-addressed store, hash-verified

8 * @param {String} selector
9 */
10export default function fixWidthsFor ($, selector) {
11 // get all relative widths and set them to fixed values by default
12 $(`${selector}`).filter(`[width*="%"]`).toNodes().forEach(($node) => {
13 const nodeWidth = $node.attr('width')
14 /**
15 * Gather all the parent percents and multiply them against
16 * the image and fixed parent width
17 */
18 let parentPercent = 1
19
20 for (let $el of $node.parents().toNodes()) {
21 const parentWidth = $el.attr('width') || getProp($el.attr('style'), 'width')
22
23 if (parentWidth && !parentWidth.endsWith('%')) {
24 const currentStyles = $node.attr('style')
25
26 $node.attr('style', setProp(currentStyles, 'width', nodeWidth))
27 $node.attr('width', parseFloat(parentWidth, 10) * parentPercent * parseFloat(nodeWidth, 10) / 100)
28
29 break
30 } else if (parentWidth && parentWidth.endsWith('%')) {
31 parentPercent = parentPercent * parseFloat(parentWidth, 10) / 100
32 }
33 }
34 })
35}

Callers 1

inlineFunction · 0.85

Calls 2

getPropFunction · 0.90
setPropFunction · 0.90

Tested by

no test coverage detected