|
| 1 | +/** |
| 2 | +* The MIT License (MIT) |
| 3 | +* |
| 4 | +* Copyright (c) 2016 Formidable |
| 5 | +* |
| 6 | +* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +* of this software and associated documentation files (the "Software"), to deal |
| 8 | +* in the Software without restriction, including without limitation the rights |
| 9 | +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +* copies of the Software, and to permit persons to whom the Software is |
| 11 | +* furnished to do so, subject to the following conditions: |
| 12 | +* |
| 13 | +* The above copyright notice and this permission notice shall be included in all |
| 14 | +* copies or substantial portions of the Software. |
| 15 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +* SOFTWARE. |
| 22 | +*/ |
| 23 | + |
| 24 | +/** |
| 25 | + * Absorb source code for `measure-text` to resolve security vulnerability until |
| 26 | + * a new version of `measure-text` is released. |
| 27 | + * |
| 28 | + * @see https://github.com/matt-d-rat/react-middle-truncate/issues/4 |
| 29 | + **/ |
| 30 | + |
| 31 | +// eslint-env browser |
| 32 | +import units from 'units-css'; |
| 33 | + |
| 34 | +const DEFAULT_CANVAS = document.createElement('canvas'); |
| 35 | +const DEFAULT_FONT_WEIGHT = 400; |
| 36 | +const DEFAULT_FONT_STYLE = 'normal'; |
| 37 | + |
| 38 | +const measureHeight = (size, lineHeight) => { |
| 39 | + // If the line-height is unitless, |
| 40 | + // multiply it by the font size. |
| 41 | + if (!lineHeight.unit) { |
| 42 | + return units.parse( |
| 43 | + `${size.value * lineHeight.value}${size.unit}` |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + // units-css requires the user to provide |
| 48 | + // DOM nodes for these units. We don't want |
| 49 | + // to pollute our API with that for the time being. |
| 50 | + const unitBlacklist = ['%', 'ch', 'cm', 'em', 'ex']; |
| 51 | + if (unitBlacklist.indexOf(lineHeight.unit) !== -1) { // eslint-disable-line no-magic-numbers |
| 52 | + throw new Error( |
| 53 | + `We do not currently support the unit ${lineHeight.unit} |
| 54 | + from the provided line-height ${lineHeight.value}. |
| 55 | + Unsupported units include ${unitBlacklist.join(', ')}.` |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + // Otherwise, the height is equivalent |
| 60 | + // to the provided line height. |
| 61 | + // Non-px units need conversion. |
| 62 | + if (lineHeight.unit === 'px') { |
| 63 | + return lineHeight; |
| 64 | + } |
| 65 | + return units.parse( |
| 66 | + units.convert(lineHeight, 'px') |
| 67 | + ); |
| 68 | +}; |
| 69 | + |
| 70 | +const measureText = ({ |
| 71 | + text, |
| 72 | + fontFamily, |
| 73 | + fontSize, |
| 74 | + lineHeight, |
| 75 | + fontWeight = DEFAULT_FONT_WEIGHT, |
| 76 | + fontStyle = DEFAULT_FONT_STYLE, |
| 77 | + canvas = DEFAULT_CANVAS |
| 78 | +}) => { |
| 79 | + const ctx = canvas.getContext('2d'); |
| 80 | + ctx.font = `${fontWeight} ${fontStyle} ${fontSize} ${fontFamily}`; |
| 81 | + |
| 82 | + const measure = (line) => { |
| 83 | + return { |
| 84 | + text: line, |
| 85 | + width: units.parse(`${ctx.measureText(line).width}px`), |
| 86 | + height: measureHeight( |
| 87 | + units.parse(fontSize), |
| 88 | + units.parse(lineHeight) |
| 89 | + ) |
| 90 | + }; |
| 91 | + }; |
| 92 | + |
| 93 | + // If multiline, measure the bounds |
| 94 | + // of all of the lines combined |
| 95 | + if (Array.isArray(text)) { |
| 96 | + return text |
| 97 | + .map(measure) |
| 98 | + .reduce((prev, curr) => { |
| 99 | + const width = curr.width.value > prev.width.value |
| 100 | + ? curr.width : prev.width; |
| 101 | + const height = units.parse( |
| 102 | + `${prev.height.value + curr.height.value}${curr.height.unit}` |
| 103 | + ); |
| 104 | + const longest = curr.text.length > prev.text.length |
| 105 | + ? curr.text : prev.text; |
| 106 | + return { width, height, text: longest }; |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + return measure(text); |
| 111 | +}; |
| 112 | + |
| 113 | +export default measureText; |
| 114 | +export { measureText }; |
0 commit comments