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
14 changes: 14 additions & 0 deletions spec/validator_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,20 @@ attribute2="attribute2"
validate('<rootNode>jekyll &abcdefghij1234567890;</rootNode>');
validate('<rootNode>jekyll &abcdefghij1234567890a;</rootNode>', error); // limit to 20 chars
});

it('should validate entity names with "-", "." and other XML name characters', function () {
const error = {
InvalidChar: "char '&' is not expected."
};
validate('<rootNode>jekyll &foo-bar; hyde</rootNode>');
validate('<rootNode>jekyll &foo.bar; hyde</rootNode>');
validate('<rootNode>jekyll &foo:bar; hyde</rootNode>');
validate('<rootNode>jekyll &caf\u00e9; hyde</rootNode>');
validate('<!DOCTYPE doc [<!ENTITY foo-bar "hello">]><a>&foo-bar;</a>');
validate('<rootNode>jekyll &foo bar; hyde</rootNode>', error);
validate('<rootNode>jekyll &foo+bar; hyde</rootNode>', error);
validate('<rootNode>jekyll &foo-bar hyde</rootNode>', error);
});
});

describe("should not validate XML documents with multiple root nodes", () => {
Expand Down
5 changes: 5 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const nameStartChar = ':A-Za-z_\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0
const nameChar = nameStartChar + '\\-.\\d\\u00B7\\u0300-\\u036F\\u203F-\\u2040';
export const nameRegexp = '[' + nameStartChar + '][' + nameChar + ']*';
const regexName = new RegExp('^' + nameRegexp + '$');
const regexNameChar = new RegExp('^[' + nameChar + ']$');

export function getAllMatches(string, regex) {
const matches = [];
Expand All @@ -26,6 +27,10 @@ export const isName = function (string) {
return !(match === null || typeof match === 'undefined');
}

export const isNameChar = function (char) {
return regexNameChar.test(char);
}

export function isExist(v) {
return typeof v !== 'undefined';
}
Expand Down
4 changes: 2 additions & 2 deletions src/validator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import { isName } from './util.js';
import { isName, isNameChar } from './util.js';

const defaultOptions = {
allowBooleanAttributes: false, //A tag can have attributes without any value
Expand Down Expand Up @@ -471,7 +471,7 @@ function validateAmpersand(xmlData, i) {
}
let count = 0;
for (; i < xmlData.length; i++, count++) {
if (xmlData[i].match(/\w/) && count < 20)
if (isNameChar(xmlData[i]) && count < 20)
continue;
if (xmlData[i] === ';')
break;
Expand Down