Skip to content

Commit

Permalink
fix tests, handle Comment
Browse files Browse the repository at this point in the history
  • Loading branch information
dummdidumm committed Apr 3, 2023
1 parent 56ce621 commit 718a25b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 32 deletions.
6 changes: 6 additions & 0 deletions src/compiler/compile/render_dom/wrappers/Comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ export default class CommentWrapper extends Wrapper {
parent_node
);
}

text() {
if (!this.renderer.options.preserveComments) return '';

return `<!--${this.node.data}-->`;
}
}
9 changes: 6 additions & 3 deletions src/compiler/compile/render_dom/wrappers/Element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import is_dynamic from '../shared/is_dynamic';
import { is_name_contenteditable, has_contenteditable_attr } from '../../../utils/contenteditable';
import create_debugging_comment from '../shared/create_debugging_comment';
import { push_array } from '../../../../utils/push_array';
import CommentWrapper from '../Comment';

interface BindingGroup {
events: string[];
Expand Down Expand Up @@ -499,7 +500,7 @@ export default class ElementWrapper extends Wrapper {
};

const can_use_raw_text = !this.node.can_use_innerhtml && can_use_textcontent;
to_html((this.fragment.nodes as unknown as Array<ElementWrapper | TextWrapper>), block, literal, state, can_use_raw_text);
to_html((this.fragment.nodes as unknown as Array<ElementWrapper | CommentWrapper | TextWrapper>), block, literal, state, can_use_raw_text);
literal.quasis.push(state.quasi as any);

if (hydratable) {
Expand Down Expand Up @@ -1250,9 +1251,11 @@ export default class ElementWrapper extends Wrapper {
const regex_backticks = /`/g;
const regex_dollar_signs = /\$/g;

function to_html(wrappers: Array<ElementWrapper | TextWrapper | MustacheTagWrapper | RawMustacheTagWrapper>, block: Block, literal: any, state: any, can_use_raw_text?: boolean) {
function to_html(wrappers: Array<CommentWrapper | ElementWrapper | TextWrapper | MustacheTagWrapper | RawMustacheTagWrapper>, block: Block, literal: any, state: any, can_use_raw_text?: boolean) {
wrappers.forEach(wrapper => {
if (wrapper instanceof TextWrapper) {
if (wrapper instanceof CommentWrapper) {
state.quasi.value.raw += wrapper.text();
} else if (wrapper instanceof TextWrapper) {
// Don't add the <pre>/<textarea> newline logic here because pre/textarea.innerHTML
// would keep the leading newline, too, only someParent.innerHTML = '..<pre/textarea>..' won't

Expand Down
15 changes: 0 additions & 15 deletions test/hydration/samples/claim-comment/_config.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
export default {
compileOptions: {
preserveComments:true
},
snapshot(target) {
const div = target.querySelector('div');

return {
div,
comment: div.childNodes[0]
};
},

test(assert, target, snapshot) {
const div = target.querySelector('div');
assert.equal(div, snapshot.div);
assert.equal(div.childNodes[0], snapshot.comment);
assert.equal(div.childNodes[1].nodeType, 8);
}
};
15 changes: 1 addition & 14 deletions test/runtime/samples/raw-anchor-first-last-child/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,11 @@ export default {
raw: '<span>foo</span>'
},

snapshot(target) {
const span = target.querySelector('span');

return {
span
};
},

test({ assert, component, target, snapshot }) {
test({ assert, component, target }) {
const span = target.querySelector('span');
assert.ok(!span.previousSibling);
assert.ok(!span.nextSibling);

if (snapshot) {
assert.equal(span, snapshot.span);
}

component.raw = '<span>bar</span>';
assert.htmlEqual(target.innerHTML, '<div><span>bar</span></div>');
}
};

0 comments on commit 718a25b

Please sign in to comment.