Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions jsonml-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,38 @@ if (typeof module === 'object') {
* @return {boolean}
*/
var isElement = JsonML.isElement = function(jml) {
return isArray(jml) && ('string' === typeof jml[0]);
return isArray(jml) && ('string' === typeof jml[0]) && (jml[0].charAt(0) !== '?');
};

/**
* @param {*} jml
* @return {boolean}
*/
var isProcessingInstruction = JsonML.isProcessingInstruction = function(jml) {
return isArray(jml) && ('string' === typeof jml[0]) && (jml[0].charAt(0) === '?');
};

/**
* @param {*} jml
* @return {string}
*/
var getTarget = JsonML.getTarget = function(jml) {
if (!isProcessingInstruction(jml)) {
throw new SyntaxError('invalid JsonML');
}
return jml[0].substring(1);
};

/**
* @param {*} jml
* @return {string}
*/
var getData = JsonML.getData = function(jml) {
if (!isProcessingInstruction(jml)) {
throw new SyntaxError('invalid JsonML');
}
return jml[1];
};
/**
* @param {*} jml
* @return {boolean}
Expand Down Expand Up @@ -196,7 +225,7 @@ if (typeof module === 'object') {

} else if (child && 'object' === typeof child) {
if (isArray(child)) {
if (!isElement(child)) {
if (!isElement(child) && !isProcessingInstruction(child)) {
throw new SyntaxError('invalid JsonML');
}

Expand Down
22 changes: 20 additions & 2 deletions jsonml-xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ if (typeof module === 'object') {

} else if (tag.charAt(0) === '!') {
return document.createComment(tag === '!' ? '' : tag.substr(1)+' ');
} else if (tag.charAt(0) === '?') {
return document.createProcessingInstruction(tag === '!' ? '' : tag.substr(1), '');
}

return document.createElement(tag);
Expand Down Expand Up @@ -99,7 +101,8 @@ if (typeof module === 'object') {
if (child.nodeType === 3) { // text node
elem.nodeValue += child.nodeValue;
}

} else if (elem.nodeType === 7) {
elem.data = child.data;
} else if (elem.canHaveChildren !== false) {
elem.appendChild(child);
}
Expand Down Expand Up @@ -250,6 +253,10 @@ if (typeof module === 'object') {

addChildren(elem, filter, jml);

if (jml[0] === '' && jml.length === 2) {
jml = jml[1]
}

// filter result
if ('function' === typeof filter) {
jml = filter(jml, elem);
Expand All @@ -264,6 +271,17 @@ if (typeof module === 'object') {
// free references
elem = null;
return str;
case 7: // ProcessingInstruction node
var jml = ['?'+elem.target, elem.data]

// filter result
if ('function' === typeof filter) {
jml = filter(jml, elem);
}

// free references
elem = null;
return jml;
case 10: // doctype
jml = ['!'];

Expand Down Expand Up @@ -361,7 +379,7 @@ if (typeof module === 'object') {
*/
JsonML.fromXMLText = function(xmlText, filter) {
var elem = parseXML(xmlText);
elem = elem && (elem.ownerDocument || elem).documentElement;
elem = elem && (elem.ownerDocument || elem);

return fromXML(elem, filter);
};
Expand Down
17 changes: 17 additions & 0 deletions test/xmlTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,21 @@ test('JsonML.fromXMLText/.toXMLText roundtrip, comments', function() {
same(actual, expected);
});

test('JsonML.fromXMLText/.toXMLText roundtrip, processing instructions', function() {

var expected =
'<?some-pi and its data?>' +
'<foo>' +
'<?another-pi with data?>' +
'</foo>';

// JsonML will strip the XML Declaration
var input = '<?xml version="1.0"?>' + expected;

var jml = JsonML.fromXMLText(input);
var actual = JsonML.toXMLText(jml);

same(actual, expected);
});

}catch(ex){alert(ex);}