Skip to content
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

WIP: Fix handling of right angle bracket (>) in attributes #82

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

andyw8
Copy link
Contributor

@andyw8 andyw8 commented Oct 14, 2024

This is the fix suggested by @jon-sully in #78 (comment)

It may need further work and verification, so I'm only opening it as a draft for now to allow for discussion.

Also, it breaks one of the current tests: HtmlBeautifier indents general self-closing tags

@andyw8 andyw8 force-pushed the andyw8/fix-right-angled-bracket-in-attributes branch from ce87755 to 8a988ed Compare October 14, 2024 17:57
@andyw8 andyw8 changed the title Fix handling of right-angled brackets in attributes WIP: Fix handling of right-angled brackets in attributes Oct 14, 2024
@andyw8 andyw8 changed the title WIP: Fix handling of right-angled brackets in attributes WIP: Fix handling of right angle bracket (>) in attributes Oct 14, 2024
@@ -4,7 +4,7 @@

module HtmlBeautifier
class HtmlParser < Parser
ELEMENT_CONTENT = %r{ (?:<%.*?%>|[^>])* }mx
ELEMENT_CONTENT = %r{ (?:<%.*?%>|data-action\s*=\s*"(?:[^"]*?->[^"]*?)"|[^>])* }mx
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this should be generalized rather being tied to data-action.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I simply couldn't figure out the regex magic to make it work without being this specific

@@ -623,6 +623,22 @@
expect(described_class.beautify(source)).to eq(expected)
end

it "does not break for > within an attribute value" do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This verifies it doesn't break code which is already formatted correctly, but there should probably be some other tests.

@phyzical
Copy link

phyzical commented Nov 1, 2024

i couldnt really come up with a more clever regex but testing in regex101 the following still matched,

%r{ (?:<%.*?%>|(?:[^"]*?->[^"]*?)|[^>])* }mx

its a bit dumber but just allows > within quotes

Hmm actually it lead me to adjust it once more to the following

%r{ (?:[^"]?[-%]>[^"]?|[^>])* }mx

moved the erb tag or - check together [-%]

im not sure if this is correct but, ill fork sometime next week and see for myself

@phyzical
Copy link

phyzical commented Nov 7, 2024

So i gave it a try, neither of my queries were perfect %r{ (?:[^"]?[-%]>[^"]?|[^>])* }mx is close.. but it breaks
it "stays indented within <details> with Boolean attribute handled by ERB" do

@andyw8 i did notice if your revert
image
all tests will pass, was this change intentional?

@andyw8
Copy link
Contributor Author

andyw8 commented Nov 7, 2024

It was copied from #78 (comment) but I haven't yet looked into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants