All files / runtime/src nodes.ts

92.16% Statements 376/408
95.45% Branches 63/66
100% Functions 35/35
92.16% Lines 376/408

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 4091x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 115x 115x 115x 1x 1x 115x 115x 115x 115x 38x 38x 115x 19x 19x 115x 115x 115x 1x 1x 30x 4x 4x 30x 30x 30x 30x 30x 30x 3x 3x 23x 23x 23x 1x 1x 9x 4x 4x 9x 9x 9x 9x 9x 9x 3x 3x 2x 2x 2x 1x 1x 41x 41x 1x 1x 6x 6x 6x 6x 1x 1x 1x 1x 94x 1x 1x 1x 94x 94x 94x 94x 94x 94x 94x 1x 1x 32x 32x 1x 1x 221x 221x 1x 1x 6x 6x 1x 1x 5x 5x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 12x     12x 3x 3x 3x 2x 2x 2x 3x 12x 12x 1x 1x 4x 4x 1x 1x 20x 20x 1x 1x 51x 51x 1x 1x 67x 67x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 1x 1x 11x 11x 11x 11x 11x 11x 1x 1x 11x 11x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 1x 1x 46x 46x 46x 46x 46x 1x 1x 3x 3x 3x 2x 2x 2x 2x 2x     2x 3x 1x 1x 1x 56x 39x 50x 17x 17x 56x 1x 1x 17x 17x 17x 17x 17x 28x 17x 17x 17x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 18x 18x 18x 18x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 1x 2x 2x                                                         2x  
import {
  getViewClass,
  getViewMeta,
  normalizeElementName,
  NSVViewMeta
} from './registry'
import { ELEMENT_REF } from '@nativescript-vue/runtime'
import { debug } from '@nativescript-vue/shared'
import { ViewBase, LayoutBase, Style } from '@nativescript/core'
import { unsetValue } from '@nativescript/core'
import set from 'set-value'
// import unset from 'unset-value'
 
// import {isContentView, isLayout} from "./index";
 
export const enum NSVNodeTypes {
  TEXT = 'text',
  ELEMENT = 'element',
  COMMENT = 'comment',
  ROOT = 'root'
}
 
// View Flags indicate the kind of view the element is
// this avoids extra checks during runtime to determine
// the method to use for adding/removing child nodes
//
export const enum NSVViewFlags {
  NONE = 0,
  SKIP_ADD_TO_DOM = 1 << 0,
  CONTENT_VIEW = 1 << 1,
  LAYOUT_VIEW = 1 << 2,
  NO_CHILDREN = 1 << 3
}
 
export interface INSVNode {
  nodeId: number
  nodeType: NSVNodeTypes
  text: string | undefined
 
  parentNode: INSVElement | null
 
  childNodes: INSVNode[]
  firstChild: INSVNode | null
  lastChild: INSVNode | null
  prevSibling: INSVNode | null
  nextSibling: INSVNode | null
}
 
export interface INSVElement extends INSVNode {
  tagName: string
  meta: NSVViewMeta
  style: Style | string
 
  addEventListener(
    event: string,
    handler: any,
    options: AddEventListenerOptions
  ): void
 
  removeEventListener(event: string, handler?: any): void
 
  dispatchEvent(event: string): void
 
  nativeView: (ViewBase | LayoutBase) & { [ELEMENT_REF]: INSVElement }
 
  getAttribute(name: string): unknown
 
  setAttribute(name: string, value: unknown): void
 
  removeAttribute(name: string): void
 
  insertBefore(el: INSVNode, anchor?: INSVNode | null): void
 
  appendChild(el: INSVNode): void
 
  removeChild(el: INSVNode): void
}
 
let nodeId = 0
 
export abstract class NSVNode implements INSVNode {
  protected constructor(nodeType: NSVNodeTypes) {
    this.nodeType = nodeType
    this.nodeId = nodeId++
  }
 
  nodeId: number
  nodeType: NSVNodeTypes
 
  private _text?: string | undefined
  public get text(): string | undefined {
    return this._text
  }
  public set text(value: string | undefined) {
    this._text = value
  }
 
  parentNode: INSVElement | null = null
  childNodes: INSVNode[] = []
 
