Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 158 lines (133 sloc) 5.987 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
<!DOCTYPE html>

<html>

<head>
    <title>Tokenizer Tests</title>

    <style type="text/css">
        table {
            border-collapse: collapse;
            border-spacing: 0;
            width: 100%;
        }
        th, td {
            text-align: left;
            padding: .2em 1em;
            border-bottom: 1px solid #CCC;
            font: 13px sans-serif;
        }

        .tokens ul,
        .tokens li {
            list-style: none;
            margin: 0;
            padding: 0;
        }

        .result {
            font-weight: bold;
        }
        .fail {
            color: orange;
        }
        .error {
            color: red;
        }
        .pass {
            color: green;
        }
    </style>
</head>

<body>

    <script type="text/javascript">
        window.PIE = {};

        function runTest( css, expectedTokens) {
            document.write( '<tr><td class="css">' + css + '</td><td class="tokens"><ul>' );

            var result, error, className;
            try {
                var tokenizer = new PIE.Tokenizer( css ),
                    actualTokens = [],
                    token, expected, failure;

                function checkMatch( msgPrefix ) {
                    if( actualTokens.length === expectedTokens.length ) {
                        for( var i = 0, len = actualTokens.length; i < len; i++ ) {
                            expected = expectedTokens[i];
                            if( actualTokens[i].type !== expected.type ) {
                                failure = ( msgPrefix || '' ) + "Type mismatch at token " + i + ": expected " + expected.type + ', got ' + actualTokens[i].type;
                                break;
                            }
                            if( actualTokens[i].value !== expected.value ) {
                                failure = ( msgPrefix || '' ) + "Value mismatch at token " + i + ": expected " + expected.value + ', got ' + actualTokens[i].value;
                                break;
                            }
                        }
                    } else {
                        failure = ( msgPrefix || '' ) + "Number of tokens is different: expected " + expectedTokens.length + ', got ' + actualTokens.length;
                    }
                }

                // verify tokens in forward order
                while( token = tokenizer.next() ) {
                    actualTokens.push( token );
                    document.write( '<li>type=' + token.tokenType + ', value=' + token.tokenValue + '</li>' );
                }
                checkMatch( '' );

                // step back through all of them to make sure backtracking works
                actualTokens = [];
                while( token = tokenizer.prev() ) {
                    actualTokens.unshift( token );
                }
                checkMatch( 'Backtracking: ' );
            } catch( e ) {
                error = e.message;
            }
            className = !!error ? 'error' : !!failure ? 'fail' : 'pass';

            document.write( '</ul></td><td class="result ' + className + '">' + ( error || failure || 'Passed' ) + '</td></tr>' );
        }

    </script>

    <script type="text/javascript" src="../sources/Color.js"></script>
    <script type="text/javascript" src="../sources/Tokenizer.js"></script>

    <table>
        <thead>
            <tr>
                <th>CSS Value</th>
                <th>Tokens</th>
                <th>Test Result</th>
            </tr>
        </thead>
        <tbody>
            <script type="text/javascript">

                runTest( '#00CCFF', [
                    { tokenType: PIE.Tokenizer.Type.COLOR, tokenValue: '#00CCFF' }
                ] );

                runTest( 'rgb(0,127, 255 )', [
                    { tokenType: PIE.Tokenizer.Type.COLOR, tokenValue:'rgb(0,127, 255 )' }
                ] );

                runTest( 'rgba(0,127, 255, 1 )', [
                    { tokenType: PIE.Tokenizer.Type.COLOR, tokenValue:'rgba(0,127, 255, 1 )' }
                ] );

                runTest( 'linear-gradient(center top, yellow, blue)', [
                    { tokenType: PIE.Tokenizer.Type.FUNCTION, tokenValue: 'linear-gradient' },
                    { tokenType: PIE.Tokenizer.Type.IDENT, tokenValue: 'center' },
                    { tokenType: PIE.Tokenizer.Type.IDENT, tokenValue: 'top' },
                    { tokenType: PIE.Tokenizer.Type.OPERATOR, tokenValue: ',' },
                    { tokenType: PIE.Tokenizer.Type.COLOR, tokenValue: 'yellow' },
                    { tokenType: PIE.Tokenizer.Type.OPERATOR, tokenValue: ',' },
                    { tokenType: PIE.Tokenizer.Type.COLOR, tokenValue: 'blue' },
                    { tokenType: PIE.Tokenizer.Type.CHARACTER, tokenValue: ')' }
                ] );

                runTest( 'linear-gradient(top, yellow, rgba(0,0,255,0.5))', [
                    { tokenType: PIE.Tokenizer.Type.FUNCTION, tokenValue: 'linear-gradient' },
                    { tokenType: PIE.Tokenizer.Type.IDENT, tokenValue: 'top' },
                    { tokenType: PIE.Tokenizer.Type.OPERATOR, tokenValue: ',' },
                    { tokenType: PIE.Tokenizer.Type.COLOR, tokenValue: 'yellow' },
                    { tokenType: PIE.Tokenizer.Type.OPERATOR, tokenValue: ',' },
                    { tokenType: PIE.Tokenizer.Type.COLOR, tokenValue: 'rgba(0,0,255,0.5)' },
                    { tokenType: PIE.Tokenizer.Type.CHARACTER, tokenValue: ')' }
                ]);

                runTest( 'top 15px 3em 50%', [
                    { tokenType: PIE.Tokenizer.Type.IDENT, tokenValue: 'top' },
                    { tokenType: PIE.Tokenizer.Type.LENGTH, tokenValue: '15px' },
                    { tokenType: PIE.Tokenizer.Type.LENGTH, tokenValue: '3em' },
                    { tokenType: PIE.Tokenizer.Type.PERCENT, tokenValue: '50%' }
                ] );

            </script>
        </tbody>
    </table>

</body>

</html>
Something went wrong with that request. Please try again.