dperini / nwsapi

Fast CSS Selectors API Engine
MIT License
105 stars 36 forks source link

micro optimization: reuse resolvers for pseudo elements #39

Closed milahu closed 4 years ago

milahu commented 4 years ago

edit: full revoke. see PR #40

edit: partial revoke: nwsapi does use different resolvers for base-element vs pseudo-element:

if (!(e.nodeType == 1)) {

.... this condition is added for the selector body>div::after compared to body>div

nodeType 1 is ELEMENT_NODE

node types in jsdom ```js module.exports = Object.freeze({ ELEMENT_NODE: 1, ATTRIBUTE_NODE: 2, TEXT_NODE: 3, CDATA_SECTION_NODE: 4, // historical ENTITY_REFERENCE_NODE: 5, // historical ENTITY_NODE: 6, // historical PROCESSING_INSTRUCTION_NODE: 7, COMMENT_NODE: 8, DOCUMENT_NODE: 9, DOCUMENT_TYPE_NODE: 10, DOCUMENT_FRAGMENT_NODE: 11, NOTATION_NODE: 12 // historical }); ```

--

currently nwsapi compiles different resolvers for (base-) elements and pseudo-elements

for example it compiles two different resolvers for the selectors div and div::before

but it should compile only one resolver since all resolvers return the same (base-) element

so, selectors in function _matches(selectors, element, callback) should be processed with something like

const baseSel = selectors.replace(/::[a-z-]+/gi, '')

or more verbose

const pseudoElementNames = [
  'after',
  'backdrop'
  'before',
  'cue',
  'cue-region',
  'first-letter',
  'first-line',
  'grammar-error',
  'marker',
  //'part()',
  'placeholder',
  'selection',
  //'slotted()',
  'spelling-error',
];

const re = new RegExp('::('+pseudoElementNames.join('|')+')', 'gi')
const baseSel = selectors.replace(re, '')
milahu commented 4 years ago

same goes for div::before and div:before both selectors should use the same resolver

const baseSel = selector
.replace(/:?:(after|before|first-letter|first-line)/g, '');

source

backward compatibility is a pain ....

dperini commented 4 years ago

@milahu thank you so much for the suggestion and for the code change in the pull request. I will have a look and do a test cycle to make sure nothing breaks, then I will commit your request.

milahu commented 4 years ago

: ]

this issue is obsolete (full revoke), resolvers can not be reused they should match the pseudo element, not the base element

the old implementation was just a placeholder my proposed solution is in PR #40