Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
to_px.js158 linesDownload Raw Back to js
1/////////////////////////////////////////////////////////////2//3// pgAdmin 4 - PostgreSQL Tools4//5// Copyright (C) 2013 - 2024, The pgAdmin Development Team6// This software is released under the PostgreSQL Licence7//8//////////////////////////////////////////////////////////////9 10/* Ref: https://github.com/heygrady/Units/blob/master/Length.js */11 12let testElem = document.createElement('test'),13  docElement = document.documentElement,14  defaultView = document.defaultView,15  getComputedStyle = defaultView?.getComputedStyle,16  computedValueBug,17  runit = /^(-?[\d+\.\-]+)([a-z]+|%)$/i,18  convert = {},19  conversions = [1 / 25.4, 1 / 2.54, 1 / 72, 1 / 6],20  units = ['mm', 'cm', 'pt', 'pc', 'in', 'mozmm'],21  i = 6; // units.length22 23// add the test element to the dom24docElement.appendChild(testElem);25 26// test for the WebKit getComputedStyle bug27// @see http://bugs.jquery.com/ticket/1063928if (getComputedStyle) {29  // add a percentage margin and measure it30  testElem.style.marginTop = '1%';31  computedValueBug = getComputedStyle(testElem).marginTop === '1%';32}33 34// pre-calculate absolute unit conversions35while (i--) {36  convert[units[i] + 'toPx'] = conversions[i] ? conversions[i] * convert.inToPx : toPx('1' + units[i], null, false, testElem);37}38 39// remove the test element from the DOM and delete it40docElement.removeChild(testElem);41testElem = undefined;42 43// convert a value to pixels44export default function toPx(value, prop, force, el) {45  let elem = document.createElement('div');46  if(el) {47    elem = el;48  } else {49    elem = document.createElement('div');50    document.body.appendChild(elem);51  }52 53  // use width as the default property, or specify your own54  prop = prop || 'width';55 56  var style,57    inlineValue,58    ret,59    unit = (value.match(runit) || [])[2],60    conversion = unit === 'px' ? 1 : convert[unit + 'toPx'],61    rem = /r?em/i;62 63  if (conversion || rem.test(unit) && !force) {64    // calculate known conversions immediately65    // find the correct element for absolute units or rem or fontSize + em or em66    elem = conversion ? elem : unit === 'rem' ? docElement : prop === 'fontSize' ? elem.parentNode || elem : elem;67 68    // use the pre-calculated conversion or fontSize of the element for rem and em69    conversion = conversion || parseFloat(curCSS(elem, 'fontSize'));70 71    // multiply the value by the conversion72    ret = parseFloat(value) * conversion;73  } else {74    // begin "the awesome hack by Dean Edwards"75    // @see http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-10229176 77    // remember the current style78    style = elem.style;79    inlineValue = style[prop];80 81    // set the style on the target element82    try {83      style[prop] = value;84    } catch (e) {85      // IE 8 and below throw an exception when setting unsupported units86      return 0;87    }88 89    // read the computed value90    // if style is nothing we probably set an unsupported unit91    ret = !style[prop] ? 0 : parseFloat(curCSS(elem, prop));92 93    // reset the style back to what it was or blank it out94    style[prop] = inlineValue !== undefined ? inlineValue : null;95  }96 97  // return a number98  if(!el) document.body.removeChild(elem);99  return ret;100}101 102// return the computed value of a CSS property103function curCSS(elem, prop) {104  var value,105    pixel,106    unit,107    rvpos = /^(top|bottom)/,108    outerProp = ['paddingTop', 'paddingBottom', 'borderTop', 'borderBottom'],109    innerHeight,110    parent,111    i = 4; // outerProp.length112 113  if (getComputedStyle) {114    // FireFox, Chrome/Safari, Opera and IE9+115    value = getComputedStyle(elem)[prop];116  } else if (pixel == elem.style['pixel' + prop.charAt(0).toUpperCase() + prop.slice(1)]) {117    // IE and Opera support pixel shortcuts for top, bottom, left, right, height, width118    // WebKit supports pixel shortcuts only when an absolute unit is used119    value = pixel + 'px';120  } else if (prop === 'fontSize') {121    // correct IE issues with font-size122    // @see http://bugs.jquery.com/ticket/760123    value = toPx(elem, '1em', 'left', 1) + 'px';124  } else {125    // IE 8 and below return the specified style126    value = elem.currentStyle[prop];127  }128 129  // check the unit130  unit = (value.match(runit) || [])[2];131  if (unit === '%' && computedValueBug) {132    // WebKit won't convert percentages for top, bottom, left, right, margin and text-indent133    if (rvpos.test(prop)) {134      // Top and bottom require measuring the innerHeight of the parent.135      innerHeight = (parent = elem.parentNode || elem).offsetHeight;136      while (i--) {137        innerHeight -= parseFloat(curCSS(parent, outerProp[i]));138      }139      value = parseFloat(value) / 100 * innerHeight + 'px';140    } else {141      // This fixes margin, left, right and text-indent142      // @see https://bugs.webkit.org/show_bug.cgi?id=29084143      // @see http://bugs.jquery.com/ticket/10639144      value = toPx(elem, value);145    }146  } else if ((value === 'auto' || (unit && unit !== 'px')) && getComputedStyle) {147    // WebKit and Opera will return auto in some cases148    // Firefox will pass back an unaltered value when it can't be set, like top on a static element149    value = 0;150  } else if (unit && unit !== 'px' && !getComputedStyle) {151    // IE 8 and below won't convert units for us152    // try to convert using a prop that will return pixels153    // this will be accurate for everything (except font-size and some percentages)154    value = toPx(elem, value) + 'px';155  }156  return value;157}158 
codekingpro/portable-devtools · Team Ai