diff --git a/book/online-book/src/10-minimum-example/070-more-complex-parser.md b/book/online-book/src/10-minimum-example/070-more-complex-parser.md index 2bb95e01..b4ada60e 100644 --- a/book/online-book/src/10-minimum-example/070-more-complex-parser.md +++ b/book/online-book/src/10-minimum-example/070-more-complex-parser.md @@ -281,6 +281,27 @@ function startsWithEndTagOpen(source: string, tag: string): boolean { 続いて parseElement と parseText について実装していきます。 +::: tip isEnd のループについて + isEnd では ancestors の配列のそれぞれの要素に対して startsWithEndTagOpen で s がその要素の閉じタグで始まっている文字列かどうかをループでチェックするような処理になっています。 +```typescript +function isEnd(context: ParserContext, ancestors: ElementNode[]): boolean { + const s = context.source + + // s が '= 0; --i) { + if (startsWithEndTagOpen(s, ancestors[i].tag)) { + return true + } + } + } + + return !s +} +``` +しかし、s が閉じタグで始まっている文字列かどうかをチェックするのであれば、ancestors の最後の要素に対してのみチェックすれば良いはずです。parser のリライトによってこのコードは無くなってしまいましたが、リライト前の Vue 3.3 のコードで ancestors の最後の要素に対してのみチェックするようにコードを書き換えても正常系のテストは全て PASS します。 +::: + ## parseText まずはシンプルな parseText の方から.一部、parseText 以外でも使うユーティリティも実装しているので少しだけ長いです。 diff --git a/book/online-book/src/en/10-minimum-example/070-more-complex-parser.md b/book/online-book/src/en/10-minimum-example/070-more-complex-parser.md index a4474d10..16fb9a5b 100644 --- a/book/online-book/src/en/10-minimum-example/070-more-complex-parser.md +++ b/book/online-book/src/en/10-minimum-example/070-more-complex-parser.md @@ -282,6 +282,27 @@ function startsWithEndTagOpen(source: string, tag: string): boolean { Next, let's implement parseElement and parseText. +::: tip About the isEnd Loop +In isEnd, there is a loop process that checks whether 's' starts with the closing tag of each element in the ancestors array using startsWithEndTagOpen. +```typescript +function isEnd(context: ParserContext, ancestors: ElementNode[]): boolean { + const s = context.source + + // If s starts with = 0; --i) { + if (startsWithEndTagOpen(s, ancestors[i].tag)) { + return true + } + } + } + + return !s +} +``` +However, if you need to check whether 's' starts with a closing tag, it should be sufficient to check only the last element in ancestors. Although this section of code was eliminated in a recent rewrite of the parser, modifying the Vue 3.3 code to only check the last element in ancestors still results in all the positive tests passing successfully. +::: + ## parseText First, let's start with the simple parseText. It is a bit long because it also implements some utilities that are used not only in parseText but also in other functions.