securemark 0.298.1 → 0.298.2

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.298.2
4
+
5
+ - Refactoring.
6
+
3
7
  ## 0.298.1
4
8
 
5
9
  - Refactoring.
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! securemark v0.298.1 https://github.com/falsandtru/securemark | (c) 2017, falsandtru | UNLICENSED License */
1
+ /*! securemark v0.298.2 https://github.com/falsandtru/securemark | (c) 2017, falsandtru | UNLICENSED License */
2
2
  (function webpackUniversalModuleDefinition(root, factory) {
3
3
  if(typeof exports === 'object' && typeof module === 'object')
4
4
  module.exports = factory(require("Prism"), require("DOMPurify"));
@@ -6312,7 +6312,6 @@ exports.annotation = (0, combinator_1.lazy)(() => (0, combinator_1.constraint)(1
6312
6312
  const {
6313
6313
  source,
6314
6314
  position,
6315
- range,
6316
6315
  linebreak,
6317
6316
  recursion,
6318
6317
  resources
@@ -6360,12 +6359,10 @@ exports.annotation = (0, combinator_1.lazy)(() => (0, combinator_1.constraint)(1
6360
6359
  if (source[context.position] === ')') {
6361
6360
  bs.push(new parser_1.Node(')'));
6362
6361
  context.position += 1;
6362
+ context.range += 1;
6363
6363
  }
6364
- const str = linebreak === 0 ? source.slice(position - range + 2, position) : '';
6365
- bs = linebreak === 0 && (str === '' || bracket_1.indexA.test(str)) ? new parser_1.List([new parser_1.Node((0, dom_1.html)('span', {
6366
- class: 'paren'
6367
- }, (0, dom_1.defrag)((0, util_1.unwrap)(bs))))]) : new parser_1.List([new parser_1.Node((0, dom_1.html)('span', {
6368
- class: 'bracket'
6364
+ bs = new parser_1.List([new parser_1.Node((0, dom_1.html)('span', {
6365
+ class: (0, bracket_1.bracketname)(context, bracket_1.indexA, 2, context.position - position)
6369
6366
  }, (0, dom_1.defrag)((0, util_1.unwrap)(bs))))]);
6370
6367
  bs.unshift(new parser_1.Node('('));
6371
6368
  const cs = parser(context);
@@ -6662,7 +6659,7 @@ const bracket = (0, combinator_1.lazy)(() => (0, combinator_1.union)([(0, combin
6662
6659
  Object.defineProperty(exports, "__esModule", ({
6663
6660
  value: true
6664
6661
  }));
6665
- exports.bracket = exports.indexA = void 0;
6662
+ exports.bracket = exports.bracketname = exports.indexA = void 0;
6666
6663
  const parser_1 = __webpack_require__(605);
6667
6664
  const combinator_1 = __webpack_require__(3484);
6668
6665
  const inline_1 = __webpack_require__(7973);
@@ -6670,8 +6667,19 @@ const link_1 = __webpack_require__(3628);
6670
6667
  const source_1 = __webpack_require__(8745);
6671
6668
  const util_1 = __webpack_require__(4992);
6672
6669
  const dom_1 = __webpack_require__(394);
6673
- exports.indexA = /^[0-9A-Za-z]+(?:(?:[.-]|, )[0-9A-Za-z]+)*$/;
6674
- const indexF = new RegExp(exports.indexA.source.replace(', ', '[,、]').replace(/[09AZaz.]|\-(?!\w)/g, c => String.fromCodePoint(c.codePointAt(0) + 0xFEE0)));
6670
+ exports.indexA = /[0-9A-Za-z]+(?:(?:[.-]|, )[0-9A-Za-z]+)*/y;
6671
+ const indexF = new RegExp(exports.indexA.source.replace(', ', '[,、]').replace(/[09AZaz.]|\-(?!\w)/g, c => String.fromCodePoint(c.codePointAt(0) + 0xFEE0)), 'y');
6672
+ function bracketname(context, syntax, opener, closer) {
6673
+ const {
6674
+ source,
6675
+ position,
6676
+ range,
6677
+ linebreak
6678
+ } = context;
6679
+ syntax.lastIndex = position - range + opener;
6680
+ return range - opener - closer === 0 || linebreak === 0 && range - opener - closer <= 16 && syntax.test(source) && syntax.lastIndex === position - closer ? 'paren' : 'bracket';
6681
+ }
6682
+ exports.bracketname = bracketname;
6675
6683
  exports.bracket = (0, combinator_1.lazy)(() => (0, combinator_1.union)([input => {
6676
6684
  const {
6677
6685
  source,
@@ -6694,58 +6702,16 @@ exports.bracket = (0, combinator_1.lazy)(() => (0, combinator_1.union)([input =>
6694
6702
  return d1(input);
6695
6703
  }
6696
6704
  }]));
6697
- const p1 = (0, combinator_1.lazy)(() => (0, combinator_1.surround)((0, source_1.str)('('), (0, combinator_1.precedence)(1, (0, combinator_1.recursion)(5 /* Recursion.bracket */, (0, combinator_1.some)(inline_1.inline, ')', [[')', 1]]))), (0, source_1.str)(')'), true, [], ([as, bs = [], cs], {
6698
- source,
6699
- position,
6700
- range,
6701
- linebreak
6702
- }) => {
6703
- const str = linebreak === 0 ? source.slice(position - range + 1, position - 1) : '';
6704
- return linebreak === 0 && (str === '' || exports.indexA.test(str)) ? new parser_1.List([new parser_1.Node((0, dom_1.html)('span', {
6705
- class: 'paren'
6706
- }, `(${str})`))]) : new parser_1.List([new parser_1.Node((0, dom_1.html)('span', {
6707
- class: 'bracket'
6708
- }, (0, dom_1.defrag)((0, util_1.unwrap)(as.import(bs).import(cs)))))]);
6709
- }, ([as, bs = new parser_1.List()], context) => {
6710
- const {
6711
- source,
6712
- position,
6713
- range,
6714
- linebreak
6715
- } = context;
6716
- const str = linebreak === 0 ? source.slice(position - range + 1, position) : '';
6717
- return linebreak === 0 && (str === '' || exports.indexA.test(str)) ? new parser_1.List([new parser_1.Node((0, dom_1.html)('span', {
6718
- class: 'paren'
6719
- }, `(${str}`))]) : new parser_1.List([new parser_1.Node((0, dom_1.html)('span', {
6720
- class: 'bracket'
6721
- }, (0, dom_1.defrag)((0, util_1.unwrap)(as.import(bs)))))]);
6722
- }));
6723
- const p2 = (0, combinator_1.lazy)(() => (0, combinator_1.surround)((0, source_1.str)('('), (0, combinator_1.precedence)(1, (0, combinator_1.recursion)(5 /* Recursion.bracket */, (0, combinator_1.some)(inline_1.inline, ')', [[')', 1]]))), (0, source_1.str)(')'), true, [], ([as, bs = [], cs], {
6724
- source,
6725
- position,
6726
- range,
6727
- linebreak
6728
- }) => {
6729
- const str = linebreak === 0 ? source.slice(position - range + 1, position - 1) : '';
6730
- return linebreak === 0 && (str === '' || indexF.test(str)) ? new parser_1.List([new parser_1.Node((0, dom_1.html)('span', {
6731
- class: 'paren'
6732
- }, `(${str})`))]) : new parser_1.List([new parser_1.Node((0, dom_1.html)('span', {
6733
- class: 'bracket'
6734
- }, (0, dom_1.defrag)((0, util_1.unwrap)(as.import(bs).import(cs)))))]);
6735
- }, ([as, bs = new parser_1.List()], context) => {
6736
- const {
6737
- source,
6738
- position,
6739
- range,
6740
- linebreak
6741
- } = context;
6742
- const str = linebreak === 0 ? source.slice(position - range + 1, position) : '';
6743
- return linebreak === 0 && (str === '' || indexF.test(str)) ? new parser_1.List([new parser_1.Node((0, dom_1.html)('span', {
6744
- class: 'paren'
6745
- }, `(${str}`))]) : new parser_1.List([new parser_1.Node((0, dom_1.html)('span', {
6746
- class: 'bracket'
6747
- }, (0, dom_1.defrag)((0, util_1.unwrap)(as.import(bs)))))]);
6748
- }));
6705
+ const p1 = (0, combinator_1.lazy)(() => (0, combinator_1.surround)((0, source_1.str)('('), (0, combinator_1.precedence)(1, (0, combinator_1.recursion)(5 /* Recursion.bracket */, (0, combinator_1.some)(inline_1.inline, ')', [[')', 1]]))), (0, source_1.str)(')'), true, [], ([as, bs = new parser_1.List(), cs], context) => new parser_1.List([new parser_1.Node((0, dom_1.html)('span', {
6706
+ class: bracketname(context, exports.indexA, 1, 1)
6707
+ }, (0, dom_1.defrag)((0, util_1.unwrap)(as.import(bs).import(cs)))))]), ([as, bs = new parser_1.List()], context) => new parser_1.List([new parser_1.Node((0, dom_1.html)('span', {
6708
+ class: bracketname(context, exports.indexA, 1, 0)
6709
+ }, (0, dom_1.defrag)((0, util_1.unwrap)(as.import(bs)))))])));
6710
+ const p2 = (0, combinator_1.lazy)(() => (0, combinator_1.surround)((0, source_1.str)('('), (0, combinator_1.precedence)(1, (0, combinator_1.recursion)(5 /* Recursion.bracket */, (0, combinator_1.some)(inline_1.inline, ')', [[')', 1]]))), (0, source_1.str)(')'), true, [], ([as, bs = new parser_1.List(), cs], context) => new parser_1.List([new parser_1.Node((0, dom_1.html)('span', {
6711
+ class: bracketname(context, indexF, 1, 1)
6712
+ }, (0, dom_1.defrag)((0, util_1.unwrap)(as.import(bs).import(cs)))))]), ([as, bs = new parser_1.List()], context) => new parser_1.List([new parser_1.Node((0, dom_1.html)('span', {
6713
+ class: bracketname(context, indexF, 1, 0)
6714
+ }, (0, dom_1.defrag)((0, util_1.unwrap)(as.import(bs)))))])));
6749
6715
  const s1 = (0, combinator_1.lazy)(() => (0, combinator_1.surround)((0, source_1.str)('['), (0, combinator_1.precedence)(1, (0, combinator_1.recursion)(5 /* Recursion.bracket */, (0, combinator_1.some)(inline_1.inline, ']', [[']', 1]]))), (0, source_1.str)(']'), true, [2 | 4 /* Backtrack.common */], ([as, bs = new parser_1.List(), cs], context) => {
6750
6716
  const {
6751
6717
  source,
@@ -8147,17 +8113,16 @@ const array_1 = __webpack_require__(6876);
8147
8113
  const dom_1 = __webpack_require__(394);
8148
8114
  const query_1 = __webpack_require__(2282);
8149
8115
  function* figure(target, notes, opts = {}) {
8116
+ const selector = ':is(figure[data-label], h1, h2)';
8150
8117
  const refs = new queue_1.MultiQueue((0, array_1.push)((0, query_1.querySelectorAll)(target, 'a.label:not(.local)[data-label]'), notes && (0, query_1.querySelectorAll)(notes.references, 'a.label:not(.local)') || []).map(el => [el.getAttribute('data-label'), el]));
8151
8118
  const labels = new Set();
8152
8119
  const numbers = new Map();
8153
- const scope = target instanceof Element ? ':scope > ' : '';
8154
8120
  let base = '0';
8155
8121
  let bases = base.split('.');
8156
8122
  let index = bases;
8157
- for (let defs = target.querySelectorAll(`${scope}:is(figure[data-label], h1, h2)`), len = defs.length, i = 0; i < len; ++i) {
8123
+ for (let defs = target instanceof Element ? target.querySelectorAll(`:scope > ${selector}`) : target.querySelectorAll(`${selector}:not(* > *)`), len = defs.length, i = 0; i < len; ++i) {
8158
8124
  yield;
8159
8125
  const def = defs[i];
8160
- if (!scope && def.parentNode !== target) continue;
8161
8126
  const {
8162
8127
  tagName
8163
8128
  } = def;
@@ -8298,9 +8263,9 @@ function* note(target, notes, opts = {}, bottom = null) {
8298
8263
  exports.note = note;
8299
8264
  const annotationRefsMemoryCaller = (0, memoize_1.memoize)(target => new Map() ?? target, new WeakMap());
8300
8265
  const referenceRefsMemoryCaller = (0, memoize_1.memoize)(target => new Map() ?? target, new WeakMap());
8301
- const annotation = build('annotation', 'annotations', '.annotation:not(:is(.annotations, .references) .annotation, .local)', n => `*${n}`, 'h1, h2, h3, h4, h5, h6, aside.aside, hr');
8302
- const reference = build('reference', 'references', '.reference:not(:is(.annotations, .references) .reference, .local)', (n, abbr) => `[${abbr || n}]`);
8303
- function build(syntax, list, query, marker, splitter = '') {
8266
+ const annotation = build('annotation', 'annotations', '.annotation:not(:is(.annotations, .references) &, .local)', n => `*${n}`, 'h1, h2, h3, h4, h5, h6, aside.aside, hr');
8267
+ const reference = build('reference', 'references', '.reference:not(:is(.annotations, .references) &, .local)', (n, abbr) => `[${abbr || n}]`);
8268
+ function build(syntax, list, selector, marker, splitter = '') {
8304
8269
  splitter &&= `${splitter}, .${list}`;
8305
8270
  return function* (memory, target, note, opts = {}, bottom = null) {
8306
8271
  const refInfoCaller = (0, memoize_1.memoize)(ref => {
@@ -8317,7 +8282,7 @@ function build(syntax, list, query, marker, splitter = '') {
8317
8282
  };
8318
8283
  }, memory);
8319
8284
  const defs = new Map();
8320
- const refs = target.querySelectorAll(query);
8285
+ const refs = target.querySelectorAll(selector);
8321
8286
  const identifierInfoCaller = (0, memoize_1.memoize)(identifier => ({
8322
8287
  defIndex: 0,
8323
8288
  defSubindex: 0,
@@ -8325,8 +8290,7 @@ function build(syntax, list, query, marker, splitter = '') {
8325
8290
  title: '' && 0,
8326
8291
  queue: []
8327
8292
  }));
8328
- const scope = target instanceof Element ? ':scope > ' : '';
8329
- const splitters = splitter ? target.querySelectorAll(`${scope}:is(${splitter})`) : [];
8293
+ const splitters = splitter ? target instanceof Element ? target.querySelectorAll(`:scope > :is(${splitter}, .${list})`) : target.querySelectorAll(`:is(${splitter}, .${list}):not(* > *)`) : [];
8330
8294
  let iSplitters = 0;
8331
8295
  let total = 0;
8332
8296
  let format;
@@ -8337,7 +8301,8 @@ function build(syntax, list, query, marker, splitter = '') {
8337
8301
  const pos = splitter?.compareDocumentPosition(ref) ?? 0;
8338
8302
  if (pos & (Node.DOCUMENT_POSITION_PRECEDING | Node.DOCUMENT_POSITION_DISCONNECTED)) break;
8339
8303
  if (~iSplitters << 32 - 8 === 0) yield;
8340
- if (splitter.classList.contains(list) && defs.size === 0) {
8304
+ if (splitter.classList.contains(list) && splitter.nextElementSibling !== splitters[iSplitters + 1]) {
8305
+ yield* proc(splitter);
8341
8306
  splitter.remove();
8342
8307
  continue;
8343
8308
  }
@@ -8346,7 +8311,7 @@ function build(syntax, list, query, marker, splitter = '') {
8346
8311
  const note = splitter.classList.contains(list) ? splitter : target.insertBefore((0, dom_1.html)('ol', {
8347
8312
  class: list
8348
8313
  }), splitter);
8349
- yield* proc(defs, note);
8314
+ yield* proc(note, defs);
8350
8315
  }
8351
8316
  }
8352
8317
  const {
@@ -8412,25 +8377,25 @@ function build(syntax, list, query, marker, splitter = '') {
8412
8377
  }
8413
8378
  if (note || defs.size > 0) {
8414
8379
  const splitter = splitters[iSplitters++];
8415
- yield* proc(defs, note ?? (splitter?.classList.contains(list) ? splitter : target.insertBefore((0, dom_1.html)('ol', {
8380
+ note ??= splitter?.classList.contains(list) ? splitter : target.insertBefore((0, dom_1.html)('ol', {
8416
8381
  class: list
8417
- }), splitter ?? bottom)));
8382
+ }), splitter ?? bottom);
8383
+ yield* proc(note, defs);
8418
8384
  }
8419
8385
  if (splitter) for (let splitter; splitter = splitters[iSplitters]; ++iSplitters) {
8420
8386
  if (~iSplitters << 32 - 8 === 0) yield;
8421
8387
  if (splitter.classList.contains(list)) {
8388
+ yield* proc(splitter);
8422
8389
  splitter.remove();
8423
8390
  }
8424
8391
  }
8425
8392
  };
8426
8393
  }
8427
- function* proc(defs, note) {
8428
- const {
8429
- children
8430
- } = note;
8394
+ function* proc(note, defs) {
8431
8395
  for (let defs = note.children, i = defs.length; i--;) {
8432
- yield note.removeChild(children[i]);
8396
+ yield note.removeChild(defs[i]);
8433
8397
  }
8398
+ if (!defs) return;
8434
8399
  for (const [, def] of defs) {
8435
8400
  yield note.appendChild(def);
8436
8401
  }
package/markdown.d.ts CHANGED
@@ -16,10 +16,12 @@ export namespace MarkdownParser {
16
16
  Markdown<'segment'>,
17
17
  Parser<string, Context, [
18
18
  SourceParser.EmptySegmentParser,
19
- BlockParser.HeadingParser.SegmentParser,
20
- BlockParser.CodeBlockParser.SegmentParser,
21
- BlockParser.MathBlockParser.SegmentParser,
22
- BlockParser.ExtensionParser.SegmentParser,
19
+ Parser<string, Context, [
20
+ BlockParser.HeadingParser.SegmentParser,
21
+ BlockParser.CodeBlockParser.SegmentParser,
22
+ BlockParser.MathBlockParser.SegmentParser,
23
+ BlockParser.ExtensionParser.SegmentParser,
24
+ ]>,
23
25
  SourceParser.ContentLineParser,
24
26
  ]> {
25
27
  }
@@ -48,20 +50,22 @@ export namespace MarkdownParser {
48
50
  Markdown<'block'>,
49
51
  Parser<HTMLElement, Context, [
50
52
  SourceParser.EmptySegmentParser,
51
- BlockParser.PagebreakParser,
52
- BlockParser.HeadingParser,
53
- BlockParser.UListParser,
54
- BlockParser.OListParser,
55
- BlockParser.IListParser,
56
- BlockParser.DListParser,
57
- BlockParser.TableParser,
58
- BlockParser.CodeBlockParser,
59
- BlockParser.MathBlockParser,
60
- BlockParser.ExtensionParser,
61
- BlockParser.SidefenceParser,
62
- BlockParser.BlockquoteParser,
63
- BlockParser.MediaBlockParser,
64
- BlockParser.ReplyParser,
53
+ Parser<HTMLElement, Context, [
54
+ BlockParser.PagebreakParser,
55
+ BlockParser.HeadingParser,
56
+ BlockParser.UListParser,
57
+ BlockParser.OListParser,
58
+ BlockParser.IListParser,
59
+ BlockParser.DListParser,
60
+ BlockParser.TableParser,
61
+ BlockParser.CodeBlockParser,
62
+ BlockParser.MathBlockParser,
63
+ BlockParser.ExtensionParser,
64
+ BlockParser.SidefenceParser,
65
+ BlockParser.BlockquoteParser,
66
+ BlockParser.MediaBlockParser,
67
+ BlockParser.ReplyParser,
68
+ ]>,
65
69
  BlockParser.ParagraphParser,
66
70
  ]> {
67
71
  }
@@ -614,25 +618,27 @@ export namespace MarkdownParser {
614
618
  export interface InlineParser extends
615
619
  Markdown<'inline'>,
616
620
  Parser<HTMLElement | string, Context, [
617
- InlineParser.AnnotationParser,
618
- InlineParser.ReferenceParser,
619
- InlineParser.TemplateParser,
620
- InlineParser.RemarkParser,
621
- InlineParser.ExtensionParser,
622
- InlineParser.LinkParser.TextLinkParser,
623
- InlineParser.RubyParser,
624
- InlineParser.HTMLParser,
625
- InlineParser.InsertionParser,
626
- InlineParser.DeletionParser,
627
- InlineParser.MarkParser,
628
- InlineParser.EmStrongParser,
629
- InlineParser.StrongParser,
630
- InlineParser.EmphasisParser,
631
- InlineParser.ItalicParser,
632
- InlineParser.MathParser,
633
- InlineParser.CodeParser,
634
- InlineParser.HTMLEntityParser,
635
- InlineParser.BracketParser,
621
+ Parser<HTMLElement | string, Context, [
622
+ InlineParser.AnnotationParser,
623
+ InlineParser.ReferenceParser,
624
+ InlineParser.TemplateParser,
625
+ InlineParser.RemarkParser,
626
+ InlineParser.ExtensionParser,
627
+ InlineParser.LinkParser.TextLinkParser,
628
+ InlineParser.RubyParser,
629
+ InlineParser.HTMLParser,
630
+ InlineParser.InsertionParser,
631
+ InlineParser.DeletionParser,
632
+ InlineParser.MarkParser,
633
+ InlineParser.EmStrongParser,
634
+ InlineParser.StrongParser,
635
+ InlineParser.EmphasisParser,
636
+ InlineParser.ItalicParser,
637
+ InlineParser.MathParser,
638
+ InlineParser.CodeParser,
639
+ InlineParser.HTMLEntityParser,
640
+ InlineParser.BracketParser,
641
+ ]>,
636
642
  InlineParser.AutolinkParser,
637
643
  SourceParser.TextParser,
638
644
  ]> {
@@ -1056,12 +1062,6 @@ export namespace MarkdownParser {
1056
1062
  Inline<'bracket'>,
1057
1063
  Parser<HTMLElement | string, Context, [
1058
1064
  InlineParser,
1059
- InlineParser,
1060
- InlineParser,
1061
- InlineParser,
1062
- InlineParser,
1063
- InlineParser,
1064
- InlineParser,
1065
1065
  ]> {
1066
1066
  }
1067
1067
  export interface AutolinkParser extends
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "securemark",
3
- "version": "0.298.1",
3
+ "version": "0.298.2",
4
4
  "description": "Secure markdown renderer working on browsers for user input data.",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/falsandtru/securemark",
@@ -121,7 +121,7 @@ export const block: BlockParser = reset(
121
121
  }
122
122
  },
123
123
  paragraph
124
- ]) as any));
124
+ ])));
125
125
 
126
126
  function error(parser: BlockParser): BlockParser {
127
127
  const reg = new RegExp(String.raw`^${Command.Error}[^\n]*\n`)
@@ -3,7 +3,7 @@ import { State, Recursion } from '../context';
3
3
  import { List, Node } from '../../combinator/data/parser';
4
4
  import { union, some, recursions, precedence, constraint, surround, open, lazy } from '../../combinator';
5
5
  import { inline } from '../inline';
6
- import { indexA } from './bracket';
6
+ import { bracketname, indexA } from './bracket';
7
7
  import { beforeNonblank, trimBlankNodeEnd } from '../visibility';
8
8
  import { unwrap } from '../util';
9
9
  import { html, defrag } from 'typed-dom/dom';
@@ -34,14 +34,20 @@ export const annotation: AnnotationParser = lazy(() => constraint(State.annotati
34
34
  if (linebreak !== 0) {
35
35
  ns.unshift(new Node('('));
36
36
  ns.push(new Node(')'));
37
- return new List([new Node(html('span', { class: 'bracket' }, ['(', html('span', { class: 'bracket' }, defrag(unwrap(ns))), ')']))]);
37
+ return new List([
38
+ new Node(html('span',
39
+ { class: 'bracket' },
40
+ ['(', html('span', { class: 'bracket' }, defrag(unwrap(ns))), ')']))
41
+ ]);
38
42
  }
39
43
  const depth = MAX_DEPTH - (resources?.recursions[Recursion.annotation] ?? resources?.recursions.at(-1) ?? MAX_DEPTH);
40
44
  recursion.add(depth);
41
- return new List([new Node(html('sup', { class: 'annotation' }, [html('span', defrag(unwrap(trimBlankNodeEnd(ns))))]))]);
45
+ return new List([
46
+ new Node(html('sup', { class: 'annotation' }, [html('span', defrag(unwrap(trimBlankNodeEnd(ns))))]))
47
+ ]);
42
48
  },
43
49
  ([, bs], context) => {
44
- const { source, position, range, linebreak, recursion, resources } = context;
50
+ const { source, position, linebreak, recursion, resources } = context;
45
51
  const depth = MAX_DEPTH - (resources?.recursions[Recursion.annotation] ?? resources?.recursions.at(-1) ?? MAX_DEPTH);
46
52
  if (linebreak === 0 && bs && bs.length === 1 && source[position] === ')' && typeof bs.head?.value === 'object') {
47
53
  const { className } = bs.head.value;
@@ -63,12 +69,20 @@ export const annotation: AnnotationParser = lazy(() => constraint(State.annotati
63
69
  }
64
70
  context.position += 1;
65
71
  recursion.add(depth);
66
- return new List([new Node(html('span', { class: 'bracket' }, ['(', html('sup', { class: 'annotation' }, [html('span', bs.head.value.childNodes)])]))]);
72
+ return new List([
73
+ new Node(html('span',
74
+ { class: 'bracket' },
75
+ ['(', html('sup', { class: 'annotation' }, [html('span', bs.head.value.childNodes)])]))
76
+ ]);
67
77
  }
68
78
  if (className === 'annotation' && deepunwrap(bs)) {
69
79
  context.position += 1;
70
80
  recursion.add(depth);
71
- return new List([new Node(html('span', { class: 'bracket' }, ['(', html('sup', { class: 'annotation' }, [html('span', [bs.head.value])])]))]);
81
+ return new List([
82
+ new Node(html('span',
83
+ { class: 'bracket' },
84
+ ['(', html('sup', { class: 'annotation' }, [html('span', [bs.head.value])])]))
85
+ ]);
72
86
  }
73
87
  }
74
88
  bs ??= new List();
@@ -76,11 +90,13 @@ export const annotation: AnnotationParser = lazy(() => constraint(State.annotati
76
90
  if (source[context.position] === ')') {
77
91
  bs.push(new Node(')'));
78
92
  context.position += 1;
93
+ context.range += 1;
79
94
  }
80
- const str = linebreak === 0 ? source.slice(position - range + 2, position) : '';
81
- bs = linebreak === 0 && (str === '' || indexA.test(str))
82
- ? new List([new Node(html('span', { class: 'paren' }, defrag(unwrap(bs))))])
83
- : new List([new Node(html('span', { class: 'bracket' }, defrag(unwrap(bs))))]);
95
+ bs = new List([
96
+ new Node(html('span',
97
+ { class: bracketname(context, indexA, 2, context.position - position) },
98
+ defrag(unwrap(bs))))
99
+ ]);
84
100
  bs.unshift(new Node('('));
85
101
  const cs = parser(context);
86
102
  if (source[context.position] === ')') {
@@ -20,9 +20,10 @@ describe('Unit: parser/inline/bracket', () => {
20
20
  assert.deepStrictEqual(inspect(parser, input('(0)-1', new Context())), [['<span class="paren">(0)</span>'], '-1']);
21
21
  assert.deepStrictEqual(inspect(parser, input('(0.1)', new Context())), [['<span class="paren">(0.1)</span>'], '']);
22
22
  assert.deepStrictEqual(inspect(parser, input('(0.1.2)', new Context())), [['<span class="paren">(0.1.2)</span>'], '']);
23
- assert.deepStrictEqual(inspect(parser, input('(1.1, 1.2-1.3, 1.4)', new Context())), [['<span class="paren">(1.1, 1.2-1.3, 1.4)</span>'], '']);
24
- assert.deepStrictEqual(inspect(parser, input('(1 2)', new Context())), [['<span class="bracket">(1 2)</span>'], '']);
25
23
  assert.deepStrictEqual(inspect(parser, input('(1, 2)', new Context())), [['<span class="paren">(1, 2)</span>'], '']);
24
+ assert.deepStrictEqual(inspect(parser, input('(1.1-1.2)', new Context())), [['<span class="paren">(1.1-1.2)</span>'], '']);
25
+ assert.deepStrictEqual(inspect(parser, input('(1.1, 1.2)', new Context())), [['<span class="paren">(1.1, 1.2)</span>'], '']);
26
+ assert.deepStrictEqual(inspect(parser, input('(1 2)', new Context())), [['<span class="bracket">(1 2)</span>'], '']);
26
27
  assert.deepStrictEqual(inspect(parser, input('(1a)', new Context())), [['<span class="paren">(1a)</span>'], '']);
27
28
  assert.deepStrictEqual(inspect(parser, input('(a)', new Context())), [['<span class="paren">(a)</span>'], '']);
28
29
  assert.deepStrictEqual(inspect(parser, input('(a1)', new Context())), [['<span class="paren">(a1)</span>'], '']);
@@ -39,6 +40,8 @@ describe('Unit: parser/inline/bracket', () => {
39
40
  assert.deepStrictEqual(inspect(parser, input('(Name, Name)', new Context())), [['<span class="paren">(Name, Name)</span>'], '']);
40
41
  assert.deepStrictEqual(inspect(parser, input('(ABBR)', new Context())), [['<span class="paren">(ABBR)</span>'], '']);
41
42
  assert.deepStrictEqual(inspect(parser, input('(ABBR, ABBR)', new Context())), [['<span class="paren">(ABBR, ABBR)</span>'], '']);
43
+ assert.deepStrictEqual(inspect(parser, input(`(${'0'.repeat(16)})`, new Context())), [[`<span class="paren">(${'0'.repeat(16)})</span>`], '']);
44
+ assert.deepStrictEqual(inspect(parser, input(`(${'0'.repeat(17)})`, new Context())), [[`<span class="bracket">(${'0'.repeat(17)})</span>`], '']);
42
45
  assert.deepStrictEqual(inspect(parser, input('(\\a)', new Context())), [['<span class="bracket">(a)</span>'], '']);
43
46
  assert.deepStrictEqual(inspect(parser, input('(==)', new Context())), [['<span class="bracket">(==)</span>'], '']);
44
47
  assert.deepStrictEqual(inspect(parser, input('(()', new Context())), [['<span class="bracket">(<span class="paren">()</span></span>'], '']);
@@ -1,5 +1,5 @@
1
1
  import { BracketParser } from '../inline';
2
- import { State, Recursion, Backtrack } from '../context';
2
+ import { Context, State, Recursion, Backtrack } from '../context';
3
3
  import { List, Node } from '../../combinator/data/parser';
4
4
  import { union, some, recursion, precedence, surround, isBacktrack, setBacktrack, lazy } from '../../combinator';
5
5
  import { inline } from '../inline';
@@ -8,9 +8,22 @@ import { str } from '../source';
8
8
  import { unwrap } from '../util';
9
9
  import { html, defrag } from 'typed-dom/dom';
10
10
 
11
- export const indexA = /^[0-9A-Za-z]+(?:(?:[.-]|, )[0-9A-Za-z]+)*$/;
12
- const indexF = new RegExp(indexA.source.replace(', ', '[,、]')
13
- .replace(/[09AZaz.]|\-(?!\w)/g, c => String.fromCodePoint(c.codePointAt(0)! + 0xFEE0)));
11
+ export const indexA = /[0-9A-Za-z]+(?:(?:[.-]|, )[0-9A-Za-z]+)*/y;
12
+ const indexF = new RegExp(
13
+ indexA.source.replace(', ', '[,、]')
14
+ .replace(/[09AZaz.]|\-(?!\w)/g, c => String.fromCodePoint(c.codePointAt(0)! + 0xFEE0)),
15
+ 'y');
16
+ export function bracketname(context: Context, syntax: RegExp, opener: number, closer: number): string {
17
+ const { source, position, range, linebreak } = context;
18
+ syntax.lastIndex = position - range + opener;
19
+ return range - opener - closer === 0
20
+ || linebreak === 0
21
+ && range - opener - closer <= 16
22
+ && syntax.test(source)
23
+ && syntax.lastIndex === position - closer
24
+ ? 'paren'
25
+ : 'bracket';
26
+ }
14
27
 
15
28
  export const bracket: BracketParser = lazy(() => union([
16
29
  input => {
@@ -32,45 +45,39 @@ export const bracket: BracketParser = lazy(() => union([
32
45
  return d1(input);
33
46
  }
34
47
  }
35
- ])) as any;
48
+ ]));
36
49
 
37
50
  const p1 = lazy(() => surround(
38
51
  str('('),
39
52
  precedence(1, recursion(Recursion.bracket, some(inline, ')', [[')', 1]]))),
40
53
  str(')'),
41
54
  true, [],
42
- ([as, bs = [], cs], { source, position, range, linebreak }) => {
43
- const str = linebreak === 0 ? source.slice(position - range + 1, position - 1) : '';
44
- return linebreak === 0 && (str === '' || indexA.test(str))
45
- ? new List([new Node(html('span', { class: 'paren' }, `(${str})`))])
46
- : new List([new Node(html('span', { class: 'bracket' }, defrag(unwrap(as.import(bs as List<Node<string>>).import(cs)))))]);
47
- },
48
- ([as, bs = new List()], context) => {
49
- const { source, position, range, linebreak } = context;
50
- const str = linebreak === 0 ? source.slice(position - range + 1, position) : '';
51
- return linebreak === 0 && (str === '' || indexA.test(str))
52
- ? new List([new Node(html('span', { class: 'paren' }, `(${str}`))])
53
- : new List([new Node(html('span', { class: 'bracket' }, defrag(unwrap(as.import(bs as List<Node<string>>)))))]);
54
- }));
55
+ ([as, bs = new List(), cs], context) => new List([
56
+ new Node(html('span',
57
+ { class: bracketname(context, indexA, 1, 1) },
58
+ defrag(unwrap(as.import(bs as List<Node<string>>).import(cs)))))
59
+ ]),
60
+ ([as, bs = new List()], context) => new List([
61
+ new Node(html('span',
62
+ { class: bracketname(context, indexA, 1, 0) },
63
+ defrag(unwrap(as.import(bs as List<Node<string>>)))))
64
+ ])));
55
65
 
56
66
  const p2 = lazy(() => surround(
57
67
  str('('),
58
68
  precedence(1, recursion(Recursion.bracket, some(inline, ')', [[')', 1]]))),
59
69
  str(')'),
60
70
  true, [],
61
- ([as, bs = [], cs], { source, position, range, linebreak }) => {
62
- const str = linebreak === 0 ? source.slice(position - range + 1, position - 1) : '';
63
- return linebreak === 0 && (str === '' || indexF.test(str))
64
- ? new List([new Node(html('span', { class: 'paren' }, `(${str})`))])
65
- : new List([new Node(html('span', { class: 'bracket' }, defrag(unwrap(as.import(bs as List<Node<string>>).import(cs)))))]);
66
- },
67
- ([as, bs = new List()], context) => {
68
- const { source, position, range, linebreak } = context;
69
- const str = linebreak === 0 ? source.slice(position - range + 1, position) : '';
70
- return linebreak === 0 && (str === '' || indexF.test(str))
71
- ? new List([new Node(html('span', { class: 'paren' }, `(${str}`))])
72
- : new List([new Node(html('span', { class: 'bracket' }, defrag(unwrap(as.import(bs as List<Node<string>>)))))]);
73
- }));
71
+ ([as, bs = new List(), cs], context) => new List([
72
+ new Node(html('span',
73
+ { class: bracketname(context, indexF, 1, 1) },
74
+ defrag(unwrap(as.import(bs as List<Node<string>>).import(cs)))))
75
+ ]),
76
+ ([as, bs = new List()], context) => new List([
77
+ new Node(html('span',
78
+ { class: bracketname(context, indexF, 1, 0) },
79
+ defrag(unwrap(as.import(bs as List<Node<string>>)))))
80
+ ])));
74
81
 
75
82
  const s1 = lazy(() => surround(
76
83
  str('['),
@@ -123,7 +123,7 @@ export const inline: InlineParser = lazy(() => union([
123
123
  },
124
124
  autolink,
125
125
  text
126
- ])) as any;
126
+ ]));
127
127
 
128
128
  export { indexee } from './inline/extension/indexee';
129
129
  export { indexer } from './inline/extension/indexer';
@@ -13,22 +13,24 @@ export function* figure(
13
13
  readonly local?: boolean;
14
14
  } = {},
15
15
  ): Generator<HTMLAnchorElement | undefined, undefined, undefined> {
16
+ const selector = ':is(figure[data-label], h1, h2)';
16
17
  const refs = new MultiQueue<string, HTMLAnchorElement>(push(
17
18
  querySelectorAll(target, 'a.label:not(.local)[data-label]'),
18
19
  notes && querySelectorAll(notes.references, 'a.label:not(.local)') || [])
19
20
  .map(el => [el.getAttribute('data-label')!, el]));
20
21
  const labels = new Set<string>();
21
22
  const numbers = new Map<string, string>();
22
- const scope = target instanceof Element ? ':scope > ' : '';
23
23
  let base = '0';
24
24
  let bases: readonly string[] = base.split('.');
25
25
  let index: readonly string[] = bases;
26
26
  for (
27
- let defs = target.querySelectorAll(`${scope}:is(figure[data-label], h1, h2)`),
27
+ let defs = target instanceof Element
28
+ ? target.querySelectorAll(`:scope > ${selector}`)
29
+ : target.querySelectorAll(`${selector}:not(* > *)`),
28
30
  len = defs.length, i = 0; i < len; ++i) {
29
31
  yield;
30
32
  const def = defs[i];
31
- if (!scope && def.parentNode !== target) continue;
33
+ assert(def.parentNode === target || !def.parentNode);
32
34
  const { tagName } = def;
33
35
  if (bases.length === 1 && tagName[0] === 'H') continue;
34
36
  assert(base === '0' || bases.length > 1);
@@ -116,11 +116,11 @@ describe('Unit: parser/processor/note', () => {
116
116
  });
117
117
 
118
118
  it('separation', () => {
119
- const target = html('blockquote', parse([
119
+ const target = parse([
120
120
  '!>> ((1))\n> ((2))\n~~~',
121
121
  '~~~~example/markdown\n((3))\n~~~~',
122
122
  '((4))',
123
- ].join('\n\n')).children);
123
+ ].join('\n\n'));
124
124
  for (let i = 0; i < 3; ++i) {
125
125
  [...note(target)];
126
126
  assert.deepStrictEqual(
@@ -50,19 +50,19 @@ const referenceRefsMemoryCaller = memoize((target: Node) =>
50
50
  const annotation = build(
51
51
  'annotation',
52
52
  'annotations',
53
- '.annotation:not(:is(.annotations, .references) .annotation, .local)',
53
+ '.annotation:not(:is(.annotations, .references) &, .local)',
54
54
  n => `*${n}`,
55
55
  'h1, h2, h3, h4, h5, h6, aside.aside, hr');
56
56
  const reference = build(
57
57
  'reference',
58
58
  'references',
59
- '.reference:not(:is(.annotations, .references) .reference, .local)',
59
+ '.reference:not(:is(.annotations, .references) &, .local)',
60
60
  (n, abbr) => `[${abbr || n}]`);
61
61
 
62
62
  function build(
63
63
  syntax: string,
64
64
  list: string,
65
- query: string,
65
+ selector: string,
66
66
  marker: (index: number, abbr: string) => string,
67
67
  splitter: string = '',
68
68
  ) {
@@ -101,7 +101,7 @@ function build(
101
101
  };
102
102
  }, memory);
103
103
  const defs = new Map<string, HTMLLIElement>();
104
- const refs = target.querySelectorAll<HTMLElement>(query);
104
+ const refs = target.querySelectorAll<HTMLElement>(selector);
105
105
  const identifierInfoCaller = memoize((identifier: string) => ({
106
106
  defIndex: 0,
107
107
  defSubindex: 0,
@@ -109,8 +109,11 @@ function build(
109
109
  title: '' && identifier,
110
110
  queue: [] as HTMLElement[],
111
111
  }));
112
- const scope = target instanceof Element ? ':scope > ' : '';
113
- const splitters = splitter ? target.querySelectorAll(`${scope}:is(${splitter})`) : [];
112
+ const splitters = splitter
113
+ ? target instanceof Element
114
+ ? target.querySelectorAll(`:scope > :is(${splitter}, .${list})`)
115
+ : target.querySelectorAll(`:is(${splitter}, .${list}):not(* > *)`)
116
+ : [];
114
117
  let iSplitters = 0;
115
118
  let total = 0;
116
119
  let format: 'number' | 'abbr';
@@ -118,11 +121,12 @@ function build(
118
121
  for (let len = refs.length, i = 0; i < len; ++i) {
119
122
  const ref = refs[i];
120
123
  if (splitter) for (let splitter; splitter = splitters[iSplitters]; ++iSplitters) {
124
+ assert(splitter.parentNode === target || !splitter.parentNode);
121
125
  const pos = splitter?.compareDocumentPosition(ref) ?? 0;
122
126
  if (pos & (Node.DOCUMENT_POSITION_PRECEDING | Node.DOCUMENT_POSITION_DISCONNECTED)) break;
123
127
  if (~iSplitters << 32 - 8 === 0) yield;
124
- if (splitter.classList.contains(list) && defs.size === 0) {
125
- assert(splitter.matches(`.${list}`));
128
+ if (splitter.classList.contains(list) && splitter.nextElementSibling !== splitters[iSplitters + 1]) {
129
+ yield* proc(splitter as HTMLOListElement);
126
130
  splitter.remove();
127
131
  continue;
128
132
  }
@@ -133,7 +137,7 @@ function build(
133
137
  ? splitter as HTMLOListElement
134
138
  : target.insertBefore(html('ol', { class: list }), splitter);
135
139
  assert(note.parentNode);
136
- yield* proc(defs, note);
140
+ yield* proc(note, defs);
137
141
  assert(defs.size === 0);
138
142
  }
139
143
  }
@@ -209,25 +213,27 @@ function build(
209
213
  }
210
214
  if (note || defs.size > 0) {
211
215
  const splitter = splitters[iSplitters++];
212
- yield* proc(defs, note ?? (splitter?.classList.contains(list)
216
+ note ??= splitter?.classList.contains(list)
213
217
  ? splitter as HTMLOListElement
214
- : target.insertBefore(html('ol', { class: list }), splitter ?? bottom)));
218
+ : target.insertBefore(html('ol', { class: list }), splitter ?? bottom);
219
+ yield* proc(note, defs);
215
220
  assert(defs.size === 0);
216
221
  }
217
222
  if (splitter) for (let splitter; splitter = splitters[iSplitters]; ++iSplitters) {
218
223
  if (~iSplitters << 32 - 8 === 0) yield;
219
224
  if (splitter.classList.contains(list)) {
225
+ yield* proc(splitter as HTMLOListElement);
220
226
  splitter.remove();
221
227
  }
222
228
  }
223
229
  }
224
230
  }
225
231
 
226
- function* proc(defs: Map<string, HTMLLIElement>, note: HTMLOListElement): Generator<HTMLLIElement | undefined, undefined, undefined> {
227
- const { children } = note;
232
+ function* proc(note: HTMLOListElement, defs?: Map<string, HTMLLIElement>): Generator<HTMLLIElement | undefined, undefined, undefined> {
228
233
  for (let defs = note.children, i = defs.length; i--;) {
229
- yield note.removeChild(children[i] as HTMLLIElement);
234
+ yield note.removeChild(defs[i] as HTMLLIElement);
230
235
  }
236
+ if (!defs) return;
231
237
  for (const [, def] of defs) {
232
238
  yield note.appendChild(def);
233
239
  }
@@ -35,7 +35,7 @@ const parser: SegmentParser = union([
35
35
  }
36
36
  },
37
37
  some(contentline, MAX_SEGMENT_SIZE + 1),
38
- ]) as any;
38
+ ]);
39
39
 
40
40
  export function* segment(source: string): Generator<readonly [string, Segment], undefined, undefined> {
41
41
  if (!validate(source, MAX_INPUT_SIZE)) return yield [`${Command.Error}Too large input over ${MAX_INPUT_SIZE.toLocaleString('en')} bytes.\n${source.slice(0, 1001)}`, Segment.unknown];