Live data from Hacker News

Show HN: A TO-DO app that fits inside a single tweet

ruky.me

51–56 of 56 posts

Re: Show HN: A TO-DO app that fits inside a single tweet

#51

Earlier quoted context omitted.

I like the createElement/appendChild-and-return-child and set-innerHTML-and-return-child combinations, they’re just the right level of repetition for minification. As it stands, the use of innerHTML is bad, because it allows an injection attack (which amuses me given you said “no HTML injection”—though I know what you meant by that); you should use innerText instead. (You should always think very carefully before set…

Nice finishing touch. I think this is the shortest it could get in this approach with semantic tags. I just learnt about insertAdjacentHTML & last variable return of arrow functions. (not using widely due to compatibility) Also, you're absolutely right about XSS & Button. I thought innerText only removing text, keeping the :) So, learnt some new stuff. Thank you! In your code, the initially outputs "undefined". So we…

> last variable return of arrow functions. (not using widely due to compatibility)

It’s not last variable return such as you get in expression-oriented languages like Ruby or Rust; rather, it’s that if what follows the fat arrow is not a curly brace, it’s parsed as an expression, which is made to be the value returned. So `a => { a }` is the void function, but `a => { return a }` and `a => a` are identity functions. Expressed as an approximate grammar, it’s

  arrow-function = arrow-function-parameters "=>" ( "{" *statement "}" | expression-minus-object-literals )
All forms of arrow functions were introduced at the same time, in ES6 (roughly meaning “everything since but excluding IE”). `a => { return a; }` has identical browser support to `a => a`, so if you use arrow functions you might as well switch to the expression form.

Re: Show HN: A TO-DO app that fits inside a single tweet

#52
post #41

Earlier quoted context omitted.

Nice use of real and submit handling! Note that HN apparently ate your "delete" character(s) (happened to me with "recycle" sign as well). Props for HTML escaping, was not in task, as well as separate delete buttons. Note though that entities in the document.write are transformed, so the "html escaping" is broken in effect; either double escaping or codepoints should make it work. ` ${x.value.replace(/\u0026/g,`\u002…

I used U+2718 HEAVY BALLOT X and forgot that it’d be trashed. On the double escaping thing, I thought of it earlier on and was careful to make sure it did the right thing, but then I transformed the code in such a way that that it ended up being read by the HTML parser again, and neglected to switch it to double escaping, which was rather careless of me. \u0026 can be more briefly written as & (saving one charact…

Thanks. And I'm glad xmp still lives in developers' minds.

But whoa, this is approach is really impressive. That "e" function for creation and appending elements is witty -- I wouldn't think it is possible to have such clean and terse JS element "factory" that in effect outperforms (in character count metric) literal HTML content.

(I must confess it took me a while to wrap my head around it, so if anyone is struggling like me, this is the above code unrolled:)

    var Doc = document;
    var FormElem;
    var BodyElem;
    var AppendElem =
    (
     aParentElem
     // reference
     ,
     aTagElemRet
     // tag name for new child, will become child itself
     ,
     aContent
     // (text) content for new child
    ) =>
    (
     aParentElem.append(aTagElemRet = Doc.createElement(aTagElemRet))
     // aTagElemRet tag name (string) becomes HTMLElement
     // and is immediately appended at the end of reference element
     ,
     // comma operator evaluates both sides, returns right
     aContent && aTagElemRet.append(aContent)
     // if third argument is truthy, put it into the new HTMLElement
     ,
     aTagElemRet
     // expression results in this so arrow function returns the new HTMLElement
    );
    var InputElem = AppendElem
     (
      FormElem = AppendElem
       (
        BodyElem = Doc.body, // = parent
        "form" // = tagName
       ), // = parent
      "input" //= tagName
      // no content
     );
    var ListElem = AppendElem(
     BodyElem, // = parent
     "ol" // = tagName
    );
    FormElem.onsubmit =
     pParamLI =>
     // event handler gets SubmitEvent object as a sole argument
     // but it is not needed, so parameter will be reassigned to
     // newly created LI element
     (
      AppendElem(
       pParamLI = AppendElem(
        ListElem, // = parent
        "li", // = tagName
        InputElem.value + " " // = (text) content
       ), // = parent
       "button", // = tagName
       "×" // = content
      ).onclick = _ => pParamLI.remove(), !1
      // inline handler attached to created button
      // ClickEvent passed in _ is not used
      // return !1 what evaluates to false
      // inline handler returning false prevents default action
      // in this case form submission
      // button is in default type, so "submit" would be dispatched
      // on its form otherwise
     );

Re: Show HN: A TO-DO app that fits inside a single tweet

#53

Earlier quoted context omitted.

Nice finishing touch. I think this is the shortest it could get in this approach with semantic tags. I just learnt about insertAdjacentHTML & last variable return of arrow functions. (not using widely due to compatibility) Also, you're absolutely right about XSS & Button. I thought innerText only removing text, keeping the :) So, learnt some new stuff. Thank you! In your code, the initially outputs "undefined". So we…

> last variable return of arrow functions. (not using widely due to compatibility) It’s not last variable return such as you get in expression-oriented languages like Ruby or Rust; rather, it’s that if what follows the fat arrow is not a curly brace, it’s parsed as an expression, which is made to be the value returned. So `a => { a }` is the void function, but `a => { return a }` and `a => a` are identity functions.…

Got it. Thanks!

Re: Show HN: A TO-DO app that fits inside a single tweet

#56
post #55
post #54

My solution in 155 chars, vanilla JS document.write(` "+i.value'>+ x `) Demo: https://codepen.io/xem/pen/dypVzaw?editors=1100 Cheers :)

Awesome What are the $ and H, is it some sort of JQuery? Can you explain :)

The "ungolfed" code looks like this:

"+i.value'>+ X

There are 4 occurrences of "innerHTML" and 3 occurrences of " onclick='" so I replaced those two substrings with H and o.

The whole HTML is rendered using an ES6 template literal (document.write(` ... `))

Everytime a ${H} or a ${o} is present in the template, the value of H or o is copied here.

It's a native feature of JS since ES6.

Besides that, the second implicitly closes the first , so no need to use . The id's are global, that's why we can access i.value or u.innerHTML directly. In onclick, "this" (the current element) is implicit. Finally, the strikethrough is done using the (deprecated but still working) .strike() String method.

Post reply on HN