New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vue: improve parsing custom blocks with angular-html-parser@1.5.0
#8153
Conversation
src/language-html/parser-html.js
Outdated
@@ -288,6 +339,11 @@ module.exports = { | |||
vue: createParser({ | |||
recognizeSelfClosing: true, | |||
isTagNameCaseSensitive: true, | |||
getTagContentType: (tagname) => { | |||
if (tagname !== "template") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (tagname !== "template") { | |
if (tagname.toLowerCase() !== "template") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this is necessary. I haven't seen template tag written with upper case. I also can't find any such mention in the specification or in the source code of the sfc-compiler.
Does it need to be supported by the vue parser? It is supported by the HTML parser(Playground), I think which is sufficient. (We don't have a vue parser tests for it.) |
I don't know, but people can use |
I'm not a Vue user, but the name of "single file components" implies that there are "multiple file components" that have their templates in separate files. I skimmed through the Vue docs, found some mentions of "in-DOM templates", but there doesn't seem to be a separate page about this. |
I forgot about directives. I cannot find about vue app written in only one html file in documentations, but the below code works fine. It would be good that Prettier support this. I'll fix. <html>
<body>
<div id="example">
<p v-if="hello === 'Hello World!'">{{ hello }}</p>
</div>
<script src="https://unpkg.com/vue"></script>
<script>
new Vue({
el: '#example',
data: { hello: 'Hello World!' }
})
</script>
</body>
</html> |
src/language-html/parser-html.js
Outdated
@@ -340,7 +340,7 @@ module.exports = { | |||
recognizeSelfClosing: true, | |||
isTagNameCaseSensitive: true, | |||
getTagContentType: (tagname) => { | |||
if (tagname !== "template") { | |||
if (tagname !== "template" && tagname !== "html") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about template
, but I'm sure about html
, it can be uppercase :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stable prettier does not support it...(Playground). We should open new issue.
parser=vue
Input:
<!DOCTYPE html><HTML>
<body>
<div v-if="foo === 'foo'">
</div>
<script>
new Vue({el: '#app'})
</script>
</body>
</HTML>
Output:
<!DOCTYPE html
>><HTML>
<body>
<div v-if="foo === 'foo'">
</div>
<script>
new Vue({el: '#app'})
</script>
</body>
</HTML>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't use angular, angular does not support it too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ooops, I'll fix.. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I saw the ci running, then I test those two links. thought it's my problem. The commits are missing on your fork too https://github.com/sosukesuzuki/prettier/commits/8106 |
I successfully fetched them from the fork. Looks like GitHub's UI is out of sync. |
All issues addressed? |
Yes all issues were probably fixed. Can we merge this? Should I fix commits history? |
I'm good. |
I didn't bring this case here #8162 (comment), but you should know. I try to throw SyntaxError in |
I reported this situation to GitHub. The commits still aren't visible on the UI. Not sure what happens if we do a merge. BTW, a question on SO relevant to this PR: https://stackoverflow.com/q/61742084/76173 |
I use this |
Try close and reopen ? |
src/language-html/parser-html.js
Outdated
@@ -65,6 +141,7 @@ function ngHtmlParser( | |||
} else if (node instanceof Text) { | |||
node.type = "text"; | |||
} else { | |||
console.log(node); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops, I'll remove this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This done?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GO!
@fisker Prettier formats |
I think so too but the behavior is happened by stable Prettier. So we can fix this in other PR. |
I think we should open new issues for #8153 (comment) and #8153 (comment) , should not fix in this PR. |
src/language-html/parser-html.js
Outdated
const getSameLocationNode = (node) => { | ||
const result = getSecondParse(); | ||
return result.rootNodes.find((_node) => { | ||
if (!_node || !_node.startSourceSpan) { | ||
return false; | ||
} | ||
if ( | ||
node.startSourceSpan.start.offset === | ||
_node.startSourceSpan.start.offset | ||
) { | ||
return true; | ||
} | ||
return false; | ||
}); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const getSameLocationNode = (node) => { | |
const result = getSecondParse(); | |
return result.rootNodes.find((_node) => { | |
if (!_node || !_node.startSourceSpan) { | |
return false; | |
} | |
if ( | |
node.startSourceSpan.start.offset === | |
_node.startSourceSpan.start.offset | |
) { | |
return true; | |
} | |
return false; | |
}); | |
}; | |
const getSameLocationNode = (node) => | |
getSecondParse().rootNodes.find( | |
({ startSourceSpan }) => | |
startSourceSpan && | |
startSourceSpan.start.offset === node.startSourceSpan.start.offset | |
); |
node
can't be false, right?
src/language-html/parser-html.js
Outdated
const getSecondParse = () => | ||
secondParseResult | ||
? secondParseResult | ||
: (secondParseResult = doSecondParse()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const getSecondParse = () => | |
secondParseResult | |
? secondParseResult | |
: (secondParseResult = doSecondParse()); | |
const getSecondParse = () => | |
secondParseResult || (secondParseResult = doSecondParse()); |
src/language-html/parser-html.js
Outdated
const replaceNode = getSameLocationNode(node); | ||
if (replaceNode) { | ||
rootNodes[i] = replaceNode; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const replaceNode = getSameLocationNode(node); | |
if (replaceNode) { | |
rootNodes[i] = replaceNode; | |
} | |
rootNodes[i] = getSameLocationNode(node) || rootNodes[i]; |
src/language-html/parser-html.js
Outdated
tagName !== "html" && | ||
!hasParent && | ||
(tagName !== "template" || | ||
attrs.find((attr) => attr.name === "lang" && attr.value !== "html")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
attrs.find((attr) => attr.name === "lang" && attr.value !== "html")) | |
attrs.some(({name, value}) => name === "lang" && value !== "html")) |
@fisker Thank you for your review! I fixed all reviewed points. |
Great job! |
Fixes #8108
Fixes #6503
Fixes #6325
Close #7975
changelog_unreleased/*/pr-XXXX.md
file followingchangelog_unreleased/TEMPLATE.md
.✨Try the playground for this PR✨