Add options when parsing html, and if for react use, map certain
attributes.
Showing
1 changed file
with
22 additions
and
9 deletions
| ... | @@ -307,9 +307,17 @@ export const createMatcher = (selector: string): Matcher => { | ... | @@ -307,9 +307,17 @@ export const createMatcher = (selector: string): Matcher => { |
| 307 | return newMatcherCreater() | 307 | return newMatcherCreater() |
| 308 | } | 308 | } |
| 309 | 309 | ||
| 310 | const reactAttributeMap = { | ||
| 311 | 'class': 'className', | ||
| 312 | 'srcset': 'srcSet', | ||
| 313 | 'maxlength': 'maxLength', | ||
| 314 | } | ||
| 315 | |||
| 310 | class NodeProxyHandler { | 316 | class NodeProxyHandler { |
| 317 | #options | ||
| 311 | #cache | 318 | #cache |
| 312 | constructor() { | 319 | constructor(options = {}) { |
| 320 | this.#options = options | ||
| 313 | this.#cache = {} | 321 | this.#cache = {} |
| 314 | } | 322 | } |
| 315 | 323 | ||
| ... | @@ -321,13 +329,18 @@ class NodeProxyHandler { | ... | @@ -321,13 +329,18 @@ class NodeProxyHandler { |
| 321 | switch (prop) { | 329 | switch (prop) { |
| 322 | case 'attributes': | 330 | case 'attributes': |
| 323 | return this.#cache[ prop ] = (origValue ? Object.fromEntries(Object.entries(origValue).map(([ key, value ]) => { | 331 | return this.#cache[ prop ] = (origValue ? Object.fromEntries(Object.entries(origValue).map(([ key, value ]) => { |
| 324 | return [ key, decode(value) ] | 332 | const decoded = decode(value) |
| 333 | if (this.#options.react) { | ||
| 334 | const { [ key ]: newKey } = reactAttributeMap | ||
| 335 | if (newKey) return [ newKey, decoded ] | ||
| 336 | } | ||
| 337 | return [ key, decoded ] | ||
| 325 | })) : origValue) | 338 | })) : origValue) |
| 326 | return newValue | 339 | return newValue |
| 327 | case 'parent': | 340 | case 'parent': |
| 328 | return this.#cache[ prop ] = proxyNode(origValue) | 341 | return this.#cache[ prop ] = proxyNode(origValue, this.#options) |
| 329 | case 'children': | 342 | case 'children': |
| 330 | return this.#cache[ prop ] = origValue.map((child) => proxyNode(child)) | 343 | return this.#cache[ prop ] = origValue.map((child) => proxyNode(child, this.#options)) |
| 331 | default: | 344 | default: |
| 332 | if (typeof origValue === 'function') { | 345 | if (typeof origValue === 'function') { |
| 333 | return this.#cache[ prop ] = (...args) => { | 346 | return this.#cache[ prop ] = (...args) => { |
| ... | @@ -339,16 +352,16 @@ class NodeProxyHandler { | ... | @@ -339,16 +352,16 @@ class NodeProxyHandler { |
| 339 | } | 352 | } |
| 340 | } | 353 | } |
| 341 | 354 | ||
| 342 | const proxyNode = (node) => { | 355 | const proxyNode = (node, options) => { |
| 343 | return new Proxy(node, new NodeProxyHandler()) | 356 | return new Proxy(node, new NodeProxyHandler(options)) |
| 344 | } | 357 | } |
| 345 | 358 | ||
| 346 | export const parseHtml = (html: string): NodeType => { | 359 | export const parseHtml = (html: string, options): NodeType => { |
| 347 | const cached = parsedHtmlCache.get(html) | 360 | const cached = parsedHtmlCache.get(html) |
| 348 | if (cached) return proxyNode(cached) | 361 | if (cached) return proxyNode(cached, options) |
| 349 | const doc = ultraParse(html) | 362 | const doc = ultraParse(html) |
| 350 | parsedHtmlCache.set(html, doc) | 363 | parsedHtmlCache.set(html, doc) |
| 351 | return proxyNode(doc) | 364 | return proxyNode(doc, options) |
| 352 | } | 365 | } |
| 353 | 366 | ||
| 354 | export const findNode = (doc: NodeType, selector: string): NodeType => { | 367 | export const findNode = (doc: NodeType, selector: string): NodeType => { | ... | ... |
-
Please register or sign in to post a comment