MCPcopy
hub / github.com/greensock/GSAP / _convertToUnit

Function _convertToUnit

esm/CSSPlugin.js:334–402  ·  view source on GitHub ↗
(target, property, value, unit)

Source from the content-addressed store, hash-verified

332},
333 //takes a single value like 20px and converts it to the unit specified, like "%", returning only the numeric amount.
334_convertToUnit = function _convertToUnit(target, property, value, unit) {
335 var curValue = parseFloat(value) || 0,
336 curUnit = (value + "").trim().substr((curValue + "").length) || "px",
337 // some browsers leave extra whitespace at the beginning of CSS variables, hence the need to trim()
338 style = _tempDiv.style,
339 horizontal = _horizontalExp.test(property),
340 isRootSVG = target.tagName.toLowerCase() === "svg",
341 measureProperty = (isRootSVG ? "client" : "offset") + (horizontal ? "Width" : "Height"),
342 amount = 100,
343 toPixels = unit === "px",
344 toPercent = unit === "%",
345 px,
346 parent,
347 cache,
348 isSVG;
349
350 if (unit === curUnit || !curValue || _nonConvertibleUnits[unit] || _nonConvertibleUnits[curUnit]) {
351 return curValue;
352 }
353
354 curUnit !== "px" && !toPixels && (curValue = _convertToUnit(target, property, value, "px"));
355 isSVG = target.getCTM && _isSVG(target);
356
357 if ((toPercent || curUnit === "%") && (_transformProps[property] || ~property.indexOf("adius"))) {
358 px = isSVG ? target.getBBox()[horizontal ? "width" : "height"] : target[measureProperty];
359 return _round(toPercent ? curValue / px * amount : curValue / 100 * px);
360 }
361
362 style[horizontal ? "width" : "height"] = amount + (toPixels ? curUnit : unit);
363 parent = unit !== "rem" && ~property.indexOf("adius") || unit === "em" && target.appendChild && !isRootSVG ? target : target.parentNode;
364
365 if (isSVG) {
366 parent = (target.ownerSVGElement || {}).parentNode;
367 }
368
369 if (!parent || parent === _doc || !parent.appendChild) {
370 parent = _doc.body;
371 }
372
373 cache = parent._gsap;
374
375 if (cache && toPercent && cache.width && horizontal && cache.time === _ticker.time && !cache.uncache) {
376 return _round(curValue / cache.width * amount);
377 } else {
378 if (toPercent && (property === "height" || property === "width")) {
379 // if we're dealing with width/height that's inside a container with padding and/or it's a flexbox/grid container, we must apply it to the target itself rather than the _tempDiv in order to ensure complete accuracy, factoring in the parent's padding.
380 var v = target.style[property];
381 target.style[property] = amount + unit;
382 px = target[measureProperty];
383 v ? target.style[property] = v : _removeProperty(target, property);
384 } else {
385 (toPercent || curUnit === "%") && !_nonStandardLayouts[_getComputedProperty(parent, "display")] && (style.position = _getComputedProperty(target, "position"));
386 parent === target && (style.position = "static"); // like for borderRadius, if it's a % we must have it relative to the target itself but that may not have position: relative or position: absolute in which case it'd go up the chain until it finds its offsetParent (bad). position: static protects against that.
387
388 parent.appendChild(_tempDiv);
389 px = _tempDiv[measureProperty];
390 parent.removeChild(_tempDiv);
391 style.position = "absolute";

Callers 6

_getFunction · 0.70
_tweenComplexCSSStringFunction · 0.70
_addPxTranslateFunction · 0.70
_renderSVGTransformsFunction · 0.70
_addRawTransformPTsFunction · 0.70
CSSPlugin.jsFile · 0.70

Calls 5

_roundFunction · 0.90
_getCacheFunction · 0.90
_isSVGFunction · 0.70
_removePropertyFunction · 0.70
_getComputedPropertyFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…