Files
react-native-devtools-frontend/node_modules/micromark/lib/initialize/text.js
T
Tim van der Lippe efb716af67 Update linter packages
- Updates stylelint to 13.8.0
- Updates ESLint to 7.14.0
- Updates @typescript-eslint to 4.9.0

DISABLE_THIRD_PARTY_CHECK=NPM update
R=jacktfranklin@chromium.org

Change-Id: I207e204607ede782710e042c17d6510c7f696905
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2566806
Reviewed-by: Jack Franklin <jacktfranklin@chromium.org>
Commit-Queue: Tim van der Lippe <tvanderlippe@chromium.org>
2020-12-01 16:04:40 +00:00

202 lines
4.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
exports.text = initializeFactory('text')
exports.string = initializeFactory('string')
exports.resolver = {resolveAll: resolver()}
var codes = require('../character/codes')
var assign = require('../constant/assign')
var constants = require('../constant/constants')
var types = require('../constant/types')
var shallow = require('../util/shallow')
function initializeFactory(field) {
return {
tokenize: initializeText,
resolveAll: resolver(field === 'text' ? resolveAllLineSuffixes : undefined)
}
function initializeText(effects) {
var self = this
var constructs = this.parser.constructs[field]
var text = effects.attempt(constructs, start, notText)
return start
function start(code) {
return atBreak(code) ? text(code) : notText(code)
}
function notText(code) {
if (code === codes.eof) {
effects.consume(code)
return
}
effects.enter(types.data)
effects.consume(code)
return data
}
function data(code) {
if (atBreak(code)) {
effects.exit(types.data)
return text(code)
}
// Data.
effects.consume(code)
return data
}
function atBreak(code) {
var list = constructs[code]
var index = -1
if (code === codes.eof) {
return true
}
if (list) {
while (++index < list.length) {
if (
!list[index].previous ||
list[index].previous.call(self, self.previous)
) {
return true
}
}
}
}
}
}
function resolver(extraResolver) {
return resolveAllText
function resolveAllText(events, context) {
var index = -1
var enter
// A rather boring computation (to merge adjacent `data` events) which
// improves mm performance by 29%.
while (++index <= events.length) {
if (enter === undefined) {
if (events[index] && events[index][1].type === types.data) {
enter = index
index++
}
} else if (!events[index] || events[index][1].type !== types.data) {
// Don’t do anything if there is one data token.
if (index !== enter + 2) {
events[enter][1].end = events[index - 1][1].end
events.splice(enter + 2, index - enter - 2)
index = enter + 2
}
enter = undefined
}
}
return extraResolver ? extraResolver(events, context) : events
}
}
// A rather ugly set of instructions which again looks at chunks in the input
// stream.
// The reason to do this here is that it is *much* faster to parse in reverse.
// And that we can’t hook into `null` to split the line suffix before an EOF.
// To do: figure out if we can make this into a clean utility, or even in core.
// As it will be useful for GFMs literal autolink extension (and maybe even
// tables?)
function resolveAllLineSuffixes(events, context) {
var eventIndex = -1
var chunks
var data
var chunk
var index
var bufferIndex
var size
var tabs
var token
while (++eventIndex <= events.length) {
if (
(eventIndex === events.length ||
events[eventIndex][1].type === types.lineEnding) &&
events[eventIndex - 1][1].type === types.data
) {
data = events[eventIndex - 1][1]
chunks = context.sliceStream(data)
index = chunks.length
bufferIndex = -1
size = 0
tabs = undefined
while (index--) {
chunk = chunks[index]
if (typeof chunk === 'string') {
bufferIndex = chunk.length
while (chunk.charCodeAt(bufferIndex - 1) === codes.space) {
size++
bufferIndex--
}
if (bufferIndex) break
bufferIndex = -1
}
// Number
else if (chunk === codes.horizontalTab) {
tabs = true
size++
} else if (chunk === codes.virtualSpace) {
// Empty
} else {
// Replacement character, exit.
index++
break
}
}
if (size) {
token = {
type:
eventIndex === events.length ||
tabs ||
size < constants.hardBreakPrefixSizeMin
? types.lineSuffix
: types.hardBreakTrailing,
start: {
line: data.end.line,
column: data.end.column - size,
offset: data.end.offset - size,
_index: data.start._index + index,
_bufferIndex: index
? bufferIndex
: data.start._bufferIndex + bufferIndex
},
end: shallow(data.end)
}
data.end = shallow(token.start)
if (data.start.offset === data.end.offset) {
assign(data, token)
} else {
events.splice(
eventIndex,
0,
['enter', token, context],
['exit', token, context]
)
eventIndex += 2
}
}
eventIndex++
}
}
return events
}