xslt-processor
Advanced tools
Comparing version 0.9.1 to 0.9.2
{ | ||
"name": "xslt-processor", | ||
"version": "0.9.1", | ||
"version": "0.9.2", | ||
"description": "a JavaScript XSLT Processor", | ||
@@ -5,0 +5,0 @@ "main": "dist/xslt-processor.js", |
140
src/dom.js
@@ -88,45 +88,2 @@ // Copyright 2018 Johannes Wilm | ||
// Splits string at delimiter when not inside of quotation marks | ||
function xmlSplit(str) { | ||
let parts = [], | ||
tag = false, | ||
quotes = false, | ||
doublequotes = false, | ||
comment = false, | ||
start = 0; | ||
for (let i = 0; i < str.length; ++i) { | ||
let char = str[i]; | ||
if (tag && char === "'") { | ||
quotes = !quotes; | ||
} else if (tag && char === "\"") { | ||
doublequotes = !doublequotes; | ||
} else if (tag && char === ">" && !quotes && !doublequotes) { | ||
parts.push({type:'tag', text: str.slice(start, i)}); | ||
start = i + 1; | ||
tag = false; | ||
quotes = false; | ||
doublequotes = false; | ||
} else if (!tag && char === "<") { | ||
parts.push(str.slice(start, i)); | ||
if (str.slice(i+1,i+4)==="!--") { | ||
let endTagIndex = str.slice(i+4).indexOf('-->'); | ||
if (endTagIndex) { | ||
parts.push({type: 'comment', text: str.slice(i+4, i+endTagIndex+4)}); | ||
i += endTagIndex+7; | ||
} | ||
} else if (str.slice(i+1,i+9)==="![CDATA[") { | ||
let endTagIndex = str.slice(i+9).indexOf(']]>'); | ||
if (endTagIndex) { | ||
parts.push({type: 'cdata', text: str.slice(i+9, i+endTagIndex+9)}); | ||
i += endTagIndex+12; | ||
} | ||
} else { | ||
tag = true; | ||
} | ||
start = i + 1; | ||
} | ||
} | ||
parts.push(str.slice(start)); | ||
return parts; | ||
} | ||
@@ -167,45 +124,66 @@ // Parses the given XML string with our custom, JavaScript XML parser. Written | ||
const x = xmlSplit(xml); | ||
for (let i = 1; i < x.length; i=i+2) { | ||
const tag = x[i]; | ||
const text = xmlResolveEntities(x[i+1]); | ||
let tag = false, | ||
quotes = false, | ||
doublequotes = false, | ||
start = 0; | ||
for (let i = 0; i < xml.length; ++i) { | ||
let char = xml.charAt(i); | ||
if (tag && char === "'") { | ||
quotes = !quotes; | ||
} else if (tag && char === "\"") { | ||
doublequotes = !doublequotes; | ||
} else if (tag && char === ">" && !quotes && !doublequotes) { | ||
let text = xml.slice(start, i); | ||
if (text.charAt(0) == '/') { | ||
stack.pop(); | ||
parent = stack[stack.length - 1]; | ||
} else if (text.charAt(0) == '?') { | ||
// Ignore XML declaration and processing instructions | ||
} else if (text.charAt(0) == '!') { | ||
// Ignore malformed notation and comments | ||
} else { | ||
const empty = text.match(regex_empty); | ||
const tagname = regex_tagname.exec(text)[1]; | ||
var node = domCreateElement(xmldoc, tagname); | ||
if (tag.type === 'cdata') { | ||
let node = domCreateCDATASection(xmldoc, tag.text); | ||
domAppendChild(parent, node); | ||
parent = node; | ||
stack.push(node); | ||
} else if (tag.type === 'comment') { | ||
let node = domCreateComment(xmldoc, tag.text); | ||
domAppendChild(parent, node); | ||
parent = node; | ||
stack.push(node); | ||
} else if (tag.text.charAt(0) == '/') { | ||
stack.pop(); | ||
parent = stack[stack.length - 1]; | ||
} else if (tag.text.charAt(0) == '?') { | ||
// Ignore XML declaration and processing instructions | ||
} else if (tag.text.charAt(0) == '!') { | ||
// Ignore malformed notation and comments | ||
} else { | ||
const empty = tag.text.match(regex_empty); | ||
const tagname = regex_tagname.exec(tag.text)[1]; | ||
var node = domCreateElement(xmldoc, tagname); | ||
let att; | ||
while (att = regex_attribute.exec(text)) { | ||
const val = xmlResolveEntities(att[5] || att[7] || ''); | ||
domSetAttribute(node, att[1], val); | ||
} | ||
let att; | ||
while (att = regex_attribute.exec(tag.text)) { | ||
const val = xmlResolveEntities(att[5] || att[7] || ''); | ||
domSetAttribute(node, att[1], val); | ||
domAppendChild(parent, node); | ||
if (!empty) { | ||
parent = node; | ||
stack.push(node); | ||
} | ||
} | ||
domAppendChild(parent, node); | ||
if (!empty) { | ||
parent = node; | ||
stack.push(node); | ||
start = i + 1; | ||
tag = false; | ||
quotes = false; | ||
doublequotes = false; | ||
} else if (!tag && char === "<") { | ||
let text = xml.slice(start, i) | ||
if (text && parent != root) { | ||
domAppendChild(parent, domCreateTextNode(xmldoc, text)); | ||
} | ||
if (xml.slice(i+1,i+4)==="!--") { | ||
let endTagIndex = xml.slice(i+4).indexOf('-->'); | ||
if (endTagIndex) { | ||
let node = domCreateComment(xmldoc, xml.slice(i+4, i+endTagIndex+4)); | ||
domAppendChild(parent, node); | ||
i += endTagIndex+7; | ||
} | ||
} else if (xml.slice(i+1,i+9)==="![CDATA[") { | ||
let endTagIndex = xml.slice(i+9).indexOf(']]>'); | ||
if (endTagIndex) { | ||
let node = domCreateCDATASection(xmldoc, xml.slice(i+9, i+endTagIndex+9)); | ||
domAppendChild(parent, node); | ||
i += endTagIndex+12; | ||
} | ||
} else { | ||
tag = true; | ||
} | ||
start = i + 1; | ||
} | ||
if (text && parent != root) { | ||
domAppendChild(parent, domCreateTextNode(xmldoc, text)); | ||
} | ||
} | ||
@@ -212,0 +190,0 @@ |
@@ -25,3 +25,3 @@ // Copyright 2018 Johannes Wilm | ||
// Throws an exception if false. | ||
export function assertNotFalse(b) { | ||
export function assert(b) { | ||
if (!b) { | ||
@@ -28,0 +28,0 @@ throw "Assertion failed"; |
@@ -93,3 +93,2 @@ // Copyright 2018 Johannes Wilm | ||
xsltPassThrough(input, template, output, outputDocument); | ||
} else { | ||
@@ -117,7 +116,8 @@ switch (nodename[1]) { | ||
const templates = []; | ||
for (var i = 0; i < top.childNodes.length; ++i) { | ||
var c = top.childNodes[i]; | ||
for (let i = 0; i < top.childNodes.length; ++i) { | ||
let c = top.childNodes[i]; | ||
if (c.nodeType == DOM_ELEMENT_NODE && | ||
c.nodeName == 'xsl:template' && | ||
c.getAttribute('mode') == mode) { | ||
(!mode || c.getAttribute('mode') == mode) | ||
) { | ||
templates.push(c); | ||
@@ -128,3 +128,3 @@ } | ||
const nj = sortContext.nodelist[j]; | ||
for (var i = 0; i < templates.length; ++i) { | ||
for (let i = 0; i < templates.length; ++i) { | ||
xsltProcessContext(sortContext.clone(nj, j), templates[i], output); | ||
@@ -634,3 +634,2 @@ } | ||
const expr = xpathParse(match); | ||
let ret; | ||
@@ -658,4 +657,3 @@ // Shortcut for the most common case. | ||
} | ||
return ret; | ||
} |
@@ -11,3 +11,3 @@ // Copyright 2018 Johannes Wilm | ||
import {xpathParse, ExprContext, StringValue, BooleanValue, NumberValue} from "../src/xpath.js" | ||
import {xmlValue, assertNotFalse} from "../src/util.js" | ||
import {xmlValue, assert} from "../src/util.js" | ||
import {xmlParse} from "../src/dom.js" | ||
@@ -333,3 +333,3 @@ //******************************************** | ||
for (let i = 0; i < expr.length; ++i) { | ||
assertNotFalse(expr[i], xpathParse(expr[i])); | ||
assert(expr[i], xpathParse(expr[i])); | ||
} | ||
@@ -336,0 +336,0 @@ } |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
557092
11202