@journeyapps/core-xml
Advanced tools
Comparing version 0.0.0-dev.375c2d3.0ce5033 to 0.0.0-dev.3b0b1d9.3341706
@@ -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 @@ /** |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./xml")); | ||
__export(require("./utils")); | ||
__export(require("./OrderedIncrementalUpdater")); | ||
__export(require("./UnorderedIncrementalUpdater")); | ||
__export(require("./pretty")); | ||
__exportStar(require("./xml"), exports); | ||
__exportStar(require("./utils"), exports); | ||
__exportStar(require("./OrderedIncrementalUpdater"), exports); | ||
__exportStar(require("./UnorderedIncrementalUpdater"), exports); | ||
__exportStar(require("./pretty"), exports); | ||
__exportStar(require("./ElementBuilder"), exports); | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
// This is the default import for NodeJS. | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// Export the same values as for browsers. | ||
__export(require("./index")); | ||
__exportStar(require("./index"), exports); | ||
// Auto-configure the parser | ||
require("./domparser"); | ||
//# sourceMappingURL=node.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.OrderedIncrementalUpdater = void 0; | ||
const index_1 = require("./index"); | ||
@@ -35,2 +36,12 @@ /** | ||
} | ||
/** | ||
* 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 +69,3 @@ const doc = element.ownerDocument; | ||
// 2. Merge source elements. | ||
let seenSourceText = false; | ||
if (this.sourceElement) { | ||
@@ -72,2 +84,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 +114,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 +120,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.stripWhitespace = exports.serializeToString = exports.prettyText = exports.pretty = void 0; | ||
const types_1 = require("@journeyapps/domparser/types"); | ||
@@ -13,6 +14,3 @@ const utils_1 = require("./utils"); | ||
const newDoc = document.implementation.createDocument(document.documentElement.tagName, null, null); | ||
const actualOptions = { | ||
indentSpaces: 4, | ||
...options | ||
}; | ||
const actualOptions = Object.assign({ indentSpaces: 4 }, options); | ||
for (let i = 0; i < document.childNodes.length; i++) { | ||
@@ -29,6 +27,16 @@ const child = document.childNodes.item(i); | ||
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 = Object.assign({ indentSpaces: 4 }, options); | ||
const prettyElement = prettyNode(node.ownerDocument, node, actualOptions, 0); | ||
return serializeToString(prettyElement); | ||
} | ||
} | ||
exports.prettyText = prettyText; | ||
@@ -44,4 +52,3 @@ function serializeToString(node) { | ||
let result = ''; | ||
if (node.firstChild && | ||
node.firstChild.nodeType == types_1.PROCESSING_INSTRUCTION_NODE) { | ||
if (node.firstChild && node.firstChild.nodeType == types_1.PROCESSING_INSTRUCTION_NODE) { | ||
// Has a processing instruction. | ||
@@ -125,3 +132,3 @@ } | ||
} | ||
filteredChildren = filteredChildren.filter(node => node != null); | ||
filteredChildren = filteredChildren.filter((node) => node != null); | ||
if (filteredChildren.length == 1 && utils_1.isText(filteredChildren[0])) { | ||
@@ -128,0 +135,0 @@ // Text on its own. Preserve whitespace. |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.serialize = exports.serializer = void 0; | ||
const types_1 = require("@journeyapps/domparser/types"); | ||
@@ -30,4 +31,3 @@ const utils_1 = require("./utils"); | ||
} | ||
if ((prefix === 'xml' && uri === 'http://www.w3.org/XML/1998/namespace') || | ||
uri == 'http://www.w3.org/2000/xmlns/') { | ||
if ((prefix === 'xml' && uri === 'http://www.w3.org/XML/1998/namespace') || uri == 'http://www.w3.org/2000/xmlns/') { | ||
return false; | ||
@@ -114,4 +114,3 @@ } | ||
} | ||
else if (node.nodeType == types_1.DOCUMENT_NODE || | ||
node.nodeType == types_1.DOCUMENT_FRAGMENT_NODE) { | ||
else if (node.nodeType == types_1.DOCUMENT_NODE || node.nodeType == types_1.DOCUMENT_FRAGMENT_NODE) { | ||
let child = node.firstChild; | ||
@@ -118,0 +117,0 @@ while (child) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.UnorderedIncrementalUpdater = void 0; | ||
const index_1 = require("./index"); | ||
@@ -4,0 +5,0 @@ /** |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.iter = exports.setAttributes = exports.isCommentNode = exports.isCdataNode = exports.isText = exports.isElement = exports.isAttribute = exports.DOCUMENT_NODE = exports.TEXT_NODE = exports.ATTRIBUTE_NODE = exports.ELEMENT_NODE = void 0; | ||
const types_1 = require("@journeyapps/domparser/types"); | ||
@@ -10,6 +11,6 @@ // From: http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1950641247 | ||
exports.TEXT_NODE = 3; | ||
exports.DOCUMENT_NODE = 9; | ||
function isAttribute(node) { | ||
return (node.nodeType == exports.ATTRIBUTE_NODE || | ||
(typeof node.name == 'string' && | ||
typeof node.ownerElement == 'object')); | ||
(typeof node.name == 'string' && typeof node.ownerElement == 'object')); | ||
} | ||
@@ -36,2 +37,6 @@ exports.isAttribute = isAttribute; | ||
* | ||
* 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 { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.childContent = exports.childNode = exports.documentToText = exports.createDocument = exports.parse = exports.configureParser = exports.getParser = exports.validateChildren = exports.children = exports.attribute = exports.parseElement = exports.warning = exports.error = exports.attributeNode = exports.elementTextPosition = exports.attributeValuePosition = exports.getPosition = exports.getAttribute = void 0; | ||
const utils_1 = require("./utils"); | ||
@@ -60,5 +61,3 @@ let configuredParser = null; | ||
} | ||
else if (utils_1.isElement(node) && | ||
node.openStart != null && | ||
node.nameEnd != null) { | ||
else if (utils_1.isElement(node) && node.openStart != null && node.nameEnd != null) { | ||
// This is an element | ||
@@ -70,8 +69,4 @@ position.start = locator.position(node.openStart + 1); | ||
// This is an attribute | ||
var attrPosition = node.ownerElement.attributePositions == null | ||
? null | ||
: node.ownerElement.attributePositions[node.name]; | ||
if (attrPosition != null && | ||
attrPosition.valueStart != null && | ||
attrPosition.end != null) { | ||
var attrPosition = node.ownerElement.attributePositions == null ? null : node.ownerElement.attributePositions[node.name]; | ||
if (attrPosition != null && attrPosition.valueStart != null && attrPosition.end != null) { | ||
if (attrPosition.end - attrPosition.valueStart > 2) { | ||
@@ -97,4 +92,3 @@ // Exclude quotes | ||
if (locator) { | ||
if (element.attributePositions != null && | ||
element.attributePositions.hasOwnProperty(attributeName)) { | ||
if (element.attributePositions != null && element.attributePositions.hasOwnProperty(attributeName)) { | ||
var p = element.attributePositions[attributeName]; | ||
@@ -118,4 +112,3 @@ return { | ||
if (locator) { | ||
if (element.attributePositions != null && | ||
element.attributePositions.hasOwnProperty(attributeName)) { | ||
if (element.attributePositions != null && element.attributePositions.hasOwnProperty(attributeName)) { | ||
var p = element.attributePositions[attributeName]; | ||
@@ -276,5 +269,3 @@ if (start == end) { | ||
if (options.indexOf(value) == -1) { | ||
var message = customMessage == null | ||
? element.name + ' must be one of ' + options | ||
: customMessage; | ||
var message = customMessage == null ? element.name + ' must be one of ' + options : customMessage; | ||
throw new Error(message); | ||
@@ -292,5 +283,3 @@ } | ||
if (options.indexOf(value) == -1) { | ||
var message = customMessage == null | ||
? element.name + ' must be one of ' + options | ||
: customMessage; | ||
var message = customMessage == null ? element.name + ' must be one of ' + options : customMessage; | ||
throw new Error(message); | ||
@@ -315,5 +304,3 @@ } | ||
var message = 'Invalid values: ' + invalidValues + '. '; | ||
var extraMessage = customMessage == null | ||
? element.name + ' values must be from ' + options | ||
: customMessage; | ||
var extraMessage = customMessage == null ? element.name + ' values must be from ' + options : customMessage; | ||
throw new Error(message + extraMessage); | ||
@@ -386,4 +373,3 @@ } | ||
function loadDefaultParser() { | ||
if (typeof document != 'undefined' && | ||
typeof document.implementation != 'undefined') { | ||
if (typeof document != 'undefined' && typeof document.implementation != 'undefined') { | ||
configureParser({ | ||
@@ -390,0 +376,0 @@ implementation: document.implementation, |
{ | ||
"name": "@journeyapps/core-xml", | ||
"version": "0.0.0-dev.375c2d3.0ce5033", | ||
"version": "0.0.0-dev.3b0b1d9.3341706", | ||
"description": "Journey JS library", | ||
@@ -18,3 +18,3 @@ "main": "./dist/src/node.js", | ||
"devDependencies": { | ||
"@journeyapps/core-test-helpers": "0.0.0-dev.375c2d3.0ce5033" | ||
"@journeyapps/core-test-helpers": "0.0.0-dev.3b0b1d9.3341706" | ||
}, | ||
@@ -26,3 +26,3 @@ "files": [ | ||
], | ||
"gitHead": "9eabd513e30ef59a7c471e2999bf2189201bb6da" | ||
"gitHead": "95e879645cc2ab0b6cae0e9da44f52c84ae1ce16" | ||
} |
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
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
110881
1370