  get nextSibling(): INSVNode | null {
    if (!this.parentNode) {
      return null
    }
 
    const selfIndex = this.parentNode.childNodes.findIndex(
      n => n.nodeId === this.nodeId
    )
 
    if (selfIndex > -1 && selfIndex < this.parentNode.childNodes.length - 1) {
      return this.parentNode.childNodes[selfIndex + 1]
    }
 
    return null
  }
 
  get prevSibling(): INSVNode | null {
    if (!this.parentNode) {
      return null
    }
 
    const selfIndex = this.parentNode.childNodes.findIndex(
      n => n.nodeId === this.nodeId
    )
 
    if (selfIndex > 0) {
      return this.parentNode.childNodes[selfIndex - 1]
    }
 
    return null
  }
 
  get firstChild() {
    return this.childNodes.length ? this.childNodes[0] : null
  }
 
  get lastChild() {
    return this.childNodes.length
      ? this.childNodes[this.childNodes.length - 1]
      : null
  }
}
 
export class NSVElement extends NSVNode implements INSVElement {
  private readonly _tagName: string
  private readonly _nativeView: any
  private _meta: NSVViewMeta | undefined
 
  constructor(tagName: string) {
    super(NSVNodeTypes.ELEMENT)
 
    this._tagName = normalizeElementName(tagName)
    const viewClass = getViewClass(tagName)
    this._nativeView = new viewClass()
    this._nativeView[ELEMENT_REF] = this
  }
 
  get tagName(): string {
    return this._tagName
  }
 
  get nativeView() {
    return this._nativeView
  }
 
  get style(): Style | string {
    return this.nativeView.style
  }
 
  set style(inlineStyle: Style | string) {
    this.nativeView.style = inlineStyle
  }
 
  get text(): string | undefined {
    return this.nativeView.text
  }
 
  set text(t: string | undefined) {
    this.nativeView.text = t
  }
 
  get meta() {
    if (this._meta) {
      return this._meta
    }
 
    return (this._meta = getViewMeta(this.tagName))
  }
 
  addEventListener(
    event: string,
    handler: any,
    options: AddEventListenerOptions = {}
  ) {
    const { capture, once } = options
    if (capture) {
      debug('Bubble propagation is not supported')
    }
    if (once) {
      const oldHandler = handler
      const self = this
      handler = (...args: any) => {
        oldHandler.call(null, ...args)
        self.removeEventListener(event, handler)
      }
    }
    this.nativeView.addEventListener(event, handler)
  }
 
  removeEventListener(event: string, handler?: any) {
    this.nativeView.removeEventListener(event, handler)
  }
 
  dispatchEvent(event: string) {
    this.nativeView.notify({ eventName: event, object: this.nativeView })
  }
 
  getAttribute(name: string): unknown {
    return this.nativeView[name]
  }
 
  setAttribute(name: string, value: unknown) {
    set(this.nativeView, name, value)
  }
 
  removeAttribute(name: string) {
    // potential issue: unsetValue is an empty object
    // not all properties/attributes may know/check for this
    set(this.nativeView, name, unsetValue)
    // originally we deleted the property, but in case of built-in properties
    // this would break them. For example, deleting the padding property
    // will prevent us from changing the padding once we deleted it
    // that's not the expected behaviour.
    // unset(this.nativeView, name)
  }
 
  insertBefore(el: INSVNode, anchor?: INSVNode | null) {
    if (!anchor) {
      return this.appendChild(el)
    }
 
    const refIndex = this.childNodes.findIndex(
      node => node.nodeId === anchor.nodeId
    )
 
    if (refIndex === -1) {
      return this.appendChild(el)
    }
 
    if (el.parentNode) {
      el.parentNode.removeChild(el)
    }
 
    this.childNodes.splice(refIndex, 0, el)
    el.parentNode = this
 
    // find index to use for the native view, since non-visual nodes
    // (comment/text don't exist in the native view hierarchy)
    // todo: potentially refactor based on my benchmark:
    // https://www.measurethat.net/Benchmarks/Show/7450/0/filter-findindex
    const trueIndex = this.childNodes
      .filter(node => node.nodeType === NSVNodeTypes.ELEMENT)
      .findIndex(node => node.nodeId === el.nodeId)
 
    this.addChild(el, trueIndex)
  }
 
  appendChild(el: INSVNode) {
    this.childNodes.push(el)
    el.parentNode = this
 
    this.addChild(el)
  }
 
  removeChild(el: INSVNode) {
    const index = this.childNodes.findIndex(node => node.nodeId === el.nodeId)
 
    if (index > -1) {
      this.childNodes.splice(index, 1)
      el.parentNode = null
      if (el.nodeType === NSVNodeTypes.ELEMENT) {
        removeChild(el as NSVElement, this)
      } else if (el.nodeType === NSVNodeTypes.TEXT) {
        this.updateText()
      }
    }
  }
 
  // abstracted from appendChild, and insertBefore to avoid code duplication
  private addChild(el: INSVNode, atIndex?: number): void {
    if (el.nodeType === NSVNodeTypes.ELEMENT) {
      addChild(el as NSVElement, this, atIndex)
    } else if (el.nodeType === NSVNodeTypes.TEXT) {
      this.updateText()
    }
  }
 
  updateText() {
    this.setAttribute(
      'text',
      this.childNodes
        .filter(node => node.nodeType === NSVNodeTypes.TEXT)
        .reduce((text: string, currentNode) => {
          return text + currentNode.text
        }, '')
    )
  }
}
 
export class NSVComment extends NSVNode {
  constructor(text: string) {
    super(NSVNodeTypes.COMMENT)
 
    this.text = text
  }
}
 
export class NSVText extends NSVNode {
  constructor(text: string) {
    super(NSVNodeTypes.TEXT)
 
    this.text = text
  }
}
 
export class NSVRoot extends NSVNode {
  el?: NSVElement
 
  constructor() {
    super(NSVNodeTypes.ROOT)
  }
 
  appendChild(el: INSVNode) {
    // console.log(`NSVRoot->appendChild(${el.nodeType})`)
    if (el instanceof NSVElement) {
      this.el = el
    }
    // no-op
  }
}
 
function addChild(child: NSVElement, parent: NSVElement, atIndex?: number) {
  if (__TEST__) return
  // debug(
  //   `...addChild(  ${child.tagName}(${child.nodeId}), ${parent.tagName}(${
  //     parent.nodeId
  //   }), ${atIndex}  )`
  // )
  if (child.meta.viewFlags & NSVViewFlags.SKIP_ADD_TO_DOM) {
    // debug('SKIP_ADD_TO_DOM')
    return
  }
 
  const parentView = parent.nativeView
  const childView = child.nativeView
 
  if (parent.meta.viewFlags & NSVViewFlags.NO_CHILDREN) {
    // debug('NO_CHILDREN')
    return
  }
  if (parent.meta.nodeOps) {
    return parent.meta.nodeOps.insert(child, parent, atIndex)
  }
 
  if (parent.meta.viewFlags & NSVViewFlags.LAYOUT_VIEW) {
    if (atIndex) {
      parentView.insertChild(childView, atIndex)
    } else {
      parentView.addChild(childView)
    }
  } else if (parent.meta.viewFlags & NSVViewFlags.CONTENT_VIEW) {
    parentView.content = childView
  } else {
    parentView._addChildFromBuilder(childView.constructor.name, childView)
  }
}
 
function removeChild(child: NSVElement, parent: NSVElement) {
  if (__TEST__) return
  // debug(
  //   `...removeChild(  ${child.tagName}(${child.nodeId}), ${parent.tagName}(${
  //     parent.nodeId
  //   })  )`
  // )

  if (child.meta.viewFlags & NSVViewFlags.SKIP_ADD_TO_DOM) {
    // debug('SKIP_ADD_TO_DOM')
    return
  }
  if (parent.meta.viewFlags & NSVViewFlags.NO_CHILDREN) {
    // debug('NO_CHILDREN')
    return
  }
  if (parent.meta.nodeOps) {
    return parent.meta.nodeOps.remove(child, parent)
  }

  const parentView = parent.nativeView
  const childView = child.nativeView

  if (parent.meta.viewFlags & NSVViewFlags.LAYOUT_VIEW) {
    parentView.removeChild(childView)
  } else if (parent.meta.viewFlags & NSVViewFlags.CONTENT_VIEW) {
    parentView.content = null
  } else {
    parentView._removeView(childView)
  }
}