@b9g/crank 0.4.2 → 0.4.3

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/dom.cjs CHANGED
@@ -106,16 +106,24 @@ const impl = {
106
106
  // falsy properties, so we use attributes in this case.
107
107
  !(typeof value === "string" &&
108
108
  typeof node[name] === "boolean")) {
109
- try {
110
- if (node[name] !== value) {
111
- node[name] = value;
109
+ // walk up the object's prototype chain to find the owner of the
110
+ // named property
111
+ let obj = node;
112
+ do {
113
+ if (Object.prototype.hasOwnProperty.call(obj, name)) {
114
+ break;
112
115
  }
116
+ } while ((obj = Object.getPrototypeOf(obj)));
117
+ // get the descriptor for the named property and check whether it
118
+ // implies that the property is writable
119
+ const descriptor = Object.getOwnPropertyDescriptor(obj, name);
120
+ if (descriptor != null &&
121
+ (descriptor.writable === true || descriptor.set !== undefined)) {
122
+ node[name] = value;
113
123
  return;
114
124
  }
115
- catch (err) {
116
- // some properties are readonly so we fallback to setting them as
117
- // attributes
118
- }
125
+ // if the property wasn't writable, fall through to the code below
126
+ // which uses setAttribute() instead of assigning directly.
119
127
  }
120
128
  if (value === true) {
121
129
  value = "";
package/dom.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"dom.cjs","sources":["../src/dom.ts"],"sourcesContent":["import {\n\tChildren,\n\tContext,\n\tElementValue,\n\tPortal,\n\tRenderer,\n\tRendererImpl,\n} from \"./crank.js\";\n\nconst SVG_NAMESPACE = \"http://www.w3.org/2000/svg\";\n\nexport const impl: Partial<RendererImpl<Node, string>> = {\n\tparse(text: string): ElementValue<Node> {\n\t\tif (typeof document.createRange === \"function\") {\n\t\t\tconst fragment = document.createRange().createContextualFragment(text);\n\t\t\treturn Array.from(fragment.childNodes);\n\t\t} else {\n\t\t\tconst childNodes = new DOMParser().parseFromString(text, \"text/html\").body\n\t\t\t\t.childNodes;\n\t\t\treturn Array.from(childNodes);\n\t\t}\n\t},\n\n\tscope(scope: string, tag: string | symbol): string | undefined {\n\t\t// TODO: Should we handle xmlns???\n\t\tswitch (tag) {\n\t\t\tcase Portal:\n\t\t\tcase \"foreignObject\":\n\t\t\t\treturn undefined;\n\t\t\tcase \"svg\":\n\t\t\t\treturn SVG_NAMESPACE;\n\t\t\tdefault:\n\t\t\t\treturn scope;\n\t\t}\n\t},\n\n\tcreate(tag: string | symbol, _props: unknown, ns: string | undefined): Node {\n\t\tif (typeof tag !== \"string\") {\n\t\t\tthrow new Error(`Unknown tag: ${tag.toString()}`);\n\t\t} else if (tag.toLowerCase() === \"svg\") {\n\t\t\tns = SVG_NAMESPACE;\n\t\t}\n\n\t\treturn ns ? document.createElementNS(ns, tag) : document.createElement(tag);\n\t},\n\n\tpatch(\n\t\t_tag: string | symbol,\n\t\t// TODO: Why does this assignment work?\n\t\tnode: HTMLElement | SVGElement,\n\t\tname: string,\n\t\t// TODO: Stricter typings?\n\t\tvalue: unknown,\n\t\toldValue: unknown,\n\t\tscope: string | undefined,\n\t): void {\n\t\tconst isSVG = scope === SVG_NAMESPACE;\n\t\tswitch (name) {\n\t\t\tcase \"style\": {\n\t\t\t\tconst style: CSSStyleDeclaration = node.style;\n\t\t\t\tif (style == null) {\n\t\t\t\t\tnode.setAttribute(\"style\", value as string);\n\t\t\t\t} else if (value == null || value === false) {\n\t\t\t\t\tnode.removeAttribute(\"style\");\n\t\t\t\t} else if (value === true) {\n\t\t\t\t\tnode.setAttribute(\"style\", \"\");\n\t\t\t\t} else if (typeof value === \"string\") {\n\t\t\t\t\tif (style.cssText !== value) {\n\t\t\t\t\t\tstyle.cssText = value;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tif (typeof oldValue === \"string\") {\n\t\t\t\t\t\tstyle.cssText = \"\";\n\t\t\t\t\t}\n\n\t\t\t\t\tfor (const styleName in {...(oldValue as {}), ...(value as {})}) {\n\t\t\t\t\t\tconst styleValue = value && (value as any)[styleName];\n\t\t\t\t\t\tif (styleValue == null) {\n\t\t\t\t\t\t\tstyle.removeProperty(styleName);\n\t\t\t\t\t\t} else if (style.getPropertyValue(styleName) !== styleValue) {\n\t\t\t\t\t\t\tstyle.setProperty(styleName, styleValue);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase \"class\":\n\t\t\tcase \"className\":\n\t\t\t\tif (value === true) {\n\t\t\t\t\tnode.setAttribute(\"class\", \"\");\n\t\t\t\t} else if (value == null) {\n\t\t\t\t\tnode.removeAttribute(\"class\");\n\t\t\t\t} else if (!isSVG) {\n\t\t\t\t\tif (node.className !== value) {\n\t\t\t\t\t\t(node as any)[\"className\"] = value;\n\t\t\t\t\t}\n\t\t\t\t} else if (node.getAttribute(\"class\") !== value) {\n\t\t\t\t\tnode.setAttribute(\"class\", value as string);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase \"innerHTML\":\n\t\t\t\tif (value !== oldValue) {\n\t\t\t\t\tnode.innerHTML = value as any;\n\t\t\t\t}\n\n\t\t\t\tbreak;\n\t\t\tdefault: {\n\t\t\t\tif (\n\t\t\t\t\tname in node &&\n\t\t\t\t\t// boolean properties will coerce strings, but sometimes they map to\n\t\t\t\t\t// enumerated attributes, where truthy strings (\"false\", \"no\") map to\n\t\t\t\t\t// falsy properties, so we use attributes in this case.\n\t\t\t\t\t!(\n\t\t\t\t\t\ttypeof value === \"string\" &&\n\t\t\t\t\t\ttypeof (node as any)[name] === \"boolean\"\n\t\t\t\t\t)\n\t\t\t\t) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tif ((node as any)[name] !== value) {\n\t\t\t\t\t\t\t(node as any)[name] = value;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn;\n\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\t// some properties are readonly so we fallback to setting them as\n\t\t\t\t\t\t// attributes\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif (value === true) {\n\t\t\t\t\tvalue = \"\";\n\t\t\t\t} else if (value == null || value === false) {\n\t\t\t\t\tnode.removeAttribute(name);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif (node.getAttribute(name) !== value) {\n\t\t\t\t\tnode.setAttribute(name, value as any);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t},\n\n\tarrange(\n\t\ttag: string | symbol,\n\t\tnode: Node,\n\t\tprops: Record<string, any>,\n\t\tchildren: Array<Element | string>,\n\t\t_oldProps: Record<string, any> | undefined,\n\t\toldChildren: Array<Element | string> | undefined,\n\t): void {\n\t\tif (tag === Portal && (node == null || typeof node.nodeType !== \"number\")) {\n\t\t\tthrow new TypeError(\n\t\t\t\t`Portal root is not a node. Received: ${JSON.stringify(\n\t\t\t\t\tnode && node.toString(),\n\t\t\t\t)}`,\n\t\t\t);\n\t\t}\n\n\t\tif (\n\t\t\t!(\"innerHTML\" in props) &&\n\t\t\t// We don’t want to update elements without explicit children (<div/>),\n\t\t\t// because these elements sometimes have child nodes added via raw\n\t\t\t// DOM manipulations.\n\t\t\t// However, if an element has previously rendered children, we clear the\n\t\t\t// them because it would be surprising not to clear Crank managed\n\t\t\t// children, even if the new element does not have explicit children.\n\t\t\t(\"children\" in props || (oldChildren && oldChildren.length))\n\t\t) {\n\t\t\tif (children.length === 0) {\n\t\t\t\tnode.textContent = \"\";\n\t\t\t} else {\n\t\t\t\tlet oldChild = node.firstChild;\n\t\t\t\tlet i = 0;\n\t\t\t\twhile (oldChild !== null && i < children.length) {\n\t\t\t\t\tconst newChild = children[i];\n\t\t\t\t\tif (oldChild === newChild) {\n\t\t\t\t\t\toldChild = oldChild.nextSibling;\n\t\t\t\t\t\ti++;\n\t\t\t\t\t} else if (typeof newChild === \"string\") {\n\t\t\t\t\t\tif (oldChild.nodeType === Node.TEXT_NODE) {\n\t\t\t\t\t\t\tif ((oldChild as Text).data !== newChild) {\n\t\t\t\t\t\t\t\t(oldChild as Text).data = newChild;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\toldChild = oldChild.nextSibling;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tnode.insertBefore(document.createTextNode(newChild), oldChild);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\ti++;\n\t\t\t\t\t} else if (oldChild.nodeType === Node.TEXT_NODE) {\n\t\t\t\t\t\tconst nextSibling = oldChild.nextSibling;\n\t\t\t\t\t\tnode.removeChild(oldChild);\n\t\t\t\t\t\toldChild = nextSibling;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tnode.insertBefore(newChild, oldChild);\n\t\t\t\t\t\ti++;\n\t\t\t\t\t\t// TODO: This is an optimization but we need to think a little more about other cases like prepending.\n\t\t\t\t\t\tif (oldChild !== children[i]) {\n\t\t\t\t\t\t\tconst nextSibling = oldChild.nextSibling;\n\t\t\t\t\t\t\tnode.removeChild(oldChild);\n\t\t\t\t\t\t\toldChild = nextSibling;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// remove excess DOM nodes\n\t\t\t\twhile (oldChild !== null) {\n\t\t\t\t\tconst nextSibling = oldChild.nextSibling;\n\t\t\t\t\tnode.removeChild(oldChild);\n\t\t\t\t\toldChild = nextSibling;\n\t\t\t\t}\n\n\t\t\t\t// append excess children\n\t\t\t\tfor (; i < children.length; i++) {\n\t\t\t\t\tconst newChild = children[i];\n\t\t\t\t\tnode.appendChild(\n\t\t\t\t\t\ttypeof newChild === \"string\"\n\t\t\t\t\t\t\t? document.createTextNode(newChild)\n\t\t\t\t\t\t\t: newChild,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t},\n};\n\nexport class DOMRenderer extends Renderer<Node, string> {\n\tconstructor() {\n\t\tsuper(impl);\n\t}\n\n\trender(\n\t\tchildren: Children,\n\t\troot: Node,\n\t\tctx?: Context,\n\t): Promise<ElementValue<Node>> | ElementValue<Node> {\n\t\tif (root == null || typeof root.nodeType !== \"number\") {\n\t\t\tthrow new TypeError(\n\t\t\t\t`Render root is not a node. Received: ${JSON.stringify(\n\t\t\t\t\troot && root.toString(),\n\t\t\t\t)}`,\n\t\t\t);\n\t\t}\n\n\t\treturn super.render(children, root, ctx);\n\t}\n}\n\nexport const renderer = new DOMRenderer();\n\ndeclare global {\n\tmodule Crank {\n\t\tinterface EventMap extends GlobalEventHandlersEventMap {}\n\t}\n}\n"],"names":["Portal","Renderer"],"mappings":";;;;;;AASA,MAAM,aAAa,GAAG,4BAA4B,CAAC;MAEtC,IAAI,GAAwC;IACxD,KAAK,CAAC,IAAY;QACjB,IAAI,OAAO,QAAQ,CAAC,WAAW,KAAK,UAAU,EAAE;YAC/C,MAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC,wBAAwB,CAAC,IAAI,CAAC,CAAC;YACvE,OAAO,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;SACvC;aAAM;YACN,MAAM,UAAU,GAAG,IAAI,SAAS,EAAE,CAAC,eAAe,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC,IAAI;iBACxE,UAAU,CAAC;YACb,OAAO,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;SAC9B;KACD;IAED,KAAK,CAAC,KAAa,EAAE,GAAoB;;QAExC,QAAQ,GAAG;YACV,KAAKA,YAAM,CAAC;YACZ,KAAK,eAAe;gBACnB,OAAO,SAAS,CAAC;YAClB,KAAK,KAAK;gBACT,OAAO,aAAa,CAAC;YACtB;gBACC,OAAO,KAAK,CAAC;SACd;KACD;IAED,MAAM,CAAC,GAAoB,EAAE,MAAe,EAAE,EAAsB;QACnE,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;YAC5B,MAAM,IAAI,KAAK,CAAC,gBAAgB,GAAG,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;SAClD;aAAM,IAAI,GAAG,CAAC,WAAW,EAAE,KAAK,KAAK,EAAE;YACvC,EAAE,GAAG,aAAa,CAAC;SACnB;QAED,OAAO,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,QAAQ,CACX,CAAC;iBACF;aACD;SACD;KACD;EACA;MAEW,WAAY,SAAQC,cAAsB;IACtD;QACC,KAAK,CAAC,IAAI,CAAC,CAAC;KACZ;IAED,MAAM,CACL,QAAkB,EAClB,IAAU,EACV,GAAa;QAEb,IAAI,IAAI,IAAI,IAAI,IAAI,OAAO,IAAI,CAAC,QAAQ,KAAK,QAAQ,EAAE;YACtD,MAAM,IAAI,SAAS,CAClB,wCAAwC,IAAI,CAAC,SAAS,CACrD,IAAI,IAAI,IAAI,CAAC,QAAQ,EAAE,CACvB,EAAE,CACH,CAAC;SACF;QAED,OAAO,KAAK,CAAC,MAAM,CAAC,QAAQ,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC;KACzC;CACD;MAEY,QAAQ,GAAG,IAAI,WAAW;;;;;;"}
1
+ {"version":3,"file":"dom.cjs","sources":["../src/dom.ts"],"sourcesContent":["import {\n\tChildren,\n\tContext,\n\tElementValue,\n\tPortal,\n\tRenderer,\n\tRendererImpl,\n} from \"./crank.js\";\n\nconst SVG_NAMESPACE = \"http://www.w3.org/2000/svg\";\n\nexport const impl: Partial<RendererImpl<Node, string>> = {\n\tparse(text: string): ElementValue<Node> {\n\t\tif (typeof document.createRange === \"function\") {\n\t\t\tconst fragment = document.createRange().createContextualFragment(text);\n\t\t\treturn Array.from(fragment.childNodes);\n\t\t} else {\n\t\t\tconst childNodes = new DOMParser().parseFromString(text, \"text/html\").body\n\t\t\t\t.childNodes;\n\t\t\treturn Array.from(childNodes);\n\t\t}\n\t},\n\n\tscope(scope: string, tag: string | symbol): string | undefined {\n\t\t// TODO: Should we handle xmlns???\n\t\tswitch (tag) {\n\t\t\tcase Portal:\n\t\t\tcase \"foreignObject\":\n\t\t\t\treturn undefined;\n\t\t\tcase \"svg\":\n\t\t\t\treturn SVG_NAMESPACE;\n\t\t\tdefault:\n\t\t\t\treturn scope;\n\t\t}\n\t},\n\n\tcreate(tag: string | symbol, _props: unknown, ns: string | undefined): Node {\n\t\tif (typeof tag !== \"string\") {\n\t\t\tthrow new Error(`Unknown tag: ${tag.toString()}`);\n\t\t} else if (tag.toLowerCase() === \"svg\") {\n\t\t\tns = SVG_NAMESPACE;\n\t\t}\n\n\t\treturn ns ? document.createElementNS(ns, tag) : document.createElement(tag);\n\t},\n\n\tpatch(\n\t\t_tag: string | symbol,\n\t\t// TODO: Why does this assignment work?\n\t\tnode: HTMLElement | SVGElement,\n\t\tname: string,\n\t\t// TODO: Stricter typings?\n\t\tvalue: unknown,\n\t\toldValue: unknown,\n\t\tscope: string | undefined,\n\t): void {\n\t\tconst isSVG = scope === SVG_NAMESPACE;\n\t\tswitch (name) {\n\t\t\tcase \"style\": {\n\t\t\t\tconst style: CSSStyleDeclaration = node.style;\n\t\t\t\tif (style == null) {\n\t\t\t\t\tnode.setAttribute(\"style\", value as string);\n\t\t\t\t} else if (value == null || value === false) {\n\t\t\t\t\tnode.removeAttribute(\"style\");\n\t\t\t\t} else if (value === true) {\n\t\t\t\t\tnode.setAttribute(\"style\", \"\");\n\t\t\t\t} else if (typeof value === \"string\") {\n\t\t\t\t\tif (style.cssText !== value) {\n\t\t\t\t\t\tstyle.cssText = value;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tif (typeof oldValue === \"string\") {\n\t\t\t\t\t\tstyle.cssText = \"\";\n\t\t\t\t\t}\n\n\t\t\t\t\tfor (const styleName in {...(oldValue as {}), ...(value as {})}) {\n\t\t\t\t\t\tconst styleValue = value && (value as any)[styleName];\n\t\t\t\t\t\tif (styleValue == null) {\n\t\t\t\t\t\t\tstyle.removeProperty(styleName);\n\t\t\t\t\t\t} else if (style.getPropertyValue(styleName) !== styleValue) {\n\t\t\t\t\t\t\tstyle.setProperty(styleName, styleValue);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase \"class\":\n\t\t\tcase \"className\":\n\t\t\t\tif (value === true) {\n\t\t\t\t\tnode.setAttribute(\"class\", \"\");\n\t\t\t\t} else if (value == null) {\n\t\t\t\t\tnode.removeAttribute(\"class\");\n\t\t\t\t} else if (!isSVG) {\n\t\t\t\t\tif (node.className !== value) {\n\t\t\t\t\t\t(node as any)[\"className\"] = value;\n\t\t\t\t\t}\n\t\t\t\t} else if (node.getAttribute(\"class\") !== value) {\n\t\t\t\t\tnode.setAttribute(\"class\", value as string);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase \"innerHTML\":\n\t\t\t\tif (value !== oldValue) {\n\t\t\t\t\tnode.innerHTML = value as any;\n\t\t\t\t}\n\n\t\t\t\tbreak;\n\t\t\tdefault: {\n\t\t\t\tif (\n\t\t\t\t\tname in node &&\n\t\t\t\t\t// boolean properties will coerce strings, but sometimes they map to\n\t\t\t\t\t// enumerated attributes, where truthy strings (\"false\", \"no\") map to\n\t\t\t\t\t// falsy properties, so we use attributes in this case.\n\t\t\t\t\t!(\n\t\t\t\t\t\ttypeof value === \"string\" &&\n\t\t\t\t\t\ttypeof (node as any)[name] === \"boolean\"\n\t\t\t\t\t)\n\t\t\t\t) {\n\t\t\t\t\t// walk up the object's prototype chain to find the owner of the\n\t\t\t\t\t// named property\n\t\t\t\t\tlet obj = node;\n\t\t\t\t\tdo {\n\t\t\t\t\t\tif (Object.prototype.hasOwnProperty.call(obj, name)) {\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t} while ((obj = Object.getPrototypeOf(obj)));\n\n\t\t\t\t\t// get the descriptor for the named property and check whether it\n\t\t\t\t\t// implies that the property is writable\n\t\t\t\t\tconst descriptor = Object.getOwnPropertyDescriptor(obj, name);\n\t\t\t\t\tif (\n\t\t\t\t\t\tdescriptor != null &&\n\t\t\t\t\t\t(descriptor.writable === true || descriptor.set !== undefined)\n\t\t\t\t\t) {\n\t\t\t\t\t\t(node as any)[name] = value;\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\t// if the property wasn't writable, fall through to the code below\n\t\t\t\t\t// which uses setAttribute() instead of assigning directly.\n\t\t\t\t}\n\n\t\t\t\tif (value === true) {\n\t\t\t\t\tvalue = \"\";\n\t\t\t\t} else if (value == null || value === false) {\n\t\t\t\t\tnode.removeAttribute(name);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif (node.getAttribute(name) !== value) {\n\t\t\t\t\tnode.setAttribute(name, value as any);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t},\n\n\tarrange(\n\t\ttag: string | symbol,\n\t\tnode: Node,\n\t\tprops: Record<string, any>,\n\t\tchildren: Array<Element | string>,\n\t\t_oldProps: Record<string, any> | undefined,\n\t\toldChildren: Array<Element | string> | undefined,\n\t): void {\n\t\tif (tag === Portal && (node == null || typeof node.nodeType !== \"number\")) {\n\t\t\tthrow new TypeError(\n\t\t\t\t`Portal root is not a node. Received: ${JSON.stringify(\n\t\t\t\t\tnode && node.toString(),\n\t\t\t\t)}`,\n\t\t\t);\n\t\t}\n\n\t\tif (\n\t\t\t!(\"innerHTML\" in props) &&\n\t\t\t// We don’t want to update elements without explicit children (<div/>),\n\t\t\t// because these elements sometimes have child nodes added via raw\n\t\t\t// DOM manipulations.\n\t\t\t// However, if an element has previously rendered children, we clear the\n\t\t\t// them because it would be surprising not to clear Crank managed\n\t\t\t// children, even if the new element does not have explicit children.\n\t\t\t(\"children\" in props || (oldChildren && oldChildren.length))\n\t\t) {\n\t\t\tif (children.length === 0) {\n\t\t\t\tnode.textContent = \"\";\n\t\t\t} else {\n\t\t\t\tlet oldChild = node.firstChild;\n\t\t\t\tlet i = 0;\n\t\t\t\twhile (oldChild !== null && i < children.length) {\n\t\t\t\t\tconst newChild = children[i];\n\t\t\t\t\tif (oldChild === newChild) {\n\t\t\t\t\t\toldChild = oldChild.nextSibling;\n\t\t\t\t\t\ti++;\n\t\t\t\t\t} else if (typeof newChild === \"string\") {\n\t\t\t\t\t\tif (oldChild.nodeType === Node.TEXT_NODE) {\n\t\t\t\t\t\t\tif ((oldChild as Text).data !== newChild) {\n\t\t\t\t\t\t\t\t(oldChild as Text).data = newChild;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\toldChild = oldChild.nextSibling;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tnode.insertBefore(document.createTextNode(newChild), oldChild);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\ti++;\n\t\t\t\t\t} else if (oldChild.nodeType === Node.TEXT_NODE) {\n\t\t\t\t\t\tconst nextSibling = oldChild.nextSibling;\n\t\t\t\t\t\tnode.removeChild(oldChild);\n\t\t\t\t\t\toldChild = nextSibling;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tnode.insertBefore(newChild, oldChild);\n\t\t\t\t\t\ti++;\n\t\t\t\t\t\t// TODO: This is an optimization but we need to think a little more about other cases like prepending.\n\t\t\t\t\t\tif (oldChild !== children[i]) {\n\t\t\t\t\t\t\tconst nextSibling = oldChild.nextSibling;\n\t\t\t\t\t\t\tnode.removeChild(oldChild);\n\t\t\t\t\t\t\toldChild = nextSibling;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// remove excess DOM nodes\n\t\t\t\twhile (oldChild !== null) {\n\t\t\t\t\tconst nextSibling = oldChild.nextSibling;\n\t\t\t\t\tnode.removeChild(oldChild);\n\t\t\t\t\toldChild = nextSibling;\n\t\t\t\t}\n\n\t\t\t\t// append excess children\n\t\t\t\tfor (; i < children.length; i++) {\n\t\t\t\t\tconst newChild = children[i];\n\t\t\t\t\tnode.appendChild(\n\t\t\t\t\t\ttypeof newChild === \"string\"\n\t\t\t\t\t\t\t? document.createTextNode(newChild)\n\t\t\t\t\t\t\t: newChild,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t},\n};\n\nexport class DOMRenderer extends Renderer<Node, string> {\n\tconstructor() {\n\t\tsuper(impl);\n\t}\n\n\trender(\n\t\tchildren: Children,\n\t\troot: Node,\n\t\tctx?: Context,\n\t): Promise<ElementValue<Node>> | ElementValue<Node> {\n\t\tif (root == null || typeof root.nodeType !== \"number\") {\n\t\t\tthrow new TypeError(\n\t\t\t\t`Render root is not a node. Received: ${JSON.stringify(\n\t\t\t\t\troot && root.toString(),\n\t\t\t\t)}`,\n\t\t\t);\n\t\t}\n\n\t\treturn super.render(children, root, ctx);\n\t}\n}\n\nexport const renderer = new DOMRenderer();\n\ndeclare global {\n\tmodule Crank {\n\t\tinterface EventMap extends GlobalEventHandlersEventMap {}\n\t}\n}\n"],"names":["Portal","Renderer"],"mappings":";;;;;;AASA,MAAM,aAAa,GAAG,4BAA4B,CAAC;MAEtC,IAAI,GAAwC;IACxD,KAAK,CAAC,IAAY;QACjB,IAAI,OAAO,QAAQ,CAAC,WAAW,KAAK,UAAU,EAAE;YAC/C,MAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC,wBAAwB,CAAC,IAAI,CAAC,CAAC;YACvE,OAAO,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;SACvC;aAAM;YACN,MAAM,UAAU,GAAG,IAAI,SAAS,EAAE,CAAC,eAAe,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC,IAAI;iBACxE,UAAU,CAAC;YACb,OAAO,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;SAC9B;KACD;IAED,KAAK,CAAC,KAAa,EAAE,GAAoB;;QAExC,QAAQ,GAAG;YACV,KAAKA,YAAM,CAAC;YACZ,KAAK,eAAe;gBACnB,OAAO,SAAS,CAAC;YAClB,KAAK,KAAK;gBACT,OAAO,aAAa,CAAC;YACtB;gBACC,OAAO,KAAK,CAAC;SACd;KACD;IAED,MAAM,CAAC,GAAoB,EAAE,MAAe,EAAE,EAAsB;QACnE,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;YAC5B,MAAM,IAAI,KAAK,CAAC,gBAAgB,GAAG,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;SAClD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ACD;iBACD;;gBAGD,OAAO,QAAQ,KAAK,IAAI,EAAE;oBACzB,MAAM,WAAW,GAAG,QAAQ,CAAC,WAAW,CAAC;oBACzC,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;oBAC3B,QAAQ,GAAG,WAAW,CAAC;iBACvB;;gBAGD,OAAO,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;oBAChC,MAAM,QAAQ,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;oBAC7B,IAAI,CAAC,WAAW,CACf,OAAO,QAAQ,KAAK,QAAQ;0BACzB,QAAQ,CAAC,cAAc,CAAC,QAAQ,CAAC;0BACjC,QAAQ,CACX,CAAC;iBACF;aACD;SACD;KACD;EACA;MAEW,WAAY,SAAQC,cAAsB;IACtD;QACC,KAAK,CAAC,IAAI,CAAC,CAAC;KACZ;IAED,MAAM,CACL,QAAkB,EAClB,IAAU,EACV,GAAa;QAEb,IAAI,IAAI,IAAI,IAAI,IAAI,OAAO,IAAI,CAAC,QAAQ,KAAK,QAAQ,EAAE;YACtD,MAAM,IAAI,SAAS,CAClB,wCAAwC,IAAI,CAAC,SAAS,CACrD,IAAI,IAAI,IAAI,CAAC,QAAQ,EAAE,CACvB,EAAE,CACH,CAAC;SACF;QAED,OAAO,KAAK,CAAC,MAAM,CAAC,QAAQ,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC;KACzC;CACD;MAEY,QAAQ,GAAG,IAAI,WAAW;;;;;;"}
package/dom.js CHANGED
@@ -103,16 +103,24 @@ const impl = {
103
103
  // falsy properties, so we use attributes in this case.
104
104
  !(typeof value === "string" &&
105
105
  typeof node[name] === "boolean")) {
106
- try {
107
- if (node[name] !== value) {
108
- node[name] = value;
106
+ // walk up the object's prototype chain to find the owner of the
107
+ // named property
108
+ let obj = node;
109
+ do {
110
+ if (Object.prototype.hasOwnProperty.call(obj, name)) {
111
+ break;
109
112
  }
113
+ } while ((obj = Object.getPrototypeOf(obj)));
114
+ // get the descriptor for the named property and check whether it
115
+ // implies that the property is writable
116
+ const descriptor = Object.getOwnPropertyDescriptor(obj, name);
117
+ if (descriptor != null &&
118
+ (descriptor.writable === true || descriptor.set !== undefined)) {
119
+ node[name] = value;
110
120
  return;
111
121
  }
112
- catch (err) {
113
- // some properties are readonly so we fallback to setting them as
114
- // attributes
115
- }
122
+ // if the property wasn't writable, fall through to the code below
123
+ // which uses setAttribute() instead of assigning directly.
116
124
  }
117
125
  if (value === true) {
118
126
  value = "";
package/dom.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"dom.js","sources":["../src/dom.ts"],"sourcesContent":["import {\n\tChildren,\n\tContext,\n\tElementValue,\n\tPortal,\n\tRenderer,\n\tRendererImpl,\n} from \"./crank.js\";\n\nconst SVG_NAMESPACE = \"http://www.w3.org/2000/svg\";\n\nexport const impl: Partial<RendererImpl<Node, string>> = {\n\tparse(text: string): ElementValue<Node> {\n\t\tif (typeof document.createRange === \"function\") {\n\t\t\tconst fragment = document.createRange().createContextualFragment(text);\n\t\t\treturn Array.from(fragment.childNodes);\n\t\t} else {\n\t\t\tconst childNodes = new DOMParser().parseFromString(text, \"text/html\").body\n\t\t\t\t.childNodes;\n\t\t\treturn Array.from(childNodes);\n\t\t}\n\t},\n\n\tscope(scope: string, tag: string | symbol): string | undefined {\n\t\t// TODO: Should we handle xmlns???\n\t\tswitch (tag) {\n\t\t\tcase Portal:\n\t\t\tcase \"foreignObject\":\n\t\t\t\treturn undefined;\n\t\t\tcase \"svg\":\n\t\t\t\treturn SVG_NAMESPACE;\n\t\t\tdefault:\n\t\t\t\treturn scope;\n\t\t}\n\t},\n\n\tcreate(tag: string | symbol, _props: unknown, ns: string | undefined): Node {\n\t\tif (typeof tag !== \"string\") {\n\t\t\tthrow new Error(`Unknown tag: ${tag.toString()}`);\n\t\t} else if (tag.toLowerCase() === \"svg\") {\n\t\t\tns = SVG_NAMESPACE;\n\t\t}\n\n\t\treturn ns ? document.createElementNS(ns, tag) : document.createElement(tag);\n\t},\n\n\tpatch(\n\t\t_tag: string | symbol,\n\t\t// TODO: Why does this assignment work?\n\t\tnode: HTMLElement | SVGElement,\n\t\tname: string,\n\t\t// TODO: Stricter typings?\n\t\tvalue: unknown,\n\t\toldValue: unknown,\n\t\tscope: string | undefined,\n\t): void {\n\t\tconst isSVG = scope === SVG_NAMESPACE;\n\t\tswitch (name) {\n\t\t\tcase \"style\": {\n\t\t\t\tconst style: CSSStyleDeclaration = node.style;\n\t\t\t\tif (style == null) {\n\t\t\t\t\tnode.setAttribute(\"style\", value as string);\n\t\t\t\t} else if (value == null || value === false) {\n\t\t\t\t\tnode.removeAttribute(\"style\");\n\t\t\t\t} else if (value === true) {\n\t\t\t\t\tnode.setAttribute(\"style\", \"\");\n\t\t\t\t} else if (typeof value === \"string\") {\n\t\t\t\t\tif (style.cssText !== value) {\n\t\t\t\t\t\tstyle.cssText = value;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tif (typeof oldValue === \"string\") {\n\t\t\t\t\t\tstyle.cssText = \"\";\n\t\t\t\t\t}\n\n\t\t\t\t\tfor (const styleName in {...(oldValue as {}), ...(value as {})}) {\n\t\t\t\t\t\tconst styleValue = value && (value as any)[styleName];\n\t\t\t\t\t\tif (styleValue == null) {\n\t\t\t\t\t\t\tstyle.removeProperty(styleName);\n\t\t\t\t\t\t} else if (style.getPropertyValue(styleName) !== styleValue) {\n\t\t\t\t\t\t\tstyle.setProperty(styleName, styleValue);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase \"class\":\n\t\t\tcase \"className\":\n\t\t\t\tif (value === true) {\n\t\t\t\t\tnode.setAttribute(\"class\", \"\");\n\t\t\t\t} else if (value == null) {\n\t\t\t\t\tnode.removeAttribute(\"class\");\n\t\t\t\t} else if (!isSVG) {\n\t\t\t\t\tif (node.className !== value) {\n\t\t\t\t\t\t(node as any)[\"className\"] = value;\n\t\t\t\t\t}\n\t\t\t\t} else if (node.getAttribute(\"class\") !== value) {\n\t\t\t\t\tnode.setAttribute(\"class\", value as string);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase \"innerHTML\":\n\t\t\t\tif (value !== oldValue) {\n\t\t\t\t\tnode.innerHTML = value as any;\n\t\t\t\t}\n\n\t\t\t\tbreak;\n\t\t\tdefault: {\n\t\t\t\tif (\n\t\t\t\t\tname in node &&\n\t\t\t\t\t// boolean properties will coerce strings, but sometimes they map to\n\t\t\t\t\t// enumerated attributes, where truthy strings (\"false\", \"no\") map to\n\t\t\t\t\t// falsy properties, so we use attributes in this case.\n\t\t\t\t\t!(\n\t\t\t\t\t\ttypeof value === \"string\" &&\n\t\t\t\t\t\ttypeof (node as any)[name] === \"boolean\"\n\t\t\t\t\t)\n\t\t\t\t) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tif ((node as any)[name] !== value) {\n\t\t\t\t\t\t\t(node as any)[name] = value;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn;\n\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\t// some properties are readonly so we fallback to setting them as\n\t\t\t\t\t\t// attributes\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif (value === true) {\n\t\t\t\t\tvalue = \"\";\n\t\t\t\t} else if (value == null || value === false) {\n\t\t\t\t\tnode.removeAttribute(name);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif (node.getAttribute(name) !== value) {\n\t\t\t\t\tnode.setAttribute(name, value as any);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t},\n\n\tarrange(\n\t\ttag: string | symbol,\n\t\tnode: Node,\n\t\tprops: Record<string, any>,\n\t\tchildren: Array<Element | string>,\n\t\t_oldProps: Record<string, any> | undefined,\n\t\toldChildren: Array<Element | string> | undefined,\n\t): void {\n\t\tif (tag === Portal && (node == null || typeof node.nodeType !== \"number\")) {\n\t\t\tthrow new TypeError(\n\t\t\t\t`Portal root is not a node. Received: ${JSON.stringify(\n\t\t\t\t\tnode && node.toString(),\n\t\t\t\t)}`,\n\t\t\t);\n\t\t}\n\n\t\tif (\n\t\t\t!(\"innerHTML\" in props) &&\n\t\t\t// We don’t want to update elements without explicit children (<div/>),\n\t\t\t// because these elements sometimes have child nodes added via raw\n\t\t\t// DOM manipulations.\n\t\t\t// However, if an element has previously rendered children, we clear the\n\t\t\t// them because it would be surprising not to clear Crank managed\n\t\t\t// children, even if the new element does not have explicit children.\n\t\t\t(\"children\" in props || (oldChildren && oldChildren.length))\n\t\t) {\n\t\t\tif (children.length === 0) {\n\t\t\t\tnode.textContent = \"\";\n\t\t\t} else {\n\t\t\t\tlet oldChild = node.firstChild;\n\t\t\t\tlet i = 0;\n\t\t\t\twhile (oldChild !== null && i < children.length) {\n\t\t\t\t\tconst newChild = children[i];\n\t\t\t\t\tif (oldChild === newChild) {\n\t\t\t\t\t\toldChild = oldChild.nextSibling;\n\t\t\t\t\t\ti++;\n\t\t\t\t\t} else if (typeof newChild === \"string\") {\n\t\t\t\t\t\tif (oldChild.nodeType === Node.TEXT_NODE) {\n\t\t\t\t\t\t\tif ((oldChild as Text).data !== newChild) {\n\t\t\t\t\t\t\t\t(oldChild as Text).data = newChild;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\toldChild = oldChild.nextSibling;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tnode.insertBefore(document.createTextNode(newChild), oldChild);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\ti++;\n\t\t\t\t\t} else if (oldChild.nodeType === Node.TEXT_NODE) {\n\t\t\t\t\t\tconst nextSibling = oldChild.nextSibling;\n\t\t\t\t\t\tnode.removeChild(oldChild);\n\t\t\t\t\t\toldChild = nextSibling;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tnode.insertBefore(newChild, oldChild);\n\t\t\t\t\t\ti++;\n\t\t\t\t\t\t// TODO: This is an optimization but we need to think a little more about other cases like prepending.\n\t\t\t\t\t\tif (oldChild !== children[i]) {\n\t\t\t\t\t\t\tconst nextSibling = oldChild.nextSibling;\n\t\t\t\t\t\t\tnode.removeChild(oldChild);\n\t\t\t\t\t\t\toldChild = nextSibling;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// remove excess DOM nodes\n\t\t\t\twhile (oldChild !== null) {\n\t\t\t\t\tconst nextSibling = oldChild.nextSibling;\n\t\t\t\t\tnode.removeChild(oldChild);\n\t\t\t\t\toldChild = nextSibling;\n\t\t\t\t}\n\n\t\t\t\t// append excess children\n\t\t\t\tfor (; i < children.length; i++) {\n\t\t\t\t\tconst newChild = children[i];\n\t\t\t\t\tnode.appendChild(\n\t\t\t\t\t\ttypeof newChild === \"string\"\n\t\t\t\t\t\t\t? document.createTextNode(newChild)\n\t\t\t\t\t\t\t: newChild,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t},\n};\n\nexport class DOMRenderer extends Renderer<Node, string> {\n\tconstructor() {\n\t\tsuper(impl);\n\t}\n\n\trender(\n\t\tchildren: Children,\n\t\troot: Node,\n\t\tctx?: Context,\n\t): Promise<ElementValue<Node>> | ElementValue<Node> {\n\t\tif (root == null || typeof root.nodeType !== \"number\") {\n\t\t\tthrow new TypeError(\n\t\t\t\t`Render root is not a node. Received: ${JSON.stringify(\n\t\t\t\t\troot && root.toString(),\n\t\t\t\t)}`,\n\t\t\t);\n\t\t}\n\n\t\treturn super.render(children, root, ctx);\n\t}\n}\n\nexport const renderer = new DOMRenderer();\n\ndeclare global {\n\tmodule Crank {\n\t\tinterface EventMap extends GlobalEventHandlersEventMap {}\n\t}\n}\n"],"names":[],"mappings":";;;AASA,MAAM,aAAa,GAAG,4BAA4B,CAAC;MAEtC,IAAI,GAAwC;IACxD,KAAK,CAAC,IAAY;QACjB,IAAI,OAAO,QAAQ,CAAC,WAAW,KAAK,UAAU,EAAE;YAC/C,MAAM,QAAQ,GAAG,QAAQ,CAAC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G,CAAC,CAAC;KACzC;CACD;MAEY,QAAQ,GAAG,IAAI,WAAW;;"}
1
+ {"version":3,"file":"dom.js","sources":["../src/dom.ts"],"sourcesContent":["import {\n\tChildren,\n\tContext,\n\tElementValue,\n\tPortal,\n\tRenderer,\n\tRendererImpl,\n} from \"./crank.js\";\n\nconst SVG_NAMESPACE = \"http://www.w3.org/2000/svg\";\n\nexport const impl: Partial<RendererImpl<Node, string>> = {\n\tparse(text: string): ElementValue<Node> {\n\t\tif (typeof document.createRange === \"function\") {\n\t\t\tconst fragment = document.createRange().createContextualFragment(text);\n\t\t\treturn Array.from(fragment.childNodes);\n\t\t} else {\n\t\t\tconst childNodes = new DOMParser().parseFromString(text, \"text/html\").body\n\t\t\t\t.childNodes;\n\t\t\treturn Array.from(childNodes);\n\t\t}\n\t},\n\n\tscope(scope: string, tag: string | symbol): string | undefined {\n\t\t// TODO: Should we handle xmlns???\n\t\tswitch (tag) {\n\t\t\tcase Portal:\n\t\t\tcase \"foreignObject\":\n\t\t\t\treturn undefined;\n\t\t\tcase \"svg\":\n\t\t\t\treturn SVG_NAMESPACE;\n\t\t\tdefault:\n\t\t\t\treturn scope;\n\t\t}\n\t},\n\n\tcreate(tag: string | symbol, _props: unknown, ns: string | undefined): Node {\n\t\tif (typeof tag !== \"string\") {\n\t\t\tthrow new Error(`Unknown tag: ${tag.toString()}`);\n\t\t} else if (tag.toLowerCase() === \"svg\") {\n\t\t\tns = SVG_NAMESPACE;\n\t\t}\n\n\t\treturn ns ? document.createElementNS(ns, tag) : document.createElement(tag);\n\t},\n\n\tpatch(\n\t\t_tag: string | symbol,\n\t\t// TODO: Why does this assignment work?\n\t\tnode: HTMLElement | SVGElement,\n\t\tname: string,\n\t\t// TODO: Stricter typings?\n\t\tvalue: unknown,\n\t\toldValue: unknown,\n\t\tscope: string | undefined,\n\t): void {\n\t\tconst isSVG = scope === SVG_NAMESPACE;\n\t\tswitch (name) {\n\t\t\tcase \"style\": {\n\t\t\t\tconst style: CSSStyleDeclaration = node.style;\n\t\t\t\tif (style == null) {\n\t\t\t\t\tnode.setAttribute(\"style\", value as string);\n\t\t\t\t} else if (value == null || value === false) {\n\t\t\t\t\tnode.removeAttribute(\"style\");\n\t\t\t\t} else if (value === true) {\n\t\t\t\t\tnode.setAttribute(\"style\", \"\");\n\t\t\t\t} else if (typeof value === \"string\") {\n\t\t\t\t\tif (style.cssText !== value) {\n\t\t\t\t\t\tstyle.cssText = value;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tif (typeof oldValue === \"string\") {\n\t\t\t\t\t\tstyle.cssText = \"\";\n\t\t\t\t\t}\n\n\t\t\t\t\tfor (const styleName in {...(oldValue as {}), ...(value as {})}) {\n\t\t\t\t\t\tconst styleValue = value && (value as any)[styleName];\n\t\t\t\t\t\tif (styleValue == null) {\n\t\t\t\t\t\t\tstyle.removeProperty(styleName);\n\t\t\t\t\t\t} else if (style.getPropertyValue(styleName) !== styleValue) {\n\t\t\t\t\t\t\tstyle.setProperty(styleName, styleValue);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase \"class\":\n\t\t\tcase \"className\":\n\t\t\t\tif (value === true) {\n\t\t\t\t\tnode.setAttribute(\"class\", \"\");\n\t\t\t\t} else if (value == null) {\n\t\t\t\t\tnode.removeAttribute(\"class\");\n\t\t\t\t} else if (!isSVG) {\n\t\t\t\t\tif (node.className !== value) {\n\t\t\t\t\t\t(node as any)[\"className\"] = value;\n\t\t\t\t\t}\n\t\t\t\t} else if (node.getAttribute(\"class\") !== value) {\n\t\t\t\t\tnode.setAttribute(\"class\", value as string);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase \"innerHTML\":\n\t\t\t\tif (value !== oldValue) {\n\t\t\t\t\tnode.innerHTML = value as any;\n\t\t\t\t}\n\n\t\t\t\tbreak;\n\t\t\tdefault: {\n\t\t\t\tif (\n\t\t\t\t\tname in node &&\n\t\t\t\t\t// boolean properties will coerce strings, but sometimes they map to\n\t\t\t\t\t// enumerated attributes, where truthy strings (\"false\", \"no\") map to\n\t\t\t\t\t// falsy properties, so we use attributes in this case.\n\t\t\t\t\t!(\n\t\t\t\t\t\ttypeof value === \"string\" &&\n\t\t\t\t\t\ttypeof (node as any)[name] === \"boolean\"\n\t\t\t\t\t)\n\t\t\t\t) {\n\t\t\t\t\t// walk up the object's prototype chain to find the owner of the\n\t\t\t\t\t// named property\n\t\t\t\t\tlet obj = node;\n\t\t\t\t\tdo {\n\t\t\t\t\t\tif (Object.prototype.hasOwnProperty.call(obj, name)) {\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t} while ((obj = Object.getPrototypeOf(obj)));\n\n\t\t\t\t\t// get the descriptor for the named property and check whether it\n\t\t\t\t\t// implies that the property is writable\n\t\t\t\t\tconst descriptor = Object.getOwnPropertyDescriptor(obj, name);\n\t\t\t\t\tif (\n\t\t\t\t\t\tdescriptor != null &&\n\t\t\t\t\t\t(descriptor.writable === true || descriptor.set !== undefined)\n\t\t\t\t\t) {\n\t\t\t\t\t\t(node as any)[name] = value;\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\t// if the property wasn't writable, fall through to the code below\n\t\t\t\t\t// which uses setAttribute() instead of assigning directly.\n\t\t\t\t}\n\n\t\t\t\tif (value === true) {\n\t\t\t\t\tvalue = \"\";\n\t\t\t\t} else if (value == null || value === false) {\n\t\t\t\t\tnode.removeAttribute(name);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif (node.getAttribute(name) !== value) {\n\t\t\t\t\tnode.setAttribute(name, value as any);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t},\n\n\tarrange(\n\t\ttag: string | symbol,\n\t\tnode: Node,\n\t\tprops: Record<string, any>,\n\t\tchildren: Array<Element | string>,\n\t\t_oldProps: Record<string, any> | undefined,\n\t\toldChildren: Array<Element | string> | undefined,\n\t): void {\n\t\tif (tag === Portal && (node == null || typeof node.nodeType !== \"number\")) {\n\t\t\tthrow new TypeError(\n\t\t\t\t`Portal root is not a node. Received: ${JSON.stringify(\n\t\t\t\t\tnode && node.toString(),\n\t\t\t\t)}`,\n\t\t\t);\n\t\t}\n\n\t\tif (\n\t\t\t!(\"innerHTML\" in props) &&\n\t\t\t// We don’t want to update elements without explicit children (<div/>),\n\t\t\t// because these elements sometimes have child nodes added via raw\n\t\t\t// DOM manipulations.\n\t\t\t// However, if an element has previously rendered children, we clear the\n\t\t\t// them because it would be surprising not to clear Crank managed\n\t\t\t// children, even if the new element does not have explicit children.\n\t\t\t(\"children\" in props || (oldChildren && oldChildren.length))\n\t\t) {\n\t\t\tif (children.length === 0) {\n\t\t\t\tnode.textContent = \"\";\n\t\t\t} else {\n\t\t\t\tlet oldChild = node.firstChild;\n\t\t\t\tlet i = 0;\n\t\t\t\twhile (oldChild !== null && i < children.length) {\n\t\t\t\t\tconst newChild = children[i];\n\t\t\t\t\tif (oldChild === newChild) {\n\t\t\t\t\t\toldChild = oldChild.nextSibling;\n\t\t\t\t\t\ti++;\n\t\t\t\t\t} else if (typeof newChild === \"string\") {\n\t\t\t\t\t\tif (oldChild.nodeType === Node.TEXT_NODE) {\n\t\t\t\t\t\t\tif ((oldChild as Text).data !== newChild) {\n\t\t\t\t\t\t\t\t(oldChild as Text).data = newChild;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\toldChild = oldChild.nextSibling;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tnode.insertBefore(document.createTextNode(newChild), oldChild);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\ti++;\n\t\t\t\t\t} else if (oldChild.nodeType === Node.TEXT_NODE) {\n\t\t\t\t\t\tconst nextSibling = oldChild.nextSibling;\n\t\t\t\t\t\tnode.removeChild(oldChild);\n\t\t\t\t\t\toldChild = nextSibling;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tnode.insertBefore(newChild, oldChild);\n\t\t\t\t\t\ti++;\n\t\t\t\t\t\t// TODO: This is an optimization but we need to think a little more about other cases like prepending.\n\t\t\t\t\t\tif (oldChild !== children[i]) {\n\t\t\t\t\t\t\tconst nextSibling = oldChild.nextSibling;\n\t\t\t\t\t\t\tnode.removeChild(oldChild);\n\t\t\t\t\t\t\toldChild = nextSibling;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// remove excess DOM nodes\n\t\t\t\twhile (oldChild !== null) {\n\t\t\t\t\tconst nextSibling = oldChild.nextSibling;\n\t\t\t\t\tnode.removeChild(oldChild);\n\t\t\t\t\toldChild = nextSibling;\n\t\t\t\t}\n\n\t\t\t\t// append excess children\n\t\t\t\tfor (; i < children.length; i++) {\n\t\t\t\t\tconst newChild = children[i];\n\t\t\t\t\tnode.appendChild(\n\t\t\t\t\t\ttypeof newChild === \"string\"\n\t\t\t\t\t\t\t? document.createTextNode(newChild)\n\t\t\t\t\t\t\t: newChild,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t},\n};\n\nexport class DOMRenderer extends Renderer<Node, string> {\n\tconstructor() {\n\t\tsuper(impl);\n\t}\n\n\trender(\n\t\tchildren: Children,\n\t\troot: Node,\n\t\tctx?: Context,\n\t): Promise<ElementValue<Node>> | ElementValue<Node> {\n\t\tif (root == null || typeof root.nodeType !== \"number\") {\n\t\t\tthrow new TypeError(\n\t\t\t\t`Render root is not a node. Received: ${JSON.stringify(\n\t\t\t\t\troot && root.toString(),\n\t\t\t\t)}`,\n\t\t\t);\n\t\t}\n\n\t\treturn super.render(children, root, ctx);\n\t}\n}\n\nexport const renderer = new DOMRenderer();\n\ndeclare global {\n\tmodule Crank {\n\t\tinterface EventMap extends GlobalEventHandlersEventMap {}\n\t}\n}\n"],"names":[],"mappings":";;;AASA,MAAM,aAAa,GAAG,4BAA4B,CAAC;MAEtC,IAAI,GAAwC;IACxD,KAAK,CAAC,IAAY;QACjB,IAAI,OAA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ACD;KACD;EACA;MAEW,WAAY,SAAQ,QAAsB;IACtD;QACC,KAAK,CAAC,IAAI,CAAC,CAAC;KACZ;IAED,MAAM,CACL,QAAkB,EAClB,IAAU,EACV,GAAa;QAEb,IAAI,IAAI,IAAI,IAAI,IAAI,OAAO,IAAI,CAAC,QAAQ,KAAK,QAAQ,EAAE;YACtD,MAAM,IAAI,SAAS,CAClB,wCAAwC,IAAI,CAAC,SAAS,CACrD,IAAI,IAAI,IAAI,CAAC,QAAQ,EAAE,CACvB,EAAE,CACH,CAAC;SACF;QAED,OAAO,KAAK,CAAC,MAAM,CAAC,QAAQ,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC;KACzC;CACD;MAEY,QAAQ,GAAG,IAAI,WAAW;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@b9g/crank",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "description": "Write JSX-driven components with functions, promises and generators.",
5
5
  "homepage": "https://crank.js.org",
6
6
  "bugs": {
package/umd.js CHANGED
@@ -1739,16 +1739,24 @@
1739
1739
  // falsy properties, so we use attributes in this case.
1740
1740
  !(typeof value === "string" &&
1741
1741
  typeof node[name] === "boolean")) {
1742
- try {
1743
- if (node[name] !== value) {
1744
- node[name] = value;
1742
+ // walk up the object's prototype chain to find the owner of the
1743
+ // named property
1744
+ let obj = node;
1745
+ do {
1746
+ if (Object.prototype.hasOwnProperty.call(obj, name)) {
1747
+ break;
1745
1748
  }
1749
+ } while ((obj = Object.getPrototypeOf(obj)));
1750
+ // get the descriptor for the named property and check whether it
1751
+ // implies that the property is writable
1752
+ const descriptor = Object.getOwnPropertyDescriptor(obj, name);
1753
+ if (descriptor != null &&
1754
+ (descriptor.writable === true || descriptor.set !== undefined)) {
1755
+ node[name] = value;
1746
1756
  return;
1747
1757
  }
1748
- catch (err) {
1749
- // some properties are readonly so we fallback to setting them as
1750
- // attributes
1751
- }
1758
+ // if the property wasn't writable, fall through to the code below
1759
+ // which uses setAttribute() instead of assigning directly.
1752
1760
  }
1753
1761
  if (value === true) {
1754
1762
  value = "";