Skip to content

Commit edd3d95

Browse files
committed
Add support for SVG
Related to wooorm/property-information#6.
1 parent 2dcc404 commit edd3d95

File tree

5 files changed

+163
-95
lines changed

5 files changed

+163
-95
lines changed

index.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict'
22

3-
var information = require('property-information')
4-
var camelcase = require('camelcase')
3+
var html = require('property-information/html')
4+
var svg = require('property-information/svg')
5+
var find = require('property-information/find')
56
var h = require('hastscript')
67
var xtend = require('xtend')
78
var count = require('ccount')
@@ -32,6 +33,7 @@ function wrapper(ast, options) {
3233
}
3334

3435
return transform(ast, {
36+
schema: settings.space === 'svg' ? svg : html,
3537
file: file,
3638
verbose: settings.verbose,
3739
location: false
@@ -40,26 +42,33 @@ function wrapper(ast, options) {
4042

4143
/* Transform a node. */
4244
function transform(ast, config) {
45+
var schema = config.schema
4346
var fn = own.call(map, ast.nodeName) ? map[ast.nodeName] : element
4447
var children
4548
var node
4649
var pos
4750

51+
if (fn === element && schema.space === 'html' && ast.nodeName === 'svg') {
52+
config.schema = svg
53+
}
54+
4855
if (ast.childNodes) {
4956
children = nodes(ast.childNodes, config)
5057
}
5158

5259
node = fn(ast, children, config)
5360

5461
if (ast.sourceCodeLocation && config.file) {
55-
pos = location(node, ast.sourceCodeLocation, config.verbose)
62+
pos = location(node, ast.sourceCodeLocation, config)
5663

5764
if (pos) {
5865
config.location = true
5966
node.position = pos
6067
}
6168
}
6269

70+
config.schema = schema
71+
6372
return node
6473
}
6574

@@ -122,24 +131,27 @@ function comment(ast) {
122131

123132
/* Transform an element. */
124133
function element(ast, children, config) {
134+
var name = ast.tagName
135+
var attributes = ast.attrs
136+
var length = attributes.length
125137
var props = {}
126-
var values = ast.attrs
127-
var length = values.length
128138
var index = -1
129-
var attr
139+
var attribute
140+
var prop
130141
var node
131142
var pos
132143
var start
133144
var end
134145

135146
while (++index < length) {
136-
attr = values[index]
137-
props[(attr.prefix ? attr.prefix + ':' : '') + attr.name] = attr.value
147+
attribute = attributes[index]
148+
prop = (attribute.prefix ? attribute.prefix + ':' : '') + attribute.name
149+
props[prop] = attribute.value
138150
}
139151

140-
node = h(ast.tagName, props, children)
152+
node = h(name, props, children)
141153

142-
if (ast.nodeName === 'template' && 'content' in ast) {
154+
if (name === 'template' && 'content' in ast) {
143155
pos = ast.sourceCodeLocation
144156
start = pos && pos.startTag && position(pos.startTag).end
145157
end = pos && pos.endTag && position(pos.endTag).start
@@ -155,13 +167,15 @@ function element(ast, children, config) {
155167
}
156168

157169
/* Create clean positional information. */
158-
function location(node, location, verbose) {
170+
function location(node, location, config) {
171+
var schema = config.schema
172+
var verbose = config.verbose
159173
var pos = position(location)
160174
var reference
161-
var values
175+
var attributes
176+
var attribute
162177
var props
163178
var prop
164-
var name
165179

166180
if (node.type === 'element') {
167181
reference = node.children[node.children.length - 1]
@@ -177,12 +191,12 @@ function location(node, location, verbose) {
177191
}
178192

179193
if (verbose) {
180-
values = location.attrs
194+
attributes = location.attrs
181195
props = {}
182196

183-
for (prop in values) {
184-
name = (information(prop) || {}).propertyName || camelcase(prop)
185-
props[name] = position(values[prop])
197+
for (attribute in attributes) {
198+
prop = find(schema, attribute).property
199+
props[prop] = position(attributes[attribute])
186200
}
187201

188202
node.data = {

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919
"index.js"
2020
],
2121
"dependencies": {
22-
"camelcase": "^5.0.0",
2322
"ccount": "^1.0.3",
24-
"hastscript": "^3.0.0",
25-
"property-information": "^3.1.0",
23+
"hastscript": "^4.0.0",
24+
"property-information": "^4.0.0",
2625
"xtend": "^4.0.1"
2726
},
2827
"devDependencies": {
@@ -39,8 +38,8 @@
3938
"remark-cli": "^5.0.0",
4039
"remark-preset-wooorm": "^4.0.0",
4140
"tape": "^4.0.0",
41+
"to-vfile": "^5.0.0",
4242
"unist-util-visit": "^1.1.3",
43-
"vfile": "^3.0.0",
4443
"xo": "^0.21.0"
4544
},
4645
"scripts": {

test/fixtures/svg/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div>
2-
<svg width="230" height="120" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
2+
<svg width="230" height="120" viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
33
<circle cx="60" cy="60" r="50" fill="red"/>
44
<circle cx="170" cy="60" r="50" fill="green"/>
55
</svg>

0 commit comments

Comments
 (0)