@journeyapps/core-xml
Advanced tools
Comparing version 4.0.1 to 4.1.0
@@ -26,5 +26,17 @@ import { XMLElement } from '@journeyapps/domparser/types'; | ||
private tagMap; | ||
/** | ||
* null means clear; undefined means unchanged. | ||
*/ | ||
private textContent; | ||
constructor(sourceElement: XMLElement, handledTags: string[]); | ||
append(sourceElement: XMLElement | null, builder: ElementBuilder): void; | ||
/** | ||
* If there is an existing text node, replace it. | ||
* If there are multiple text nodes, remove all after the first one. | ||
* If there are no text nodes, append one. | ||
* | ||
* @param text The text, or null to clear. | ||
*/ | ||
setTextContent(text: string | null): void; | ||
update(element: XMLElement): void; | ||
} |
@@ -14,3 +14,3 @@ import { XMLNode, XMLDocument } from '@journeyapps/domparser/types'; | ||
export declare function pretty(document: XMLDocument, options?: Partial<PrettyOptions>): XMLDocument; | ||
export declare function prettyText(document: XMLDocument, options?: Partial<PrettyOptions>): string; | ||
export declare function prettyText(node: XMLDocument | XMLNode, options?: Partial<PrettyOptions>): string; | ||
export declare function serializeToString(node: XMLNode): string; | ||
@@ -17,0 +17,0 @@ /** |
@@ -5,2 +5,3 @@ import { XMLElement, XMLNode, IterableNodeList, XMLCharacterNode } from '@journeyapps/domparser/types'; | ||
export declare const TEXT_NODE = 3; | ||
export declare const DOCUMENT_NODE = 9; | ||
export declare function isAttribute(node: unknown): node is AttributeNode; | ||
@@ -22,2 +23,6 @@ export declare function isElement(node: unknown): node is XMLElement; | ||
* | ||
* If an attribute value is either null or matches the default, and the current | ||
* element attribute value is also either null or matches the default, it is preserved. | ||
* If the current element attribute value is anything else, it is removed. | ||
* | ||
* @param element - The element to set the attributes on | ||
@@ -27,5 +32,8 @@ * @param attributes - The attributes to set. | ||
* If an attribute is present in the element but not this map, it is preserved. | ||
* @param defaults - Default attribute values, which are treated the same as not present. | ||
*/ | ||
export declare function setAttributes(element: XMLElement, attributes: { | ||
[key: string]: string | null; | ||
}, defaults?: { | ||
[key: string]: string; | ||
}): void; | ||
@@ -32,0 +40,0 @@ /** |
@@ -35,2 +35,12 @@ "use strict"; | ||
} | ||
/** | ||
* If there is an existing text node, replace it. | ||
* If there are multiple text nodes, remove all after the first one. | ||
* If there are no text nodes, append one. | ||
* | ||
* @param text The text, or null to clear. | ||
*/ | ||
setTextContent(text) { | ||
this.textContent = text; | ||
} | ||
update(element) { | ||
@@ -58,2 +68,3 @@ const doc = element.ownerDocument; | ||
// 2. Merge source elements. | ||
let seenSourceText = false; | ||
if (this.sourceElement) { | ||
@@ -72,2 +83,18 @@ const sourceNodes = this.sourceElement.childNodes; | ||
} | ||
else if (index_1.isText(child) && this.textContent !== undefined) { | ||
// Skip any nodes after the first one. | ||
if (!seenSourceText) { | ||
if (this.textContent !== null) { | ||
// Replace the text | ||
const textNode = doc.createTextNode(this.textContent); | ||
if (insertBefore) { | ||
element.insertBefore(textNode, insertBefore); | ||
} | ||
else { | ||
element.appendChild(textNode); | ||
} | ||
} | ||
seenSourceText = true; | ||
} | ||
} | ||
else { | ||
@@ -86,2 +113,6 @@ // Insert before the last known one | ||
} | ||
if (!seenSourceText && this.textContent !== undefined && this.textContent !== null) { | ||
// No source text found - append text | ||
element.appendChild(doc.createTextNode(this.textContent)); | ||
} | ||
} | ||
@@ -88,0 +119,0 @@ } |
@@ -28,6 +28,19 @@ "use strict"; | ||
exports.pretty = pretty; | ||
function prettyText(document, options) { | ||
const doc = pretty(document, options); | ||
return serializeToString(doc); | ||
function isDocument(document) { | ||
return document.nodeType == types_1.DOCUMENT_NODE; | ||
} | ||
function prettyText(node, options) { | ||
if (isDocument(node)) { | ||
const doc = pretty(node, options); | ||
return serializeToString(doc); | ||
} | ||
else { | ||
const actualOptions = { | ||
indentSpaces: 4, | ||
...options | ||
}; | ||
const prettyElement = prettyNode(node.ownerDocument, node, actualOptions, 0); | ||
return serializeToString(prettyElement); | ||
} | ||
} | ||
exports.prettyText = prettyText; | ||
@@ -34,0 +47,0 @@ function serializeToString(node) { |
@@ -10,2 +10,3 @@ "use strict"; | ||
exports.TEXT_NODE = 3; | ||
exports.DOCUMENT_NODE = 9; | ||
function isAttribute(node) { | ||
@@ -36,2 +37,6 @@ return (node.nodeType == exports.ATTRIBUTE_NODE || | ||
* | ||
* If an attribute value is either null or matches the default, and the current | ||
* element attribute value is also either null or matches the default, it is preserved. | ||
* If the current element attribute value is anything else, it is removed. | ||
* | ||
* @param element - The element to set the attributes on | ||
@@ -41,8 +46,20 @@ * @param attributes - The attributes to set. | ||
* If an attribute is present in the element but not this map, it is preserved. | ||
* @param defaults - Default attribute values, which are treated the same as not present. | ||
*/ | ||
function setAttributes(element, attributes) { | ||
function setAttributes(element, attributes, defaults) { | ||
if (defaults == null) { | ||
defaults = {}; | ||
} | ||
for (let key in attributes) { | ||
const value = attributes[key]; | ||
if (value == null) { | ||
element.removeAttribute(key); | ||
const defaultValue = defaults[key]; | ||
if (value == null || value === defaultValue) { | ||
if (element.getAttribute(key) !== defaultValue) { | ||
// The attribute is not currently equal to the default value, so we remove it. | ||
element.removeAttribute(key); | ||
} | ||
else { | ||
// The element currently contains the default value explicitly. | ||
// We preserve it. | ||
} | ||
} | ||
@@ -49,0 +66,0 @@ else { |
{ | ||
"name": "@journeyapps/core-xml", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "Journey JS library", | ||
@@ -25,3 +25,3 @@ "main": "./dist/src/node.js", | ||
], | ||
"gitHead": "3262b216750fd6383292e0f6a36551cdb702a804" | ||
"gitHead": "cf6c6164d75166033224c51e7ddfa640ad38d755" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
109350
1373