How to parse CSS font shorthand format

前端 未结 4 408
梦谈多话
梦谈多话 2021-01-11 23:19

I need to parse the CSS font shorthand format into the separate components (font-family, font-size, font-weight, ...). This shorthand format looks pretty complicated. Here a

相关标签:
4条回答
  • 2021-01-11 23:27

    Here's a "temporary DOM element and using jquery's css() function" solution:

    http://jsfiddle.net/thirtydot/tpSsE/2/

    var $test = $('<span />');
    $test.css('font', 'bold italic small-caps 1em/1.5em verdana,sans-serif');
    
    alert($test.css('fontWeight'));
    alert($test.css('fontStyle'));
    alert($test.css('fontVariant'));
    alert($test.css('fontSize'));
    alert($test.css('lineHeight'));
    alert($test.css('fontFamily'));
    

    0 讨论(0)
  • 2021-01-11 23:27

    Here is my own humble try of a font parser function I just created. But I'm not sure if it works with all specialities of the font short hand format.

    function parseFont(font)
    {
        var fontFamily = null,
            fontSize = null,
            fontStyle = "normal",
            fontWeight = "normal",
            fontVariant = "normal",
            lineHeight = "normal";
    
        var elements = font.split(/\s+/);
        outer: while (element = elements.shift())
        {
            switch (element)
            {
                case "normal":
                    break;
    
                case "italic":
                case "oblique":
                    fontStyle = element;
                    break;
    
                case "small-caps":
                    fontVariant = element;
                    break;
    
                case "bold":
                case "bolder":
                case "lighter":
                case "100":
                case "200":
                case "300":
                case "400":
                case "500":
                case "600":
                case "700":
                case "800":
                case "900":
                    fontWeight = element;
                    break;
    
                default:
                    if (!fontSize)
                    {
                        var parts = element.split("/");
                        fontSize = parts[0];
                        if (parts.length > 1) lineHeight = parts[1];
                        break;
                    }
    
                    fontFamily = element;
                    if (elements.length)
                        fontFamily += " " + elements.join(" ");
                    break outer;
            }
        }
    
        return {
            "fontStyle": fontStyle,
            "fontVariant": fontVariant,
            "fontWeight": fontWeight,
            "fontSize": fontSize,
            "lineHeight": lineHeight,
            "fontFamily": fontFamily
        }
    }
    
    0 讨论(0)
  • 2021-01-11 23:29

    The parse rules are described in the spec which also has a guide to reading the language used to express the value rules.

    0 讨论(0)
  • 2021-01-11 23:38

    Pure free-range Javascript version:

    var parsedStyleForCSS = function(cssString){
        var el = document.createElement("span");
        el.setAttribute("style", cssString);
    
        return el.style; // CSSStyleDeclaration object
    };
    
    var parsedStyle = parsedStyleForCSS("font: bold italic small-caps 1em/1.5em verdana,sans-serif");
    
    console.log(parsedStyle["fontWeight"]); // bold
    console.log(parsedStyle["fontStyle"]); // italic
    console.log(parsedStyle["fontVariant"]); // small-caps
    console.log(parsedStyle["fontSize"]); // 1em
    console.log(parsedStyle["lineHeight"]); // 1.5em
    console.log(parsedStyle["fontFamily"]); // verdana, sans-serif
    

    If you're looking to do something similar with complete stylesheets, see this answer: Parsing CSS in JavaScript / jQuery

    0 讨论(0)
提交回复
热议问题