• The shortest DOM machine I have ever written [Not an Aprils Fooljoke]

    From Mild Shock@janburse@fastmail.fm to comp.lang.prolog on Wed Apr 2 23:07:58 2025
    From Newsgroup: comp.lang.prolog

    Hi,

    Ok, today was an interesting day. Well yesterday
    was also an interesting day , but I didn't want
    to post a DOM machine on april fools day.

    But yesterday I came up with:

    function fiddle_out(data, buf) {
    let i = 0;
    let k = buf.indexOf("<", i);
    while (k !== -1) {
    let k2 = buf.indexOf(">", k+1);
    if (buf.charCodeAt(k+1) === 47) { // '/'
    data.insertAdjacentHTML("beforeend",
    buf.substring(i, k));
    data = data.parentElement;
    i = k2+1;
    k = buf.indexOf("<", i);
    } else if (buf.charCodeAt(k2-1) !== 47) { // '/'
    data.insertAdjacentHTML("beforeend",
    buf.substring(i, k2 + 1));
    data = data.lastElementChild;
    i = k2+1;
    k = buf.indexOf("<", i);
    } else {
    k = buf.indexOf("<", k2+1);
    }
    }
    data.insertAdjacentHTML("beforeend", buf.substring(i));
    return data;
    }

    And today I came up with the idea that fiddle_out
    has not only data as an input argument, but also as
    a return value. Namely the DOM cursor before and after.

    My streams now have general understanding of
    Sinks that are state machines. Every Sink can
    be a state machine now, and modify this data.

    Holy Cow!

    Bye
    --- Synchronet 3.20c-Linux NewsLink 1.2
  • From Mild Shock@janburse@fastmail.fm to comp.lang.prolog on Wed Apr 2 23:12:12 2025
    From Newsgroup: comp.lang.prolog


    BTW: Who says writing something first in Prolog doesn't
    lead to short "native" host code implementations later?

    Mild Shock schrieb:
    Hi,

    Ok, today was an interesting day. Well yesterday
    was also an interesting day , but I didn't want
    to post a DOM machine on april fools day.

    But yesterday I came up with:

    function fiddle_out(data, buf) {
        let i = 0;
        let k = buf.indexOf("<", i);
        while (k !== -1) {
            let k2 = buf.indexOf(">", k+1);
            if (buf.charCodeAt(k+1) === 47) { // '/'
                data.insertAdjacentHTML("beforeend",
                      buf.substring(i, k));
                data = data.parentElement;
                i = k2+1;
                k = buf.indexOf("<", i);
            } else if (buf.charCodeAt(k2-1) !== 47) { // '/'
                data.insertAdjacentHTML("beforeend",
                      buf.substring(i, k2 + 1));
                data = data.lastElementChild;
                i = k2+1;
                k = buf.indexOf("<", i);
            } else {
                k = buf.indexOf("<", k2+1);
            }
        }
        data.insertAdjacentHTML("beforeend", buf.substring(i));
        return data;
    }

    And today I came up with the idea that fiddle_out
    has not only data as an input argument, but also as
    a return value. Namely the DOM cursor before and after.

    My streams now have general understanding of
    Sinks that are state machines. Every Sink can
    be a state machine now, and modify this data.

    Holy Cow!

    Bye

    --- Synchronet 3.20c-Linux NewsLink 1.2