Skip to content

Commit

Permalink
#6813 Enhance the SvgRegistry.registerIconFromSvg() method to suppo…
Browse files Browse the repository at this point in the history
…rt the `icon-` prefix (#6947)

* #6813 Enhance the `SvgRegistry.registerIconFromSvg()` method to support the `icon-` prefix
Fixes #6813

* #6813 Enhance the `SvgRegistry.registerIconFromSvg()` method to support the `icon-` prefix - fixed test
Fixes #6813
  • Loading branch information
novikov82 authored Sep 15, 2023
1 parent b8fc4f3 commit 9ab7c41
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/svgbundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ export class SvgIconRegistry {
icons: SvgIconData = {};
private iconPrefix = "icon-";

private processId(iconId: string, iconPrefix: string) {
if (iconId.indexOf(iconPrefix) == 0) iconId = iconId.substring(iconPrefix.length);
return iconId;
}
public registerIconFromSymbol(iconId: string, iconSymbolSvg: string) {
this.icons[iconId] = iconSymbolSvg;
}
public registerIconFromSvgViaElement(iconId: string, iconSvg: string, iconPrefix: string = this.iconPrefix) {
iconId = this.processId(iconId, iconPrefix);
let divSvg = document.createElement("div");
divSvg.innerHTML = iconSvg;
let symbol = document.createElement("symbol");
Expand All @@ -26,6 +31,7 @@ export class SvgIconRegistry {
this.registerIconFromSymbol(iconId, symbol.outerHTML);
}
public registerIconFromSvg(iconId: string, iconSvg: string, iconPrefix: string = this.iconPrefix): boolean {
iconId = this.processId(iconId, iconPrefix);
const startStr = "<svg ";
const endStr = "</svg>";
iconSvg = iconSvg.trim();
Expand Down
6 changes: 3 additions & 3 deletions testCafe/survey/svgRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const json = {
{
"type": "html",
"name": "question1",
"html": "<svg id=\"svg-icon-test\"><use xlink:href=\"#icon-icon-test\"></use></svg>"
"html": "<svg id=\"svg-icon-test\"><use xlink:href=\"#icon-icn-test\"></use></svg>"
}
]
};
Expand All @@ -18,7 +18,7 @@ frameworks.forEach(async framework => {
fixture`${framework} ${title}`.page`${url}${framework}`.beforeEach(
async t => {
await ClientFunction(() => {
window["Survey"].SvgRegistry.registerIconFromSvg("icon-test", "<svg viewBox=\"1 2 3 4\"></svg>");
window["Survey"].SvgRegistry.registerIconFromSvg("icn-test", "<svg viewBox=\"1 2 3 4\"></svg>");
})();
await initSurvey(framework, json);
}
Expand All @@ -29,7 +29,7 @@ frameworks.forEach(async framework => {
});
await t
.expect(Selector("#sv-icon-holder-global-container").exists).ok()
.expect(svgContainer()).contains("<symbol id=\"icon-icon-test\" viewBox=\"1 2 3 4\"></symbol>")
.expect(svgContainer()).contains("<symbol id=\"icon-icn-test\" viewBox=\"1 2 3 4\"></symbol>")
.expect(svgContainer()).contains("<symbol id=\"icon-left\"");
});
});
13 changes: 13 additions & 0 deletions tests/svgRegistryTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,17 @@ QUnit.test("svg import in the custom environment", function (assert) {
rootElement: document.body,
svgMountContainer: document.head
};
});

QUnit.test("svg import from svg via element - use prefix", function (assert) {
let svg = new SvgIconRegistry();
svg.registerIconFromSvgViaElement("icon-a", "<svg viewBox=\"0 0 100 100\"><circle/></symbol>");
assert.equal(svg.iconsRenderedHtml(), "<symbol viewBox=\"0 0 100 100\" id=\"icon-a\"><circle></circle></symbol>");
});

QUnit.test("svg import from svg via string - use prefix", function (assert) {
let svg = new SvgIconRegistry();
let res = svg.registerIconFromSvg("icon-a", "<svg viewBox=\"0 0 100 100\"><circle/></svg>");
assert.ok(res);
assert.equal(svg.iconsRenderedHtml(), "<symbol id=\"icon-a\" viewBox=\"0 0 100 100\"><circle/></symbol>");
});

0 comments on commit 9ab7c41

Please sign in to comment.