@markuplint/rules
Advanced tools
Comparing version 1.0.0-alpha.10 to 1.0.0-alpha.11
@@ -1,2 +0,3 @@ | ||
declare const _default: import("@markuplint/ml-core").MLRule<null, null>; | ||
export declare const rePCEN: RegExp; | ||
declare const _default: import("@markuplint/ml-core").MLRule<string | null, null>; | ||
export default _default; |
@@ -40,9 +40,45 @@ "use strict"; | ||
var ml_core_1 = require("@markuplint/ml-core"); | ||
/** | ||
* @see https://html.spec.whatwg.org/multipage/custom-elements.html#prod-potentialcustomelementname | ||
*/ | ||
var pcenChar = [ | ||
'\\-', | ||
'\\.', | ||
'[0-9]', | ||
'_', | ||
'[a-z]', | ||
'\u00B7', | ||
'[\u00C0-\u00D6]', | ||
'[\u00D8-\u00F6]', | ||
'[\u00F8-\u037D]', | ||
'[\u037F-\u1FFF]', | ||
'[\u200C-\u200D]', | ||
'[\u203F-\u2040]', | ||
'[\u2070-\u218F]', | ||
'[\u2C00-\u2FEF]', | ||
'[\u3001-\uD7FF]', | ||
'[\uF900-\uFDCF]', | ||
'[\uFDF0-\uFFFD]', | ||
'[\uD800-\uDBFF][\uDC00-\uDFFF]', | ||
].join('|'); | ||
exports.rePCEN = new RegExp("^[a-z](?:" + pcenChar + ")*\\-(?:" + pcenChar + ")*$"); | ||
// TODO: Ignore tags | ||
// const ignoreTags = [ | ||
// 'annotation-xml', | ||
// 'color-profile', | ||
// 'font-face', | ||
// 'font-face-src', | ||
// 'font-face-uri', | ||
// 'font-face-format', | ||
// 'font-face-name', | ||
// 'missing-glyph', | ||
// ]; | ||
exports.default = ml_core_1.createRule({ | ||
name: 'name', | ||
name: 'custom-element-naming', | ||
defaultValue: null, | ||
defaultOptions: null, | ||
defaultLevel: 'warning', | ||
verify: function (document, messages) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var reports, message; | ||
var reports; | ||
var _this = this; | ||
@@ -53,15 +89,25 @@ return __generator(this, function (_a) { | ||
reports = []; | ||
message = messages('error'); | ||
return [4 /*yield*/, document.walkOn('Element', function (node) { return __awaiter(_this, void 0, void 0, function () { | ||
var tagName, isCustomElement, pattern, rePattern; | ||
return __generator(this, function (_a) { | ||
// eslint-disable-next-line no-constant-condition | ||
if (true) { | ||
// reports.push({ | ||
// level: node.rule.level, | ||
// message, | ||
// line: node.line, | ||
// col: node.col, | ||
// raw: node.raw, | ||
// }); | ||
if (!node.rule.value) { | ||
return [2 /*return*/]; | ||
} | ||
tagName = node.nodeName; | ||
isCustomElement = exports.rePCEN.test(tagName); | ||
if (!isCustomElement) { | ||
return [2 /*return*/]; | ||
} | ||
pattern = node.rule.value; | ||
rePattern = new RegExp(pattern); | ||
if (rePattern.test(tagName)) { | ||
return [2 /*return*/]; | ||
} | ||
reports.push({ | ||
severity: node.rule.severity, | ||
message: messages("Invalid custom element name. Expected pattern is /" + pattern + "/"), | ||
line: node.startLine, | ||
col: node.startCol, | ||
raw: node.raw, | ||
}); | ||
return [2 /*return*/]; | ||
@@ -68,0 +114,0 @@ }); |
declare const _default: (import("@markuplint/ml-core").MLRule<boolean, import("./attr-spacing").AttrSpasingOptions> | import("@markuplint/ml-core").MLRule<boolean, null> | import("@markuplint/ml-core").MLRule<import("./class-naming").Value, null> | import("@markuplint/ml-core").MLRule<"always", { | ||
denyObsolateType: boolean; | ||
}> | import("@markuplint/ml-core").MLRule<import("./indentation").Value, import("./indentation").IndentationOptions> | import("@markuplint/ml-core").MLRule<boolean, import("./required-h1").RequiredH1Options>)[]; | ||
}> | import("@markuplint/ml-core").MLRule<import("./indentation").Value, import("./indentation").IndentationOptions> | import("@markuplint/ml-core").MLRule<boolean, { | ||
tag: string; | ||
contents: { | ||
require: string; | ||
min?: number | undefined; | ||
max?: number | undefined; | ||
} | { | ||
optional: string; | ||
} | { | ||
oneOrMore: string; | ||
max?: number | undefined; | ||
} | { | ||
choice: string[]; | ||
}; | ||
}[]> | import("@markuplint/ml-core").MLRule<boolean, import("./required-h1").RequiredH1Options>)[]; | ||
export default _default; |
@@ -1,2 +0,27 @@ | ||
declare const _default: import("@markuplint/ml-core").MLRule<null, null>; | ||
declare type TagRule = { | ||
tag: string; | ||
contents: Contents[]; | ||
}; | ||
declare type Contents = Required | Optional | OneOrMore | ZeroOrMore | Choice; | ||
declare type Required = { | ||
require: string; | ||
min?: number; | ||
max?: number; | ||
}; | ||
declare type Optional = { | ||
optional: string; | ||
max?: number; | ||
}; | ||
declare type OneOrMore = { | ||
oneOrMore: string; | ||
max?: number; | ||
}; | ||
declare type ZeroOrMore = { | ||
zeroOrMore: string; | ||
max?: number; | ||
}; | ||
declare type Choice = { | ||
choice: Contents[]; | ||
}; | ||
declare const _default: import("@markuplint/ml-core").MLRule<boolean, TagRule[]>; | ||
export default _default; |
@@ -38,11 +38,18 @@ "use strict"; | ||
}; | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var ml_core_1 = require("@markuplint/ml-core"); | ||
exports.default = ml_core_1.createRule({ | ||
name: 'name', | ||
defaultValue: null, | ||
defaultOptions: null, | ||
name: 'permitted-contents', | ||
defaultValue: true, | ||
defaultOptions: [], | ||
verify: function (document, messages) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var reports, message; | ||
var reports; | ||
var _this = this; | ||
@@ -53,15 +60,125 @@ return __generator(this, function (_a) { | ||
reports = []; | ||
message = messages('error'); | ||
return [4 /*yield*/, document.walkOn('Element', function (node) { return __awaiter(_this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
// eslint-disable-next-line no-constant-condition | ||
if (true) { | ||
// reports.push({ | ||
// level: node.rule.level, | ||
// message, | ||
// line: node.line, | ||
// col: node.col, | ||
// raw: node.raw, | ||
// }); | ||
var _i, _a, rule, childElementsAndTextOrigin, childElementsAndText, _b, _c, ruleContent, permittedTag, min, max, numPrefix, childElementsAndTextNameList, count, _d, childElementsAndTextNameList_1, childElemName, el, name_1; | ||
return __generator(this, function (_e) { | ||
if (!node.rule.value) { | ||
return [2 /*return*/]; | ||
} | ||
// console.log(node.rule.option); | ||
for (_i = 0, _a = node.rule.option; _i < _a.length; _i++) { | ||
rule = _a[_i]; | ||
if (rule.tag.toLowerCase() !== node.nodeName.toLowerCase()) { | ||
continue; | ||
} | ||
childElementsAndTextOrigin = Object.freeze(node.getChildElementsAndTextNodeWithoutWhitespaces()); | ||
childElementsAndText = __spreadArrays(childElementsAndTextOrigin); | ||
for (_b = 0, _c = rule.contents; _b < _c.length; _b++) { | ||
ruleContent = _c[_b]; | ||
permittedTag = null; | ||
min = 0; | ||
max = Infinity; | ||
numPrefix = ''; | ||
if (isRequiredContents(ruleContent)) { | ||
permittedTag = ruleContent.require; | ||
min = ruleContent.min || 1; | ||
max = ruleContent.max || 1; | ||
} | ||
else if (isOptionalContents(ruleContent)) { | ||
permittedTag = ruleContent.optional; | ||
min = 0; | ||
max = ruleContent.max || 1; | ||
} | ||
else if (isOneOrMoreContents(ruleContent)) { | ||
permittedTag = ruleContent.oneOrMore; | ||
min = 1; | ||
max = ruleContent.max || Infinity; | ||
numPrefix = 'one or more '; | ||
} | ||
else if (isZeroOrMoreContents(ruleContent)) { | ||
permittedTag = ruleContent.zeroOrMore; | ||
min = 0; | ||
max = ruleContent.max || Infinity; | ||
} | ||
if (!permittedTag) { | ||
continue; | ||
} | ||
childElementsAndTextNameList = childElementsAndText.map(function (node) { | ||
return node.type === 'Element' ? node.nodeName.toLowerCase() : '#text'; | ||
}); | ||
count = 0; | ||
for (_d = 0, childElementsAndTextNameList_1 = childElementsAndTextNameList; _d < childElementsAndTextNameList_1.length; _d++) { | ||
childElemName = childElementsAndTextNameList_1[_d]; | ||
if (childElemName === permittedTag) { | ||
count++; | ||
continue; | ||
} | ||
break; | ||
} | ||
if (count < min) { | ||
reports.push({ | ||
severity: node.rule.severity, | ||
message: messages("Invalid content, Require " + numPrefix + "\"" + permittedTag + "\" element"), | ||
line: node.startLine, | ||
col: node.startCol, | ||
raw: node.raw, | ||
}); | ||
continue; | ||
} | ||
if (count > max) { | ||
reports.push({ | ||
severity: node.rule.severity, | ||
message: messages("Invalid content, The maximum length of \"" + permittedTag + "\" elements is " + max), | ||
line: node.startLine, | ||
col: node.startCol, | ||
raw: node.raw, | ||
}); | ||
} | ||
childElementsAndText.splice(0, count); | ||
// if (childElementsAndText.length < min) { | ||
// reports.push({ | ||
// severity: node.rule.severity, | ||
// message: messages(`Invalid content, Require ${numPrefix}"${permittedTag}" element`), | ||
// line: node.startLine, | ||
// col: node.startCol, | ||
// raw: node.raw, | ||
// }); | ||
// } | ||
// if (permittedTag) { | ||
// for (const child of childElementsAndText) { | ||
// if (child.type === 'Element' && child.nodeName.toLowerCase() !== permittedTag) { | ||
// reports.push({ | ||
// severity: node.rule.severity, | ||
// message: messages( | ||
// `Invalid contents, "${node.nodeName}" is unpermitted to contain "${child.nodeName}"`, | ||
// ), | ||
// line: node.startLine, | ||
// col: node.startCol, | ||
// raw: node.raw, | ||
// }); | ||
// } | ||
// } | ||
// } | ||
// if (childElementsAndText.length > max) { | ||
// reports.push({ | ||
// severity: node.rule.severity, | ||
// message: messages(`Invalid content, Require ${numPrefix}"${permittedTag}" element`), | ||
// line: node.startLine, | ||
// col: node.startCol, | ||
// raw: node.raw, | ||
// }); | ||
// } | ||
} | ||
if (childElementsAndText[0]) { | ||
console.log(childElementsAndText); | ||
el = childElementsAndText[0]; | ||
name_1 = el.type === 'Element' ? el.nodeName : 'TextNode'; | ||
reports.push({ | ||
severity: node.rule.severity, | ||
message: messages("Invalid contents, \"" + node.nodeName + "\" is unpermitted to contain \"" + name_1 + "\""), | ||
line: el.startLine, | ||
col: el.startCol, | ||
raw: el.raw, | ||
}); | ||
} | ||
} | ||
return [2 /*return*/]; | ||
@@ -78,1 +195,16 @@ }); | ||
}); | ||
function isRequiredContents(contents) { | ||
return 'require' in contents; | ||
} | ||
function isOptionalContents(contents) { | ||
return 'optional' in contents; | ||
} | ||
function isOneOrMoreContents(contents) { | ||
return 'oneOrMore' in contents; | ||
} | ||
function isZeroOrMoreContents(contents) { | ||
return 'zeroOrMore' in contents; | ||
} | ||
function isChoiceContents(contents) { | ||
return 'choice' in contents; | ||
} |
{ | ||
"name": "@markuplint/rules", | ||
"version": "1.0.0-alpha.10", | ||
"version": "1.0.0-alpha.11", | ||
"description": "Rules for markuplint", | ||
@@ -23,5 +23,5 @@ "repository": "git@github.com:markuplint/markuplint.git", | ||
"devDependencies": { | ||
"@markuplint/ml-core": "^1.0.0-alpha.9" | ||
"@markuplint/ml-core": "^1.0.0-alpha.10" | ||
}, | ||
"gitHead": "9e77270cab9fedc1ee16dbaa59ffbe6e65fb6a3e" | ||
"gitHead": "c641faacf69229f2ace793454a6ebd615b8249ff" | ||
} |
Sorry, the diff of this file is not supported yet
447761
4701