Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runtime): support insertAdjacentHTML #9596

Merged
merged 6 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions packages/taro-runtime/src/__tests__/dom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,30 @@ describe('DOM', () => {
container.textContent = ''
expect(container.hasChildNodes()).toBe(false)
})

it('insertAdjacentHTML', () => {
const container = document.createElement('container')
const div = document.createElement('div')
const text = document.createTextNode('text')
container.appendChild(div)
container.firstChild.appendChild(text)

const divBeforeInsert = container.firstChild
divBeforeInsert.insertAdjacentHTML('beforebegin', '<view />')
divBeforeInsert.insertAdjacentHTML('afterbegin', '<button />')
divBeforeInsert.insertAdjacentHTML('beforeend', '<input />')
divBeforeInsert.insertAdjacentHTML('afterend', '<image />')

expect(container.childNodes.length).toBe(3)
expect(container.childNodes[0].nodeName).toBe('view')
expect(container.childNodes[1].nodeName).toBe('div')
expect(container.childNodes[2].nodeName).toBe('image')

const divAfterInsert = container.childNodes[1]
expect(divAfterInsert.childNodes.length).toBe(3)
expect(divAfterInsert.firstChild.nodeName).toBe('button')
expect(divAfterInsert.lastChild.nodeName).toBe('input')
})
})

describe('text', () => {
Expand Down
35 changes: 35 additions & 0 deletions packages/taro-runtime/src/dom/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Shortcuts, ensure } from '@tarojs/shared'
import { hydrate, HydratedData } from '../hydrate'
import { TaroElement } from './element'
import { setInnerHTML } from './html/html'
import { parser } from './html/parser'
import { CurrentReconciler } from '../reconciler'
import { document } from '../bom/document'

Expand Down Expand Up @@ -216,6 +217,40 @@ export class TaroNode extends TaroEventTarget {
return ''
}

/**
* An implementation of `Element.insertAdjacentHTML()`
* to support Vue 3 with a version of or greater than `vue@3.1.2`
*/
public insertAdjacentHTML (
position: 'beforebegin' | 'afterbegin' | 'beforeend' | 'afterend',
html: string
) {
const parsedNodes = parser(html)

for (let i = 0; i < parsedNodes.length; i++) {
const n = parsedNodes[i]

switch (position) {
case 'beforebegin':
this.parentNode?.insertBefore(n, this)
break
case 'afterbegin':
if (this.hasChildNodes()) {
this.childNodes[0].insertBefore(n)
Copy link
Contributor

@Chen-jj Chen-jj Jul 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@b2nil 这里应该是 this.insertBefore(n, this.childNodes[0]) 吧,但我没搞懂为什么测试能过。

现在的测试运行结果:

<container>
  <view></view>
  <div>
    <text>
      <button></button>
    </text>
    <input />
  </div>
  <image></image>
</container>

正确的运行结果:

<container>
  <view></view>
  <div>
    <button></button>
    <text></text>
    <input />
  </div>
  <image></image>
</container>

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

尴尬,的确插错位置了。
抱歉,我没有在本地跑测试,纯粹依赖 CI 了。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

仔细看了几个 PR 的 CI 测试日志,虽然 scope 里有 @tarojs/runtime, 但是都没有执行 ci 脚本的记录。

插入位置错误的地方,你看是需要我再提 PR 改,还是你直接改?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@b2nil 我这边改下哈,另外目测是因为 @tarojs/runtime 跑测试的 npm script 和 yarn test 跑的不一致。

} else {
this.appendChild(n)
}
break
case 'beforeend':
this.appendChild(n)
break
case 'afterend':
this.parentNode?.appendChild(n)
break
}
}
}

protected findIndex (childeNodes: TaroNode[], refChild: TaroNode) {
const index = childeNodes.indexOf(refChild)
ensure(index !== -1, 'The node to be replaced is not a child of this node.')
Expand Down