2d8750b4 by Adam Heath

Update react side for adjustments and multiple xpath matches.

1 parent 1367a306
1 import React from 'react' 1 import React from 'react'
2 2
3 import { COMMENT_NODE, DOCUMENT_NODE, DOCTYPE_NODE, ELEMENT_NODE, TEXT_NODE } from 'ultrahtml' 3 import { COMMENT_NODE, DOCUMENT_NODE, DOCTYPE_NODE, ELEMENT_NODE, TEXT_NODE } from 'ultrahtml'
4 import { parseHtml, createMatcher, findNode } from 'astro-wt/html' 4 import { parseHtml, createAdjuster, createMatcher, findNode } from 'astro-wt/html'
5 import { decode } from 'html-entities' 5 import { decode } from 'html-entities'
6 6
7 export const ReparseStaticChildren = (replaceOptions, Component) => (props) => { 7 export const ReparseStaticChildren = (replaceOptions, Component) => (props) => {
...@@ -43,13 +43,14 @@ export const CreateReplacement = (Element, propsOrCreator) => (matchOptions) => ...@@ -43,13 +43,14 @@ export const CreateReplacement = (Element, propsOrCreator) => (matchOptions) =>
43 export const DeleteHandler = () => null 43 export const DeleteHandler = () => null
44 44
45 export const Replace = (props) => { 45 export const Replace = (props) => {
46 const { debug = 0, html, xpath, replacements } = props 46 const { debug = 0, html, xpath, adjustments, replacements } = props
47 const doc = props.node ? props.node : parseHtml(html, { react: false }) 47 const adjuster = createAdjuster(adjustments)
48 const node = xpath ? findNode(doc, xpath) : doc 48 const doc = props.node ? props.node : parseHtml(html, { adjuster })
49 const nodes = xpath ? findNode(doc, xpath, { single: false }) : [ doc ]
49 const replacers = Object.entries(replacements).map(([ selector, Handler ]) => [ createMatcher(selector), Handler ]) 50 const replacers = Object.entries(replacements).map(([ selector, Handler ]) => [ createMatcher(selector), Handler ])
50 const nextDebug = debug ? debug - 1 : 0 51 const nextDebug = debug ? debug - 1 : 0
51 if (debug) console.log('Replace', { html, replacers, node }) 52 if (debug) console.log('Replace', { html, replacers, node })
52 const result = Match({ debug: nextDebug, node, replacers }) 53 const result = nodes.map(node => Match({ debug: nextDebug, node, replacers }))
53 if (debug) console.log('Replace result', result) 54 if (debug) console.log('Replace result', result)
54 return result 55 return result
55 } 56 }
......