如何解析`svgo`中具有未引用属性的svg? [英] How to parse svgs with unquoted attributes in `svgo`?

查看:88
本文介绍了如何解析`svgo`中具有未引用属性的svg?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 svgo 或作为webpack加载器的一部分( imagemin )都使用svgo(如果您的svg具有未加引号的属性,例如:

When parsing using svgo or as part of webpack loader (svgo-loader) or plugin (imagemin) that both use svgo, if you have svgs with unquoted attributes such as:

xmlns="http://www.w3.org/2000/svg" width=20px height=20px

错误:解析SVG时出错:发出了未引用的属性值.

所有这些属性都可以使用不带引号的svg吗?

Can svg's with unquoted attributes be used in all of these?

推荐答案

我创建了一个加载程序以对webpack中的xml(包括svgs)进行规范化

I've created a loader to canonize xmls (including svgs) in webpack:

xml-fix-loader

{
  test: /\.svg$/,
  use: [
    { loader: 'file-loader' },
    { loader: 'svgo-loader' },
    { loader: 'xml-fix-loader' }
  ]
}

通常,它使用 xml2js 来严格解析xml,然后进行字符串化然后返回以获取音调的xml(或svg).

what it does in general is to use xml2js to parse the xml unstrictly and then stringify it back to get a cononized xml (or svg).

这消除了诸如未引用属性之类的问题.所以:

This removes issues like unquoted attributes. so:

xmlns="http://www.w3.org/2000/svg" width=20px height=20px

成为

xmlns="http://www.w3.org/2000/svg" width="20px" height="20px"

这篇关于如何解析`svgo`中具有未引用属性的svg?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