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
---
import Match from './match.astro'
const { props: { parent, children, debug = false, replacers, slotHandler } } = Astro
const { props: { parent, children, debug = 0, replacers, slotHandler } } = Astro
//console.log('Children:render', { parent, children, replacers })
if (debug) {
console.log('children.astro:debug', { parent, children, replacers })
}
const nextDebug = debug ? debug - 1 : 0
---
{
Array.isArray(children) ?
children.map((child, index) => <Match parent={parent} node={child} index={index} debug={debug} replacers={replacers} slotHandler={slotHandler}/>)
children.map((child, index) => <Match parent={parent} node={child} index={index} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>)
: !children ? ''
: <Match parent={parent} node={children} index={0} debug={debug} replacers={replacers} slotHandler={slotHandler}/>
: <Match parent={parent} node={children} index={0} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>
}
......
......@@ -36,18 +36,18 @@ const nthChildPos = (node, parent) => filterChildElements(parent).findIndex((chi
const filterChildElementsMatcher = (context, child, parent, i) => child.type === ELEMENT_NODE
type Matcher = (context, node, parent, i, debug) => boolean
type Matcher = (context, node: NodeType, parent?: NodeType, i: number, debug: number) => boolean
type MatcherProducer = () => Matcher
type AttrValueMatch = (string) => string
type ParentFilter = (context, node, parent, i) => boolean
const compileMatcher = (ast: AST, selector: string): Matcher => {
const compileMatcher = (ast: AST, selector: string): MatcherProducer => {
let counter = 0
const neededContext = []
const selectorCacheCounter = counter++
neededContext[ selectorCacheCounter ] = () => new WeakMap()
const findChildren = (context, parent: NodeType, selector: string, matcher: Matcher): array[NodeType] => {
const findChildren = (context, parent?: NodeType, selector: string, matcher: Matcher): array[NodeType] => {
if (parent === null) console.log('null parent', new Error())
if (parent === null) return []
let selectorCache = context[ selectorCacheCounter ].get(parent)
......@@ -90,9 +90,7 @@ const compileMatcher = (ast: AST, selector: string): Matcher => {
}
}
return (context, node, parent, i, debug) => {
const r = nMatch(i)
console.log('foo', {argument, A, B, debug, i, r})
return r
return nMatch(i)
}
}
}
......@@ -299,7 +297,7 @@ const compileMatcher = (ast: AST, selector: string): Matcher => {
}
}
export const createMatcher = (selector: string) => {
export const createMatcher = (selector: string): Matcher => {
const matcherCreater = selectorCache.get(selector)
if (matcherCreater) return matcherCreater()
const ast = elParse(selector)
......@@ -309,7 +307,7 @@ export const createMatcher = (selector: string) => {
return newMatcherCreater()
}
export const parseHtml = (html: string) => {
export const parseHtml = (html: string): NodeType => {
const cached = parsedHtmlCache.get(html)
if (cached) return cached
const doc = ultraParse(html)
......@@ -317,7 +315,7 @@ export const parseHtml = (html: string) => {
return doc
}
export const findNode = (doc: NodeType, selector: string) => {
export const findNode = (doc: NodeType, selector: string): NodeType => {
if (!selector) return doc
let docCache = findNodeCache.get(doc)
if (!docCache) {
......
......@@ -2,7 +2,7 @@
import { ELEMENT_NODE, TEXT_NODE } from 'ultrahtml'
import Node from './node.astro'
const { props: { parent = null, node, index = 0, debug = false, replacers, slotHandler } } = Astro
const { props: { parent = null, node, index = 0, debug = 0, replacers, slotHandler } } = Astro
const { name: Name, attributes } = node
if (debug) {
......@@ -17,8 +17,9 @@ for (const [ matcher, handler ] of replacers) {
}
}
const nextDebug = debug ? debug - 1 : 0
---
{
slotName ? slotHandler(slotName, node)
: <Node parent={parent} node={node} index={index} debug={debug} replacers={replacers} slotHandler={slotHandler}/>
: <Node parent={parent} node={node} index={index} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>
}
......
......@@ -5,7 +5,7 @@ import Children from './children.astro'
interface Props {
html?: string,
debug?: boolean,
debug?: number,
xpath?: string,
replacements?: Replacements,
......@@ -16,21 +16,22 @@ interface Props {
}
const { props: { parent = null, node, index = 0, debug = false, replacers, slotHandler } } = Astro
const { props: { parent = null, node, index = 0, debug = 0, replacers, slotHandler } } = Astro
const { name: Name, attributes } = node
if (debug) {
console.log('Node.astro:debug', {node})
console.log('node.astro:debug', {node})
}
const nextDebug = debug ? debug - 1 : 0
---
{
node.type === TEXT_NODE ? <Fragment set:html={node.value}/>
: node.type === ELEMENT_NODE ? (
node.isSelfClosingTag ? <Name {...attributes}/>
: <Name {...attributes}>
<Children parent={node} children={node.children} debug={debug} replacers={replacers} slotHandler={slotHandler}/>
<Children parent={node} children={node.children} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>
</Name>
) : (
<Children parent={node} children={node.children} debug={debug} replacers={replacers} slotHandler={slotHandler}/>
<Children parent={node} children={node.children} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>
)
}
......
......@@ -7,13 +7,13 @@ import type { SlotHandler, Replacements } from './astro.ts'
interface Props {
html?: string,
debug?: boolean,
debug?: number,
xpath?: string,
replacements?: Replacements,
}
const { props } = Astro
const { html, debug = false, xpath, replacements = {} } = props
const { html, debug = 0, xpath, replacements = {} } = props
const doc = props.node ? props.node : parseHtml(props.html)
const node = xpath ? findNode(doc, xpath) : doc
......@@ -22,5 +22,6 @@ const replacers = Object.entries(replacements).map(([ selector, handler ]) => [
const slotHandler: SlotHandler = (slotName, node) => {
return Astro.slots.render(slotName, [ node, { slotHandler, replacers } ] )
}
const nextDebug = debug ? debug - 1 : 0
---
<Match node={node} debug={debug} replacers={replacers} slotHandler={slotHandler}/>
<Match node={node} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>
......