@markuplint/html-parser 3.3.0 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,17 +5,18 @@ const reAttrsInStartTag =
5
5
  // eslint-disable-next-line no-control-regex
6
6
  /(\s*)([^\x00-\x1f\x7f-\x9f "'>/=]+)(?:(\s*)(=)(\s*)(?:(?:"([^"]*)")|(?:'([^']*)')|([^\s]*)))?/;
7
7
  function attrTokenizer(raw, line, col, startOffset) {
8
+ var _a, _b, _c, _d, _e, _f, _g;
8
9
  const attrMatchedMap = raw.match(reAttrsInStartTag);
9
10
  if (!attrMatchedMap) {
10
11
  throw new SyntaxError('Illegal attribute token');
11
12
  }
12
- const spacesBeforeAttrString = attrMatchedMap[1];
13
- const nameChars = attrMatchedMap[2];
14
- const spacesBeforeEqualChars = attrMatchedMap[3] || '';
13
+ const spacesBeforeAttrString = (_a = attrMatchedMap[1]) !== null && _a !== void 0 ? _a : '';
14
+ const nameChars = (_b = attrMatchedMap[2]) !== null && _b !== void 0 ? _b : '';
15
+ const spacesBeforeEqualChars = (_c = attrMatchedMap[3]) !== null && _c !== void 0 ? _c : '';
15
16
  const equalChars = attrMatchedMap[4] || null;
16
- const spacesAfterEqualChars = attrMatchedMap[5] || '';
17
+ const spacesAfterEqualChars = (_d = attrMatchedMap[5]) !== null && _d !== void 0 ? _d : '';
17
18
  const quoteChars = attrMatchedMap[6] != null ? '"' : attrMatchedMap[7] != null ? "'" : null;
18
- const valueChars = attrMatchedMap[6] || attrMatchedMap[7] || attrMatchedMap[8] || (quoteChars ? '' : null);
19
+ const valueChars = (_g = (_f = (_e = attrMatchedMap[6]) !== null && _e !== void 0 ? _e : attrMatchedMap[7]) !== null && _f !== void 0 ? _f : attrMatchedMap[8]) !== null && _g !== void 0 ? _g : (quoteChars ? '' : null);
19
20
  let offset = startOffset;
20
21
  const spacesBeforeName = (0, parser_utils_1.tokenizer)(spacesBeforeAttrString, line, col, offset);
21
22
  line = spacesBeforeName.endLine;
@@ -1,2 +1,8 @@
1
1
  import type { MLASTNode } from '@markuplint/ml-ast';
2
- export declare function createTree(rawCode: string, isFragment: boolean, offsetOffset: number, offsetLine: number, offsetColumn: number): MLASTNode[];
2
+ export declare function createTree(
3
+ rawCode: string,
4
+ isFragment: boolean,
5
+ offsetOffset: number,
6
+ offsetLine: number,
7
+ offsetColumn: number,
8
+ ): MLASTNode[];
@@ -5,7 +5,10 @@ const tslib_1 = require("tslib");
5
5
  const parser_utils_1 = require("@markuplint/parser-utils");
6
6
  const parse5_1 = require("parse5");
7
7
  const parse_raw_tag_1 = tslib_1.__importDefault(require("./parse-raw-tag"));
8
- const P5_OPTIONS = { sourceCodeLocationInfo: true };
8
+ const P5_OPTIONS = {
9
+ scriptingEnabled: false,
10
+ sourceCodeLocationInfo: true,
11
+ };
9
12
  function createTree(rawCode, isFragment, offsetOffset, offsetLine, offsetColumn) {
10
13
  const doc = isFragment
11
14
  ? (0, parse5_1.parseFragment)(rawCode, P5_OPTIONS)
@@ -231,28 +234,8 @@ function nodeize(originNode, prevNode, parentNode, rawHtml, offsetOffset, offset
231
234
  * getChildNodes
232
235
  *
233
236
  * - If node has "content" property then parse as document fragment.
234
- * - If node is <noscript> then that childNodes is a TextNode. But parse as document fragment it for disabled script.
235
237
  */
236
238
  function getChildNodes(rootNode) {
237
- if (rootNode.nodeName === 'noscript') {
238
- const textNode = rootNode.childNodes[0];
239
- if (!textNode || textNode.nodeName !== '#text') {
240
- return [];
241
- }
242
- // @ts-ignore
243
- const html = textNode.value;
244
- // @ts-ignore
245
- const { startOffset, startLine, startCol } = textNode.sourceCodeLocation;
246
- const breakCount = startLine - 1;
247
- const indentWidth = startCol - 1;
248
- const offsetSpaces = ' '.repeat(startOffset - Math.max(breakCount, 0) - Math.max(indentWidth, 0)) +
249
- '\n'.repeat(breakCount) +
250
- ' '.repeat(indentWidth);
251
- const fragment = (0, parse5_1.parseFragment)(`${offsetSpaces}${html}`, P5_OPTIONS);
252
- const childNodes = fragment.childNodes.slice(offsetSpaces ? 1 : 0);
253
- // const childNodes = ('childNodes' in _childNodes && _childNodes.childNodes) || [];
254
- return childNodes;
255
- }
256
239
  return rootNode.content ? rootNode.content.childNodes : rootNode.childNodes || [];
257
240
  }
258
241
  function hasLocation(node) {
@@ -26,7 +26,7 @@ function flattenNodes(nodeTree, rawHtml, createLastText = true) {
26
26
  const prevWreckagesText = prevToken;
27
27
  if (prevWreckagesText) {
28
28
  const wreckages = (0, tag_splitter_1.default)(prevWreckagesText.raw, prevWreckagesText.startLine, prevWreckagesText.startCol);
29
- if (wreckages.length) {
29
+ if (wreckages.length && wreckages[0]) {
30
30
  // console.log('wreckages\n', wreckages);
31
31
  const lastText = wreckages[0];
32
32
  const raw = lastText.raw;
@@ -27,7 +27,7 @@ function getNamespace(tagName, parentNamespace = DEFAULT_NAMESPACE) {
27
27
  const doc = (0, parse5_1.parse)(tag);
28
28
  node = doc.childNodes[0];
29
29
  }
30
- if ('namespaceURI' in node) {
30
+ if (node && 'namespaceURI' in node) {
31
31
  return node.namespaceURI;
32
32
  }
33
33
  return DEFAULT_NAMESPACE;
@@ -1,8 +1,11 @@
1
1
  import type { MLASTNode } from '@markuplint/ml-ast';
2
2
  export declare function isStartsHeadTagOrBodyTag(rawCode: string): boolean;
3
3
  export declare function optimizeStartsHeadTagOrBodyTagSetup(rawCode: string): {
4
- code: string;
5
- heads: string[];
6
- bodies: string[];
4
+ code: string;
5
+ heads: string[];
6
+ bodies: string[];
7
7
  };
8
- export declare function optimizeStartsHeadTagOrBodyTagResume(nodeList: MLASTNode[], replacements: ReturnType<typeof optimizeStartsHeadTagOrBodyTagSetup>): void;
8
+ export declare function optimizeStartsHeadTagOrBodyTagResume(
9
+ nodeList: MLASTNode[],
10
+ replacements: ReturnType<typeof optimizeStartsHeadTagOrBodyTagSetup>,
11
+ ): void;
@@ -1,9 +1,17 @@
1
1
  import type { MLASTAttr, MLToken } from '@markuplint/ml-ast';
2
2
  type TagTokens = {
3
- tagName: string;
4
- attrs: MLASTAttr[];
5
- selfClosingSolidus: MLToken;
6
- endSpace: MLToken;
3
+ tagName: string;
4
+ attrs: MLASTAttr[];
5
+ selfClosingSolidus: MLToken;
6
+ endSpace: MLToken;
7
7
  };
8
- export default function parseRawTag(raw: string, startLine: number, startCol: number, startOffset: number, offsetOffset?: number, offsetLine?: number, offsetColumn?: number): TagTokens;
8
+ export default function parseRawTag(
9
+ raw: string,
10
+ startLine: number,
11
+ startCol: number,
12
+ startOffset: number,
13
+ offsetOffset?: number,
14
+ offsetLine?: number,
15
+ offsetColumn?: number,
16
+ ): TagTokens;
9
17
  export {};
@@ -8,14 +8,15 @@ const const_1 = require("./const");
8
8
  const reAttrsInStartTag = /\s*[^\x00-\x1f\x7f-\x9f "'>/=]+(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^\s]*))?/;
9
9
  const reEndTokens = /(\s*\/)?(\s*)>$/;
10
10
  function parseRawTag(raw, startLine, startCol, startOffset, offsetOffset = 0, offsetLine = 0, offsetColumn = 0) {
11
+ var _a, _b;
11
12
  let offset = startOffset + offsetOffset;
12
13
  let line = startLine + offsetLine;
13
14
  let col = startCol + (startLine === 1 ? offsetColumn : 0);
14
15
  const matches = raw.match(const_1.reTag);
15
- if (!matches) {
16
+ const tagWithAttrs = matches === null || matches === void 0 ? void 0 : matches[1];
17
+ if (!tagWithAttrs) {
16
18
  throw new SyntaxError(`Invalid tag syntax: "${raw}"`);
17
19
  }
18
- const tagWithAttrs = matches[1];
19
20
  // eslint-disable-next-line no-control-regex
20
21
  const tagNameSplitted = tagWithAttrs.split(/[\u0000\u0009\u000A\u000C\u0020/>]/);
21
22
  const tagName = tagNameSplitted[0] || tagNameSplitted[1];
@@ -41,11 +42,11 @@ function parseRawTag(raw, startLine, startCol, startOffset, offsetOffset = 0, of
41
42
  }
42
43
  }
43
44
  const endTokens = reEndTokens.exec(raw);
44
- const selfClosingSolidus = (0, parser_utils_1.tokenizer)(endTokens && endTokens[1], line, col, offset);
45
+ const selfClosingSolidus = (0, parser_utils_1.tokenizer)((_a = endTokens === null || endTokens === void 0 ? void 0 : endTokens[1]) !== null && _a !== void 0 ? _a : '', line, col, offset);
45
46
  line = selfClosingSolidus.endLine;
46
47
  col = selfClosingSolidus.endCol;
47
48
  offset = selfClosingSolidus.endOffset;
48
- const endSpace = (0, parser_utils_1.tokenizer)(endTokens && endTokens[2], line, col, offset);
49
+ const endSpace = (0, parser_utils_1.tokenizer)((_b = endTokens === null || endTokens === void 0 ? void 0 : endTokens[2]) !== null && _b !== void 0 ? _b : '', line, col, offset);
49
50
  return {
50
51
  tagName,
51
52
  attrs,
@@ -1,7 +1,7 @@
1
1
  export interface N {
2
- type: 'text' | 'starttag' | 'endtag' | 'comment' | 'boguscomment';
3
- raw: string;
4
- line: number;
5
- col: number;
2
+ type: 'text' | 'starttag' | 'endtag' | 'comment' | 'boguscomment';
3
+ raw: string;
4
+ line: number;
5
+ col: number;
6
6
  }
7
7
  export default function tagSplitter(raw: string, line: number, col: number): N[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markuplint/html-parser",
3
- "version": "3.3.0",
3
+ "version": "3.4.0",
4
4
  "description": "HTML parser for markuplint",
5
5
  "repository": "git@github.com:markuplint/markuplint.git",
6
6
  "author": "Yusuke Hirao <yusukehirao@me.com>",
@@ -21,9 +21,9 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "@markuplint/ml-ast": "3.0.0",
24
- "@markuplint/parser-utils": "3.3.0",
24
+ "@markuplint/parser-utils": "3.4.0",
25
25
  "parse5": "7.1.2",
26
26
  "tslib": "^2.4.1"
27
27
  },
28
- "gitHead": "791fb22a4df7acb985ced3808923fba0cd95c28a"
28
+ "gitHead": "a83e0f5f214a9bbcc0286b9e269074ddca6189e7"
29
29
  }