Skip to content

Commit

Permalink
fix(sax): Only apply HTML rules if mimeType is present
Browse files Browse the repository at this point in the history
  • Loading branch information
karfau committed Feb 16, 2022
1 parent 04c9f26 commit 9072016
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
25 changes: 19 additions & 6 deletions lib/sax.js
@@ -1,4 +1,6 @@
var NAMESPACE = require("./conventions").NAMESPACE;
var conventions = require("./conventions");
var NAMESPACE = conventions.NAMESPACE;
var MIME_TYPE = conventions.MIME_TYPE;

//[4] NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
//[4a] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
Expand Down Expand Up @@ -50,6 +52,7 @@ XMLReader.prototype = {
}
}
function parse(source,defaultNSMapCopy,entityMap,domBuilder,errorHandler){
var isHTML = MIME_TYPE.isHTML(domBuilder.mimeType);
function fixedFromCharCode(code) {
// String.prototype.fromCharCode does not supports
// > 2 bytes unicode chars directly
Expand Down Expand Up @@ -162,7 +165,15 @@ function parse(source,defaultNSMapCopy,entityMap,domBuilder,errorHandler){
var el = new ElementAttributes();
var currentNSMap = parseStack[parseStack.length-1].currentNSMap;
//elStartEnd
var end = parseElementStartPart(source,tagStart,el,currentNSMap,entityReplacer,errorHandler);
var end = parseElementStartPart(
source,
tagStart,
el,
currentNSMap,
entityReplacer,
errorHandler,
isHTML
)
var len = el.length;


Expand Down Expand Up @@ -191,7 +202,7 @@ function parse(source,defaultNSMapCopy,entityMap,domBuilder,errorHandler){
}
}

if (NAMESPACE.isHTML(el.uri) && !el.closed) {
if (isHTML && !el.closed) {
end = parseHtmlSpecialContent(source,end,el.tagName,entityReplacer,domBuilder)
} else {
end++;
Expand Down Expand Up @@ -222,7 +233,9 @@ function copyLocator(f,t){
* @see #appendElement(source,elStartEnd,el,selfClosed,entityReplacer,domBuilder,parseStack);
* @return end of the elementStartPart(end of elementEndPart for selfClosed el)
*/
function parseElementStartPart(source,start,el,currentNSMap,entityReplacer,errorHandler){
function parseElementStartPart(
source,start,el,currentNSMap,entityReplacer,errorHandler, isHTML
){

/**
* @param {string} qname
Expand Down Expand Up @@ -337,7 +350,7 @@ function parseElementStartPart(source,start,el,currentNSMap,entityReplacer,error
errorHandler.warning('attribute "'+value+'" missed quot(")!');
addAttribute(attrName, value, start)
}else{
if(!NAMESPACE.isHTML(currentNSMap['']) || !value.match(/^(?:disabled|checked|selected)$/i)){
if(!isHTML || !value.match(/^(?:disabled|checked|selected)$/i)){
errorHandler.warning('attribute "'+value+'" missed value!! "'+value+'" instead!!')
}
addAttribute(value, value, start)
Expand Down Expand Up @@ -385,7 +398,7 @@ function parseElementStartPart(source,start,el,currentNSMap,entityReplacer,error
//case S_ATTR_NOQUOT_VALUE:void();break;
case S_ATTR_SPACE:
var tagName = el.tagName;
if (!NAMESPACE.isHTML(currentNSMap['']) || !attrName.match(/^(?:disabled|checked|selected)$/i)) {
if (!isHTML || !attrName.match(/^(?:disabled|checked|selected)$/i)) {
errorHandler.warning('attribute "'+attrName+'" missed value!! "'+attrName+'" instead2!!')
}
addAttribute(attrName, attrName, start);
Expand Down
21 changes: 21 additions & 0 deletions test/html/normalize.test.js
Expand Up @@ -53,6 +53,27 @@ describe('html normalizer', () => {
})
})

it.each([
`<html xmlns="http://www.w3.org/1999/xhtml"><script>let message = " &amp; ETH";</script></html>`,
`<html><script>let message = " &amp; ETH";</script></html>`,
])(`should %s`, (xml) => {
const { errors, parser } = getTestParser()

const actual = parser.parseFromString(xml, 'application/xml')

expect(actual.documentElement.firstChild.textContent).toBe('let message = " & ETH";');
})
it.each([
`<html xmlns="http://www.w3.org/1999/xhtml"><script>let message = " &amp; ETH";</script></html>`,
`<html><script>let message = " &amp; ETH";</script></html>`,
])(`should %s`, (xml) => {
const { errors, parser } = getTestParser()

const actual = parser.parseFromString(xml, 'text/html')

expect(actual.documentElement.firstChild.textContent).toBe('let message = " &amp; ETH";');
})

it('European entities', () => {
const { errors, parser } = getTestParser()

Expand Down

0 comments on commit 9072016

Please sign in to comment.