754d6092 by Adam Heath

Adjustments are now done centrally, inside the node proxy, instead of

ad-hoc in Match.
1 parent 20070e45
---
import Match from './match.astro'
const { props: { parent, children, debug = 0, replacers, slotHandler, adjuster } } = Astro
const { props: { parent, children, debug = 0, replacers, slotHandler } } = Astro
//console.log('Children:render', { parent, children, replacers })
if (debug) {
......@@ -11,7 +11,7 @@ const nextDebug = debug ? debug - 1 : 0
---
{
Array.isArray(children) ?
children.map((child, index) => <Match parent={parent} node={child} index={index} debug={nextDebug} replacers={replacers} slotHandler={slotHandler} adjuster={adjuster}/>)
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={nextDebug} replacers={replacers} slotHandler={slotHandler} adjuster={adjuster}/>
: <Match parent={parent} node={children} index={0} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>
}
......
......@@ -2,7 +2,7 @@
import Children from './children.astro'
const { props: { wrap = false, wrapAttributes = {}, node, replacers, slotHandler, adjuster } } = Astro
const { props: { wrap = false, wrapAttributes = {}, node, replacers, slotHandler } } = Astro
const { name: Name, attributes, children } = node
const CustomName = `custom-${Name}`
......@@ -15,14 +15,14 @@ const CustomName = `custom-${Name}`
{
node.isSelfClosingTag ? <Name {...attributes}/>
: <Name {...attributes}>
<Children parent={node} children={children} replacers={replacers} slotHandler={slotHandler} adjuster={adjuster}/>
<Children parent={node} children={children} replacers={replacers} slotHandler={slotHandler}/>
</Name>
}
</CustomName>
) : (
node.isSelfClosingTag ? <CustomName {...attributes}/>
: <CustomName {...attributes}>
<Children parent={node} children={children} replacers={replacers} slotHandler={slotHandler} adjuster={adjuster}/>
<Children parent={node} children={children} replacers={replacers} slotHandler={slotHandler}/>
</CustomName>
)
}
......
......@@ -344,7 +344,7 @@ class NodeProxyHandler {
#options
#cache
#parent
constructor(options = {}, parent = null) {
constructor(options, parent = null) {
this.#options = options
this.#cache = {}
this.#parent = parent
......@@ -369,7 +369,7 @@ class NodeProxyHandler {
case 'parent':
return this.#parent
case 'children':
return this.#cache[ prop ] = origValue.map((child) => proxyNode(child, this.#options, this))
return this.#cache[ prop ] = origValue.map((child, index) => proxyNode(child, this.#options, this, index))
default:
if (typeof origValue === 'function') {
return this.#cache[ prop ] = (...args) => {
......@@ -381,8 +381,10 @@ class NodeProxyHandler {
}
}
const proxyNode = (node, options, parent) => {
return new Proxy(node, new NodeProxyHandler(options, parent))
const proxyNode = (node, options = {}, parent = null, index = 0) => {
const { adjuster = (node, parent, index) => node } = options
const adjusted = adjuster(node, parent, index)
return new Proxy(adjusted, new NodeProxyHandler(options, parent))
}
export const parseHtml = (html: string, options): NodeType => {
......
......@@ -2,17 +2,16 @@
import { ELEMENT_NODE, TEXT_NODE } from 'ultrahtml'
import Node from './node.astro'
const { props: { parent = null, node, index = 0, debug = 0, replacers, slotHandler, adjuster } } = Astro
const { props: { parent = null, node, index = 0, debug = 0, replacers, slotHandler } } = Astro
const { name: Name, attributes } = node
const fixedNode = adjuster(node, parent, index)
if (debug) {
console.log('trying to match against', {fixedNode})
console.log('trying to match against', {node})
}
let slotName
for (const [ matcher, handler ] of replacers) {
if (debug) console.log('attempting matcher', matcher.toString())
if (matcher(fixedNode, parent, index, false)) {
if (matcher(node, parent, index, false)) {
if (debug) console.log('matched')
slotName = handler
}
......@@ -21,6 +20,6 @@ for (const [ matcher, handler ] of replacers) {
const nextDebug = debug ? debug - 1 : 0
---
{
slotName ? slotHandler(slotName, fixedNode)
: <Node parent={parent} node={fixedNode} index={index} debug={nextDebug} replacers={replacers} slotHandler={slotHandler} adjuster={adjuster}/>
slotName ? slotHandler(slotName, node)
: <Node parent={parent} node={node} index={index} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>
}
......
......@@ -16,7 +16,7 @@ interface Props {
}
const { props: { parent = null, node, index = 0, debug = 0, replacers, slotHandler, adjuster } } = Astro
const { props: { parent = null, node, index = 0, debug = 0, replacers, slotHandler } } = Astro
const { name: Name, attributes } = node
if (debug) {
......@@ -32,7 +32,7 @@ const nextDebug = debug ? debug - 1 : 0
: node.type === ELEMENT_NODE ? (
node.isSelfClosingTag ? <Name {...attributes}/>
: <Name {...attributes}>
<Children parent={node} children={node.children} debug={nextDebug} replacers={replacers} slotHandler={slotHandler} adjuster={adjuster}/>
<Children parent={node} children={node.children} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>
</Name>
) : ''
}
......
......@@ -14,9 +14,7 @@ interface Props {
const { props } = Astro
const { html, debug = 0, xpath, replacements = {}, adjustments = {} } = props
const doc = props.node ? props.node : parseHtml(props.html)
const node = xpath ? findNode(doc, xpath) : doc
const replacers = Object.entries(replacements).map(([ selector, handler ]) => [ createMatcher(selector), handler ])
const adjustmentsCompiled = Object.entries(adjustments).map(([ selector, handler ]) => [ createMatcher(selector), handler ])
......@@ -29,9 +27,12 @@ const adjuster = (node, parent, index) => {
return node
}
const doc = props.node ? props.node : parseHtml(props.html, { adjuster })
const node = xpath ? findNode(doc, xpath) : doc
const slotHandler: SlotHandler = (slotName, node) => {
return Astro.slots.render(slotName, [ node, { slotHandler, replacers, adjuster } ] )
return Astro.slots.render(slotName, [ node, { slotHandler, replacers } ] )
}
const nextDebug = debug ? debug - 1 : 0
---
<Match node={node} debug={nextDebug} replacers={replacers} slotHandler={slotHandler} adjuster={adjuster}/>
<Match node={node} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>
......