Skip to content
This repository has been archived by the owner on Oct 30, 2021. It is now read-only.

Commit

Permalink
The marker attribute will be replaced with marker-start, `marker-…
Browse files Browse the repository at this point in the history
…mid`, and `marker-end` attributes for compatibility reasons.

Closes #112
  • Loading branch information
RazrFalcon committed Jan 17, 2018
1 parent 56ca6c6 commit 11da047
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http:https://semver.org/).
## [Unreleased]
### Added
- `--list-separator`.
- The `marker` attribute will be replaced with `marker-start`, `marker-mid`, and `marker-end` attributes
for compatibility reasons.

### Changed
- Attributes with `inherit` or `currentColor` and without a proper parent is an error now.
Expand Down
60 changes: 60 additions & 0 deletions src/task/fix_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub fn fix_invalid_attributes(doc: &Document) {
_ => {}
}
}

fix_marker(doc);
}

/// Fix `rect` element attributes.
Expand Down Expand Up @@ -306,3 +308,61 @@ fn rm_negative_len(node: &mut Node, id: AId) {
}
}
}

/// The `marker` attribute is not well supported, so we have to replace it
/// with `marker-start`, `marker-mid` and `marker-end`.
fn fix_marker(doc: &Document) {
let marker_attrs = &[
AId::MarkerStart,
AId::MarkerMid,
AId::MarkerEnd,
];

for mut node in doc.descendants() {
if node.has_attribute(AId::Marker) {
let v = node.attributes().get_value(AId::Marker).unwrap().clone();

for aid in marker_attrs {
node.set_attribute_if_none(*aid, &v);
}

node.remove_attribute(AId::Marker);
}
}
}

#[cfg(test)]
mod test_marker_attrs {
use super::*;
use svgdom::{Document, ToStringWithOptions};

macro_rules! test {
($name:ident, $in_text:expr, $out_text:expr) => (
base_test!($name, fix_invalid_attributes, $in_text, $out_text);
)
}

test!(fix_marker_1,
"<svg>
<marker id='m1'/>
<path marker='url(#m1)'/>
</svg>",
"<svg>
<marker id='m1'/>
<path marker-end='url(#m1)' marker-mid='url(#m1)' marker-start='url(#m1)'/>
</svg>
");

test!(fix_marker_2,
"<svg>
<marker id='m1'/>
<marker id='m2'/>
<path marker='url(#m1)' marker-start='url(#m2)'/>
</svg>",
"<svg>
<marker id='m1'/>
<marker id='m2'/>
<path marker-end='url(#m1)' marker-mid='url(#m1)' marker-start='url(#m2)'/>
</svg>
");
}

0 comments on commit 11da047

Please sign in to comment.