@startside/swa 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@startside/swa",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "The Startside Widget API (SWA): the runtime a widget file loads, its TypeScript types, its specification in Markdown, and a command to check a widget file and inline its runtime, and what another host needs to run a widget in a frame.",
5
5
  "keywords": [
6
6
  "swa",
package/swa/1.0/agents.md CHANGED
@@ -188,7 +188,9 @@ licence (CC BY 4.0).*
188
188
  `validate` and `inline`, below.
189
189
  - **Running widgets in another page.** Since 1.0.3, `@startside/swa` also carries the runtime a host writes into a
190
190
  widget's frame (`swa/1.0/frame.js`, SWA files only), its base styles (`frame.css`), and `host.md`: how a host runs a
191
- frame (10.1), the configuration that runtime reads and the messages a host answers.
191
+ frame (10.1), the configuration that runtime reads and the messages a host answers. Since 1.0.4, `host.js` too: a
192
+ widget file read (6.2), its settings' values, the frame's document written and a message checked, by the code
193
+ Startside's dashboard runs, as a module for the browser.
192
194
  - **Checking a file.** `npx @startside/swa validate my-widget.swa.html` reads a widget file without running it, with no
193
195
  network: the file's name and document, its runtime tag, metadata and settings form, and its scripts against this
194
196
  API, UWA's members and globals named with what replaces them. Each finding is `file:line:col: error|warning:
@@ -0,0 +1,27 @@
1
+ /* SWA 1.0, the host's side of a widget file (host.md) */
2
+ const e=e=>e.replace(/&/g,`&amp;`).replace(/"/g,`&quot;`).replace(/</g,`&lt;`),t=e=>`<script>${e.replace(/<\/(script)/gi,`<\\/$1`)}<\/script>`;function n(e){let t=[],n=e.replace(/@charset\s+[^;]+;/gi,``).replace(/@import\s+([^;]+?)\s*;/gi,(e,n)=>(t.push(`@import ${n} layer(widget);`),``));return`${t.join(`
3
+ `)}\n@layer widget{${n}\n}`}const r=e=>/(^|[,;\s])viewport-fit\s*=\s*cover\b/i.test(e.viewport??``);function i(e){return{title:e.title,preferences:e.preferences.map(e=>e.type===`password`?{...e,defaultValue:``}:e),actions:e.actions,metas:e.metas,url:e.url,format:e.format??`uwa`}}function a(i,a,o){let s=[...o.classes??[],o.mode===`dark`?`uwa-dark`:``,r(i.metas)?`uwa-cover`:``].filter(Boolean);return`<!doctype html>
4
+ <html lang="${e(o.lang)}" data-css-layers data-color-scheme="${o.mode===`dark`?`dark`:`light`}"${s.length?` class="${s.join(` `)}"`:``}>
5
+ <head>
6
+ <meta charset="utf-8">
7
+ <meta http-equiv="Content-Security-Policy" content="connect-src https:">
8
+ <base href="${e(i.url)}" target="_blank">
9
+ <style>@layer host, widget;\n@layer host{${o.hostCss}}</style>
10
+ ${i.stylesheets.map(t=>`<style>@import url("${e(t)}") layer(widget);</style>`).join(`
11
+ `)}
12
+ ${i.styles.map(e=>`<style>${n(e).replace(/<\/(style)/gi,`<\\/$1`)}</style>`).join(`
13
+ `)}
14
+ <style>html{-webkit-text-size-adjust:100%;text-size-adjust:100%}</style>
15
+ ${o.look??``}
16
+ ${t(`window.__UWA_CONFIG__ = ${JSON.stringify(a).replace(/</g,`\\u003c`)};`)}
17
+ ${o.runtime}
18
+ </head>
19
+ <body>
20
+ <div id="moduleContent" class="moduleContent">${i.body}</div>
21
+ ${t(`__startside.attachBody();`)}
22
+ ${[...i.scripts.filter(e=>`src`in e),...i.scripts.filter(e=>!(`src`in e))].map(e=>`src`in e?o.script(e.src):t(e.code)).join(`
23
+ `)}
24
+ ${t(`__startside.start();`)}
25
+ </body>
26
+ </html>`}const o={".cook":[`text/plain`],".txt":[`text/plain`],".md":[`text/markdown`,`text/plain`],".csv":[`text/csv`,`text/plain`],".json":[`application/json`,`text/plain`],".ics":[`text/calendar`,`text/plain`],".opml":[`text/x-opml`,`application/xml`,`text/xml`,`text/plain`],".gpx":[`application/gpx+xml`,`application/xml`,`text/xml`,`text/plain`]};function s(e){if(typeof e!=`string`)return[];let t=[];for(let n of e.trim().split(/\s+/)){let e=n.toLowerCase();/^\.[a-z0-9]{1,10}$/.test(e)&&Object.hasOwn(o,e)&&!t.includes(e)&&t.push(e)}return t}const c=/^[A-Za-z0-9_.-]{1,40}$/,l={reset:`↺`,refresh:`↻`,undo:`↶`,redo:`↷`,copy:`⧉`,download:`⇩`,upload:`⇧`,share:`↗`,add:`+`,remove:`×`,clear:`⌫`,edit:`✎`,search:`⌕`,print:`⎙`,play:`▶`,pause:`⏸`,stop:`■`,check:`✓`,star:`★`,info:`ⓘ`,settings:`⚙`};function u(e){let t=Intl.Segmenter;return t?[...new t().segment(e)].length===1:[...e].length<=2}const d=/\p{Extended_Pictographic}/u;function f(e){if(typeof e!=`string`)return``;let t=e.trim();return!t||t.length>16?``:Object.hasOwn(l,t.toLowerCase())?l[t.toLowerCase()]:d.test(t)&&u(t)?t:``}const p=e=>typeof e==`string`?e.replace(/\s+/g,` `).trim():``,m=e=>{let t=p(e);return t&&t.length<=40?t:null};function h(e){let t=[];for(let n of Array.isArray(e)?e:[]){if(t.length>=5)break;if(!n||typeof n!=`object`)continue;let{id:e,label:r,disabled:i,hidden:a,icon:o,confirm:l,save:u,album:d}=n,h=m(r),g=p(l);typeof e!=`string`||!c.test(e)||!h||g.length>200||t.some(t=>t.id===e)||t.push({id:e,label:h,disabled:i===!0,hidden:a===!0,icon:f(o),confirm:g,save:s(u),album:d===!0})}return t}const g=e=>(e??``).toLowerCase().split(/[\s,]+/).filter(e=>/^\.[a-z0-9]{1,16}$/.test(e)),_=/\/(js\/UWA\/|js\/c\/UWA_|themes\/uwa\/|api\/0\.[23]\/style\.css|swa\/\d+\.\d+\/runtime\.js|uwa\/\d+\.\d+\/runtime\.js(\?|$))/i,v=/^(?:[a-z]+:\/\/[^/]+)?(?:\/[^?#]*)?\/rwa\/(\d+(?:\.\d+)?)\/(runtime\.js|style\.css)$/i,y=(e,t)=>typeof location<`u`?new URL(`/rwa/${e}/${t}`,location.origin).href:null,b=(e,t)=>{let n=v.exec(e);if(n)return t(n[1]===`1`?`1.0`:n[1],n[2]===`runtime.js`?`frame.js`:n[2])},x=(e,t)=>Array.from(e.getElementsByTagName(`*`)).filter(e=>e.localName.toLowerCase().replace(/^widget:/,``)===t);function S(e,t){let n=new DOMParser;if(/\.html?([?#]|$)/i.test(t))return n.parseFromString(e,`text/html`);let r=n.parseFromString(e,`application/xhtml+xml`);return r.getElementsByTagName(`parsererror`).length?n.parseFromString(e,`text/html`):r}function C(e,t,n=y){let r=S(e,t),i=e=>{try{return new URL(e,t).href}catch{return e}},a={};for(let e of Array.from(r.getElementsByTagName(`meta`))){let t=e.getAttribute(`name`);t&&(a[t]=e.getAttribute(`content`)??``)}let o=Array.from(r.getElementsByTagName(`link`)),s=o.find(e=>/(^|\s)icon(\s|$)/i.test(e.getAttribute(`rel`)??``)),c=o.filter(e=>/stylesheet/i.test(e.getAttribute(`rel`)??``)&&e.getAttribute(`href`)).flatMap(e=>{let t=b(e.getAttribute(`href`),n);return t===void 0?[i(e.getAttribute(`href`))]:t?[t]:[]}).filter(e=>!_.test(e)),l=x(r,`preference`).map(e=>{let t=t=>e.getAttribute(t)==null?void 0:Number(e.getAttribute(t));return{name:e.getAttribute(`name`)??``,type:(e.getAttribute(`type`)??`text`).toLowerCase(),label:e.getAttribute(`label`)??e.getAttribute(`name`)??``,defaultValue:e.getAttribute(`defaultValue`)??e.getAttribute(`defaultvalue`)??``,options:O(e,`option`).map(e=>{let t=(e.getAttribute(`feed`)??e.getAttribute(`data-feed`))?.trim();return{value:e.getAttribute(`value`)??``,label:e.getAttribute(`label`)??e.getAttribute(`value`)??``,...t?{feed:t}:{}}}),min:t(`min`),max:t(`max`),step:t(`step`),...e.getAttribute(`shareable`)===`true`?{shareable:!0}:{},...e.getAttribute(`send`)===`true`?{sendable:!0}:{},...e.getAttribute(`hint`)?.trim()?{hint:e.getAttribute(`hint`).trim()}:{},...e.getAttribute(`commit`)===`ok`?{commit:`ok`}:{},...e.getAttribute(`when`)?.trim()?{when:e.getAttribute(`when`).trim()}:{}}}).concat(E(r)).filter(e=>e.name&&e.name!==`xwaFiles`),u=Array.from(r.getElementsByTagName(`script`)).map(e=>e.getAttribute(`src`)??``),d=e=>!e.getAttribute(`src`)&&/^swa\/\d+\.\d+$/.test(e.getAttribute(`data-runtime`)??``),f=Array.from(r.getElementsByTagName(`script`)).some(d),p=t.split(/[?#]/)[0],m=u.some(e=>v.test(e))||/\.rwa\.(x?html)$/i.test(p)?`rwa`:u.some(e=>/\/swa\/\d+\.\d+\/runtime\.js(\?|$)/i.test(e))||f||/\.swa\.html$/i.test(p)?`swa`:`uwa`,h=[],g=e=>/^(|text\/javascript|application\/javascript|module)$/i.test((e.getAttribute(`type`)??``).trim());for(let e of Array.from(r.getElementsByTagName(`script`)).filter(g)){let t=e.getAttribute(`src`);if(t){let e=b(t,n);(e===void 0?!_.test(t):e)&&h.push({src:e??i(t)})}else e.textContent?.trim()&&!d(e)&&h.push({code:e.textContent})}let C=t.split(/[?#]/)[0].split(`/`).pop()??``,D=0;for(let t of Array.from(r.getElementsByTagName(`script`)).filter(e=>!g(e))){t.setAttribute(`data-file`,C);let n=t.textContent??``,r=n?e.indexOf(n,D):-1;r<0||(t.setAttribute(`data-line`,String(e.slice(0,r).split(`
27
+ `).length)),D=r+n.length)}let k=Array.from(r.getElementsByTagName(`style`)).map(e=>e.textContent??``),A=T(r),j=w(r),M=r.getElementsByTagName(`body`)[0];if(M?.querySelectorAll(`script`).forEach(e=>g(e)&&e.remove()),M){for(let e of Array.from(M.querySelectorAll(`style, link, meta, title`)))e.remove();for(let e of O(M,`preferences`))e.remove();M.querySelector(`form#preferences`)?.remove(),M.querySelector(`menu#actions`)?.remove();for(let e of M.querySelectorAll(`[autofocus]`))e.removeAttribute(`autofocus`),e.setAttribute(`data-autofocus`,``)}return{url:t,format:m,title:r.getElementsByTagName(`title`)[0]?.textContent?.trim()||`UWA widget`,icon:s?.getAttribute(`href`)?i(s.getAttribute(`href`)):null,metas:a,preferences:l,actions:j,styles:k,stylesheets:c,scripts:h,body:M?.innerHTML??``,...A?{formOk:A}:{}}}function w(e){let t=e.getElementById(`actions`);return!t||t.localName!==`menu`?[]:h(Array.from(t.children).filter(e=>e.localName===`button`).map(e=>({id:e.getAttribute(`name`)??``,label:e.textContent??``,icon:e.getAttribute(`data-icon`)??``,confirm:e.getAttribute(`data-confirm`)??``,disabled:e.hasAttribute(`disabled`),hidden:e.hasAttribute(`hidden`),save:e.getAttribute(`data-save`)??``,album:e.hasAttribute(`data-album`)})))}function T(e){let t=e.getElementById(`preferences`);if(!t||t.localName!==`form`)return null;let n=t.querySelector(`button:not([type]), button[type="submit"], input[type="submit"]`);return n?((n.localName===`input`?n.getAttribute(`value`):n.textContent)??``).replace(/\s+/g,` `).trim()||`OK`:null}function E(e){let t=e.getElementById(`preferences`);if(!t||t.localName!==`form`)return[];let n=e=>{if(!e)return``;let t=e.cloneNode(!0);return t.querySelectorAll(`select, input, textarea`).forEach(e=>e.remove()),(t.textContent??``).replace(/\s+/g,` `).trim().replace(/\s*[::]$/,``)},r=e=>n(e.closest(`label`))||(e.id?n(t.querySelector(`label[for="${CSS.escape(e.id)}"]`)):``)||e.getAttribute(`aria-label`)||e.getAttribute(`name`)||``,i=(e,t)=>e.getAttribute(t)==null?void 0:Number(e.getAttribute(t)),a=[];for(let e of Array.from(t.querySelectorAll(`select[name], input[name]:not([type=submit i], [type=button i], [type=reset i], [type=image i]), textarea[name]`))){let t=e.getAttribute(`name`)??``,n=e.getAttribute(`data-hint`)?.trim(),o={name:t,label:r(e),options:[],...n?{hint:n}:{},...e.getAttribute(`data-commit`)===`ok`?{commit:`ok`}:{},...e.getAttribute(`data-when`)?.trim()?{when:e.getAttribute(`data-when`).trim()}:{}};if(e.localName===`select`){let t=Array.from(e.querySelectorAll(`option`)).map(e=>({value:e.getAttribute(`value`)??e.textContent?.trim()??``,label:(e.textContent??``).trim(),...e.getAttribute(`data-feed`)?.trim()?{feed:e.getAttribute(`data-feed`).trim()}:{}})),n=e.querySelector(`option[selected]`),r=t[0]?.value??``;a.push({...o,type:`list`,options:t,defaultValue:n?n.getAttribute(`value`)??n.textContent?.trim()??``:r})}else if(e.localName===`textarea`)a.push({...o,type:`text`,defaultValue:e.textContent??``});else{let t=(e.getAttribute(`type`)??`text`).toLowerCase();if(t===`url`)a.push({...o,type:`url`,defaultValue:e.getAttribute(`value`)??``});else if(t===`number`)a.push({...o,type:`number`,defaultValue:e.getAttribute(`value`)??``,min:i(e,`min`),max:i(e,`max`),step:i(e,`step`)});else if(t===`checkbox`)a.push({...o,type:`boolean`,defaultValue:e.hasAttribute(`checked`)?`true`:`false`});else if(t===`range`)a.push({...o,type:`range`,defaultValue:e.getAttribute(`value`)??``,min:i(e,`min`),max:i(e,`max`),step:i(e,`step`)});else if(t===`hidden`)a.push({...o,type:`hidden`,defaultValue:e.getAttribute(`value`)??``,...e.hasAttribute(`data-share`)?{shareable:!0}:{},...e.hasAttribute(`data-send`)?{sendable:!0}:{},...e.hasAttribute(`data-share`)&&e.getAttribute(`data-load`)?.trim()?{load:e.getAttribute(`data-load`).trim().slice(0,40)}:{},...e.hasAttribute(`data-share`)&&Number(e.getAttribute(`maxlength`))>0?{maxLength:Math.min(Math.floor(Number(e.getAttribute(`maxlength`))),16e3)}:{},...g(e.getAttribute(`data-file`)).length?{file:g(e.getAttribute(`data-file`))}:{}});else{let n=t===`password`&&e.hasAttribute(`data-key-for`),r=n?D(e.getAttribute(`data-key-for`)):null;a.push({...o,type:n&&!r?`hidden`:t===`password`?`password`:`text`,defaultValue:n?``:e.getAttribute(`value`)??``,...r?{keyFor:r}:{}})}}}return a}function D(e){let t=typeof e==`string`?e.trim():``;if(!/^https:\/\/[^\s/?#@\\]+\/?$/i.test(t))return null;try{let e=new URL(t);return e.protocol===`https:`&&!e.username&&!e.password&&!e.search&&!e.hash&&e.pathname===`/`?e.origin:null}catch{return null}}function O(e,t){return Array.from(e.getElementsByTagName(`*`)).filter(e=>e.localName.toLowerCase().replace(/^widget:/,``)===t)}function k(e,t){let n={};for(let r of e.preferences)n[r.name]=r.name in t?String(t[r.name]):r.defaultValue;for(let[e,r]of Object.entries(t))e in n||(n[e]=String(r));return n}function A(e,t){if(!e.when)return!0;let n=/^\s*([\w-]+)\s*(!?=)\s*(.*?)\s*$/.exec(e.when);if(!n)return!0;let r=n[3].split(`|`).map(e=>e.trim()).includes(t[n[1]]??``);return n[2]===`=`?r:!r}function j(e,t,n){let r=C(e,t,()=>null);if(!n.includes(r.format??`uwa`))throw Error(`Not ${n.map(e=>`an ${e.toUpperCase()}`).join(` or `)} widget file`);return r}const M=(t,n)=>`<script src="${e(t)}"${n?` crossorigin`:``}><\/script>`,N=e=>{try{return new URL(e).origin}catch{return null}};function P(e,n,r){let o=r.mode===`dark`?`dark`:`light`,s=Object.entries(r.vars??{}).map(([e,t])=>`${e}:${t}`).join(`;`);return a(e,{...n,...i(e)},{lang:n.lang??`en`,mode:o,hostCss:`:root{${s?`${s};`:``}color-scheme:${o}}\n${r.css??``}`,runtime:`src`in r.runtime?M(r.runtime.src,!0):t(r.runtime.code),script:e=>M(e,!!r.origin&&N(e)===r.origin)})}const F=(e,t,n)=>e.source===t&&e.data!=null&&typeof e.data==`object`&&e.data.uwa===n,I=(e,t)=>j(e,t,[`swa`]);export{F as accepts,P as buildSrcdoc,I as parseWidget,A as prefShown,k as prefValues};
package/swa/1.0/host.md CHANGED
@@ -9,7 +9,33 @@ The messages below are this runtime's, release by release: a host serving `frame
9
9
  speaks that release's messages. Pin the version you tested.
10
10
 
11
11
  `runtime.js` is not for a host: it is the runtime a widget file loads to run on its own page, and a host skips it (4.3).
12
- RWA files (`.rwa.html`) need RWA's runtime, which this package doesn't carry.
12
+ RWA files (`.rwa.html`) need RWA's runtime: `@startside/rwa`, whose `rwa/1.0/host.md` says what differs.
13
+
14
+ ## The host's side as code: `host.js`
15
+
16
+ Since 1.0.4, `swa/1.0/host.js` does what this page describes for the file and the frame's document, with the code
17
+ Startside's dashboard runs for its own frames: a plain ES module for the browser, nothing to build. Take it from the
18
+ same release as `frame.js`.
19
+
20
+ ```js
21
+ import { accepts, buildSrcdoc, parseWidget, prefShown, prefValues } from 'https://cdn.jsdelivr.net/npm/@startside/swa@1.0.4/swa/1.0/host.js';
22
+ ```
23
+
24
+ - `parseWidget(text, url)`: the file's text, fetched from `url`, read (6.2) with the browser's `DOMParser`: `{ url,
25
+ format, title, icon, metas, preferences, actions, styles, stylesheets, scripts, body }`, the file's own runtime left
26
+ out. It throws for a file that isn't SWA.
27
+ - `prefValues(doc, saved)`: each setting's value as a string, the saved one, else its default; saved values of no
28
+ declared setting are kept.
29
+ - `prefShown(pref, values)`: whether a setting shows, by its `data-when` (5).
30
+ - `buildSrcdoc(doc, config, look)`: the frame's document, below. `config` is your part of the config (`instance`,
31
+ `values`, `lang`, `memory`, `stored`, `read`, `noAdd`); what it says of the file (`title`, `url`, `format`,
32
+ `preferences` with a password's default left out, `metas`, `actions`) is added from `doc`. `look`: `mode`, `light` or
33
+ `dark`; `vars`, the CSS variables for `:root` (`{ '--text': '#222' }`); `css`, `frame.css`'s text; `runtime`, `{ code
34
+ }` to write the runtime in or `{ src }` to load it by address (with `crossorigin`); `origin`, an origin that answers
35
+ with CORS, whose scripts the file names are loaded with `crossorigin` too.
36
+ - `accepts(event, frameWindow, instance)`: whether a message is from that frame, about that instance.
37
+
38
+ The frame itself, its sandbox and the messages stay yours, as below.
13
39
 
14
40
  ## The frame
15
41
 
package/swa/1.0/spec.md CHANGED
@@ -2337,7 +2337,7 @@ UWA's API, globals or extensions of built-in objects: it runs SWA files only); a
2337
2337
  page skips it, and its catalogue lists the files that load it. The same runtime and types make the npm package
2338
2338
  `@startside/swa` (1.0.0 published November 8, 2026; 1.0.1 adds this specification, the quickstart and the guide for
2339
2339
  agents in Markdown, and the command `validate` and `inline`: Appendix F; 1.0.2, November 16, the API's additions since,
2340
- declared actions and saved files (7.13), a photo's zoom and credit (7.12), and its types corrected; 1.0.3, not yet published, what another host needs to run widgets in frames: Appendix F), served by jsDelivr and unpkg (4.3). jsDelivr keeps a
2340
+ declared actions and saved files (7.13), a photo's zoom and credit (7.12), and its types corrected; 1.0.3, November 25, what another host needs to run widgets in frames: Appendix F; 1.0.4, the host's side of a file as code, `host.js`), served by jsDelivr and unpkg (4.3). jsDelivr keeps a
2341
2341
  range's file (`@1`) up to 12 hours and lets a browser keep it up to 7 days, an exact version's for good; unpkg sends
2342
2342
  `@1` on to the exact version: a runtime fix reaches a CDN address within those times, startside.app's at once. Startside's own widgets are 57 SWA files (51 of
2343
2343
  them its UWA widgets, rewritten November 11, 2026), 29 RWA files and one UWA 1.2 file (xkcd), as of November 15, 2026; Appendix A's first example is
@@ -2452,7 +2452,9 @@ and an `action` event carrying the `id` when one is chosen; a question it then a
2452
2452
  `validate` and `inline`, below.
2453
2453
  - **Running widgets in another page.** Since 1.0.3, `@startside/swa` also carries the runtime a host writes into a
2454
2454
  widget's frame (`swa/1.0/frame.js`, SWA files only), its base styles (`frame.css`), and `host.md`: how a host runs a
2455
- frame (10.1), the configuration that runtime reads and the messages a host answers.
2455
+ frame (10.1), the configuration that runtime reads and the messages a host answers. Since 1.0.4, `host.js` too: a
2456
+ widget file read (6.2), its settings' values, the frame's document written and a message checked, by the code
2457
+ Startside's dashboard runs, as a module for the browser.
2456
2458
  - **Checking a file.** `npx @startside/swa validate my-widget.swa.html` reads a widget file without running it, with no
2457
2459
  network: the file's name and document, its runtime tag, metadata and settings form, and its scripts against this
2458
2460
  API, UWA's members and globals named with what replaces them. Each finding is `file:line:col: error|warning: