| 8 | * @param {String} selector |
| 9 | */ |
| 10 | export 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 | } |