Skip to content

Commit 0ed23f5

Browse files
committed
Comment and raw support, better handling for boolean and falsy attributes
1 parent e30a945 commit 0ed23f5

2 files changed

Lines changed: 39 additions & 10 deletions

File tree

modules/@apostrophecms/module/index.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ module.exports = {
349349
// ```
350350
// methods(self) {
351351
// return {
352-
// myMethod() {
352+
// myMethod(req) {
353353
// return [
354354
// {
355355
// name: 'meta',
@@ -360,26 +360,41 @@ module.exports = {
360360
// }
361361
// ];
362362
// },
363-
// anotherMethod() {
363+
// anotherMethod(req) {
364364
// return [
365365
// {
366366
// tag: 'h4',
367367
// body: [
368368
// {
369+
// comment: 'Start Heading text'
370+
// },
371+
// {
369372
// text: 'Heading text'
370373
// }
374+
// {
375+
// comment: 'End Heading text'
376+
// }
377+
// {
378+
// name: 'script`,
379+
// body: [
380+
// {
381+
// raw: 'console.log("This is not escaped, be careful!");'
382+
// }
383+
// ]
384+
// }
371385
// ]
372386
// }
373387
// ];
374388
// }
375389
// };
376390
// }
377391
// ```
378-
// Node objects SHOULD have either `name` or `text` property.
379-
// A node with `name` property can have `attrs` (array of element attributes)
380-
// and/or `body` (array of child nodes).
381-
// `text` nodes are rendered as text (no HTML tags), they don't support
382-
// any other properties.
392+
// Node object SHOULD have either `name`, `text`, `raw` or `comment` property.
393+
// A node with `name` can have `attrs` (array of element attributes)
394+
// and `body` (array of child nodes, recursion).
395+
// `text` nodes are rendered as text (no HTML tags), the value is always a string.
396+
// `comment` nodes are rendered as HTML comments, the value is always a string.
397+
// `raw` nodes are rendered as is, no escaping, the value is always a string.
383398
prependNodes(location, method) {
384399
return self.apos.template
385400
.prependNodes(location, self.__meta.name, method);

modules/@apostrophecms/template/index.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,10 +1136,12 @@ module.exports = {
11361136
// attrs: { href: '/some/path', rel: 'stylesheet' }
11371137
// }
11381138
// ]
1139-
// Node objects SHOULD have either `name` or `text` property.
1139+
// Node object SHOULD have either `name`, `text`, `raw` or `comment` property.
11401140
// A node with `name` can have `attrs` (array of element attributes)
1141-
// and `body` (array of child nodes).
1142-
// `text` nodes are rendered as text (no HTML tags).
1141+
// and `body` (array of child nodes, recursion).
1142+
// `text` nodes are rendered as text (no HTML tags), the value is always a string.
1143+
// `comment` nodes are rendered as HTML comments, the value is always a string.
1144+
// `raw` nodes are rendered as is, no escaping, the value is always a string.
11431145
renderNodes(nodes) {
11441146
if (!Array.isArray(nodes)) {
11451147
self.logError(
@@ -1152,10 +1154,22 @@ module.exports = {
11521154
if (node.text) {
11531155
return self.apos.util.escapeHtml(node.text);
11541156
}
1157+
if (node.comment) {
1158+
return `\n<!-- ${self.apos.util.escapeHtml(node.comment)} -->\n`;
1159+
}
1160+
if (node.raw) {
1161+
return node.raw;
1162+
}
11551163
if (node.name) {
11561164
const name = self.apos.util.escapeHtml(node.name);
11571165
const attrs = Object.entries(node.attrs || {})
11581166
.map(([ key, value ]) => {
1167+
if (value === false || value === null || value === undefined) {
1168+
return '';
1169+
}
1170+
if (value === true) {
1171+
return ` ${self.apos.util.escapeHtml(key)}`;
1172+
}
11591173
return ` ${self.apos.util.escapeHtml(key)}="${self.apos.util.escapeHtml(value)}"`;
11601174
})
11611175
.join('')

0 commit comments

Comments
 (0)