// findNestedElement uses reflection to find the element corresponding to the dot-separated string parameter.
968476
func findNestedElement(s string, c interface{}) (reflect.Value, error) {
	fields := strings.Split(s, ".")

	// Take the ValueOf to get a pointer, so we can actually mutate the element.
	e := reflect.Indirect(reflect.ValueOf(c).Elem())

	for _, field := range fields {
		e = reflect.Indirect(e.FieldByName(field))

		// FieldByName returns the zero value if the field does not exist.
		if e == (reflect.Value{}) {
			return e, fmt.Errorf("Unable to find field by name: %s", field)
		}
		// Start the loop again, on the next level.
	}
	return e, nil
}
818847
func (c *Container) AddElement(el Element) {
	c.Body = append(c.Body, el)
}
379542
func FindSegment(a []*SeriesSegment, id uint16) *SeriesSegment {
	for _, segment := range a {
		if segment.id == id {
			return segment
		}
	}
	return nil
}
827911
func (n NameIDFormat) Element() *etree.Element {
	el := etree.NewElement("")
	el.SetText(string(n))
	return el
}
1022126
func FindAndSet(path string, c interface{}, value string) error {
	elem, err := findNestedElement(path, c)
	if err != nil {
		return err
	}
	return setElement(elem, value)
}
1142282
func (ctx *textifyTraverseContext) handleTableElement(node *html.Node) error {
	if !ctx.options.PrettyTables {
		panic("handleTableElement invoked when PrettyTables not active")
	}

	switch node.DataAtom {
	case atom.Table:
		if err := ctx.emit("\n\n"); err != nil {
			return err
		}

		// Re-intialize all table context.
		ctx.tableCtx.init()

		// Browse children, enriching context with table data.
		if err := ctx.traverseChildren(node); err != nil {
			return err
		}

		buf := &bytes.Buffer{}
		table := tablewriter.NewWriter(buf)
		if ctx.options.PrettyTablesOptions != nil {
			options := ctx.options.PrettyTablesOptions
			table.SetAutoFormatHeaders(options.AutoFormatHeader)
			table.SetAutoWrapText(options.AutoWrapText)
			table.SetReflowDuringAutoWrap(options.ReflowDuringAutoWrap)
			table.SetColWidth(options.ColWidth)
			table.SetColumnSeparator(options.ColumnSeparator)
			table.SetRowSeparator(options.RowSeparator)
			table.SetCenterSeparator(options.CenterSeparator)
			table.SetHeaderAlignment(options.HeaderAlignment)
			table.SetFooterAlignment(options.FooterAlignment)
			table.SetAlignment(options.Alignment)
			table.SetColumnAlignment(options.ColumnAlignment)
			table.SetNewLine(options.NewLine)
			table.SetHeaderLine(options.HeaderLine)
			table.SetRowLine(options.RowLine)
			table.SetAutoMergeCells(options.AutoMergeCells)
			table.SetBorders(options.Borders)
		}
		table.SetHeader(ctx.tableCtx.header)
		table.SetFooter(ctx.tableCtx.footer)
		table.AppendBulk(ctx.tableCtx.body)

		// Render the table using ASCII.
		table.Render()
		if err := ctx.emit(buf.String()); err != nil {
			return err
		}

		return ctx.emit("\n\n")

	case atom.Tfoot:
		ctx.tableCtx.isInFooter = true
		if err := ctx.traverseChildren(node); err != nil {
			return err
		}
		ctx.tableCtx.isInFooter = false

	case atom.Tr:
		ctx.tableCtx.body = append(ctx.tableCtx.body, []string{})
		if err := ctx.traverseChildren(node); err != nil {
			return err
		}
		ctx.tableCtx.tmpRow++

	case atom.Th:
		res, err := ctx.renderEachChild(node)
		if err != nil {
			return err
		}

		ctx.tableCtx.header = append(ctx.tableCtx.header, res)

	case atom.Td:
		res, err := ctx.renderEachChild(node)
		if err != nil {
			return err
		}

		if ctx.tableCtx.isInFooter {
			ctx.tableCtx.footer = append(ctx.tableCtx.footer, res)
		} else {
			ctx.tableCtx.body[ctx.tableCtx.tmpRow] = append(ctx.tableCtx.body[ctx.tableCtx.tmpRow], res)
		}

	}
	return nil
}
979996
func removeElementFromStringSlice(list []string, elem string) []string {
	for i, e := range list {
		if e == elem {
			return append(list[:i], list[i+1:]...)
		}
	}
	return list
}
674379
func (t *Tab) GetDocument() (*Element, error) {
	docEle, ok := t.getElement(t.GetTopNodeId())
	if !ok {
		return nil, &ElementNotFoundErr{Message: "top document node id not found."}
	}
	return docEle, nil
}
239042
func (k *Key) AddNestedValue(val string) error {
	if !k.s.f.options.AllowNestedValues {
		return errors.New("nested value is not allowed")
	}
	return k.addNestedValue(val)
}
517492
func FindNodes(input *html.Node, match Matcher, callback MatcherCallback) {
	for c := input.FirstChild; c != nil; c = c.NextSibling {
		if match(c.Type, c.DataAtom) {
			callback(c)
		}
		FindNodes(c, match, callback)
	}
}
158793
func (p *parser) addElement() {
	p.addChild(&Node{
		Type:     ElementNode,
		DataAtom: p.tok.DataAtom,
		Data:     p.tok.Data,
		Attr:     p.tok.Attr,
	})
}
637548
func DeepElementsWithTag(e Markup, f string, depth int) []Markup {
	if depth <= 0 {
		return nil
	}

	f = strings.TrimSpace(strings.ToLower(f))

	var found []Markup

	for _, ch := range e.Children() {
		// if che, ok := ch.(*Element); ok {
		if ch.Name() == f {
			found = append(found, ch)
			cfo := DeepElementsWithTag(ch, f, depth-1)
			if len(cfo) > 0 {
				found = append(found, cfo...)
			}
		}
		// }
	}

	return found
}
313988
func (tc *TreeCache) findNode(path string) (*TreeNode, error) {
	if !strings.HasPrefix(path, tc.root.path) {
		return nil, ErrRootNotMatch
	}

	path = strings.TrimPrefix(path, tc.root.path)
	current := tc.root
	for _, part := range strings.Split(path, "/") {
		if part == "" {
			continue
		}
		next, exists := current.FindChild(part)
		if !exists {
			return nil, ErrNodeNotFound
		}
		current = next
	}

	return current, nil
}
264052
func FindText(input *html.Node) (data string) {
	var buffer string
	for c := input.FirstChild; c != nil; c = c.NextSibling {
		if c.Parent.DataAtom == input.DataAtom && c.Type == html.TextNode {
			buffer = strings.TrimSpace(c.Data)
			if len(buffer) > 0 {
				data += " " + buffer
			}
		}
		FindText(c)
	}
	return strings.TrimSpace(data)
}
631929
func NSFindOneChildCtx(ctx NSContext, el *etree.Element, namespace, tag string) (*etree.Element, error) {
	var found *etree.Element

	err := NSFindChildrenIterateCtx(ctx, el, namespace, tag, func(ctx NSContext, el *etree.Element) error {
		found = el
		return ErrTraversalHalted
	})

	if err != nil && err != ErrTraversalHalted {
		return nil, err
	}

	return found, nil
}
1066620
func (t *Tab) nodeToElement(node *gcdapi.DOMNode) *Element {
	if ele, ok := t.getElement(node.NodeId); ok {
		ele.populateElement(node)
		return ele
	}
	newEle := newReadyElement(t, node)
	return newEle
}
1047726
func (e *Element) populateElement(node *gcdapi.DOMNode) {
	e.lock.Lock()
	e.node = node
	e.id = node.NodeId
	e.nodeType = node.NodeType
	e.nodeName = strings.ToLower(node.NodeName)
	e.childNodeCount = node.ChildNodeCount
	if node.NodeType == int(TEXT_NODE) {
		e.characterData = node.NodeValue
	}

	e.lock.Unlock()

	for i := 0; i < len(node.Attributes); i += 2 {
		e.updateAttribute(node.Attributes[i], node.Attributes[i+1])
	}

	// close it
	if !e.ready {
		close(e.readyGate)
	}
	e.lock.Lock()
	defer e.lock.Unlock()
	e.ready = true
}