Skip to content

Commit

Permalink
Fixed a bug that caused an error with prefixed attributes. (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi authored Jun 23, 2020
1 parent 87a1bea commit 71c4633
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
10 changes: 6 additions & 4 deletions lib/services/html-parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict"

const parse5 = require("parse5")
const ast = require("../ast/html-nodes")

const NODENAME_TO_TYPE_MAP = {
"#document": "HTMLDocument",
Expand All @@ -9,8 +10,6 @@ const NODENAME_TO_TYPE_MAP = {
"#documentType": "HTMLDocumentType",
"#text": "HTMLText",
}
const ast = require("../ast/html-nodes")

const HTMLDocument = ast.HTMLDocument
const HTMLDocumentFragment = ast.HTMLDocumentFragment
const HTMLDocumentType = ast.HTMLDocumentType
Expand Down Expand Up @@ -136,15 +135,18 @@ const TOKEN_BUILDERS = {
{ parent: token }
))
startTag.attributes = node.attrs.map((attr) => {
const attrLoc = location.startTag.attrs[attr.name]
const key = attr.prefix
? `${attr.prefix}:${attr.name}`
: attr.name
const attrLoc = location.startTag.attrs[key]
const attrToken = new HTMLAttribute(
html,
attrLoc.startOffset,
attrLoc.endOffset,
sourceCodeStore,
{
parent: startTag,
key: attr.name,
key,
value: attr.value,
}
)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-lodash-template",
"version": "0.18.0",
"version": "0.18.1",
"description": "ESLint plugin for John Resig-style micro template, Lodash's template, Underscore's template and EJS.",
"main": "lib/index.js",
"scripts": {
Expand Down
4 changes: 4 additions & 0 deletions tests/lib/rules/attribute-name-casing.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ tester.run("attribute-name-casing", rule, {
code: '<body><div onClick="onClick"></div></body>',
options: [{ ignore: ["onClick"] }],
},
{
filename: "test.html",
code: '<svg xml:space="preserve"></svg>',
},
],

invalid: [
Expand Down
38 changes: 38 additions & 0 deletions tests/lib/services/html-parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"use strict"

const assert = require("assert")
const htmlParser = require("../../../lib/services/html-parser")
const SourceCodeStore = require("../../../lib/services/source-code-store")

/**
* Parse html
* @param {string} code source code
*/
function parse(code) {
const store = new SourceCodeStore(code)
return htmlParser(code, store)
}

describe("html-parser test", () => {
it("prefixed attributes", () => {
// https://github.com/ota-meshi/eslint-plugin-lodash-template/issues/136
const ast = parse('<svg xml:space="preserve"></svg>')
assert.strictEqual(ast.type, "HTMLDocumentFragment")
assert.strictEqual(ast.children.length, 1)
assert.strictEqual(ast.children[0].type, "HTMLElement")
assert.strictEqual(ast.children[0].name, "svg")
assert.strictEqual(ast.children[0].startTag.attributes.length, 1)
assert.deepStrictEqual(ast.children[0].startTag.attributes[0].range, [
5,
25,
])
assert.strictEqual(
ast.children[0].startTag.attributes[0].key,
"xml:space"
)
assert.strictEqual(
ast.children[0].startTag.attributes[0].value,
"preserve"
)
})
})

0 comments on commit 71c4633

Please sign in to comment.