dfcreative/mucss

View on GitHub
padding.js

Summary

Maintainability
A
3 hrs
Test Coverage
/**
 * Caclulate paddings of an element.
 * @module  mucss/paddings
 */


var Rect = require('./rect');
var parse = require('./parse-value');


/**
 * Return paddings of an element.
 *
 * @param    {Element}   el   An element to calc paddings.
 * @return   {Object}   Paddings object `{top:n, bottom:n, left:n, right:n}`.
 */
module.exports = function(el){
    if (el === window) return Rect();

    if (!(el instanceof Element)) throw Error('Argument is not an element');

    var style = window.getComputedStyle(el);

    return Rect(
        parse(style.paddingLeft),
        parse(style.paddingTop),
        parse(style.paddingRight),
        parse(style.paddingBottom)
    );
};