|
| 1 | +/* eslint-disable */ |
| 2 | +/* |
| 3 | + * from https://github.com/heygrady/Units/blob/master/Length.js |
| 4 | + * by heygrady |
| 5 | +*/ |
| 6 | + |
| 7 | +// create a test element |
| 8 | +var testElem = document.createElement('test'), |
| 9 | + docElement = document.documentElement, |
| 10 | + defaultView = document.defaultView, |
| 11 | + getComputedStyle = defaultView && defaultView.getComputedStyle, |
| 12 | + computedValueBug, |
| 13 | + runit = /^(-?[\d+\.\-]+)([a-z]+|%)$/i, |
| 14 | + convert = {}, |
| 15 | + conversions = [1 / 25.4, 1 / 2.54, 1 / 72, 1 / 6], |
| 16 | + units = ['mm', 'cm', 'pt', 'pc', 'in', 'mozmm'], |
| 17 | + i = 6; // units.length |
| 18 | + |
| 19 | +// add the test element to the dom |
| 20 | +docElement.appendChild(testElem); |
| 21 | + |
| 22 | +// test for the WebKit getComputedStyle bug |
| 23 | +// @see http://bugs.jquery.com/ticket/10639 |
| 24 | +if (getComputedStyle) { |
| 25 | + // add a percentage margin and measure it |
| 26 | + testElem.style.marginTop = '1%'; |
| 27 | + computedValueBug = getComputedStyle(testElem).marginTop === '1%'; |
| 28 | +} |
| 29 | + |
| 30 | +// pre-calculate absolute unit conversions |
| 31 | +while (i--) { |
| 32 | + convert[units[i] + "toPx"] = conversions[i] ? conversions[i] * convert.inToPx : toPx(testElem, '1' + units[i]); |
| 33 | +} |
| 34 | + |
| 35 | +// remove the test element from the DOM and delete it |
| 36 | +docElement.removeChild(testElem); |
| 37 | +testElem = undefined; |
| 38 | + |
| 39 | +// convert a value to pixels |
| 40 | +function toPx(elem, value, prop, force) { |
| 41 | + // use width as the default property, or specify your own |
| 42 | + prop = prop || 'width'; |
| 43 | + |
| 44 | + var style, |
| 45 | + inlineValue, |
| 46 | + ret, |
| 47 | + unit = (value.match(runit) || [])[2], |
| 48 | + conversion = unit === 'px' ? 1 : convert[unit + 'toPx'], |
| 49 | + rem = /r?em/i; |
| 50 | + |
| 51 | + if (conversion || rem.test(unit) && !force) { |
| 52 | + // calculate known conversions immediately |
| 53 | + // find the correct element for absolute units or rem or fontSize + em or em |
| 54 | + elem = conversion ? elem : unit === 'rem' ? docElement : prop === 'fontSize' ? elem.parentNode || elem : elem; |
| 55 | + |
| 56 | + // use the pre-calculated conversion or fontSize of the element for rem and em |
| 57 | + conversion = conversion || parseFloat(curCSS(elem, 'fontSize')); |
| 58 | + |
| 59 | + // multiply the value by the conversion |
| 60 | + ret = parseFloat(value) * conversion; |
| 61 | + } else { |
| 62 | + // begin "the awesome hack by Dean Edwards" |
| 63 | + // @see http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291 |
| 64 | + |
| 65 | + // remember the current style |
| 66 | + style = elem.style; |
| 67 | + inlineValue = style[prop]; |
| 68 | + |
| 69 | + // set the style on the target element |
| 70 | + try { |
| 71 | + style[prop] = value; |
| 72 | + } catch (e) { |
| 73 | + // IE 8 and below throw an exception when setting unsupported units |
| 74 | + return 0; |
| 75 | + } |
| 76 | + |
| 77 | + // read the computed value |
| 78 | + // if style is nothing we probably set an unsupported unit |
| 79 | + ret = !style[prop] ? 0 : parseFloat(curCSS(elem, prop)); |
| 80 | + |
| 81 | + // reset the style back to what it was or blank it out |
| 82 | + style[prop] = inlineValue !== undefined ? inlineValue : null; |
| 83 | + } |
| 84 | + |
| 85 | + // return a number |
| 86 | + return ret; |
| 87 | +} |
| 88 | + |
| 89 | +// return the computed value of a CSS property |
| 90 | +function curCSS(elem, prop) { |
| 91 | + var value, |
| 92 | + pixel, |
| 93 | + unit, |
| 94 | + rvpos = /^top|bottom/, |
| 95 | + outerProp = ["paddingTop", "paddingBottom", "borderTop", "borderBottom"], |
| 96 | + innerHeight, |
| 97 | + parent, |
| 98 | + i = 4; // outerProp.length |
| 99 | + |
| 100 | + if (getComputedStyle) { |
| 101 | + // FireFox, Chrome/Safari, Opera and IE9+ |
| 102 | + value = getComputedStyle(elem)[prop]; |
| 103 | + } else if (pixel = elem.style['pixel' + prop.charAt(0).toUpperCase() + prop.slice(1)]) { |
| 104 | + // IE and Opera support pixel shortcuts for top, bottom, left, right, height, width |
| 105 | + // WebKit supports pixel shortcuts only when an absolute unit is used |
| 106 | + value = pixel + 'px'; |
| 107 | + } else if (prop === 'fontSize') { |
| 108 | + // correct IE issues with font-size |
| 109 | + // @see http://bugs.jquery.com/ticket/760 |
| 110 | + value = toPx(elem, '1em', 'left', 1) + 'px'; |
| 111 | + } else { |
| 112 | + // IE 8 and below return the specified style |
| 113 | + value = elem.currentStyle[prop]; |
| 114 | + } |
| 115 | + |
| 116 | + // check the unit |
| 117 | + unit = (value.match(runit) || [])[2]; |
| 118 | + if (unit === '%' && computedValueBug) { |
| 119 | + // WebKit won't convert percentages for top, bottom, left, right, margin and text-indent |
| 120 | + if (rvpos.test(prop)) { |
| 121 | + // Top and bottom require measuring the innerHeight of the parent. |
| 122 | + innerHeight = (parent = elem.parentNode || elem).offsetHeight; |
| 123 | + while (i--) { |
| 124 | + innerHeight -= parseFloat(curCSS(parent, outerProp[i])); |
| 125 | + } |
| 126 | + value = parseFloat(value) / 100 * innerHeight + 'px'; |
| 127 | + } else { |
| 128 | + // This fixes margin, left, right and text-indent |
| 129 | + // @see https://bugs.webkit.org/show_bug.cgi?id=29084 |
| 130 | + // @see http://bugs.jquery.com/ticket/10639 |
| 131 | + value = toPx(elem, value); |
| 132 | + } |
| 133 | + } else if ((value === 'auto' || (unit && unit !== 'px')) && getComputedStyle) { |
| 134 | + // WebKit and Opera will return auto in some cases |
| 135 | + // Firefox will pass back an unaltered value when it can't be set, like top on a static element |
| 136 | + value = 0; |
| 137 | + } else if (unit && unit !== 'px' && !getComputedStyle) { |
| 138 | + // IE 8 and below won't convert units for us |
| 139 | + // try to convert using a prop that will return pixels |
| 140 | + // this will be accurate for everything (except font-size and some percentages) |
| 141 | + value = toPx(elem, value) + 'px'; |
| 142 | + } |
| 143 | + return value; |
| 144 | +} |
| 145 | + |
| 146 | +export default toPx |
0 commit comments