-
Notifications
You must be signed in to change notification settings - Fork 25
Feat: Support <script>
#18
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
Open
ymtszw
wants to merge
8
commits into
hecrj:master
Choose a base branch
from
ymtszw:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c619086
test: activate scriptTests and add realWorld1 test
ymtszw b1885c3
test: separate Double/Single quote
ymtszw c2e991e
feat: support consuming <script> as a Text (and Comment)
ymtszw 645a07d
chore: update README/CHANGELOG
ymtszw 08e3e81
fix: typo
ymtszw 1a739f1
test: PR #18 comment
ymtszw 690b171
chore: rename due to Main.elm discovery issues in some cases
ymtszw b89dc29
fix: inappropriate module name
ymtszw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -259,6 +259,17 @@ element = | |
] | ||
|. Parser.chompIf ((==) '>') | ||
|
||
else if name == "script" then | ||
-- <script> can contain JavaScript operator '<' which confuses `closingTag` parser so: | ||
-- | ||
-- * look for `</script>` exactly to close the tag | ||
-- * UNLESS it is contained in a JavaScript string or comment | ||
-- | ||
-- In essence, we provide partial JavaScript parser here | ||
Parser.succeed (Element name attributes) | ||
|. Parser.chompIf ((==) '>') | ||
|= consumeJavaScriptUntilClosingTag | ||
|
||
else | ||
Parser.succeed (Element name attributes) | ||
|. Parser.chompIf ((==) '>') | ||
|
@@ -267,6 +278,103 @@ element = | |
) | ||
|
||
|
||
consumeJavaScriptUntilClosingTag : Parser (List Node) | ||
consumeJavaScriptUntilClosingTag = | ||
Parser.loop [] <| | ||
\acc -> | ||
let | ||
accumulate newNode = | ||
Parser.Loop <| | ||
case ( acc, newNode ) of | ||
( [], first ) -> | ||
[ first ] | ||
|
||
( (Text accChunk) :: tail, Text newChunk ) -> | ||
-- Merge top-most text node unless HTML comment nodes are interleaved | ||
Text (accChunk ++ newChunk) :: tail | ||
|
||
( nonTextNode :: tail, _ ) -> | ||
newNode :: nonTextNode :: tail | ||
in | ||
Parser.oneOf | ||
[ -- HTML comments are, albeit considered a bad practice recently, | ||
-- allowed inside <script> to hide scripts from really ancient web browser | ||
comment | ||
|> Parser.map accumulate | ||
, Parser.lineComment "//" | ||
|> Parser.getChompedString | ||
|> Parser.map (Text >> accumulate) | ||
, Parser.multiComment "/*" "*/" Parser.NotNestable | ||
|> Parser.getChompedString | ||
|> Parser.map (Text >> accumulate) | ||
, javaScriptStringLike '"' | ||
|> Parser.map (Text >> accumulate) | ||
, javaScriptStringLike '\'' | ||
|> Parser.map (Text >> accumulate) | ||
, javaScriptStringLike '`' | ||
|> Parser.map (Text >> accumulate) | ||
, closingScriptTag | ||
|> Parser.map (\() -> Parser.Done (List.reverse acc)) | ||
, Parser.chompIf (always True) | ||
|> Parser.getChompedString | ||
|> Parser.map (Text >> accumulate) | ||
] | ||
|
||
|
||
closingScriptTag : Parser () | ||
closingScriptTag = | ||
Parser.token "</" | ||
|. (Parser.chompWhile (\char -> char /= '>' && not (isSpaceCharacter char)) | ||
|> Parser.getChompedString | ||
|> Parser.andThen | ||
(\chunk -> | ||
if String.toLower chunk == "script" then | ||
Parser.succeed () | ||
|
||
else | ||
Parser.problem "not a </script>" | ||
) | ||
) | ||
|. Parser.chompWhile isSpaceCharacter | ||
|. Parser.token ">" | ||
|
||
|
||
javaScriptStringLike : Char -> Parser String | ||
javaScriptStringLike terminatorChar = | ||
let | ||
terminatorStr = | ||
String.fromChar terminatorChar | ||
in | ||
Parser.succeed identity | ||
|. Parser.token terminatorStr | ||
|= Parser.loop "" (stringHelp terminatorChar terminatorStr) | ||
-- Restoring original shape | ||
|> Parser.map (\chunk -> terminatorStr ++ chunk ++ terminatorStr) | ||
|
||
|
||
stringHelp : Char -> String -> String -> Parser (Parser.Step String String) | ||
stringHelp terminatorChar terminatorStr acc = | ||
Parser.oneOf | ||
[ Parser.succeed (\char -> Parser.Loop (acc ++ "\\" ++ char)) | ||
|. Parser.token "\\" | ||
|= justOneChar | ||
, Parser.token terminatorStr | ||
|> Parser.map (\_ -> Parser.Done acc) | ||
, chompOneOrMore (\char -> char /= '\\' && char /= terminatorChar) | ||
|> Parser.getChompedString | ||
|> Parser.map (\chunk -> Parser.Loop (acc ++ chunk)) | ||
] | ||
|
||
|
||
justOneChar : Parser String | ||
justOneChar = | ||
Parser.loop () <| | ||
\_ -> | ||
Parser.chompIf (always True) | ||
|> Parser.getChompedString | ||
|> Parser.map Parser.Done | ||
Comment on lines
+369
to
+375
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This "consuming just one whatever character (after backslash escape)" is somewhat rough, but couldn't come up with better impl |
||
|
||
|
||
tagName : Parser String | ||
tagName = | ||
Parser.getChompedString | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 is mostly based on https://github.com/elm/parser/blob/master/examples/DoubleQuoteString.elm