wxml-loader

This commit is contained in:
Xu Chang 2022-04-25 10:29:06 +08:00
parent 9cdbbe9a9c
commit 43190b071e
2 changed files with 36 additions and 3 deletions

View File

@ -1,9 +1,41 @@
/**
*
* @param {*} content 文件信息
*/
const { DOMParser, XMLSerializer } = require('@xmldom/xmldom')
function traverse(doc, callback) {
callback(doc);
if (doc.childNodes) {
const { length } = doc.childNodes;
for (let i = 0; i < length; i ++) {
traverse(doc.childNodes.item(i), callback);
}
}
}
module.exports = function (content) {
console.log(content);
return content;
//console.log(content);
const doc = new DOMParser().parseFromString(content, 'text/xml');
traverse(doc, (node) => {
if (node.nodeType === node.ELEMENT_NODE) {
// 处理oak:value声明的属性
if (node.hasAttribute('oak:value')) {
const oakValue = node.getAttribute('oak:value');
node.removeAttribute('oak:value');
node.setAttribute('value', `{{${oakValue}}}`);
node.setAttribute('data-path', oakValue);
if (node.hasAttribute('oak:forbidFocus')) {
node.removeAttribute('oak:forbidFocus');
}
else {
node.setAttribute('focus', `{{!!oakFocused.${oakValue}}}`);
}
}
}
});
const serialized = new XMLSerializer().serializeToString(doc);
return serialized;
};

View File

@ -21,6 +21,7 @@
"@types/inquirer": "^7.3.1",
"@types/node": "^12.0.27",
"@types/shelljs": "^0.8.8",
"@xmldom/xmldom": "^0.8.2",
"typescript": "^4.6.3"
},
"dependencies": {