@@ -2,7 +2,6 @@ import Renderer from '../Renderer';
22import Block from '../Block' ;
33import Tag from './shared/Tag' ;
44import Wrapper from './shared/Wrapper' ;
5- import deindent from '../../utils/deindent' ;
65import MustacheTag from '../../nodes/MustacheTag' ;
76import RawMustacheTag from '../../nodes/RawMustacheTag' ;
87
@@ -19,95 +18,47 @@ export default class RawMustacheTagWrapper extends Tag {
1918 this . cannot_use_innerhtml ( ) ;
2019 }
2120
22- render ( block : Block , parent_node : string , parent_nodes : string ) {
23- const name = this . var ;
24-
21+ render ( block : Block , parent_node : string , _parent_nodes : string ) {
2522 const in_head = parent_node === '@_document.head' ;
26- const needs_anchors = ! parent_node || in_head ;
27-
28- // if in head always needs anchors
29- if ( in_head ) {
30- this . prev = null ;
31- this . next = null ;
32- }
3323
34- // TODO use is_dom_node instead of type === 'Element'?
35- const needs_anchor_before = this . prev ? this . prev . node . type !== 'Element' : needs_anchors ;
36- const needs_anchor_after = this . next ? this . next . node . type !== 'Element' : needs_anchors ;
24+ const can_use_innerhtml = ! in_head && parent_node && ! this . prev && ! this . next ;
3725
38- const anchor_before = needs_anchor_before
39- ? block . get_unique_name ( `${ name } _before` )
40- : ( this . prev && this . prev . var ) || 'null' ;
26+ if ( can_use_innerhtml ) {
27+ const insert = content => `${ parent_node } .innerHTML = ${ content } ;` ;
4128
42- const anchor_after = needs_anchor_after
43- ? block . get_unique_name ( `${ name } _after` )
44- : ( this . next && this . next . var ) || 'null' ;
45-
46- let detach : string ;
47- let insert : ( content : string ) => string ;
48- let use_innerhtml = false ;
29+ const { init } = this . rename_this_method (
30+ block ,
31+ content => insert ( content )
32+ ) ;
4933
50- if ( anchor_before === 'null' && anchor_after === 'null' ) {
51- use_innerhtml = true ;
52- detach = `${ parent_node } .innerHTML = '';` ;
53- insert = content => `${ parent_node } .innerHTML = ${ content } ;` ;
54- } else if ( anchor_before === 'null' ) {
55- detach = `@detach_before(${ anchor_after } );` ;
56- insert = content => `${ anchor_after } .insertAdjacentHTML("beforebegin", ${ content } );` ;
57- } else if ( anchor_after === 'null' ) {
58- detach = `@detach_after(${ anchor_before } );` ;
59- insert = content => `${ anchor_before } .insertAdjacentHTML("afterend", ${ content } );` ;
60- } else {
61- detach = `@detach_between(${ anchor_before } , ${ anchor_after } );` ;
62- insert = content => `${ anchor_before } .insertAdjacentHTML("afterend", ${ content } );` ;
34+ block . builders . mount . add_line ( insert ( init ) ) ;
6335 }
6436
65- const { init } = this . rename_this_method (
66- block ,
67- content => deindent `
68- ${ ! use_innerhtml && detach }
69- ${ insert ( content ) }
70- `
71- ) ;
37+ else {
38+ const needs_anchor = in_head || ( this . next && ! this . next . is_dom_node ( ) ) ;
7239
73- // we would have used comments here, but the `insertAdjacentHTML` api only
74- // exists for `Element`s.
75- if ( needs_anchor_before ) {
76- block . add_element (
77- anchor_before ,
78- `@element('noscript')` ,
79- parent_nodes && `@element('noscript')` ,
80- parent_node ,
81- true
82- ) ;
83- }
40+ const html_tag = block . get_unique_name ( 'html_tag' ) ;
41+ const html_anchor = needs_anchor && block . get_unique_name ( 'html_anchor' ) ;
42+
43+ block . add_variable ( html_tag ) ;
8444
85- function add_anchor_after ( ) {
86- block . add_element (
87- anchor_after ,
88- `@element('noscript')` ,
89- parent_nodes && `@element('noscript')` ,
90- parent_node
45+ const { init } = this . rename_this_method (
46+ block ,
47+ content => `${ html_tag } .p(${ content } );`
9148 ) ;
92- }
9349
94- if ( needs_anchor_after && anchor_before === 'null' ) {
95- // anchor_after needs to be in the DOM before we
96- // insert the HTML...
97- add_anchor_after ( ) ;
98- }
50+ const anchor = in_head ? 'null' : needs_anchor ? html_anchor : this . next ? this . next . var : 'null' ;
9951
100- block . builders . mount . add_line ( insert ( init ) ) ;
52+ block . builders . hydrate . add_line ( `${ html_tag } = new @HtmlTag(${ init } , ${ anchor } );` ) ;
53+ block . builders . mount . add_line ( `${ html_tag } .m(${ parent_node || '#target' } , anchor);` ) ;
10154
102- if ( needs_anchors ) {
103- block . builders . destroy . add_conditional ( 'detaching' , needs_anchor_before
104- ? `${ detach } \n@detach(${ anchor_before } );`
105- : detach ) ;
106- }
55+ if ( needs_anchor ) {
56+ block . add_element ( html_anchor , '@empty()' , '@empty()' , parent_node ) ;
57+ }
10758
108- if ( needs_anchor_after && anchor_before !== 'null' ) {
109- // ...otherwise it should go afterwards
110- add_anchor_after ( ) ;
59+ if ( ! parent_node || in_head ) {
60+ block . builders . destroy . add_conditional ( 'detaching' , ` ${ html_tag } .d();` ) ;
61+ }
11162 }
11263 }
11364}
0 commit comments