|
1 | 1 | import type Application from '@ember/application'; |
2 | 2 |
|
3 | | -import { serializeBuilder } from '@glimmer/node'; |
4 | 3 | // @ts-expect-error - glimmer internals not typed for direct import |
5 | 4 | import { clientBuilder, rehydrationBuilder } from '@glimmer/runtime'; |
| 5 | +// @ts-expect-error - glimmer internals not typed for direct import |
| 6 | +import { ConcreteBounds, NewElementBuilder } from '@glimmer/runtime'; |
6 | 7 |
|
7 | 8 | declare const FastBoot: unknown; |
8 | 9 |
|
| 10 | +// Inlined from @glimmer/node to avoid pulling in a second copy of |
| 11 | +// @glimmer/runtime (and its transitive @glimmer/global-context) which |
| 12 | +// would cause webpack to bundle two uninitialised copies and break at |
| 13 | +// runtime with "scheduleDestroyed is not a function". |
| 14 | +const NEEDS_EXTRA_CLOSE = new WeakMap(); |
| 15 | + |
| 16 | +class SerializeBuilder extends (NewElementBuilder as any) { |
| 17 | + serializeBlockDepth = 0; |
| 18 | + |
| 19 | + __openBlock() { |
| 20 | + let { tagName } = this.element; |
| 21 | + if (tagName !== 'TITLE' && tagName !== 'SCRIPT' && tagName !== 'STYLE') { |
| 22 | + let depth = this.serializeBlockDepth++; |
| 23 | + this.__appendComment(`%+b:${depth}%`); |
| 24 | + } |
| 25 | + super.__openBlock(); |
| 26 | + } |
| 27 | + |
| 28 | + __closeBlock() { |
| 29 | + let { tagName } = this.element; |
| 30 | + super.__closeBlock(); |
| 31 | + if (tagName !== 'TITLE' && tagName !== 'SCRIPT' && tagName !== 'STYLE') { |
| 32 | + let depth = --this.serializeBlockDepth; |
| 33 | + this.__appendComment(`%-b:${depth}%`); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + __appendHTML(html: string) { |
| 38 | + let { tagName } = this.element; |
| 39 | + if (tagName === 'TITLE' || tagName === 'SCRIPT' || tagName === 'STYLE') { |
| 40 | + return super.__appendHTML(html); |
| 41 | + } |
| 42 | + let first = this.__appendComment('%glmr%'); |
| 43 | + if (tagName === 'TABLE') { |
| 44 | + let openIndex = html.indexOf('<'); |
| 45 | + if (openIndex > -1 && html.slice(openIndex + 1, openIndex + 3) === 'tr') { |
| 46 | + html = `<tbody>${html}</tbody>`; |
| 47 | + } |
| 48 | + } |
| 49 | + if (html === '') { |
| 50 | + this.__appendComment('% %'); |
| 51 | + } else { |
| 52 | + super.__appendHTML(html); |
| 53 | + } |
| 54 | + let last = this.__appendComment('%glmr%'); |
| 55 | + return new (ConcreteBounds as any)(this.element, first, last); |
| 56 | + } |
| 57 | + |
| 58 | + __appendText(string: string) { |
| 59 | + let { tagName } = this.element; |
| 60 | + let current = ((): any => { |
| 61 | + let { element, nextSibling } = this as any; |
| 62 | + return nextSibling === null ? element.lastChild : nextSibling.previousSibling; |
| 63 | + })(); |
| 64 | + if (tagName === 'TITLE' || tagName === 'SCRIPT' || tagName === 'STYLE') { |
| 65 | + return super.__appendText(string); |
| 66 | + } |
| 67 | + if (string === '') { |
| 68 | + return this.__appendComment('% %'); |
| 69 | + } |
| 70 | + if (current && current.nodeType === 3) { |
| 71 | + this.__appendComment('%|%'); |
| 72 | + } |
| 73 | + return super.__appendText(string); |
| 74 | + } |
| 75 | + |
| 76 | + closeElement() { |
| 77 | + if (NEEDS_EXTRA_CLOSE.has(this.element)) { |
| 78 | + NEEDS_EXTRA_CLOSE.delete(this.element); |
| 79 | + super.closeElement(); |
| 80 | + } |
| 81 | + return super.closeElement(); |
| 82 | + } |
| 83 | + |
| 84 | + openElement(tag: string) { |
| 85 | + if ( |
| 86 | + tag === 'tr' && |
| 87 | + this.element.tagName !== 'TBODY' && |
| 88 | + this.element.tagName !== 'THEAD' && |
| 89 | + this.element.tagName !== 'TFOOT' |
| 90 | + ) { |
| 91 | + this.openElement('tbody'); |
| 92 | + NEEDS_EXTRA_CLOSE.set(this.constructing, true); |
| 93 | + this.flushElement(null); |
| 94 | + } |
| 95 | + return super.openElement(tag); |
| 96 | + } |
| 97 | + |
| 98 | + pushRemoteElement(element: any, cursorId: string, insertBefore: any = null) { |
| 99 | + let { dom } = this as any; |
| 100 | + let script = dom.createElement('script'); |
| 101 | + script.setAttribute('glmr', cursorId); |
| 102 | + dom.insertBefore(element, script, insertBefore); |
| 103 | + return super.pushRemoteElement(element, cursorId, insertBefore); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +function serializeBuilder(env: any, cursor: any) { |
| 108 | + return SerializeBuilder.forInitialRender(env, cursor); |
| 109 | +} |
| 110 | + |
9 | 111 | export function initialize(application: Application): void { |
10 | 112 | // Don't override in FastBoot (server-side) — let Ember's default serialize mode work |
11 | 113 | if (typeof FastBoot !== 'undefined') { |
|
0 commit comments