Skip to content

Commit

Permalink
add merror tag when a child node is missing
Browse files Browse the repository at this point in the history
see #29.

Currently the message is "missing child node", which isn't particularly helpful.
  • Loading branch information
christianp committed May 2, 2014
1 parent b4b6946 commit bfee5d7
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions ctop.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var CToP =
(function() {
/*
* Content MathML to Presentation MathML conversion
*
Expand All @@ -6,17 +8,24 @@
*/

var CToP = {
/* Transform the given <math> elements from Content MathML to Presentation MathML
/* Transform the given <math> elements from Content MathML to Presentation MathML and replace the original elements
*/
transform: function(elements){
for (var i = 0; i< elements.length;i++){
var mathNode = elements[i].cloneNode(false);
for(var j=0;j<elements[i].childNodes.length; j++ ) {
CToP.applyTransform(mathNode,elements[i].childNodes[j],0);
}
var mathNode = CToP.transformElement(elements[i]);
elements[i].parentNode.replaceChild(mathNode,elements[i]);
}
},

/* Transform a Content MathML element into Presentation MathML, and return the new element
*/
transformElement: function(element) {
var mathNode = element.cloneNode(false);
for(var j=0;j<element.childNodes.length; j++ ) {
CToP.applyTransform(mathNode,element.childNodes[j],0);
}
return mathNode;
},

/* Create an element with given name, belonging to the MathML namespace
*/
Expand Down Expand Up @@ -78,6 +87,12 @@ var CToP = {
/* Transform a Content MathML node to Presentation MathML node(s), and attach it to the parent
*/
applyTransform: function(parentNode,contentMMLNode,precedence) {
if(!contentMMLNode) {
var merror = CToP.createElement('merror');
CToP.appendToken(merror,'mtext','Missing child node');
parentNode.appendChild(merror);
return;
}
if (contentMMLNode.nodeType==document.ELEMENT_NODE) {
if(CToP.tokens[contentMMLNode.localName]) {
CToP.tokens[contentMMLNode.localName](parentNode,contentMMLNode,precedence);
Expand Down Expand Up @@ -1446,4 +1461,5 @@ CToP.applyTokens["partialdiff"] = function(parentNode,contentMMLNode,firstArg,ar
parentNode.appendChild(m);
}
}

return CToP;
})();

0 comments on commit bfee5d7

Please sign in to comment.