Skip to content

Commit

Permalink
fix: html parser, close #628
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 11, 2021
1 parent d648a01 commit d2aee15
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
2 changes: 1 addition & 1 deletion examples/by-features/extractions/Demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
export default {
computed: {
foobar() {
return 'Good Morning!'
return '<>Good Morning!'
},
},
}
Expand Down
32 changes: 24 additions & 8 deletions src/core/CurrentFile.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { workspace, ExtensionContext, Uri, window, EventEmitter } from 'vscode'
import { throttle } from 'lodash'
import { ComposedLoader } from './loaders/ComposedLoader'
import { Global } from './Global'
import { VueSfcLoader } from './loaders/VueSfcLoader'
import { Loader, Analyst } from '.'
import { DetectHardStrings } from '~/commands/detectHardStrings'
import { DetectionResult } from '~/core/types'
import { Log } from '~/utils/Log'

export class CurrentFile {
static _vue_sfc_loader: VueSfcLoader | null = null
static _composed_loader = new ComposedLoader()
static _onInvalidate = new EventEmitter<boolean>()
static _onInitialized = new EventEmitter<void>()
static _onHardStringDetected = new EventEmitter<DetectionResult[] | undefined>()
static _currentUri: Uri | undefined

static onInvalidate = CurrentFile._onInvalidate.event
static onHardStringDetected = CurrentFile._onHardStringDetected.event
Expand All @@ -22,23 +25,27 @@ export class CurrentFile {
}

static watch(ctx: ExtensionContext) {
ctx.subscriptions.push(workspace.onDidSaveTextDocument(e => this.update(e.uri)))
ctx.subscriptions.push(workspace.onDidChangeTextDocument(e => this.update(e.document.uri)))
ctx.subscriptions.push(window.onDidChangeActiveTextEditor(e => this.update(e && e.document.uri)))
ctx.subscriptions.push(workspace.onDidSaveTextDocument(e => this._currentUri && e?.uri === this._currentUri && this.update(e.uri)))
ctx.subscriptions.push(workspace.onDidChangeTextDocument(e => this._currentUri && e?.document?.uri === this._currentUri && this.throttleUpdate(e.document.uri)))
ctx.subscriptions.push(window.onDidChangeActiveTextEditor(e => e?.document && this.update(e.document.uri)))
ctx.subscriptions.push(Global.onDidChangeLoader(() => {
Log.warn('invalidate1', true)
this.invalidate()
this.updateLoaders()
this._composed_loader.fire('{Config}')
}))
ctx.subscriptions.push(Analyst.watch())
this.update(window.activeTextEditor && window.activeTextEditor.document.uri)
this.update(window.activeTextEditor?.document.uri)
this.updateLoaders()
}

static throttleUpdate = throttle((uri?: Uri) => CurrentFile.update(uri), 100)

static update(uri?: Uri) {
if (!Global.enabled)
return

this._currentUri = uri
this.invalidate()
if (this.VueSfc) {
if (this._vue_sfc_loader) {
Expand Down Expand Up @@ -80,9 +87,18 @@ export class CurrentFile {
static hardStrings: DetectionResult[] | undefined

static async detectHardStrings(force = false) {
if (!this.hardStrings || force)
this.hardStrings = await DetectHardStrings()
this._onHardStringDetected.fire(this.hardStrings)
return this.hardStrings
try {
if (!this.hardStrings || force) {
this.hardStrings = await DetectHardStrings()
this._onHardStringDetected.fire(this.hardStrings)
}
return this.hardStrings
}
catch (e) {
Log.error('Failed to extract current file', false)
Log.error(e, false)
this.hardStrings = []
return this.hardStrings
}
}
}
15 changes: 10 additions & 5 deletions src/extraction/parsers/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ export function detect(
input = input.replace(/<(.*?)={(.*?)}(.*?)>/g, '<$1="$2"$3>')

let lastTag = ''
let lastTagIndex: number | null = null
const parser = new Parser({
onopentag(name, attrs) {
lastTag = name
lastTagIndex = parser.endIndex! + 1
if (IGNORED_TAGS.includes(name))
return

Expand Down Expand Up @@ -84,15 +86,18 @@ export function detect(
})
}
},
onclosetag(name) {
if (name !== 'script' || lastTagIndex == null)
return
const start = lastTagIndex
const fullText = input.slice(start, parser.startIndex!)
if (extractScripts && lastTag === 'script')
detections.push(...shiftDetectionPosition(extractScripts(fullText, start), start))
},
ontext(fullText) {
const start = parser.startIndex
const end = parser.endIndex! + 1

if (extractScripts && lastTag === 'script') {
detections.push(...shiftDetectionPosition(extractScripts(fullText, start), start))
return
}

if (IGNORED_TAGS.includes(lastTag))
return

Expand Down
12 changes: 4 additions & 8 deletions src/views/items/CurrentFileExtractionItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { CurrentFileLocalesTreeProvider } from '../providers'
import { BaseTreeItem } from './Base'
import { HardStringDetectResultItem } from './HardStringDetectResultItem'
import i18n from '~/i18n'
import { Config, CurrentFile, Global, DetectionResult } from '~/core'
import { Config, CurrentFile, Global } from '~/core'

export class CurrentFileExtractionItem extends BaseTreeItem {
result: DetectionResult[] | undefined
langId = 'unknown'

constructor(readonly provider: CurrentFileLocalesTreeProvider) {
Expand Down Expand Up @@ -36,7 +35,7 @@ export class CurrentFileExtractionItem extends BaseTreeItem {
}
else {
const length = CurrentFile.hardStrings?.length
if (this.collapsibleState === TreeItemCollapsibleState.Collapsed && !length)
if (this.collapsibleState === TreeItemCollapsibleState.Collapsed && length == null)
return i18n.t('view.current_file_hard_strings_expand_to_detect')
else
return length == null ? '' : length.toString()
Expand All @@ -55,11 +54,8 @@ export class CurrentFileExtractionItem extends BaseTreeItem {
if (this.collapsibleState === TreeItemCollapsibleState.None)
return []

this.result = await CurrentFile.detectHardStrings()
await CurrentFile.detectHardStrings()

if (this.result == null)
return []

return this.result.map(i => new HardStringDetectResultItem(this.ctx, i))
return CurrentFile.hardStrings?.map(i => new HardStringDetectResultItem(this.ctx, i)) || []
}
}

0 comments on commit d2aee15

Please sign in to comment.