Skip to content

Commit

Permalink
feat: android xml resources parser
Browse files Browse the repository at this point in the history
  • Loading branch information
seanghay committed Jul 18, 2023
1 parent 673c5b2 commit 6cfc927
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const svgContent = transform(vectorDrawbleContent, {
override: {
'@color/colorPrimary': '#00ff00',
'@color/colorSecondary': '#00ff00',
'?android:attr/textColorPrimary': 'white'
'?android:attr/textColorPrimary': 'white',
});
```
Expand Down
33 changes: 33 additions & 0 deletions src/vector-drawable-svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,39 @@ const gradientItemAttrsTransforms = {
'android:color': convertHexColor,
}

/**
* @param {string | undefined} value
*/
exports.parseAndroidResource = function (value) {
if (typeof value !== 'string') return;
const parser = new DOMParser()
const doc = parser.parseFromString(value)
const resourcesNode = doc.getElementsByTagName("resources")[0]
if (!resourcesNode) return;

const map = new Map()

for (let i = 0; i < resourcesNode.childNodes.length; i++) {
const node = resourcesNode.childNodes[i];
if (node.nodeType !== 1) continue // if the current node is not Element, continue
if (node.firstChild.nodeType !== 3) continue; // if the first child is not TextNode, continue
const key = `@${node.tagName}/${node.getAttribute("name")}`
const value = node.textContent
map.set(key, value)
}

// resolve references
for (const [key, value] of map.entries()) {
if (/\@\w+\/\w+/g.test(value)) {
if (map.has(value)) {
map.set(key, map.get(value))
}
}
}

return Object.fromEntries(map.entries())
}

function parsePath(root, pathNode) {
const svgPath = root.createElement("path");
svgPath.setAttribute("fill", "none");
Expand Down
27 changes: 27 additions & 0 deletions tests/parse-resources.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { parseAndroidResource } = require('../src/vector-drawable-svg.js')

describe("transform android resources to map", () => {
it("should transform xml to map", () => {
const input = `
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#CE3168</color>
<color name="colorPrimaryDark">#b9275a</color>
<color name="colorAccent">#CE3168</color>
<color name="ucrop_color_toolbar">@color/colorPrimary</color>
<color name="ucrop_color_statusbar">@color/colorPrimaryDark</color>
<color name="ucrop_color_widget_active">@color/colorPrimary</color>
</resources>
`

const result = parseAndroidResource(input)
expect(result).toMatchObject({
"@color/colorAccent": "#CE3168",
"@color/colorPrimary": "#CE3168",
"@color/colorPrimaryDark": "#b9275a",
"@color/ucrop_color_statusbar": "#b9275a",
"@color/ucrop_color_toolbar": "#CE3168",
"@color/ucrop_color_widget_active": "#CE3168",
})
})
})

0 comments on commit 6cfc927

Please sign in to comment.