acstll / parse-sel

Parse hyperscript selector strings
3 stars 0 forks source link

Question:why set the tagName to `div` when selector is undefined #1

Open Jocs opened 6 years ago

Jocs commented 6 years ago

Hi,

A little confused about the source code:

  selector = selector || ''
 // other codes
  if (notClassId.test(tagParts[1]) || selector === '') {
    tagName = 'div'
  }

if the selector is undefined, according to snabbdom vnode data structure, it a text node, why add a div tagName?

acstll commented 6 years ago

Hey there,

short answer is this

If the tag name begins with a class or id, it defaults to a <div>.

https://github.com/hyperhype/hyperscript#default-tag-name

The code in this library was directly taken from here, the original virtual-dom library, which uses (used?) hyperscript to represent the DOM.

Snabbdom uses that same syntax for creating elements.

If what you say is correct and an undefined selector means it is a text node, then this is a bit weird.

Jocs commented 6 years ago

If the tag name begins with a class or id, it defaults to a \

.

So we should not translate undefined to ''? in other words. selector = selector || '' is not needed?

And need tell the user who use this library it is not a valid selector if he pass undefined to parse-sel?

acstll commented 6 years ago

I think undefined is valid if we keep the premise that no selector means a default div.

I wouldn't change any code if we don't need to change any behaviour. Don't you think?

Here are the tests related to the default div: https://github.com/acstll/parse-sel/blob/master/test.js#L8-L12