Skip to content

Instantly share code, notes, and snippets.

@yckart
Forked from 140bytes/LICENSE.txt
Last active November 21, 2019 16:18
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save yckart/6627671 to your computer and use it in GitHub Desktop.
Save yckart/6627671 to your computer and use it in GitHub Desktop.
Domster | Syntactic sugar for DOM-traversing

Inspired by https://github.com/dunxrion/voyeur.js

##Usage

<nav>
  <ul>
    <li>1st</li>
    <li>2nd</li>
    <li>3rd</li>
  </ul>
</nav>
domster( document.body.querySelectorAll('*') ) // => undefined

document.body.nav.ul.li  // => <li>3rd</li>
document.body.nav.ul.lis // => [li, li, li]

The following is 150-bytes, but more compliant. It returns the root element (what makes the usage more voyeur-like) and specifies the first instead of the last dom-node:

function (
  a, // A Nodelist whichto use as root
  b, // iterate-placeholder
  c, // elem-placeholder
  d, // tags-placeholder
  e, // tag-placeholder
  f, // parent-placeholder
  g  // root-placeholder
) {
  for (b = 0; c = a[b++];

    // parent has already the same `tagName` as property
    // so there is more than one element in the tree
    (f = c.parentNode)[e] && f[d].push(c) ||

    // otherwise add/assign an array
    // containing the element
    (f[d] = [c]),

    // assign the first single node to the parent-node
    f[e] = f[e] || c,

    // assign the root element
    g = g || f
  )

  d = (e = c.tagName.toLowerCase()) + 's';

  // return the root element
  return g
}
var dom = domster( document.body.querySelectorAll('*') ) // => document.body

dom.nav.ul.li  // => <li>1st</li>
dom.nav.ul.lis // => [li, li, li]
function (
a, // A Nodelist whichto use as root
b, // iterate-placeholder
c, // elem-placeholder
d, // tags-placeholder
e, // tag-placeholder
f // parent-placeholder
) {
for (b = 0; c = a[b++];
// parent has already the same `tagName` as property
// so there is more than one element in the tree
(f = c.parentNode)[e] && f[d].push(c) ||
// otherwise add/assign an array
// containing the element
(f[d] = [c]),
// assign the single node to the parent-node
f[e] = c)
d = (e = c.tagName.toLowerCase()) + 's'
}
function(a,b,c,d,e,f){for(b=0;c=a[b++];(f=c.parentNode)[e]&&f[d].push(c)||(f[d]=[c]),f[e]=c)d=(e=c.tagName.toLowerCase())+'s'}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2013 Yannick Albert <http://yckart.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "Domster",
"description": "Syntactic sugar for DOM-traversing.",
"keywords": [
"dom",
"node",
"traverse",
"manipulate"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>140byt.es</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
var domster = function(a,b,c,d,e,f){for(b=0;c=a[b++];(f=c.parentNode)[e]&&f[d].push(c)||(f[d]=[c]),f[e]=c)d=(e=c.tagName.toLowerCase())+'s'};
domster( document.body.querySelectorAll('*') );
document.body.div.b.innerHTML = '140byt.es';
</script>
@atk
Copy link

atk commented Sep 20, 2013

Use the for loop - don't waste those semicolons/brackets:

function(a,b,c,d,e,f){for(b=0;c=a[b++];(f=c.parentNode)[e]&&f[d].push(c)||(f[d]=[c]),f[e]=c)d=(e=c.tagName.toLowerCase())+'s'}

@nbubna
Copy link

nbubna commented Sep 23, 2013

Hah, dot-traversal in 140 bytes! And the 's' idea has intriguing possibilities.

@Nashorn
Copy link

Nashorn commented Nov 21, 2019

Pretty cool. No longer accurate after DOM subtree modifications though. So that 150-byte gold medal goes outta the window :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment