Skip to content

Commit

Permalink
Add support for parsing properly <template> elements. Closes Grapes…
Browse files Browse the repository at this point in the history
  • Loading branch information
artf committed May 21, 2024
1 parent 88c6d22 commit a7d4942
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/parser/model/ParserHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ const ParserHtml = (em?: EditorModel, config: ParserConfig & { returnArray?: boo
},

parseNode(node: HTMLElement, opts: ParseNodeOptions = {}) {
const nodes = node.childNodes;
const nodes = (node as HTMLTemplateElement).content?.childNodes || node.childNodes;
const nodesLen = nodes.length;
let model = this.detectNode(node, opts);

Expand Down Expand Up @@ -267,7 +267,7 @@ const ParserHtml = (em?: EditorModel, config: ParserConfig & { returnArray?: boo
*/
parseNodes(el: HTMLElement, opts: ParseNodeOptions = {}) {
const result: ComponentDefinitionDefined[] = [];
const nodes = el.childNodes;
const nodes = (el as HTMLTemplateElement).content?.childNodes || el.childNodes;
const nodesLen = nodes.length;

for (let i = 0; i < nodesLen; i++) {
Expand Down
28 changes: 28 additions & 0 deletions test/specs/parser/model/ParserHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,34 @@ describe('ParserHtml', () => {
expect(obj.parse(str).html).toEqual(result);
});

test('<template> with content is properly parsed', () => {
const str = `<template class="test">
<tr>
<td>Cell</td>
</tr>
</template>`;
const result = [
{
tagName: 'template',
classes: ['test'],
components: [
{
type: 'row',
tagName: 'tr',
components: [
{
type: 'cell',
tagName: 'td',
components: { type: 'textnode', content: 'Cell' },
},
],
},
],
},
];
expect(obj.parse(str).html).toEqual(result);
});

describe('Options', () => {
test('Remove unsafe attributes', () => {
const str = '<img src="path/img" data-test="1" class="test" onload="unsafe"/>';
Expand Down

0 comments on commit a7d4942

Please sign in to comment.