• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

i-net-software / jlessc / 281

pending completion
281

push

travis-ci-com

volker
Handle doubled rule correctly in the compressed formatter if previous
rule ends with an block

5 of 5 new or added lines in 1 file covered. (100.0%)

3586 of 3873 relevant lines covered (92.59%)

2.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.0
/src/com/inet/lib/less/Less.java
1
/**
2
 * MIT License (MIT)
3
 *
4
 * Copyright (c) 2014 - 2020 Volker Berlin
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 * 
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 * 
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * UT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 *
24
 * @author Volker Berlin
25
 * @license: The MIT license <http://opensource.org/licenses/MIT>
26
 */
27
package com.inet.lib.less;
28

29
import java.io.File;
30
import java.io.IOException;
31
import java.io.StringReader;
32
import java.net.URL;
33
import java.nio.charset.StandardCharsets;
34
import java.nio.file.Files;
35
import java.util.Collections;
36
import java.util.Map;
37
import java.util.concurrent.ConcurrentHashMap;
38

39
import javax.annotation.Nonnull;
40

41
/**
42
 * The main class of JLessC library. Its contain all start points for converting LESS to CSS files.
43
 */
44
public class Less {
3✔
45

46
    /**
47
     * Key for compress option. true, if the CSS data should be compressed without any extra formating characters.
48
     */
49
    public static final String COMPRESS     = "compress";
50

51
    /**
52
     * Rewrites URLs to make them relative to the base less file. 'all' rewrites all URLs, 'local' just those starting
53
     * with a '.', 'off' simple inline it.
54
     * @see <a href="http://lesscss.org/usage/#less-options-rewrite-urls">http://lesscss.org/usage/#less-options-rewrite-urls</a>
55
     */
56
    public static final String REWRITE_URLS = "rewrite-urls";
57

58
    /**
59
     * A map with custom less functions 
60
     */
61
    static final ConcurrentHashMap<String, CustomLessFunction> CUSTOM_FUNKTIONS = new ConcurrentHashMap<>();
3✔
62
    static {
63
        registerCustomFunction( "colorize-image", new CustomFunctionColorizeImage() );
3✔
64
    }
3✔
65

66
    /**
67
     * Compile the less data from a string.
68
     * 
69
     * @param baseURL
70
     *            the baseURL for import of external less data.
71
     * @param lessData
72
     *            the input less data
73
     * @param compress
74
     *            true, if the CSS data should be compressed without any extra formating characters.
75
     * @return the resulting less data
76
     * @throws LessException 
77
     *            if any error occur on compiling.
78
     */
79
    public static String compile( URL baseURL, String lessData, boolean compress ) throws LessException {
80
        return compile( baseURL, lessData, compress, new ReaderFactory() );
3✔
81
    }
82

83
    /**
84
     * Compile the less data from a string.
85
     * 
86
     * @param baseURL
87
     *            the baseURL for import of external less data.
88
     * @param lessData
89
     *            the input less data
90
     * @param compress
91
     *            true, if the CSS data should be compressed without any extra formating characters.
92
     * @param readerFactory
93
     *            A factory for the readers for imports.
94
     * @return the resulting less data
95
     * @throws LessException 
96
     *            if any error occur on compiling.
97
     */
98
    public static String compile( URL baseURL, String lessData, boolean compress, ReaderFactory readerFactory ) throws LessException {
99
        Map<String,String> params = Collections.singletonMap( COMPRESS, Boolean.toString( compress ) );
3✔
100
        return compile( baseURL, lessData, params, readerFactory );
3✔
101
    }
102

103
    /**
104
     * Compile the less data from a string.
105
     * 
106
     * @param baseURL
107
     *            the baseURL for import of external less data.
108
     * @param lessData
109
     *            the input less data
110
     * @param options
111
     *            some optional options, see constants for details
112
     * @return the resulting less data
113
     * @throws LessException 
114
     *            if any error occur on compiling.
115
     */
116
    public static String compile( URL baseURL, String lessData,  Map<String, String> options ) throws LessException {
117
        return compile( baseURL, lessData, options, new ReaderFactory() );
3✔
118
    }
119

120
    /**
121
     * Compile the less data from a string.
122
     * 
123
     * @param baseURL
124
     *            the baseURL for import of external less data.
125
     * @param lessData
126
     *            the input less data
127
     * @param options
128
     *            some optional options, see constants for details
129
     * @param readerFactory
130
     *            A factory for the readers for imports.
131
     * @return the resulting less data
132
     * @throws LessException
133
     *             if any error occur on compiling.
134
     */
135
    public static String compile( URL baseURL, String lessData, Map<String, String> options, ReaderFactory readerFactory ) throws LessException {
136
        try {
137
            if( options == null ) {
3✔
138
                options = Collections.emptyMap();
×
139
            }
140
            LessParser parser = new LessParser();
3✔
141
            parser.parse( baseURL, new StringReader( lessData ), readerFactory );
3✔
142

143
            boolean compress = Boolean.parseBoolean( options.get( COMPRESS ) );
3✔
144
            CssFormatter formatter = compress ? new CompressCssFormatter() : new CssFormatter();
3✔
145
            parser.parseLazy( formatter );
3✔
146
            StringBuilder builder = new StringBuilder();
3✔
147
            formatter.format( parser, baseURL, readerFactory, builder, options );
3✔
148
            return builder.toString();
3✔
149
        } catch( LessException ex ) {
3✔
150
            throw ex;
3✔
151
        } catch( Exception ex ) {
×
152
            throw new LessException( ex );
×
153
        }
154
    }
155

156
    /**
157
     * Compile the less data from a file.
158
     * 
159
     * @param lessFile
160
     *            the less file
161
     * @param compress
162
     *            true, if the CSS data should be compressed without any extra formating characters.
163
     * @return the resulting less data
164
     * @throws IOException
165
     *             if an I/O error occurs reading from the less file
166
     */
167
    public static String compile( File lessFile, boolean compress ) throws IOException {
168
        String lessData = new String( Files.readAllBytes( lessFile.toPath() ), StandardCharsets.UTF_8 );
3✔
169
        return Less.compile( lessFile.toURI().toURL(), lessData, compress, new ReaderFactory() );
3✔
170
    }
171

172
    /**
173
     * Compile the less data from a file.
174
     * 
175
     * @param lessFile
176
     *            the less file
177
     * @param compress
178
     *            true, if the CSS data should be compressed without any extra formating characters.
179
     * @param readerFactory
180
     *            A factory for the readers for imports.
181
     * @return the resulting less data
182
     * @throws IOException
183
     *             if an I/O error occurs reading from the less file
184
     */
185
    public static String compile( File lessFile, boolean compress, ReaderFactory readerFactory ) throws IOException {
186
        String lessData = new String( Files.readAllBytes( lessFile.toPath() ), StandardCharsets.UTF_8 );
×
187
        return Less.compile( lessFile.toURI().toURL(), lessData, compress, readerFactory );
×
188
    }
189

190
    /**
191
     * Register a custom less function.
192
     * @param name the non null name
193
     * @param func the function
194
     */
195
    public static void registerCustomFunction( @Nonnull String name, CustomLessFunction func ) {
196
        if( func == null ) {
3✔
197
            CUSTOM_FUNKTIONS.remove( name );
×
198
        } else {
199
            CUSTOM_FUNKTIONS.put( name, func );
3✔
200
        }
201
    }
3✔
202
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc