All files / runtime/src/modules style.ts

100% Statements 44/44
100% Branches 10/10
100% Functions 5/5
100% Lines 44/44

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 451x 1x 1x 1x 1x 1x 3x 3x 3x 1x 4x 4x 4x 1x 4x 4x 4x 1x 3x 3x 3x 3x 3x 1x 6x 6x 3x 3x 3x 3x 3x 3x 3x 4x 3x 3x 6x 6x 1x 6x 5x 5x 5x 6x  
import { unsetValue } from '@nativescript/core'
import { cssTreeParse } from '@nativescript/core/css/css-tree-parser'
import { INSVElement } from '../nodes'
 
type Style = string | null
 
function isRule(node: any): boolean {
  return node.type === 'rule'
}
 
function isDeclaration(node: any): boolean {
  return node.type === 'declaration'
}
 
function createDeclaration(decl: any): any {
  return { property: decl.property.toLowerCase(), value: decl.value }
}
 
function declarationsFromAstNodes(astRules: any[]): any[] {
  return astRules.filter(isRule).map((rule) => {
    return rule.declarations.filter(isDeclaration).map(createDeclaration)
  })
}
 
export function patchStyle(el: INSVElement, prev: Style, next: Style) {
  if (prev) {
    // todo: check if we can substitute cssTreeParse with parseInlineCSS from compiler/transforms/transformStyle (by extracting it to shared)
    // reset previous styles
    const localStyle = `local { ${prev} }`
    const ast: any = cssTreeParse(localStyle, undefined)
    const [declarations] = declarationsFromAstNodes(ast.stylesheet.rules)
 
    declarations.forEach((d: any) => {
      ;(el.nativeView.style as any)[d.property] = unsetValue
    })
  }
 
  if (!next) {
    el.removeAttribute('style')
  } else {
    // set new styles
    el.style = next
  }
}