///|
/// Presentation attributes that inherit from ancestors.
let inheritable : Array[String] = [
"clip-rule", "color", "color-interpolation", "color-rendering", "cursor", "direction",
"fill", "fill-opacity", "fill-rule", "font", "font-family", "font-size", "font-style",
"font-variant", "font-weight", "letter-spacing", "marker-end", "marker-mid", "marker-start",
"paint-order", "pointer-events", "shape-rendering", "stroke", "stroke-dasharray",
"stroke-dashoffset", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit",
"stroke-opacity", "stroke-width", "text-anchor", "text-rendering", "visibility",
"word-spacing", "writing-mode",
]
///|
/// Default values of presentation attributes: an attribute equal to its
/// default can be dropped when no ancestor overrides it.
let default_values : Map[String, String] = {
"fill": "#000",
"fill-opacity": "1",
"stroke-opacity": "1",
"opacity": "1",
"stroke-width": "1",
"stroke-linecap": "butt",
"stroke-linejoin": "miter",
"stroke-miterlimit": "4",
"stroke-dasharray": "none",
"stroke-dashoffset": "0",
"fill-rule": "nonzero",
"clip-rule": "nonzero",
"stroke": "none",
"font-style": "normal",
"font-weight": "normal",
"font-variant": "normal",
"text-anchor": "start",
"visibility": "visible",
"display": "inline",
"overflow": "visible",
"clip-path": "none",
"mask": "none",
"filter": "none",
"letter-spacing": "normal",
"word-spacing": "normal",
"writing-mode": "lr-tb",
"color-interpolation": "sRGB",
"color-rendering": "auto",
"shape-rendering": "auto",
"text-rendering": "auto",
"image-rendering": "auto",
"pointer-events": "visiblePainted",
"paint-order": "normal",
}
///|
/// Presentation attributes whose value may be a `url(#…)` reference to
/// paint, markers, clipping, masking or filtering. When such a reference is
/// present the transform must stay on the element because the referenced
/// resource is also subject to it.
let references_props : Set[String] = Set([
"clip-path", "color-profile", "fill", "filter", "marker-end", "marker-mid", "marker-start",
"mask", "stroke", "style",
])
///|
/// Attributes that are 0 by default on these elements.
let zero_defaults : Map[String, Array[String]] = {
"rect": ["x", "y", "rx", "ry"],
"circle": ["cx", "cy"],
"ellipse": ["cx", "cy"],
"image": ["x", "y"],
"use": ["x", "y"],
"svg": ["x", "y"],
"pattern": ["x", "y"],
"text": ["x", "y", "dx", "dy"],
"tspan": ["dx", "dy"],
"line": ["x1", "y1", "x2", "y2"],
"foreignObject": ["x", "y"],
"mask": [],
"marker": ["refX", "refY"],
}
///|
fn ancestors_define(chain : Array[@xml.Element], attr : String) -> Bool {
for a in chain {
if a.attrs.contains(attr) {
return true
}
if a.attrs.get("style") is Some(s) && s.contains(attr) {
return true
}
}
false
}
///|
/// Remove unknown elements and attributes, and attributes whose value equals
/// the element's default (when no ancestor overrides it).
let remove_unknowns_and_defaults : Plugin = {
name: "removeUnknownsAndDefaults",
description: "remove unknown elements/attributes and default attribute values",
run: (doc, ctx) => {
let tables = svg_tables()
let mut changed = false
// collect CSS text to detect attribute selectors that would make a
// default-valued attribute meaningful
let css = style_text(doc)
let unknown_content = ctx.param_bool("unknownContent", true)
let unknown_attrs = ctx.param_bool("unknownAttrs", true)
let default_attrs = ctx.param_bool("defaultAttrs", true)
let useless_overrides = ctx.param_bool("uselessOverrides", true)
let keep_data_attrs = ctx.param_bool("keepDataAttrs", true)
let keep_aria_attrs = ctx.param_bool("keepAriaAttrs", true)
let keep_role_attr = ctx.param_bool("keepRoleAttr", false)
// remove standalone="no" from the XML declaration
for node in doc.children {
match node {
ProcInst("xml", v) => {
let cleaned = remove_standalone_no(v)
if cleaned != v {
let idx = doc.children.search_by(n => n is ProcInst("xml", _))
match idx {
Some(i) => {
doc.children[i] = ProcInst("xml", cleaned)
changed = true
}
None => ()
}
}
}
_ => ()
}
}
fn walk(e : @xml.Element, chain : Array[@xml.Element]) {
// foreignObject holds arbitrary (non-SVG) content; leave it alone
if e.name == "foreignObject" {
return
}
// namespaced elements are not ours to judge
if e.name.contains(":") {
return
}
let parent = chain.last()
// remove elements that are not allowed in their parent.
// svgo's rule: if the parent has a non-empty allowed-children set,
// only those children are kept; otherwise (empty or no set), only
// unknown elements are removed.
let parent_allowed = match parent {
Some(p) => tables.element(p.name).map(t => t.allowed_children)
None => None
}
let is_unknown = !tables.known_elements.contains(e.name)
let not_allowed = match parent_allowed {
Some(allowed) if allowed.length() > 0 => !allowed.contains(e.name)
_ => is_unknown
}
if unknown_content && not_allowed {
match parent {
Some(p) => {
let idx = p.children.search_by(c => {
c is @xml.Element(x) && physical_equal(x, e)
})
match idx {
Some(i) => {
ignore(p.children.remove(i))
changed = true
}
None => ()
}
}
None => ()
}
return
}
let has_id = e.attrs.contains("id")
let element_tables = tables.element(e.name)
let allowed = element_tables.map(t => t.allowed_attrs)
let defaults = element_tables.map(t => t.defaults)
for kv in e.attrs.to_array() {
let (k, v) = kv
// always keep data-*, aria-*, xmlns and xmlns:*
if (keep_data_attrs && k.has_prefix("data-")) ||
(keep_aria_attrs && k.has_prefix("aria-")) ||
(keep_role_attr && k == "role") ||
k == "xmlns" ||
k.has_prefix("xmlns:") {
continue
}
// role is removed by default (keepRoleAttr = false)
if k == "role" && !keep_role_attr {
e.attrs.remove(k)
changed = true
continue
}
// version and baseProfile on