| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>Custom HX Tag Extension Test</title> |
| 5 | <script src="../../src/htmx.js"></script> |
| 6 | <style> |
| 7 | body { font-family: Arial, sans-serif; padding: 20px; } |
| 8 | #result { margin-top: 20px; padding: 10px; border: 1px solid #ccc; } |
| 9 | .log { background: #f0f0f0; padding: 10px; margin: 10px 0; border-left: 3px solid #007bff; } |
| 10 | .success { border-left-color: #28a745; } |
| 11 | .error { border-left-color: #dc3545; } |
| 12 | </style> |
| 13 | </head> |
| 14 | <body> |
| 15 | <h1>Custom HX Tag Extension Test</h1> |
| 16 | |
| 17 | <button hx-get="/test" hx-target="#result"> |
| 18 | Trigger Request |
| 19 | </button> |
| 20 | |
| 21 | <div id="result"> |
| 22 | <p>Click the button to test custom hx-* tags</p> |
| 23 | </div> |
| 24 | |
| 25 | <div id="log"> |
| 26 | <h3>Extension Log:</h3> |
| 27 | </div> |
| 28 | |
| 29 | <script> |
| 30 | // Helper to log messages |
| 31 | function log(message, type = '') { |
| 32 | const logDiv = document.createElement('div'); |
| 33 | logDiv.className = 'log ' + type; |
| 34 | logDiv.textContent = message; |
| 35 | document.getElementById('log').appendChild(logDiv); |
| 36 | console.log(message); |
| 37 | } |
| 38 | |
| 39 | // Mock fetch to return custom hx-* tags |
| 40 | const originalFetch = window.fetch; |
| 41 | window.fetch = function(url, options) { |
| 42 | console.log('Mocked fetch called:', url); |
| 43 | |
| 44 | const mockResponse = ` |
| 45 | <div> |
| 46 | <h2>Response Content</h2> |
| 47 | <p>This is the main content</p> |
| 48 | <hx-toast message="Hello from server!" duration="3000"></hx-toast> |
| 49 | <hx-modal title="Server Modal" hx-target="body"> |
| 50 | <p>This is modal content from the server</p> |
| 51 | </hx-modal> |
| 52 | <hx-script> |
| 53 | console.log('Custom script from hx-script tag'); |
| 54 | </hx-script> |
| 55 | </div> |
| 56 | `; |
| 57 | |
| 58 | return Promise.resolve({ |
| 59 | ok: true, |
| 60 | status: 200, |
| 61 | headers: new Headers(), |
| 62 | text: () => Promise.resolve(mockResponse), |
| 63 | json: () => Promise.resolve({}), |
| 64 | clone: function() { return this; } |
| 65 | }); |
| 66 | }; |
| 67 | |
| 68 | // Register extension using htmx4 format |
| 69 | (() => { |
| 70 | let api; |
| 71 | |
| 72 | htmx.registerExtension('custom-tags', { |
| 73 | init: (internalAPI) => { |
| 74 | api = internalAPI; |
| 75 | log('Custom tags extension initialized', 'success'); |
| 76 | }, |
| 77 | |
| 78 | htmx_process_toast: (templateElt, detail) => { |
| 79 | const { ctx, tasks } = detail; |
| 80 | |
| 81 | log('Toast tag detected!', 'success'); |
| 82 | log('Message: ' + templateElt.getAttribute('message')); |
| 83 | log('Duration: ' + templateElt.getAttribute('duration')); |
| 84 | |
| 85 | // Create a toast notification |
| 86 | const toast = document.createElement('div'); |
| 87 | toast.className = 'log success'; |
| 88 | toast.textContent = '🍞 Toast: ' + templateElt.getAttribute('message'); |
| 89 | |
| 90 | // Add as a task to be inserted |
| 91 | const fragment = document.createDocumentFragment(); |
| 92 | fragment.appendChild(toast); |
| 93 | |
| 94 | tasks.push({ |
| 95 | type: 'oob', |
| 96 | fragment: fragment, |
| 97 | target: '#log', |
| 98 | swapSpec: { style: 'beforeend' }, |
| 99 | sourceElement: ctx.sourceElement |
| 100 | }); |
| 101 | }, |
| 102 | |
| 103 | htmx_process_modal: (templateElt, detail) => { |
| 104 | const { ctx, tasks } = detail; |
| 105 | |
| 106 | log('Modal tag detected!', 'success'); |
| 107 | log('Title: ' + templateElt.getAttribute('title')); |
| 108 | log('Target: ' + templateElt.getAttribute('hx-target')); |
| 109 | |
| 110 | // Create modal overlay |
| 111 | const modal = document.createElement('div'); |
| 112 | modal.style.cssText = 'position:fixed;top:50%;left:50%;transform:translate(-50%,-50%);background:white;padding:20px;border:2px solid #007bff;box-shadow:0 4px 6px rgba(0,0,0,0.1);z-index:1000;'; |
| 113 | modal.innerHTML = ` |
| 114 | <h3>${templateElt.getAttribute('title')}</h3> |
| 115 | ${templateElt.content.querySelector('p').outerHTML} |
| 116 | <button onclick="this.parentElement.remove()">Close</button> |
| 117 | `; |
| 118 | |
| 119 | const fragment = document.createDocumentFragment(); |
| 120 | fragment.appendChild(modal); |
| 121 | |
| 122 | tasks.push({ |
| 123 | type: 'oob', |
| 124 | fragment: fragment, |
| 125 | target: 'body', |
| 126 | swapSpec: { style: 'beforeend' }, |
| 127 | sourceElement: ctx.sourceElement |
| 128 | }); |
| 129 | }, |
| 130 | |
| 131 | htmx_process_script: (templateElt, detail) => { |
| 132 | const { ctx, tasks } = detail; |
| 133 | |
| 134 | log('Script tag detected!', 'success'); |
| 135 | |
| 136 | // Execute the script content |
| 137 | const scriptContent = templateElt.content.textContent.trim(); |
| 138 | log('Executing: ' + scriptContent); |
| 139 | |
| 140 | try { |
| 141 | eval(scriptContent); |
| 142 | log('Script executed successfully', 'success'); |
| 143 | } catch (e) { |
| 144 | log('Script error: ' + e.message, 'error'); |
| 145 | } |
| 146 | } |
| 147 | }); |
| 148 | })(); |
| 149 | |
| 150 | log('Extension loaded and ready'); |
| 151 | </script> |
| 152 | </body> |
| 153 | </html> |