MCPcopy Create free account
hub / github.com/postlight/parser / convertNodeTo

Function convertNodeTo

src/utils/dom/convert-node-to.js:3–26  ·  view source on GitHub ↗
($node, $, tag = 'p')

Source from the content-addressed store, hash-verified

1import { getAttrs } from 'utils/dom';
2
3export default function convertNodeTo($node, $, tag = 'p') {
4 const node = $node.get(0);
5 if (!node) {
6 return $;
7 }
8 const attrs = getAttrs(node) || {};
9
10 const attribString = Reflect.ownKeys(attrs)
11 .map(key => `${key}=${attrs[key]}`)
12 .join(' ');
13 let html;
14
15 if ($.browser) {
16 // In the browser, the contents of noscript tags aren't rendered, therefore
17 // transforms on the noscript tag (commonly used for lazy-loading) don't work
18 // as expected. This test case handles that
19 html =
20 node.tagName.toLowerCase() === 'noscript' ? $node.text() : $node.html();
21 } else {
22 html = $node.contents();
23 }
24 $node.replaceWith(`<${tag} ${attribString}>${html}</${tag}>`);
25 return $;
26}

Callers 7

cleanHOnesFunction · 0.90
convertDivsFunction · 0.90
convertSpansFunction · 0.90
rewriteTopLevelFunction · 0.90
transformElementsFunction · 0.90
convertSpansFunction · 0.90

Calls 1

getAttrsFunction · 0.90

Tested by

no test coverage detected