grails / grails-gsp

GSP (Grails Server Pages) - A server-side view rendering technology based on Groovy
Apache License 2.0
15 stars 32 forks source link

BUG: GroovyPageParser config loaded too late therefore ignored #380

Closed codeconsole closed 1 year ago

codeconsole commented 1 year ago

GroovyPageParser is configured after initialization which is totally useless.

For instance, in the constructor the method isSitemeshPreprocessingEnabled is run which looks to see if enableSitemeshPreprocessing is set, but the variable isn't set until AFTER initialization!

GroovyPagesTemplateEngine

    String gspSource = IOUtils.toString(inputStream, getGspEncoding());
    parser = new GroovyPageParser(name, path, path, decorateGroovyPageSource(new StringBuilder(gspSource)).toString());

    if (grailsApplication != null) {
        Config config = grailsApplication.getConfig();
        parser.configure(config);
    }

GroovyPageParser

    public GroovyPageParser(String name, String uri, String filename, String gspSource, String expressionCodecName) throws IOException {

        this.expressionCodecDirectiveValue = expressionCodecName;
        if (expressionCodecDirectiveValue == null) {
            expressionCodecDirectiveValue = OutputEncodingSettings.getDefaultValue(OutputEncodingSettings.EXPRESSION_CODEC_NAME);
        }

        Map<String, String> directives = parseDirectives(gspSource);

        if (isSitemeshPreprocessingEnabled(directives.get(SITEMESH_PREPROCESS_DIRECTIVE))) {
            // GSP preprocessing for direct sitemesh integration: replace head -> g:captureHead, title -> g:captureTitle, meta -> g:captureMeta, body -> g:captureBody
            gspSource = sitemeshPreprocessor.addGspSitemeshCapturing(gspSource);
            sitemeshPreprocessMode=true;
        }
        scan = new GroovyPageScanner(gspSource, uri);
        pageName = uri;
        environment = Environment.getCurrent();
        makeName(name);
        makeSourceName(filename);
    }

Fix: Put configuration in the constructor.