|  | 
|  | 1 | +var input = document.getElementById("input"); | 
|  | 2 | + | 
|  | 3 | +function doStuff() { | 
|  | 4 | +    let output = document.getElementById("output"); | 
|  | 5 | +    output.textContent = fixCssValue(input.textContent.trim()); | 
|  | 6 | +} | 
|  | 7 | + | 
|  | 8 | +doStuff(); | 
|  | 9 | + | 
|  | 10 | +input.focus(); | 
|  | 11 | + | 
|  | 12 | +input.oninput = function() { | 
|  | 13 | +    doStuff(); | 
|  | 14 | +}; | 
|  | 15 | + | 
|  | 16 | +function fixCssValue(value) { | 
|  | 17 | +    let length = value.length; | 
|  | 18 | +    let parenthesisUnclosedCount = 0; | 
|  | 19 | + | 
|  | 20 | +    const State = { | 
|  | 21 | +        Data: 0, | 
|  | 22 | +        SingleQuoteString: 2, | 
|  | 23 | +        DoubleQuoteString: 3, | 
|  | 24 | +        Comment: 4 | 
|  | 25 | +    }; | 
|  | 26 | + | 
|  | 27 | +    let state = State.Data; | 
|  | 28 | + | 
|  | 29 | +    for (let i = 0; i < length; i++) { | 
|  | 30 | +        let char = value[i]; | 
|  | 31 | +        switch (char) { | 
|  | 32 | +        case "'": | 
|  | 33 | +            if (state === State.Data) | 
|  | 34 | +                state = State.SingleQuoteString; | 
|  | 35 | +            else if (state === State.SingleQuoteString) | 
|  | 36 | +                state = State.Data; | 
|  | 37 | +            break; | 
|  | 38 | + | 
|  | 39 | +        case "\"": | 
|  | 40 | +            if (state === State.Data) | 
|  | 41 | +                state = State.DoubleQuoteString; | 
|  | 42 | +            else if (state === State.DoubleQuoteString) | 
|  | 43 | +                state = State.Data; | 
|  | 44 | +            break; | 
|  | 45 | + | 
|  | 46 | +        case "(": | 
|  | 47 | +            if (state === State.Data) | 
|  | 48 | +                ++parenthesisUnclosedCount; | 
|  | 49 | +            break; | 
|  | 50 | + | 
|  | 51 | +        case ")": | 
|  | 52 | +            if (state === State.Data && parenthesisUnclosedCount) | 
|  | 53 | +                --parenthesisUnclosedCount; | 
|  | 54 | +            break; | 
|  | 55 | + | 
|  | 56 | +        case "/": | 
|  | 57 | +            if (state === State.Data) { | 
|  | 58 | +                if (i + 1 < length && value[i + 1] === "*") | 
|  | 59 | +                    state = State.Comment; | 
|  | 60 | +            } | 
|  | 61 | +            break; | 
|  | 62 | + | 
|  | 63 | +        case "*": | 
|  | 64 | +            if (state === State.Comment) { | 
|  | 65 | +                if (i + 1 < length && value[i + 1] === "/") | 
|  | 66 | +                    state = State.Data; | 
|  | 67 | +            } | 
|  | 68 | +            break; | 
|  | 69 | + | 
|  | 70 | +        } | 
|  | 71 | +    } | 
|  | 72 | + | 
|  | 73 | +    let suffix = ""; | 
|  | 74 | +    switch (state) { | 
|  | 75 | +    case State.SingleQuoteString: | 
|  | 76 | +        suffix = "'"; | 
|  | 77 | +        break; | 
|  | 78 | +    case State.DoubleQuoteString: | 
|  | 79 | +        suffix = "\""; | 
|  | 80 | +        break; | 
|  | 81 | +    case State.Comment: | 
|  | 82 | +        suffix = "*/"; | 
|  | 83 | +        break; | 
|  | 84 | +    } | 
|  | 85 | + | 
|  | 86 | +    if (parenthesisUnclosedCount) | 
|  | 87 | +        suffix += ")".repeat(parenthesisUnclosedCount); | 
|  | 88 | + | 
|  | 89 | +    return suffix; | 
|  | 90 | +} | 
0 commit comments