Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function format (nodes, options) {
const outputNode = type === 'element'
? {
type,
tagName: node.tagName.toLowerCase(),
tagName: options.tagNameToLowerCase ? node.tagName.toLowerCase() : node.tagName,
attributes: formatAttributes(node.attributes),
children: format(node.children, options)
}
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export const parseDefaults = {
closingTags,
childlessTags,
closingTagAncestorBreakers,
includePositions: false
includePositions: false,
tagNameToLowerCase: true
}

export function parse (str, options = parseDefaults) {
Expand Down
8 changes: 4 additions & 4 deletions src/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function lex (state) {
lexComment(state)
} else {
const tagName = lexTag(state)
const safeTag = tagName.toLowerCase()
const safeTag = state.options.tagNameToLowerCase ? tagName.toLowerCase() : tagName
if (arrayIncludes(childlessTags, safeTag)) {
lexSkipTag(tagName, state)
}
Expand Down Expand Up @@ -273,7 +273,7 @@ const push = [].push

export function lexSkipTag (tagName, state) {
const {str, position, tokens} = state
const safeTagName = tagName.toLowerCase()
const safeTagName = state.options.tagNameToLowerCase ? tagName.toLowerCase() : tagName
const len = str.length
let index = position.index
while (index < len) {
Expand All @@ -286,8 +286,8 @@ export function lexSkipTag (tagName, state) {
const tagStartPosition = copyPosition(position)
jumpPosition(tagStartPosition, str, nextTag)
const tagState = {str, position: tagStartPosition, tokens: []}
const name = lexTag(tagState)
if (safeTagName !== name.toLowerCase()) {
const name = state.options.tagNameToLowerCase ? lexTag(tagState).toLowerCase() : lexTag(tagState)
if (safeTagName !== name) {
index = tagState.position.index
continue
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function parse (state) {

const tagToken = tokens[++cursor]
cursor++
const tagName = tagToken.content.toLowerCase()
const tagName = state.options.tagNameToLowerCase ? tagToken.content.toLowerCase() : tagToken.content
if (token.close) {
let index = stack.length
let shouldRewind = false
Expand Down
7 changes: 4 additions & 3 deletions src/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ export function toHTML (tree, options) {
return `<!--${node.content}-->`
}
const {tagName, attributes, children} = node
const isSelfClosing = arrayIncludes(options.voidTags, tagName.toLowerCase())
const safeTagName = options.tagNameToLowerCase ? tagName.toLowerCase() : tagName
const isSelfClosing = arrayIncludes(options.voidTags, safeTagName)
return isSelfClosing
? `<${tagName}${formatAttributes(attributes)}>`
: `<${tagName}${formatAttributes(attributes)}>${toHTML(children, options)}</${tagName}>`
? `<${safeTagName}${formatAttributes(attributes)}>`
: `<${safeTagName}${formatAttributes(attributes)}>${toHTML(children, options)}</${safeTagName}>`
}).join('')
}

Expand Down
10 changes: 5 additions & 5 deletions test/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ test('lexTagAttributes should handle incomplete attributes', t => {
test('lexSkipTag should tokenize as text until the matching tag name', t => {
const str = 'abcd<test><h1>Test case</h1></test><x>'
const finish = str.indexOf('<x>')
const state = {str, position: ps(10), tokens: []}
const state = {str, position: ps(10), tokens: [], options: {tagNameToLowerCase: false}}
lexSkipTag('test', state)
t.is(state.position.index, finish)
t.deepEqual(state.tokens, [
Expand All @@ -358,7 +358,7 @@ test('lexSkipTag should tokenize as text until the matching tag name', t => {
test('lexSkipTag should stop at the case-insensitive matching tag name', t => {
const str = '<tEsT>proving <???> the point</TeSt><x>'
const finish = str.indexOf('<x>')
const state = {str, position: ps(6), tokens: []}
const state = {str, position: ps(6), tokens: [], options: {tagNameToLowerCase: true}}
lexSkipTag('tEsT', state)
t.is(state.position.index, finish)
t.deepEqual(state.tokens, [
Expand All @@ -371,7 +371,7 @@ test('lexSkipTag should stop at the case-insensitive matching tag name', t => {

test('lexSkipTag should auto-close if the end tag is not found', t => {
const str = '<script>This never ends'
const state = {str, position: ps(8), tokens: []}
const state = {str, position: ps(8), tokens: [], options: {tagNameToLowerCase: true}}
lexSkipTag('script', state)
t.is(state.position.index, str.length)
t.deepEqual(state.tokens, [
Expand All @@ -381,7 +381,7 @@ test('lexSkipTag should auto-close if the end tag is not found', t => {

test('lexSkipTag should handle finding a stray "</" [resilience]', t => {
const str = '<script>proving </nothing></script>'
const state = {str, position: ps(8), tokens: []}
const state = {str, position: ps(8), tokens: [], options: {tagNameToLowerCase: true}}
lexSkipTag('script', state)
t.is(state.position.index, str.length)
t.deepEqual(state.tokens, [
Expand All @@ -394,7 +394,7 @@ test('lexSkipTag should handle finding a stray "</" [resilience]', t => {

test('lexSkipTag should not add an empty inner text node', t => {
const str = '<script></script>'
const state = {str, position: ps(8), tokens: []}
const state = {str, position: ps(8), tokens: [], options: {tagNameToLowerCase: true}}
lexSkipTag('script', state)
t.is(state.position.index, str.length)
t.deepEqual(state.tokens, [
Expand Down
5 changes: 3 additions & 2 deletions test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ function ps (index) {
return { index, line: 0, column: index }
}

const lexerOptions = { childlessTags: [] }
const lexerOptions = { childlessTags: [], tagNameToLowerCase: true }
const parserOptions = {
voidTags: [],
closingTags: [],
closingTagAncestorBreakers: {}
closingTagAncestorBreakers: {},
tagNameToLowerCase: true
}

test('parser() should return nodes', t => {
Expand Down