How to use normalizeDirectives method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

directives.js

Source: directives.js Github

copy
1/* @flow */
2
3import { emptyNode } from 'core/vdom/patch'
4import { resolveAsset, handleError } from 'core/util/index'
5import { mergeVNodeHook } from 'core/vdom/helpers/index'
6
7export default {
8  create: updateDirectives,
9  update: updateDirectives,
10  destroy: function unbindDirectives (vnode: VNodeWithData) {
11    updateDirectives(vnode, emptyNode)
12  }
13}
14
15function updateDirectives (oldVnode: VNodeWithData, vnode: VNodeWithData) {
16  if (oldVnode.data.directives || vnode.data.directives) {
17    _update(oldVnode, vnode)
18  }
19}
20
21function _update (oldVnode, vnode) {
22  const isCreate = oldVnode === emptyNode
23  const isDestroy = vnode === emptyNode
24  const oldDirs = normalizeDirectives(oldVnode.data.directives, oldVnode.context)
25  const newDirs = normalizeDirectives(vnode.data.directives, vnode.context)
26
27  const dirsWithInsert = []
28  const dirsWithPostpatch = []
29
30  let key, oldDir, dir
31  for (key in newDirs) {
32    oldDir = oldDirs[key]
33    dir = newDirs[key]
34    if (!oldDir) {
35      // new directive, bind
36      callHook(dir, 'bind', vnode, oldVnode)
37      if (dir.def && dir.def.inserted) {
38        dirsWithInsert.push(dir)
39      }
40    } else {
41      // existing directive, update
42      dir.oldValue = oldDir.value
43      dir.oldArg = oldDir.arg
44      callHook(dir, 'update', vnode, oldVnode)
45      if (dir.def && dir.def.componentUpdated) {
46        dirsWithPostpatch.push(dir)
47      }
48    }
49  }
50
51  if (dirsWithInsert.length) {
52    const callInsert = () => {
53      for (let i = 0; i < dirsWithInsert.length; i++) {
54        callHook(dirsWithInsert[i], 'inserted', vnode, oldVnode)
55      }
56    }
57    if (isCreate) {
58      mergeVNodeHook(vnode, 'insert', callInsert)
59    } else {
60      callInsert()
61    }
62  }
63
64  if (dirsWithPostpatch.length) {
65    mergeVNodeHook(vnode, 'postpatch', () => {
66      for (let i = 0; i < dirsWithPostpatch.length; i++) {
67        callHook(dirsWithPostpatch[i], 'componentUpdated', vnode, oldVnode)
68      }
69    })
70  }
71
72  if (!isCreate) {
73    for (key in oldDirs) {
74      if (!newDirs[key]) {
75        // no longer present, unbind
76        callHook(oldDirs[key], 'unbind', oldVnode, oldVnode, isDestroy)
77      }
78    }
79  }
80}
81
82const emptyModifiers = Object.create(null)
83
84function normalizeDirectives (
85  dirs: ?Array<VNodeDirective>,
86  vm: Component
87): { [key: string]: VNodeDirective } {
88  const res = Object.create(null)
89  if (!dirs) {
90    // $flow-disable-line
91    return res
92  }
93  let i, dir
94  for (i = 0; i < dirs.length; i++) {
95    dir = dirs[i]
96    if (!dir.modifiers) {
97      // $flow-disable-line
98      dir.modifiers = emptyModifiers
99    }
100    res[getRawDirName(dir)] = dir
101    dir.def = resolveAsset(vm.$options, 'directives', dir.name, true)
102  }
103  // $flow-disable-line
104  return res
105}
106
107function getRawDirName (dir: VNodeDirective): string {
108  return dir.rawName || `${dir.name}.${Object.keys(dir.modifiers || {}).join('.')}`
109}
110
111function callHook (dir, hook, vnode, oldVnode, isDestroy) {
112  const fn = dir.def && dir.def[hook]
113  if (fn) {
114    try {
115      fn(vnode.elm, dir, vnode, oldVnode, isDestroy)
116    } catch (e) {
117      handleError(e, vnode.context, `directive ${dir.name} ${hook} hook`)
118    }
119  }
120}
121
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)