From e0e40f6e88d2bf68372bd61410e414850a142a0a Mon Sep 17 00:00:00 2001 From: Jan Holthuis Date: Sun, 11 Jun 2017 17:15:44 +0200 Subject: [PATCH 1/2] Add unittests for Processing Instructions --- test/dom/index.js | 1 + test/dom/pi.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 test/dom/pi.js diff --git a/test/dom/index.js b/test/dom/index.js index 8977eb8..15b0952 100644 --- a/test/dom/index.js +++ b/test/dom/index.js @@ -1,6 +1,7 @@ require('./attr'); require('./clone'); require('./element'); +require('./pi'); require('./fragment'); require('./level3'); require('./serializer'); diff --git a/test/dom/pi.js b/test/dom/pi.js new file mode 100644 index 0000000..9be324c --- /dev/null +++ b/test/dom/pi.js @@ -0,0 +1,30 @@ +var wows = require('vows'); +var DOMParser = require('xmldom').DOMParser; +var assert = require('assert') +var XMLSerializer = require('xmldom').XMLSerializer; +// Create a Test Suite +wows.describe('Processing Instruction Parse').addBatch({ + 'Generic PI': function () { + var doc = new DOMParser().parseFromString(''); + var pi = doc.childNodes[0]; + console.assert(doc.childNodes.length == 2); + console.assert(pi.nodeType == 7); + console.assert(pi.target == 'foo'); + console.assert(pi.data == 'bar baz'); + }, + 'XML stylesheet PI': function () { + var doc = new DOMParser().parseFromString(''); + var pi = doc.childNodes[0]; + console.assert(doc.childNodes.length == 2); + console.assert(pi.nodeType == 7); + console.assert(pi.target == 'xml-stylesheet'); + console.assert(pi.data == 'href="style.css"'); + }, + 'XML declaration is not a PI': function () { + var doc = new DOMParser().parseFromString(''); + var elem = doc.childNodes[0]; + console.log(doc) + console.assert(doc.childNodes.length == 1); + console.assert(elem.nodeType == 1); + }, +}).run(); // Run it From 5f560e2b8eecc10f887429800f490828fae8db21 Mon Sep 17 00:00:00 2001 From: Jan Holthuis Date: Sun, 11 Jun 2017 17:16:02 +0200 Subject: [PATCH 2/2] Don't treat XML declarations as Processing Instructions The XML declaration is not a processing instruction. According to XML 1.0 (5th Edition), Section 2.6, the PI target names "xml", "XML" and so on are reserved. Production Rule 17 states: > PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l')) This fixes #174. --- sax.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sax.js b/sax.js index f8b494e..f451330 100644 --- a/sax.js +++ b/sax.js @@ -551,8 +551,9 @@ function parseInstruction(source,start,domBuilder){ if(end){ var match = source.substring(start,end).match(/^<\?(\S*)\s*([\s\S]*?)\s*$/); if(match){ - var len = match[0].length; - domBuilder.processingInstruction(match[1], match[2]) ; + if(match[1].toLowerCase() !== 'xml'){//ignore XML declarations + domBuilder.processingInstruction(match[1], match[2]) ; + } return end+2; }else{//error return -1;