all files / model/ HTMLImporter.js

28.57% Statements 2/7
100% Branches 0/0
33.33% Functions 1/3
28.57% Lines 2/7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65                          387×     387×                                                                                                
import { DefaultDOMElement } from '../dom'
import DOMImporter from './DOMImporter'
 
/*
  Base class for custom HTML importers. If you want to use XML as your
  exchange format see {@link model/XMLImporter}.
 
  @class
  @abstract
*/
class HTMLImporter extends DOMImporter {
 
  constructor(config) {
    super(Object.assign({ idAttribute: 'data-id' }, config))
 
    // only used internally for creating wrapper elements
    this._el = DefaultDOMElement.parseHTML('<html></html>')
  }
 
  importDocument(html) {
    this.reset()
    var parsed = DefaultDOMElement.parseHTML(html)
    this.convertDocument(parsed)
    return this.state.doc
  }
 
  /**
    Orchestrates conversion of a whole document.
 
    This method should be overridden by custom importers to reflect the
    structure of a custom HTML document or fragment, and to control where
    things go to within the document.
 
    @abstract
    @param {ui/DOMElement} documentEl the document element.
 
    @example
 
    When a fragment `<h1>Foo</h1><p></Bar</p>` is imported the implementation
    looks like this.
 
    ```js
      convertDocument(els) {
        this.convertContainer(els, 'body')
      }
    ```
 
    If a full document `<html><body><p>A</p><p>B</p></body></html>` is imported
    you get the `<html>` element instead of a node array.
 
    ```js
      convertDocument(htmlEl) {
        var bodyEl = htmlEl.find('body')
        this.convertContainer(bodyEl.children, 'body')
      }
    ```
  */
  convertDocument(documentEl) { // eslint-disable-line
    throw new Error('This method is abstract')
  }
 
}
 
export default HTMLImporter