Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix declaring node also allows to use prefix for namespace #1106

Merged

Conversation

Fruchtzwerg94
Copy link
Contributor

@Fruchtzwerg94 Fruchtzwerg94 commented Dec 13, 2023

Issue / problem

Writing a SVG file where the namespace of a node is declared as an attribute in this node fails to resolve the namespace prefix.

As a minimal example, assume we want to add some additional meta data to a SVG file:

SvgDocument svg = SvgDocument.Open("MyImageWithMetaData.svg");

const string NAMESPACE_RDF = "https://www.w3.org/1999/02/22-rdf-syntax-ns#";
const string NAMESPACE_DC = "https://purl.org/dc/elements/1.1/";

SvgUnknownElement metadata = new SvgUnknownElement("metadata");
NonSvgElement rdfMetadata = new NonSvgElement("RDF", NAMESPACE_RDF);
rdfMetadata.Namespaces["rdf"] = NAMESPACE_RDF;

NonSvgElement rdfMetadataDescription = new NonSvgElement("Description", NAMESPACE_RDF);
rdfMetadataDescription.Children.Add(new NonSvgElement("creator", NAMESPACE_DC) { Content = "Hello World" });
rdfMetadata.Children.Add(rdfMetadataDescription);
metadata.Children.Add(rdfMetadata);
svg.Children.Add(metadata);

svg.Write("MyImageWithMetaData.svg");

The additional meta data will look like this:

<RDF xmlns:rdf="https://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="https://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <rdf:Description />
</RDF>

Obviously the namespace here is mentioned twice which is not necessary since (see e.g. Oracle or w3resource)

The scope of a declared namespace begins at the element where it is declared and applies to the entire content of that element

which means namespace prefixes can be already used in the node they are declared.

<rdf:RDF xmlns:rdf="https://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <rdf:Description />
</rdf:RDF>

This PR also takes care of this and looks up the namespaces declared in the nodes themselfes.

Copy link
Member

@mrbean-bremen mrbean-bremen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good! Please also add an entry in the release notes, and if possible, a test.

@Fruchtzwerg94
Copy link
Contributor Author

Fruchtzwerg94 commented Dec 14, 2023

Thanks, looks good! Please also add an entry in the release notes, and if possible, a test.

Test and changelog added @mrbean-bremen . Please keep in mind that the version and date in the changelog is just a placeholder XX yet and needs to be set accordingly at release.

Source/SvgElement.cs Outdated Show resolved Hide resolved
Source/SvgElement.cs Show resolved Hide resolved
doc/ReleaseNotes.md Outdated Show resolved Hide resolved
Copy link
Member

@mrbean-bremen mrbean-bremen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@mrbean-bremen mrbean-bremen merged commit 6f5eaec into svg-net:master Dec 17, 2023
7 checks passed
github-actions bot pushed a commit that referenced this pull request Dec 17, 2023
…ildProcessTemplates CONTRIBUTING.md Generators Nuget README.md Samples Source Svg.Custom Tests doc docfx.json index.md license.txt Fix declaring node also allows to use prefix for namespace BuildProcessTemplates CONTRIBUTING.md Generators Nuget README.md Samples Source Svg.Custom Tests doc docfx.json index.md license.txt Release notes written and changelog added BuildProcessTemplates CONTRIBUTING.md Generators Nuget README.md Samples Source Svg.Custom Tests doc docfx.json index.md license.txt Used string.Equals uniformly
github-actions bot pushed a commit to inforithmics/SVG that referenced this pull request Dec 17, 2023
) BuildProcessTemplates CONTRIBUTING.md Generators Nuget README.md Samples Source Svg.Custom Tests doc docfx.json index.md license.txt Fix declaring node also allows to use prefix for namespace
 BuildProcessTemplates CONTRIBUTING.md Generators Nuget README.md Samples Source Svg.Custom Tests doc docfx.json index.md license.txt Release notes written and changelog added
 BuildProcessTemplates CONTRIBUTING.md Generators Nuget README.md Samples Source Svg.Custom Tests doc docfx.json index.md license.txt Used string.Equals uniformly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants