(
nodeObj: any,
childNodeNames: string[],
platform: string,
{metadata, addIndexPath, parentPath}: TransformNodeOptions,
)
| 140 | * @returns Object containing unknown nodes and attributes |
| 141 | */ |
| 142 | export function transformChildNodes( |
| 143 | nodeObj: any, |
| 144 | childNodeNames: string[], |
| 145 | platform: string, |
| 146 | {metadata, addIndexPath, parentPath}: TransformNodeOptions, |
| 147 | ): NodesAndAttributes { |
| 148 | const unknownNodes: string[] = []; |
| 149 | const unknownAttrs: string[] = []; |
| 150 | for (const nodeName of childNodeNames) { |
| 151 | // before modifying the name of this child node, recurse down and modify the subtree |
| 152 | const {nodes, attrs} = transformNode(nodeObj[nodeName], platform, { |
| 153 | metadata, |
| 154 | addIndexPath, |
| 155 | parentPath: parentPath || '', |
| 156 | }); |
| 157 | unknownNodes.push(...nodes); |
| 158 | unknownAttrs.push(...attrs); |
| 159 | |
| 160 | // now translate the node name and replace the subtree with this node |
| 161 | const universalName = getUniversalNodeName(nodeName, platform); |
| 162 | if (universalName === null) { |
| 163 | unknownNodes.push(nodeName); |
| 164 | continue; |
| 165 | } |
| 166 | |
| 167 | // since multiple child node names could map to the same new transformed node name, we can't |
| 168 | // simply assign nodeObj[universalName] = nodeObj[nodeName]; we need to be sensitive to the |
| 169 | // situation where the end result is an array of children having the same node name |
| 170 | if (nodeObj[universalName]) { |
| 171 | // if we already have a node with the universal name, that means we are mapping a second |
| 172 | // original node name to the same universal node name, so we just push all its children into |
| 173 | // the list |
| 174 | if (Array.isArray(nodeObj[universalName])) { |
| 175 | if (Array.isArray(nodeObj[nodeName])) { |
| 176 | nodeObj[universalName].push(...nodeObj[nodeName]); |
| 177 | } else { |
| 178 | nodeObj[universalName].push(nodeObj[nodeName]); |
| 179 | } |
| 180 | } else { |
| 181 | nodeObj[universalName] = [nodeObj[universalName]]; |
| 182 | if (Array.isArray(nodeObj[nodeName])) { |
| 183 | nodeObj[universalName].push(...nodeObj[nodeName]); |
| 184 | } else { |
| 185 | nodeObj[universalName].push(nodeObj[nodeName]); |
| 186 | } |
| 187 | } |
| 188 | } else { |
| 189 | nodeObj[universalName] = nodeObj[nodeName]; |
| 190 | } |
| 191 | delete nodeObj[nodeName]; |
| 192 | } |
| 193 | return {nodes: unknownNodes, attrs: unknownAttrs}; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Transforms attributes of a node object |
no test coverage detected