MCPcopy Index your code
hub / github.com/microsoft/vscode-cpptools / traverseHtml

Function traverseHtml

Extension/gulpfile.js:98–124  ·  view source on GitHub ↗
(contents, nodeCallback, attributeCallback, isFragment)

Source from the content-addressed store, hash-verified

96// nodeCallback(locId, locHint, node) is invoked for nodes
97// attributeCallback(locId, locHint, attribute) is invoked for attributes
98const traverseHtml = (contents, nodeCallback, attributeCallback, isFragment) => {
99 const htmlTree = isFragment ? parse5.parseFragment(contents) : parse5.parse(contents);
100 traverse(htmlTree, {
101 pre(node, parent) {
102 if (node.attrs) {
103 // Check if content text should be localized based on presense of data-loc-id attribute
104 let locId = node.attrs.find(attribute => attribute.name.toLowerCase() == dataLocIdAttribute);
105 if (locId) {
106 let locHint = node.attrs.find(attribute => attribute.name.toLowerCase() == dataLocHintAttribute);
107 nodeCallback(locId.value, locHint?.value, node);
108 }
109 // Check if an attribute should be localized based on presense of data-loc-id-<attribute_name> attribute
110 node.attrs.forEach(attribute => {
111 if (attribute.name.startsWith(`${dataLocIdAttribute}-`)) {
112 let targetAttributeName = attribute.name.substring(dataLocIdAttribute.length + 1);
113 let targetAttribute = node.attrs.find(a => a.name == targetAttributeName);
114 if (targetAttribute) {
115 let hint = node.attrs.find(a => a.name.toLowerCase() == `${dataLocHintAttribute}-${targetAttributeName}`);
116 attributeCallback(attribute.value, hint?.value, targetAttribute);
117 }
118 }
119 });
120 }
121 }
122 });
123 return htmlTree;
124};
125
126// Traverses the HTML document looking for node and attributes containing data-loc-id, to localize
127// Outputs *.nls.json files containing strings to localize.

Callers 2

processHtmlFilesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected