C# Class Bloom.Book.HtmlDom

HtmlDom manages the lower-level operations on a Bloom XHTML DOM. These doms can be a whole book, or just one page we're currently editing. They are actually XHTML, though when we save or send to a browser, we always convert to plain html.
Show file Open project: BloomBooks/BloomDesktop Class Usage Examples

Private Properties

Property Type Description
AddKnownStyleIfMissing void
Ensure void
EnsureIdsAreUnique void
GetClasses IEnumerable
MakeJavascriptElement System.Xml.XmlElement
MigrateChildren void
MigrateEditableData string
RemoveStyleSheetIfFound void

Public Methods

Method Description
AddCalendarFoldClassToBody ( XmlDocument dom ) : void
AddClass ( XmlElement e, string className ) : void
AddClassIfMissing ( XmlElement element, string className ) : void
AddCreationType ( string mode ) : void

The Creation Type is either "translation" or "original". This is used to protect fields that should normally not be editable in one or the other. This is a bad name, and we know it!

AddHidePlaceHoldersClassToBody ( XmlDocument dom ) : void
AddJavascriptFile ( string pathToJavascript ) : void
AddJavascriptFileToBody ( string pathToJavascript ) : void
AddPublishClassToBody ( ) : void
AddPublishClassToBody ( XmlDocument dom ) : void

By including this class, we help stylesheets do something different for edit vs. publish mode.

AddRightToLeftClassToBody ( XmlDocument dom ) : void
AddRtlDir ( XmlElement e ) : void
AddStyleSheet ( string path ) : void
AddStyleSheetIfMissing ( string path ) : void
AddStylesheetFromAnotherBook ( HtmlDom sourceBookDom, HtmlDom targetBookDom ) : void
ApplyXSLT ( string pathToXSLT ) : XmlDocument

Applies the XSLT, and returns an XML dom

Clone ( ) : HtmlDom
ConvertHtmlBreaksToNewLines ( string html ) : string
FindChildWithClass ( XmlElement parent, string classVal ) : XmlElement

Find the first child of parent that has the specified class as (one of) its classes.

FindFontsUsedInCss ( string cssContent, HashSet result, bool includeFallbackFonts ) : void

Finds a list of fonts used in the given css

GatherDataBookLanguages ( ) : List

Gives all the unique language codes found in datadiv elements that have data-book

GetAttributeValue ( XmlElement elt, string name ) : string
GetBookSetting ( string key ) : MultiTextBase
GetImageElementUrl ( ElementProxy imgOrDivWithBackgroundImage ) : UrlPathString

Gets the url for the image, either from an img element or any other element that has an inline style with background-image set.

GetImageElementUrl ( GeckoHtmlElement imageElement ) : UrlPathString

Gets the url for the image, either from an img element or any other element that has an inline style with background-image set.

GetImageElementUrl ( XmlElement imageElement ) : UrlPathString

Gets the url for the image, either from an img element or any other element that has an inline style with background-image set.

GetMetaValue ( string name, string defaultValue ) : string
GetOrCreateDataDiv ( XmlNode dom ) : XmlElement
GetTemplateStyleSheets ( ) : IEnumerable
HasMetaElement ( string name ) : bool
HtmlDom ( ) : System
HtmlDom ( XmlDocument domToClone ) : System
HtmlDom ( string xhtml ) : System
IsImgOrSomethingWithBackgroundImage ( XmlElement element ) : bool
MergeClassesIntoNewPage ( XmlElement sourcePage, XmlElement targetPage, string classesToDrop ) : void

Blindly merge the classes from the source into the target.

ProcessPageAfterEditing ( XmlElement destinationPageDiv, XmlElement edittedPageDiv ) : void
RemoveBookSetting ( string key ) : void
RemoveClassesBeginingWith ( XmlElement xmlElement, string classPrefix ) : void
RemoveDirectorySpecificationFromStyleSheetLinks ( ) : void
RemoveExtraBookTitles ( ) : void

Fix BL-2789, where Tok Pisin and Indonesian would show up in the source bubble for book titles, saying the equivalent of "new book" in each language. BasicBook doesn't have that anymore, but this cleans it up in books made from old shells.

RemoveExtraContentTypesMetas ( ) : void
RemoveFileProtocolFromStyleSheetLinks ( ) : void

gecko 11 requires the file://, but modern firefox and chrome can't handle it. Checked also that IE10 works without it.

RemoveMetaElement ( string name ) : void
RemoveMetaElement ( string oldName, Func read, Action write ) : void

Can be called without knowing that the old exists. If it already has the new, the old is just removed. This is just for migration.

RemoveModeStyleSheets ( ) : void
RemoveRtlDir ( XmlElement e ) : void
RemoveXMatterStyleSheets ( ) : void

The chosen xmatter changes, so we need to clear out any old ones

SafeSelectNodes ( string xpath ) : XmlNodeList
SelectChildImgAndBackgroundImageElements ( XmlElement element ) : XmlNodeList
SelectSingleNode ( string xpath ) : XmlElement
SelectSingleNodeHonoringDefaultNS ( string xpath ) : XmlElement
SetBookSetting ( string key, string writingSystemId, string form ) : void
SetElementFromUserStringPreservingLineBreaks ( XmlElement node, string form ) : void
SetImageElementUrl ( ElementProxy imgOrDivWithBackgroundImage, UrlPathString url ) : void

Sets the url attribute either of an img (the src attribute) or a div with an inline style with an background-image rule

SortStyleSheetLinks ( ) : void
UpdateMetaElement ( string name, string value ) : void

creates if necessary, then updates the named in the head of the html

UpdatePageDivs ( ) : void
UpdatePageToTemplate ( HtmlDom pageDom, XmlElement templatePageDiv, string pageId ) : void
ValidateBook ( string descriptionOfBookForErrorLog ) : string

Private Methods

Method Description
AddKnownStyleIfMissing ( XmlNode child ) : void
Ensure ( bool passes, string message, StringBuilder builder ) : void
EnsureIdsAreUnique ( HtmlDom dom, string elementTag, List ids, StringBuilder builder ) : void
GetClasses ( XmlElement element ) : IEnumerable
MakeJavascriptElement ( string pathToJavascript ) : XmlElement
MigrateChildren ( XmlElement page, string parentClass, XmlElement newPage ) : void

For each div in the page which has the specified class, find the corresponding div with that class in newPage, and replace its contents with the contents of the source page. Also inserts any needed styles we know about.

MigrateEditableData ( XmlElement page, XmlElement template, string lineage ) : string

Replace page in its parent with an element which is a clone of template, but with the contents of page transferred as far as possible. Retain the id of the page. Set its lineage to the supplied value

RemoveStyleSheetIfFound ( string path ) : void

Method Details

AddCalendarFoldClassToBody() public static method

public static AddCalendarFoldClassToBody ( XmlDocument dom ) : void
dom System.Xml.XmlDocument
return void

AddClass() public static method

public static AddClass ( XmlElement e, string className ) : void
e System.Xml.XmlElement
className string
return void

AddClassIfMissing() public static method

public static AddClassIfMissing ( XmlElement element, string className ) : void
element System.Xml.XmlElement
className string
return void

AddCreationType() public method

The Creation Type is either "translation" or "original". This is used to protect fields that should normally not be editable in one or the other. This is a bad name, and we know it!
public AddCreationType ( string mode ) : void
mode string
return void

AddHidePlaceHoldersClassToBody() public static method

public static AddHidePlaceHoldersClassToBody ( XmlDocument dom ) : void
dom System.Xml.XmlDocument
return void

AddJavascriptFile() public method

public AddJavascriptFile ( string pathToJavascript ) : void
pathToJavascript string
return void

AddJavascriptFileToBody() public method

public AddJavascriptFileToBody ( string pathToJavascript ) : void
pathToJavascript string
return void

AddPublishClassToBody() public method

public AddPublishClassToBody ( ) : void
return void

AddPublishClassToBody() public static method

By including this class, we help stylesheets do something different for edit vs. publish mode.
public static AddPublishClassToBody ( XmlDocument dom ) : void
dom System.Xml.XmlDocument
return void

AddRightToLeftClassToBody() public static method

public static AddRightToLeftClassToBody ( XmlDocument dom ) : void
dom System.Xml.XmlDocument
return void

AddRtlDir() public static method

public static AddRtlDir ( XmlElement e ) : void
e System.Xml.XmlElement
return void

AddStyleSheet() public method

public AddStyleSheet ( string path ) : void
path string
return void

AddStyleSheetIfMissing() public method

public AddStyleSheetIfMissing ( string path ) : void
path string
return void

AddStylesheetFromAnotherBook() public static method

public static AddStylesheetFromAnotherBook ( HtmlDom sourceBookDom, HtmlDom targetBookDom ) : void
sourceBookDom HtmlDom
targetBookDom HtmlDom
return void

ApplyXSLT() public method

Applies the XSLT, and returns an XML dom
public ApplyXSLT ( string pathToXSLT ) : XmlDocument
pathToXSLT string
return System.Xml.XmlDocument

Clone() public method

public Clone ( ) : HtmlDom
return HtmlDom

ConvertHtmlBreaksToNewLines() public static method

public static ConvertHtmlBreaksToNewLines ( string html ) : string
html string
return string

FindChildWithClass() public static method

Find the first child of parent that has the specified class as (one of) its classes.
public static FindChildWithClass ( XmlElement parent, string classVal ) : XmlElement
parent System.Xml.XmlElement
classVal string
return System.Xml.XmlElement

FindFontsUsedInCss() public static method

Finds a list of fonts used in the given css
public static FindFontsUsedInCss ( string cssContent, HashSet result, bool includeFallbackFonts ) : void
cssContent string
result HashSet
includeFallbackFonts bool true to include fallback fonts, false to include only the first font in each font family
return void

GatherDataBookLanguages() public method

Gives all the unique language codes found in datadiv elements that have data-book
public GatherDataBookLanguages ( ) : List
return List

GetAttributeValue() public static method

public static GetAttributeValue ( XmlElement elt, string name ) : string
elt System.Xml.XmlElement
name string
return string

GetBookSetting() public method

public GetBookSetting ( string key ) : MultiTextBase
key string
return MultiTextBase

GetImageElementUrl() public static method

Gets the url for the image, either from an img element or any other element that has an inline style with background-image set.
public static GetImageElementUrl ( ElementProxy imgOrDivWithBackgroundImage ) : UrlPathString
imgOrDivWithBackgroundImage ElementProxy
return UrlPathString

GetImageElementUrl() public static method

Gets the url for the image, either from an img element or any other element that has an inline style with background-image set.
public static GetImageElementUrl ( GeckoHtmlElement imageElement ) : UrlPathString
imageElement GeckoHtmlElement
return UrlPathString

GetImageElementUrl() public static method

Gets the url for the image, either from an img element or any other element that has an inline style with background-image set.
public static GetImageElementUrl ( XmlElement imageElement ) : UrlPathString
imageElement System.Xml.XmlElement
return UrlPathString

GetMetaValue() public method

public GetMetaValue ( string name, string defaultValue ) : string
name string
defaultValue string
return string

GetOrCreateDataDiv() public static method

public static GetOrCreateDataDiv ( XmlNode dom ) : XmlElement
dom System.Xml.XmlNode
return System.Xml.XmlElement

GetTemplateStyleSheets() public method

public GetTemplateStyleSheets ( ) : IEnumerable
return IEnumerable

HasMetaElement() public method

public HasMetaElement ( string name ) : bool
name string
return bool

HtmlDom() public method

public HtmlDom ( ) : System
return System

HtmlDom() public method

public HtmlDom ( XmlDocument domToClone ) : System
domToClone System.Xml.XmlDocument
return System

HtmlDom() public method

public HtmlDom ( string xhtml ) : System
xhtml string
return System

IsImgOrSomethingWithBackgroundImage() public static method

public static IsImgOrSomethingWithBackgroundImage ( XmlElement element ) : bool
element System.Xml.XmlElement
return bool

MergeClassesIntoNewPage() public static method

Blindly merge the classes from the source into the target.
public static MergeClassesIntoNewPage ( XmlElement sourcePage, XmlElement targetPage, string classesToDrop ) : void
sourcePage System.Xml.XmlElement
targetPage System.Xml.XmlElement
classesToDrop string
return void

ProcessPageAfterEditing() public static method

public static ProcessPageAfterEditing ( XmlElement destinationPageDiv, XmlElement edittedPageDiv ) : void
destinationPageDiv System.Xml.XmlElement
edittedPageDiv System.Xml.XmlElement
return void

RemoveBookSetting() public method

public RemoveBookSetting ( string key ) : void
key string
return void

RemoveClassesBeginingWith() public static method

public static RemoveClassesBeginingWith ( XmlElement xmlElement, string classPrefix ) : void
xmlElement System.Xml.XmlElement
classPrefix string
return void

RemoveDirectorySpecificationFromStyleSheetLinks() public method

public RemoveDirectorySpecificationFromStyleSheetLinks ( ) : void
return void

RemoveExtraBookTitles() public method

Fix BL-2789, where Tok Pisin and Indonesian would show up in the source bubble for book titles, saying the equivalent of "new book" in each language. BasicBook doesn't have that anymore, but this cleans it up in books made from old shells.
public RemoveExtraBookTitles ( ) : void
return void

RemoveExtraContentTypesMetas() public method

public RemoveExtraContentTypesMetas ( ) : void
return void

RemoveFileProtocolFromStyleSheetLinks() public method

gecko 11 requires the file://, but modern firefox and chrome can't handle it. Checked also that IE10 works without it.
public RemoveFileProtocolFromStyleSheetLinks ( ) : void
return void

RemoveMetaElement() public method

public RemoveMetaElement ( string name ) : void
name string
return void

RemoveMetaElement() public method

Can be called without knowing that the old exists. If it already has the new, the old is just removed. This is just for migration.
public RemoveMetaElement ( string oldName, Func read, Action write ) : void
oldName string
read Func
write Action
return void

RemoveModeStyleSheets() public method

public RemoveModeStyleSheets ( ) : void
return void

RemoveRtlDir() public static method

public static RemoveRtlDir ( XmlElement e ) : void
e System.Xml.XmlElement
return void

RemoveXMatterStyleSheets() public method

The chosen xmatter changes, so we need to clear out any old ones
public RemoveXMatterStyleSheets ( ) : void
return void

SafeSelectNodes() public method

public SafeSelectNodes ( string xpath ) : XmlNodeList
xpath string
return System.Xml.XmlNodeList

SelectChildImgAndBackgroundImageElements() public static method

public static SelectChildImgAndBackgroundImageElements ( XmlElement element ) : XmlNodeList
element System.Xml.XmlElement
return System.Xml.XmlNodeList

SelectSingleNode() public method

public SelectSingleNode ( string xpath ) : XmlElement
xpath string
return System.Xml.XmlElement

SelectSingleNodeHonoringDefaultNS() public method

public SelectSingleNodeHonoringDefaultNS ( string xpath ) : XmlElement
xpath string
return System.Xml.XmlElement

SetBookSetting() public method

public SetBookSetting ( string key, string writingSystemId, string form ) : void
key string
writingSystemId string
form string
return void

SetElementFromUserStringPreservingLineBreaks() public static method

public static SetElementFromUserStringPreservingLineBreaks ( XmlElement node, string form ) : void
node System.Xml.XmlElement
form string
return void

SetImageElementUrl() public static method

Sets the url attribute either of an img (the src attribute) or a div with an inline style with an background-image rule
public static SetImageElementUrl ( ElementProxy imgOrDivWithBackgroundImage, UrlPathString url ) : void
imgOrDivWithBackgroundImage ElementProxy
url UrlPathString
return void

SortStyleSheetLinks() public method

public SortStyleSheetLinks ( ) : void
return void

UpdateMetaElement() public method

creates if necessary, then updates the named in the head of the html
public UpdateMetaElement ( string name, string value ) : void
name string
value string
return void

UpdatePageDivs() public method

public UpdatePageDivs ( ) : void
return void

UpdatePageToTemplate() public method

public UpdatePageToTemplate ( HtmlDom pageDom, XmlElement templatePageDiv, string pageId ) : void
pageDom HtmlDom
templatePageDiv System.Xml.XmlElement
pageId string
return void

ValidateBook() public method

public ValidateBook ( string descriptionOfBookForErrorLog ) : string
descriptionOfBookForErrorLog string
return string