Skip to content

Instantly share code, notes, and snippets.

@dfkaye
dfkaye / element-to-xpath.js
Created March 4, 2024 08:52
generate an XPath expression for an element in an HTML document
// 3 March 2024
// generate an XPath expression for an element in an HTML document
// ONLY WORKS FOR ELEMENTS SO FAR, NO FANCY NODE TYPE SELECTION YET
// for an element tree structure such as
// <a><b><c><d id="test"> this is <e> embedded </e> text </d></c></b></a>
// the XPath expression for window.test (id="test") should be
// //BODY[1]/A[1]/B[1]/C[1]/D[1]
// and passing that to document.evaluate() should return
@dfkaye
dfkaye / xslt-document-fn.js
Last active May 18, 2024 01:39
import XML source documents into XSLT using the document() function.
// 2 March 2024
// import XML source documents into XSLT using the document() function.
// example markup borrowed from
// https://www.abbeyworkshop.com/howto/xslt/document/
// parsing function defined in gist 19 June 2023,
// "Using XML, XSLT, XHR, to parse and serialize HTML in the browser",
// https://gist.github.com/dfkaye/94bc03241c4c3bf458ab0dc5d56b1958
// 17 May 2024
@dfkaye
dfkaye / forms-elements-access-patterns.js
Created February 16, 2024 06:28
many ways to access forms and elements in the DOM
// 15 February 2024
// many ways to access forms and elements in the DOM
var input = document.createElement("input");
input.name = "E";
var form = document.createElement("form");
form.name = "F";
form.appendChild(input);
document.body.appendChild(form);
@dfkaye
dfkaye / shadow-dom-mutation-benchmark.js
Last active December 12, 2023 22:20
Shadow DOM mutation benchmark: How long does it take to update the Shadow DOM?
// 12 December 2023
// Shadow DOM mutation benchmark
// How long does it take to update the Shadow DOM?
// Successor to "How long does it take to update the DOM?" gist at
// https://gist.github.com/dfkaye/6ceef75ee61892428ef09b3b67138cd5
// In this test, as before, we create a table with a tbody, 1000 rows (<tr>),
// where each row contains 4 cells (<td>), and perform updates on all 4000 cells
@dfkaye
dfkaye / dom-mutation-benchmark.js
Last active December 12, 2023 22:19
DOM mutation benchmark: How long does it take to update the DOM?
// 11 December 2023
// DOM mutation benchmark
// How long does it take to update the DOM?
// Continued by "How long does it take to update the Shadow DOM?" gist at
// https://gist.github.com/dfkaye/1c4068e05b5d891a394d8c97fbe87684
// When it comes to those fastest DOM mutation framework benchmarks, ask how
@dfkaye
dfkaye / dynamic-exit-condition-convergence-test.js
Created November 30, 2023 00:04
convergence test for pseudo-stochastic monte carloesque dynamically updated loop exit condition
// 21 November 2023
// convergence test for pseudo-stochastic monte carloesque dynamically updated
// loop exit condition
// program proof that loops with changing random length converge on square root
// of max length (from a Tweet the other day that jonathan blow responded to).
// T asked why programmers think a loop with length reset to random * 100 will
// average to 50 instead of 10 (well, actually, it will converge on the square
// root of max plus 2, given the first two steps always execute).
@dfkaye
dfkaye / whats-in-an-array.js
Last active March 22, 2024 07:09
What's "in" an array?
// 29 September 2023
// What's in an array?
// Here's an array:
var a = [1,2,3,4];
// If we use want only the *own* properties of an array, we can use the
// Object.keys() method to inspect it.
@dfkaye
dfkaye / collect-comment-text-attribute-childnodes.js
Created November 29, 2023 23:52
Collect a node's Comment, Text, and Attribute child nodes
// 21 September 2023
// Collect a node's Comment, Text, and Attribute child nodes
// should break this up into separate functions focused on node type
// then find items by their nodeValue
function collect(node) {
var comments = [];
var text = [];
var attributes = [];
@dfkaye
dfkaye / onpropertychange-signal-v.9.js
Last active April 14, 2024 06:30
onpropertychange signal v.9 -- Success! v.9 supports both Object and EventTarget
// 27 September 2023
// onpropertychange signal v.9
// cf. v.8 https://gist.github.com/dfkaye/548622151971122110bf4047ae3ac432
// and v.1 https://gist.github.com/dfkaye/619c5f31080fce2cd383ac966e132311
// Success, at last! v.9 supports both Object and EventTarget.
// Investigating where proxy on an array allows array.push mutations, the goal
// here was to get the proxy to work on EventTarget and Object, this time with
@dfkaye
dfkaye / onpropertychange-signal-v.8.js
Created November 29, 2023 23:37
onpropertychange signal v.8 -- trying to simplify the prototype stack
// 26 September 2023
// onpropertychange signal v.8
// cont'd from https://gist.github.com/dfkaye/3affad4c43e363a84ac4320eae375129
// Going back to v.1 prototype enhancement/pollution, (see
// https://gist.github.com/dfkaye/619c5f31080fce2cd383ac966e132311), trying to
// simplify the prototype stack.
// The signals and delegate properties are assigned on the target using the