-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Markup: Added support type="text/plain" scripts #2442
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
RunDevelopment
wants to merge
7
commits into
PrismJS:master
Choose a base branch
from
RunDevelopment:markup-script-plain-text
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 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fea1223
Markup: Added support type="text/plain" scripts
RunDevelopment 759b51f
Merge branch 'master' into markup-script-plain-text
RunDevelopment df2ebad
Moved attribute logic into its own "language"
RunDevelopment 6e65542
Code golfing
RunDevelopment 4389802
Merge branch 'master' into markup-script-plain-text
RunDevelopment ee0f699
Flattened markup alias assignments
RunDevelopment 5afc2f9
Added modify attribute
RunDevelopment 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| (function (Prism) { | ||
|
|
||
| var tag = Prism.languages.markup['tag']; | ||
|
|
||
| // A general tag attribute. | ||
| var TAG_ATTR = tag.TAG_ATTR; | ||
|
|
||
| /** | ||
| * Replaces all occurrences of `<KEY>` with the value of `replacements[KEY]`. | ||
| * | ||
| * @param {string} string | ||
| * @param {string[] | Object<string, string>} replacements | ||
| * @returns {string} | ||
| */ | ||
| function replace(string, replacements) { | ||
| return string.replace(/<(\w+)>/g, function (m, key) { return '(?:' + replacements[key] + ')'; }); | ||
| } | ||
|
|
||
| /** | ||
| * @typedef TagAttributes | ||
| * @property {string} attr | ||
| * @property {string | undefined} [before=""] | ||
| */ | ||
|
|
||
| /** | ||
| * @param {Attribute[]} attributes | ||
| * @returns {TagAttributes | undefined} | ||
| * | ||
| * @typedef Attribute | ||
| * @property {RegExp} name | ||
| * @property {RegExp} value | ||
| * @property {boolean} [optional=false] | ||
| */ | ||
| function specificAttr(attributes) { | ||
| if (attributes.length === 0) { | ||
| return undefined; | ||
| } | ||
|
|
||
| // We have to do two things: 1) guarantee that non-optional attributes are present and 2) of the attributes that | ||
| // are present, we have to guarantee that they have the correct value. | ||
| // | ||
| // Implementing 1) is relatively easy. After the tag name, we add a lookahead for each non-optional attribute | ||
| // checking that the attribute is present. (We do not check the value here.) | ||
| // | ||
| // Implementing 2) is harder. The basic loop that is used to consume markup attribute looks roughly like the | ||
| // following. In the pseudo pattern, AN is a general pattern of an attribute name and AV is a general attribute | ||
| // value: | ||
| // (\s+AN(=AV)?)* | ||
| // What we need here, is a conditional, so we can treat our target attributes differently and to do this, we use | ||
| // lookaheads again. In the following, AN<i> will i-th target attribute name and AV<i> will be the i-th | ||
| // attribute value: | ||
| // (\s+(AN1=AV1|AN2=AV2|...|(?!AN1|AN2|...)AN(=AV)?))* | ||
|
|
||
| /** | ||
| * @param {Attribute} attr | ||
| */ | ||
| function getNameSource(attr) { | ||
| return attr.name.source; | ||
| } | ||
|
|
||
| // Implement 1) | ||
| var required = ''; | ||
| for (var attr, i = 0; attr = attributes[i++];) { | ||
| if (!attr.optional) { | ||
| required += '(?=(?:\\s*(?:' + TAG_ATTR + '))*?\\s*(?:' + getNameSource(attr) + ')\\s*=)'; | ||
| } | ||
| } | ||
|
|
||
| // Implement 2) | ||
| var attrChoices = '(?!(?:' + attributes.map(getNameSource).join('|') + ')[\\s/>=])(?:' + TAG_ATTR + ')'; | ||
| for (var attr, i = 0; attr = attributes[i++];) { | ||
| var value = replace( | ||
| /\s*=\s*(?:"(?=<VALUE>")[^"]*"|'(?=<VALUE>')[^']*'|(?=<VALUE>[\s/>])[^\s'"/>=]+(?=[\s/>]))/.source, | ||
| { VALUE: attr.value.source } | ||
| ); | ||
| attrChoices += '|' + getNameSource(attr) + value; | ||
| } | ||
|
|
||
| // create the new opening tag pattern | ||
| return { | ||
| attr: attrChoices, | ||
| before: required | ||
| }; | ||
| } | ||
|
|
||
| Object.defineProperties(tag, { | ||
| specificAttr: { value: specificAttr }, | ||
| }); | ||
|
|
||
| tag.addInlined('script', 'none', specificAttr([{ name: /type/, value: /text\/plain/ }])); | ||
|
|
||
| }(Prism)); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.
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.
If what we're doing now is designed primarily for Vue, would it be worth just making this a full-fledged Vue language for Vue SFCs? If we need this more generally for multiple languages, we can extract it then, but
markup-attributesisn't terribly clear as to what the languages actually does.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 think that the modification it makes to markup justifies it being a "language". It would be kinda weird if Vue added support for plain text scripts in HTML. Honestly, the whole thing should be split even further: 1 "language" with just the attributes function and 1 language that adds support for plain text scripts.
Also, it's not like the whole thing is useful for just Vue and HTML. ASP.net could also profit from it but I'll need to make some more adjustments for this.
True. I can't think of a better name thou...