Add \class and \cssId on non-strict mode#1437
Add \class and \cssId on non-strict mode#1437ylemkimon wants to merge 8 commits intoKaTeX:masterfrom ylemkimon:html-extension
Conversation
* Added `string` group type, which passes raw string argument
Codecov Report
@@ Coverage Diff @@
## master #1437 +/- ##
=========================================
- Coverage 82.57% 82.2% -0.38%
=========================================
Files 77 78 +1
Lines 4408 4406 -2
Branches 772 771 -1
=========================================
- Hits 3640 3622 -18
- Misses 665 676 +11
- Partials 103 108 +5
Continue to review full report at Codecov.
|
| if (lastToken.text === "{") { | ||
| nest += 1; | ||
| } else if (lastToken.text === "}") { | ||
| if (nest <= 0) { |
There was a problem hiding this comment.
This was unreachable, as while is terminated if nest === 0 && this.nextToken.text === groupEnd.
|
As @kevinbarabash once reminded me, some people may want to protect their web site from users adding classes. Maybe this (terrific) code should go behind its own rendering option |
|
|
||
| defineFunction({ | ||
| type: "html", | ||
| names: ["\\class", "\\cssId"], |
There was a problem hiding this comment.
While I know mathjax calls this "cssId", I think it would be nice to give it a more generic name (i.e. "id"). My only reasoning is, if multiple renderers end up being supported, css may have no meaning (i.e. in a canvas).
For a similar reason, I would give this file a more generic name as well (i.e. identifiers.js)
There was a problem hiding this comment.
@rrandallcainc were you thinking that id might be useful for hit testing in canvas based renderer.
There was a problem hiding this comment.
@kevinbarabash Hit testing could be one use case. I was more thinking along the lines of being able to manipulate specific parts of the LaTeX separately. For example, let's say I had \frac{\id{numerator}{a}}{\id{denominator}{b}} and I wanted to switch the two. I see as id/class as general ways of targeting specific areas of the latex.
| names: ["\\class", "\\cssId"], | ||
| props: { | ||
| numArgs: 2, | ||
| argTypes: ["string", "original"], |
There was a problem hiding this comment.
I'm assuming we want this allowed in text?
There was a problem hiding this comment.
@rrandallcainc Set allowedInText true.
There was a problem hiding this comment.
Assuming you'll want to apply allowInText: true also to html, no?
|
@ronkok I'm not sure how |
Styles are predefined, that's true. But a malicious user, writing in an input box like the one I am writing in now, could mis-apply existing CSS style rules to places where they should not be. An administrator of a site like this one might want to restrict that. @ylemkimon I think this PR is terrific, and I want to see it succeed. I only write to suggest a way that everybody can be happy with it. |
|
Curious how "reserved" classes/id's are handled. For example, if someone tried Also, do you plan on adding tests for this? |
|
My guess is that |
| /** | ||
| * Parses a url string. | ||
| */ | ||
| parseUrlGroup(optional: boolean): ?ParsedArg { |
| "html": {| | ||
| type: "html", | ||
| class?: string, | ||
| cssId?: string, |
There was a problem hiding this comment.
Maybe we could change this be:
type: "html"
attribute: "class" | "id",
value: string,
body: AnyParseNode[],
There was a problem hiding this comment.
@kevinbarabash I thought there may be use cases of setting both class and id.
There was a problem hiding this comment.
But wouldn't there be separate commands, e.g. \class{foo}{\id{bar}{content}}?
| "string": {| | ||
| type: "string", | ||
| value: string, | ||
| |}, |
There was a problem hiding this comment.
Also for the first argument of \cssId and \class.
| math.setAttribute('class', group.value.class); | ||
| } | ||
| if (group.value.cssId) { | ||
| math.setAttribute('id', group.value.cssId); |
There was a problem hiding this comment.
I've found that browsers tend to get confused about things when there are multiple elements with the same id.
There was a problem hiding this comment.
Does it apply to the MathML elements, too?
There was a problem hiding this comment.
If you query for an element by id you can get an HTML element, SVG element, MathML element, etc.
|
@ronkok @ry-randall Another way of limiting their usage is allowing only those with specific prefix, like MathJax safe extension. What do you think? |
|
@ylemkimon I don't really have a strong personal opinion on this issue. I spoke up earlier to make you aware of a comment that @kevinbarabash had once made to me. Anything he likes on this issue will be okay with me, too. |
|
@ylemkimon I'm not too familiar with the mathjax safe extension. Do you mean something like Edit: I just took a look @ http://docs.mathjax.org/en/latest/options/other/Safe.html#configure-safe and found what you're referring to. I think that's the right approach, for sure. Others thoughts? |
|
Hmm, related to #1436 then I guess. Is there a nice uniform solution? It'd be cool to be able to specify |
|
P.S. I do worry about defining an |
I think the limitation should be at the command level. We should disable |
What if a web site administrator wants protection from a malicious user? Would the limitation be better if set as a KaTeX rendering option? |
|
Perhaps we should add a new option |
What I meant by this is that I don't think we should try to filter things in/out by some id/class prefix. I think we should have a |
|
I think we can go with |
|
@ylemkimon the |
|
Agree that a naming strategy for options/settings is a good idea. |
|
For this to move forward it sounds like we need:
|
|
Hi @ylemkimon , As #1706 and #1711 are both merged by now, do you see anything blocking this? Is #1771 blocking this or could a (disabled by default) option (e.g: I am currently needing this feature for a WYSIWYG editor, would be glad to give a hand! |
|
@dvergeylen Yes, this is blocked on #1771, or more precisely, #1794. We don't want to add a bunch of options and then switch to a different mechanic. Your feedback on #1794 would be appreciated! |
|
While we are waiting for #1794, is there a workaround to add styling to an element that corresponds to a substring of tex input? Id is overkill for this and so is class. For example, there could be something like \localId{id1}{x^2} which associates id1 with an element implementing the x^2 substring. We could have an option passed into render that supplies a map or object to be populated with these ids. Then whatever permission the page has to style elements, it can apply to these elements. Thanks. |
|
I think this is still blocked, but by #1794 now. |
|
This is no longer blocked. |
|
Closing in preference of #2082. |
stringgroup type, which passes raw string argument