Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

Commit

Permalink
fix: preview now working for files without xmlns
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSiefke committed Apr 14, 2019
1 parent f335bb8 commit 80f6824
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"bundlesize": [
{
"path": "./packages/extension/dist/extension.js",
"maxSize": "7.2 kB",
"maxSize": "7.6 kB",
"compression": "none"
},
{
Expand Down
3 changes: 0 additions & 3 deletions packages/extension/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export const configuration: Configuration = {
addChangeListener(callback) {
if (!listeners) {
listeners = []
console.log('inside')
context.subscriptions.push(
vscode.workspace.onDidChangeConfiguration(event => {
if (!event.affectsConfiguration('svgPreview')) {
Expand All @@ -60,11 +59,9 @@ export const configuration: Configuration = {
listeners.push(callback)
},
get(key, resource) {
console.log('before')
const result = vscode.workspace
.getConfiguration('svgPreview', resource)
.get<any>(key)
console.log('after')
return result
},
}
30 changes: 29 additions & 1 deletion packages/extension/src/preview/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,41 @@ const postMessage = (message: Message): void => {
})
}

function indexOfGroup(match, n): number {
let { index } = match
for (let i = 1; i < n; i++) {
index += match[i].length
}
return index
}

async function getActualContent(): Promise<string> {
let content = await withInlineStyles(
path.dirname(state.fsPath),
state.content
)
const match = /(<svg)([^>]*?)(>)/i.exec(content)
if (match) {
// insert xmlns if it doesn't exist
const svgTagStart = indexOfGroup(match, 0)
const svgTagEnd = indexOfGroup(match, 3)
const svgTagContent = content.slice(svgTagStart, svgTagEnd)
if (!/xmlns=/.test(svgTagContent)) {
content = `${content.slice(
0,
svgTagStart + 4
)} xmlns="http://www.w3.org/2000/svg" ${content.slice(svgTagStart + 4)}`
}
}
return content
}
/**
* Update the contents.
*/
async function invalidateContent(): Promise<void> {
postMessage({
command: 'update.content',
payload: await withInlineStyles(path.dirname(state.fsPath), state.content),
payload: await getActualContent(),
})
}

Expand Down

0 comments on commit 80f6824

Please sign in to comment.