@journeyapps/core-xml
Advanced tools
Comparing version 0.0.0-dev.362799e.d299e33 to 0.0.0-dev.375c2d3.0ce5033
@@ -1,2 +0,2 @@ | ||
import { XMLElement, XMLNode, IterableNodeList } from '@journeyapps/domparser/types'; | ||
import { XMLElement, XMLNode, IterableNodeList, XMLCharacterNode } from '@journeyapps/domparser/types'; | ||
export declare const ELEMENT_NODE = 1; | ||
@@ -7,2 +7,5 @@ export declare const ATTRIBUTE_NODE = 2; | ||
export declare function isElement(node: unknown): node is XMLElement; | ||
export declare function isText(node: unknown): node is XMLCharacterNode; | ||
export declare function isCdataNode(node: unknown): node is XMLCharacterNode; | ||
export declare function isCommentNode(node: unknown): node is XMLCharacterNode; | ||
/** | ||
@@ -14,2 +17,3 @@ * Subset of the fields we're interested in. | ||
name: string; | ||
value: string; | ||
} | ||
@@ -16,0 +20,0 @@ /** |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const xml_1 = require("./xml"); | ||
const types_1 = require("@journeyapps/domparser/types"); | ||
const utils_1 = require("./utils"); | ||
const serialize_1 = require("./serialize"); | ||
/** | ||
@@ -18,3 +19,3 @@ * Given a document, return a copy that is formatted. | ||
const child = document.childNodes.item(i); | ||
if (isText(child)) { | ||
if (utils_1.isText(child)) { | ||
// workaround for xmldom that has text nodes in the document | ||
@@ -30,3 +31,2 @@ continue; | ||
const doc = pretty(document, options); | ||
// TODO: close tags with " />". | ||
return serializeToString(doc); | ||
@@ -36,6 +36,5 @@ } | ||
function serializeToString(node) { | ||
const { serializer } = xml_1.getParser(); | ||
if (node.nodeType != types_1.DOCUMENT_NODE) { | ||
// Not a document - use the default | ||
return serializer.serializeToString(node); | ||
return serialize_1.serializer.serializeToString(node); | ||
} | ||
@@ -55,9 +54,9 @@ // Whitespace characters between processing instructions are lost, and browsers serialize them differently. | ||
const children = node.childNodes; | ||
for (var i = 0; i < children.length; i++) { | ||
for (let i = 0; i < children.length; i++) { | ||
const child = children[i]; | ||
if (isText(child)) { | ||
if (utils_1.isText(child)) { | ||
// Workaround for xmldom inserting extra newlines | ||
continue; | ||
} | ||
const part = serializer.serializeToString(children[i]); | ||
const part = serialize_1.serializer.serializeToString(child); | ||
result += part; | ||
@@ -85,8 +84,2 @@ result += '\n'; | ||
exports.stripWhitespace = stripWhitespace; | ||
function isElement(node) { | ||
return node.nodeType == types_1.ELEMENT_NODE; | ||
} | ||
function isText(node) { | ||
return node.nodeType == types_1.TEXT_NODE; | ||
} | ||
function indent(n, options) { | ||
@@ -101,5 +94,12 @@ let r = '\n'; | ||
const DOUBLE_NEW_LINE = {}; | ||
function removeFinalNewline(text) { | ||
const lines = text.split('\n'); | ||
if (/^\s*$/.test(lines[lines.length - 1])) { | ||
lines.pop(); | ||
} | ||
return lines.join('\n'); | ||
} | ||
function prettyNode(doc, node, options, level) { | ||
let newNode = node.cloneNode(false); | ||
if (isText(node)) { | ||
if (utils_1.isText(node)) { | ||
const trimmedText = node.nodeValue.trim(); | ||
@@ -122,3 +122,3 @@ const newLines = node.nodeValue.split('\n').length - 1; | ||
} | ||
else if (isElement(node)) { | ||
else if (utils_1.isElement(node)) { | ||
let filteredChildren = []; | ||
@@ -130,3 +130,4 @@ for (let i = 0; i < node.childNodes.length; i++) { | ||
filteredChildren = filteredChildren.filter(node => node != null); | ||
if (filteredChildren.length == 1 && isText(filteredChildren[0])) { | ||
if (filteredChildren.length == 1 && utils_1.isText(filteredChildren[0])) { | ||
// Text on its own. Preserve whitespace. | ||
newNode.appendChild(filteredChildren[0]); | ||
@@ -156,6 +157,16 @@ } | ||
else { | ||
if (level != null) { | ||
newNode.appendChild(doc.createTextNode(indent(level + 1, options))); | ||
if (utils_1.isText(child)) { | ||
// Text in mixed-mode. | ||
// We currently preserve whitespace _before_ the text element, but not _after_. | ||
// If the last line is a blank line, remove it, and replace it with our indentation. | ||
let text = child.nodeValue; | ||
newNode.appendChild(doc.createTextNode(removeFinalNewline(text))); | ||
} | ||
newNode.appendChild(child); | ||
else { | ||
if (level != null) { | ||
const indentation = indent(level + 1, options); | ||
newNode.appendChild(doc.createTextNode(indentation)); | ||
} | ||
newNode.appendChild(child); | ||
} | ||
} | ||
@@ -162,0 +173,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const types_1 = require("@journeyapps/domparser/types"); | ||
// From: http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1950641247 | ||
@@ -19,2 +20,14 @@ // These constants are also defined one Node, but it's better to not | ||
exports.isElement = isElement; | ||
function isText(node) { | ||
return node.nodeType == exports.TEXT_NODE; | ||
} | ||
exports.isText = isText; | ||
function isCdataNode(node) { | ||
return node.nodeType == types_1.CDATA_SECTION_NODE; | ||
} | ||
exports.isCdataNode = isCdataNode; | ||
function isCommentNode(node) { | ||
return node.nodeType == types_1.COMMENT_NODE; | ||
} | ||
exports.isCommentNode = isCommentNode; | ||
/** | ||
@@ -21,0 +34,0 @@ * Set or clear a set of attributes. |
@@ -160,3 +160,9 @@ "use strict"; | ||
if (element.getAttributeNode(attributeName)) { | ||
return { ownerElement: element, name: attributeName }; | ||
return { | ||
ownerElement: element, | ||
name: attributeName, | ||
get value() { | ||
return element.getAttribute(attributeName); | ||
} | ||
}; | ||
} | ||
@@ -163,0 +169,0 @@ else { |
{ | ||
"name": "@journeyapps/core-xml", | ||
"version": "0.0.0-dev.362799e.d299e33", | ||
"version": "0.0.0-dev.375c2d3.0ce5033", | ||
"description": "Journey JS library", | ||
@@ -18,3 +18,3 @@ "main": "./dist/src/node.js", | ||
"devDependencies": { | ||
"@journeyapps/core-test-helpers": "0.0.0-dev.362799e.d299e33" | ||
"@journeyapps/core-test-helpers": "0.0.0-dev.375c2d3.0ce5033" | ||
}, | ||
@@ -26,3 +26,3 @@ "files": [ | ||
], | ||
"gitHead": "fa37b6feabcb4c1474d813d54d8380c4d5137029" | ||
"gitHead": "9eabd513e30ef59a7c471e2999bf2189201bb6da" | ||
} |
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
104727
37
1292