Commit 96e73439 authored by David Sveningsson's avatar David Sveningsson

fix(meta): support case-insensitive regexp flag

fixes #69
parent cfcc4889
Pipeline #128400480 passed with stages
in 9 minutes and 19 seconds
export { MetaTable } from "./table";
export {
MetaData,
MetaDataTable,
MetaElement,
ElementTable,
MetaLookupableProperty,
MetaCopyableProperty,
PropertyExpression,
......
......@@ -328,20 +328,56 @@ describe("MetaTable", () => {
});
});
it("should expand regexp", () => {
expect.assertions(2);
const table = new MetaTable();
table.loadFromObject({
foo: mockEntry({
attributes: {
attr: ["foo", "/bar/", /baz/],
},
}),
describe("regexp", () => {
it("should expand regular expression from string", () => {
expect.assertions(2);
const table = new MetaTable();
table.loadFromObject({
foo: mockEntry({
attributes: {
attr: ["/foo/"],
},
}),
});
const meta = table.getMetaFor("foo");
expect(meta).not.toBeUndefined();
expect(meta.attributes).toEqual({
attr: [/foo/],
});
});
it("should handle case-insensitive flag", () => {
expect.assertions(2);
const table = new MetaTable();
table.loadFromObject({
foo: mockEntry({
attributes: {
attr: ["/foo/i"],
},
}),
});
const meta = table.getMetaFor("foo");
expect(meta).not.toBeUndefined();
expect(meta.attributes).toEqual({
attr: [/foo/i],
});
});
const meta = table.getMetaFor("foo");
expect(meta).not.toBeUndefined();
expect(meta.attributes).toEqual({
attr: ["foo", /bar/, /baz/],
it("should retain literal regexp", () => {
expect.assertions(2);
const table = new MetaTable();
table.loadFromObject({
foo: mockEntry({
attributes: {
attr: [/foo/],
},
}),
});
const meta = table.getMetaFor("foo");
expect(meta).not.toBeUndefined();
expect(meta.attributes).toEqual({
attr: [/foo/],
});
});
});
......
......@@ -221,10 +221,11 @@ function expandRegexValue(value: string | RegExp): string | RegExp {
if (value instanceof RegExp) {
return value;
}
const match = value.match(/^\/(.*)\/$/);
const match = value.match(/^\/(.*)\/([i]*)$/);
if (match) {
const [, expr, flags] = match;
// eslint-disable-next-line security/detect-non-literal-regexp
return new RegExp(match[1]);
return new RegExp(expr, flags);
} else {
return value;
}
......
import HtmlValidate from "../htmlvalidate";
import "../matchers";
import { MetaDataTable } from "../meta";
import { processAttribute } from "../transform/mocks/attribute";
const metadata: MetaDataTable = {
"mock-element": {
attributes: {
"case-insensitive": ["/foo/i"],
},
},
};
describe("rule attribute-allowed-values", () => {
let htmlvalidate: HtmlValidate;
beforeAll(() => {
htmlvalidate = new HtmlValidate({
elements: ["html5", metadata],
rules: { "attribute-allowed-values": "error" },
});
});
......@@ -89,6 +99,18 @@ describe("rule attribute-allowed-values", () => {
expect(report).toBeValid();
});
it("should support case-insensitive comparison", () => {
expect.assertions(1);
const report = htmlvalidate.validateString(
[
'<mock-element case-insensitive="foo"></mock-element>',
'<mock-element case-insensitive="FOO"></mock-element>',
'<mock-element case-insensitive="Foo"></mock-element>',
].join("\n")
);
expect(report).toBeValid();
});
it("smoketest", () => {
expect.assertions(1);
const report = htmlvalidate.validateFile(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment