aboutsummaryrefslogblamecommitdiffstats
path: root/src/construct/label_start_image.rs
blob: 8d35df2c8a091258f542c1449495893e520e1a9b (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
                                                            
   



                                                                            




                                 

           


                                                                           
                                                                                



                                                     


                                                    





                                                                                                                                                                 
                                  
                                            
                                                                                            
 
                       
                                        
                                             
                                              



                                 

                
       
                                                  







                                                                                              


     
                      

               

                
       

                                                 
                       
                                               
                                

                                              
                                                                   
                                                                                

                                
                                                                   
                     
         
                        

     
//! Label start (image) occurs in the [text][] content type.
//!
//! ## Grammar
//!
//! Label start (image) forms with the following BNF
//! (<small>see [construct][crate::construct] for character groups</small>):
//!
//! ```bnf
//! label_start_image ::= '!' '['
//! ```
//!
//! ## HTML
//!
//! Label start (image) does not, on its own, relate to anything in HTML.
//! When matched with a [label end][label_end], they together relate to the
//! `<img>` element in HTML.
//! See [*§ 4.8.3 The `img` element*][html_img] in the HTML spec for more info.
//! Without an end, the characters (`![`) are output.
//!
//! ## Tokens
//!
//! *   [`LabelImage`][Name::LabelImage]
//! *   [`LabelImageMarker`][Name::LabelImageMarker]
//! *   [`LabelMarker`][Name::LabelMarker]
//!
//! ## References
//!
//! *   [`label-start-image.js` in `micromark`](https://github.com/micromark/micromark/blob/main/packages/micromark-core-commonmark/dev/lib/label-start-image.js)
//! *   [*§ 6.4 Images* in `CommonMark`](https://spec.commonmark.org/0.30/#images)
//!
//! [text]: crate::construct::text
//! [label_end]: crate::construct::label_end
//! [html_img]: https://html.spec.whatwg.org/multipage/embedded-content.html#the-img-element

use crate::event::Name;
use crate::resolve::Name as ResolveName;
use crate::state::{Name as StateName, State};
use crate::tokenizer::{LabelStart, Tokenizer};

/// Start of label (image) start.
///
/// ```markdown
/// > | a ![b] c
///       ^
/// ```
pub fn start(tokenizer: &mut Tokenizer) -> State {
    if tokenizer.parse_state.constructs.label_start_image && tokenizer.current == Some(b'!') {
        tokenizer.enter(Name::LabelImage);
        tokenizer.enter(Name::LabelImageMarker);
        tokenizer.consume();
        tokenizer.exit(Name::LabelImageMarker);
        State::Next(StateName::LabelStartImageOpen)
    } else {
        State::Nok
    }
}

/// After `!`, at `[`.
///
/// ```markdown
/// > | a ![b] c
///        ^
/// ```
pub fn open(tokenizer: &mut Tokenizer) -> State {
    match tokenizer.current {
        Some(b'[') => {
            tokenizer.enter(Name::LabelMarker);
            tokenizer.consume();
            tokenizer.exit(Name::LabelMarker);
            tokenizer.exit(Name::LabelImage);
            tokenizer.tokenize_state.label_starts.push(LabelStart {
                start: (tokenizer.events.len() - 6, tokenizer.events.len() - 1),
                inactive: false,
            });
            tokenizer.register_resolver_before(ResolveName::Label);
            State::Ok
        }
        _ => State::Nok,
    }
}