minify-xml
Advanced tools
Comparing version 4.1.0 to 4.2.0
@@ -31,2 +31,4 @@ #!/usr/bin/env node | ||
--collapse-whitespace-in-doctype Collapse whitespace in the document type declaration | ||
--remove-schema-location-attributes Remove schema location attributes | ||
--remove-unnecessary-standalone-declaration Remove unnecessary standalone in prolog | ||
--remove-unused-namespaces Remove any unused namespaces from tags | ||
@@ -95,2 +97,8 @@ --remove-unused-default-namespace Remove unused default namespace declaration | ||
}, | ||
removeSchemaLocationAttributes: { | ||
type: "boolean" | ||
}, | ||
removeUnnecessaryStandaloneDeclaration: { | ||
type: "boolean" | ||
}, | ||
removeUnusedNamespaces: { | ||
@@ -97,0 +105,0 @@ type: "boolean" |
32
index.js
@@ -12,2 +12,4 @@ const strict = "strict", strictOption = option => option === strict && { strict: true }; | ||
collapseWhitespaceInDocType: true, | ||
removeSchemaLocationAttributes: false, | ||
removeUnnecessaryStandaloneDeclaration: true, | ||
removeUnusedNamespaces: true, | ||
@@ -44,3 +46,4 @@ removeUnusedDefaultNamespace: true, | ||
prologPattern = tagPattern.replace(/(?<=(?<!\(\?)<).*(?=\\b)/, "\\?xml"), | ||
preservePattern = /(?<!<(?:[^\s\/>:]+:)?pre[^<]*?>|\s+xml:space\s*=\s*(?:"preserve"|'preserve')(?:\s+[^=\s>]+\s*=\s*(?:"[^"]*"|'[^']*'))*\s*>)/.source; | ||
docTypePattern = /<!DOCTYPE\s+([^\s>[]+)(?:\s+(SYSTEM|PUBLIC)\s+("[^"]*"|'[^']*')(?:\s+("[^"]*"|'[^']*'))?)?(?:\s*\[([^\]]*)\])?\s*>/.source, | ||
preservePattern = /(?<!<(?:[^\s\/>:]+:)?pre[^<]*?>|\s+xml:space\s*=\s*(?:"preserve"|'preserve'|preserve)(?:\s+[^=\s>]+\s*=\s*(?:"[^"]*"|'[^']*'))*\s*>)/.source; | ||
function findAllMatchesInTags(xml, regexp, options = { tagPattern, lookbehind: emptyRegExp, lookbehindPattern: String(), group: 0 }) { | ||
@@ -127,2 +130,7 @@ const lookbehindPattern = options.lookbehindPattern || (options.lookbehind || emptyRegExp).source; | ||
// remove any xsi:schemaLocation / xsi:noNamespaceSchemaLocation attributes <anyTag xsi:schemaLocation="/schema/" /> | ||
if (options.removeSchemaLocationAttributes) { | ||
xml = replaceInTags(xml, /\s+xsi:(?:noNamespaceS|s)chemaLocation\s*=\s*(?:"[^"]*"|'[^']*')/, replacer(" ")); | ||
} | ||
// remove / collapse whitespace in tags <anyTag attributeA = "..." attributeB = "..."> ... </anyTag > | ||
@@ -153,2 +161,13 @@ if (options.collapseWhitespaceInTags) { | ||
// remove remove unnecessary standalone declaration in prolog <?xml standalone = "yes" ?> | ||
// the standalone declaration has "no meaning" according to the W3C definition, in case neither the external subset of the DocType declaration | ||
// contains any markup declarations (<!ELEMENT, <!ATTLIST, <!ENTITY, <!NOTATION) or a parameter entity (<!ENTITY %) is defined in the any subset | ||
// (because we do not read the external subset definition file e.g. schema.dtd, we assume as soon as either a SYSTEM/PUBLIC subset is defined, the standalone attribute must stay) | ||
if (options.removeUnnecessaryStandaloneDeclaration) { | ||
const docType = xml.match(new RegExp(docTypePattern)); | ||
if (!docType || (!docType[2] && !(docType[5] && /<!ENTITY\s+%/.test(docType[5])))) { | ||
xml = replaceInTags(xml, /\s+standalone\s*=\s*(?:"yes"|'yes'|yes|"no"|'no'|no)/, emptyReplacer, { tagPattern: prologPattern }); | ||
} | ||
} | ||
// remove / collapse whitespace in the xml prolog <?xml version = "1.0" ?> | ||
@@ -161,3 +180,3 @@ if (options.collapseWhitespaceInProlog) { | ||
if (options.collapseWhitespaceInDocType) { | ||
xml = xml.replace(/<!DOCTYPE\s+([^\s>[]+)(?:\s+(SYSTEM|PUBLIC)\s+("[^"]*"|'[^']*')(?:\s+("[^"]*"|'[^']*'))?)?(?:\s*\[([^\]]*)\])?\s*>/, replacer( | ||
xml = xml.replace(new RegExp(docTypePattern), replacer( | ||
(match, name, type, literal1, literal2, subset) => `<!DOCTYPE ${name}${ [type, literal1, literal2] | ||
@@ -225,4 +244,5 @@ .map(token => token && " " + token).join(String()) }${ subset ? `[${ (xml => { | ||
all.forEach((ns, idx) => { | ||
if (ns.length === 1) { | ||
return; // already at minimal length | ||
// never shorten the special "xsi" namespace or if already at absolute minimal length | ||
if (ns === "xsi" || ns.length === 1) { | ||
return; | ||
} | ||
@@ -253,3 +273,5 @@ | ||
const unsupportedStreamOptions = ["removeUnusedNamespaces", "removeUnusedDefaultNamespace", "shortenNamespaces", "ignoreCData"]; | ||
// some options require prior knowledge, like 'removeUnnecessaryStandaloneDeclaration' will have to read the DocType first and | ||
// 'removeUnusedNamespaces' needs to scan the document for namespaces in use, thus some options cannot be used when streaming | ||
const unsupportedStreamOptions = ["removeUnnecessaryStandaloneDeclaration", "removeUnusedNamespaces", "removeUnusedDefaultNamespace", "shortenNamespaces", "ignoreCData"]; | ||
export const defaultStreamOptions = { | ||
@@ -256,0 +278,0 @@ ...defaultOptions, |
{ | ||
"name": "minify-xml", | ||
"version": "4.1.0", | ||
"version": "4.2.0", | ||
"description": "Fast XML minifier / compressor / uglifier with a command-line", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -94,2 +94,6 @@ # minify-xml | ||
- `removeSchemaLocationAttributes` (default: `false`): Remove any `xsi:schemaLocation` and `xsi:noNamespaceSchemaLocation` attributes `<anyTag xsi:schemaLocation = "..." />` | ||
- `removeUnnecessaryStandaloneDeclaration` (default: `true`): Remove an unnecessary standalone declaration in the xml prolog `<?xml version = "1.0" standalone = 'yes' ?>`. Note that according to the W3C standalone has "no meaning" and thus is removed, in case there are no external markup declarations. | ||
- `removeUnusedNamespaces` (default: `true`): Remove any namespaces from tags, which are not used anywhere in the document, like `<tag xmlns:unused="any_uri" />`. Notice the word *anywhere* here, the minifier not does consider the structure of the XML document, thus namespaces which might be only used in a certain sub-tree of elements might not be removed, even though they are not used in that sub-tree. | ||
@@ -96,0 +100,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47816
449
145