This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix style handling in convertFromHTMLToContentBlocks
Summary: # The Problem `convertFromHTMLToContentBlocks` currently mishandles styles pretty significantly. It has a `currentStyle` attribute that is mutated as the HTML is processed. When processing a node, it determines what styles that node should have (by unioning styes inferred from the tag and styles inferred from the style attribute) and adds them to `currentStyle`. After it's done processing the node, it removes whatever it added. This has several problems: ## Styles are *never* removed before rendering the children of a node For example, if you have the following content: ``` <span style="font-weight: bold"> <span style="font-weight: normal"> Not bold! </span> </span> ``` then the "Not bold!" text will actually be given the `'BOLD'` style. ## Styles are sometimes removed when they shouldn't be For example, if you have the following content: ``` <span style="font-weight: bold"> <span style="font-weight: bold"> Bold! </span> <span> Still bold! </span> </span> ``` then the "Still bold!" text will *not* be given the `'BOLD'` style. ## Styles from tag override styles from the style attribute For example, if you have the following content: ``` <b style="font-weight: normal">Not bold!</b> ``` then "Not bold!" will be given the `'BOLD'` style. (In all browsers I've tested, this content would render as the normal font weight, so I'm assuming this should not have the `'BOLD'` style.) # The Fix Using a single `OrderedSet` of styles that is mutated as you process nodes can't fix the second issue mentioned above, because it simply does not store enough info to be able to know what styles it should be using at any given time. So instead of using this attribute, I'm adding a `style` param to `_toBlockConfigs`. When a node has some styling applied to it, we just make a copy of the style object (implicitly, since it's an immutable `OrderedSet`) with the appropriate styles added or removed. When we're done processing the children of that node, we don't need to do any cleanup because the updated style object is simply thrown away. In addition, we add styles from looking at tags before adding/removing styles based on the actual style attribute. This allows `<b style="font-weight: normal">Not bold!</b>` to actually render as non-bold text. Reviewed By: mrkev Differential Revision: D19295374 fbshipit-source-id: 0ba040a9a19de36d716a4fcd6f55bf57077f001e
- Loading branch information