public static InlineCodeExtractionResult ConvertHtmlTagsInInLineCodes(this string htmlText) { var doc = new HtmlDocument(); doc.LoadHtml(htmlText); var text = new List <ResourceStringContent>(); var originalData = new Dictionary <String, String>(); int tagCounter = 0; int originalDataCounter = 1; foreach (var node in doc.DocumentNode.ChildNodes) { if (node.NodeType == HtmlNodeType.Text) { text.Add(new PlainText(node.InnerText)); } if (node.NodeType == HtmlNodeType.Element && node.HasChildNodes) { var inlineCodeType = Map.GetValueOrDefault(node.Name); if (inlineCodeType.Type == null) { text.Add(new PlainText(node.InnerText)); } else { tagCounter++; var startTagId = AddOriginalData(originalData, "<" + node.Name + ">", ref originalDataCounter); var endTagId = AddOriginalData(originalData, "</" + node.Name + ">", ref originalDataCounter); var pc = new SpanningCode(tagCounter.ToString()); pc.DataReferenceStart = startTagId; pc.DataReferenceEnd = endTagId; pc.Text.Add(new PlainText(node.InnerText)); pc.Type = inlineCodeType.Type; pc.SubType = inlineCodeType.Subtype; text.Add(pc); } } if (node.NodeType == HtmlNodeType.Element && !node.HasChildNodes) { var inlineCodeType = Map.GetValueOrDefault(node.Name); if (inlineCodeType.Type == null) { continue; } tagCounter++; var tagId = AddOriginalData(originalData, "<" + node.Name + "/>", ref originalDataCounter); var ph = new StandaloneCode(tagCounter.ToString()); ph.DataReference = tagId; ph.Type = inlineCodeType.Type; ph.SubType = inlineCodeType.Subtype; text.Add(ph); } } return(new InlineCodeExtractionResult() { OriginalData = originalData, Text = text }); }
public void XliffElement_CollapseChildren() { List <PlainText> textList; List <ResourceStringContent> tagList; List <Source> sourceList; List <Target> targetList; List <XliffElement> elementList; PlainText text; Segment segment; StandaloneCode code; Unit unit; XliffDocument document; code = new StandaloneCode(); text = new PlainText(); segment = new Segment(); segment.Source = new Source(); segment.Source.Text.Add(text); segment.Source.Text.Add(code); unit = new Unit(); unit.Resources.Add(segment); document = new XliffDocument(); document.Files.Add(new File()); document.Files[0].Containers.Add(unit); elementList = document.CollapseChildren <XliffElement>(); Assert.AreEqual(6, elementList.Count, "Element count is incorrect."); Assert.IsTrue(elementList.Contains(code), "StandaloneCode not found in list"); Assert.IsFalse(elementList.Contains(document), "Document not found in list"); Assert.IsTrue(elementList.Contains(document.Files[0]), "File not found in list"); Assert.IsTrue(elementList.Contains(segment), "Segment not found in list"); Assert.IsTrue(elementList.Contains(segment.Source), "Source not found in list"); Assert.IsTrue(elementList.Contains(text), "PlainText not found in list"); Assert.IsTrue(elementList.Contains(unit), "Unit not found in list"); sourceList = document.CollapseChildren <Source>(); Assert.AreEqual(1, sourceList.Count, "Element count is incorrect."); Assert.IsTrue(sourceList.Contains(segment.Source), "Source not found in list"); tagList = document.CollapseChildren <ResourceStringContent>(); Assert.AreEqual(2, tagList.Count, "Element count is incorrect."); Assert.IsTrue(tagList.Contains(code), "StandaloneCode not found in list"); Assert.IsTrue(tagList.Contains(text), "PlainText not found in list"); targetList = document.CollapseChildren <Target>(); Assert.AreEqual(0, targetList.Count, "Element count is incorrect."); textList = document.CollapseChildren <PlainText>(); Assert.AreEqual(1, textList.Count, "Element count is incorrect."); Assert.IsTrue(textList.Contains(text), "PlainText not found in list"); }
public void PhWithImageTypeGetsBackToHtmlImgTag() { var xliffCode = new List <ResourceStringContent>(); var ph = new StandaloneCode("1"); ph.Type = CodeType.Image; xliffCode.Add(ph); var htmlString = xliffCode.ConvertToHtml(); Assert.AreEqual("<img/>", htmlString); }
public void PhWithLinebreakSubtypeGetsBackToHtmlBrTag() { var xliffCode = new List <ResourceStringContent>(); var ph = new StandaloneCode("1"); ph.Type = CodeType.Formatting; ph.SubType = "xlf:lb"; xliffCode.Add(ph); var htmlString = xliffCode.ConvertToHtml(); Assert.AreEqual("<br/>", htmlString); }
public void PhWithOneSubFlowGetsBackToHtmlWithAttributes() { var xliffCode = new List <ResourceStringContent>(); var ph = new StandaloneCode("1"); ph.Type = CodeType.Image; ph.SubFlows = "u1-5-1-src"; xliffCode.Add(ph); var subflows = new Dictionary <string, string>() { { "u1-5-1-src", "media\\12345\\image.jpg" } }; var htmlString = xliffCode.ConvertToHtml(s => SearchSubflowsInDictionary(s, subflows)); Assert.AreEqual("<img src=\"media\\12345\\image.jpg\"/>", htmlString); }
public static InlineCodeExtractionResult ConvertHtmlTagsInInLineCodes(this string htmlText, string subflowIdPrefix = "") { var doc = new HtmlDocument(); doc.LoadHtml(htmlText); var text = new List <ResourceStringContent>(); var originalData = new Dictionary <String, String>(); var allAttributes = new Dictionary <String, String>(); int tagCounter = 0; int originalDataCounter = 1; foreach (var node in doc.DocumentNode.ChildNodes) { var currentNodeAttributeList = node.ExtractAttributes(); if (node.NodeType == HtmlNodeType.Text) { text.Add(new PlainText(node.InnerText)); } if (node.NodeType == HtmlNodeType.Element && node.HasChildNodes) { var inlineCodeType = ExtractInlineCodeType(node.Name); if (inlineCodeType.Type == null) { text.Add(new PlainText(node.InnerText)); } else { tagCounter++; var startTagId = AddOriginalData(originalData, node.Name, currentNodeAttributeList, TagType.StartTag, ref originalDataCounter); var endTagId = AddOriginalData(originalData, node.Name, currentNodeAttributeList, TagType.EndTag, ref originalDataCounter); currentNodeAttributeList = PrepareAttributesAsSubflow(currentNodeAttributeList, tagCounter, subflowIdPrefix); allAttributes.AddAll(currentNodeAttributeList); var pc = new SpanningCode(tagCounter.ToString()); pc.DataReferenceStart = startTagId; pc.DataReferenceEnd = endTagId; pc.Text.Add(new PlainText(node.InnerText)); pc.Type = inlineCodeType.Type; pc.SubType = inlineCodeType.Subtype; if (currentNodeAttributeList.Count > 0) { pc.SubFlowsStart = string.Join(" ", currentNodeAttributeList.Keys); } text.Add(pc); } } if (node.NodeType == HtmlNodeType.Element && !node.HasChildNodes) { var inlineCodeType = ExtractInlineCodeType(node.Name); if (inlineCodeType.Type == null) { continue; } tagCounter++; var tagId = AddOriginalData(originalData, node.Name, currentNodeAttributeList, TagType.Standalone, ref originalDataCounter); currentNodeAttributeList = PrepareAttributesAsSubflow(currentNodeAttributeList, tagCounter); allAttributes.AddAll(currentNodeAttributeList); var ph = new StandaloneCode(tagCounter.ToString()); ph.DataReference = tagId; ph.Type = inlineCodeType.Type; ph.SubType = inlineCodeType.Subtype; if (currentNodeAttributeList.Count > 0) { ph.SubFlows = string.Join(" ", currentNodeAttributeList.Keys); } text.Add(ph); } } return(new InlineCodeExtractionResult() { OriginalData = originalData, Text = text, SubFlow = allAttributes }); }
public void TestInitialize() { this._element = new StandaloneCode(); this._provider = this._element; }
public void XliffElement_CollapseChildren() { List<PlainText> textList; List<ResourceStringContent> tagList; List<Source> sourceList; List<Target> targetList; List<XliffElement> elementList; PlainText text; Segment segment; StandaloneCode code; Unit unit; XliffDocument document; code = new StandaloneCode(); text = new PlainText(); segment = new Segment(); segment.Source = new Source(); segment.Source.Text.Add(text); segment.Source.Text.Add(code); unit = new Unit(); unit.Resources.Add(segment); document = new XliffDocument(); document.Files.Add(new File()); document.Files[0].Containers.Add(unit); elementList = document.CollapseChildren<XliffElement>(); Assert.AreEqual(6, elementList.Count, "Element count is incorrect."); Assert.IsTrue(elementList.Contains(code), "StandaloneCode not found in list"); Assert.IsFalse(elementList.Contains(document), "Document not found in list"); Assert.IsTrue(elementList.Contains(document.Files[0]), "File not found in list"); Assert.IsTrue(elementList.Contains(segment), "Segment not found in list"); Assert.IsTrue(elementList.Contains(segment.Source), "Source not found in list"); Assert.IsTrue(elementList.Contains(text), "PlainText not found in list"); Assert.IsTrue(elementList.Contains(unit), "Unit not found in list"); sourceList = document.CollapseChildren<Source>(); Assert.AreEqual(1, sourceList.Count, "Element count is incorrect."); Assert.IsTrue(sourceList.Contains(segment.Source), "Source not found in list"); tagList = document.CollapseChildren<ResourceStringContent>(); Assert.AreEqual(2, tagList.Count, "Element count is incorrect."); Assert.IsTrue(tagList.Contains(code), "StandaloneCode not found in list"); Assert.IsTrue(tagList.Contains(text), "PlainText not found in list"); targetList = document.CollapseChildren<Target>(); Assert.AreEqual(0, targetList.Count, "Element count is incorrect."); textList = document.CollapseChildren<PlainText>(); Assert.AreEqual(1, textList.Count, "Element count is incorrect."); Assert.IsTrue(textList.Contains(text), "PlainText not found in list"); }
public void XliffElement_CollapseChildrenWithScope() { IExtensible extensible; CustomExtension extension1; CustomExtension extension2; CustomElement1 element1; CustomElement2 element2; List<XliffElement> elementList; List<Source> sourceList; PlainText text; Segment segment; Source source; StandaloneCode code; Unit unit; XliffDocument document; code = new StandaloneCode(); text = new PlainText(); segment = new Segment(); segment.Source = new Source(); segment.Source.Text.Add(text); segment.Source.Text.Add(code); unit = new Unit(); unit.Resources.Add(segment); document = new XliffDocument(); document.Files.Add(new File()); document.Files[0].Containers.Add(unit); extensible = document.Files[0]; extension1 = new CustomExtension("namespace"); element1 = new CustomElement1("pre1", "namespace"); extension1.AddChild(new ElementInfo(new XmlNameInfo("localname"), element1)); extensible.Extensions.Add(extension1); extension2 = new CustomExtension("namespace"); element2 = new CustomElement2("pre1", "namespace"); extension2.AddChild(new ElementInfo(new XmlNameInfo("localname"), element2)); source = new Source(); extension2.AddChild(new ElementInfo(new XmlNameInfo("localname"), source)); extensible.Extensions.Add(extension2); Console.WriteLine("Test with none."); elementList = document.CollapseChildren<XliffElement>(CollapseScope.None); Assert.AreEqual(0, elementList.Count, "Element count is incorrect."); Console.WriteLine("Test with current element."); elementList = document.CollapseChildren<XliffElement>(CollapseScope.CurrentElement); Assert.AreEqual(1, elementList.Count, "Element count is incorrect."); Assert.IsTrue(elementList.Contains(document), "Document not found in list"); Console.WriteLine("Test with default."); elementList = document.CollapseChildren<XliffElement>(CollapseScope.Default); Assert.AreEqual(6, elementList.Count, "Element count is incorrect."); Assert.IsTrue(elementList.Contains(code), "StandaloneCode not found in list"); Assert.IsFalse(elementList.Contains(document), "Document not found in list"); Assert.IsTrue(elementList.Contains(document.Files[0]), "File not found in list"); Assert.IsTrue(elementList.Contains(segment), "Segment not found in list"); Assert.IsTrue(elementList.Contains(segment.Source), "Source not found in list"); Assert.IsTrue(elementList.Contains(text), "PlainText not found in list"); Assert.IsTrue(elementList.Contains(unit), "Unit not found in list"); Console.WriteLine("Test with default with current element."); elementList = document.CollapseChildren<XliffElement>(CollapseScope.Default | CollapseScope.CurrentElement); Assert.AreEqual(7, elementList.Count, "Element count is incorrect."); Assert.IsTrue(elementList.Contains(document), "Document not found in list"); Assert.IsTrue(elementList.Contains(code), "StandaloneCode not found in list"); Assert.IsTrue(elementList.Contains(document.Files[0]), "File not found in list"); Assert.IsTrue(elementList.Contains(segment), "Segment not found in list"); Assert.IsTrue(elementList.Contains(segment.Source), "Source not found in list"); Assert.IsTrue(elementList.Contains(text), "PlainText not found in list"); Assert.IsTrue(elementList.Contains(unit), "Unit not found in list"); elementList = document.CollapseChildren<XliffElement>(CollapseScope.Extensions | CollapseScope.CoreElements | CollapseScope.AllDescendants); Assert.AreEqual(9, elementList.Count, "Element count is incorrect."); Assert.IsTrue(elementList.Contains(code), "StandaloneCode not found in list"); Assert.IsTrue(elementList.Contains(document.Files[0]), "File not found in list"); Assert.IsTrue(elementList.Contains(segment), "Segment not found in list"); Assert.IsTrue(elementList.Contains(segment.Source), "Source not found in list"); Assert.IsTrue(elementList.Contains(text), "PlainText not found in list"); Assert.IsTrue(elementList.Contains(unit), "Unit not found in list"); Assert.IsTrue(elementList.Contains(source), "Source not found in list"); Assert.IsTrue(elementList.Contains(element1), "Element1 not found in list"); Assert.IsTrue(elementList.Contains(element2), "Element2 not found in list"); sourceList = document.CollapseChildren<Source>(CollapseScope.Extensions | CollapseScope.CoreElements | CollapseScope.AllDescendants); Assert.AreEqual(2, sourceList.Count, "Element count is incorrect."); Assert.IsTrue(sourceList.Contains(segment.Source), "Source not found in list"); Assert.IsTrue(sourceList.Contains(source), "Source not found in list"); elementList = document.CollapseChildren<XliffElement>(CollapseScope.All); Assert.AreEqual(10, elementList.Count, "Element count is incorrect."); Assert.IsTrue(elementList.Contains(document), "Document not found in list"); Assert.IsTrue(elementList.Contains(code), "StandaloneCode not found in list"); Assert.IsTrue(elementList.Contains(document.Files[0]), "File not found in list"); Assert.IsTrue(elementList.Contains(segment), "Segment not found in list"); Assert.IsTrue(elementList.Contains(segment.Source), "Source not found in list"); Assert.IsTrue(elementList.Contains(text), "PlainText not found in list"); Assert.IsTrue(elementList.Contains(unit), "Unit not found in list"); Assert.IsTrue(elementList.Contains(source), "Source not found in list"); Assert.IsTrue(elementList.Contains(element1), "Element1 not found in list"); Assert.IsTrue(elementList.Contains(element2), "Element2 not found in list"); sourceList = document.CollapseChildren<Source>(CollapseScope.All); Assert.AreEqual(2, sourceList.Count, "Element count is incorrect."); Assert.IsTrue(sourceList.Contains(source), "Source not found in list"); Assert.IsTrue(sourceList.Contains(segment.Source), "Source not found in list"); Console.WriteLine("Test with extensions with core elements for Source."); sourceList = document.CollapseChildren<Source>(CollapseScope.Extensions | CollapseScope.AllDescendants); Assert.AreEqual(1, sourceList.Count, "Element count is incorrect."); Assert.IsTrue(sourceList.Contains(source), "Source not found in list"); Console.WriteLine("Test with extensions without core elements."); elementList = document.CollapseChildren<XliffElement>(CollapseScope.Extensions | CollapseScope.AllDescendants); Assert.AreEqual(3, elementList.Count, "Element count is incorrect."); Assert.IsTrue(elementList.Contains(element1), "Element1 not found in list"); Assert.IsTrue(elementList.Contains(element2), "Element2 not found in list"); Assert.IsTrue(elementList.Contains(source), "Source not found in list"); elementList = document.CollapseChildren<XliffElement>(CollapseScope.TopLevelDescendants); Assert.AreEqual(0, elementList.Count, "Element count is incorrect."); elementList = document.CollapseChildren<XliffElement>(CollapseScope.TopLevelDescendants | CollapseScope.CoreElements); Assert.AreEqual(1, elementList.Count, "Element count is incorrect."); Assert.IsTrue(elementList.Contains(document.Files[0]), "File not found in list"); }
public void XliffElement_CollapseChildrenWithScope() { IExtensible extensible; CustomExtension extension1; CustomExtension extension2; CustomElement1 element1; CustomElement2 element2; List <XliffElement> elementList; List <Source> sourceList; PlainText text; Segment segment; Source source; StandaloneCode code; Unit unit; XliffDocument document; code = new StandaloneCode(); text = new PlainText(); segment = new Segment(); segment.Source = new Source(); segment.Source.Text.Add(text); segment.Source.Text.Add(code); unit = new Unit(); unit.Resources.Add(segment); document = new XliffDocument(); document.Files.Add(new File()); document.Files[0].Containers.Add(unit); extensible = document.Files[0]; extension1 = new CustomExtension("namespace"); element1 = new CustomElement1("pre1", "namespace"); extension1.AddChild(new ElementInfo(new XmlNameInfo("localname"), element1)); extensible.Extensions.Add(extension1); extension2 = new CustomExtension("namespace"); element2 = new CustomElement2("pre1", "namespace"); extension2.AddChild(new ElementInfo(new XmlNameInfo("localname"), element2)); source = new Source(); extension2.AddChild(new ElementInfo(new XmlNameInfo("localname"), source)); extensible.Extensions.Add(extension2); Console.WriteLine("Test with none."); elementList = document.CollapseChildren <XliffElement>(CollapseScope.None); Assert.AreEqual(0, elementList.Count, "Element count is incorrect."); Console.WriteLine("Test with current element."); elementList = document.CollapseChildren <XliffElement>(CollapseScope.CurrentElement); Assert.AreEqual(1, elementList.Count, "Element count is incorrect."); Assert.IsTrue(elementList.Contains(document), "Document not found in list"); Console.WriteLine("Test with default."); elementList = document.CollapseChildren <XliffElement>(CollapseScope.Default); Assert.AreEqual(6, elementList.Count, "Element count is incorrect."); Assert.IsTrue(elementList.Contains(code), "StandaloneCode not found in list"); Assert.IsFalse(elementList.Contains(document), "Document not found in list"); Assert.IsTrue(elementList.Contains(document.Files[0]), "File not found in list"); Assert.IsTrue(elementList.Contains(segment), "Segment not found in list"); Assert.IsTrue(elementList.Contains(segment.Source), "Source not found in list"); Assert.IsTrue(elementList.Contains(text), "PlainText not found in list"); Assert.IsTrue(elementList.Contains(unit), "Unit not found in list"); Console.WriteLine("Test with default with current element."); elementList = document.CollapseChildren <XliffElement>(CollapseScope.Default | CollapseScope.CurrentElement); Assert.AreEqual(7, elementList.Count, "Element count is incorrect."); Assert.IsTrue(elementList.Contains(document), "Document not found in list"); Assert.IsTrue(elementList.Contains(code), "StandaloneCode not found in list"); Assert.IsTrue(elementList.Contains(document.Files[0]), "File not found in list"); Assert.IsTrue(elementList.Contains(segment), "Segment not found in list"); Assert.IsTrue(elementList.Contains(segment.Source), "Source not found in list"); Assert.IsTrue(elementList.Contains(text), "PlainText not found in list"); Assert.IsTrue(elementList.Contains(unit), "Unit not found in list"); elementList = document.CollapseChildren <XliffElement>(CollapseScope.Extensions | CollapseScope.CoreElements | CollapseScope.AllDescendants); Assert.AreEqual(9, elementList.Count, "Element count is incorrect."); Assert.IsTrue(elementList.Contains(code), "StandaloneCode not found in list"); Assert.IsTrue(elementList.Contains(document.Files[0]), "File not found in list"); Assert.IsTrue(elementList.Contains(segment), "Segment not found in list"); Assert.IsTrue(elementList.Contains(segment.Source), "Source not found in list"); Assert.IsTrue(elementList.Contains(text), "PlainText not found in list"); Assert.IsTrue(elementList.Contains(unit), "Unit not found in list"); Assert.IsTrue(elementList.Contains(source), "Source not found in list"); Assert.IsTrue(elementList.Contains(element1), "Element1 not found in list"); Assert.IsTrue(elementList.Contains(element2), "Element2 not found in list"); sourceList = document.CollapseChildren <Source>(CollapseScope.Extensions | CollapseScope.CoreElements | CollapseScope.AllDescendants); Assert.AreEqual(2, sourceList.Count, "Element count is incorrect."); Assert.IsTrue(sourceList.Contains(segment.Source), "Source not found in list"); Assert.IsTrue(sourceList.Contains(source), "Source not found in list"); elementList = document.CollapseChildren <XliffElement>(CollapseScope.All); Assert.AreEqual(10, elementList.Count, "Element count is incorrect."); Assert.IsTrue(elementList.Contains(document), "Document not found in list"); Assert.IsTrue(elementList.Contains(code), "StandaloneCode not found in list"); Assert.IsTrue(elementList.Contains(document.Files[0]), "File not found in list"); Assert.IsTrue(elementList.Contains(segment), "Segment not found in list"); Assert.IsTrue(elementList.Contains(segment.Source), "Source not found in list"); Assert.IsTrue(elementList.Contains(text), "PlainText not found in list"); Assert.IsTrue(elementList.Contains(unit), "Unit not found in list"); Assert.IsTrue(elementList.Contains(source), "Source not found in list"); Assert.IsTrue(elementList.Contains(element1), "Element1 not found in list"); Assert.IsTrue(elementList.Contains(element2), "Element2 not found in list"); sourceList = document.CollapseChildren <Source>(CollapseScope.All); Assert.AreEqual(2, sourceList.Count, "Element count is incorrect."); Assert.IsTrue(sourceList.Contains(source), "Source not found in list"); Assert.IsTrue(sourceList.Contains(segment.Source), "Source not found in list"); Console.WriteLine("Test with extensions with core elements for Source."); sourceList = document.CollapseChildren <Source>(CollapseScope.Extensions | CollapseScope.AllDescendants); Assert.AreEqual(1, sourceList.Count, "Element count is incorrect."); Assert.IsTrue(sourceList.Contains(source), "Source not found in list"); Console.WriteLine("Test with extensions without core elements."); elementList = document.CollapseChildren <XliffElement>(CollapseScope.Extensions | CollapseScope.AllDescendants); Assert.AreEqual(3, elementList.Count, "Element count is incorrect."); Assert.IsTrue(elementList.Contains(element1), "Element1 not found in list"); Assert.IsTrue(elementList.Contains(element2), "Element2 not found in list"); Assert.IsTrue(elementList.Contains(source), "Source not found in list"); elementList = document.CollapseChildren <XliffElement>(CollapseScope.TopLevelDescendants); Assert.AreEqual(0, elementList.Count, "Element count is incorrect."); elementList = document.CollapseChildren <XliffElement>(CollapseScope.TopLevelDescendants | CollapseScope.CoreElements); Assert.AreEqual(1, elementList.Count, "Element count is incorrect."); Assert.IsTrue(elementList.Contains(document.Files[0]), "File not found in list"); }