Pārlūkot izejas kodu

Fix dom walker to only skip nested components and their children

Caleb Porzio 5 gadi atpakaļ
vecāks
revīzija
9c4a932702
7 mainītis faili ar 27 papildinājumiem un 9 dzēšanām
  1. 2 2
      dist/mix-manifest.json
  2. 3 2
      dist/project-x.js
  3. 0 0
      dist/project-x.min.js
  4. 1 1
      index.html
  5. 1 1
      package.json
  6. 4 3
      src/utils.js
  7. 16 0
      test/nesting.spec.js

+ 2 - 2
dist/mix-manifest.json

@@ -1,4 +1,4 @@
 {
-    "/project-x.js": "/project-x.js?id=c71aaeb90e1486c6278a",
-    "/project-x.min.js": "/project-x.min.js?id=c4fab9eee62bbe35e148"
+    "/project-x.js": "/project-x.js?id=6a940bba5ac7fd0b68e1",
+    "/project-x.min.js": "/project-x.min.js?id=39ed1c431c27b0184930"
 }

+ 3 - 2
dist/project-x.js

@@ -1417,12 +1417,13 @@ function isTesting() {
   return navigator.userAgent, navigator.userAgent.includes("Node.js") || navigator.userAgent.includes("jsdom");
 }
 function walkSkippingNestedComponents(el, callback) {
+  var isRoot = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
+  if (el.hasAttribute('x-data') && !isRoot) return;
   callback(el);
   var node = el.firstElementChild;
 
   while (node) {
-    if (node.hasAttribute('x-data')) return;
-    walkSkippingNestedComponents(node, callback);
+    walkSkippingNestedComponents(node, callback, false);
     node = node.nextElementSibling;
   }
 }

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 0 - 0
dist/project-x.min.js


+ 1 - 1
index.html

@@ -5,7 +5,7 @@
             [x-cloak] { display: none; }
         </style>
 
-        <script src="https://cdn.jsdelivr.net/gh/calebporzio/project-x@v0.4.2/dist/project-x.min.js" defer></script>
+        <script src="https://cdn.jsdelivr.net/gh/calebporzio/project-x@v0.4.3/dist/project-x.min.js" defer></script>
     </head>
     <body>
         <table>

+ 1 - 1
package.json

@@ -1,7 +1,7 @@
 {
   "main": "dist/project-x.js",
   "name": "project-x",
-  "version": "0.4.2",
+  "version": "0.4.3",
   "repository": {
     "type": "git",
     "url": "git://github.com/calebporzio/project-x.git"

+ 4 - 3
src/utils.js

@@ -16,15 +16,16 @@ export function isTesting() {
         || navigator.userAgent.includes("jsdom")
 }
 
-export function walkSkippingNestedComponents(el, callback) {
+export function walkSkippingNestedComponents(el, callback, isRoot = true) {
+    if (el.hasAttribute('x-data') && ! isRoot) return
+
     callback(el)
 
     let node = el.firstElementChild
 
     while (node) {
-        if (node.hasAttribute('x-data')) return
+        walkSkippingNestedComponents(node, callback, false)
 
-        walkSkippingNestedComponents(node, callback)
         node = node.nextElementSibling
     }
 }

+ 16 - 0
test/nesting.spec.js

@@ -27,3 +27,19 @@ test('can nest components', async () => {
 
     await wait(() => { expect(document.querySelector('span').innerText).toEqual('bar') })
 })
+
+test('can access parent properties after nested components', async () => {
+    document.body.innerHTML = `
+        <div x-data="{ foo: 'bar' }">
+            <div x-data="{ foo: 'bob' }">
+                <button x-on:click="foo = 'baz'">Something</button>
+            </div>
+
+            <span x-text="foo"></span>
+        </div>
+    `
+
+    projectX.start()
+
+    expect(document.querySelector('span').innerText).toEqual('bar')
+})

Daži faili netika attēloti, jo izmaiņu fails ir pārāk liels