/
SitemeshTagLib.groovy
223 lines (206 loc) · 7.47 KB
/
SitemeshTagLib.groovy
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* Copyright 2004-2005 the original author or authors.
*
* Licensed 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
*
* http://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.codehaus.groovy.grails.plugins.web.taglib
import grails.artefact.Artefact
import groovy.transform.CompileStatic
import org.apache.commons.lang.WordUtils
import org.codehaus.groovy.grails.web.pages.FastStringWriter
import org.codehaus.groovy.grails.web.pages.SitemeshPreprocessor
import org.codehaus.groovy.grails.web.sitemesh.GSPSitemeshPage
import org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter
import org.codehaus.groovy.grails.web.util.StreamCharBuffer
import org.codehaus.groovy.runtime.InvokerHelper
import com.opensymphony.module.sitemesh.RequestConstants
/**
* Internal Sitemesh pre-processor tags.
*
* @author Graeme Rocher
* @since 1.2
*/
@Artefact("TagLibrary")
class SitemeshTagLib implements RequestConstants {
static namespace = 'sitemesh'
@CompileStatic
def captureTagContent(Writer writer, String tagname, Map attrs, Object body, boolean noEndTagForEmpty=false) {
def content = null
if (body != null) {
if (body instanceof Closure) {
content = body()
}
else {
content = body
}
}
if (content instanceof StreamCharBuffer) {
content.setPreferSubChunkWhenWritingToOtherBuffer(true)
}
writer << '<'
writer << tagname
def useXmlClosingForEmptyTag = false
if (attrs) {
def xmlClosingString = attrs.remove(SitemeshPreprocessor.XML_CLOSING_FOR_EMPTY_TAG_ATTRIBUTE_NAME)
if (xmlClosingString=='/') {
useXmlClosingForEmptyTag = true
}
attrs.each { k, v ->
writer << ' '
writer << k
writer << '="'
writer << InvokerHelper.invokeMethod(v.toString(), "encodeAsHTML", null)
writer << '"'
}
}
if (content) {
writer << '>'
// the following row must be written separately (append StreamCharBuffer gets appended as subchunk)
writer << content
writer << '</'
writer << tagname
writer << '>'
}
else {
if (!useXmlClosingForEmptyTag) {
writer << '>'
// in valid HTML , closing of an empty tag depends on the element name
// for empty title, the tag must be closed properly
// for empty meta tag shouldn't be closed at all, see GRAILS-5696
if (!noEndTagForEmpty) {
writer << '</'
writer << tagname
writer << '>'
}
}
else {
// XML / XHTML empty tag
writer << '/>'
}
}
content
}
@CompileStatic
def StreamCharBuffer wrapContentInBuffer(Object content) {
if (content instanceof Closure) {
content = content()
}
if (!(content instanceof StreamCharBuffer)) {
// the body closure might be a string constant, so wrap it in a StreamCharBuffer in that case
def newbuffer = new StreamCharBuffer()
newbuffer.setPreferSubChunkWhenWritingToOtherBuffer(true)
InvokerHelper.write(newbuffer.writer, content)
return newbuffer
} else {
return (StreamCharBuffer)content
}
}
/**
* Captures the <head> tag.
*/
Closure captureHead = { attrs, body ->
def content = captureTagContent(out, 'head', attrs, body)
if (content != null) {
GSPSitemeshPage smpage=request[GrailsPageFilter.GSP_SITEMESH_PAGE]
if (smpage) {
smpage.setHeadBuffer(wrapContentInBuffer(content))
}
}
}
/**
* Allows passing of parameters to Sitemesh layout.<br/>
*
* <sitemesh:parameter name="foo" value="bar" />
*/
Closure parameter = { attrs, body ->
GSPSitemeshPage smpage=request[GrailsPageFilter.GSP_SITEMESH_PAGE]
def name = attrs.name?.toString()
def val = attrs.value?.toString()
if (smpage && name && val != null) {
smpage.addProperty("page.$name", val)
}
}
/**
* Captures the <body> tag.
*/
Closure captureBody = { attrs, body ->
def content = captureTagContent(out, 'body', attrs, body)
if (content != null) {
GSPSitemeshPage smpage = request[GrailsPageFilter.GSP_SITEMESH_PAGE]
if (smpage) {
smpage.setBodyBuffer(wrapContentInBuffer(content))
if (attrs) {
attrs.each { k, v ->
smpage.addProperty("body.${k.toLowerCase()}", v?.toString())
}
}
}
}
}
/**
* Captures the individual <content> tags.
*/
Closure captureContent = { attrs, body ->
if (body != null) {
GSPSitemeshPage smpage=request[GrailsPageFilter.GSP_SITEMESH_PAGE]
if (smpage && attrs.tag) {
smpage.setContentBuffer(attrs.tag, wrapContentInBuffer(body))
}
}
}
/**
* Captures the individual <meta> tags.
*/
Closure captureMeta = { attrs, body ->
def content = captureTagContent(out, 'meta', attrs, body, true)
GSPSitemeshPage smpage = request[GrailsPageFilter.GSP_SITEMESH_PAGE]
def val = attrs.content?.toString()
if (attrs && smpage && val != null) {
if (attrs.name) {
smpage.addProperty("meta.${attrs.name}", val)
smpage.addProperty("meta.${attrs.name.toLowerCase()}", val)
}
else if (attrs['http-equiv']) {
smpage.addProperty("meta.http-equiv.${attrs['http-equiv']}", val)
smpage.addProperty("meta.http-equiv.${attrs['http-equiv'].toLowerCase()}", val)
smpage.addProperty("meta.http-equiv.${WordUtils.capitalize(attrs['http-equiv'],['-'] as char[])}", val)
}
}
}
/**
* Captures the <title> tag.
*/
Closure captureTitle = { attrs, body ->
GSPSitemeshPage smpage = request[GrailsPageFilter.GSP_SITEMESH_PAGE]
def content = captureTagContent(out, 'title', attrs, body)
if (smpage && content != null) {
smpage.addProperty('title', content?.toString())
smpage.setTitleCaptured(true)
}
}
/**
* Wraps the title tag so that the buffer can be cleared out from the head buffer
*/
Closure wrapTitleTag = { attrs, body ->
if (body != null) {
GSPSitemeshPage smpage=request[GrailsPageFilter.GSP_SITEMESH_PAGE]
if (smpage) {
def wrapped = wrapContentInBuffer(body)
smpage.setTitleBuffer(wrapped)
out << wrapped
} else {
out << body()
}
}
}
}