8a5e8d74 by Adam Heath

Tweaks to the debug processing, it's now a number, which counts down,

and only when not 0 will it show things.  To show all debug all the way
down, pass in -1.
1 parent 5202ad66
1 --- 1 ---
2 import Match from './match.astro' 2 import Match from './match.astro'
3 3
4 const { props: { parent, children, debug = false, replacers, slotHandler } } = Astro 4 const { props: { parent, children, debug = 0, replacers, slotHandler } } = Astro
5 5
6 //console.log('Children:render', { parent, children, replacers }) 6 //console.log('Children:render', { parent, children, replacers })
7 if (debug) {
8 console.log('children.astro:debug', { parent, children, replacers })
9 }
10 const nextDebug = debug ? debug - 1 : 0
7 --- 11 ---
8 { 12 {
9 Array.isArray(children) ? 13 Array.isArray(children) ?
10 children.map((child, index) => <Match parent={parent} node={child} index={index} debug={debug} replacers={replacers} slotHandler={slotHandler}/>) 14 children.map((child, index) => <Match parent={parent} node={child} index={index} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>)
11 : !children ? '' 15 : !children ? ''
12 : <Match parent={parent} node={children} index={0} debug={debug} replacers={replacers} slotHandler={slotHandler}/> 16 : <Match parent={parent} node={children} index={0} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>
13 } 17 }
......
...@@ -36,18 +36,18 @@ const nthChildPos = (node, parent) => filterChildElements(parent).findIndex((chi ...@@ -36,18 +36,18 @@ const nthChildPos = (node, parent) => filterChildElements(parent).findIndex((chi
36 36
37 const filterChildElementsMatcher = (context, child, parent, i) => child.type === ELEMENT_NODE 37 const filterChildElementsMatcher = (context, child, parent, i) => child.type === ELEMENT_NODE
38 38
39 type Matcher = (context, node, parent, i, debug) => boolean 39 type Matcher = (context, node: NodeType, parent?: NodeType, i: number, debug: number) => boolean
40 type MatcherProducer = () => Matcher
40 type AttrValueMatch = (string) => string 41 type AttrValueMatch = (string) => string
41 type ParentFilter = (context, node, parent, i) => boolean
42 42
43 const compileMatcher = (ast: AST, selector: string): Matcher => { 43 const compileMatcher = (ast: AST, selector: string): MatcherProducer => {
44 let counter = 0 44 let counter = 0
45 45
46 const neededContext = [] 46 const neededContext = []
47 const selectorCacheCounter = counter++ 47 const selectorCacheCounter = counter++
48 neededContext[ selectorCacheCounter ] = () => new WeakMap() 48 neededContext[ selectorCacheCounter ] = () => new WeakMap()
49 49
50 const findChildren = (context, parent: NodeType, selector: string, matcher: Matcher): array[NodeType] => { 50 const findChildren = (context, parent?: NodeType, selector: string, matcher: Matcher): array[NodeType] => {
51 if (parent === null) console.log('null parent', new Error()) 51 if (parent === null) console.log('null parent', new Error())
52 if (parent === null) return [] 52 if (parent === null) return []
53 let selectorCache = context[ selectorCacheCounter ].get(parent) 53 let selectorCache = context[ selectorCacheCounter ].get(parent)
...@@ -90,9 +90,7 @@ const compileMatcher = (ast: AST, selector: string): Matcher => { ...@@ -90,9 +90,7 @@ const compileMatcher = (ast: AST, selector: string): Matcher => {
90 } 90 }
91 } 91 }
92 return (context, node, parent, i, debug) => { 92 return (context, node, parent, i, debug) => {
93 const r = nMatch(i) 93 return nMatch(i)
94 console.log('foo', {argument, A, B, debug, i, r})
95 return r
96 } 94 }
97 } 95 }
98 } 96 }
...@@ -299,7 +297,7 @@ const compileMatcher = (ast: AST, selector: string): Matcher => { ...@@ -299,7 +297,7 @@ const compileMatcher = (ast: AST, selector: string): Matcher => {
299 } 297 }
300 } 298 }
301 299
302 export const createMatcher = (selector: string) => { 300 export const createMatcher = (selector: string): Matcher => {
303 const matcherCreater = selectorCache.get(selector) 301 const matcherCreater = selectorCache.get(selector)
304 if (matcherCreater) return matcherCreater() 302 if (matcherCreater) return matcherCreater()
305 const ast = elParse(selector) 303 const ast = elParse(selector)
...@@ -309,7 +307,7 @@ export const createMatcher = (selector: string) => { ...@@ -309,7 +307,7 @@ export const createMatcher = (selector: string) => {
309 return newMatcherCreater() 307 return newMatcherCreater()
310 } 308 }
311 309
312 export const parseHtml = (html: string) => { 310 export const parseHtml = (html: string): NodeType => {
313 const cached = parsedHtmlCache.get(html) 311 const cached = parsedHtmlCache.get(html)
314 if (cached) return cached 312 if (cached) return cached
315 const doc = ultraParse(html) 313 const doc = ultraParse(html)
...@@ -317,7 +315,7 @@ export const parseHtml = (html: string) => { ...@@ -317,7 +315,7 @@ export const parseHtml = (html: string) => {
317 return doc 315 return doc
318 } 316 }
319 317
320 export const findNode = (doc: NodeType, selector: string) => { 318 export const findNode = (doc: NodeType, selector: string): NodeType => {
321 if (!selector) return doc 319 if (!selector) return doc
322 let docCache = findNodeCache.get(doc) 320 let docCache = findNodeCache.get(doc)
323 if (!docCache) { 321 if (!docCache) {
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
2 import { ELEMENT_NODE, TEXT_NODE } from 'ultrahtml' 2 import { ELEMENT_NODE, TEXT_NODE } from 'ultrahtml'
3 import Node from './node.astro' 3 import Node from './node.astro'
4 4
5 const { props: { parent = null, node, index = 0, debug = false, replacers, slotHandler } } = Astro 5 const { props: { parent = null, node, index = 0, debug = 0, replacers, slotHandler } } = Astro
6 const { name: Name, attributes } = node 6 const { name: Name, attributes } = node
7 7
8 if (debug) { 8 if (debug) {
...@@ -17,8 +17,9 @@ for (const [ matcher, handler ] of replacers) { ...@@ -17,8 +17,9 @@ for (const [ matcher, handler ] of replacers) {
17 } 17 }
18 } 18 }
19 19
20 const nextDebug = debug ? debug - 1 : 0
20 --- 21 ---
21 { 22 {
22 slotName ? slotHandler(slotName, node) 23 slotName ? slotHandler(slotName, node)
23 : <Node parent={parent} node={node} index={index} debug={debug} replacers={replacers} slotHandler={slotHandler}/> 24 : <Node parent={parent} node={node} index={index} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>
24 } 25 }
......
...@@ -5,7 +5,7 @@ import Children from './children.astro' ...@@ -5,7 +5,7 @@ import Children from './children.astro'
5 5
6 interface Props { 6 interface Props {
7 html?: string, 7 html?: string,
8 debug?: boolean, 8 debug?: number,
9 xpath?: string, 9 xpath?: string,
10 replacements?: Replacements, 10 replacements?: Replacements,
11 11
...@@ -16,21 +16,22 @@ interface Props { ...@@ -16,21 +16,22 @@ interface Props {
16 } 16 }
17 17
18 18
19 const { props: { parent = null, node, index = 0, debug = false, replacers, slotHandler } } = Astro 19 const { props: { parent = null, node, index = 0, debug = 0, replacers, slotHandler } } = Astro
20 const { name: Name, attributes } = node 20 const { name: Name, attributes } = node
21 21
22 if (debug) { 22 if (debug) {
23 console.log('Node.astro:debug', {node}) 23 console.log('node.astro:debug', {node})
24 } 24 }
25 const nextDebug = debug ? debug - 1 : 0
25 --- 26 ---
26 { 27 {
27 node.type === TEXT_NODE ? <Fragment set:html={node.value}/> 28 node.type === TEXT_NODE ? <Fragment set:html={node.value}/>
28 : node.type === ELEMENT_NODE ? ( 29 : node.type === ELEMENT_NODE ? (
29 node.isSelfClosingTag ? <Name {...attributes}/> 30 node.isSelfClosingTag ? <Name {...attributes}/>
30 : <Name {...attributes}> 31 : <Name {...attributes}>
31 <Children parent={node} children={node.children} debug={debug} replacers={replacers} slotHandler={slotHandler}/> 32 <Children parent={node} children={node.children} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>
32 </Name> 33 </Name>
33 ) : ( 34 ) : (
34 <Children parent={node} children={node.children} debug={debug} replacers={replacers} slotHandler={slotHandler}/> 35 <Children parent={node} children={node.children} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>
35 ) 36 )
36 } 37 }
......
...@@ -7,13 +7,13 @@ import type { SlotHandler, Replacements } from './astro.ts' ...@@ -7,13 +7,13 @@ import type { SlotHandler, Replacements } from './astro.ts'
7 7
8 interface Props { 8 interface Props {
9 html?: string, 9 html?: string,
10 debug?: boolean, 10 debug?: number,
11 xpath?: string, 11 xpath?: string,
12 replacements?: Replacements, 12 replacements?: Replacements,
13 } 13 }
14 14
15 const { props } = Astro 15 const { props } = Astro
16 const { html, debug = false, xpath, replacements = {} } = props 16 const { html, debug = 0, xpath, replacements = {} } = props
17 const doc = props.node ? props.node : parseHtml(props.html) 17 const doc = props.node ? props.node : parseHtml(props.html)
18 18
19 const node = xpath ? findNode(doc, xpath) : doc 19 const node = xpath ? findNode(doc, xpath) : doc
...@@ -22,5 +22,6 @@ const replacers = Object.entries(replacements).map(([ selector, handler ]) => [ ...@@ -22,5 +22,6 @@ const replacers = Object.entries(replacements).map(([ selector, handler ]) => [
22 const slotHandler: SlotHandler = (slotName, node) => { 22 const slotHandler: SlotHandler = (slotName, node) => {
23 return Astro.slots.render(slotName, [ node, { slotHandler, replacers } ] ) 23 return Astro.slots.render(slotName, [ node, { slotHandler, replacers } ] )
24 } 24 }
25 const nextDebug = debug ? debug - 1 : 0
25 --- 26 ---
26 <Match node={node} debug={debug} replacers={replacers} slotHandler={slotHandler}/> 27 <Match node={node} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>
......