Repository navigation
Write an escaped GSP expression in long text without its backslash #17181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jdaugherty
wants to merge
5
commits into
apache:8.0.x
Choose a base branch
from
jdaugherty:fix/gsp-escaped-expression-long-text
base: 8.0.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+210
−2
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9251ad4
Write an escaped GSP expression in long text without its backslash
jdaugherty 57a1e69
Cut long GSP text before an escaped expression
jdaugherty 71c7df0
Use one check for an escaped GSP expression when cutting long text
jdaugherty 992a13e
Merge remote-tracking branch 'upstream/8.0.x' into fix/gsp-escaped-ex…
jdaugherty 73ad8d0
Add the escaped GSP expression fix to the upgrade skill
jdaugherty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
grails-gsp/core/src/test/groovy/org/grails/gsp/GspLongTextEscapeSpec.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.grails.gsp | ||
|
|
||
| import spock.lang.Specification | ||
| import spock.lang.TempDir | ||
| import spock.lang.Unroll | ||
|
|
||
| import org.grails.gsp.compiler.GroovyPageCompiler | ||
| import org.grails.gsp.io.GroovyPageCompiledScriptSource | ||
|
|
||
| /** | ||
| * Long text is handed from the scanner to the parser in pieces of 64,000 characters. An escaped | ||
| * expression at the end of a piece is still one. | ||
| */ | ||
| class GspLongTextEscapeSpec extends Specification { | ||
|
|
||
| @TempDir | ||
| File tempDir | ||
|
|
||
| GroovyPagesTemplateEngine engine | ||
|
|
||
| void setup() { | ||
| engine = new GroovyPagesTemplateEngine() | ||
| engine.afterPropertiesSet() | ||
| } | ||
|
|
||
| @Unroll | ||
| void "an escaped expression after #length characters of text is written without its backslash"() { | ||
| given: | ||
| String text = 'x' * length | ||
|
|
||
| expect: | ||
| render(text + '\\${a} and ${1 + 1}') == text + '${a} and 2' | ||
|
|
||
| where: | ||
| length << (63_990..64_005) + (127_990..128_005) | ||
| } | ||
|
|
||
| @Unroll | ||
| void "a backslash in front of an escaped expression after #length characters of text is written, and the expression as text"() { | ||
| given: | ||
| String text = 'x' * length | ||
|
|
||
| expect: | ||
| render(text + '\\\\${a} and ${1 + 1}') == text + '\\${a} and 2' | ||
|
|
||
| where: | ||
| length << (63_995..64_001) | ||
| } | ||
|
|
||
| @Unroll | ||
| void "#lookalike after #length characters of text is not an escaped expression and is written as it is"() { | ||
| given: | ||
| String text = 'x' * length | ||
|
|
||
| expect: | ||
| render(text + lookalike + ' and ${1 + 1}') == text + lookalike + ' and 2' | ||
|
|
||
| where: | ||
| [length, lookalike] << [63_997..64_000, ['\\$ {a}', '\\{a}', '$\\{a}']].combinations() | ||
| } | ||
|
|
||
| @Unroll | ||
| void "a compiled page with an escaped expression after #length characters, #twoByte of them two bytes long, is written without its backslash"() { | ||
| given: 'text whose first 64,000 characters take up to the 65,535 bytes a compiled page can store as one piece' | ||
| String text = ('é' * twoByte) + ('x' * (length - twoByte)) | ||
|
|
||
| expect: | ||
| renderCompiled(text + '\\${a} and ${1 + 1}') == text + '${a} and 2' | ||
|
|
||
| where: | ||
| length | twoByte | ||
| 63_998 | 0 | ||
| 63_999 | 0 | ||
| 63_998 | 1_535 | ||
| 63_999 | 1_535 | ||
| 64_000 | 1_535 | ||
| } | ||
|
|
||
| private String render(String source) { | ||
| StringWriter out = new StringWriter() | ||
| engine.createTemplate(source, "long${source.hashCode().abs()}").make([:]).writeTo(out) | ||
| out.toString() | ||
| } | ||
|
|
||
| private String renderCompiled(String source) { | ||
| File viewsDir = new File(tempDir, 'views') | ||
| File classesDir = new File(tempDir, 'classes') | ||
| viewsDir.mkdirs() | ||
| classesDir.mkdirs() | ||
| File page = new File(viewsDir, 'page.gsp') | ||
| page.setText(source, 'UTF-8') | ||
|
|
||
| GroovyPageCompiler compiler = new GroovyPageCompiler() | ||
| compiler.viewsDir = viewsDir | ||
| compiler.targetDir = classesDir | ||
| compiler.generatedGroovyPagesDirectory = new File(tempDir, 'generated') | ||
| compiler.srcFiles = [page] | ||
| Map compiled = compiler.compile() | ||
|
|
||
| new URLClassLoader([classesDir.toURI().toURL()] as URL[], getClass().classLoader).withCloseable { URLClassLoader loader -> | ||
| Class<?> pageClass = loader.loadClass(compiled['/page.gsp'] as String) | ||
| StringWriter out = new StringWriter() | ||
| engine.createTemplate(new GroovyPageCompiledScriptSource('/page.gsp', '/page.gsp', pageClass)).make([:]).writeTo(out) | ||
| out.toString() | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A page of accented letters and plain letters ends in an escaped expression. It should compile and write the expression as text. It fails, because the piece before the escape is still one byte too long. Cut that piece shorter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is not from the escape. A piece is cut at 64,000 characters, and a compiled page stores a piece in at most 65,535 bytes, so any text with more than 1,535 two-byte characters in one piece fails to compile, with or without
\${.I compiled pages of 63,996 to 64,002 characters with 1,534 to 1,538
éon this branch and on the commit before it. The page with 1,537éin 63,999 characters and\${a}at the end fails on both: the piece is 65,536 bytes here and one byte more before. The same text with plain text in place of the escape fails the same way, and 7.0.x has the same cut and the samewriteUTFcall. No page that compiled before fails now; this PR only makes the piece in front of an escape one or two characters shorter.Cutting by encoded length is a fix of its own, and it belongs on 7.0.x, so I am leaving it out of this PR.